From 608a68a680e4a1aecb83cda0378570a1352686b4 Mon Sep 17 00:00:00 2001 From: thaipandada Date: Sat, 7 Mar 2020 20:02:49 +0800 Subject: [PATCH] Add /v1/producer/create_acts_snapshot to export all accounts system token amount. file inlude: name,liquid,self_staked,other_staked,unstaking,rex_deposit --- plugins/chain_plugin/chain_plugin.cpp | 56 +++++++++++++++++++ .../eosio/chain_plugin/chain_plugin.hpp | 13 +++++ .../producer_api_plugin.cpp | 2 + .../eosio/producer_plugin/producer_plugin.hpp | 1 + plugins/producer_plugin/producer_plugin.cpp | 42 ++++++++++++++ scripts/eosio_build_darwin.sh | 16 +++++- 6 files changed, 127 insertions(+), 3 deletions(-) diff --git a/plugins/chain_plugin/chain_plugin.cpp b/plugins/chain_plugin/chain_plugin.cpp index 093480135bd..e23c252cfce 100644 --- a/plugins/chain_plugin/chain_plugin.cpp +++ b/plugins/chain_plugin/chain_plugin.cpp @@ -2023,6 +2023,62 @@ read_only::get_account_results read_only::get_account( const get_account_params& return result; } +read_only::get_act_token_result read_only::get_act_token( const name& account_name )const { + get_act_token_result token_result{0.0, 0.0, 0.0, 0.0, 0.0}; + + get_account_params params{account_name}; + get_account_results res = get_account(params); + + if( res.core_liquid_balance.valid() ) { + token_result.liquid_balance = res.core_liquid_balance->to_real(); + } + + double cpu_own = 0.0, net_own = 0.0, cpu_total = 0.0, net_total = 0.0; + if ( res.total_resources.is_object() ) { + net_total = asset::from_string(res.total_resources.get_object()["net_weight"].as_string()).to_real(); + cpu_total = asset::from_string(res.total_resources.get_object()["cpu_weight"].as_string()).to_real(); + } + if( res.self_delegated_bandwidth.is_object() ) { + cpu_own = asset::from_string( res.self_delegated_bandwidth.get_object()["cpu_weight"].as_string() ).to_real(); + net_own = asset::from_string( res.self_delegated_bandwidth.get_object()["net_weight"].as_string() ).to_real(); + } + token_result.self_staked = cpu_own + net_own; + token_result.other_staked = net_total + cpu_total - token_result.self_staked; + if( res.refund_request.is_object() ) { + auto obj = res.refund_request.get_object(); + token_result.unstaking = asset::from_string( obj["net_amount"].as_string() ).to_real() + asset::from_string( obj["cpu_amount"].as_string() ).to_real(); + } + // REX + const auto& d = db.db(); + const auto& code_account = db.db().get( config::system_account_name ); + abi_def abi; + if( abi_serializer::to_abi(code_account.abi, abi) ) { + abi_serializer abis( abi, abi_serializer_max_time ); + + const auto* t_id = d.find(boost::make_tuple( config::system_account_name, config::system_account_name, N(rexfund) )); + if (t_id != nullptr) { + const auto &idx = d.get_index(); + auto it = idx.find(boost::make_tuple( t_id->id, account_name )); + if ( it != idx.end() ) { + vector data; + copy_inline_row(*it, data); + token_result.rex_deposit = abis.binary_to_variant( "rex_fund", data, abi_serializer_max_time, shorten_abi_errors ).get_object()["balance"].as().to_real(); + } + } + t_id = d.find(boost::make_tuple( config::system_account_name, config::system_account_name, N(rexbal) )); + if (t_id != nullptr) { + const auto &idx = d.get_index(); + auto it = idx.find(boost::make_tuple( t_id->id, account_name )); + if ( it != idx.end() ) { + vector data; + copy_inline_row(*it, data); + token_result.rex_deposit += abis.binary_to_variant( "rex_balance", data, abi_serializer_max_time, shorten_abi_errors ).get_object()["vote_stake"].as().to_real(); + } + } + } + return token_result; +} + static variant action_abi_to_variant( const abi_def& abi, type_name action_type ) { variant v; auto it = std::find_if(abi.structs.begin(), abi.structs.end(), [&](auto& x){return x.name == action_type;}); diff --git a/plugins/chain_plugin/include/eosio/chain_plugin/chain_plugin.hpp b/plugins/chain_plugin/include/eosio/chain_plugin/chain_plugin.hpp index 036d70358fb..b397b2da0b2 100644 --- a/plugins/chain_plugin/include/eosio/chain_plugin/chain_plugin.hpp +++ b/plugins/chain_plugin/include/eosio/chain_plugin/chain_plugin.hpp @@ -117,6 +117,14 @@ class read_only { using account_resource_limit = chain::resource_limits::account_resource_limit; + struct get_act_token_result { + double liquid_balance; + double self_staked; + double other_staked; + double unstaking; + double rex_deposit; // rexfund.balance + rexbal.vote_stake + }; + struct get_account_results { name account_name; uint32_t head_block_num = 0; @@ -151,6 +159,8 @@ class read_only { }; get_account_results get_account( const get_account_params& params )const; + get_act_token_result get_act_token( const name& account_name )const; + struct get_code_results { name account_name; @@ -745,6 +755,9 @@ 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)(homepage) ) +FC_REFLECT( eosio::chain_apis::read_only::get_act_token_result, + (liquid_balance)(self_staked)(other_staked)(unstaking)(rex_deposit) ) + 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) ) FC_REFLECT( eosio::chain_apis::read_only::get_abi_results, (account_name)(abi) ) diff --git a/plugins/producer_api_plugin/producer_api_plugin.cpp b/plugins/producer_api_plugin/producer_api_plugin.cpp index 0ef7631c868..d640a6f218d 100644 --- a/plugins/producer_api_plugin/producer_api_plugin.cpp +++ b/plugins/producer_api_plugin/producer_api_plugin.cpp @@ -90,6 +90,8 @@ void producer_api_plugin::plugin_startup() { INVOKE_R_V(producer, get_integrity_hash), 201), CALL(producer, producer, create_snapshot, INVOKE_R_V(producer, create_snapshot), 201), + CALL(producer, producer, create_acts_snapshot, + INVOKE_R_V(producer, create_acts_snapshot), 201), }); } diff --git a/plugins/producer_plugin/include/eosio/producer_plugin/producer_plugin.hpp b/plugins/producer_plugin/include/eosio/producer_plugin/producer_plugin.hpp index c43f0e0f38b..f20d15d4fa9 100644 --- a/plugins/producer_plugin/include/eosio/producer_plugin/producer_plugin.hpp +++ b/plugins/producer_plugin/include/eosio/producer_plugin/producer_plugin.hpp @@ -81,6 +81,7 @@ class producer_plugin : public appbase::plugin { integrity_hash_information get_integrity_hash() const; snapshot_information create_snapshot() const; + snapshot_information create_acts_snapshot() const; signal confirmed_block; private: diff --git a/plugins/producer_plugin/producer_plugin.cpp b/plugins/producer_plugin/producer_plugin.cpp index 6232a711221..227215a7d5d 100644 --- a/plugins/producer_plugin/producer_plugin.cpp +++ b/plugins/producer_plugin/producer_plugin.cpp @@ -59,6 +59,7 @@ namespace eosio { static appbase::abstract_plugin& _producer_plugin = app().register_plugin(); using namespace eosio::chain; +using namespace eosio::chain_apis; using namespace eosio::chain::plugin_interface; namespace { @@ -985,6 +986,47 @@ producer_plugin::snapshot_information producer_plugin::create_snapshot() const { return {head_id, snapshot_path}; } +producer_plugin::snapshot_information producer_plugin::create_acts_snapshot() const { + + chain::controller& chain = my->chain_plug->chain(); + + auto reschedule = fc::make_scoped_exit([this](){ + my->schedule_production_loop(); + }); + + if (chain.pending_block_state()) { + // abort the pending block + chain.abort_block(); + } else { + reschedule.cancel(); + } + + auto head_id = chain.head_block_id(); + std::string acts_snapshot_path = (my->_snapshots_dir / fc::format_string("acts-snapshot-${id}.txt", fc::mutable_variant_object()("id", head_id))).generic_string(); + + EOS_ASSERT( !fc::is_regular_file(acts_snapshot_path), snapshot_exists_exception, + "acts-snapshot named ${name} already exists", ("name", acts_snapshot_path)); + + auto acts_stream = std::ofstream(acts_snapshot_path, (std::ios::out)); + acts_stream << "name,liquid,self_staked,other_staked,unstaking,rex_deposit" << std::endl; + + const auto& idx = chain.db().get_index(); + auto lower = idx.lower_bound( N(1) ); + auto upper = idx.upper_bound( N(zzzzzzzzzzzz) ); + + for (auto itr = lower; itr != upper; ++itr ){ + chain_apis::read_only::get_act_token_result result = my->chain_plug->get_read_only_api().get_act_token( itr->name ); + acts_stream << itr->name.to_string() << "," << result.liquid_balance << "," + << result.self_staked << "," << result.other_staked << "," + << result.unstaking << "," << result.rex_deposit << std::endl; + } + + acts_stream.flush(); + acts_stream.close(); + + return {head_id, acts_snapshot_path}; +} + optional producer_plugin_impl::calculate_next_block_time(const account_name& producer_name, const block_timestamp_type& current_block_time) const { chain::controller& chain = chain_plug->chain(); const auto& hbs = chain.head_block_state(); diff --git a/scripts/eosio_build_darwin.sh b/scripts/eosio_build_darwin.sh index 6eb48083a54..14738ca021e 100644 --- a/scripts/eosio_build_darwin.sh +++ b/scripts/eosio_build_darwin.sh @@ -68,7 +68,7 @@ printf "\\tChecking Home Brew installation\\n" if ! BREW=$( command -v brew ) then - printf "\\tHomebrew must be installed to compile EOS.IO\\n\\n" + printf "\\tHomebrew must be installed to compile EOSIO\\n\\n" printf "\\tDo you wish to install Home Brew?\\n" if is_noninteractive; then exec <<< "1"; fi select yn in "Yes" "No"; do @@ -177,13 +177,23 @@ printf "\\n\\tNo required Home Brew dependencies to install.\\n" fi - + printf "\\n\\tChecking clang in CommandLineTools for MacOS.\\n" + which clang | grep CommandLineTools 1> /dev/null + ISCMDCLANG=$? + if [ ${ISCMDCLANG} -ne 0 ]; then + print "\\tPlease use the Apple clang version 11.0.0 at least.\\n" + print "\\tIf installed, please change the PATH to use it\\n" + print "\\tOr download v11.3.1 from: https://developer.apple.com/download/more/. \\n" + printf "\\tExiting now.\\n\\n" + exit 1; + fi + printf "\\n\\tChecking boost library installation.\\n" BVERSION=$( grep "#define BOOST_VERSION" "/usr/local/include/boost/version.hpp" 2>/dev/null | tail -1 | tr -s ' ' | cut -d\ -f3 ) if [ "${BVERSION}" != "107100" ]; then if [ ! -z "${BVERSION}" ]; then printf "\\tFound Boost Version %s.\\n" "${BVERSION}" - printf "\\tEOS.IO requires Boost version 1.71.\\n" + printf "\\tEOSIO requires Boost version 1.71.\\n" printf "\\tWould you like to uninstall version %s and install Boost version 1.71.\\n" "${BVERSION}" if is_noninteractive; then exec <<< "1"; fi select yn in "Yes" "No"; do