referrerpolicy=no-referrer-when-downgrade

mmr_rpc/
lib.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18#![warn(missing_docs)]
19#![warn(unused_crate_dependencies)]
20
21//! Node-specific RPC methods for interaction with Merkle Mountain Range pallet.
22
23use std::{marker::PhantomData, sync::Arc};
24
25use codec::{Codec, Decode, Encode};
26use jsonrpsee::{
27	core::{async_trait, RpcResult},
28	proc_macros::rpc,
29	types::{error::ErrorObject, ErrorObjectOwned},
30};
31use serde::{Deserialize, Serialize};
32
33use sp_api::{ApiExt, ProvideRuntimeApi};
34use sp_blockchain::HeaderBackend;
35use sp_core::{
36	offchain::{storage::OffchainDb, OffchainDbExt, OffchainStorage},
37	Bytes,
38};
39use sp_mmr_primitives::{AncestryProof as MmrAncestryProof, Error as MmrError, LeafProof};
40use sp_runtime::traits::{Block as BlockT, NumberFor};
41
42pub use sp_mmr_primitives::MmrApi as MmrRuntimeApi;
43
44const RUNTIME_ERROR: i32 = 8000;
45const MMR_ERROR: i32 = 8010;
46
47/// Retrieved MMR leaves and their proof.
48#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
49#[serde(rename_all = "camelCase")]
50pub struct LeavesProof<BlockHash> {
51	/// Block hash the proof was generated for.
52	pub block_hash: BlockHash,
53	/// SCALE-encoded vector of `LeafData`.
54	pub leaves: Bytes,
55	/// SCALE-encoded proof data. See [sp_mmr_primitives::LeafProof].
56	pub proof: Bytes,
57}
58
59impl<BlockHash> LeavesProof<BlockHash> {
60	/// Create new `LeavesProof` from a given vector of `Leaf` and a
61	/// [sp_mmr_primitives::LeafProof].
62	pub fn new<Leaf, MmrHash>(
63		block_hash: BlockHash,
64		leaves: Vec<Leaf>,
65		proof: LeafProof<MmrHash>,
66	) -> Self
67	where
68		Leaf: Encode,
69		MmrHash: Encode,
70	{
71		Self { block_hash, leaves: Bytes(leaves.encode()), proof: Bytes(proof.encode()) }
72	}
73}
74
75/// MMR RPC methods.
76#[rpc(client, server)]
77pub trait MmrApi<BlockHash, BlockNumber, MmrHash> {
78	/// Get the MMR root hash for the current best block.
79	#[method(name = "mmr_root")]
80	fn mmr_root(&self, at: Option<BlockHash>) -> RpcResult<MmrHash>;
81
82	/// Generate an MMR proof for the given `block_numbers`.
83	///
84	/// This method calls into a runtime with MMR pallet included and attempts to generate
85	/// an MMR proof for the set of blocks that have the given `block_numbers` with the MMR root at
86	/// `best_known_block_number`. `best_known_block_number` must be larger than all the
87	/// `block_numbers` for the function to succeed.
88	///
89	/// Optionally via `at`, a block hash at which the runtime should be queried can be specified.
90	/// Optionally via `best_known_block_number`, the proof can be generated using the MMR's state
91	/// at a specific best block. Note that if `best_known_block_number` is provided, then also
92	/// specifying the block hash via `at` isn't super-useful here, unless you're generating proof
93	/// using non-finalized blocks where there are several competing forks. That's because MMR state
94	/// will be fixed to the state with `best_known_block_number`, which already points to
95	/// some historical block.
96	///
97	/// Returns the (full) leaves and a proof for these leaves (compact encoding, i.e. hash of
98	/// the leaves). Both parameters are SCALE-encoded.
99	/// The order of entries in the `leaves` field of the returned struct
100	/// is the same as the order of the entries in `block_numbers` supplied
101	#[method(name = "mmr_generateProof")]
102	fn generate_proof(
103		&self,
104		block_numbers: Vec<BlockNumber>,
105		best_known_block_number: Option<BlockNumber>,
106		at: Option<BlockHash>,
107	) -> RpcResult<LeavesProof<BlockHash>>;
108
109	/// Generate an MMR ancestry proof for the given `prev_block_number`.
110	///
111	/// This method calls into a runtime with MMR pallet included and attempts to generate
112	/// an MMR ancestry proof for the MMR root at the prior block with number `prev_block_number`,
113	/// with the reference MMR root at `best_known_block_number`. `best_known_block_number` must be
114	/// larger than the `prev_block_number` for the function to succeed.
115	///
116	/// Optionally via `at`, a block hash at which the runtime should be queried can be specified.
117	/// Optionally via `best_known_block_number`, the proof can be generated using the MMR's state
118	/// at a specific best block. Note that if `best_known_block_number` is provided, then also
119	/// specifying the block hash via `at` isn't super-useful here, unless you're generating proof
120	/// using non-finalized blocks where there are several competing forks. That's because MMR state
121	/// will be fixed to the state with `best_known_block_number`, which already points to
122	/// some historical block.
123	///
124	/// Returns the SCALE-encoded ancestry proof for the prior block's MMR root against the MMR root
125	/// of the best block specified. The order of entries in the `leaves` field of the returned
126	/// struct is the same as the order of the entries in `block_numbers` supplied
127	#[method(name = "mmr_generateAncestryProof")]
128	fn generate_ancestry_proof(
129		&self,
130		prev_block_number: BlockNumber,
131		best_known_block_number: Option<BlockNumber>,
132		at: Option<BlockHash>,
133	) -> RpcResult<MmrAncestryProof<MmrHash>>;
134
135	/// Verify an MMR `proof`.
136	///
137	/// This method calls into a runtime with MMR pallet included and attempts to verify
138	/// an MMR proof.
139	///
140	/// Returns `true` if the proof is valid, else returns the verification error.
141	#[method(name = "mmr_verifyProof")]
142	fn verify_proof(&self, proof: LeavesProof<BlockHash>) -> RpcResult<bool>;
143
144	/// Verify an MMR `proof` statelessly given an `mmr_root`.
145	///
146	/// This method calls into a runtime with MMR pallet included and attempts to verify
147	/// an MMR proof against a provided MMR root.
148	///
149	/// Returns `true` if the proof is valid, else returns the verification error.
150	#[method(name = "mmr_verifyProofStateless")]
151	fn verify_proof_stateless(
152		&self,
153		mmr_root: MmrHash,
154		proof: LeavesProof<BlockHash>,
155	) -> RpcResult<bool>;
156}
157
158/// MMR RPC methods.
159pub struct Mmr<Client, Block, S> {
160	client: Arc<Client>,
161	offchain_db: OffchainDb<S>,
162	_marker: PhantomData<Block>,
163}
164
165impl<C, B, S> Mmr<C, B, S> {
166	/// Create new `Mmr` with the given reference to the client.
167	pub fn new(client: Arc<C>, offchain_storage: S) -> Self {
168		Self { client, _marker: Default::default(), offchain_db: OffchainDb::new(offchain_storage) }
169	}
170}
171
172#[async_trait]
173impl<Client, Block, MmrHash, S> MmrApiServer<<Block as BlockT>::Hash, NumberFor<Block>, MmrHash>
174	for Mmr<Client, (Block, MmrHash), S>
175where
176	Block: BlockT,
177	Client: Send + Sync + 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,
178	Client::Api: MmrRuntimeApi<Block, MmrHash, NumberFor<Block>>,
179	MmrHash: Codec + Send + Sync + 'static,
180	S: OffchainStorage + 'static,
181{
182	fn mmr_root(&self, at: Option<<Block as BlockT>::Hash>) -> RpcResult<MmrHash> {
183		let block_hash = at.unwrap_or_else(||
184			// If the block hash is not supplied assume the best block.
185			self.client.info().best_hash);
186		let api = self.client.runtime_api();
187		let mmr_root = api
188			.mmr_root(block_hash)
189			.map_err(runtime_error_into_rpc_error)?
190			.map_err(mmr_error_into_rpc_error)?;
191		Ok(mmr_root)
192	}
193
194	fn generate_proof(
195		&self,
196		block_numbers: Vec<NumberFor<Block>>,
197		best_known_block_number: Option<NumberFor<Block>>,
198		at: Option<<Block as BlockT>::Hash>,
199	) -> RpcResult<LeavesProof<<Block as BlockT>::Hash>> {
200		let mut api = self.client.runtime_api();
201		let block_hash = at.unwrap_or_else(||
202			// If the block hash is not supplied assume the best block.
203			self.client.info().best_hash);
204
205		api.register_extension(OffchainDbExt::new(self.offchain_db.clone()));
206
207		let (leaves, proof) = api
208			.generate_proof(block_hash, block_numbers, best_known_block_number)
209			.map_err(runtime_error_into_rpc_error)?
210			.map_err(mmr_error_into_rpc_error)?;
211
212		Ok(LeavesProof::new(block_hash, leaves, proof))
213	}
214
215	fn generate_ancestry_proof(
216		&self,
217		prev_block_number: NumberFor<Block>,
218		best_known_block_number: Option<NumberFor<Block>>,
219		at: Option<<Block as BlockT>::Hash>,
220	) -> RpcResult<MmrAncestryProof<MmrHash>> {
221		let mut api = self.client.runtime_api();
222		let block_hash = at.unwrap_or_else(||
223			// If the block hash is not supplied assume the best block.
224			self.client.info().best_hash);
225
226		api.register_extension(OffchainDbExt::new(self.offchain_db.clone()));
227
228		let proof = api
229			.generate_ancestry_proof(block_hash, prev_block_number, best_known_block_number)
230			.map_err(runtime_error_into_rpc_error)?
231			.map_err(mmr_error_into_rpc_error)?;
232
233		Ok(proof)
234	}
235
236	fn verify_proof(&self, proof: LeavesProof<<Block as BlockT>::Hash>) -> RpcResult<bool> {
237		let mut api = self.client.runtime_api();
238
239		let leaves = Decode::decode(&mut &proof.leaves.0[..]).map_err(invalid_params)?;
240
241		let decoded_proof = Decode::decode(&mut &proof.proof.0[..]).map_err(invalid_params)?;
242
243		api.register_extension(OffchainDbExt::new(self.offchain_db.clone()));
244
245		api.verify_proof(proof.block_hash, leaves, decoded_proof)
246			.map_err(runtime_error_into_rpc_error)?
247			.map_err(mmr_error_into_rpc_error)?;
248
249		Ok(true)
250	}
251
252	fn verify_proof_stateless(
253		&self,
254		mmr_root: MmrHash,
255		proof: LeavesProof<<Block as BlockT>::Hash>,
256	) -> RpcResult<bool> {
257		let api = self.client.runtime_api();
258
259		let leaves = Decode::decode(&mut &proof.leaves.0[..]).map_err(invalid_params)?;
260
261		let decoded_proof = Decode::decode(&mut &proof.proof.0[..]).map_err(invalid_params)?;
262
263		api.verify_proof_stateless(proof.block_hash, mmr_root, leaves, decoded_proof)
264			.map_err(runtime_error_into_rpc_error)?
265			.map_err(mmr_error_into_rpc_error)?;
266
267		Ok(true)
268	}
269}
270
271/// Converts an mmr-specific error into a [`CallError`].
272fn mmr_error_into_rpc_error(err: MmrError) -> ErrorObjectOwned {
273	let error_code = MMR_ERROR +
274		match err {
275			MmrError::LeafNotFound => 1,
276			MmrError::GenerateProof => 2,
277			MmrError::Verify => 3,
278			MmrError::InvalidNumericOp => 4,
279			MmrError::InvalidBestKnownBlock => 5,
280			_ => 0,
281		};
282
283	ErrorObject::owned(error_code, err.to_string(), Some(format!("{:?}", err)))
284}
285
286/// Converts a runtime trap into a [`CallError`].
287fn runtime_error_into_rpc_error(err: impl std::fmt::Debug) -> ErrorObjectOwned {
288	ErrorObject::owned(RUNTIME_ERROR, "Runtime trapped", Some(format!("{:?}", err)))
289}
290
291fn invalid_params(e: impl std::error::Error) -> ErrorObjectOwned {
292	ErrorObject::owned(
293		jsonrpsee::types::error::ErrorCode::InvalidParams.code(),
294		e.to_string(),
295		None::<()>,
296	)
297}
298
299#[cfg(test)]
300mod tests {
301	use super::*;
302	use sp_core::H256;
303
304	#[test]
305	fn should_serialize_leaf_proof() {
306		// given
307		let leaf = vec![1_u8, 2, 3, 4];
308		let proof = LeafProof {
309			leaf_indices: vec![1],
310			leaf_count: 9,
311			items: vec![H256::repeat_byte(1), H256::repeat_byte(2)],
312		};
313
314		let leaf_proof = LeavesProof::new(H256::repeat_byte(0), vec![leaf], proof);
315
316		// when
317		let actual = serde_json::to_string(&leaf_proof).unwrap();
318
319		// then
320		assert_eq!(
321			actual,
322			r#"{"blockHash":"0x0000000000000000000000000000000000000000000000000000000000000000","leaves":"0x041001020304","proof":"0x04010000000000000009000000000000000801010101010101010101010101010101010101010101010101010101010101010202020202020202020202020202020202020202020202020202020202020202"}"#
323		);
324	}
325
326	#[test]
327	fn should_serialize_leaves_proof() {
328		// given
329		let leaf_a = vec![1_u8, 2, 3, 4];
330		let leaf_b = vec![2_u8, 2, 3, 4];
331		let proof = LeafProof {
332			leaf_indices: vec![1, 2],
333			leaf_count: 9,
334			items: vec![H256::repeat_byte(1), H256::repeat_byte(2)],
335		};
336
337		let leaf_proof = LeavesProof::new(H256::repeat_byte(0), vec![leaf_a, leaf_b], proof);
338
339		// when
340		let actual = serde_json::to_string(&leaf_proof).unwrap();
341
342		// then
343		assert_eq!(
344			actual,
345			r#"{"blockHash":"0x0000000000000000000000000000000000000000000000000000000000000000","leaves":"0x0810010203041002020304","proof":"0x080100000000000000020000000000000009000000000000000801010101010101010101010101010101010101010101010101010101010101010202020202020202020202020202020202020202020202020202020202020202"}"#
346		);
347	}
348
349	#[test]
350	fn should_deserialize_leaf_proof() {
351		// given
352		let expected = LeavesProof {
353			block_hash: H256::repeat_byte(0),
354			leaves: Bytes(vec![vec![1_u8, 2, 3, 4]].encode()),
355			proof: Bytes(
356				LeafProof {
357					leaf_indices: vec![1],
358					leaf_count: 9,
359					items: vec![H256::repeat_byte(1), H256::repeat_byte(2)],
360				}
361				.encode(),
362			),
363		};
364
365		// when
366		let actual: LeavesProof<H256> = serde_json::from_str(r#"{
367			"blockHash":"0x0000000000000000000000000000000000000000000000000000000000000000",
368			"leaves":"0x041001020304",
369			"proof":"0x04010000000000000009000000000000000801010101010101010101010101010101010101010101010101010101010101010202020202020202020202020202020202020202020202020202020202020202"
370		}"#).unwrap();
371
372		// then
373		assert_eq!(actual, expected);
374	}
375
376	#[test]
377	fn should_deserialize_leaves_proof() {
378		// given
379		let expected = LeavesProof {
380			block_hash: H256::repeat_byte(0),
381			leaves: Bytes(vec![vec![1_u8, 2, 3, 4], vec![2_u8, 2, 3, 4]].encode()),
382			proof: Bytes(
383				LeafProof {
384					leaf_indices: vec![1, 2],
385					leaf_count: 9,
386					items: vec![H256::repeat_byte(1), H256::repeat_byte(2)],
387				}
388				.encode(),
389			),
390		};
391
392		// when
393		let actual: LeavesProof<H256> = serde_json::from_str(r#"{
394			"blockHash":"0x0000000000000000000000000000000000000000000000000000000000000000",
395			"leaves":"0x0810010203041002020304",
396			"proof":"0x080100000000000000020000000000000009000000000000000801010101010101010101010101010101010101010101010101010101010101010202020202020202020202020202020202020202020202020202020202020202"
397		}"#).unwrap();
398
399		// then
400		assert_eq!(actual, expected);
401	}
402}