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

Commit

Permalink
Refactor beacon client (paritytech#816)
Browse files Browse the repository at this point in the history
* Refactor types

* more refactoring

* refactor

* refactor

* refactor

* refactor

* refactor

* update cumulus

* Update benchmarks

* Update template for generating benchmarking data

* Fix bug in tests to prevent stack overflow

* Fix for benchmark & More refactoring (paritytech#817)

* Fix for benchmark & More refactoring

* More refactoring

* Add const_assert back

---------

Co-authored-by: Ron <yrong1997@gmail.com>
  • Loading branch information
vgeddes and yrong authored May 1, 2023
1 parent 4f3b591 commit cabadce
Show file tree
Hide file tree
Showing 32 changed files with 3,314 additions and 3,476 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/parachain.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,6 @@ jobs:
args: >-
--manifest-path parachain/Cargo.toml
--verbose --workspace
--exclude snowbridge
--exclude snowbridge-runtime
--exclude snowblink-runtime
--exclude snowbase-runtime
--features runtime-benchmarks
--avoid-cfg-tarpaulin
--coveralls ${{ secrets.COVERALLS_REPO_TOKEN }}
Expand Down
2 changes: 1 addition & 1 deletion cumulus
6 changes: 6 additions & 0 deletions parachain/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions parachain/pallets/ethereum-beacon-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,16 @@ frame-support = { git = "/~https://github.com/paritytech/substrate.git", branch =
frame-system = { git = "/~https://github.com/paritytech/substrate.git", branch = "master", default-features = false }
sp-core = { git = "/~https://github.com/paritytech/substrate.git", branch = "master", default-features = false }
sp-std = { git = "/~https://github.com/paritytech/substrate.git", branch = "master", default-features = false }
sp-io = { git = "/~https://github.com/paritytech/substrate.git", branch = "master", default-features = false }
sp-runtime = { git = "/~https://github.com/paritytech/substrate.git", branch = "master", default-features = false }

snowbridge-core = { path = "../../primitives/core", default-features = false }
snowbridge-ethereum = { path = "../../primitives/ethereum", default-features = false }
snowbridge-beacon-primitives = { path = "../../primitives/beacon", default-features = false }
primitives = { package = "snowbridge-beacon-primitives", path = "../../primitives/beacon", default-features = false }
static_assertions = { version = "1.1.0" }

[dev-dependencies]
sp-keyring = { git = "/~https://github.com/paritytech/substrate.git", branch = "master" }
sp-io = { git = "/~https://github.com/paritytech/substrate.git", branch = "master" }
snowbridge-testutils = { path = "../../primitives/testutils" }
serde_json = "1.0.96"
hex-literal = { version = "0.4.1" }
Expand All @@ -50,12 +51,11 @@ std = [
"frame-system/std",
'frame-benchmarking/std',
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
"sp-std/std",
"snowbridge-core/std",
"snowbridge-ethereum/std",
"snowbridge-beacon-primitives/std",
"primitives/std",
"milagro_bls/std",
"ssz-rs/std",
"byte-slice-cast/std",
Expand Down
2,131 changes: 1,053 additions & 1,078 deletions parachain/pallets/ethereum-beacon-client/src/benchmarking/data_mainnet.rs

Large diffs are not rendered by default.

Large diffs are not rendered by default.

19 changes: 7 additions & 12 deletions parachain/pallets/ethereum-beacon-client/src/benchmarking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ benchmarks! {

}: sync_committee_period_update(RawOrigin::Signed(caller.clone()), sync_committee_update.clone())
verify {
assert!(<SyncCommittees<T>>::get(sync_committee_update.sync_committee_period+1).pubkeys.len() > 0);
assert!(<SyncCommittees<T>>::get(sync_committee_update.sync_committee_period+1).unwrap().pubkeys.len() > 0);
}

import_finalized_header {
Expand Down Expand Up @@ -46,9 +46,7 @@ benchmarks! {

}: _(RawOrigin::Signed(caller.clone()), finalized_header_update.clone())
verify {
let header_hash_bytes = merkleization::hash_tree_root_beacon_header(finalized_header_update.finalized_header).unwrap();

let header_hash: H256 = header_hash_bytes.into();
let header_hash: H256 = finalized_header_update.finalized_header.hash_tree_root().unwrap();

<FinalizedBeaconHeaders<T>>::get(header_hash).unwrap();
}
Expand All @@ -66,13 +64,11 @@ benchmarks! {
header_update.beacon_header.slot,
), initial_sync_data.current_sync_committee);

let finalized_update: FinalizedHeaderUpdate<T::MaxSignatureSize, T::MaxProofBranchSize, T::MaxSyncCommitteeSize> = finalized_header_update();
let finalized_update: FinalizedHeaderUpdate<> = finalized_header_update();

let finalized_slot = finalized_update.finalized_header.slot;
let finalized_block_root: H256 =
merkleization::hash_tree_root_beacon_header(finalized_update.finalized_header)
.unwrap()
.into();
let finalized_block_root = finalized_update.finalized_header.hash_tree_root()
.unwrap();

LatestFinalizedHeaderState::<T>::set(FinalizedHeaderState{
beacon_block_root: finalized_block_root,
Expand All @@ -85,8 +81,7 @@ benchmarks! {
);
}: _(RawOrigin::Signed(caller.clone()), header_update.clone())
verify {
let header: ExecutionHeader = header_update.execution_header.try_into().unwrap();
<ExecutionHeaders<T>>::get(header.block_hash).unwrap();
assert!(<ExecutionHeaders<T>>::contains_key(header_update.execution_header.block_hash))
}

unblock_bridge {
Expand All @@ -100,7 +95,7 @@ benchmarks! {
let participant_pubkeys = get_participant_pubkeys::<T>(&update)?;
let signing_root = get_signing_message::<T>(&update)?;
}:{
EthereumBeaconClient::<T>::bls_fast_aggregate_verify(participant_pubkeys,signing_root,update.sync_aggregate.sync_committee_signature)?;
EthereumBeaconClient::<T>::bls_fast_aggregate_verify(&participant_pubkeys,signing_root,&update.sync_aggregate.sync_committee_signature)?;
}

bls_aggregate_pubkey {
Expand Down
36 changes: 20 additions & 16 deletions parachain/pallets/ethereum-beacon-client/src/benchmarking/util.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
use super::*;
use crate::Pallet as EthereumBeaconClient;
use milagro_bls::{AggregatePublicKey, AggregateSignature, Signature};
use crate::{
Config, LatestSyncCommitteePeriod, Pallet as EthereumBeaconClient, PublicKey, Signature,
SyncCommitteeUpdate, SyncCommittees, ValidatorsRoot, Vec,
};
use milagro_bls::{AggregatePublicKey, AggregateSignature, Signature as MilagroSignature};
use sp_core::H256;

pub fn initialize_sync_committee<T: Config>() -> Result<SyncCommitteePeriodUpdateOf<T>, &'static str>
{
use super::{initial_sync, sync_committee_update};
use crate::{decompress_sync_committee_bits, Error};

pub fn initialize_sync_committee<T: Config>() -> Result<SyncCommitteeUpdate, &'static str> {
let initial_sync_data = initial_sync();

EthereumBeaconClient::<T>::initial_sync(initial_sync_data.clone())?;

let sync_committee_update: SyncCommitteePeriodUpdateOf<T> = sync_committee_update();
let sync_committee_update = sync_committee_update();

//initialize SyncCommittees with period in sync_committee_update
LatestSyncCommitteePeriod::<T>::set(EthereumBeaconClient::<T>::compute_current_sync_period(
Expand All @@ -24,14 +29,14 @@ pub fn initialize_sync_committee<T: Config>() -> Result<SyncCommitteePeriodUpdat
}

pub fn get_participant_pubkeys<T: Config>(
update: &SyncCommitteePeriodUpdateOf<T>,
update: &SyncCommitteeUpdate,
) -> Result<Vec<PublicKey>, &'static str> {
let sync_committee_bits =
get_sync_committee_bits(update.sync_aggregate.sync_committee_bits.clone()).unwrap();
decompress_sync_committee_bits(update.sync_aggregate.sync_committee_bits.clone());
let current_period =
EthereumBeaconClient::<T>::compute_current_sync_period(update.attested_header.slot);
let current_sync_committee =
EthereumBeaconClient::<T>::get_sync_committee_for_period(current_period)?;
SyncCommittees::<T>::get(current_period).ok_or("no sync committee")?;
let sync_committee_pubkeys = current_sync_committee.pubkeys;
let mut participant_pubkeys: Vec<PublicKey> = Vec::new();
for (bit, pubkey) in sync_committee_bits.iter().zip(sync_committee_pubkeys.iter()) {
Expand All @@ -43,17 +48,15 @@ pub fn get_participant_pubkeys<T: Config>(
Ok(participant_pubkeys)
}

pub fn get_signing_message<T: Config>(
update: &SyncCommitteePeriodUpdateOf<T>,
) -> Result<Root, &'static str> {
pub fn get_signing_message<T: Config>(update: &SyncCommitteeUpdate) -> Result<H256, &'static str> {
let validators_root = <ValidatorsRoot<T>>::get();
let fork_version = EthereumBeaconClient::<T>::compute_fork_version(
EthereumBeaconClient::<T>::compute_epoch_at_slot(
update.signature_slot,
config::SLOTS_PER_EPOCH,
crate::config::SLOTS_PER_EPOCH,
),
);
let domain_type = config::DOMAIN_SYNC_COMMITTEE.to_vec();
let domain_type = crate::config::DOMAIN_SYNC_COMMITTEE.to_vec();
let domain =
EthereumBeaconClient::<T>::compute_domain(domain_type, fork_version, validators_root)?;
let signing_root =
Expand All @@ -62,9 +65,10 @@ pub fn get_signing_message<T: Config>(
}

pub fn get_aggregate_signature<T: Config>(
signature: BoundedVec<u8, T::MaxSignatureSize>,
signature: Signature,
) -> Result<AggregateSignature, Error<T>> {
let sig = Signature::from_bytes(&signature[..]).map_err(|_| Error::<T>::InvalidSignature)?;
let sig =
MilagroSignature::from_bytes(&signature.0[..]).map_err(|_| Error::<T>::InvalidSignature)?;
let agg_sig = AggregateSignature::from_signature(&sig);
Ok(agg_sig)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub const SLOTS_PER_EPOCH: u64 = 32;
pub const SECONDS_PER_SLOT: u64 = 12;
pub const EPOCHS_PER_SYNC_COMMITTEE_PERIOD: u64 = 256;
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: u64 = 13;
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub const SLOTS_PER_EPOCH: u64 = 8;
pub const SECONDS_PER_SLOT: u64 = 6;
pub const EPOCHS_PER_SYNC_COMMITTEE_PERIOD: u64 = 8;
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: u64 = 6;
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#[cfg(feature = "minimal")]
mod minimal;
use static_assertions::const_assert;

#[cfg(not(feature = "minimal"))]
mod mainnet;
pub mod mainnet;
pub mod minimal;

#[cfg(feature = "minimal")]
pub use minimal::*;
Expand All @@ -11,10 +10,8 @@ pub use minimal::*;
pub use mainnet::*;

pub const CURRENT_SYNC_COMMITTEE_INDEX: u64 = 22;
pub const CURRENT_SYNC_COMMITTEE_DEPTH: u64 = 5;

pub const NEXT_SYNC_COMMITTEE_DEPTH: u64 = 5;
pub const NEXT_SYNC_COMMITTEE_INDEX: u64 = 23;
pub const SYNC_COMMITTEE_DEPTH: u64 = 5;

pub const FINALIZED_ROOT_DEPTH: u64 = 6;
pub const FINALIZED_ROOT_INDEX: u64 = 41;
Expand All @@ -29,9 +26,14 @@ pub const MAX_EXTRA_DATA_BYTES: usize = 32;
pub const MAX_LOGS_BLOOM_SIZE: usize = 256;
pub const MAX_FEE_RECIPIENT_SIZE: usize = 20;

pub const MAX_FINALIZED_HEADER_SLOT_ARRAY: u32 = 1000;
pub const MAX_BRANCH_PROOF_SIZE: u32 = 20;

/// DomainType('0x07000000')
/// /~https://github.com/ethereum/consensus-specs/blob/dev/specs/altair/beacon-chain.md#domain-types
pub const DOMAIN_SYNC_COMMITTEE: [u8; 4] = [7, 0, 0, 0];

pub const PUBKEY_SIZE: usize = 48;
pub const SIGNATURE_SIZE: usize = 96;

const_assert!(SYNC_COMMITTEE_BITS_SIZE == SYNC_COMMITTEE_SIZE / 8);
Loading

0 comments on commit cabadce

Please sign in to comment.