Skip to content

Commit

Permalink
Native action sysio.roa reducepolicy implemented
Browse files Browse the repository at this point in the history
Implmented sysio.roas native action 'reducepolicy' and all pieces
necessary for that.

Replaced userres and got roa usage tracking added

Replaced userres in chain_plugin to reference sysio.roa's reslimit table.
Adjusted the flow of resource usage tracking:
    RAM isn't charged during the action play through instead incrementing our new total_ram_usage
    In finalize we do our check to determine who to charge contract or caller where we use
    total_ram_usage to charge the appropriate person.
Cleaner overall flow and maintains Leap resource management architecture.

Also added a check, to not allow nodeowners to remove the sysio or sysio.* policies that are
established on registration as these are system requirements. They can expand them but not
lower them.

Modified newaccount so only sysio can call it.
sysio is charged for resources used for account creation.
all accounts are created with no resources.
Removed delegatebw and buyram from account creation.

Fixed finalize() fallback logic

Fixed an issue in finalize() logic when it checks contracts resources.
Logic was incorrect for measuring accounts with unlimited CPU / NET.

Fixed fallback edge case

There was an edge case where contract == user and so it would SYS_ASSERT
instead since that is to be expected sometimes. We attempt to charge the contract
and if it doesn't have enough we reject without trying the user.

modified the "free resources" check to accomodate bios boot.
toggles after block 200.

Fixed chain plugin get_account

Previous edits didn't fully adjust to account for our sysio.roa resource management changes
It now gets and populates properly. Resolved unlimited CPU / NET on clio get account
  • Loading branch information
dtaghavi committed Dec 13, 2024
1 parent daedb7d commit 889a509
Show file tree
Hide file tree
Showing 14 changed files with 758 additions and 220 deletions.
10 changes: 10 additions & 0 deletions libraries/chain/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <sysio/chain/global_property_object.hpp>
#include <sysio/chain/protocol_state_object.hpp>
#include <sysio/chain/contract_table_objects.hpp>
#include <sysio/chain/sysio_roa_objects.hpp> // **Roa Change** Added for db initialization of sysio.roa tables.
#include <sysio/chain/generated_transaction_object.hpp>
#include <sysio/chain/transaction_object.hpp>
#include <sysio/chain/genesis_intrinsics.hpp>
Expand Down Expand Up @@ -334,6 +335,8 @@ struct controller_impl {
wasmif.current_lib(bsp->block_num);
});




#define SET_APP_HANDLER( receiver, contract, action) \
set_apply_handler( account_name(#receiver), account_name(#contract), action_name(#action), \
Expand All @@ -346,6 +349,8 @@ struct controller_impl {
SET_APP_HANDLER( sysio, sysio, deleteauth );
SET_APP_HANDLER( sysio, sysio, linkauth );
SET_APP_HANDLER( sysio, sysio, unlinkauth );
// **Roa Change** Handler for native action reducepolicy
SET_APP_HANDLER( sysio.roa, roa, reducepolicy );
/*
SET_APP_HANDLER( sysio, sysio, postrecovery );
SET_APP_HANDLER( sysio, sysio, passrecovery );
Expand Down Expand Up @@ -984,6 +989,11 @@ struct controller_impl {
authorization.initialize_database();
resource_limits.initialize_database();

// **Roa Changes** New sysio.roa contract multi indexes for resource management.
db.add_index<sysio::chain::nodeowners_index>();
db.add_index<sysio::chain::policies_index>();
db.add_index<sysio::chain::reslimit_index>();

authority system_auth(genesis.initial_key);
create_native_account( genesis.initial_timestamp, config::system_account_name, system_auth, system_auth, true );

Expand Down
22 changes: 22 additions & 0 deletions libraries/chain/include/sysio/chain/contract_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <sysio/chain/authority.hpp>
#include <sysio/chain/types.hpp>
#include <sysio/chain/asset.hpp> // Included for reducepolicy

namespace sysio { namespace chain {

Expand Down Expand Up @@ -152,6 +153,24 @@ struct onerror {
}
};

// **Roa change** to facilitate native action
struct reducepolicy {
account_name owner;
account_name issuer;
asset net_weight;
asset cpu_weight;
asset ram_weight;
uint8_t network_gen;

static account_name get_account() {
return "sysio.roa"_n; // The account this contract is deployed to.
}

static action_name get_name() {
return "reducepolicy"_n; // The action name.
}
};

} } /// namespace sysio::chain

FC_REFLECT( sysio::chain::newaccount , (creator)(name)(owner)(active) )
Expand All @@ -163,3 +182,6 @@ FC_REFLECT( sysio::chain::linkauth , (account)(code)(typ
FC_REFLECT( sysio::chain::unlinkauth , (account)(code)(type) )
FC_REFLECT( sysio::chain::canceldelay , (canceling_auth)(trx_id) )
FC_REFLECT( sysio::chain::onerror , (sender_id)(sent_trx) )
// **Roa change**
FC_REFLECT( sysio::chain::reducepolicy , (owner)(issuer)(net_weight)(cpu_weight)(ram_weight)(network_gen))

1 change: 0 additions & 1 deletion libraries/chain/include/sysio/chain/controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,6 @@ namespace sysio { namespace chain {
chainbase::database& mutable_db()const;

std::unique_ptr<controller_impl> my;

};

} } /// sysio::chain
4 changes: 2 additions & 2 deletions libraries/chain/include/sysio/chain/resource_limits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ namespace sysio { namespace chain {
public:

explicit resource_limits_manager(chainbase::database& db, std::function<deep_mind_handler*()> get_deep_mind_logger)
:_db(db),_get_deep_mind_logger(get_deep_mind_logger)
:_db(db), _get_deep_mind_logger(get_deep_mind_logger)
{
}

Expand Down Expand Up @@ -115,4 +115,4 @@ namespace sysio { namespace chain {

FC_REFLECT( sysio::chain::resource_limits::account_resource_limit, (used)(available)(max) )
FC_REFLECT( sysio::chain::resource_limits::ratio, (numerator)(denominator))
FC_REFLECT( sysio::chain::resource_limits::elastic_limit_parameters, (target)(max)(periods)(max_multiplier)(contract_rate)(expand_rate))
FC_REFLECT( sysio::chain::resource_limits::elastic_limit_parameters, (target)(max)(periods)(max_multiplier)(contract_rate)(expand_rate))
2 changes: 2 additions & 0 deletions libraries/chain/include/sysio/chain/sysio_contract.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ namespace sysio { namespace chain {
void apply_sysio_deleteauth(apply_context&);
void apply_sysio_linkauth(apply_context&);
void apply_sysio_unlinkauth(apply_context&);
// **Roa changes**
void apply_roa_reducepolicy(apply_context&);

/*
void apply_sysio_postrecovery(apply_context&);
Expand Down
129 changes: 129 additions & 0 deletions libraries/chain/include/sysio/chain/sysio_roa_objects.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#pragma once
#include <sysio/chain/types.hpp>
#include <sysio/chain/name.hpp>
#include <sysio/chain/asset.hpp>
#include <chainbase/chainbase.hpp>
#include <fc/reflect/reflect.hpp>

/**
* @brief This file represents the structure of the system contract sysio.roa which replaces the old resource management system.
* The tables defined here MUST match the structure in the system contract ( sysio.roa.hpp ).
*/
namespace sysio { namespace chain {

// Note: The object_type IDs below start at 200 to avoid conflicts with core SYSIO object types.
// Check sysio/chain/types.hpp for core definitions.
// Do not modify sysio/chain/types.hpp. Keep custom object types in separate files.
enum sysio_roa_object_type {
nodeowners_object_type = 200,
policies_object_type,
reslimit_object_type
};

class nodeowners_object : public chainbase::object<nodeowners_object_type, nodeowners_object> {
CHAINBASE_DEFAULT_CONSTRUCTOR(nodeowners_object)

public:
id_type id;
name owner; // Node Owner's account name.
uint8_t tier;
asset total_sys;
asset allocated_sys;
asset allocated_bw;
asset allocated_ram;
uint8_t network_gen; // Added field for scoping

// Primary key for chainbase
id_type primary_key() const { return id; }

// Accessors for indexing by (network_gen, owner)
name get_owner() const { return owner; }
uint8_t get_network_gen() const { return network_gen; }

// No need for get_tier() since we are not indexing or filtering by tier in native code
};

struct by_network_gen_owner;

using nodeowners_index = chainbase::shared_multi_index_container<
nodeowners_object,
indexed_by<
ordered_unique<tag<by_id>, member<nodeowners_object, nodeowners_object::id_type, &nodeowners_object::id>>,
// Composite key using network_gen and owner for scoping
ordered_unique<tag<by_network_gen_owner>,
composite_key<nodeowners_object,
const_mem_fun<nodeowners_object, uint8_t, &nodeowners_object::get_network_gen>,
const_mem_fun<nodeowners_object, name, &nodeowners_object::get_owner>
>
>
>
>;

class policies_object : public chainbase::object<policies_object_type, policies_object> {
CHAINBASE_DEFAULT_CONSTRUCTOR(policies_object)

public:
id_type id;
name owner; // The account this policy applies to.
name issuer; // The Node Owner who issued this policy.
asset net_weight;
asset cpu_weight;
asset ram_weight;
uint64_t bytes_per_unit;
uint32_t time_block;

id_type primary_key() const { return id; }
name get_owner() const { return owner; }
name get_issuer() const { return issuer; }
};

struct by_issuer_owner;
// struct by_owner; // Remove if not used

using policies_index = chainbase::shared_multi_index_container<
policies_object,
indexed_by<
ordered_unique<tag<by_id>, member<policies_object, policies_object::id_type, &policies_object::id>>,
ordered_unique<tag<by_issuer_owner>,
composite_key<policies_object,
const_mem_fun<policies_object, name, &policies_object::get_issuer>,
const_mem_fun<policies_object, name, &policies_object::get_owner>
>
>
>
>;

class reslimit_object : public chainbase::object<reslimit_object_type, reslimit_object> {
CHAINBASE_DEFAULT_CONSTRUCTOR(reslimit_object)

public:
id_type id;
name owner;
asset net_weight;
asset cpu_weight;
uint64_t ram_bytes;

id_type primary_key() const { return id; }
name get_owner() const { return owner; }
};

struct by_reslimit_owner;

using reslimit_index = chainbase::shared_multi_index_container<
reslimit_object,
indexed_by<
ordered_unique<tag<by_id>, member<reslimit_object, reslimit_object::id_type, &reslimit_object::id>>,
ordered_unique<tag<by_reslimit_owner>, const_mem_fun<reslimit_object, name, &reslimit_object::get_owner>>
>
>;

}} // namespace sysio::chain

CHAINBASE_SET_INDEX_TYPE(sysio::chain::nodeowners_object, sysio::chain::nodeowners_index)
CHAINBASE_SET_INDEX_TYPE(sysio::chain::policies_object, sysio::chain::policies_index)
CHAINBASE_SET_INDEX_TYPE(sysio::chain::reslimit_object, sysio::chain::reslimit_index)

FC_REFLECT(sysio::chain::nodeowners_object, (id)(owner)(tier)(total_sys)(allocated_sys)(allocated_bw)(allocated_ram)(network_gen))
FC_REFLECT(sysio::chain::policies_object, (id)(owner)(issuer)(net_weight)(cpu_weight)(ram_weight)(bytes_per_unit)(time_block))
FC_REFLECT(sysio::chain::reslimit_object, (id)(owner)(net_weight)(cpu_weight)(ram_bytes))

3 changes: 3 additions & 0 deletions libraries/chain/include/sysio/chain/transaction_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ namespace sysio { namespace chain {
int64_t billing_timer_exception_code = block_cpu_usage_exceeded::code_value;
fc::time_point pseudo_start;
fc::microseconds billed_time;

// Roa Change: Store total RAM usage accumulated during actions here. transaction_context::finalize() will use to finalize charges.
int64_t total_ram_usage = 0;
};

} }
2 changes: 2 additions & 0 deletions libraries/chain/include/sysio/chain/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ namespace sysio { namespace chain {
*
* UNUSED_ enums can be taken for new purposes but otherwise the offsets
* in this enumeration are potentially shared_memory breaking
*
* Note: sysio_roa_objects.hpp defines usage of: 200 - 202
*/
enum object_type
{
Expand Down
4 changes: 2 additions & 2 deletions libraries/chain/resource_limits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <sysio/chain/deep_mind.hpp>
#include <boost/tuple/tuple_io.hpp>
#include <sysio/chain/database_utils.hpp>
#include <sysio/chain/account_object.hpp>
#include <algorithm>

namespace sysio { namespace chain { namespace resource_limits {
Expand Down Expand Up @@ -240,7 +241,6 @@ int64_t resource_limits_manager::get_account_ram_usage( const account_name& name
return _db.get<resource_usage_object,by_owner>( name ).ram_usage;
}


bool resource_limits_manager::set_account_limits( const account_name& account, int64_t ram_bytes, int64_t net_weight, int64_t cpu_weight) {
//const auto& usage = _db.get<resource_usage_object,by_owner>( account );
/*
Expand Down Expand Up @@ -516,4 +516,4 @@ std::pair<account_resource_limit, bool> resource_limits_manager::get_account_net
return {arl, greylisted};
}

} } } /// sysio::chain::resource_limits
} } } /// sysio::chain::resource_limits
Loading

0 comments on commit 889a509

Please sign in to comment.