Skip to content

Commit

Permalink
Introduce the bundle_weight host function
Browse files Browse the repository at this point in the history
This host function is required by the incoming invalid bundle weight fraud proof,
also notice it is marked as  to keep compatible with gemini-3h

Signed-off-by: linning <[email protected]>
  • Loading branch information
NingLin-P committed May 15, 2024
1 parent d159452 commit 1c4b3fb
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

8 changes: 8 additions & 0 deletions crates/pallet-domains/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,14 @@ impl FraudProofHostFunctions for MockDomainFraudProofExtension {
) -> Option<bool> {
None
}

fn bundle_weight(
&self,
_domain_runtime_code: Vec<u8>,
_bundle_body: Vec<OpaqueExtrinsic>,
) -> Option<Weight> {
None
}
}

pub(crate) fn new_test_ext_with_extensions() -> sp_io::TestExternalities {
Expand Down
2 changes: 2 additions & 0 deletions crates/sp-domains-fraud-proof/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ sp-runtime-interface = { default-features = false, git = "https://github.com/sub
sp-state-machine = { optional = true, git = "https://github.com/subspace/polkadot-sdk", rev = "808269708cf5375526755797e8f9a9986016727d" }
sp-std = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "808269708cf5375526755797e8f9a9986016727d" }
sp-trie = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "808269708cf5375526755797e8f9a9986016727d" }
sp-weights = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "808269708cf5375526755797e8f9a9986016727d" }
subspace-core-primitives = { version = "0.1.0", default-features = false, path = "../subspace-core-primitives" }
subspace-runtime-primitives = { version = "0.1.0", default-features = false, path = "../subspace-runtime-primitives" }
trie-db = { version = "0.28.0", default-features = false }
Expand Down Expand Up @@ -91,6 +92,7 @@ std = [
"sp-std/std",
"sp-state-machine/std",
"sp-trie/std",
"sp-weights/std",
"subspace-core-primitives/std",
"subspace-runtime-primitives/std",
"trie-db/std",
Expand Down
28 changes: 28 additions & 0 deletions crates/sp-domains-fraud-proof/src/host_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use sp_runtime::traits::{Block as BlockT, Hash as HashT, Header as HeaderT, Numb
use sp_runtime::OpaqueExtrinsic;
use sp_state_machine::{OverlayedChanges, StateMachine, TrieBackend, TrieBackendBuilder};
use sp_trie::{MemoryDB, StorageProof};
use sp_weights::Weight;
use std::borrow::Cow;
use std::marker::PhantomData;
use std::sync::Arc;
Expand Down Expand Up @@ -108,6 +109,12 @@ pub trait FraudProofHostFunctions: Send + Sync {
domain_runtime_code: Vec<u8>,
call: StatelessDomainRuntimeCall,
) -> Option<bool>;

fn bundle_weight(
&self,
domain_runtime_code: Vec<u8>,
bundle_body: Vec<OpaqueExtrinsic>,
) -> Option<Weight>;
}

sp_externalities::decl_extension! {
Expand Down Expand Up @@ -826,6 +833,27 @@ where
)),
}
}

fn bundle_weight(
&self,
domain_runtime_code: Vec<u8>,
bundle_body: Vec<OpaqueExtrinsic>,
) -> Option<Weight> {
let domain_stateless_runtime = StatelessRuntime::<DomainBlock, _>::new(
self.executor.clone(),
domain_runtime_code.into(),
);
let mut estimated_bundle_weight = Weight::default();
for opaque_extrinsic in bundle_body {
let encoded_extrinsic = opaque_extrinsic.encode();
let extrinsic =
<DomainBlock as BlockT>::Extrinsic::decode(&mut encoded_extrinsic.as_slice())
.ok()?;
let tx_weight = domain_stateless_runtime.extrinsic_weight(&extrinsic).ok()?;
estimated_bundle_weight = estimated_bundle_weight.saturating_add(tx_weight);
}
Some(estimated_bundle_weight)
}
}

type CreateProofCheckBackedResult<H> = Result<
Expand Down
12 changes: 12 additions & 0 deletions crates/sp-domains-fraud-proof/src/runtime_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use sp_domains::DomainId;
use sp_externalities::ExternalitiesExt;
use sp_runtime::OpaqueExtrinsic;
use sp_runtime_interface::runtime_interface;
use sp_weights::Weight;

/// Domain fraud proof related runtime interface
#[runtime_interface]
Expand Down Expand Up @@ -158,4 +159,15 @@ pub trait FraudProofRuntimeInterface {
.expect("No `FraudProofExtension` associated for the current context!")
.domain_runtime_call(domain_runtime_code, call)
}

#[version(1, register_only)]
fn bundle_weight(
&mut self,
domain_runtime_code: Vec<u8>,
bundle_body: Vec<OpaqueExtrinsic>,
) -> Option<Weight> {
self.extension::<FraudProofExtension>()
.expect("No `FraudProofExtension` associated for the current context!")
.bundle_weight(domain_runtime_code, bundle_body)
}
}
1 change: 1 addition & 0 deletions domains/client/block-preprocessor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ sp-messenger = { version = "0.1.0", path = "../../primitives/messenger" }
sp-runtime = { git = "https://github.com/subspace/polkadot-sdk", rev = "808269708cf5375526755797e8f9a9986016727d" }
sp-state-machine = { git = "https://github.com/subspace/polkadot-sdk", rev = "808269708cf5375526755797e8f9a9986016727d" }
sp-timestamp = { git = "https://github.com/subspace/polkadot-sdk", rev = "808269708cf5375526755797e8f9a9986016727d" }
sp-weights = { git = "https://github.com/subspace/polkadot-sdk", rev = "808269708cf5375526755797e8f9a9986016727d" }
sp-version = { git = "https://github.com/subspace/polkadot-sdk", rev = "808269708cf5375526755797e8f9a9986016727d" }
subspace-core-primitives = { version = "0.1.0", path = "../../../crates/subspace-core-primitives" }
subspace-runtime-primitives = { version = "0.1.0", path = "../../../crates/subspace-runtime-primitives" }
Expand Down
8 changes: 8 additions & 0 deletions domains/client/block-preprocessor/src/stateless_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use sp_runtime::traits::{Block as BlockT, NumberFor};
use sp_runtime::Storage;
use sp_state_machine::BasicExternalities;
use sp_version::RuntimeVersion;
use sp_weights::Weight;
use std::borrow::Cow;
use std::marker::PhantomData;
use std::sync::Arc;
Expand Down Expand Up @@ -310,4 +311,11 @@ where
Ok(sp_domains::operator_block_fees_final_key())
}
}

pub fn extrinsic_weight(
&self,
extrinsic: &<Block as BlockT>::Extrinsic,
) -> Result<Weight, ApiError> {
<Self as DomainCoreApi<Block>>::extrinsic_weight(self, Default::default(), extrinsic)
}
}

0 comments on commit 1c4b3fb

Please sign in to comment.