Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Merge pull request #10250 from EOSIO/subjective_cpu_cleos_enhancement
Browse files Browse the repository at this point in the history
Rel 2.0.x: Subjective CPU billing cleos enhancement and adding subjective_cpu_bill to /v1/chain/get_account result
  • Loading branch information
linhuang-blockone authored Apr 17, 2021
2 parents 8c587de + faaadc5 commit e4ee6d4
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 12 deletions.
4 changes: 2 additions & 2 deletions plugins/chain_plugin/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ add_library( chain_plugin
chain_plugin.cpp
${HEADERS} )

target_link_libraries( chain_plugin eosio_chain appbase )
target_include_directories( chain_plugin PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include" "${CMAKE_CURRENT_SOURCE_DIR}/../chain_interface/include" "${CMAKE_CURRENT_SOURCE_DIR}/../../libraries/appbase/include")
target_link_libraries( chain_plugin producer_plugin eosio_chain appbase )
target_include_directories( chain_plugin PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include" "${CMAKE_CURRENT_SOURCE_DIR}/../chain_interface/include" "${CMAKE_CURRENT_SOURCE_DIR}/../../libraries/appbase/include" "${CMAKE_CURRENT_SOURCE_DIR}/../producer_plugin/include")

add_subdirectory( test )
13 changes: 12 additions & 1 deletion plugins/chain_plugin/chain_plugin.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <eosio/chain_plugin/chain_plugin.hpp>
#include <eosio/producer_plugin/producer_plugin.hpp>
#include <eosio/chain/fork_database.hpp>
#include <eosio/chain/block_log.hpp>
#include <eosio/chain/exceptions.hpp>
Expand Down Expand Up @@ -185,6 +186,7 @@ class chain_plugin_impl {


fc::optional<chain_apis::account_query_db> _account_query_db;
const producer_plugin* producer_plug;
};

chain_plugin::chain_plugin()
Expand Down Expand Up @@ -1144,6 +1146,9 @@ void chain_plugin::plugin_startup()
EOS_ASSERT( my->chain_config->read_mode != db_read_mode::IRREVERSIBLE || !accept_transactions(), plugin_config_exception,
"read-mode = irreversible. transactions should not be enabled by enable_accept_transactions" );
try {
my->producer_plug = app().find_plugin<producer_plugin>();
EOS_ASSERT(my->producer_plug, plugin_exception, "Failed to find producer_plugin");

auto shutdown = [](){ return app().is_quiting(); };
if (my->snapshot_path) {
auto infile = std::ifstream(my->snapshot_path->generic_string(), (std::ios::in | std::ios::binary));
Expand Down Expand Up @@ -1212,7 +1217,7 @@ void chain_apis::read_write::validate() const {
}

chain_apis::read_only chain_plugin::get_read_only_api() const {
return chain_apis::read_only(chain(), my->_account_query_db, get_abi_serializer_max_time());
return chain_apis::read_only(chain(), my->_account_query_db, get_abi_serializer_max_time(), my->producer_plug);
}


Expand Down Expand Up @@ -2436,6 +2441,12 @@ read_only::get_account_results read_only::get_account( const get_account_params&
result.cpu_limit = rm.get_account_cpu_limit_ex( result.account_name, greylist_limit).first;
result.ram_usage = rm.get_account_ram_usage( result.account_name );

if ( producer_plug ) { // producer_plug is null when called from chain_plugin_tests.cpp and get_table_tests.cpp
account_resource_limit subjective_cpu_bill_limit;
subjective_cpu_bill_limit.used = producer_plug->get_subjective_bill( result.account_name, fc::time_point::now() );
result.subjective_cpu_bill_limit = subjective_cpu_bill_limit;
}

const auto& permissions = d.get_index<permission_index,by_owner>();
auto perm = permissions.lower_bound( boost::make_tuple( params.account_name ) );
while( perm != permissions.end() && perm->owner == params.account_name ) {
Expand Down
12 changes: 9 additions & 3 deletions plugins/chain_plugin/include/eosio/chain_plugin/chain_plugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ namespace eosio {
using chain::abi_def;
using chain::abi_serializer;

class producer_plugin;

namespace chain_apis {
struct empty{};

Expand Down Expand Up @@ -83,12 +85,14 @@ class read_only {
const fc::optional<account_query_db>& aqdb;
const fc::microseconds abi_serializer_max_time;
bool shorten_abi_errors = true;
const producer_plugin* producer_plug;

public:
static const string KEYi64;

read_only(const controller& db, const fc::optional<account_query_db>& aqdb, const fc::microseconds& abi_serializer_max_time)
: db(db), aqdb(aqdb), abi_serializer_max_time(abi_serializer_max_time) {}
read_only(const controller& db, const fc::optional<account_query_db>& aqdb, const fc::microseconds& abi_serializer_max_time, const producer_plugin* producer_plug)
: db(db), aqdb(aqdb), abi_serializer_max_time(abi_serializer_max_time), producer_plug(producer_plug) {
}

void validate() const {}

Expand Down Expand Up @@ -167,6 +171,8 @@ class read_only {
fc::variant refund_request;
fc::variant voter_info;
fc::variant rex_info;

optional<account_resource_limit> subjective_cpu_bill_limit;
};

struct get_account_params {
Expand Down Expand Up @@ -798,7 +804,7 @@ FC_REFLECT( eosio::chain_apis::read_only::get_scheduled_transactions_result, (tr
FC_REFLECT( eosio::chain_apis::read_only::get_account_results,
(account_name)(head_block_num)(head_block_time)(privileged)(last_code_update)(created)
(core_liquid_balance)(ram_quota)(net_weight)(cpu_weight)(net_limit)(cpu_limit)(ram_usage)(permissions)
(total_resources)(self_delegated_bandwidth)(refund_request)(voter_info)(rex_info) )
(total_resources)(self_delegated_bandwidth)(refund_request)(voter_info)(rex_info)(subjective_cpu_bill_limit) )
// @swap code_hash
FC_REFLECT( eosio::chain_apis::read_only::get_code_results, (account_name)(code_hash)(wast)(wasm)(abi) )
FC_REFLECT( eosio::chain_apis::read_only::get_code_hash_results, (account_name)(code_hash) )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class producer_plugin : public appbase::plugin<producer_plugin> {

bool is_producer_key(const chain::public_key_type& key) const;
chain::signature_type sign_compact(const chain::public_key_type& key, const fc::sha256& digest) const;
uint32_t get_subjective_bill( const account_name& first_auth, const fc::time_point& now ) const;

virtual void plugin_initialize(const boost::program_options::variables_map& options);
virtual void plugin_startup();
Expand Down
5 changes: 5 additions & 0 deletions plugins/producer_plugin/producer_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,11 @@ bool producer_plugin::is_producer_key(const chain::public_key_type& key) const
return false;
}

uint32_t producer_plugin::get_subjective_bill( const account_name& first_auth, const fc::time_point& now ) const
{
return my->_subjective_billing.get_subjective_bill( first_auth, now );
}

chain::signature_type producer_plugin::sign_compact(const chain::public_key_type& key, const fc::sha256& digest) const
{
if(key != chain::public_key_type()) {
Expand Down
7 changes: 6 additions & 1 deletion programs/cleos/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2304,13 +2304,18 @@ void get_account( const string& accountName, const string& coresym, bool json_fo
}
}


std::cout << std::fixed << setprecision(3);
std::cout << indent << std::left << std::setw(11) << "used:" << std::right << std::setw(18) << to_pretty_time( res.cpu_limit.used ) << "\n";
std::cout << indent << std::left << std::setw(11) << "available:" << std::right << std::setw(18) << to_pretty_time( res.cpu_limit.available ) << "\n";
std::cout << indent << std::left << std::setw(11) << "limit:" << std::right << std::setw(18) << to_pretty_time( res.cpu_limit.max ) << "\n";
std::cout << std::endl;

if( res.subjective_cpu_bill_limit.valid() ) {
std::cout << "subjective cpu bandwidth:" << std::endl;
std::cout << indent << std::left << std::setw(11) << "used:" << std::right << std::setw(18) << to_pretty_time( (res.subjective_cpu_bill_limit)->used ) << "\n";
std::cout << std::endl;
}

if( res.refund_request.is_object() ) {
auto obj = res.refund_request.get_object();
auto request_time = fc::time_point_sec::from_iso_string( obj["request_time"].as_string() );
Expand Down
2 changes: 1 addition & 1 deletion tests/chain_plugin_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ BOOST_FIXTURE_TEST_CASE( get_block_with_invalid_abi, TESTER ) try {
char headnumstr[20];
sprintf(headnumstr, "%d", headnum);
chain_apis::read_only::get_block_params param{headnumstr};
chain_apis::read_only plugin(*(this->control), {}, fc::microseconds::maximum());
chain_apis::read_only plugin(*(this->control), {}, fc::microseconds::maximum(), {});

// block should be decoded successfully
std::string block_str = json::to_pretty_string(plugin.get_block(param));
Expand Down
8 changes: 4 additions & 4 deletions tests/get_table_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ BOOST_FIXTURE_TEST_CASE( get_scope_test, TESTER ) try {
produce_blocks(1);

// iterate over scope
eosio::chain_apis::read_only plugin(*(this->control), {}, fc::microseconds::maximum());
eosio::chain_apis::read_only plugin(*(this->control), {}, fc::microseconds::maximum(), {});
eosio::chain_apis::read_only::get_table_by_scope_params param{N(eosio.token), N(accounts), "inita", "", 10};
eosio::chain_apis::read_only::get_table_by_scope_result result = plugin.read_only::get_table_by_scope(param);

Expand Down Expand Up @@ -194,7 +194,7 @@ BOOST_FIXTURE_TEST_CASE( get_table_test, TESTER ) try {
produce_blocks(1);

// get table: normal case
eosio::chain_apis::read_only plugin(*(this->control), {}, fc::microseconds::maximum());
eosio::chain_apis::read_only plugin(*(this->control), {}, fc::microseconds::maximum(), {});
eosio::chain_apis::read_only::get_table_rows_params p;
p.code = N(eosio.token);
p.scope = "inita";
Expand Down Expand Up @@ -363,7 +363,7 @@ BOOST_FIXTURE_TEST_CASE( get_table_by_seckey_test, TESTER ) try {
produce_blocks(1);

// get table: normal case
eosio::chain_apis::read_only plugin(*(this->control), {}, fc::microseconds::maximum());
eosio::chain_apis::read_only plugin(*(this->control), {}, fc::microseconds::maximum(), {});
eosio::chain_apis::read_only::get_table_rows_params p;
p.code = N(eosio);
p.scope = "eosio";
Expand Down Expand Up @@ -515,7 +515,7 @@ BOOST_FIXTURE_TEST_CASE( get_table_next_key_test, TESTER ) try {
// }


chain_apis::read_only plugin(*(this->control), {}, fc::microseconds::maximum());
chain_apis::read_only plugin(*(this->control), {}, fc::microseconds::maximum(), {});
chain_apis::read_only::get_table_rows_params params{
.json=true,
.code=N(test),
Expand Down

0 comments on commit e4ee6d4

Please sign in to comment.