Skip to content

Commit

Permalink
Merge pull request #2769 from subspace/bundle-weight-host-func
Browse files Browse the repository at this point in the history
Introduce the `bundle_weight` host function
  • Loading branch information
NingLin-P authored May 28, 2024
2 parents 47ccb63 + f995017 commit 98f60ed
Show file tree
Hide file tree
Showing 6 changed files with 53 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.

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 @@ -34,6 +34,7 @@ sp-state-machine = { optional = true, git = "https://github.com/subspace/polkado
sp-subspace-mmr = { version = "0.1.0", default-features = false, path = "../sp-subspace-mmr" }
sp-std = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "9b8cdb87de8f1c0e6b48c468b6196d1d99eeabee" }
sp-trie = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "9b8cdb87de8f1c0e6b48c468b6196d1d99eeabee" }
sp-weights = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "9b8cdb87de8f1c0e6b48c468b6196d1d99eeabee" }
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 @@ -93,6 +94,7 @@ std = [
"sp-state-machine/std",
"sp-subspace-mmr/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 = "9b8cdb87de8f1c0e6b48c468b6196d1d99eeabee" }
sp-state-machine = { git = "https://github.com/subspace/polkadot-sdk", rev = "9b8cdb87de8f1c0e6b48c468b6196d1d99eeabee" }
sp-timestamp = { git = "https://github.com/subspace/polkadot-sdk", rev = "9b8cdb87de8f1c0e6b48c468b6196d1d99eeabee" }
sp-weights = { git = "https://github.com/subspace/polkadot-sdk", rev = "9b8cdb87de8f1c0e6b48c468b6196d1d99eeabee" }
sp-version = { git = "https://github.com/subspace/polkadot-sdk", rev = "9b8cdb87de8f1c0e6b48c468b6196d1d99eeabee" }
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 98f60ed

Please sign in to comment.