From 0ff22d27bef3e37f3efc156cec4e6b3be6bf6aca Mon Sep 17 00:00:00 2001 From: marek Date: Fri, 9 Nov 2018 09:55:22 +0100 Subject: [PATCH 1/4] - add parameter evaluated on contract side to `reward_all`; - `distribution::execute` now call native function `reward_all`; - `apply_context::reward_all` implementation; - `check_data` now get resource limits from `resource_limit_mgr`. --- contracts/beoslib/beos_privileged.h | 2 +- .../eosio.distribution/eosio.distribution.cpp | 17 ++-- libraries/chain/apply_context.cpp | 82 +++++++++++++++++++ .../include/eosio/chain/apply_context.hpp | 14 ++++ libraries/chain/wasm_interface.cpp | 23 ++++-- unittests/eosio.interchain_tests.cpp | 19 ++++- 6 files changed, 143 insertions(+), 14 deletions(-) diff --git a/contracts/beoslib/beos_privileged.h b/contracts/beoslib/beos_privileged.h index 0ab5b0bafc4..39a5ca0a6f1 100644 --- a/contracts/beoslib/beos_privileged.h +++ b/contracts/beoslib/beos_privileged.h @@ -17,7 +17,7 @@ extern "C" { /** * Defined in distribution_api */ - void reward_all(uint32_t, const void*, int, bool); + void reward_all(uint32_t, uint64_t, const void*, int, bool); void reward_done(const void*, int, bool); #ifdef __cplusplus diff --git a/contracts/eosio.distribution/eosio.distribution.cpp b/contracts/eosio.distribution/eosio.distribution.cpp index 5bb322a52a5..5722e3aadfa 100644 --- a/contracts/eosio.distribution/eosio.distribution.cpp +++ b/contracts/eosio.distribution/eosio.distribution.cpp @@ -15,8 +15,9 @@ distribution::~distribution() { } -void distribution::execute( uint32_t block_nr, asset proxy_asset, uint32_t starting_block_for_any_distribution, uint32_t ending_block_for_any_distribution, - uint32_t distribution_payment_block_interval_for_any_distribution, uint32_t nr_items, bool is_beos_mode ) +void distribution::execute( uint32_t block_nr, asset proxy_asset, + uint32_t starting_block_for_any_distribution, uint32_t ending_block_for_any_distribution, + uint32_t distribution_payment_block_interval_for_any_distribution, uint32_t nr_items, bool is_beos_mode ) { //Only during a distribution period, an action can be called. if( block_nr >= starting_block_for_any_distribution && block_nr <= ending_block_for_any_distribution ) @@ -25,8 +26,12 @@ void distribution::execute( uint32_t block_nr, asset proxy_asset, uint32_t start if( ( ( block_nr - starting_block_for_any_distribution ) % distribution_payment_block_interval_for_any_distribution ) == 0 ) { //Rewarding all accounts. - rewardall( nr_items, proxy_asset, is_beos_mode ); - //reward_all( nr_items, &proxy_asset, sizeof(asset), is_beos_mode ); + //rewardall( nr_items, proxy_asset, is_beos_mode ); + + uint64_t gathered_amount = get_sum(); + + if (gathered_amount != 0) + reward_all( nr_items, gathered_amount, &proxy_asset, sizeof(asset), is_beos_mode ); //Total end of distribution period. Transferring from staked BEOS/RAM to liquid BEOS/RAM. //It depends on `is_beos_mode` variable. @@ -35,8 +40,8 @@ void distribution::execute( uint32_t block_nr, asset proxy_asset, uint32_t start ( block_nr + distribution_payment_block_interval_for_any_distribution > ending_block_for_any_distribution ) ) { - rewarddone( proxy_asset, is_beos_mode ); - //reward_done( &proxy_asset, sizeof(asset), is_beos_mode ); + //rewarddone( proxy_asset, is_beos_mode ); + reward_done( &proxy_asset, sizeof(asset), is_beos_mode ); } } } diff --git a/libraries/chain/apply_context.cpp b/libraries/chain/apply_context.cpp index de1450013d8..03fe3ad2c8d 100644 --- a/libraries/chain/apply_context.cpp +++ b/libraries/chain/apply_context.cpp @@ -110,6 +110,88 @@ void apply_context::finalize_trace( action_trace& trace, const fc::time_point& s trace.elapsed = fc::time_point::now() - start; } +void apply_context::reward_stake( const account_name& account, int64_t val ) +{ + auto& resource_limit_mgr = control.get_mutable_resource_limits_manager(); + int64_t ram_bytes = 0; + int64_t net_weight = 0; + int64_t cpu_weight = 0; + + resource_limit_mgr.get_account_limits( account, ram_bytes, net_weight, cpu_weight ); + + int64_t stake_net = val / 2; + int64_t stake_cpu = val - stake_net; + + net_weight += stake_net; + cpu_weight += stake_cpu; + + // [MK]: ram stay unchanged, so no need to check? + //if (resource_limit_mgr.set_account_limits( name, ram_bytes, net_weight, cpu_weight )) + //trx_context.validate_ram_usage.insert( name ); + bool need_validation = resource_limit_mgr.set_account_limits( account, ram_bytes, net_weight, cpu_weight ); + EOS_ASSERT( need_validation == false, + resource_limit_exception, + "new ram_bytes limit cannot be more restrictive than the previously set one (account '{account}')", ("account", account) ); +} + +void apply_context::reward_ram( const account_name& account, int64_t val ) +{ + auto& resource_limit_mgr = control.get_mutable_resource_limits_manager(); + int64_t ram_bytes = 0; + int64_t net_weight = 0; + int64_t cpu_weight = 0; + + resource_limit_mgr.get_account_limits( account, ram_bytes, net_weight, cpu_weight ); + ram_bytes += val; + + if (resource_limit_mgr.set_account_limits( account, ram_bytes, net_weight, cpu_weight )) + trx_context.validate_ram_usage.insert( account ); +} + +void apply_context::reward_all( uint32_t block_nr, uint64_t gathered_amount, asset sym, bool is_beos_mode ) +{ + auto& resource_limit_mgr = control.get_mutable_resource_limits_manager(); + const auto& accounts_index = db.template get_index(); + + auto get_currency_balance = [this]( const account_name& name, const symbol& asset_symbol ) -> asset + { + const auto* tbl = db.template find(std::make_tuple(N(eosio.token), name, N(accounts))); + share_type result = 0; + + // the balance is implied to be 0 if either the table or row does not exist + if (tbl) { + const auto *obj = db.template find(std::make_tuple(tbl->id, asset_symbol.to_symbol_code().value)); + if (obj) { + //balance is the first field in the serialization + fc::datastream ds(obj->value.data(), obj->value.size()); + fc::raw::unpack(ds, result); + } + } + return asset(result, asset_symbol); + }; + + for (auto& account : accounts_index) + { + auto& name = account.name; + asset balance( get_currency_balance(name, sym.get_symbol()) ); + //Calculation ratio for given account. + long double ratio = static_cast( balance.get_amount() ) / gathered_amount; + int64_t val = static_cast( block_nr * ratio ); + + if (val <= 0) + continue; + + if (is_beos_mode) + reward_stake( name, val ); + else + reward_ram( name, val ); + } +} + +void apply_context::reward_done( asset symbol, bool is_beos_mode ) +{ +} + void apply_context::exec( action_trace& trace ) { _notified.push_back(receiver); diff --git a/libraries/chain/include/eosio/chain/apply_context.hpp b/libraries/chain/include/eosio/chain/apply_context.hpp index a253d950358..633e505ab40 100644 --- a/libraries/chain/include/eosio/chain/apply_context.hpp +++ b/libraries/chain/include/eosio/chain/apply_context.hpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -575,6 +576,19 @@ class apply_context { void add_ram_usage( account_name account, int64_t ram_delta ); void finalize_trace( action_trace& trace, const fc::time_point& start ); + /// Distribution methods: + public: + + void reward_all( uint32_t block_nr, uint64_t gathered_amount, + asset symbol/*correct symbol of BEOS coin, for example: `0.0000 BEOS`*/, + bool is_beos_mode ); + void reward_done( asset symbol/*correct symbol of BEOS coin, for example: `0.0000 BEOS`*/, + bool is_beos_mode ); + + private: + void reward_stake( const account_name& account, int64_t val ); + void reward_ram( const account_name& account, int64_t val ); + private: void validate_referenced_accounts( const transaction& t )const; diff --git a/libraries/chain/wasm_interface.cpp b/libraries/chain/wasm_interface.cpp index 9d7d1aba429..0b3d5fcf04f 100644 --- a/libraries/chain/wasm_interface.cpp +++ b/libraries/chain/wasm_interface.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -1673,23 +1674,35 @@ class distribution_api : public context_aware_api { distribution_api( apply_context& ctx ) : context_aware_api( ctx, true ) {} - void reward_all( uint32_t total_amount, + void reward_all( uint32_t block_nr, uint64_t gathered_amount, array_ptr symbol, size_t symbol_len, //asset symbol/*correct symbol of BEOS coin, for example: `0.0000 BEOS`*/, bool is_beos_mode ) { - ilog( "From inside reward_all! total_amount == ${n}", ("n", total_amount) ); + //elog( "From inside reward_all! block_nr == ${n}, gathered_amount = ${a}", ("n", block_nr) ("a", gathered_amount) ); + + datastream ds( symbol, symbol_len ); + asset _symbol; + fc::raw::unpack(ds, _symbol); + + context.reward_all( block_nr, gathered_amount, _symbol, is_beos_mode ); } void reward_done( array_ptr symbol, size_t symbol_len, //asset symbol/*correct symbol of BEOS coin, for example: `0.0000 BEOS`*/, bool is_beos_mode ) { - idump(("from inside reward_done!")); + //edump(("from inside reward_done!")); + + datastream ds( symbol, symbol_len ); + asset _symbol; + fc::raw::unpack(ds, _symbol); + + context.reward_done( _symbol, is_beos_mode ); } }; REGISTER_INTRINSICS( distribution_api, - (reward_all, void(int,int,int,int)) - (reward_done, void(int,int,int)) + (reward_all, void(int, int64_t, int, int, int) ) + (reward_done, void(int, int, int) ) ); REGISTER_INJECTED_INTRINSICS(call_depth_api, diff --git a/unittests/eosio.interchain_tests.cpp b/unittests/eosio.interchain_tests.cpp index 2affe1cd304..1688f0ca0c9 100644 --- a/unittests/eosio.interchain_tests.cpp +++ b/unittests/eosio.interchain_tests.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include @@ -242,7 +243,21 @@ class eosio_interchain_tester : public actions fc::variant check_data( account_name acc ) { asset balance = get_balance( acc, "PROXY" ); - vector data = get_row_by_account( config::system_account_name, acc, N(userres), acc ); + const auto& resource_limit_mgr = control->get_resource_limits_manager(); + int64_t ram_bytes = 0; + int64_t net_weight = 0; + int64_t cpu_weight = 0; + + resource_limit_mgr.get_account_limits( acc, ram_bytes, net_weight, cpu_weight ); + + asset a_total_net_cpu( net_weight + cpu_weight ); + + return mvo() + ( "balance", balance ) + ( "staked_balance", a_total_net_cpu ) + ( "staked_ram", ram_bytes ); + + /*vector data = get_row_by_account( config::system_account_name, acc, N(userres), acc ); if( data.empty() ) { @@ -264,7 +279,7 @@ class eosio_interchain_tester : public actions ( "balance", balance ) ( "staked_balance", total_net_cpu ) ( "staked_ram", ram_bytes ); - } + }*/ } transaction_trace_ptr create_account_with_resources( account_name creator, account_name a, int64_t bytes = DEFAULT_RAM ) { From e324d24e2ae95a0cfe811e5bcd8c8243acf55f21 Mon Sep 17 00:00:00 2001 From: marek Date: Fri, 9 Nov 2018 09:55:22 +0100 Subject: [PATCH 2/4] - add parameter evaluated on contract side to `reward_all`; - `distribution::execute` now call native function `reward_all`; - `apply_context::reward_all` implementation; - `check_data` now get resource limits from `resource_limit_mgr`. --- contracts/beoslib/beos_privileged.h | 2 +- .../eosio.distribution/eosio.distribution.cpp | 17 ++-- libraries/chain/apply_context.cpp | 82 +++++++++++++++++++ .../include/eosio/chain/apply_context.hpp | 14 ++++ libraries/chain/wasm_interface.cpp | 23 ++++-- unittests/eosio.interchain_tests.cpp | 19 ++++- 6 files changed, 143 insertions(+), 14 deletions(-) diff --git a/contracts/beoslib/beos_privileged.h b/contracts/beoslib/beos_privileged.h index 0ab5b0bafc4..39a5ca0a6f1 100644 --- a/contracts/beoslib/beos_privileged.h +++ b/contracts/beoslib/beos_privileged.h @@ -17,7 +17,7 @@ extern "C" { /** * Defined in distribution_api */ - void reward_all(uint32_t, const void*, int, bool); + void reward_all(uint32_t, uint64_t, const void*, int, bool); void reward_done(const void*, int, bool); #ifdef __cplusplus diff --git a/contracts/eosio.distribution/eosio.distribution.cpp b/contracts/eosio.distribution/eosio.distribution.cpp index fac67f7c9bf..3d79c81357d 100644 --- a/contracts/eosio.distribution/eosio.distribution.cpp +++ b/contracts/eosio.distribution/eosio.distribution.cpp @@ -15,8 +15,9 @@ distribution::~distribution() { } -void distribution::execute( uint32_t block_nr, asset proxy_asset, uint32_t starting_block_for_any_distribution, uint32_t ending_block_for_any_distribution, - uint32_t distribution_payment_block_interval_for_any_distribution, uint32_t nr_items, bool is_beos_mode ) +void distribution::execute( uint32_t block_nr, asset proxy_asset, + uint32_t starting_block_for_any_distribution, uint32_t ending_block_for_any_distribution, + uint32_t distribution_payment_block_interval_for_any_distribution, uint32_t nr_items, bool is_beos_mode ) { //Only during a distribution period, an action can be called. if( block_nr >= starting_block_for_any_distribution && block_nr <= ending_block_for_any_distribution ) @@ -25,8 +26,12 @@ void distribution::execute( uint32_t block_nr, asset proxy_asset, uint32_t start if( ( ( block_nr - starting_block_for_any_distribution ) % distribution_payment_block_interval_for_any_distribution ) == 0 ) { //Rewarding all accounts. - rewardall( nr_items, proxy_asset, is_beos_mode ); - //reward_all( nr_items, &proxy_asset, sizeof(asset), is_beos_mode ); + //rewardall( nr_items, proxy_asset, is_beos_mode ); + + uint64_t gathered_amount = get_sum(); + + if (gathered_amount != 0) + reward_all( nr_items, gathered_amount, &proxy_asset, sizeof(asset), is_beos_mode ); //Total end of distribution period. Transferring from staked BEOS/RAM to liquid BEOS/RAM. //It depends on `is_beos_mode` variable. @@ -35,8 +40,8 @@ void distribution::execute( uint32_t block_nr, asset proxy_asset, uint32_t start ( block_nr + distribution_payment_block_interval_for_any_distribution > ending_block_for_any_distribution ) ) { - rewarddone( proxy_asset, is_beos_mode ); - //reward_done( &proxy_asset, sizeof(asset), is_beos_mode ); + //rewarddone( proxy_asset, is_beos_mode ); + reward_done( &proxy_asset, sizeof(asset), is_beos_mode ); } } } diff --git a/libraries/chain/apply_context.cpp b/libraries/chain/apply_context.cpp index de1450013d8..03fe3ad2c8d 100644 --- a/libraries/chain/apply_context.cpp +++ b/libraries/chain/apply_context.cpp @@ -110,6 +110,88 @@ void apply_context::finalize_trace( action_trace& trace, const fc::time_point& s trace.elapsed = fc::time_point::now() - start; } +void apply_context::reward_stake( const account_name& account, int64_t val ) +{ + auto& resource_limit_mgr = control.get_mutable_resource_limits_manager(); + int64_t ram_bytes = 0; + int64_t net_weight = 0; + int64_t cpu_weight = 0; + + resource_limit_mgr.get_account_limits( account, ram_bytes, net_weight, cpu_weight ); + + int64_t stake_net = val / 2; + int64_t stake_cpu = val - stake_net; + + net_weight += stake_net; + cpu_weight += stake_cpu; + + // [MK]: ram stay unchanged, so no need to check? + //if (resource_limit_mgr.set_account_limits( name, ram_bytes, net_weight, cpu_weight )) + //trx_context.validate_ram_usage.insert( name ); + bool need_validation = resource_limit_mgr.set_account_limits( account, ram_bytes, net_weight, cpu_weight ); + EOS_ASSERT( need_validation == false, + resource_limit_exception, + "new ram_bytes limit cannot be more restrictive than the previously set one (account '{account}')", ("account", account) ); +} + +void apply_context::reward_ram( const account_name& account, int64_t val ) +{ + auto& resource_limit_mgr = control.get_mutable_resource_limits_manager(); + int64_t ram_bytes = 0; + int64_t net_weight = 0; + int64_t cpu_weight = 0; + + resource_limit_mgr.get_account_limits( account, ram_bytes, net_weight, cpu_weight ); + ram_bytes += val; + + if (resource_limit_mgr.set_account_limits( account, ram_bytes, net_weight, cpu_weight )) + trx_context.validate_ram_usage.insert( account ); +} + +void apply_context::reward_all( uint32_t block_nr, uint64_t gathered_amount, asset sym, bool is_beos_mode ) +{ + auto& resource_limit_mgr = control.get_mutable_resource_limits_manager(); + const auto& accounts_index = db.template get_index(); + + auto get_currency_balance = [this]( const account_name& name, const symbol& asset_symbol ) -> asset + { + const auto* tbl = db.template find(std::make_tuple(N(eosio.token), name, N(accounts))); + share_type result = 0; + + // the balance is implied to be 0 if either the table or row does not exist + if (tbl) { + const auto *obj = db.template find(std::make_tuple(tbl->id, asset_symbol.to_symbol_code().value)); + if (obj) { + //balance is the first field in the serialization + fc::datastream ds(obj->value.data(), obj->value.size()); + fc::raw::unpack(ds, result); + } + } + return asset(result, asset_symbol); + }; + + for (auto& account : accounts_index) + { + auto& name = account.name; + asset balance( get_currency_balance(name, sym.get_symbol()) ); + //Calculation ratio for given account. + long double ratio = static_cast( balance.get_amount() ) / gathered_amount; + int64_t val = static_cast( block_nr * ratio ); + + if (val <= 0) + continue; + + if (is_beos_mode) + reward_stake( name, val ); + else + reward_ram( name, val ); + } +} + +void apply_context::reward_done( asset symbol, bool is_beos_mode ) +{ +} + void apply_context::exec( action_trace& trace ) { _notified.push_back(receiver); diff --git a/libraries/chain/include/eosio/chain/apply_context.hpp b/libraries/chain/include/eosio/chain/apply_context.hpp index a253d950358..633e505ab40 100644 --- a/libraries/chain/include/eosio/chain/apply_context.hpp +++ b/libraries/chain/include/eosio/chain/apply_context.hpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -575,6 +576,19 @@ class apply_context { void add_ram_usage( account_name account, int64_t ram_delta ); void finalize_trace( action_trace& trace, const fc::time_point& start ); + /// Distribution methods: + public: + + void reward_all( uint32_t block_nr, uint64_t gathered_amount, + asset symbol/*correct symbol of BEOS coin, for example: `0.0000 BEOS`*/, + bool is_beos_mode ); + void reward_done( asset symbol/*correct symbol of BEOS coin, for example: `0.0000 BEOS`*/, + bool is_beos_mode ); + + private: + void reward_stake( const account_name& account, int64_t val ); + void reward_ram( const account_name& account, int64_t val ); + private: void validate_referenced_accounts( const transaction& t )const; diff --git a/libraries/chain/wasm_interface.cpp b/libraries/chain/wasm_interface.cpp index 9d7d1aba429..0b3d5fcf04f 100644 --- a/libraries/chain/wasm_interface.cpp +++ b/libraries/chain/wasm_interface.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -1673,23 +1674,35 @@ class distribution_api : public context_aware_api { distribution_api( apply_context& ctx ) : context_aware_api( ctx, true ) {} - void reward_all( uint32_t total_amount, + void reward_all( uint32_t block_nr, uint64_t gathered_amount, array_ptr symbol, size_t symbol_len, //asset symbol/*correct symbol of BEOS coin, for example: `0.0000 BEOS`*/, bool is_beos_mode ) { - ilog( "From inside reward_all! total_amount == ${n}", ("n", total_amount) ); + //elog( "From inside reward_all! block_nr == ${n}, gathered_amount = ${a}", ("n", block_nr) ("a", gathered_amount) ); + + datastream ds( symbol, symbol_len ); + asset _symbol; + fc::raw::unpack(ds, _symbol); + + context.reward_all( block_nr, gathered_amount, _symbol, is_beos_mode ); } void reward_done( array_ptr symbol, size_t symbol_len, //asset symbol/*correct symbol of BEOS coin, for example: `0.0000 BEOS`*/, bool is_beos_mode ) { - idump(("from inside reward_done!")); + //edump(("from inside reward_done!")); + + datastream ds( symbol, symbol_len ); + asset _symbol; + fc::raw::unpack(ds, _symbol); + + context.reward_done( _symbol, is_beos_mode ); } }; REGISTER_INTRINSICS( distribution_api, - (reward_all, void(int,int,int,int)) - (reward_done, void(int,int,int)) + (reward_all, void(int, int64_t, int, int, int) ) + (reward_done, void(int, int, int) ) ); REGISTER_INJECTED_INTRINSICS(call_depth_api, diff --git a/unittests/eosio.interchain_tests.cpp b/unittests/eosio.interchain_tests.cpp index 019b1ebfa91..cdad1d71087 100644 --- a/unittests/eosio.interchain_tests.cpp +++ b/unittests/eosio.interchain_tests.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include @@ -232,7 +233,21 @@ class eosio_interchain_tester : public actions fc::variant check_data( account_name acc ) { asset balance = get_balance( acc, "PROXY" ); - vector data = get_row_by_account( config::system_account_name, acc, N(userres), acc ); + const auto& resource_limit_mgr = control->get_resource_limits_manager(); + int64_t ram_bytes = 0; + int64_t net_weight = 0; + int64_t cpu_weight = 0; + + resource_limit_mgr.get_account_limits( acc, ram_bytes, net_weight, cpu_weight ); + + asset a_total_net_cpu( net_weight + cpu_weight ); + + return mvo() + ( "balance", balance ) + ( "staked_balance", a_total_net_cpu ) + ( "staked_ram", ram_bytes ); + + /*vector data = get_row_by_account( config::system_account_name, acc, N(userres), acc ); if( data.empty() ) { @@ -254,7 +269,7 @@ class eosio_interchain_tester : public actions ( "balance", balance ) ( "staked_balance", total_net_cpu ) ( "staked_ram", ram_bytes ); - } + }*/ } transaction_trace_ptr create_account_with_resources( account_name creator, account_name a, int64_t bytes = DEFAULT_RAM ) { From da69db62061c9c11942f338bce3461fb88eadaa0 Mon Sep 17 00:00:00 2001 From: marek Date: Fri, 9 Nov 2018 16:49:14 +0100 Subject: [PATCH 3/4] - remove `user_resources_table` from `system::contract`; - introduce `privileged_api::change_resource_limits` method to use instead of `user_resources_table`; - change `chain_plugin::read_only::get_account` body according to removed `user_resources_table`; - change `eosio_system_tester::get_total_stake` body according to removed `user_resources_table`; - better resolution for evaluate reward in `apply_context::reward_all`. --- contracts/eosio.system/delegate_bandwidth.cpp | 99 ++----------------- contracts/eosio.system/eosio.system.abi | 6 -- contracts/eosio.system/eosio.system.cpp | 27 +---- contracts/eosiolib/privileged.h | 10 ++ libraries/chain/apply_context.cpp | 85 +++++++++------- libraries/chain/wasm_interface.cpp | 25 ++++- plugins/chain_plugin/chain_plugin.cpp | 36 +++++-- unittests/eosio.interchain_tests.cpp | 38 ++----- unittests/eosio_system_tester.hpp | 50 +++++++--- 9 files changed, 158 insertions(+), 218 deletions(-) diff --git a/contracts/eosio.system/delegate_bandwidth.cpp b/contracts/eosio.system/delegate_bandwidth.cpp index d84eb78aa24..0efe5283223 100644 --- a/contracts/eosio.system/delegate_bandwidth.cpp +++ b/contracts/eosio.system/delegate_bandwidth.cpp @@ -78,7 +78,6 @@ namespace eosiosystem { * These tables are designed to be constructed in the scope of the relevant user, this * facilitates simpler API for per-user queries */ - typedef eosio::multi_index< N(userres), user_resources> user_resources_table; typedef eosio::multi_index< N(delband), delegated_bandwidth> del_bandwidth_table; typedef eosio::multi_index< N(refunds), refund_request> refunds_table; @@ -149,19 +148,7 @@ namespace eosiosystem { _gstate.total_ram_bytes_reserved += uint64_t(bytes_out); _gstate.total_ram_stake += quant_after_fee.amount; - user_resources_table userres( _self, receiver ); - auto res_itr = userres.find( receiver ); - if( res_itr == userres.end() ) { - res_itr = userres.emplace( receiver, [&]( auto& res ) { - res.owner = receiver; - res.ram_bytes = bytes_out; - }); - } else { - userres.modify( res_itr, receiver, [&]( auto& res ) { - res.ram_bytes += bytes_out; - }); - } - set_resource_limits( res_itr->owner, res_itr->ram_bytes, res_itr->net_weight.amount, res_itr->cpu_weight.amount ); + change_resource_limits( receiver, bytes_out, 0, 0 ); } void system_contract::initresource( account_name receiver, int64_t bytes, asset stake_net_quantity, asset stake_cpu_quantity ) @@ -169,26 +156,7 @@ namespace eosiosystem { require_auth( _self ); //Delegate RAM from payer to receiver - user_resources_table userres( _self, receiver ); - - auto res_itr = userres.find( receiver ); - - if( res_itr == userres.end() ) { - res_itr = userres.emplace( receiver, [&]( auto& res ) { - res.owner = receiver; - res.ram_bytes = bytes; - res.net_weight = stake_net_quantity; - res.cpu_weight = stake_cpu_quantity; - }); - } else { - userres.modify( res_itr, receiver, [&]( auto& res ) { - res.ram_bytes += bytes; - res.net_weight += stake_net_quantity; - res.cpu_weight += stake_cpu_quantity; - }); - } - - set_resource_limits( res_itr->owner, res_itr->ram_bytes, res_itr->net_weight.amount, res_itr->cpu_weight.amount ); + change_resource_limits( receiver, bytes, stake_net_quantity.amount, stake_cpu_quantity.amount ); } void system_contract::delegateram( account_name payer, account_name receiver, int64_t bytes ) @@ -196,34 +164,10 @@ namespace eosiosystem { require_auth( payer ); //Check amount of RAM for payer - user_resources_table userres_payer( _self, payer ); - auto res_itr = userres_payer.find( payer ); - eosio_assert( res_itr != userres_payer.end(), "payer must exist" ); - - eosio_assert( res_itr->ram_bytes >= bytes, "not enough RAM" ); - - //Decrease amount of RAM for payer - userres_payer.modify( res_itr, payer, [&]( auto& res ) { - res.ram_bytes -= bytes; - }); + change_resource_limits( payer, -bytes, 0, 0 ); //Delegate RAM from payer to receiver - user_resources_table userres( _self, receiver ); - - res_itr = userres.find( receiver ); - - if( res_itr == userres.end() ) { - res_itr = userres.emplace( receiver, [&]( auto& res ) { - res.owner = receiver; - res.ram_bytes = bytes; - }); - } else { - userres.modify( res_itr, receiver, [&]( auto& res ) { - res.ram_bytes += bytes; - }); - } - - set_resource_limits( res_itr->owner, res_itr->ram_bytes, res_itr->net_weight.amount, res_itr->cpu_weight.amount ); + change_resource_limits( receiver, bytes, 0, 0 ); } /** @@ -239,11 +183,6 @@ namespace eosiosystem { require_auth( account ); eosio_assert( bytes > 0, "cannot sell negative byte" ); - user_resources_table userres( _self, account ); - auto res_itr = userres.find( account ); - eosio_assert( res_itr != userres.end(), "no resource row" ); - eosio_assert( res_itr->ram_bytes >= bytes, "insufficient quota" ); - asset tokens_out; auto itr = _rammarket.find(S(4,RAMCORE)); _rammarket.modify( itr, 0, [&]( auto& es ) { @@ -259,10 +198,7 @@ namespace eosiosystem { //// this shouldn't happen, but just in case it does we should prevent it eosio_assert( _gstate.total_ram_stake >= 0, "error, attempt to unstake more tokens than previously staked" ); - userres.modify( res_itr, account, [&]( auto& res ) { - res.ram_bytes -= bytes; - }); - set_resource_limits( res_itr->owner, res_itr->ram_bytes, res_itr->net_weight.amount, res_itr->cpu_weight.amount ); + change_resource_limits( account, -bytes, 0, 0 ); INLINE_ACTION_SENDER(eosio::token, transfer)( N(eosio.token), {N(eosio.ram),N(active)}, { N(eosio.ram), account, asset(tokens_out), std::string("sell ram") } ); @@ -324,30 +260,7 @@ namespace eosiosystem { } // itr can be invalid, should go out of scope // update totals of "receiver" - { - user_resources_table totals_tbl( _self, receiver ); - auto tot_itr = totals_tbl.find( receiver ); - if( tot_itr == totals_tbl.end() ) { - tot_itr = totals_tbl.emplace( from, [&]( auto& tot ) { - tot.owner = receiver; - tot.net_weight = stake_net_delta; - tot.cpu_weight = stake_cpu_delta; - }); - } else { - totals_tbl.modify( tot_itr, from == receiver ? from : 0, [&]( auto& tot ) { - tot.net_weight += stake_net_delta; - tot.cpu_weight += stake_cpu_delta; - }); - } - eosio_assert( asset(0) <= tot_itr->net_weight, "insufficient staked total net bandwidth" ); - eosio_assert( asset(0) <= tot_itr->cpu_weight, "insufficient staked total cpu bandwidth" ); - - set_resource_limits( receiver, tot_itr->ram_bytes, tot_itr->net_weight.amount, tot_itr->cpu_weight.amount ); - - if ( tot_itr->net_weight == asset(0) && tot_itr->cpu_weight == asset(0) && tot_itr->ram_bytes == 0 ) { - totals_tbl.erase( tot_itr ); - } - } // tot_itr can be invalid, should go out of scope + change_resource_limits( receiver, 0, stake_net_delta.amount, stake_cpu_delta.amount ); // create refund or update from existing refund if ( N(eosio.stake) != source_stake_from ) { //for eosio both transfer and refund make no sense diff --git a/contracts/eosio.system/eosio.system.abi b/contracts/eosio.system/eosio.system.abi index 97b115c2a7f..1b05ded356f 100644 --- a/contracts/eosio.system/eosio.system.abi +++ b/contracts/eosio.system/eosio.system.abi @@ -566,12 +566,6 @@ "index_type": "i64", "key_names" : ["owner"], "key_types" : ["account_name"] - },{ - "name": "userres", - "type": "user_resources", - "index_type": "i64", - "key_names" : ["owner"], - "key_types" : ["uint64"] },{ "name": "delband", "type": "delegated_bandwidth", diff --git a/contracts/eosio.system/eosio.system.cpp b/contracts/eosio.system/eosio.system.cpp index 3ec38c20c72..d283d4623d9 100644 --- a/contracts/eosio.system/eosio.system.cpp +++ b/contracts/eosio.system/eosio.system.cpp @@ -136,26 +136,7 @@ namespace eosiosystem { void system_contract::reward( account_name receiver, int64_t ram_bytes, asset net_weight, asset cpu_weight ) { - user_resources_table userres( _self, receiver ); - - auto res_itr = userres.find( receiver ); - - if( res_itr == userres.end() ) { - res_itr = userres.emplace( receiver, [&]( auto& res ) { - res.owner = receiver; - res.ram_bytes = ram_bytes; - res.net_weight = net_weight; - res.cpu_weight = cpu_weight; - }); - } else { - userres.modify( res_itr, receiver, [&]( auto& res ) { - res.ram_bytes += ram_bytes; - res.net_weight += net_weight; - res.cpu_weight += cpu_weight; - }); - } - - set_resource_limits( res_itr->owner, res_itr->ram_bytes, res_itr->net_weight.amount, res_itr->cpu_weight.amount ); + change_resource_limits( receiver, ram_bytes, net_weight.amount, cpu_weight.amount ); } /** @@ -196,12 +177,6 @@ namespace eosiosystem { } } - user_resources_table userres( _self, newact); - - userres.emplace( newact, [&]( auto& res ) { - res.owner = newact; - }); - set_resource_limits( newact, 0, 0, 0 ); } diff --git a/contracts/eosiolib/privileged.h b/contracts/eosiolib/privileged.h index 8943a09db23..9f2261c8f87 100644 --- a/contracts/eosiolib/privileged.h +++ b/contracts/eosiolib/privileged.h @@ -38,6 +38,16 @@ extern "C" { */ void set_resource_limits( account_name account, int64_t ram_bytes, int64_t net_weight, int64_t cpu_weight ); + /** + * @brief Change the resource limits of an account + * Change the resource limits of an account + * @param account - name of the account whose resource limit to be change + * @param ram_bytes - ram delta limit in absolute bytes + * @param net_weight - fractionally proportionate net delta limit of available resources based on (weight / total_weight_of_all_accounts) + * @param cpu_weight - fractionally proportionate cpu delta limit of available resources based on (weight / total_weight_of_all_accounts) + */ + void change_resource_limits( account_name account, int64_t ram_bytes, int64_t net_weight, int64_t cpu_weight ); + /** * Proposes a schedule change, once the block that contains the proposal becomes irreversible, the schedule is promoted to "pending" automatically. Once the block that promotes the schedule is irreversible, the schedule will become "active" * @param producer_data - packed data of produce_keys in the appropriate producer schedule order diff --git a/libraries/chain/apply_context.cpp b/libraries/chain/apply_context.cpp index 03fe3ad2c8d..6c160d034b3 100644 --- a/libraries/chain/apply_context.cpp +++ b/libraries/chain/apply_context.cpp @@ -110,28 +110,36 @@ void apply_context::finalize_trace( action_trace& trace, const fc::time_point& s trace.elapsed = fc::time_point::now() - start; } +//ABW: uncomment the following symbol to unconditionally write eosio::print calls to file (works even during unit tests) +//#define CAVEMEN_DEBUG +#ifdef CAVEMEN_DEBUG +#define DBG(format, ... ) { FILE *pFile = fopen("debug.log","a"); fprintf(pFile,format "\n",__VA_ARGS__); fclose(pFile); } +#else +#define DBG(format, ... ) +#endif + void apply_context::reward_stake( const account_name& account, int64_t val ) { + int64_t stake_net = val / 2; + int64_t stake_cpu = val - stake_net; + auto& resource_limit_mgr = control.get_mutable_resource_limits_manager(); int64_t ram_bytes = 0; int64_t net_weight = 0; int64_t cpu_weight = 0; - resource_limit_mgr.get_account_limits( account, ram_bytes, net_weight, cpu_weight ); - - int64_t stake_net = val / 2; - int64_t stake_cpu = val - stake_net; + resource_limit_mgr.get_account_limits( account, ram_bytes, net_weight, cpu_weight ); - net_weight += stake_net; - cpu_weight += stake_cpu; + net_weight += stake_net; + cpu_weight += stake_cpu; // [MK]: ram stay unchanged, so no need to check? - //if (resource_limit_mgr.set_account_limits( name, ram_bytes, net_weight, cpu_weight )) - //trx_context.validate_ram_usage.insert( name ); - bool need_validation = resource_limit_mgr.set_account_limits( account, ram_bytes, net_weight, cpu_weight ); - EOS_ASSERT( need_validation == false, - resource_limit_exception, - "new ram_bytes limit cannot be more restrictive than the previously set one (account '{account}')", ("account", account) ); + //if (resource_limit_mgr.set_account_limits( name, ram_bytes, net_weight, cpu_weight )) + //trx_context.validate_ram_usage.insert( name ); + bool need_validation = resource_limit_mgr.set_account_limits( account, ram_bytes, net_weight, cpu_weight ); + EOS_ASSERT( need_validation == false, + resource_limit_exception, + "new ram_bytes limit cannot be more restrictive than the previously set one (account '{account}')", ("account", account) ); } void apply_context::reward_ram( const account_name& account, int64_t val ) @@ -141,11 +149,11 @@ void apply_context::reward_ram( const account_name& account, int64_t val ) int64_t net_weight = 0; int64_t cpu_weight = 0; - resource_limit_mgr.get_account_limits( account, ram_bytes, net_weight, cpu_weight ); - ram_bytes += val; - - if (resource_limit_mgr.set_account_limits( account, ram_bytes, net_weight, cpu_weight )) - trx_context.validate_ram_usage.insert( account ); + resource_limit_mgr.get_account_limits( account, ram_bytes, net_weight, cpu_weight ); + ram_bytes += val; + + if (resource_limit_mgr.set_account_limits( account, ram_bytes, net_weight, cpu_weight )) + trx_context.validate_ram_usage.insert( account ); } void apply_context::reward_all( uint32_t block_nr, uint64_t gathered_amount, asset sym, bool is_beos_mode ) @@ -153,34 +161,41 @@ void apply_context::reward_all( uint32_t block_nr, uint64_t gathered_amount, ass auto& resource_limit_mgr = control.get_mutable_resource_limits_manager(); const auto& accounts_index = db.template get_index(); - auto get_currency_balance = [this]( const account_name& name, const symbol& asset_symbol ) -> asset - { - const auto* tbl = db.template find(std::make_tuple(N(eosio.token), name, N(accounts))); - share_type result = 0; - - // the balance is implied to be 0 if either the table or row does not exist - if (tbl) { - const auto *obj = db.template find(std::make_tuple(tbl->id, asset_symbol.to_symbol_code().value)); - if (obj) { - //balance is the first field in the serialization - fc::datastream ds(obj->value.data(), obj->value.size()); - fc::raw::unpack(ds, result); - } - } - return asset(result, asset_symbol); - }; + auto get_currency_balance = [this]( const account_name& name, const symbol& asset_symbol ) -> asset + { + const auto* tbl = db.template find(std::make_tuple(N(eosio.token), name, N(accounts))); + share_type result = 0; + + // the balance is implied to be 0 if either the table or row does not exist + if (tbl) { + const auto *obj = db.template find(std::make_tuple(tbl->id, asset_symbol.to_symbol_code().value)); + if (obj) { + //balance is the first field in the serialization + fc::datastream ds(obj->value.data(), obj->value.size()); + fc::raw::unpack(ds, result); + } + } + return asset(result, asset_symbol); + }; for (auto& account : accounts_index) { auto& name = account.name; asset balance( get_currency_balance(name, sym.get_symbol()) ); //Calculation ratio for given account. - long double ratio = static_cast( balance.get_amount() ) / gathered_amount; - int64_t val = static_cast( block_nr * ratio ); + int128_t block_amount = static_cast(balance.get_amount()) * block_nr; + long double dval = static_cast(block_amount) / gathered_amount; + //int64_t val = static_cast(dval); + int64_t val = llround(dval); // [MK]: round is for compatibility with the same code on contract side if (val <= 0) continue; + //fc::uint128_t v_block_amount(block_amount>>64, static_cast(block_amount)); + + //DBG("reward_all for %s: balance = %lu, gathered_amount = %lu, block_amount = %s, block_nr = %u, dval = %.20Lg, val = %li", + // name.to_string().c_str(), balance.get_amount(), gathered_amount, fc::variant(v_block_amount).get_string().c_str(), block_nr, dval, val); + if (is_beos_mode) reward_stake( name, val ); else diff --git a/libraries/chain/wasm_interface.cpp b/libraries/chain/wasm_interface.cpp index 0b3d5fcf04f..6efaa0fc52e 100644 --- a/libraries/chain/wasm_interface.cpp +++ b/libraries/chain/wasm_interface.cpp @@ -139,7 +139,7 @@ class privileged_api : public context_aware_api { * @param net_weight - the weight for determining share of network capacity * @param cpu_weight - the weight for determining share of compute capacity */ - void set_resource_limits( account_name account, int64_t ram_bytes, int64_t net_weight, int64_t cpu_weight) { + void set_resource_limits( account_name account, int64_t ram_bytes, int64_t net_weight, int64_t cpu_weight ) { EOS_ASSERT(ram_bytes >= -1, wasm_execution_error, "invalid value for ram resource limit expected [-1,INT64_MAX]"); EOS_ASSERT(net_weight >= -1, wasm_execution_error, "invalid value for net resource weight expected [-1,INT64_MAX]"); EOS_ASSERT(cpu_weight >= -1, wasm_execution_error, "invalid value for cpu resource weight expected [-1,INT64_MAX]"); @@ -152,6 +152,20 @@ class privileged_api : public context_aware_api { context.control.get_resource_limits_manager().get_account_limits( account, ram_bytes, net_weight, cpu_weight); } + void change_resource_limits( account_name account, int64_t ram_bytes, int64_t net_weight, int64_t cpu_weight ) { + int64_t _ram_bytes = 0; + int64_t _net_weight = 0; + int64_t _cpu_weight = 0; + + get_resource_limits( account, _ram_bytes, _net_weight, _cpu_weight ); + + _ram_bytes += ram_bytes; + _net_weight += net_weight; + _cpu_weight += cpu_weight; + + set_resource_limits( account, _ram_bytes, _net_weight, _cpu_weight ); + } + int64_t set_proposed_producers( array_ptr packed_producer_schedule, size_t datalen) { datastream ds( packed_producer_schedule, datalen ); vector producers; @@ -991,10 +1005,10 @@ class console_api : public context_aware_api { //ABW: uncomment the following symbol to unconditionally write eosio::print calls to file (works even during unit tests) //#define CAVEMEN_DEBUG #ifdef CAVEMEN_DEBUG -#define DBG(format,value) { FILE *pFile = fopen("debug.log","a"); fprintf(pFile,format "\n",value); fclose(pFile); } -#else -#define DBG(format,value) -#endif +#define DBG(format,value) { FILE *pFile = fopen("debug.log","a"); fprintf(pFile,format "\n",value); fclose(pFile); } +#else +#define DBG(format,value) +#endif // Kept as intrinsic rather than implementing on WASM side (using prints_l and strlen) because strlen is faster on native side. void prints(null_terminated_ptr str) { @@ -1760,6 +1774,7 @@ REGISTER_INTRINSICS(privileged_api, (activate_feature, void(int64_t) ) (get_resource_limits, void(int64_t,int,int,int) ) (set_resource_limits, void(int64_t,int64_t,int64_t,int64_t) ) + (change_resource_limits, void(int64_t,int64_t,int64_t,int64_t) ) (set_proposed_producers, int64_t(int,int) ) (get_blockchain_parameters_packed, int(int, int) ) (set_blockchain_parameters_packed, void(int,int) ) diff --git a/plugins/chain_plugin/chain_plugin.cpp b/plugins/chain_plugin/chain_plugin.cpp index 92b297ab727..cad81a397e8 100644 --- a/plugins/chain_plugin/chain_plugin.cpp +++ b/plugins/chain_plugin/chain_plugin.cpp @@ -1710,15 +1710,33 @@ read_only::get_account_results read_only::get_account( const get_account_params& } } - t_id = d.find(boost::make_tuple( config::system_account_name, params.account_name, N(userres) )); - if (t_id != nullptr) { - const auto &idx = d.get_index(); - auto it = idx.find(boost::make_tuple( t_id->id, params.account_name )); - if ( it != idx.end() ) { - vector data; - copy_inline_row(*it, data); - result.total_resources = abis.binary_to_variant( "user_resources", data, abi_serializer_max_time, shorten_abi_errors ); - } + { + int64_t ram_bytes = 0; + int64_t net_weight = 0; + int64_t cpu_weight = 0; + + rm.get_account_limits( params.account_name, ram_bytes, net_weight, cpu_weight ); + + asset a_net_weight( net_weight ); + asset a_cpu_weight( cpu_weight ); + + /*struct user_resources { + account_name owner; + asset net_weight; + asset cpu_weight; + int64_t ram_bytes = 0; + + uint64_t primary_key()const { return owner; } + + // explicit serialization macro is not necessary, used here only to improve compilation time + EOSLIB_SERIALIZE( user_resources, (owner)(net_weight)(cpu_weight)(ram_bytes) ) + };*/ + + result.total_resources = mutable_variant_object() + ( "owner", params.account_name ) + ( "net_weight", a_net_weight ) + ( "cpu_weight", a_cpu_weight ) + ( "ram_bytes", ram_bytes ); } t_id = d.find(boost::make_tuple( config::system_account_name, params.account_name, N(delband) )); diff --git a/unittests/eosio.interchain_tests.cpp b/unittests/eosio.interchain_tests.cpp index cdad1d71087..24628742b30 100644 --- a/unittests/eosio.interchain_tests.cpp +++ b/unittests/eosio.interchain_tests.cpp @@ -1,7 +1,7 @@ #include #include #include -#include +#include #include #include @@ -238,38 +238,14 @@ class eosio_interchain_tester : public actions int64_t net_weight = 0; int64_t cpu_weight = 0; - resource_limit_mgr.get_account_limits( acc, ram_bytes, net_weight, cpu_weight ); - - asset a_total_net_cpu( net_weight + cpu_weight ); - - return mvo() - ( "balance", balance ) - ( "staked_balance", a_total_net_cpu ) - ( "staked_ram", ram_bytes ); + resource_limit_mgr.get_account_limits( acc, ram_bytes, net_weight, cpu_weight ); - /*vector data = get_row_by_account( config::system_account_name, acc, N(userres), acc ); + asset a_total_net_cpu( net_weight + cpu_weight ); - if( data.empty() ) - { - return mvo() - ( "balance", balance ) - ( "staked_balance", "0.0000 BEOS" ) - ( "staked_ram", "0.0000 BEOS" ); - } - else - { - auto resources = system_abi_ser.binary_to_variant( "user_resources", data, abi_serializer_max_time ); - auto net_weight = resources["net_weight"].as(); - auto cpu_weight = resources["cpu_weight"].as(); - auto ram_bytes = resources["ram_bytes"].as(); - - auto total_net_cpu = net_weight + cpu_weight; - - return mvo() - ( "balance", balance ) - ( "staked_balance", total_net_cpu ) - ( "staked_ram", ram_bytes ); - }*/ + return mvo() + ( "balance", balance ) + ( "staked_balance", a_total_net_cpu ) + ( "staked_ram", ram_bytes ); } transaction_trace_ptr create_account_with_resources( account_name creator, account_name a, int64_t bytes = DEFAULT_RAM ) { diff --git a/unittests/eosio_system_tester.hpp b/unittests/eosio_system_tester.hpp index 444a9d27dee..0c5ee6e47bd 100644 --- a/unittests/eosio_system_tester.hpp +++ b/unittests/eosio_system_tester.hpp @@ -16,14 +16,14 @@ #include #include -#include -#include - -#include -#include - -#include -#include +#include +#include + +#include +#include + +#include +#include #include @@ -77,9 +77,9 @@ class eosio_system_tester : public TESTER { set_code( config::system_account_name, eosio_system_wast ); set_abi( config::system_account_name, eosio_system_abi ); - - initial_settings(eosio_init_wast, eosio_init_abi, eosio_gateway_wast, eosio_gateway_abi, eosio_distribution_wast, eosio_distribution_abi); - + + initial_settings(eosio_init_wast, eosio_init_abi, eosio_gateway_wast, eosio_gateway_abi, eosio_distribution_wast, eosio_distribution_abi); + { const auto& accnt = control->db().get( config::system_account_name ); abi_def abi; @@ -345,8 +345,32 @@ class eosio_system_tester : public TESTER { } fc::variant get_total_stake( const account_name& act ) { - vector data = get_row_by_account( config::system_account_name, act, N(userres), act ); - return data.empty() ? fc::variant() : abi_ser.binary_to_variant( "user_resources", data, abi_serializer_max_time ); + int64_t ram_bytes = 0; + int64_t net_weight = 0; + int64_t cpu_weight = 0; + + control->get_resource_limits_manager().get_account_limits( act, ram_bytes, net_weight, cpu_weight ); + + asset a_net_weight( net_weight ); + asset a_cpu_weight( cpu_weight ); + + /*struct user_resources { + account_name owner; + asset net_weight; + asset cpu_weight; + int64_t ram_bytes = 0; + + uint64_t primary_key()const { return owner; } + + // explicit serialization macro is not necessary, used here only to improve compilation time + EOSLIB_SERIALIZE( user_resources, (owner)(net_weight)(cpu_weight)(ram_bytes) ) + };*/ + + return mvo() + ( "owner", act ) + ( "net_weight", a_net_weight ) + ( "cpu_weight", a_cpu_weight ) + ( "ram_bytes", ram_bytes ); } fc::variant get_voter_info( const account_name& act ) { From 5288251081f470e22dd730752c7a6bc47f7e030e Mon Sep 17 00:00:00 2001 From: marek Date: Fri, 9 Nov 2018 17:14:13 +0100 Subject: [PATCH 4/4] - code clean-up (close #5) --- .../eosio.distribution/eosio.distribution.cpp | 62 ------------------- .../eosio.distribution/eosio.distribution.hpp | 4 -- 2 files changed, 66 deletions(-) diff --git a/contracts/eosio.distribution/eosio.distribution.cpp b/contracts/eosio.distribution/eosio.distribution.cpp index 3d79c81357d..6458207576a 100644 --- a/contracts/eosio.distribution/eosio.distribution.cpp +++ b/contracts/eosio.distribution/eosio.distribution.cpp @@ -26,8 +26,6 @@ void distribution::execute( uint32_t block_nr, asset proxy_asset, if( ( ( block_nr - starting_block_for_any_distribution ) % distribution_payment_block_interval_for_any_distribution ) == 0 ) { //Rewarding all accounts. - //rewardall( nr_items, proxy_asset, is_beos_mode ); - uint64_t gathered_amount = get_sum(); if (gathered_amount != 0) @@ -40,7 +38,6 @@ void distribution::execute( uint32_t block_nr, asset proxy_asset, ( block_nr + distribution_payment_block_interval_for_any_distribution > ending_block_for_any_distribution ) ) { - //rewarddone( proxy_asset, is_beos_mode ); reward_done( &proxy_asset, sizeof(asset), is_beos_mode ); } } @@ -73,65 +70,6 @@ uint64_t distribution::get_sum() return issued - withdrawn; } -void distribution::rewardall( uint32_t total_amount, asset symbol/*correct symbol of BEOS coin, for example: `0.0000 BEOS`*/, bool is_beos_mode ) -{ - //Retrieve total sum of balances for every account. - uint64_t gathered_amount = get_sum(); - - if( gathered_amount == 0 ) - return; - - auto callable = [&]( const init_data& obj ) - { - auto balance = eosio::token( N(eosio.token) ).check_balance( obj.owner, symbol.symbol ); - //Calculation ratio for given account. - long double ratio = static_cast< long double >( balance.amount ) / gathered_amount; - int64_t val = static_cast< int64_t >( total_amount * ratio ); - - if( val > 0 ) - { - if( is_beos_mode ) - { - //Staking BEOS for user `obj.owner` always during every reward-time. - auto from = N(beos.distrib); - - auto stake_net = asset( val / 2 ); - auto stake_cpu = asset( val - stake_net.amount ); - - INLINE_ACTION_SENDER(eosiosystem::system_contract, reward)( N(eosio), {N(eosio),N(active)}, - { obj.owner, 0/*ram_bytes*/, stake_net/*net_weight*/, stake_cpu/*cpu_weight*/ } ); - - } - else - { - INLINE_ACTION_SENDER(eosiosystem::system_contract, reward)( N(eosio), {N(eosio),N(active)}, - { obj.owner, val/*ram_bytes*/, asset()/*net_weight*/, asset()/*cpu_weight*/ } ); - } - } - - }; - - //Review all accounts - review( callable ); -} - -void distribution::rewarddone( asset symbol/*correct symbol of BEOS coin, for example: `0.0000 BEOS`*/, bool is_beos_mode ) -{ -} - -void distribution::review( ConstModifier&& mod ) -{ - inits _init( N(beos.gateway), N(beos.gateway) ); - auto itr = _init.begin(); - - //Go through all accounts and do specific action `mod`. - while( itr != _init.end() ) - { - mod( *itr ); - ++itr; - } -} - } /// namespace eosio EOSIO_ABI( eosio::distribution, (onblock) ) diff --git a/contracts/eosio.distribution/eosio.distribution.hpp b/contracts/eosio.distribution/eosio.distribution.hpp index 551a86d6ca6..fe5a1aac620 100644 --- a/contracts/eosio.distribution/eosio.distribution.hpp +++ b/contracts/eosio.distribution/eosio.distribution.hpp @@ -21,14 +21,10 @@ namespace eosio { using ConstModifier = std::function< void( const init_data& a )>; uint64_t get_sum(); - void review( ConstModifier&& mod ); void execute( uint32_t block_nr, asset proxy_asset, uint32_t starting_block_for_any_distribution, uint32_t ending_block_for_any_distribution, uint32_t distribution_payment_block_interval_for_any_distribution, uint32_t nr_items, bool is_beos_mode ); - void rewardall( uint32_t total_amount, asset symbol/*correct symbol of BEOS coin, for example: `0.0000 BEOS`*/, bool is_beos_mode ); - void rewarddone( asset symbol/*correct symbol of BEOS coin, for example: `0.0000 BEOS`*/, bool is_beos_mode ); - public: distribution( account_name self );