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

[1.0-beta1.1] Store proposed producers and proposed finalizers in building block #189

Merged
merged 2 commits into from
May 23, 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/block_header_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ void finish_next(const block_header_state& prev,
if (if_ext.new_proposer_policy) {
// called when assembling the block
next_header_state.proposer_policies[if_ext.new_proposer_policy->active_time] =
std::move(if_ext.new_proposer_policy);
std::make_shared<proposer_policy>(std::move(*if_ext.new_proposer_policy));
}

// finality_core
Expand Down
202 changes: 75 additions & 127 deletions libraries/chain/controller.cpp

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion libraries/chain/include/eosio/chain/block_header_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ struct building_block_input {
// this struct can be extracted from a building block
struct block_header_state_input : public building_block_input {
digest_type transaction_mroot; // Comes from std::get<checksum256_type>(building_block::trx_mroot_or_receipt_digests)
std::shared_ptr<proposer_policy> new_proposer_policy; // Comes from building_block::new_proposer_policy
std::optional<proposer_policy> new_proposer_policy; // Comes from building_block::new_proposer_policy
std::optional<finalizer_policy> new_finalizer_policy; // Comes from building_block::new_finalizer_policy
qc_claim_t most_recent_ancestor_with_qc; // Comes from traversing branch from parent and calling get_best_qc()
digest_type finality_mroot_claim;
Expand Down
9 changes: 6 additions & 3 deletions libraries/chain/include/eosio/chain/controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ namespace eosio::chain {
using chainbase::pinnable_mapped_file;
using boost::signals2::signal;

class transaction_context;
struct trx_block_context;
class dynamic_global_property_object;
class global_property_object;
class permission_object;
Expand Down Expand Up @@ -327,10 +329,11 @@ namespace eosio::chain {

bool is_known_unexpired_transaction( const transaction_id_type& id) const;

int64_t set_proposed_producers( vector<producer_authority> producers );
// called by host function
int64_t set_proposed_producers( transaction_context& trx_context, vector<producer_authority> producers );

void apply_trx_block_context( trx_block_context& trx_blk_context );

// called by host function set_finalizers
void set_proposed_finalizers( finalizer_policy&& fin_pol );
// called from net threads
void process_vote_message( uint32_t connection_id, const vote_message_ptr& msg );
// thread safe, for testing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct instant_finality_extension : fc::reflect_init {
instant_finality_extension() = default;
instant_finality_extension(qc_claim_t qc_claim,
std::optional<finalizer_policy> new_finalizer_policy,
std::shared_ptr<proposer_policy> new_proposer_policy) :
std::optional<proposer_policy> new_proposer_policy) :
qc_claim(qc_claim),
new_finalizer_policy(std::move(new_finalizer_policy)),
new_proposer_policy(std::move(new_proposer_policy))
Expand All @@ -27,7 +27,7 @@ struct instant_finality_extension : fc::reflect_init {

qc_claim_t qc_claim;
std::optional<finalizer_policy> new_finalizer_policy;
std::shared_ptr<proposer_policy> new_proposer_policy;
std::optional<proposer_policy> new_proposer_policy;
};

} /// eosio::chain
Expand Down
12 changes: 0 additions & 12 deletions libraries/chain/include/eosio/chain/global_property_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,6 @@ namespace eosio::chain {
chain_config configuration;
chain_id_type chain_id;
wasm_config wasm_configuration;
// Note: proposed_fin_pol_block_num and proposed_fin_pol are used per block
// and reset after uses. They do not need to be stored in snapshots.
std::optional<block_num_type> proposed_fin_pol_block_num;
finalizer_policy proposed_fin_pol;

// For snapshot_global_property_object_v2 and initialize_from( const T& legacy )
template<typename T>
Expand All @@ -115,10 +111,6 @@ namespace eosio::chain {
} else {
wasm_configuration = legacy.wasm_configuration;
}

// proposed_fin_pol_block_num and proposed_fin_pol are set to default values.
proposed_fin_pol_block_num = std::nullopt;
proposed_fin_pol = finalizer_policy{};
}

// For snapshot_global_property_object v3, v4, and v5
Expand Down Expand Up @@ -162,10 +154,6 @@ namespace eosio::chain {
value.configuration = row.configuration;
value.chain_id = row.chain_id;
value.wasm_configuration = row.wasm_configuration;
// Snapshot does not contain proposed_fin_pol_block_num and proposed_fin_pol.
// Return their default values
value.proposed_fin_pol_block_num = std::nullopt;
value.proposed_fin_pol = finalizer_policy{};
}
};
}
Expand Down
29 changes: 26 additions & 3 deletions libraries/chain/include/eosio/chain/transaction_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
#include <eosio/chain/controller.hpp>
#include <eosio/chain/trace.hpp>
#include <eosio/chain/platform_timer.hpp>
#include <signal.h>

namespace eosio::benchmark {
struct interface_in_benchmark; // for benchmark testing
}

namespace eosio { namespace chain {
namespace eosio::chain {

struct transaction_checktime_timer {
public:
Expand Down Expand Up @@ -79,6 +78,26 @@ namespace eosio { namespace chain {
}
};

// transaction side affects to apply to block when block is assembled
struct trx_block_context {
std::optional<block_num_type> proposed_schedule_block_num;
producer_authority_schedule proposed_schedule;

std::optional<block_num_type> proposed_fin_pol_block_num;
finalizer_policy proposed_fin_pol;

void apply(trx_block_context&& rhs) {
if (rhs.proposed_schedule_block_num) {
proposed_schedule_block_num = rhs.proposed_schedule_block_num;
proposed_schedule = std::move(rhs.proposed_schedule);
}
if (rhs.proposed_fin_pol_block_num) {
proposed_fin_pol_block_num = rhs.proposed_fin_pol_block_num;
proposed_fin_pol = std::move(rhs.proposed_fin_pol);
}
}
};

class transaction_context {
private:
void init( uint64_t initial_net_usage);
Expand Down Expand Up @@ -139,6 +158,9 @@ namespace eosio { namespace chain {
bool is_read_only()const { return trx_type == transaction_metadata::trx_type::read_only; };
bool is_transient()const { return trx_type == transaction_metadata::trx_type::read_only || trx_type == transaction_metadata::trx_type::dry_run; };

int64_t set_proposed_producers(vector<producer_authority> producers);
void set_proposed_finalizers(finalizer_policy&& fin_pol);

private:

friend struct controller_impl;
Expand Down Expand Up @@ -228,6 +250,7 @@ namespace eosio { namespace chain {
int64_t billing_timer_exception_code = block_cpu_usage_exceeded::code_value;
fc::time_point pseudo_start;
fc::microseconds billed_time;
trx_block_context trx_blk_context;

enum class tx_cpu_usage_exceeded_reason {
account_cpu_limit, // includes subjective billing
Expand All @@ -239,4 +262,4 @@ namespace eosio { namespace chain {
tx_cpu_usage_exceeded_reason tx_cpu_usage_reason = tx_cpu_usage_exceeded_reason::account_cpu_limit;
};

} }
} // namespace eosio::chain
20 changes: 19 additions & 1 deletion libraries/chain/transaction_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <eosio/chain/global_property_object.hpp>
#include <eosio/chain/deep_mind.hpp>

#include <chrono>
#include <bit>

namespace eosio::chain {
Expand Down Expand Up @@ -407,6 +406,7 @@ namespace eosio::chain {

void transaction_context::squash() {
if (undo_session) undo_session->squash();
control.apply_trx_block_context(trx_blk_context);
}

void transaction_context::undo() {
Expand Down Expand Up @@ -831,5 +831,23 @@ namespace eosio::chain {
}
}

int64_t transaction_context::set_proposed_producers(vector<producer_authority> producers) {
if (producers.empty())
return -1; // INSTANT_FINALITY depends on DISALLOW_EMPTY_PRODUCER_SCHEDULE

EOS_ASSERT(producers.size() <= config::max_producers, 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;
// proposed_schedule.version is set in assemble_block
trx_blk_context.proposed_schedule.producers = std::move(producers);

return std::numeric_limits<uint32_t>::max();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this meant to be a uint32_t instead of int64_t? I realize that's how it was before this PR but the types being mismatched here looks sus

Copy link
Member Author

@heifner heifner May 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that is in accordance to what @arhag requested.

* @return pre-savanna: -1 if proposing a new producer schedule was unsuccessful, otherwise returns the version of the new proposed schedule.
* post-savanna: -1 if proposing a new producer schedule was unsuccessful, otherwise returns max uint32_t

}

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 = std::move(fin_pol);
}

} /// eosio::chain
4 changes: 2 additions & 2 deletions libraries/chain/webassembly/privileged.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ namespace eosio { namespace chain { namespace webassembly {
}
EOS_ASSERT( producers.size() == unique_producers.size(), wasm_execution_error, "duplicate producer name in producer schedule" );

return context.control.set_proposed_producers( std::move(producers) );
return context.control.set_proposed_producers( context.trx_context, std::move(producers) );
}

uint32_t interface::get_wasm_parameters_packed( span<char> packed_parameters, uint32_t max_version ) const {
Expand Down Expand Up @@ -206,7 +206,7 @@ namespace eosio { namespace chain { namespace webassembly {
"and less than or equal to the sum of the weights",
("t", finpol.threshold)("w", weight_sum) );

context.control.set_proposed_finalizers( std::move(finpol) );
context.trx_context.set_proposed_finalizers( std::move(finpol) );
}

uint32_t interface::get_blockchain_parameters_packed( legacy_span<char> packed_blockchain_parameters ) const {
Expand Down
8 changes: 4 additions & 4 deletions unittests/block_header_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ BOOST_AUTO_TEST_CASE(instant_finality_extension_with_empty_values_test)
header.header_extensions,
instant_finality_extension::extension_id(),
fc::raw::pack( instant_finality_extension{qc_claim_t{last_qc_block_num, is_last_strong_qc},
std::optional<finalizer_policy>{}, std::shared_ptr<proposer_policy>{}} )
std::optional<finalizer_policy>{}, std::optional<proposer_policy>{}} )
);

std::optional<block_header_extension> ext = header.extract_header_extension(instant_finality_extension::extension_id());
Expand All @@ -46,7 +46,7 @@ BOOST_AUTO_TEST_CASE(instant_finality_extension_uniqueness_test)
header.header_extensions,
instant_finality_extension::extension_id(),
fc::raw::pack( instant_finality_extension{qc_claim_t{0, false}, {std::nullopt},
std::shared_ptr<proposer_policy>{}} )
std::optional<proposer_policy>{}} )
);

std::vector<finalizer_authority> finalizers { {"test description", 50, fc::crypto::blslib::bls_public_key{"PUB_BLS_qVbh4IjYZpRGo8U_0spBUM-u-r_G0fMo4MzLZRsKWmm5uyeQTp74YFaMN9IDWPoVVT5rj_Tw1gvps6K9_OZ6sabkJJzug3uGfjA6qiaLbLh5Fnafwv-nVgzzzBlU2kwRrcHc8Q" }} };
Expand All @@ -60,7 +60,7 @@ BOOST_AUTO_TEST_CASE(instant_finality_extension_uniqueness_test)
emplace_extension(
header.header_extensions,
instant_finality_extension::extension_id(),
fc::raw::pack( instant_finality_extension{qc_claim_t{100, true}, new_finalizer_policy, new_proposer_policy} )
fc::raw::pack( instant_finality_extension{qc_claim_t{100, true}, new_finalizer_policy, *new_proposer_policy} )
);

BOOST_CHECK_THROW(header.validate_and_extract_header_extensions(), invalid_block_header_extension);
Expand All @@ -84,7 +84,7 @@ BOOST_AUTO_TEST_CASE(instant_finality_extension_with_values_test)
emplace_extension(
header.header_extensions,
instant_finality_extension::extension_id(),
fc::raw::pack( instant_finality_extension{qc_claim_t{last_qc_block_num, is_strong_qc}, new_finalizer_policy, new_proposer_policy} )
fc::raw::pack( instant_finality_extension{qc_claim_t{last_qc_block_num, is_strong_qc}, new_finalizer_policy, *new_proposer_policy} )
);

std::optional<block_header_extension> ext = header.extract_header_extension(instant_finality_extension::extension_id());
Expand Down
Loading