Skip to content

Commit

Permalink
Merge pull request #2341 from AntelopeIO/merge-main-03-27-2024
Browse files Browse the repository at this point in the history
IF: Merge main 03-27-2024
  • Loading branch information
heifner authored Mar 27, 2024
2 parents 39cd75a + 383c6e9 commit a2013f7
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 54 deletions.
56 changes: 7 additions & 49 deletions libraries/chain/include/eosio/chain/incremental_merkle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,24 @@
#include <eosio/chain/types.hpp>
#include <eosio/chain/merkle.hpp>
#include <fc/io/raw.hpp>
#include <bit>

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.
*
* @param node_count - the number of nodes in the implied tree
* @return the max depth of the minimal tree that stores them
*/
constexpr int calculate_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;
// following is non-floating point equivalent to `std::ceil(std::log2(node_count)) + 1)` (and about 9x faster)
return std::bit_width(std::bit_ceil(node_count));
}

template<typename ContainerA, typename ContainerB>
Expand Down Expand Up @@ -253,7 +211,7 @@ typedef incremental_merkle_impl<digest_type, true> incremental_le
typedef incremental_merkle_impl<digest_type, true, shared_vector> shared_incremental_legacy_merkle_tree;
typedef incremental_merkle_impl<digest_type> incremental_merkle_tree;

} } /// eosio::chain
} /// eosio::chain

FC_REFLECT( eosio::chain::incremental_legacy_merkle_tree, (_active_nodes)(_node_count) );
FC_REFLECT( eosio::chain::incremental_merkle_tree, (_active_nodes)(_node_count) );
6 changes: 4 additions & 2 deletions libraries/chain/transaction_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <eosio/chain/deep_mind.hpp>

#include <chrono>
#include <bit>

namespace eosio::chain {

Expand Down Expand Up @@ -710,11 +711,12 @@ namespace eosio::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 reserve call.

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,
Expand Down
4 changes: 2 additions & 2 deletions plugins/producer_api_plugin/producer_api_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)); \
Expand Down Expand Up @@ -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<in_param, http_params_types::possible_no_params>(body);\
Expand Down Expand Up @@ -91,7 +92,6 @@ void producer_api_plugin::plugin_startup() {
// lifetime of plugin is lifetime of application
auto& producer = app().get_plugin<producer_plugin>();
auto& http = app().get_plugin<http_plugin>();
fc::microseconds http_max_response_time = http.get_max_response_time();

app().get_plugin<http_plugin>().add_api({
CALL_WITH_400(producer, producer_ro, producer, paused,
Expand Down

0 comments on commit a2013f7

Please sign in to comment.