mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 08:51:09 +00:00
Storage root for Ext.
This commit is contained in:
@@ -93,9 +93,9 @@ pub fn chain_id() -> u64 {
|
||||
}
|
||||
|
||||
/// "Commit" all existing operations and get the resultant storage root.
|
||||
pub fn commit() -> [u8; 32] {
|
||||
pub fn storage_root() -> [u8; 32] {
|
||||
ext::with(|ext|
|
||||
ext.commit()
|
||||
ext.storage_root()
|
||||
).unwrap_or([0u8; 32])
|
||||
}
|
||||
|
||||
|
||||
@@ -40,6 +40,9 @@ pub trait Backend {
|
||||
/// Commit updates to the backend and get new state.
|
||||
fn commit<I>(&mut self, changes: I) -> Committed
|
||||
where I: IntoIterator<Item=Update>;
|
||||
|
||||
/// Get all key/value pairs into a Vec.
|
||||
fn pairs(&self) -> Vec<(&[u8], &[u8])>;
|
||||
}
|
||||
|
||||
/// Error impossible.
|
||||
@@ -87,6 +90,10 @@ impl Backend for InMemory {
|
||||
storage_tree_root,
|
||||
}
|
||||
}
|
||||
|
||||
fn pairs(&self) -> Vec<(&[u8], &[u8])> {
|
||||
self.inner.storage.iter().map(|(k, v)| (&k[..], &v[..])).collect()
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: DB-based backend
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
//! Conrete externalities implementation.
|
||||
|
||||
use std::{error, fmt};
|
||||
|
||||
use triehash::trie_root;
|
||||
use backend::Backend;
|
||||
use {Externalities, ExternalitiesError, OverlayedChanges};
|
||||
|
||||
@@ -76,7 +76,14 @@ impl<'a, B: 'a> Externalities for Ext<'a, B>
|
||||
42
|
||||
}
|
||||
|
||||
fn commit(&self) -> [u8; 32] {
|
||||
unimplemented!();
|
||||
fn storage_root(&self) -> [u8; 32] {
|
||||
let mut all_pairs = self.backend.pairs();
|
||||
all_pairs.extend(
|
||||
self.overlay.committed.storage.iter()
|
||||
.chain(self.overlay.prospective.storage.iter())
|
||||
.map(|(k, v)| (&k[..], &v[..]))
|
||||
);
|
||||
|
||||
trie_root(all_pairs.into_iter().map(|(k, v)| (k.to_vec(), v.to_vec()))).0
|
||||
}
|
||||
}
|
||||
|
||||
@@ -147,7 +147,7 @@ pub trait Externalities {
|
||||
fn chain_id(&self) -> u64;
|
||||
|
||||
/// Get the trie root of the current storage map.
|
||||
fn commit(&self) -> [u8; 32];
|
||||
fn storage_root(&self) -> [u8; 32];
|
||||
|
||||
/// Get the current set of authorities from storage.
|
||||
fn authorities(&self) -> Result<Vec<&[u8]>, ExternalitiesError> {
|
||||
|
||||
@@ -23,11 +23,13 @@ use triehash::trie_root;
|
||||
/// Simple HashMap based Externalities impl.
|
||||
#[derive(Debug, Default)]
|
||||
pub struct TestExternalities {
|
||||
/// The storage.
|
||||
pub storage: HashMap<Vec<u8>, Vec<u8>>,
|
||||
}
|
||||
|
||||
impl TestExternalities {
|
||||
fn new() -> Self {
|
||||
/// Create a new instance with empty storage.
|
||||
pub fn new() -> Self {
|
||||
TestExternalities {
|
||||
storage: HashMap::new(),
|
||||
}
|
||||
@@ -45,7 +47,7 @@ impl Externalities for TestExternalities {
|
||||
|
||||
fn chain_id(&self) -> u64 { 42 }
|
||||
|
||||
fn commit(&self) -> [u8; 32] {
|
||||
fn storage_root(&self) -> [u8; 32] {
|
||||
trie_root(self.storage.clone()).0
|
||||
}
|
||||
}
|
||||
@@ -61,6 +63,6 @@ mod tests {
|
||||
ext.set_storage(b"dog".to_vec(), b"puppy".to_vec());
|
||||
ext.set_storage(b"dogglesworth".to_vec(), b"cat".to_vec());
|
||||
const ROOT: [u8; 32] = hex!("8aad789dff2f538bca5d8ea56e8abe10f4c7ba3a5dea95fea4cd6e7c3a1168d3");
|
||||
assert_eq!(ext.commit(), ROOT);
|
||||
assert_eq!(ext.storage_root(), ROOT);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user