Skip to content

Commit

Permalink
define host function to get consensus block hash and define a host fu…
Browse files Browse the repository at this point in the history
…nction provider for pallet_mmr
  • Loading branch information
vedhavyas committed Apr 15, 2024
1 parent e45c1d3 commit 6126b79
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 7 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion crates/sc-domains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl<CClient, CBlock, Block, Executor> ExtensionsFactoryT<Block>
where
Block: BlockT,
CBlock: BlockT,
CBlock::Hash: From<H256>,
CBlock::Hash: From<H256> + Into<H256>,
CClient: HeaderBackend<CBlock> + ProvideRuntimeApi<CBlock> + 'static,
CClient::Api: MmrApi<CBlock, H256, NumberFor<CBlock>>
+ MessengerApi<CBlock>
Expand Down
2 changes: 2 additions & 0 deletions crates/sp-subspace-mmr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ sp-externalities = { default-features = false, git = "https://github.com/subspac
sp-mmr-primitives = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "b66c1c2e48b44f6433b421e6d5a0aedd2165b85f" }
sp-runtime = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "b66c1c2e48b44f6433b421e6d5a0aedd2165b85f" }
sp-runtime-interface = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "b66c1c2e48b44f6433b421e6d5a0aedd2165b85f" }
subspace-core-primitives = { default-features = false, path = "../subspace-core-primitives"}

[features]
default = ["std"]
Expand All @@ -38,4 +39,5 @@ std = [
"sp-mmr-primitives/std",
"sp-runtime/std",
"sp-runtime-interface/std",
"subspace-core-primitives/std"
]
17 changes: 16 additions & 1 deletion crates/sp-subspace-mmr/src/host_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub use sp_mmr_primitives::{EncodableOpaqueLeaf, MmrApi, Proof};
use sp_runtime::traits::{Block as BlockT, Header as HeaderT, NumberFor};
use std::marker::PhantomData;
use std::sync::Arc;
use subspace_core_primitives::BlockNumber;

/// Trait to query MMR specific data through host function..
pub trait SubspaceMmrHostFunctions: Send + Sync {
Expand All @@ -15,6 +16,12 @@ pub trait SubspaceMmrHostFunctions: Send + Sync {

/// Verifies the mmr proof using consensus chain.
fn verify_mmr_proof(&self, leaves: Vec<EncodableOpaqueLeaf>, encoded_proof: Vec<u8>) -> bool;

/// Returns the consensus block hash for given block number.
fn consensus_block_hash(
&self,
block_number: subspace_core_primitives::BlockNumber,
) -> Option<H256>;
}

sp_externalities::decl_extension! {
Expand Down Expand Up @@ -46,7 +53,7 @@ impl<Block, Client> SubspaceMmrHostFunctionsImpl<Block, Client> {
impl<Block, Client> SubspaceMmrHostFunctions for SubspaceMmrHostFunctionsImpl<Block, Client>
where
Block: BlockT,
Block::Hash: From<H256>,
Block::Hash: From<H256> + Into<H256>,
Client: HeaderBackend<Block> + ProvideRuntimeApi<Block>,
Client::Api: MmrApi<Block, H256, NumberFor<Block>>,
{
Expand Down Expand Up @@ -81,4 +88,12 @@ where
"Runtime Api should not fail in host function, there is no recovery from this; qed.",
).is_ok()
}

fn consensus_block_hash(&self, block_number: BlockNumber) -> Option<H256> {
let block_number = NumberFor::<Block>::from(block_number);
self.consensus_client
.hash(block_number)
.expect("Header must be available. This is unrecoverable error")
.map(|block_hash| block_hash.into())
}
}
8 changes: 8 additions & 0 deletions crates/sp-subspace-mmr/src/runtime_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use sp_core::H256;
use sp_externalities::ExternalitiesExt;
use sp_mmr_primitives::EncodableOpaqueLeaf;
use sp_runtime_interface::runtime_interface;
use subspace_core_primitives::BlockNumber;

/// MMR related runtime interface
#[runtime_interface]
Expand All @@ -22,6 +23,13 @@ pub trait SubspaceMmrRuntimeInterface {
.expect("No `SubspaceMmrExtension` associated for the current context!")
.get_mmr_leaf_data(consensus_block_hash)
}

/// Returns the consensus block hash for a given block number.
fn consensus_block_hash(&mut self, block_number: BlockNumber) -> Option<H256> {
self.extension::<SubspaceMmrExtension>()
.expect("No `SubspaceMmrExtension` associated for the current context!")
.consensus_block_hash(block_number)
}
}

/// Leaf data sent back from host function.
Expand Down
2 changes: 2 additions & 0 deletions crates/subspace-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ sp-offchain = { default-features = false, git = "https://github.com/subspace/pol
sp-runtime = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "b66c1c2e48b44f6433b421e6d5a0aedd2165b85f" }
sp-session = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "b66c1c2e48b44f6433b421e6d5a0aedd2165b85f" }
sp-std = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "b66c1c2e48b44f6433b421e6d5a0aedd2165b85f" }
sp-subspace-mmr = { default-features = false, path = "../sp-subspace-mmr" }
sp-transaction-pool = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "b66c1c2e48b44f6433b421e6d5a0aedd2165b85f" }
sp-version = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "b66c1c2e48b44f6433b421e6d5a0aedd2165b85f" }
static_assertions = "1.1.0"
Expand Down Expand Up @@ -118,6 +119,7 @@ std = [
"sp-runtime/std",
"sp-session/std",
"sp-std/std",
"sp-subspace-mmr/std",
"sp-transaction-pool/std",
"sp-version/std",
"subspace-core-primitives/std",
Expand Down
11 changes: 10 additions & 1 deletion crates/subspace-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ use sp_runtime::{
use sp_std::collections::btree_map::BTreeMap;
use sp_std::marker::PhantomData;
use sp_std::prelude::*;
use sp_subspace_mmr::subspace_mmr_runtime_interface::consensus_block_hash;
use sp_version::RuntimeVersion;
use static_assertions::const_assert;
use subspace_core_primitives::objects::BlockObjectMapping;
Expand Down Expand Up @@ -738,12 +739,20 @@ mod mmr {
pub type Hash = <Hashing as sp_runtime::traits::Hash>::Output;
}

pub struct BlockHashProvider;

impl pallet_mmr::BlockHashProvider<BlockNumber, Hash> for BlockHashProvider {
fn block_hash(block_number: BlockNumber) -> Hash {
consensus_block_hash(block_number).expect("Hash must exist for a given block number.")
}
}

impl pallet_mmr::Config for Runtime {
const INDEXING_PREFIX: &'static [u8] = mmr::INDEXING_PREFIX;
type Hashing = Keccak256;
type LeafData = SubspaceMmr;
type OnNewRoot = SubspaceMmr;
type BlockHashProvider = pallet_mmr::DefaultBlockHashProvider<Runtime>;
type BlockHashProvider = BlockHashProvider;
type WeightInfo = ();
}

Expand Down
2 changes: 1 addition & 1 deletion crates/subspace-service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ impl<PosTable, Block, Client, DomainBlock> ExtensionsFactory<Block>
where
PosTable: Table,
Block: BlockT,
Block::Hash: From<H256>,
Block::Hash: From<H256> + Into<H256>,
DomainBlock: BlockT,
DomainBlock::Hash: Into<H256> + From<H256>,
Client: BlockBackend<Block>
Expand Down
2 changes: 1 addition & 1 deletion domains/service/src/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ fn new_partial<RuntimeApi, CBlock, CClient, BIMP>(
where
CBlock: BlockT,
NumberFor<CBlock>: From<NumberFor<Block>>,
CBlock::Hash: From<Hash>,
CBlock::Hash: From<Hash> + Into<Hash>,
CClient: HeaderBackend<CBlock>
+ BlockBackend<CBlock>
+ ProvideRuntimeApi<CBlock>
Expand Down
2 changes: 2 additions & 0 deletions test/subspace-test-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ sp-offchain = { default-features = false, git = "https://github.com/subspace/pol
sp-runtime = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "b66c1c2e48b44f6433b421e6d5a0aedd2165b85f" }
sp-session = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "b66c1c2e48b44f6433b421e6d5a0aedd2165b85f" }
sp-std = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "b66c1c2e48b44f6433b421e6d5a0aedd2165b85f" }
sp-subspace-mmr = { default-features = false, path = "../../crates/sp-subspace-mmr" }
sp-transaction-pool = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "b66c1c2e48b44f6433b421e6d5a0aedd2165b85f" }
sp-version = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "b66c1c2e48b44f6433b421e6d5a0aedd2165b85f" }
static_assertions = "1.1.0"
Expand Down Expand Up @@ -109,6 +110,7 @@ std = [
"sp-runtime/std",
"sp-session/std",
"sp-std/std",
"sp-subspace-mmr/std",
"sp-transaction-pool/std",
"sp-version/std",
"subspace-core-primitives/std",
Expand Down
11 changes: 10 additions & 1 deletion test/subspace-test-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -762,12 +762,21 @@ mod mmr {
pub type Hash = <Hashing as sp_runtime::traits::Hash>::Output;
}

pub struct BlockHashProvider;

impl pallet_mmr::BlockHashProvider<BlockNumber, Hash> for BlockHashProvider {
fn block_hash(block_number: BlockNumber) -> Hash {
sp_subspace_mmr::subspace_mmr_runtime_interface::consensus_block_hash(block_number)
.expect("Hash must exist for a given block number.")
}
}

impl pallet_mmr::Config for Runtime {
const INDEXING_PREFIX: &'static [u8] = mmr::INDEXING_PREFIX;
type Hashing = Keccak256;
type LeafData = SubspaceMmr;
type OnNewRoot = SubspaceMmr;
type BlockHashProvider = pallet_mmr::DefaultBlockHashProvider<Runtime>;
type BlockHashProvider = BlockHashProvider;
type WeightInfo = ();
}

Expand Down
2 changes: 1 addition & 1 deletion test/subspace-test-service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ impl<Block, Client, DomainBlock, Executor, CBackend> ExtensionsFactory<Block>
for MockExtensionsFactory<Client, DomainBlock, Executor, CBackend>
where
Block: BlockT,
Block::Hash: From<H256>,
Block::Hash: From<H256> + Into<H256>,
DomainBlock: BlockT,
DomainBlock::Hash: Into<H256> + From<H256>,
Client: BlockBackend<Block> + HeaderBackend<Block> + ProvideRuntimeApi<Block> + 'static,
Expand Down

0 comments on commit 6126b79

Please sign in to comment.