sp_subspace_mmr/
runtime_interface.rs1#[cfg(all(feature = "std", not(feature = "runtime-benchmarks")))]
5use crate::host_functions::SubspaceMmrExtension;
6use parity_scale_codec::{Decode, Encode};
7use scale_info::TypeInfo;
8use scale_info::prelude::vec::Vec;
9use sp_core::H256;
10#[cfg(all(feature = "std", not(feature = "runtime-benchmarks")))]
11use sp_externalities::ExternalitiesExt;
12use sp_mmr_primitives::EncodableOpaqueLeaf;
13use sp_runtime_interface::pass_by::{
14 AllocateAndReturnByCodec, PassFatPointerAndDecode, PassFatPointerAndRead,
15 PassPointerAndReadCopy,
16};
17use sp_runtime_interface::runtime_interface;
18use subspace_core_primitives::BlockNumber;
19
20#[runtime_interface]
22pub trait SubspaceMmrRuntimeInterface {
23 fn get_mmr_leaf_data(
25 &mut self,
26 consensus_block_hash: PassPointerAndReadCopy<H256, 32>,
27 ) -> AllocateAndReturnByCodec<Option<LeafData>> {
28 #[cfg(not(feature = "runtime-benchmarks"))]
29 {
30 self.extension::<SubspaceMmrExtension>()
31 .expect("No `SubspaceMmrExtension` associated for the current context!")
32 .get_mmr_leaf_data(consensus_block_hash)
33 }
34
35 #[cfg(feature = "runtime-benchmarks")]
41 {
42 crate::benchmarking::mock_subspace_mmr_extension()
43 .get_mmr_leaf_data(consensus_block_hash)
44 }
45 }
46
47 fn consensus_block_hash(
49 &mut self,
50 block_number: BlockNumber,
51 ) -> AllocateAndReturnByCodec<Option<H256>> {
52 #[cfg(not(feature = "runtime-benchmarks"))]
53 {
54 self.extension::<SubspaceMmrExtension>()
55 .expect("No `SubspaceMmrExtension` associated for the current context!")
56 .consensus_block_hash(block_number)
57 }
58
59 #[cfg(feature = "runtime-benchmarks")]
63 {
64 crate::benchmarking::mock_subspace_mmr_extension().consensus_block_hash(block_number)
65 }
66 }
67}
68
69#[derive(Debug, Decode, Encode, TypeInfo, PartialEq, Eq, Clone, Default)]
71pub struct LeafData {
72 pub state_root: H256,
73 pub extrinsics_root: H256,
74}
75
76#[runtime_interface]
77pub trait DomainMmrRuntimeInterface {
78 fn verify_mmr_proof(
80 &mut self,
81 leaves: PassFatPointerAndDecode<Vec<EncodableOpaqueLeaf>>,
82 encoded_proof: PassFatPointerAndRead<Vec<u8>>,
83 ) -> bool {
84 #[cfg(not(feature = "runtime-benchmarks"))]
85 {
86 self.extension::<SubspaceMmrExtension>()
87 .expect("No `SubspaceMmrExtension` associated for the current context!")
88 .verify_mmr_proof(leaves, encoded_proof)
89 }
90
91 #[cfg(feature = "runtime-benchmarks")]
95 {
96 crate::benchmarking::mock_subspace_mmr_extension()
97 .verify_mmr_proof(leaves, encoded_proof)
98 }
99 }
100
101 fn is_consensus_block_finalized(&mut self, block_number: BlockNumber) -> bool {
104 #[cfg(not(feature = "runtime-benchmarks"))]
105 {
106 self.extension::<SubspaceMmrExtension>()
107 .expect("No `SubspaceMmrExtension` associated for the current context!")
108 .is_consensus_block_finalized(block_number)
109 }
110
111 #[cfg(feature = "runtime-benchmarks")]
115 {
116 crate::benchmarking::mock_subspace_mmr_extension()
117 .is_consensus_block_finalized(block_number)
118 }
119 }
120}