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

Add beefy light client tests and benchamarks #13

Open
wants to merge 29 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
b8c380e
add negative tests
N1ghtStorm Dec 22, 2022
a138afd
add signatures error tests
N1ghtStorm Jan 4, 2023
ae175eb
finish verify_validator_proof_lengths
N1ghtStorm Jan 4, 2023
ec3170f
add new tests
N1ghtStorm Jan 5, 2023
88fdd42
Merge branch 'develop' into add_beefy_tests
N1ghtStorm Mar 3, 2023
f54ef0e
add ValidatorNotOnceInbitfield error test
N1ghtStorm Mar 3, 2023
c792abd
add ValidatorSetIncorrectPosition error test
N1ghtStorm Mar 3, 2023
86367aa
change previous test
N1ghtStorm Mar 3, 2023
87d3b1e
add payload and signature tests
N1ghtStorm Mar 6, 2023
4500c73
refactor tests to helpers
N1ghtStorm Mar 6, 2023
0a98d6c
change validator proof location
N1ghtStorm Mar 6, 2023
76626cb
refactoring
N1ghtStorm Mar 6, 2023
58b61a7
add 200 val 5000 tree size temp benchmark
N1ghtStorm Mar 6, 2023
a7884d8
add weights
N1ghtStorm Mar 6, 2023
9841fb8
add benchmarks with powers of 2
N1ghtStorm Mar 7, 2023
c3be388
add benchmark asserts
N1ghtStorm Mar 7, 2023
9d511a6
add channel abi vec import
N1ghtStorm Mar 7, 2023
8199c9d
make weight module public
N1ghtStorm Mar 7, 2023
c033163
add impl benchmark test suite
N1ghtStorm Mar 8, 2023
9551865
remove benches
N1ghtStorm Mar 10, 2023
c3fe9f9
remove serde json
N1ghtStorm Mar 10, 2023
be37673
add serde json
N1ghtStorm Mar 10, 2023
88d2807
add slice for foxture
N1ghtStorm Mar 10, 2023
983bacb
add all benchmarks
N1ghtStorm Mar 10, 2023
d74b89c
add vec use
N1ghtStorm Mar 10, 2023
f865071
remove unnecessary fixtures
N1ghtStorm Mar 10, 2023
3c608c2
add headers
N1ghtStorm Mar 10, 2023
280b11b
remove unnecessary comments
N1ghtStorm Mar 10, 2023
a956ab9
fmt
N1ghtStorm Mar 10, 2023
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
12 changes: 8 additions & 4 deletions pallets/beefy-light-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ edition = "2021"
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
hex-literal = { version = "0.3.1", optional = true }
serde_json = { version = "1.0.73", default-features = false, features = ["alloc"], optional = true }
codec = { package = "parity-scale-codec", version = "3.0.0", features = [
"derive",
], default-features = false }
Expand All @@ -37,16 +39,18 @@ beefy-primitives = { git = "https://github.com/paritytech/substrate.git", branch
beefy-merkle-tree = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.31", default-features = false }

[dev-dependencies]
# Substrate

serde_json = "1.0.73"
sp-runtime = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.31", default-features = false }
hex-literal = "0.3.1"
serde_json = "1.0.73"
test-case = "2.2.2"

[features]
default = ["std"]
runtime-benchmarks = ["frame-benchmarking/runtime-benchmarks"]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"hex-literal",
"serde_json",
]
std = [
"codec/std",
"serde/std",
Expand Down
43 changes: 43 additions & 0 deletions pallets/beefy-light-client/src/benchmark_features.rs

Large diffs are not rendered by default.

233 changes: 229 additions & 4 deletions pallets/beefy-light-client/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,232 @@

use super::*;

// #[allow(unused)]
// use crate::Pallet as BeefyLightClient;
// use frame_benchmarking::{benchmarks, impl_benchmark_test_suite, whitelisted_caller};
// use frame_system::RawOrigin;
use crate::benchmark_features::*;
use crate::test_helpers::*;
use crate::Pallet as BeefyLightClient;
use frame_benchmarking::{benchmarks, impl_benchmark_test_suite};
use frame_system::RawOrigin;
use hex_literal::hex;

benchmarks! {
initialize {
let root = hex!("36ee7c9903f810b22f7e6fca82c1c0cd6a151eca01f087683d92333094d94dc1");
let curr_val_set = ValidatorSet {
id: 0,
len: 3,
root: root.into(),
};
let next_val_set = ValidatorSet {
id: 1,
len: 3,
root: root.into(),
};
}: _(RawOrigin::Root, SubNetworkId::Mainnet, 1, curr_val_set, next_val_set)
verify {
assert!(BeefyLightClient::<T>::current_validator_set(SubNetworkId::Mainnet).is_some());
assert!(BeefyLightClient::<T>::next_validator_set(SubNetworkId::Mainnet).is_some());
}


submit_signature_commitment_10_128 {
Copy link
Contributor

Choose a reason for hiding this comment

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

What is output of your benchmarks?
As I see the result is not mapped to WeightInfo trait.
Also how did you get submit_signature_commitment() weight? Because you will get of methods submit_signature_commitment_x_y() with different weights?

let validators = 10;
let tree_size = 128;

let fixture = load_slice_fixture(FIXTURE_10_128);
let validator_set = fixture.validator_set.clone().into();
let next_validator_set = fixture.next_validator_set.clone().into();

BeefyLightClient::<T>::initialize(
RawOrigin::Root.into(),
SubNetworkId::Mainnet,
0,
validator_set,
next_validator_set
).expect("Error while initializing pallet");

let signed_commitment: beefy_primitives::SignedCommitment<
u32,
beefy_primitives::crypto::Signature,
> = Decode::decode(&mut &fixture.commitment[..]).unwrap();
let commitment = signed_commitment.commitment.clone();
let validator_proof = validator_proof::<T>(&fixture, signed_commitment.signatures, validators);
let leaf: BeefyMMRLeaf = Decode::decode(&mut &fixture.leaf[..]).unwrap();
}: submit_signature_commitment(RawOrigin::Signed(alice::<T>()), SubNetworkId::Mainnet, commitment, validator_proof, leaf, fixture.leaf_proof.into())
verify {
assert!(BeefyLightClient::<T>::latest_mmr_roots(SubNetworkId::Mainnet).len() > 0);
}

submit_signature_commitment_20_256 {
let validators = 20;
let tree_size = 256;
Copy link
Contributor

Choose a reason for hiding this comment

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

Never used


let fixture = load_slice_fixture(FIXTURE_20_256);
let validator_set = fixture.validator_set.clone().into();
let next_validator_set = fixture.next_validator_set.clone().into();

BeefyLightClient::<T>::initialize(
RawOrigin::Root.into(),
SubNetworkId::Mainnet,
0,
validator_set,
next_validator_set
).expect("Error while initializing pallet");

let signed_commitment: beefy_primitives::SignedCommitment<
u32,
beefy_primitives::crypto::Signature,
> = Decode::decode(&mut &fixture.commitment[..]).unwrap();
let commitment = signed_commitment.commitment.clone();
let validator_proof = validator_proof::<T>(&fixture, signed_commitment.signatures, validators);
let leaf: BeefyMMRLeaf = Decode::decode(&mut &fixture.leaf[..]).unwrap();
}: submit_signature_commitment(RawOrigin::Signed(alice::<T>()), SubNetworkId::Mainnet, commitment, validator_proof, leaf, fixture.leaf_proof.into())
verify {
assert!(BeefyLightClient::<T>::latest_mmr_roots(SubNetworkId::Mainnet).len() > 0);
}

submit_signature_commitment_40_512 {
let validators = 40;
let tree_size = 512;

let fixture = load_slice_fixture(FIXTURE_40_512);
let validator_set = fixture.validator_set.clone().into();
let next_validator_set = fixture.next_validator_set.clone().into();

BeefyLightClient::<T>::initialize(
RawOrigin::Root.into(),
SubNetworkId::Mainnet,
0,
validator_set,
next_validator_set
).expect("Error while initializing pallet");

let signed_commitment: beefy_primitives::SignedCommitment<
u32,
beefy_primitives::crypto::Signature,
> = Decode::decode(&mut &fixture.commitment[..]).unwrap();
let commitment = signed_commitment.commitment.clone();
let validator_proof = validator_proof::<T>(&fixture, signed_commitment.signatures, validators);
let leaf: BeefyMMRLeaf = Decode::decode(&mut &fixture.leaf[..]).unwrap();
}: submit_signature_commitment(RawOrigin::Signed(alice::<T>()), SubNetworkId::Mainnet, commitment, validator_proof, leaf, fixture.leaf_proof.into())
verify {
assert!(BeefyLightClient::<T>::latest_mmr_roots(SubNetworkId::Mainnet).len() > 0);
}

submit_signature_commitment_80_1024 {
let validators = 80;
let tree_size = 1024;

let fixture = load_slice_fixture(FIXTURE_80_1024);
let validator_set = fixture.validator_set.clone().into();
let next_validator_set = fixture.next_validator_set.clone().into();

BeefyLightClient::<T>::initialize(
RawOrigin::Root.into(),
SubNetworkId::Mainnet,
0,
validator_set,
next_validator_set
).expect("Error while initializing pallet");

let signed_commitment: beefy_primitives::SignedCommitment<
u32,
beefy_primitives::crypto::Signature,
> = Decode::decode(&mut &fixture.commitment[..]).unwrap();
let commitment = signed_commitment.commitment.clone();
let validator_proof = validator_proof::<T>(&fixture, signed_commitment.signatures, validators);
let leaf: BeefyMMRLeaf = Decode::decode(&mut &fixture.leaf[..]).unwrap();
}: submit_signature_commitment(RawOrigin::Signed(alice::<T>()), SubNetworkId::Mainnet, commitment, validator_proof, leaf, fixture.leaf_proof.into())
verify {
assert!(BeefyLightClient::<T>::latest_mmr_roots(SubNetworkId::Mainnet).len() > 0);
}

submit_signature_commitment_160_2048 {
let validators = 160;
let tree_size = 2048;

let fixture = load_slice_fixture(FIXTURE_160_2048);
let validator_set = fixture.validator_set.clone().into();
let next_validator_set = fixture.next_validator_set.clone().into();

BeefyLightClient::<T>::initialize(
RawOrigin::Root.into(),
SubNetworkId::Mainnet,
0,
validator_set,
next_validator_set
).expect("Error while initializing pallet");

let signed_commitment: beefy_primitives::SignedCommitment<
u32,
beefy_primitives::crypto::Signature,
> = Decode::decode(&mut &fixture.commitment[..]).unwrap();
let commitment = signed_commitment.commitment.clone();
let validator_proof = validator_proof::<T>(&fixture, signed_commitment.signatures, validators);
let leaf: BeefyMMRLeaf = Decode::decode(&mut &fixture.leaf[..]).unwrap();
}: submit_signature_commitment(RawOrigin::Signed(alice::<T>()), SubNetworkId::Mainnet, commitment, validator_proof, leaf, fixture.leaf_proof.into())
verify {
assert!(BeefyLightClient::<T>::latest_mmr_roots(SubNetworkId::Mainnet).len() > 0);
}

submit_signature_commitment_200_4096 {
let validators = 200;
let tree_size = 4096;

let fixture = load_slice_fixture(FIXTURE_200_4096);
let validator_set = fixture.validator_set.clone().into();
let next_validator_set = fixture.next_validator_set.clone().into();

BeefyLightClient::<T>::initialize(
RawOrigin::Root.into(),
SubNetworkId::Mainnet,
0,
validator_set,
next_validator_set
).expect("Error while initializing pallet");

let signed_commitment: beefy_primitives::SignedCommitment<
u32,
beefy_primitives::crypto::Signature,
> = Decode::decode(&mut &fixture.commitment[..]).unwrap();
let commitment = signed_commitment.commitment.clone();
let validator_proof = validator_proof::<T>(&fixture, signed_commitment.signatures, validators);
let leaf: BeefyMMRLeaf = Decode::decode(&mut &fixture.leaf[..]).unwrap();
}: submit_signature_commitment(RawOrigin::Signed(alice::<T>()), SubNetworkId::Mainnet, commitment, validator_proof, leaf, fixture.leaf_proof.into())
verify {
assert!(BeefyLightClient::<T>::latest_mmr_roots(SubNetworkId::Mainnet).len() > 0);
}

submit_signature_commitment_300_8192 {
let validators = 300;
let tree_size = 8192;

let fixture = load_slice_fixture(FIXTURE_300_8192);
let validator_set = fixture.validator_set.clone().into();
let next_validator_set = fixture.next_validator_set.clone().into();

BeefyLightClient::<T>::initialize(
RawOrigin::Root.into(),
SubNetworkId::Mainnet,
0,
validator_set,
next_validator_set
).expect("Error while initializing pallet");

let signed_commitment: beefy_primitives::SignedCommitment<
u32,
beefy_primitives::crypto::Signature,
> = Decode::decode(&mut &fixture.commitment[..]).unwrap();
let commitment = signed_commitment.commitment.clone();
let validator_proof = validator_proof::<T>(&fixture, signed_commitment.signatures, validators);
let leaf: BeefyMMRLeaf = Decode::decode(&mut &fixture.leaf[..]).unwrap();
}: submit_signature_commitment(RawOrigin::Signed(alice::<T>()), SubNetworkId::Mainnet, commitment, validator_proof, leaf, fixture.leaf_proof.into())
verify {
assert!(BeefyLightClient::<T>::latest_mmr_roots(SubNetworkId::Mainnet).len() > 0);
}
}

impl_benchmark_test_suite!(
BeefyLightClient,
crate::mock::new_test_ext(),
crate::mock::Test,
);

Large diffs are not rendered by default.

20 changes: 18 additions & 2 deletions pallets/beefy-light-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,21 @@ mod tests;
#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;

#[cfg(feature = "runtime-benchmarks")]
mod benchmark_features;

#[cfg(any(test, feature = "runtime-benchmarks"))]
// #[cfg(test)]
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you need this commented code?

mod test_helpers;

pub mod weights;

pub trait WeightInfo {
fn initialize() -> Weight;

fn submit_signature_commitment() -> Weight;
Comment on lines +82 to +84
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
fn initialize() -> Weight;
fn submit_signature_commitment() -> Weight;
fn initialize() -> Weight;
fn submit_signature_commitment() -> Weight;

}

#[derive(Clone, RuntimeDebug, Encode, Decode, PartialEq, Eq, scale_info::TypeInfo)]
pub struct ProvedSubstrateBridgeMessage<Message> {
pub message: Message,
Expand Down Expand Up @@ -115,6 +130,7 @@ pub mod pallet {
#[pallet::config]
pub trait Config: frame_system::Config {
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
type WeightInfo: WeightInfo;
type Randomness: frame_support::traits::Randomness<Self::Hash, Self::BlockNumber>;
type Message: Parameter;
}
Expand Down Expand Up @@ -202,7 +218,7 @@ pub mod pallet {
// Dispatchable functions must be annotated with a weight and must return a DispatchResult.
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::weight(0)]
#[pallet::weight(<T as Config>::WeightInfo::initialize())]
pub fn initialize(
origin: OriginFor<T>,
network_id: SubNetworkId,
Expand All @@ -217,7 +233,7 @@ pub mod pallet {
Ok(().into())
}

#[pallet::weight(0)]
#[pallet::weight(<T as Config>::WeightInfo::submit_signature_commitment())]
Copy link
Contributor

Choose a reason for hiding this comment

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

Looking at your benchmarks I think you need to rework the weight of submit_signature_commitment()

#[frame_support::transactional]
pub fn submit_signature_commitment(
origin: OriginFor<T>,
Expand Down
1 change: 1 addition & 0 deletions pallets/beefy-light-client/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ impl beefy_light_client::Config for Test {
type RuntimeEvent = RuntimeEvent;
type Randomness = beefy_light_client::SidechainRandomness<Test, SidechainRandomnessNetwork>;
type Message = ();
type WeightInfo = ();
}

// Build genesis storage according to the mock runtime.
Expand Down
Loading