Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify API for accessing controller's head #368

Merged
merged 14 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libraries/chain/apply_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ void apply_context::schedule_deferred_transaction( const uint128_t& sender_id, a
trx.ref_block_prefix = 0;
} else {
trx.expiration = time_point_sec{control.pending_block_time() + fc::microseconds(999'999)}; // Rounds up to nearest second (makes expiration check unnecessary)
trx.set_reference_block(control.head_block_id()); // No TaPoS check necessary
trx.set_reference_block(control.head().id()); // No TaPoS check necessary
}

// Charge ahead of time for the additional net usage needed to retire the deferred transaction
Expand Down
15 changes: 13 additions & 2 deletions libraries/chain/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,13 @@ struct controller_impl {
}

// --------------- access fork_db head ----------------------------------------------------------------------
block_handle fork_db_head()const {
return fork_db.apply<block_handle>(
[&](const auto& forkdb) {
return block_handle{forkdb.head(include_root_t::yes)};
});
}

uint32_t fork_db_head_block_num() const {
return fork_db.apply<uint32_t>(
[&](const auto& forkdb) {
Expand Down Expand Up @@ -2989,7 +2996,7 @@ struct controller_impl {

EOS_ASSERT( skip_db_sessions(s) || db.revision() == chain_head.block_num(), database_exception,
"db revision is not on par with head block",
("db.revision()", db.revision())("controller_head_block", chain_head.block_num())("fork_db_head_block", fork_db_head_block_num()) );
("db.revision()", db.revision())("controller_head_block", chain_head.block_num())("fork_db_head_block", fork_db_head().block_num()) );

block_handle_accessor::apply<void>(chain_head, overloaded{
[&](const block_state_legacy_ptr& head) {
Expand Down Expand Up @@ -4319,7 +4326,7 @@ struct controller_impl {
//Look for expired transactions in the deduplication list, and remove them.
auto& transaction_idx = db.get_mutable_index<transaction_multi_index>();
const auto& dedupe_index = transaction_idx.indices().get<by_expiration>();
auto now = is_building_block() ? pending_block_time() : chain_head.block_time().to_time_point();
auto now = is_building_block() ? pending_block_time() : chain_head.timestamp().to_time_point();
const auto total = dedupe_index.size();
uint32_t num_removed = 0;
while( (!dedupe_index.empty()) && ( now > dedupe_index.begin()->expiration.to_time_point() ) ) {
Expand Down Expand Up @@ -5120,6 +5127,10 @@ std::optional<finality_data_t> controller::head_finality_data() const {
return my->head_finality_data();
}

block_handle controller::fork_db_head()const {
return my->fork_db_head();
}

uint32_t controller::fork_db_head_block_num()const {
return my->fork_db_head_block_num();
}
Expand Down
4 changes: 2 additions & 2 deletions libraries/chain/eosio_contract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ void apply_eosio_setcode(apply_context& context) {
old_size = (int64_t)old_code_entry.code.size() * config::setcode_ram_bytes_multiplier;
if( old_code_entry.code_ref_count == 1 ) {
db.remove(old_code_entry);
context.control.code_block_num_last_used(account.code_hash, account.vm_type, account.vm_version, context.control.head_block_num() + 1);
context.control.code_block_num_last_used(account.code_hash, account.vm_type, account.vm_version, context.control.head().block_num() + 1);
} else {
db.modify(old_code_entry, [](code_object& o) {
--o.code_ref_count;
Expand All @@ -180,7 +180,7 @@ void apply_eosio_setcode(apply_context& context) {
o.code_hash = code_hash;
o.code.assign(act.code.data(), code_size);
o.code_ref_count = 1;
o.first_block_used = context.control.head_block_num() + 1;
o.first_block_used = context.control.head().block_num() + 1;
o.vm_type = act.vmtype;
o.vm_version = act.vmversion;
});
Expand Down
3 changes: 2 additions & 1 deletion libraries/chain/include/eosio/chain/block_handle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ struct block_handle {
bool is_valid() const { return !_bsp.valueless_by_exception() && std::visit([](const auto& bsp) { return !!bsp; }, _bsp); }

uint32_t block_num() const { return std::visit([](const auto& bsp) { return bsp->block_num(); }, _bsp); }
block_timestamp_type block_time() const { return std::visit([](const auto& bsp) { return bsp->timestamp(); }, _bsp); };
block_timestamp_type timestamp() const { return std::visit([](const auto& bsp) { return bsp->timestamp(); }, _bsp); };
time_point block_time() const { return std::visit([](const auto& bsp) { return time_point{bsp->timestamp()}; }, _bsp); };
linh2931 marked this conversation as resolved.
Show resolved Hide resolved
const block_id_type& id() const { return std::visit<const block_id_type&>([](const auto& bsp) -> const block_id_type& { return bsp->id(); }, _bsp); }
const block_id_type& previous() const { return std::visit<const block_id_type&>([](const auto& bsp) -> const block_id_type& { return bsp->previous(); }, _bsp); }
const signed_block_ptr& block() const { return std::visit<const signed_block_ptr&>([](const auto& bsp) -> const signed_block_ptr& { return bsp->block; }, _bsp); }
Expand Down
21 changes: 12 additions & 9 deletions libraries/chain/include/eosio/chain/controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,21 +240,24 @@ namespace eosio::chain {
void set_disable_replay_opts( bool v );

block_handle head()const;
uint32_t head_block_num()const;
time_point head_block_time()const;
block_timestamp_type head_block_timestamp()const;
block_id_type head_block_id()const;
account_name head_block_producer()const;
const block_header& head_block_header()const;
const signed_block_ptr& head_block()const;
block_handle fork_db_head()const;

[[deprecated("Use head().block_num().")]] uint32_t head_block_num()const;
[[deprecated("Use head().block_time().")]] time_point head_block_time()const;
[[deprecated("Use head().timestamp().")]] block_timestamp_type head_block_timestamp()const;
[[deprecated("Use head().id().")]] block_id_type head_block_id()const;
[[deprecated("Use head().producer().")]] account_name head_block_producer()const;
[[deprecated("Use head().header().")]] const block_header& head_block_header()const;
[[deprecated("Use head().block().")]] const signed_block_ptr& head_block()const;

// returns nullptr after instant finality enabled
block_state_legacy_ptr head_block_state_legacy()const;
// returns finality_data associated with chain head for SHiP when in Savanna,
// std::nullopt in Legacy
std::optional<finality_data_t> head_finality_data() const;

uint32_t fork_db_head_block_num()const;
block_id_type fork_db_head_block_id()const;
[[deprecated("Use fork_db_head().block_num().")]] uint32_t fork_db_head_block_num()const;
[[deprecated("Use fork_db_head().id().")]] block_id_type fork_db_head_block_id()const;

time_point pending_block_time()const;
block_timestamp_type pending_block_timestamp()const;
Expand Down
6 changes: 3 additions & 3 deletions libraries/chain/snapshot_scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,9 @@ void snapshot_scheduler::execute_snapshot(uint32_t srid, chain::controller& chai
}

void snapshot_scheduler::create_snapshot(next_function<snapshot_information> next, chain::controller& chain, std::function<void(void)> predicate) {
auto head_id = chain.head_block_id();
const auto head_block_num = chain.head_block_num();
const auto head_block_time = chain.head_block_time();
auto head_id = chain.head().id();
const auto head_block_num = chain.head().block_num();
const auto head_block_time = chain.head().block_time();
const auto& snapshot_path = pending_snapshot<snapshot_information>::get_final_path(head_id, _snapshots_dir);
const auto& temp_path = pending_snapshot<snapshot_information>::get_temp_path(head_id, _snapshots_dir);

Expand Down
6 changes: 3 additions & 3 deletions libraries/chain/transaction_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ namespace eosio::chain {
undo_session.emplace(c.mutable_db().start_undo_session(true));
}
trace->id = id;
trace->block_num = c.head_block_num() + 1;
trace->block_num = c.head().block_num() + 1;
trace->block_time = c.pending_block_time();
trace->producer_block_id = c.pending_producer_block_id();

Expand Down Expand Up @@ -838,15 +838,15 @@ namespace eosio::chain {
EOS_ASSERT(producers.size() <= config::max_proposers, wasm_execution_error,
"Producer schedule exceeds the maximum proposer count for this chain");

trx_blk_context.proposed_schedule_block_num = control.head_block_num() + 1;
trx_blk_context.proposed_schedule_block_num = control.head().block_num() + 1;
// proposed_schedule.version is set in assemble_block
trx_blk_context.proposed_schedule.producers = std::move(producers);

return std::numeric_limits<uint32_t>::max();
}

void transaction_context::set_proposed_finalizers(finalizer_policy&& fin_pol) {
trx_blk_context.proposed_fin_pol_block_num = control.head_block_num() + 1;
trx_blk_context.proposed_fin_pol_block_num = control.head().block_num() + 1;
trx_blk_context.proposed_fin_pol = std::move(fin_pol);
}

Expand Down
12 changes: 8 additions & 4 deletions libraries/testing/include/eosio/testing/tester.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -507,14 +507,18 @@ namespace eosio::testing {
return {cfg, gen};
}

// ideally, users of `tester` should not access the controller directly
// so we provide APIs to access the chain head and fork_db head
// --------------------------------------------------------------------
block_handle head() const { return control->head(); }
block_handle fork_db_head() const { return control->fork_db_head(); }

// checks that the active `finalizer_policy` for `block` matches the
// passed `generation` and `keys_span`.
// -----------------------------------------------------------------
void check_head_finalizer_policy(uint32_t generation,
std::span<const bls_public_key> keys_span) {
auto finpol = active_finalizer_policy(control->head_block_header().calculate_id());
auto finpol = active_finalizer_policy(control->head().header().calculate_id());
BOOST_REQUIRE(!!finpol);
BOOST_REQUIRE_EQUAL(finpol->generation, generation);
BOOST_REQUIRE_EQUAL(keys_span.size(), finpol->finalizers.size());
Expand Down Expand Up @@ -789,9 +793,9 @@ namespace eosio::testing {
}

bool validate() {
const auto& hbh = control->head_block_header();
const auto& vn_hbh = validating_node->head_block_header();
bool ok = control->head_block_id() == validating_node->head_block_id() &&
const auto& hbh = control->head().header();
const auto& vn_hbh = validating_node->head().header();
bool ok = control->head().id() == validating_node->head().id() &&
hbh.previous == vn_hbh.previous &&
hbh.timestamp == vn_hbh.timestamp &&
hbh.transaction_mroot == vn_hbh.transaction_mroot &&
Expand Down
26 changes: 13 additions & 13 deletions libraries/testing/tester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ namespace eosio::testing {
}

bool base_tester::is_same_chain( base_tester& other ) {
return control->head_block_id() == other.control->head_block_id();
return control->head().id() == other.control->head().id();
}

void base_tester::init(const setup_policy policy, db_read_mode read_mode, std::optional<uint32_t> genesis_max_inline_action_size) {
Expand Down Expand Up @@ -414,7 +414,7 @@ namespace eosio::testing {
produce_block_result_t base_tester::_produce_block( fc::microseconds skip_time, bool skip_pending_trxs, bool no_throw ) {
produce_block_result_t res;

auto head_time = control->head_block_time();
auto head_time = control->head().block_time();
auto next_time = head_time + skip_time;
static transaction_trace_ptr onblock_trace;

Expand Down Expand Up @@ -458,7 +458,7 @@ namespace eosio::testing {
}

transaction_trace_ptr base_tester::_start_block(fc::time_point block_time) {
auto head_block_number = control->head_block_num();
auto head_block_number = control->head().block_num();
auto producer = control->head_active_producers().get_scheduled_producer(block_time);

auto last_produced_block_num = control->last_irreversible_block_num();
Expand Down Expand Up @@ -575,7 +575,7 @@ namespace eosio::testing {
while(true) {
blocks_per_round = control->active_producers().producers.size() * config::producer_repetitions;
produce_block();
if (control->head_block_num() % blocks_per_round == (blocks_per_round - 1)) break;
if (control->head().block_num() % blocks_per_round == (blocks_per_round - 1)) break;
}
}

Expand Down Expand Up @@ -603,8 +603,8 @@ namespace eosio::testing {


void base_tester::set_transaction_headers( transaction& trx, uint32_t expiration, uint32_t delay_sec ) const {
trx.expiration = fc::time_point_sec{control->head_block_time() + fc::seconds(expiration)};
trx.set_reference_block( control->head_block_id() );
trx.expiration = fc::time_point_sec{control->head().block_time() + fc::seconds(expiration)};
trx.set_reference_block( control->head().id() );

trx.max_net_usage_words = 0; // No limit
trx.max_cpu_usage_ms = 0; // No limit
Expand Down Expand Up @@ -664,7 +664,7 @@ namespace eosio::testing {
)
{ try {
if( !control->is_building_block() )
_start_block(control->head_block_time() + fc::microseconds(config::block_interval_us));
_start_block(control->head().block_time() + fc::microseconds(config::block_interval_us));

auto ptrx = std::make_shared<packed_transaction>(trx);
auto time_limit = deadline == fc::time_point::maximum() ?
Expand All @@ -685,7 +685,7 @@ namespace eosio::testing {
)
{ try {
if( !control->is_building_block() )
_start_block(control->head_block_time() + fc::microseconds(config::block_interval_us));
_start_block(control->head().block_time() + fc::microseconds(config::block_interval_us));
auto c = packed_transaction::compression_type::none;

if( fc::raw::pack_size(trx) > 1000 ) {
Expand Down Expand Up @@ -1147,14 +1147,14 @@ namespace eosio::testing {

void base_tester::sync_with(base_tester& other) {
// Already in sync?
if (control->head_block_id() == other.control->head_block_id())
if (control->head().id() == other.control->head().id())
return;
// If other has a longer chain than we do, sync it to us first
if (control->head_block_num() < other.control->head_block_num())
if (control->head().block_num() < other.control->head().block_num())
return other.sync_with(*this);

auto sync_dbs = [](base_tester& a, base_tester& b) {
for( uint32_t i = 1; i <= a.control->head_block_num(); ++i ) {
for( uint32_t i = 1; i <= a.control->head().block_num(); ++i ) {

auto block = a.control->fetch_block_by_number(i);
if( block ) { //&& !b.control->is_known_block(block->id()) ) {
Expand Down Expand Up @@ -1360,9 +1360,9 @@ namespace eosio::testing {
void base_tester::preactivate_builtin_protocol_features(const std::vector<builtin_protocol_feature_t>& builtins) {
const auto& pfm = control->get_protocol_feature_manager();
const auto& pfs = pfm.get_protocol_feature_set();
const auto current_block_num = control->head_block_num() + (control->is_building_block() ? 1 : 0);
const auto current_block_num = control->head().block_num() + (control->is_building_block() ? 1 : 0);
const auto current_block_time = ( control->is_building_block() ? control->pending_block_time()
: control->head_block_time() + fc::milliseconds(config::block_interval_ms) );
: control->head().block_time() + fc::milliseconds(config::block_interval_ms) );

set<digest_type> preactivation_set;
vector<digest_type> preactivations;
Expand Down
2 changes: 1 addition & 1 deletion plugins/chain_plugin/account_query_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ namespace eosio::chain_apis {

// build a initial time to block number map
const auto lib_num = controller.last_irreversible_block_num();
const auto head_num = controller.head_block_num();
const auto head_num = controller.head().block_num();

for (uint32_t block_num = lib_num + 1; block_num <= head_num; block_num++) {
const auto block_p = controller.fetch_block_by_number(block_num);
Expand Down
20 changes: 10 additions & 10 deletions plugins/chain_plugin/chain_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1019,7 +1019,7 @@ void chain_plugin_impl::plugin_initialize(const variables_map& options) {
} );

get_head_block_id_provider = app().get_method<methods::get_head_block_id>().register_provider( [this]() {
return chain->head_block_id();
return chain->head().id();
} );

get_last_irreversible_block_number_provider = app().get_method<methods::get_last_irreversible_block_number>().register_provider(
Expand Down Expand Up @@ -1132,10 +1132,10 @@ void chain_plugin_impl::plugin_startup()

if (genesis) {
ilog("Blockchain started; head block is #${num}, genesis timestamp is ${ts}",
("num", chain->head_block_num())("ts", genesis->initial_timestamp));
("num", chain->head().block_num())("ts", genesis->initial_timestamp));
}
else {
ilog("Blockchain started; head block is #${num}", ("num", chain->head_block_num()));
ilog("Blockchain started; head block is #${num}", ("num", chain->head().block_num()));
}

chain_config.reset();
Expand Down Expand Up @@ -1289,9 +1289,9 @@ const string read_only::KEYi64 = "i64";
read_only::get_info_results read_only::get_info(const read_only::get_info_params&, const fc::time_point&) const {
const auto& rm = db.get_resource_limits_manager();

auto head_id = db.head_block_id();
auto head_id = db.head().id();
auto lib_id = db.last_irreversible_block_id();
auto fhead_id = db.fork_db_head_block_id();
auto fhead_id = db.fork_db_head().id();

return {
itoh(static_cast<uint32_t>(app().version())),
Expand All @@ -1300,8 +1300,8 @@ read_only::get_info_results read_only::get_info(const read_only::get_info_params
block_header::num_from_id(lib_id),
lib_id,
head_id,
db.head_block_time(),
db.head_block_producer(),
db.head().block_time(),
db.head().producer(),
rm.get_virtual_block_cpu_limit(),
rm.get_virtual_block_net_limit(),
rm.get_block_cpu_limit(),
Expand Down Expand Up @@ -2392,8 +2392,8 @@ read_only::get_account_return_t read_only::get_account( const get_account_params
const auto& d = db.db();
const auto& rm = db.get_resource_limits_manager();

result.head_block_num = db.head_block_num();
result.head_block_time = db.head_block_time();
result.head_block_num = db.head().block_num();
result.head_block_time = db.head().block_time();

rm.get_account_limits( result.account_name, result.ram_quota, result.net_weight, result.cpu_weight );

Expand All @@ -2405,7 +2405,7 @@ read_only::get_account_return_t read_only::get_account( const get_account_params
result.created = accnt_obj.creation_date;

uint32_t greylist_limit = db.is_resource_greylisted(result.account_name) ? 1 : config::maximum_elastic_resource_multiplier;
const block_timestamp_type current_usage_time (db.head_block_time());
const block_timestamp_type current_usage_time (db.head().block_time());
result.net_limit.set( rm.get_account_net_limit_ex( result.account_name, greylist_limit, current_usage_time).first );
if ( result.net_limit.last_usage_update_time && (result.net_limit.last_usage_update_time->slot == 0) ) { // account has no action yet
result.net_limit.last_usage_update_time = accnt_obj.creation_date;
Expand Down
Loading
Loading