1use polkadot_sdk::*;
18
19use codec::{Decode, Encode};
20use ismp::router::{GetResponse, Request};
21use scale_info::TypeInfo;
22use sp_core::{RuntimeDebug, H256};
23use sp_mmr_primitives::NodeIndex;
24use sp_std::prelude::*;
25
26#[derive(codec::Encode, codec::Decode, scale_info::TypeInfo)]
28#[cfg_attr(feature = "std", derive(serde::Deserialize, serde::Serialize))]
29pub struct LeafIndexQuery {
30 pub commitment: H256,
32}
33
34#[derive(
36 codec::Encode,
37 codec::Decode,
38 scale_info::TypeInfo,
39 Ord,
40 PartialOrd,
41 Eq,
42 PartialEq,
43 Clone,
44 Copy,
45 RuntimeDebug,
46)]
47#[cfg_attr(feature = "std", derive(serde::Deserialize, serde::Serialize))]
48pub struct LeafIndexAndPos {
49 pub leaf_index: u64,
51 pub pos: u64,
53}
54
55#[derive(Debug, Clone, Encode, Decode, PartialEq, Eq, scale_info::TypeInfo)]
57pub enum Leaf {
58 Request(Request),
60 GetResponse(GetResponse),
62}
63
64impl FullLeaf for Leaf {
65 fn preimage(&self) -> Vec<u8> {
66 match self {
67 Leaf::Request(req) => req.encode(),
68 Leaf::GetResponse(res) => res.encode(),
69 }
70 }
71}
72
73#[derive(
75 codec::Encode,
76 codec::Decode,
77 scale_info::TypeInfo,
78 Ord,
79 PartialOrd,
80 Eq,
81 PartialEq,
82 Clone,
83 Copy,
84 RuntimeDebug,
85 Default,
86)]
87#[cfg_attr(feature = "std", derive(serde::Deserialize, serde::Serialize))]
88pub struct LeafMetadata {
89 pub index: u64,
91 pub position: u64,
93}
94
95pub trait OffchainDBProvider {
99 type Leaf;
101
102 fn count() -> u64;
104
105 fn push(leaf: Self::Leaf) -> LeafMetadata;
107
108 fn finalize() -> Result<H256, sp_mmr_primitives::Error>;
111
112 fn leaf(pos: NodeIndex) -> Result<Option<Self::Leaf>, sp_mmr_primitives::Error>;
114
115 fn proof(
118 indices: Vec<NodeIndex>,
119 ) -> Result<(Vec<Self::Leaf>, sp_mmr_primitives::LeafProof<H256>), sp_mmr_primitives::Error>;
120}
121
122pub fn leaf_default_key(commitment: H256) -> Vec<u8> {
124 let prefix = b"no_op";
125 (prefix, commitment).encode()
126}
127
128impl OffchainDBProvider for () {
129 type Leaf = Leaf;
130
131 fn count() -> u64 {
132 0
133 }
134
135 fn proof(
136 _indices: Vec<u64>,
137 ) -> Result<(Vec<Self::Leaf>, sp_mmr_primitives::LeafProof<H256>), sp_mmr_primitives::Error> {
138 Err(sp_mmr_primitives::Error::GenerateProof)?
139 }
140
141 fn push(leaf: Self::Leaf) -> LeafMetadata {
142 let encoded = leaf.preimage();
143 let commitment = sp_io::hashing::keccak_256(&encoded);
144 let offchain_key = leaf_default_key(commitment.into());
145 sp_io::offchain_index::set(&offchain_key, &leaf.encode());
146 Default::default()
147 }
148
149 fn finalize() -> Result<H256, sp_mmr_primitives::Error> {
150 Ok(H256::default())
151 }
152
153 fn leaf(_pos: NodeIndex) -> Result<Option<Self::Leaf>, sp_mmr_primitives::Error> {
154 Ok(None)
155 }
156}
157
158pub trait FullLeaf: Clone + PartialEq + core::fmt::Debug + codec::FullCodec {
160 fn preimage(&self) -> Vec<u8>;
162}
163
164pub trait ForkIdentifier<T: frame_system::Config> {
167 fn identifier() -> T::Hash;
169}
170
171#[derive(TypeInfo, Encode, Decode, serde::Deserialize, serde::Serialize)]
173pub enum ProofKeys {
174 Requests(Vec<H256>),
176 Responses(Vec<H256>),
178}
179
180#[derive(codec::Encode, codec::Decode, RuntimeDebug, Clone, PartialEq, Eq, TypeInfo)]
182#[cfg_attr(feature = "std", derive(serde::Deserialize, serde::Serialize))]
183pub struct Proof<Hash> {
184 pub leaf_indices_and_pos: Vec<LeafIndexAndPos>,
186 pub leaf_count: NodeIndex,
188 pub items: Vec<Hash>,
190}