mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 13:31:10 +00:00
d530bf2199
commit 265365920836bb1d286c9b48b1902a2de278fdd9 Author: Hernando Castano <castano.ha@gmail.com> Date: Wed Jan 29 19:51:15 2020 -0500 Move hc-jp-bridge repo to different folder commit 8271991e95320baba70bd1cb9c4234d0ffd5b638 Merge: 57d0811 304cbc5 Author: Hernando Castano <castano.ha@gmail.com> Date: Wed Jan 29 19:36:41 2020 -0500 Merge branch 'hc-jp-bridge-module' of hc-jp-bridge-module commit 304cbc5f02d003ffa5404c1c01e461e5b8539888 Author: Hernando Castano <HCastano@users.noreply.github.com> Date: Wed Jan 29 00:38:27 2020 -0500 Update bridge pallet to work with the (almost) lastest master (#4672) * Update decl_error usage * WIP: Update error handling to use DispatchResult * Get module compiling with new error handling * Make tests compile again Main change was updating the usage of InMemoryBackend * Move `sp-state-machine` into dev-dependencies * Bump dependencies to v2.0.0 * Remove some stray comments * Appy code review suggestion commit 510cd6d96372688517496efa61773ea2839f8474 Author: Hernando Castano <HCastano@users.noreply.github.com> Date: Tue Dec 17 12:52:51 2019 -0500 Move Bridge Pallet into FRAME (#4373) * Move `bridge` crate into `frame` folder * Make `bridge` pallet compile after `the-big-reorg` commit ab54e838ef75e6a3f68fd0944bf22598c10c552f Author: Hernando Castano <castano.ha@gmail.com> Date: Mon Nov 11 21:56:40 2019 +0100 Use new StorageProof type from #3834 commit 8fc8911fd1b4acc2274c6863fb3dba91b30c90af Author: Hernando Castano <HCastano@users.noreply.github.com> Date: Tue Nov 5 00:50:34 2019 +0100 Verify Ancestry between Headers (#3963) * Create module for checking ancestry proofs * Use Vec of Headers instead of a HashMap * Move the ancestry verification into the lib.rs file * Change the proof format to exclude `child` and `ancestor` headers * Add a testing function for building header chains * Rename AncestorNotFound error to InvalidAncestryProof * Use ancestor hash instead of header when verifying ancestry * Clean up some stuff missed in the merge commit dbe85738b68358b790cf927b34a804b965a88f96 or: Hernando Castano <HCastano@users.noreply.github.com> Date: Fri Nov 1 15:41:58 2019 +0100 Check given Grandpa validator set against set found in storage (#3915) * Make StorageProofChecker happy * Update some tests * Check given validator set against set found in storage * Use Finality Grandpa's Authority Id and Weight * Add better error handling * Use error type from decl_error! macro commit 31b09216603d3e9c21144ce8c0b6bf59307a4f97 or: Hernando Castano <HCastano@users.noreply.github.com> Date: Wed Oct 23 14:55:37 2019 +0200 Make tests work after the changes introduced in #3793 (#3874) * Make tests work after the changes introduced in #3793 * Remove unneccessary import commit bce6d804aa86504599ff912387295c58f846cbf3 Author: Jim Posen <jim.posen@gmail.com> Date: Thu Oct 10 12:18:58 2019 +0200 Logic for checking Substrate proofs from within runtime module. (#3783) commit a7013e94b6c772c1d45a7cacbb445f73f6554fca Author: Hernando Castano <castano.ha@gmail.com> Date: Fri Oct 4 15:21:00 2019 +0300 Allow tracking of multiple bridges commit 3cf648242d631e32bd553a67df54bf5a48912839 Author: Hernando Castano <castano.ha@gmail.com> Date: Tue Oct 1 14:55:04 2019 +0200 Add BridgeId => Bridge mapping commit 001c74c45072213e01857d0a2454379b447c5a76 Author: Hernando Castano <castano.ha@gmail.com> Date: Tue Oct 1 11:10:19 2019 +0200 Get the mock runtime for tests set up commit 38443a1e8b424ed2f148eb95121d009f730e3b5a Author: Hernando Castano <castano.ha@gmail.com> Date: Fri Sep 27 14:52:53 2019 +0200 Clean up some warnings commit bdc3b01401e89c7111f8bf71f84c50750d25089f Author: Hernando Castano <castano.ha@gmail.com> Date: Thu Sep 26 16:41:01 2019 +0200 Add more skeleton code commit 26995efbf4bac2842eb2822322f7ad3c3e88feb8 Author: Hernando Castano <castano.ha@gmail.com> Date: Wed Sep 25 15:16:57 2019 +0200 Create `bridge` module skeleton
113 lines
3.7 KiB
Rust
113 lines
3.7 KiB
Rust
// Copyright 2017-2019 Parity Technologies (UK) Ltd.
|
|
// This file is part of Substrate.
|
|
|
|
// Substrate is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
|
|
// Substrate is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//! Logic for checking Substrate storage proofs.
|
|
|
|
use hash_db::{Hasher, HashDB, EMPTY_PREFIX};
|
|
use sp_trie::{MemoryDB, Trie, trie_types::TrieDB};
|
|
use sp_runtime::RuntimeDebug;
|
|
|
|
pub(crate) type StorageProof = Vec<Vec<u8>>;
|
|
|
|
/// This struct is used to read storage values from a subset of a Merklized database. The "proof"
|
|
/// is a subset of the nodes in the Merkle structure of the database, so that it provides
|
|
/// authentication against a known Merkle root as well as the values in the database themselves.
|
|
pub struct StorageProofChecker<H>
|
|
where H: Hasher
|
|
{
|
|
root: H::Out,
|
|
db: MemoryDB<H>,
|
|
}
|
|
|
|
impl<H> StorageProofChecker<H>
|
|
where H: Hasher
|
|
{
|
|
/// Constructs a new storage proof checker.
|
|
///
|
|
/// This returns an error if the given proof is invalid with respect to the given root.
|
|
pub fn new(root: H::Out, proof: StorageProof) -> Result<Self, Error> {
|
|
let mut db = MemoryDB::default();
|
|
for item in proof {
|
|
db.insert(EMPTY_PREFIX, &item);
|
|
}
|
|
let checker = StorageProofChecker {
|
|
root,
|
|
db,
|
|
};
|
|
// Return error if trie would be invalid.
|
|
let _ = checker.trie()?;
|
|
Ok(checker)
|
|
}
|
|
|
|
/// Reads a value from the available subset of storage. If the value cannot be read due to an
|
|
/// incomplete or otherwise invalid proof, this returns an error.
|
|
pub fn read_value(&self, key: &[u8]) -> Result<Option<Vec<u8>>, Error> {
|
|
self.trie()?
|
|
.get(key)
|
|
.map(|value| value.map(|value| value.to_vec()))
|
|
.map_err(|_| Error::StorageValueUnavailable)
|
|
}
|
|
|
|
fn trie(&self) -> Result<TrieDB<H>, Error> {
|
|
TrieDB::new(&self.db, &self.root)
|
|
.map_err(|_| Error::StorageRootMismatch)
|
|
}
|
|
}
|
|
|
|
#[derive(RuntimeDebug, PartialEq)]
|
|
pub enum Error {
|
|
StorageRootMismatch,
|
|
StorageValueUnavailable,
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
use sp_core::{Blake2Hasher, H256};
|
|
use sp_state_machine::{prove_read, backend::Backend, InMemoryBackend};
|
|
|
|
#[test]
|
|
fn storage_proof_check() {
|
|
// construct storage proof
|
|
let backend = <InMemoryBackend<Blake2Hasher>>::from(vec![
|
|
(None, vec![(b"key1".to_vec(), Some(b"value1".to_vec()))]),
|
|
(None, vec![(b"key2".to_vec(), Some(b"value2".to_vec()))]),
|
|
(None, vec![(b"key3".to_vec(), Some(b"value3".to_vec()))]),
|
|
// Value is too big to fit in a branch node
|
|
(None, vec![(b"key11".to_vec(), Some(vec![0u8; 32]))]),
|
|
]);
|
|
let root = backend.storage_root(std::iter::empty()).0;
|
|
let proof: StorageProof = prove_read(backend, &[&b"key1"[..], &b"key2"[..], &b"key22"[..]])
|
|
.unwrap()
|
|
.iter_nodes()
|
|
.collect();
|
|
|
|
// check proof in runtime
|
|
let checker = <StorageProofChecker<Blake2Hasher>>::new(root, proof.clone()).unwrap();
|
|
assert_eq!(checker.read_value(b"key1"), Ok(Some(b"value1".to_vec())));
|
|
assert_eq!(checker.read_value(b"key2"), Ok(Some(b"value2".to_vec())));
|
|
assert_eq!(checker.read_value(b"key11111"), Err(Error::StorageValueUnavailable));
|
|
assert_eq!(checker.read_value(b"key22"), Ok(None));
|
|
|
|
// checking proof against invalid commitment fails
|
|
assert_eq!(
|
|
<StorageProofChecker<Blake2Hasher>>::new(H256::random(), proof).err(),
|
|
Some(Error::StorageRootMismatch)
|
|
);
|
|
}
|
|
}
|