From c8fc0e15dd090214c0134128a7d589141f406835 Mon Sep 17 00:00:00 2001 From: arhag Date: Fri, 27 Apr 2018 23:05:57 -0400 Subject: [PATCH] reduce max_cpu limit for trx to what billed accounts can pay (and what the block limit can support) This change still has not fixed the broken tests in wasm_tests. --- .../eosio/chain/transaction_context.hpp | 7 ++-- libraries/chain/transaction_context.cpp | 34 ++++++++++++------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/libraries/chain/include/eosio/chain/transaction_context.hpp b/libraries/chain/include/eosio/chain/transaction_context.hpp index 415d78133..fbdde0120 100644 --- a/libraries/chain/include/eosio/chain/transaction_context.hpp +++ b/libraries/chain/include/eosio/chain/transaction_context.hpp @@ -7,8 +7,8 @@ namespace eosio { namespace chain { class transaction_context { public: - transaction_context( controller& c, - const signed_transaction& t, + transaction_context( controller& c, + const signed_transaction& t, const transaction_id_type& trx_id ) :control(c), id(trx_id), @@ -33,7 +33,7 @@ namespace eosio { namespace chain { bool apply_actions = true; bool is_input = true; - fc::microseconds delay; + fc::microseconds delay; vector executed; @@ -51,7 +51,6 @@ namespace eosio { namespace chain { uint32_t net_usage = 0; //const transaction_metadata_ptr& trx_meta; chainbase::database::session undo_session; - flat_set bill_to_accounts; }; } } diff --git a/libraries/chain/transaction_context.cpp b/libraries/chain/transaction_context.cpp index 800bf9185..d8d651947 100644 --- a/libraries/chain/transaction_context.cpp +++ b/libraries/chain/transaction_context.cpp @@ -18,12 +18,31 @@ namespace eosio { namespace chain { control.validate_expiration( trx ); record_transaction( id, trx.expiration ); /// checks for dupes } - max_cpu = control.get_global_properties().configuration.max_transaction_cpu_usage; - // TODO: reduce max_cpu to the minimum of the amount that the paying accounts can afford to pay + + auto& rl = control.get_mutable_resource_limits_manager(); + auto current_slot = block_timestamp_type(control.pending_block_time()).slot; + + flat_set bill_to_accounts; + for( const auto& act : trx.actions ) { + for( const auto& auth : act.authorization ) + bill_to_accounts.insert( auth.actor ); + } + + rl.add_transaction_usage( bill_to_accounts, 0, 0, current_slot ); // Update usage values of accounts to reflect new time + + max_cpu = std::min( rl.get_block_cpu_limit(), + static_cast(control.get_global_properties().configuration.max_transaction_cpu_usage) ); + for( const auto& a : bill_to_accounts ) { + auto l = rl.get_account_cpu_limit(a); + if( l >= 0 ) + max_cpu = std::min( max_cpu, static_cast(l) ); // reduce max_cpu to the amount the account is able to pay + } + uint64_t trx_specified_cpu_usage_limit = uint64_t(trx.max_kcpu_usage.value)*1024; // overflow checked in transaction_header::validate() if( trx_specified_cpu_usage_limit > 0 ) max_cpu = std::min( max_cpu, trx_specified_cpu_usage_limit ); + if( apply_context_free ) { for( const auto& act : trx.context_free_actions ) { dispatch_action( act, act.account, true ); @@ -38,23 +57,14 @@ namespace eosio { namespace chain { schedule_transaction(); } - for( const auto& act : trx.actions ) { - for( const auto& auth : act.authorization ) - bill_to_accounts.insert( auth.actor ); - } - trace->cpu_usage = ((trace->cpu_usage + 1023)/1024)*1024; // Round up to nearest multiple of 1024 EOS_ASSERT( trace->cpu_usage <= max_cpu, tx_resource_exhausted, "cpu usage of transaction is too high: ${actual_net_usage} > ${cpu_usage_limit}", ("actual_net_usage", trace->cpu_usage)("cpu_usage_limit", max_cpu) ); - auto& rl = control.get_mutable_resource_limits_manager(); - - flat_set bta( bill_to_accounts.begin(), bill_to_accounts.end() ); FC_ASSERT( net_usage % 8 == 0, "net_usage must be a multiple of word size (8)" ); - rl.add_transaction_usage( bta, trace->cpu_usage, net_usage, block_timestamp_type(control.pending_block_time()).slot ); - + rl.add_transaction_usage( bill_to_accounts, trace->cpu_usage, net_usage, current_slot ); } void transaction_context::squash() { -- GitLab