From 484115e636b02de927706100aeece6b0de03d0cc Mon Sep 17 00:00:00 2001 From: Matt Witherspoon <32485495+spoonincode@users.noreply.github.com> Date: Tue, 12 Mar 2024 23:45:14 -0400 Subject: [PATCH 01/12] fix usage of stack variable after return --- plugins/producer_api_plugin/producer_api_plugin.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/producer_api_plugin/producer_api_plugin.cpp b/plugins/producer_api_plugin/producer_api_plugin.cpp index 65cbe2f58b..7e10d46357 100644 --- a/plugins/producer_api_plugin/producer_api_plugin.cpp +++ b/plugins/producer_api_plugin/producer_api_plugin.cpp @@ -63,6 +63,7 @@ using namespace eosio; auto result = api_handle.call_name(std::move(params)); #define INVOKE_R_R_D(api_handle, call_name, in_param) \ + const fc::microseconds http_max_response_time = http.get_max_response_time(); \ auto deadline = http_max_response_time == fc::microseconds::maximum() ? fc::time_point::maximum() \ : fc::time_point::now() + http_max_response_time; \ auto params = parse_params(body);\ @@ -91,7 +92,6 @@ void producer_api_plugin::plugin_startup() { // lifetime of plugin is lifetime of application auto& producer = app().get_plugin(); auto& http = app().get_plugin(); - fc::microseconds http_max_response_time = http.get_max_response_time(); app().get_plugin().add_api({ CALL_WITH_400(producer, producer_ro, producer, paused, From 1918155b13596bf219aa4de3aedf860e8e2c7979 Mon Sep 17 00:00:00 2001 From: greg7mdp Date: Mon, 18 Mar 2024 11:31:51 -0400 Subject: [PATCH 02/12] Replace hacky version of `calculate_max_depth` with simpler one. --- .../eosio/chain/incremental_merkle.hpp | 56 ++----------------- 1 file changed, 6 insertions(+), 50 deletions(-) diff --git a/libraries/chain/include/eosio/chain/incremental_merkle.hpp b/libraries/chain/include/eosio/chain/incremental_merkle.hpp index 0a84d076f5..090e2f4b5e 100644 --- a/libraries/chain/include/eosio/chain/incremental_merkle.hpp +++ b/libraries/chain/include/eosio/chain/incremental_merkle.hpp @@ -3,52 +3,10 @@ #include #include -namespace eosio { namespace chain { +namespace eosio::chain { namespace detail { -/** - * given an unsigned integral number return the smallest - * power-of-2 which is greater than or equal to the given number - * - * @param value - an unsigned integral - * @return - the minimum power-of-2 which is >= value - */ -constexpr uint64_t next_power_of_2(uint64_t value) { - value -= 1; - value |= value >> 1; - value |= value >> 2; - value |= value >> 4; - value |= value >> 8; - value |= value >> 16; - value |= value >> 32; - value += 1; - return value; -} - -/** - * Given a power-of-2 (assumed correct) return the number of leading zeros - * - * This is a classic count-leading-zeros in parallel without the necessary - * math to make it safe for anything that is not already a power-of-2 - * - * @param value - and integral power-of-2 - * @return the number of leading zeros - */ -constexpr int clz_power_2(uint64_t value) { - int lz = 64; - - if (value) lz--; - if (value & 0x00000000FFFFFFFFULL) lz -= 32; - if (value & 0x0000FFFF0000FFFFULL) lz -= 16; - if (value & 0x00FF00FF00FF00FFULL) lz -= 8; - if (value & 0x0F0F0F0F0F0F0F0FULL) lz -= 4; - if (value & 0x3333333333333333ULL) lz -= 2; - if (value & 0x5555555555555555ULL) lz -= 1; - - return lz; -} - /** * Given a number of nodes return the depth required to store them * in a fully balanced binary tree. @@ -56,12 +14,10 @@ constexpr int clz_power_2(uint64_t value) { * @param node_count - the number of nodes in the implied tree * @return the max depth of the minimal tree that stores them */ -constexpr int calcluate_max_depth(uint64_t node_count) { - if (node_count == 0) { +constexpr uint64_t calculate_max_depth(uint64_t node_count) { + if (node_count == 0) return 0; - } - auto implied_count = next_power_of_2(node_count); - return clz_power_2(implied_count) + 1; + return std::llround(std::ceil(std::log2(node_count))) + 1; } template @@ -167,7 +123,7 @@ class incremental_merkle_impl { */ const DigestType& append(const DigestType& digest) { bool partial = false; - auto max_depth = detail::calcluate_max_depth(_node_count + 1); + auto max_depth = detail::calculate_max_depth(_node_count + 1); auto current_depth = max_depth - 1; auto index = _node_count; auto top = digest; @@ -246,6 +202,6 @@ class incremental_merkle_impl { typedef incremental_merkle_impl incremental_merkle; typedef incremental_merkle_impl shared_incremental_merkle; -} } /// eosio::chain +} /// eosio::chain FC_REFLECT( eosio::chain::incremental_merkle, (_active_nodes)(_node_count) ); From cdb766b10aa33bd83ddc341a23876574d0c9d510 Mon Sep 17 00:00:00 2001 From: greg7mdp Date: Mon, 18 Mar 2024 13:48:04 -0400 Subject: [PATCH 03/12] Use Matt's non-floating point implementation of `calculate_max_depth`. --- libraries/chain/include/eosio/chain/incremental_merkle.hpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/libraries/chain/include/eosio/chain/incremental_merkle.hpp b/libraries/chain/include/eosio/chain/incremental_merkle.hpp index 090e2f4b5e..56708f7be0 100644 --- a/libraries/chain/include/eosio/chain/incremental_merkle.hpp +++ b/libraries/chain/include/eosio/chain/incremental_merkle.hpp @@ -2,6 +2,7 @@ #include #include #include +#include namespace eosio::chain { @@ -17,7 +18,8 @@ namespace detail { constexpr uint64_t calculate_max_depth(uint64_t node_count) { if (node_count == 0) return 0; - return std::llround(std::ceil(std::log2(node_count))) + 1; + // following is non-floating point equivalent to `std::ceil(std::log2(node_count)) + 1) + return 8*sizeof(node_count) - std::countl_zero(std::bit_ceil(node_count)); } template From dce42d13eb7ca217e6814ec3249e54791b01f76f Mon Sep 17 00:00:00 2001 From: greg7mdp Date: Mon, 18 Mar 2024 13:53:11 -0400 Subject: [PATCH 04/12] Update comment. --- libraries/chain/include/eosio/chain/incremental_merkle.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/chain/include/eosio/chain/incremental_merkle.hpp b/libraries/chain/include/eosio/chain/incremental_merkle.hpp index 56708f7be0..8f06c8205f 100644 --- a/libraries/chain/include/eosio/chain/incremental_merkle.hpp +++ b/libraries/chain/include/eosio/chain/incremental_merkle.hpp @@ -18,7 +18,7 @@ namespace detail { constexpr uint64_t calculate_max_depth(uint64_t node_count) { if (node_count == 0) return 0; - // following is non-floating point equivalent to `std::ceil(std::log2(node_count)) + 1) + // following is non-floating point equivalent to `std::ceil(std::log2(node_count)) + 1)` (and about 9x faster) return 8*sizeof(node_count) - std::countl_zero(std::bit_ceil(node_count)); } From c365b9c0bb1683eab0c77602fa272f605ffd0eeb Mon Sep 17 00:00:00 2001 From: greg7mdp Date: Mon, 18 Mar 2024 16:06:01 -0400 Subject: [PATCH 05/12] Use improved version from Matt. --- libraries/chain/include/eosio/chain/incremental_merkle.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/chain/include/eosio/chain/incremental_merkle.hpp b/libraries/chain/include/eosio/chain/incremental_merkle.hpp index 8f06c8205f..68b4348e9d 100644 --- a/libraries/chain/include/eosio/chain/incremental_merkle.hpp +++ b/libraries/chain/include/eosio/chain/incremental_merkle.hpp @@ -19,7 +19,7 @@ constexpr uint64_t calculate_max_depth(uint64_t node_count) { if (node_count == 0) return 0; // following is non-floating point equivalent to `std::ceil(std::log2(node_count)) + 1)` (and about 9x faster) - return 8*sizeof(node_count) - std::countl_zero(std::bit_ceil(node_count)); + return std::bit_width(std::bit_ceil(node_count)); } template From 4f4560fac6c25cf3bbcd73d2712aaac937181e3c Mon Sep 17 00:00:00 2001 From: Matt Witherspoon <32485495+spoonincode@users.noreply.github.com> Date: Mon, 18 Mar 2024 16:38:37 -0400 Subject: [PATCH 06/12] bump EOS VM submodule to branch w/ less greedy signal handler --- libraries/eos-vm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/eos-vm b/libraries/eos-vm index ee0b142058..5996e485e7 160000 --- a/libraries/eos-vm +++ b/libraries/eos-vm @@ -1 +1 @@ -Subproject commit ee0b142058212073a78f71872dd54160ca69eb14 +Subproject commit 5996e485e75b19788c448145f8c1f37b77a536b7 From 45ceb2c90d6d68fa78c3368e80377c0a7a9fb988 Mon Sep 17 00:00:00 2001 From: Matt Witherspoon <32485495+spoonincode@users.noreply.github.com> Date: Mon, 18 Mar 2024 16:59:38 -0400 Subject: [PATCH 07/12] bump EOS VM submodule to pick up include --- libraries/eos-vm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/eos-vm b/libraries/eos-vm index 5996e485e7..9962c3728c 160000 --- a/libraries/eos-vm +++ b/libraries/eos-vm @@ -1 +1 @@ -Subproject commit 5996e485e75b19788c448145f8c1f37b77a536b7 +Subproject commit 9962c3728c66f4daa2fdc18193978b1316a6f905 From 7ab231b761c9c7c5e414335eefd3c6fb44d8b379 Mon Sep 17 00:00:00 2001 From: Matt Witherspoon <32485495+spoonincode@users.noreply.github.com> Date: Mon, 18 Mar 2024 21:46:42 -0400 Subject: [PATCH 08/12] bump eos-vm submodule to grab another include --- libraries/eos-vm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/eos-vm b/libraries/eos-vm index 9962c3728c..4f9ef0e4ff 160000 --- a/libraries/eos-vm +++ b/libraries/eos-vm @@ -1 +1 @@ -Subproject commit 9962c3728c66f4daa2fdc18193978b1316a6f905 +Subproject commit 4f9ef0e4ffea842e0f882a9828087efbad1ed105 From 307b47a912526068016a58a1e476a9c5cd54d5af Mon Sep 17 00:00:00 2001 From: greg7mdp Date: Wed, 20 Mar 2024 07:40:46 -0400 Subject: [PATCH 09/12] Avoid vector copy at each insertion. --- libraries/chain/transaction_context.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/libraries/chain/transaction_context.cpp b/libraries/chain/transaction_context.cpp index 262d7995a7..7c1a24af8b 100644 --- a/libraries/chain/transaction_context.cpp +++ b/libraries/chain/transaction_context.cpp @@ -10,6 +10,7 @@ #include #include +#include namespace eosio { namespace chain { @@ -708,11 +709,12 @@ namespace eosio { namespace chain { { uint32_t new_action_ordinal = trace->action_traces.size() + 1; - trace->action_traces.reserve( new_action_ordinal ); + trace->action_traces.reserve( std::bit_ceil(new_action_ordinal) ); // bit_ceil to avoid vector copy on every insertion const action& provided_action = get_action_trace( action_ordinal ).act; - // The reserve above is required so that the emplace_back below does not invalidate the provided_action reference. + // The reserve above is required so that the emplace_back below does not invalidate the provided_action reference, + // which references an action within the `trace->action_traces` vector we are appending to. trace->action_traces.emplace_back( *trace, provided_action, receiver, context_free, new_action_ordinal, creator_action_ordinal, From 365ef4852f070bf3901d0d343c9d69bbefea46f1 Mon Sep 17 00:00:00 2001 From: greg7mdp Date: Wed, 20 Mar 2024 07:50:10 -0400 Subject: [PATCH 10/12] Update comment. --- libraries/chain/transaction_context.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/chain/transaction_context.cpp b/libraries/chain/transaction_context.cpp index 7c1a24af8b..a535f0579e 100644 --- a/libraries/chain/transaction_context.cpp +++ b/libraries/chain/transaction_context.cpp @@ -709,7 +709,7 @@ namespace eosio { namespace chain { { uint32_t new_action_ordinal = trace->action_traces.size() + 1; - trace->action_traces.reserve( std::bit_ceil(new_action_ordinal) ); // bit_ceil to avoid vector copy on every insertion + trace->action_traces.reserve( std::bit_ceil(new_action_ordinal) ); // bit_ceil to avoid vector copy on every reserve call. const action& provided_action = get_action_trace( action_ordinal ).act; From 98940c1a3dd7a1277347118651a2b587f1dc7b0e Mon Sep 17 00:00:00 2001 From: Matt Witherspoon <32485495+spoonincode@users.noreply.github.com> Date: Fri, 22 Mar 2024 11:03:30 -0400 Subject: [PATCH 11/12] bump eos-vm submodule to main head w/ less greedy signal handler --- libraries/eos-vm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/eos-vm b/libraries/eos-vm index 4f9ef0e4ff..aa8bd0a310 160000 --- a/libraries/eos-vm +++ b/libraries/eos-vm @@ -1 +1 @@ -Subproject commit 4f9ef0e4ffea842e0f882a9828087efbad1ed105 +Subproject commit aa8bd0a310bd39aa225dcb6c73ee069f697a0ccd From d80b1437b6f1e7538e5d0c7a1f79994edccd9bfa Mon Sep 17 00:00:00 2001 From: Matt Witherspoon <32485495+spoonincode@users.noreply.github.com> Date: Fri, 22 Mar 2024 22:47:33 -0400 Subject: [PATCH 12/12] explicit capture list --- plugins/producer_api_plugin/producer_api_plugin.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/producer_api_plugin/producer_api_plugin.cpp b/plugins/producer_api_plugin/producer_api_plugin.cpp index 7e10d46357..cd59645a0b 100644 --- a/plugins/producer_api_plugin/producer_api_plugin.cpp +++ b/plugins/producer_api_plugin/producer_api_plugin.cpp @@ -23,7 +23,7 @@ using namespace eosio; #define CALL_WITH_400(api_name, category, api_handle, call_name, INVOKE, http_response_code) \ {std::string("/v1/" #api_name "/" #call_name), \ api_category::category, \ - [&](string&&, string&& body, url_response_callback&& cb) mutable { \ + [&http, &producer](string&&, string&& body, url_response_callback&& cb) mutable { \ try { \ INVOKE \ cb(http_response_code, fc::variant(result)); \