This repository has been archived by the owner on Oct 22, 2024. It is now read-only.
forked from paritytech/polkadot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Benchmark with main spec (paritytech#802)
* Benchmark with main spec * Update benchmark.md
- Loading branch information
Showing
13 changed files
with
246 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule cumulus
updated
4 files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# Motivation | ||
Demonstrate that [FastAggregateVerify](https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-bls-signature-04#section-3.3.4) is the most expensive call in ethereum beacon light client, though in [#13031](https://github.com/paritytech/substrate/pull/13031) Parity team has wrapped some low level host functions for `bls-12381` but adding a high level host function specific for it is super helpful. | ||
|
||
# Benchmark | ||
We add several benchmarks [here](https://github.com/Snowfork/snowbridge/blob/8891ca3cdcf2e04d8118c206588c956541ae4710/parachain/pallets/ethereum-beacon-client/src/benchmarking/mod.rs#L98-L124) as following to demonstrate [bls_fast_aggregate_verify](https://github.com/Snowfork/snowbridge/blob/8891ca3cdcf2e04d8118c206588c956541ae4710/parachain/pallets/ethereum-beacon-client/src/lib.rs#L764) is the main bottleneck. Test data [here](https://github.com/Snowfork/snowbridge/blob/8891ca3cdcf2e04d8118c206588c956541ae4710/parachain/pallets/ethereum-beacon-client/src/benchmarking/data_mainnet.rs#L553-L1120) is real from goerli network which contains 512 public keys from sync committee. | ||
|
||
|
||
## sync_committee_period_update | ||
Base line benchmark for extrinsic [sync_committee_period_update](https://github.com/Snowfork/snowbridge/blob/8891ca3cdcf2e04d8118c206588c956541ae4710/parachain/pallets/ethereum-beacon-client/src/lib.rs#L233) | ||
|
||
|
||
## bls_fast_aggregate_verify | ||
Subfunction of extrinsic `sync_committee_period_update` which does what [FastAggregateVerify](https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-bls-signature-04#section-3.3.4) requires. | ||
|
||
## bls_aggregate_pubkey | ||
Subfunction of `bls_fast_aggregate_verify` which decompress and instantiate G1 pubkeys only. | ||
|
||
## bls_verify_message | ||
Subfunction of `bls_fast_aggregate_verify` which verify the prepared signature only. | ||
|
||
|
||
# Result | ||
|
||
## hardware spec | ||
Run benchmark in a EC2 instance | ||
``` | ||
cargo run --release --bin polkadot-parachain --features runtime-benchmarks -- benchmark machine --base-path /mnt/scratch/benchmark | ||
+----------+----------------+-------------+-------------+-------------------+ | ||
| Category | Function | Score | Minimum | Result | | ||
+===========================================================================+ | ||
| CPU | BLAKE2-256 | 1.08 GiBs | 1.00 GiBs | ✅ Pass (107.5 %) | | ||
|----------+----------------+-------------+-------------+-------------------| | ||
| CPU | SR25519-Verify | 568.87 KiBs | 666.00 KiBs | ❌ Fail ( 85.4 %) | | ||
|----------+----------------+-------------+-------------+-------------------| | ||
| Memory | Copy | 13.67 GiBs | 14.32 GiBs | ✅ Pass ( 95.4 %) | | ||
|----------+----------------+-------------+-------------+-------------------| | ||
| Disk | Seq Write | 334.35 MiBs | 450.00 MiBs | ❌ Fail ( 74.3 %) | | ||
|----------+----------------+-------------+-------------+-------------------| | ||
| Disk | Rnd Write | 143.59 MiBs | 200.00 MiBs | ❌ Fail ( 71.8 %) | | ||
+----------+----------------+-------------+-------------+-------------------+ | ||
``` | ||
|
||
## benchmark | ||
|
||
``` | ||
cargo run --release --bin polkadot-parachain \ | ||
--features runtime-benchmarks \ | ||
-- \ | ||
benchmark pallet \ | ||
--base-path /mnt/scratch/benchmark \ | ||
--chain=bridge-hub-rococo-dev \ | ||
--pallet=snowbridge_ethereum_beacon_client \ | ||
--extrinsic="*" \ | ||
--execution=wasm --wasm-execution=compiled \ | ||
--steps 50 --repeat 20 \ | ||
--output ./parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/weights/snowbridge_ethereum_beacon_client.rs | ||
``` | ||
|
||
### [Weights](https://github.com/Snowfork/cumulus/blob/ron/benchmark-beacon-bridge/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/weights/snowbridge_ethereum_beacon_client.rs) | ||
|
||
|extrinsic | minimum execution time benchmarked(us) | | ||
| --------------------------------------- |----------------------------------------| | ||
|sync_committee_period_update | 123_126 | | ||
|bls_fast_aggregate_verify| 121_083 | | ||
|bls_aggregate_pubkey | 90_306 | | ||
|bls_verify_message | 28_000 | | ||
|
||
- [bls_fast_aggregate_verify](#bls_fast_aggregate_verify) consumes 98% execution time of [sync_committee_period_update](#sync_committee_period_update) | ||
|
||
- [bls_aggregate_pubkey](#bls_aggregate_pubkey) consumes 75% execution time of [bls_fast_aggregate_verify](#bls_fast_aggregate_verify) | ||
|
||
- [bls_verify_message](#bls_verify_message) consumes 23% execution time of [bls_fast_aggregate_verify](#bls_fast_aggregate_verify) | ||
|
||
# Conclusion | ||
|
||
A high level host function specific for [bls_fast_aggregate_verify](https://github.com/Snowfork/snowbridge/blob/8891ca3cdcf2e04d8118c206588c956541ae4710/parachain/pallets/ethereum-beacon-client/src/lib.rs#L764) is super helpful. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
parachain/pallets/ethereum-beacon-client/src/benchmarking/util.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
use super::*; | ||
use crate::Pallet as EthereumBeaconClient; | ||
use milagro_bls::{AggregatePublicKey, AggregateSignature, Signature}; | ||
|
||
pub fn initialize_sync_committee<T: Config>() -> Result<SyncCommitteePeriodUpdateOf<T>, &'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(); | ||
|
||
//initialize SyncCommittees with period in sync_committee_update | ||
LatestSyncCommitteePeriod::<T>::set(EthereumBeaconClient::<T>::compute_current_sync_period( | ||
sync_committee_update.attested_header.slot, | ||
)); | ||
SyncCommittees::<T>::insert( | ||
EthereumBeaconClient::<T>::compute_current_sync_period( | ||
sync_committee_update.attested_header.slot, | ||
), | ||
initial_sync_data.current_sync_committee, | ||
); | ||
Ok(sync_committee_update) | ||
} | ||
|
||
pub fn get_participant_pubkeys<T: Config>( | ||
update: &SyncCommitteePeriodUpdateOf<T>, | ||
) -> Result<Vec<PublicKey>, &'static str> { | ||
let sync_committee_bits = | ||
get_sync_committee_bits(update.sync_aggregate.sync_committee_bits.clone()).unwrap(); | ||
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)?; | ||
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()) { | ||
if *bit == 1 as u8 { | ||
let pubk = pubkey.clone(); | ||
participant_pubkeys.push(pubk); | ||
} | ||
} | ||
Ok(participant_pubkeys) | ||
} | ||
|
||
pub fn get_signing_message<T: Config>( | ||
update: &SyncCommitteePeriodUpdateOf<T>, | ||
) -> Result<Root, &'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, | ||
), | ||
); | ||
let domain_type = config::DOMAIN_SYNC_COMMITTEE.to_vec(); | ||
let domain = | ||
EthereumBeaconClient::<T>::compute_domain(domain_type, fork_version, validators_root)?; | ||
let signing_root = | ||
EthereumBeaconClient::<T>::compute_signing_root(update.attested_header.clone(), domain)?; | ||
Ok(signing_root) | ||
} | ||
|
||
pub fn get_aggregate_signature<T: Config>( | ||
signature: BoundedVec<u8, T::MaxSignatureSize>, | ||
) -> Result<AggregateSignature, Error<T>> { | ||
let sig = Signature::from_bytes(&signature[..]).map_err(|_| Error::<T>::InvalidSignature)?; | ||
let agg_sig = AggregateSignature::from_signature(&sig); | ||
Ok(agg_sig) | ||
} | ||
|
||
pub fn get_aggregate_pubkey<T: Config>( | ||
pubkeys: Vec<PublicKey>, | ||
) -> Result<AggregatePublicKey, Error<T>> { | ||
let milagro_public_keys = pubkeys | ||
.iter() | ||
.map(|bytes| milagro_bls::PublicKey::from_bytes_unchecked(&bytes.0)) | ||
.collect::<Result<Vec<milagro_bls::PublicKey>, _>>() | ||
.map_err(|_| Error::<T>::InvalidSignaturePoint)?; | ||
let agg_pub_key = AggregatePublicKey::into_aggregate(&milagro_public_keys) | ||
.map_err(|_| Error::<T>::InvalidAggregatePublicKeys)?; | ||
Ok(agg_pub_key) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.