Skip to content

Commit a3f1a56

Browse files
authored
Merge pull request #3546 from autonomys/fp-validation-benchmark
Add fraud proof validation benchmark and weights
2 parents aa8e0df + 33cc825 commit a3f1a56

File tree

12 files changed

+1209
-9
lines changed

12 files changed

+1209
-9
lines changed

Cargo.lock

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/pallet-domains/src/benchmarking.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,60 @@ mod benchmarks {
10361036
}
10371037
}
10381038

1039+
#[benchmark]
1040+
fn fraud_proof_pre_check() {
1041+
let domain_id = register_domain::<T>();
1042+
let (_, operator_id) =
1043+
register_helper_operator::<T>(domain_id, T::MinNominatorStake::get());
1044+
1045+
let mut target_receipt_hash = None;
1046+
let mut receipt =
1047+
BlockTree::<T>::get::<_, DomainBlockNumberFor<T>>(domain_id, Zero::zero())
1048+
.and_then(BlockTreeNodes::<T>::get)
1049+
.expect("genesis receipt must exist")
1050+
.execution_receipt;
1051+
for i in 1u32..=4u32 {
1052+
let consensus_block_number = i.into();
1053+
let domain_block_number = i.into();
1054+
1055+
// Run to `block_number`
1056+
run_to_block::<T>(
1057+
consensus_block_number,
1058+
frame_system::Pallet::<T>::block_hash(consensus_block_number - One::one()),
1059+
);
1060+
1061+
// Submit a bundle with the receipt of the last block
1062+
let bundle = dummy_opaque_bundle(domain_id, operator_id, receipt);
1063+
assert_ok!(Domains::<T>::submit_bundle(
1064+
DomainOrigin::ValidatedUnsigned.into(),
1065+
bundle
1066+
));
1067+
1068+
// Create ER for the above bundle
1069+
let head_receipt_number = HeadReceiptNumber::<T>::get(domain_id);
1070+
let parent_domain_block_receipt = BlockTree::<T>::get(domain_id, head_receipt_number)
1071+
.expect("parent receipt must exist");
1072+
receipt = ExecutionReceipt::dummy::<DomainHashingFor<T>>(
1073+
consensus_block_number,
1074+
frame_system::Pallet::<T>::block_hash(consensus_block_number),
1075+
domain_block_number,
1076+
parent_domain_block_receipt,
1077+
);
1078+
if i == 3 {
1079+
target_receipt_hash.replace(receipt.hash::<DomainHashingFor<T>>());
1080+
}
1081+
}
1082+
assert_eq!(Domains::<T>::head_receipt_number(domain_id), 3u32.into());
1083+
1084+
// Construct fraud proof that target the ER at block #3
1085+
let fraud_proof = FraudProof::dummy_fraud_proof(domain_id, target_receipt_hash.unwrap());
1086+
1087+
#[block]
1088+
{
1089+
assert_ok!(Domains::<T>::validate_fraud_proof(&fraud_proof));
1090+
}
1091+
}
1092+
10391093
fn register_runtime<T: Config>() -> RuntimeId {
10401094
let genesis_storage = include_bytes!("../res/evm-domain-genesis-storage").to_vec();
10411095
let runtime_id = NextRuntimeId::<T>::get();

crates/pallet-domains/src/extensions.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use frame_support::weights::Weight;
88
use frame_system::pallet_prelude::RuntimeCallFor;
99
use parity_scale_codec::{Decode, Encode};
1010
use scale_info::prelude::fmt;
11+
use sp_domains_fraud_proof::weights::fraud_proof_verification_weights;
1112
use sp_domains_fraud_proof::InvalidTransactionCode;
1213
use sp_runtime::impl_tx_ext_default;
1314
use sp_runtime::traits::{
@@ -183,9 +184,9 @@ where
183184
Some(DomainsCall::submit_bundle { .. }) => {
184185
<Runtime as Config>::WeightInfo::validate_submit_bundle()
185186
}
186-
Some(DomainsCall::submit_fraud_proof { .. }) => {
187-
// FIXME: proper weight
188-
Weight::zero()
187+
Some(DomainsCall::submit_fraud_proof { fraud_proof }) => {
188+
<Runtime as Config>::WeightInfo::fraud_proof_pre_check()
189+
.saturating_add(fraud_proof_verification_weights::<_, _, _, _>(fraud_proof))
189190
}
190191
Some(DomainsCall::submit_receipt { .. }) => {
191192
<Runtime as Config>::WeightInfo::validate_singleton_receipt()

crates/pallet-domains/src/lib.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2647,7 +2647,19 @@ impl<T: Config> Pallet<T> {
26472647
})?
26482648
}
26492649
#[cfg(any(feature = "std", feature = "runtime-benchmarks"))]
2650-
FraudProofVariant::Dummy => {}
2650+
FraudProofVariant::Dummy => {
2651+
// Almost every fraud proof (except `InvalidDomainBlockHash` fraud proof) need to call
2652+
// `get_domain_runtime_code_for_receipt` thus we include this part in the benchmark of
2653+
// the dummy fraud proof.
2654+
//
2655+
// NOTE: the dummy fraud proof's `maybe_domain_runtime_code_proof` is `None` thus
2656+
// this proof's verification is not included in the benchmark here.
2657+
let _ = Self::get_domain_runtime_code_for_receipt(
2658+
domain_id,
2659+
&bad_receipt,
2660+
fraud_proof.maybe_domain_runtime_code_proof.clone(),
2661+
)?;
2662+
}
26512663
}
26522664

26532665
// The priority of fraud proof is determined by how many blocks left before the bad ER

crates/pallet-domains/src/weights.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ pub trait WeightInfo {
5252
fn submit_receipt() -> Weight;
5353
fn validate_submit_bundle() -> Weight;
5454
fn validate_singleton_receipt() -> Weight;
55+
fn fraud_proof_pre_check() -> Weight;
5556
}
5657

5758
/// Weights for pallet_domains using the Substrate node and recommended hardware.
@@ -626,6 +627,25 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
626627
.saturating_add(T::DbWeight::get().reads(22))
627628
.saturating_add(T::DbWeight::get().writes(2))
628629
}
630+
/// Storage: `Domains::BlockTreeNodes` (r:2 w:0)
631+
/// Proof: `Domains::BlockTreeNodes` (`max_values`: None, `max_size`: None, mode: `Measured`)
632+
/// Storage: `Domains::HeadReceiptNumber` (r:1 w:0)
633+
/// Proof: `Domains::HeadReceiptNumber` (`max_values`: None, `max_size`: None, mode: `Measured`)
634+
/// Storage: `Domains::DomainRegistry` (r:1 w:0)
635+
/// Proof: `Domains::DomainRegistry` (`max_values`: None, `max_size`: None, mode: `Measured`)
636+
/// Storage: `Domains::RuntimeRegistry` (r:1 w:0)
637+
/// Proof: `Domains::RuntimeRegistry` (`max_values`: None, `max_size`: None, mode: `Measured`)
638+
/// Storage: `Domains::LatestConfirmedDomainExecutionReceipt` (r:1 w:0)
639+
/// Proof: `Domains::LatestConfirmedDomainExecutionReceipt` (`max_values`: None, `max_size`: None, mode: `Measured`)
640+
fn fraud_proof_pre_check() -> Weight {
641+
// Proof Size summary in bytes:
642+
// Measured: `2483056`
643+
// Estimated: `2488996`
644+
// Minimum execution time: 931_000_000 picoseconds.
645+
Weight::from_parts(976_000_000, 0)
646+
.saturating_add(Weight::from_parts(0, 2488996))
647+
.saturating_add(T::DbWeight::get().reads(6))
648+
}
629649
}
630650

631651
// For backwards compatibility and tests
@@ -1199,4 +1219,23 @@ impl WeightInfo for () {
11991219
.saturating_add(ParityDbWeight::get().reads(22))
12001220
.saturating_add(ParityDbWeight::get().writes(2))
12011221
}
1222+
/// Storage: `Domains::BlockTreeNodes` (r:2 w:0)
1223+
/// Proof: `Domains::BlockTreeNodes` (`max_values`: None, `max_size`: None, mode: `Measured`)
1224+
/// Storage: `Domains::HeadReceiptNumber` (r:1 w:0)
1225+
/// Proof: `Domains::HeadReceiptNumber` (`max_values`: None, `max_size`: None, mode: `Measured`)
1226+
/// Storage: `Domains::DomainRegistry` (r:1 w:0)
1227+
/// Proof: `Domains::DomainRegistry` (`max_values`: None, `max_size`: None, mode: `Measured`)
1228+
/// Storage: `Domains::RuntimeRegistry` (r:1 w:0)
1229+
/// Proof: `Domains::RuntimeRegistry` (`max_values`: None, `max_size`: None, mode: `Measured`)
1230+
/// Storage: `Domains::LatestConfirmedDomainExecutionReceipt` (r:1 w:0)
1231+
/// Proof: `Domains::LatestConfirmedDomainExecutionReceipt` (`max_values`: None, `max_size`: None, mode: `Measured`)
1232+
fn fraud_proof_pre_check() -> Weight {
1233+
// Proof Size summary in bytes:
1234+
// Measured: `2483056`
1235+
// Estimated: `2488996`
1236+
// Minimum execution time: 931_000_000 picoseconds.
1237+
Weight::from_parts(976_000_000, 0)
1238+
.saturating_add(Weight::from_parts(0, 2488996))
1239+
.saturating_add(ParityDbWeight::get().reads(6))
1240+
}
12021241
}

crates/sp-domains-fraud-proof/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@ sp-trie.workspace = true
3838
sp-weights.workspace = true
3939
subspace-core-primitives.workspace = true
4040
subspace-runtime-primitives.workspace = true
41+
tracing.workspace = true
4142
trie-db.workspace = true
4243
thiserror.workspace = true
4344

4445
[dev-dependencies]
46+
criterion.workspace = true
4547
domain-block-preprocessor.workspace = true
4648
domain-test-service.workspace = true
4749
ethereum.workspace = true
@@ -52,13 +54,18 @@ frame-system.workspace = true
5254
futures.workspace = true
5355
libsecp256k1 = { workspace = true, features = ["static-context", "hmac"] }
5456
pallet-balances.workspace = true
57+
pallet-domains.workspace = true
5558
pallet-ethereum.workspace = true
5659
rand = { workspace = true, features = ["min_const_gen"] }
5760
rlp.workspace = true
5861
sp-core.workspace = true
62+
sp-externalities.workspace = true
63+
sp-weights.workspace = true
5964
sc-cli.workspace = true
6065
sp-domains = { workspace = true, features = ["test-ethereum"] }
66+
sc-domains.workspace = true
6167
sc-service.workspace = true
68+
subspace-test-runtime.workspace = true
6269
subspace-test-service.workspace = true
6370
subspace-runtime-primitives.workspace = true
6471
tempfile.workspace = true
@@ -98,3 +105,7 @@ std = [
98105
"thiserror/std",
99106
]
100107
runtime-benchmarks = []
108+
109+
[[bench]]
110+
name = "fraud_proof_verification"
111+
harness = false

0 commit comments

Comments
 (0)