Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
Refactor verification codepaths in beacon client (paritytech#835)
Browse files Browse the repository at this point in the history
Co-authored-by: ron <[email protected]>
  • Loading branch information
vgeddes and yrong authored May 18, 2023
1 parent eb4e768 commit 54ab138
Show file tree
Hide file tree
Showing 33 changed files with 631 additions and 932 deletions.
20 changes: 3 additions & 17 deletions core/packages/test/scripts/configure-beacon.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,12 @@ source scripts/xcm-helper.sh

config_beacon_checkpoint()
{
pushd $root_dir
check_point_call=$($relay_bin generate-beacon-checkpoint --spec $active_spec --url $beacon_endpoint_http)
popd
send_governance_transact_from_relaychain $bridgehub_para_id "$check_point_call" 180000000000 900000
}

config_beacon_relayer()
{
# Configure beacon relay
jq \
--arg beacon_endpoint_http $beacon_endpoint_http \
--arg active_spec $active_spec \
'
.source.beacon.endpoint = $beacon_endpoint_http
| .source.beacon.activeSpec = $active_spec
' \
config/beacon-relay.json > $output_dir/beacon-relay.json
}

wait_beacon_chain_ready()
{
initial_beacon_block=""
Expand All @@ -38,14 +27,11 @@ wait_beacon_chain_ready()
function configure_beacon()
{
wait_beacon_chain_ready
config_beacon_relayer
config_beacon_checkpoint
}

if [ -z "${from_start_services:-}" ]; then
echo "config beacon checkpoint only!"
wait_beacon_chain_ready
config_beacon_relayer
config_beacon_checkpoint
configure_beacon
wait
fi
Original file line number Diff line number Diff line change
Expand Up @@ -1099,7 +1099,6 @@ pub fn sync_committee_update() -> SyncCommitteeUpdate {
sync_committee_bits: hex!("ff7ffffdbffffff7edffd5f4fffffd79ffce7ff7dff6ffcdefb9fffdffff3bffdedbffbf7fff9fdfe7bf5dfebfbffeffffe7e1fffbfeefeb77bfdfa7ffddffbb"),
sync_committee_signature: hex!("a58417f897521dc58fa339c9465b1346a7005251ad582fafae984918606005b23627eface374912c70e4ed28c4358eca03783419e3625ab1159b9562837581c6647fc960a6a7d54b0a811b1d5af853ee8274d9023a195f14530d46b8f6af07c2").into(),
},
sync_committee_period: 636,
signature_slot: 5217992,
block_roots_root: hex!("f46cd66ac816b7b890d37832c0a2fa8b56984edd7266926be26dbecf6bd93f62").into(),
block_roots_branch: vec![
Expand Down Expand Up @@ -1154,7 +1153,7 @@ pub fn finalized_header_update() -> FinalizedHeaderUpdate {

pub fn header_update() -> ExecutionHeaderUpdate {
return ExecutionHeaderUpdate {
beacon_header: BeaconHeader{
header: BeaconHeader{
slot: 5217982,
proposer_index: 320636,
parent_root: hex!("a4571a17210f45c48e1cf7732817d5a38c18f5d4c5642845978ed47ea7129ce9").into(),
Expand Down Expand Up @@ -1184,12 +1183,8 @@ pub fn header_update() -> ExecutionHeaderUpdate {
hex!("db56114e00fdd4c1f85c892bf35ac9a89289aaecb1ebd0a96cde606a748b5d71").into(),
hex!("6fcc83169ef7b54eb9af29f63890644ac3fd1bffeb6edee8945bb13d9df802e9").into(),
].try_into().expect("too many branch proof items"),
sync_aggregate: SyncAggregate{
sync_committee_bits: hex!("ff7ffefdbfffbff7edffd5b4fffffd39ff4c7f77dff6dfcdefa17ffdffff3bffded9fdbd7fff9ddfe7bf5dfeafb7feff7fe3e1fffbffefef77bfdfa7f7ddffb3"),
sync_committee_signature: hex!("9642b8a94e17eff18c50b1e0603e062fd7637e121d34a4a91e5ad48a9b8076497909de6c9c8e61980a5b33fa6f412d9519bb6f710f0144dfddd01eb36b18e9109ff7cdabc40752d15da22371214189320bc7588090ce4bfafeaee84972f4c7cf").into(),
},
signature_slot: 5217983,
block_root_branch: vec![
block_roots_root: hex!("52c9f5ef64da062dee00a30ebe16710a3ffe59cf91eedf96a69ae5d198d7d982").into(),
block_roots_branch: vec![
hex!("8406114752f4b0df307983b3d233e56b81822dcfcf37a04d31c03062582047a4").into(),
hex!("7d1e19255fd12ccb31eff0832762c57cc6fcfe635191cb4afa9f1640b592e64f").into(),
hex!("8de2db9fecd1827ab497b96968e1a880667aba9b55f73a76e5b16810fdbfd392").into(),
Expand All @@ -1204,6 +1199,5 @@ pub fn header_update() -> ExecutionHeaderUpdate {
hex!("f3b04813e6897bb1642c1c42f1edfdaf8b00b2b1b57d567e512dc1c5d51660db").into(),
hex!("8795c1259089af496aa6339978f58f2fe25f89225dc97dd82dd0c3739fe94d10").into(),
],
block_root_branch_header_root: hex!("52c9f5ef64da062dee00a30ebe16710a3ffe59cf91eedf96a69ae5d198d7d982").into(),
};
}

This file was deleted.

30 changes: 24 additions & 6 deletions parachain/pallets/ethereum-beacon-client/src/benchmarking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,29 @@ use data_mainnet::*;
mod util;
use primitives::{
fast_aggregate_verify, fast_aggregate_verify_legacy, prepare_aggregate_pubkey,
prepare_aggregate_signature, Mode,
prepare_aggregate_signature, verify_merkle_branch, Mode,
};
use util::*;

benchmarks! {
sync_committee_period_update {
let caller: T::AccountId = whitelisted_caller();

let initial_sync_data = initial_sync();
let sync_committee_update = initialize_sync_committee::<T>()?;

let period = EthereumBeaconClient::<T>::compute_current_sync_period(sync_committee_update.attested_header.slot);

// initialize LatestFinalizedHeaderState with parent slot of finalized_header_update
LatestFinalizedHeader::<T>::set(FinalizedHeaderState {
beacon_block_root: Default::default(),
import_time: initial_sync_data.import_time,
beacon_slot: sync_committee_update.finalized_header.slot - 1,
});

}: sync_committee_period_update(RawOrigin::Signed(caller.clone()), sync_committee_update.clone())
verify {
EthereumBeaconClient::<T>::sync_committee_for_period(sync_committee_update.sync_committee_period+1).unwrap();
EthereumBeaconClient::<T>::sync_committee_for_period(period+1).unwrap();
}

import_finalized_header {
Expand All @@ -41,8 +51,8 @@ benchmarks! {

EthereumBeaconClient::<T>::store_sync_committee(current_period, &initial_sync_data.current_sync_committee)?;

//initialize LatestFinalizedHeaderState with parent slot of finalized_header_update
LatestFinalizedHeaderState::<T>::set(FinalizedHeaderState {
// initialize LatestFinalizedHeaderState with parent slot of finalized_header_update
LatestFinalizedHeader::<T>::set(FinalizedHeaderState {
beacon_block_root: Default::default(),
import_time: initial_sync_data.import_time + 51200,
beacon_slot: finalized_header_update.finalized_header.slot - 1,
Expand All @@ -65,7 +75,7 @@ benchmarks! {
let header_update = header_update();

let current_period = EthereumBeaconClient::<T>::compute_current_sync_period(
header_update.beacon_header.slot,
header_update.header.slot,
);

EthereumBeaconClient::<T>::store_sync_committee(current_period, &initial_sync_data.current_sync_committee)?;
Expand All @@ -76,7 +86,7 @@ benchmarks! {
let finalized_block_root = finalized_update.finalized_header.hash_tree_root()
.unwrap();

LatestFinalizedHeaderState::<T>::set(FinalizedHeaderState{
LatestFinalizedHeader::<T>::set(FinalizedHeaderState{
beacon_block_root: finalized_block_root,
beacon_slot: finalized_slot,
import_time: 0,
Expand Down Expand Up @@ -122,6 +132,14 @@ benchmarks! {
}:{
fast_aggregate_verify(&current_sync_committee.aggregate_pubkey, &absent_pubkeys, signing_root, &update.sync_aggregate.sync_committee_signature).unwrap();
}

merkle_branch_verify {
let update = initialize_sync_committee::<T>()?;
let block_root: H256 = update.finalized_header.hash_tree_root().unwrap();
}:{
verify_merkle_branch(block_root,&update.finality_branch,config::FINALIZED_ROOT_SUBTREE_INDEX,
config::FINALIZED_ROOT_DEPTH,update.attested_header.state_root);
}
}

impl_benchmark_test_suite!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub fn initialize_sync_committee<T: Config>() -> Result<SyncCommitteeUpdate, &'s

let sync_committee_update = sync_committee_update();

//initialize SyncCommittees with period in sync_committee_update
// initialize SyncCommittees with period in sync_committee_update
LatestSyncCommitteePeriod::<T>::set(EthereumBeaconClient::<T>::compute_current_sync_period(
sync_committee_update.attested_header.slot,
));
Expand Down Expand Up @@ -69,7 +69,7 @@ pub fn absent_pubkeys<T: Config>(
pub fn signing_root<T: Config>(update: &SyncCommitteeUpdate) -> Result<H256, &'static str> {
let validators_root = <ValidatorsRoot<T>>::get();
let signing_root = EthereumBeaconClient::<T>::signing_root(
update.attested_header,
&update.attested_header,
validators_root,
update.signature_slot,
)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ pub const SYNC_COMMITTEE_SIZE: usize = 512;
pub const SYNC_COMMITTEE_BITS_SIZE: usize = SYNC_COMMITTEE_SIZE / 8;
pub const SLOTS_PER_HISTORICAL_ROOT: usize = 8192;
pub const IS_MINIMAL: bool = false;
pub const BLOCK_ROOT_AT_INDEX_PROOF_DEPTH: usize = 13;
pub const BLOCK_ROOT_AT_INDEX_DEPTH: usize = 13;
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ pub const SYNC_COMMITTEE_SIZE: usize = 32;
pub const SYNC_COMMITTEE_BITS_SIZE: usize = SYNC_COMMITTEE_SIZE / 8;
pub const SLOTS_PER_HISTORICAL_ROOT: usize = 64;
pub const IS_MINIMAL: bool = true;
pub const BLOCK_ROOT_AT_INDEX_PROOF_DEPTH: usize = 6;
pub const BLOCK_ROOT_AT_INDEX_DEPTH: usize = 6;
40 changes: 28 additions & 12 deletions parachain/pallets/ethereum-beacon-client/src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use primitives::merkle_proof::{generalized_index_length, subtree_index};
use static_assertions::const_assert;

pub mod mainnet;
Expand All @@ -9,18 +10,33 @@ pub use minimal::*;
#[cfg(not(feature = "minimal"))]
pub use mainnet::*;

pub const CURRENT_SYNC_COMMITTEE_INDEX: usize = 22;
pub const NEXT_SYNC_COMMITTEE_INDEX: usize = 23;
pub const SYNC_COMMITTEE_DEPTH: usize = 5;

pub const FINALIZED_ROOT_DEPTH: usize = 6;
pub const FINALIZED_ROOT_INDEX: usize = 41;

pub const BLOCK_ROOTS_DEPTH: usize = 5;
pub const BLOCK_ROOTS_INDEX: usize = 5;

pub const EXECUTION_HEADER_DEPTH: usize = 4;
pub const EXECUTION_HEADER_INDEX: usize = 9;
// Generalized Indices

// get_generalized_index(BeaconState, 'block_roots')
pub const BLOCK_ROOTS_INDEX: usize = 37;
pub const BLOCK_ROOTS_SUBTREE_INDEX: usize = subtree_index(BLOCK_ROOTS_INDEX);
pub const BLOCK_ROOTS_DEPTH: usize = generalized_index_length(BLOCK_ROOTS_INDEX);

// get_generalized_index(BeaconState, 'finalized_checkpoint', 'root')
pub const FINALIZED_ROOT_INDEX: usize = 105;
pub const FINALIZED_ROOT_SUBTREE_INDEX: usize = subtree_index(FINALIZED_ROOT_INDEX);
pub const FINALIZED_ROOT_DEPTH: usize = generalized_index_length(FINALIZED_ROOT_INDEX);

// get_generalized_index(BeaconState, 'current_sync_committee')
pub const CURRENT_SYNC_COMMITTEE_INDEX: usize = 54;
pub const CURRENT_SYNC_COMMITTEE_SUBTREE_INDEX: usize = subtree_index(CURRENT_SYNC_COMMITTEE_INDEX);
pub const CURRENT_SYNC_COMMITTEE_DEPTH: usize =
generalized_index_length(CURRENT_SYNC_COMMITTEE_INDEX);

// get_generalized_index(BeaconState, 'next_sync_committee')
pub const NEXT_SYNC_COMMITTEE_INDEX: usize = 55;
pub const NEXT_SYNC_COMMITTEE_SUBTREE_INDEX: usize = subtree_index(NEXT_SYNC_COMMITTEE_INDEX);
pub const NEXT_SYNC_COMMITTEE_DEPTH: usize = generalized_index_length(NEXT_SYNC_COMMITTEE_INDEX);

// get_generalized_index(BeaconBlockBody, 'execution_payload')
pub const EXECUTION_HEADER_INDEX: usize = 25;
pub const EXECUTION_HEADER_SUBTREE_INDEX: usize = subtree_index(EXECUTION_HEADER_INDEX);
pub const EXECUTION_HEADER_DEPTH: usize = generalized_index_length(EXECUTION_HEADER_INDEX);

pub const MAX_EXTRA_DATA_BYTES: usize = 32;
pub const MAX_LOGS_BLOOM_SIZE: usize = 256;
Expand Down
Loading

0 comments on commit 54ab138

Please sign in to comment.