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

IF: Use set_finalizer type names that match eosio.bios and CDT #2004

Merged
merged 1 commit into from
Dec 20, 2023
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/hotstuff/test/test_hotstuff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ static finalizer_policy create_fs(std::vector<std::string> keys){
f_auths.push_back(eosio::chain::finalizer_authority{"" , 1 , pk});
}
eosio::chain::finalizer_policy fset;
fset.fthreshold = 15;
fset.threshold = 15;
fset.finalizers = f_auths;
return fset;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ namespace eosio::chain {
struct finalizer_authority {

std::string description;
uint64_t fweight = 0; // weight that this finalizer's vote has for meeting fthreshold
uint64_t weight = 0; // weight that this finalizer's vote has for meeting fthreshold
fc::crypto::blslib::bls_public_key public_key;

auto operator<=>(const finalizer_authority&) const = default;
};

} /// eosio::chain

FC_REFLECT( eosio::chain::finalizer_authority, (description)(fweight)(public_key) )
FC_REFLECT( eosio::chain::finalizer_authority, (description)(weight)(public_key) )
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace eosio::chain {
finalizer_policy& operator=(finalizer_policy&&) noexcept;

uint32_t generation = 0; ///< sequentially incrementing version number
uint64_t fthreshold = 0; ///< vote fweight threshold to finalize blocks
uint64_t threshold = 0; ///< vote weight threshold to finalize blocks
std::vector<finalizer_authority> finalizers; ///< Instant Finality voter set
};

Expand All @@ -33,5 +33,5 @@ namespace eosio::chain {

} /// eosio::chain

FC_REFLECT( eosio::chain::finalizer_policy, (generation)(fthreshold)(finalizers) )
FC_REFLECT( eosio::chain::finalizer_policy, (generation)(threshold)(finalizers) )
FC_REFLECT_DERIVED( eosio::chain::finalizer_policy_extension, (eosio::chain::finalizer_policy), )
42 changes: 21 additions & 21 deletions libraries/chain/webassembly/privileged.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,50 +153,50 @@ namespace eosio { namespace chain { namespace webassembly {
}

// format for packed_finalizer_policy
struct abi_finalizer_authority {
struct finalizer_authority {
std::string description;
uint64_t fweight = 0; // weight that this finalizer's vote has for meeting fthreshold
std::vector<uint8_t> public_key_g1_affine_le; // size 96, cdt/abi_serializer has issues with std::array
uint64_t weight = 0; // weight that this finalizer's vote has for meeting fthreshold
std::vector<uint8_t> public_key; // Affine little endian non-montgomery g1, cdt/abi_serializer has issues with std::array, size 96
};
struct abi_finalizer_policy {
uint64_t fthreshold = 0;
std::vector<abi_finalizer_authority> finalizers;
struct finalizer_policy {
uint64_t threshold = 0;
std::vector<finalizer_authority> finalizers;
};

void interface::set_finalizers(span<const char> packed_finalizer_policy) {
EOS_ASSERT(!context.trx_context.is_read_only(), wasm_execution_error, "set_finalizers not allowed in a readonly transaction");
fc::datastream<const char*> ds( packed_finalizer_policy.data(), packed_finalizer_policy.size() );
abi_finalizer_policy abi_finpol;
finalizer_policy abi_finpol;
fc::raw::unpack(ds, abi_finpol);

std::vector<abi_finalizer_authority>& finalizers = abi_finpol.finalizers;
std::vector<finalizer_authority>& finalizers = abi_finpol.finalizers;

EOS_ASSERT( finalizers.size() <= config::max_finalizers, wasm_execution_error, "Finalizer policy exceeds the maximum finalizer count for this chain" );
EOS_ASSERT( finalizers.size() > 0, wasm_execution_error, "Finalizers cannot be empty" );

std::set<bls12_381::g1> unique_finalizer_keys;

uint64_t f_weight_sum = 0;
uint64_t weight_sum = 0;

finalizer_policy finpol;
finpol.fthreshold = abi_finpol.fthreshold;
chain::finalizer_policy finpol;
finpol.threshold = abi_finpol.threshold;
for (auto& f: finalizers) {
EOS_ASSERT( f.description.size() <= config::max_finalizer_description_size, wasm_execution_error,
"Finalizer description greater than ${s}", ("s", config::max_finalizer_description_size) );
EOS_ASSERT(std::numeric_limits<uint64_t>::max() - f_weight_sum >= f.fweight, wasm_execution_error, "sum of weights causes uint64_t overflow");
f_weight_sum += f.fweight;
EOS_ASSERT(std::numeric_limits<uint64_t>::max() - weight_sum >= f.weight, wasm_execution_error, "sum of weights causes uint64_t overflow");
weight_sum += f.weight;
constexpr bool check = true; // always validate key
constexpr bool raw = false; // non-montgomery
EOS_ASSERT(f.public_key_g1_affine_le.size() == 96, wasm_execution_error, "Invalid bls public key length");
std::optional<bls12_381::g1> pk = bls12_381::g1::fromAffineBytesLE(std::span<const uint8_t,96>(f.public_key_g1_affine_le.data(), 96), check, raw);
EOS_ASSERT(f.public_key.size() == 96, wasm_execution_error, "Invalid bls public key length");
std::optional<bls12_381::g1> pk = bls12_381::g1::fromAffineBytesLE(std::span<const uint8_t,96>(f.public_key.data(), 96), check, raw);
EOS_ASSERT( pk, wasm_execution_error, "Invalid public key for: ${d}", ("d", f.description) );
EOS_ASSERT( unique_finalizer_keys.insert(*pk).second, wasm_execution_error, "Duplicate public key: ${pk}", ("pk", fc::crypto::blslib::bls_public_key{*pk}.to_string()) );
finpol.finalizers.push_back(finalizer_authority{.description = std::move(f.description),
.fweight = f.fweight,
.public_key{fc::crypto::blslib::bls_public_key{*pk}}});
finpol.finalizers.push_back(chain::finalizer_authority{.description = std::move(f.description),
.weight = f.weight,
.public_key{fc::crypto::blslib::bls_public_key{*pk}}});
}

EOS_ASSERT( finpol.fthreshold > f_weight_sum / 2, wasm_execution_error, "Finalizer policy threshold cannot be met by finalizer weights" );
EOS_ASSERT( finpol.threshold > weight_sum / 2, wasm_execution_error, "Finalizer policy threshold cannot be met by finalizer weights" );

context.control.set_proposed_finalizers( finpol );
}
Expand Down Expand Up @@ -276,5 +276,5 @@ namespace eosio { namespace chain { namespace webassembly {
}
}}} // ns eosio::chain::webassembly

FC_REFLECT(eosio::chain::webassembly::abi_finalizer_authority, (description)(fweight)(public_key_g1_affine_le));
FC_REFLECT(eosio::chain::webassembly::abi_finalizer_policy, (fthreshold)(finalizers));
FC_REFLECT(eosio::chain::webassembly::finalizer_authority, (description)(weight)(public_key));
FC_REFLECT(eosio::chain::webassembly::finalizer_policy, (threshold)(finalizers));
2 changes: 1 addition & 1 deletion unittests/api_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3882,7 +3882,7 @@ BOOST_AUTO_TEST_CASE(set_finalizer_test) { try {
BOOST_TEST(!!ext);
BOOST_TEST(std::get<finalizer_policy_extension>(*ext).finalizers.size() == finalizers.size());
BOOST_TEST(std::get<finalizer_policy_extension>(*ext).generation == 1);
BOOST_TEST(std::get<finalizer_policy_extension>(*ext).fthreshold == finalizers.size() / 3 * 2 + 1);
BOOST_TEST(std::get<finalizer_policy_extension>(*ext).threshold == finalizers.size() / 3 * 2 + 1);

// old dpos still in affect until block is irreversible
BOOST_TEST(block->confirmed == 0);
Expand Down