sc_client_api/proof_provider.rs
1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19//! Proof utilities
20use crate::{CompactProof, StorageProof};
21use sp_runtime::traits::Block as BlockT;
22use sp_state_machine::{KeyValueStates, KeyValueStorageLevel};
23use sp_storage::ChildInfo;
24
25/// Interface for providing block proving utilities.
26pub trait ProofProvider<Block: BlockT> {
27 /// Reads storage value at a given block + key, returning read proof.
28 fn read_proof(
29 &self,
30 hash: Block::Hash,
31 keys: &mut dyn Iterator<Item = &[u8]>,
32 ) -> sp_blockchain::Result<StorageProof>;
33
34 /// Reads child storage value at a given block + storage_key + key, returning
35 /// read proof.
36 fn read_child_proof(
37 &self,
38 hash: Block::Hash,
39 child_info: &ChildInfo,
40 keys: &mut dyn Iterator<Item = &[u8]>,
41 ) -> sp_blockchain::Result<StorageProof>;
42
43 /// Execute a call to a contract on top of state in a block of given hash
44 /// AND returning execution proof.
45 ///
46 /// No changes are made.
47 fn execution_proof(
48 &self,
49 hash: Block::Hash,
50 method: &str,
51 call_data: &[u8],
52 ) -> sp_blockchain::Result<(Vec<u8>, StorageProof)>;
53
54 /// Given a `Hash` iterate over all storage values starting at `start_keys`.
55 /// Last `start_keys` element contains last accessed key value.
56 /// With multiple `start_keys`, first `start_keys` element is
57 /// the current storage key of of the last accessed child trie.
58 /// at last level the value to start at exclusively.
59 /// Proofs is build until size limit is reached and always include at
60 /// least one key following `start_keys`.
61 /// Returns combined proof and the numbers of collected keys.
62 fn read_proof_collection(
63 &self,
64 hash: Block::Hash,
65 start_keys: &[Vec<u8>],
66 size_limit: usize,
67 ) -> sp_blockchain::Result<(CompactProof, u32)>;
68
69 /// Given a `Hash` iterate over all storage values starting at `start_key`.
70 /// Returns collected keys and values.
71 /// Returns the collected keys values content of the top trie followed by the
72 /// collected keys values of child tries.
73 /// Only child tries with their root part of the collected content or
74 /// related to `start_key` are attached.
75 /// For each collected state a boolean indicates if state reach
76 /// end.
77 fn storage_collection(
78 &self,
79 hash: Block::Hash,
80 start_key: &[Vec<u8>],
81 size_limit: usize,
82 ) -> sp_blockchain::Result<Vec<(KeyValueStorageLevel, bool)>>;
83
84 /// Verify read storage proof for a set of keys.
85 /// Returns collected key-value pairs and the nested state
86 /// depth of current iteration or 0 if completed.
87 fn verify_range_proof(
88 &self,
89 root: Block::Hash,
90 proof: CompactProof,
91 start_keys: &[Vec<u8>],
92 ) -> sp_blockchain::Result<(KeyValueStates, usize)>;
93}