Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
trie tests
Browse files Browse the repository at this point in the history
  • Loading branch information
svyatonik committed Jun 7, 2018
1 parent 16535d3 commit 94fb5db
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 0 deletions.
39 changes: 39 additions & 0 deletions substrate/state-machine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,22 @@ mod tests {
use super::backend::InMemory;
use super::ext::Ext;

struct DummyCodeExecutor;

impl CodeExecutor for DummyCodeExecutor {
type Error = u8;

fn call<E: Externalities>(
&self,
ext: &mut E,
_code: &[u8],
_method: &str,
_data: &[u8],
) -> Result<Vec<u8>, Self::Error> {
Ok(vec![ext.storage(b"value1").unwrap()[0] + ext.storage(b"value2").unwrap()[0]])
}
}

#[test]
fn overlayed_storage_works() {
let mut overlayed = OverlayedChanges::default();
Expand Down Expand Up @@ -304,4 +320,27 @@ mod tests {
const ROOT: [u8; 32] = hex!("8aad789dff2f538bca5d8ea56e8abe10f4c7ba3a5dea95fea4cd6e7c3a1168d3");
assert_eq!(ext.storage_root(), ROOT);
}

#[test]
fn execute_works() {
assert_eq!(execute(&trie_backend::tests::test_trie(),
&mut Default::default(), &DummyCodeExecutor, "test", &[]).unwrap().0, vec![66]);
}

#[test]
fn prove_and_proof_check_works() {
// fetch execution proof from 'remote' full node
let remote_backend = trie_backend::tests::test_trie();
let remote_root = remote_backend.storage_root(::std::iter::empty()).0;
let (remote_result, remote_proof, _) = prove(remote_backend,
&mut Default::default(), &DummyCodeExecutor, "test", &[]).unwrap();

// check proof locally
let (local_result, _) = proof_check(remote_root, remote_proof,
&mut Default::default(), &DummyCodeExecutor, "test", &[]).unwrap();

// check that both results are correct
assert_eq!(remote_result, vec![66]);
assert_eq!(remote_result, local_result);
}
}
35 changes: 35 additions & 0 deletions substrate/state-machine/src/proving_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,43 @@ pub fn create_proof_check_backend(root: TrieH256, proof: Vec<Vec<u8>>) -> Result
#[cfg(test)]
mod tests {
use backend::{InMemory};
use trie_backend::tests::test_trie;
use super::*;

fn test_proving() -> ProvingBackend {
ProvingBackend::new(test_trie())
}

#[test]
fn proof_is_empty_until_value_is_read() {
assert!(test_proving().extract_proof().is_empty());
}

#[test]
fn proof_is_non_empty_after_value_is_read() {
let backend = test_proving();
assert_eq!(backend.storage(b"key").unwrap(), Some(b"value".to_vec()));
assert!(!backend.extract_proof().is_empty());
}

#[test]
fn proof_is_invalid_when_does_not_contains_root() {
assert!(create_proof_check_backend(1.into(), vec![]).is_err());
}

#[test]
fn passes_throgh_backend_calls() {
let trie_backend = test_trie();
let proving_backend = test_proving();
assert_eq!(trie_backend.storage(b"key").unwrap(), proving_backend.storage(b"key").unwrap());
assert_eq!(trie_backend.pairs(), proving_backend.pairs());

let (trie_root, mut trie_mdb) = trie_backend.storage_root(::std::iter::empty());
let (proving_root, mut proving_mdb) = proving_backend.storage_root(::std::iter::empty());
assert_eq!(trie_root, proving_root);
assert_eq!(trie_mdb.drain(), proving_mdb.drain());
}

#[test]
fn proof_recorded_and_checked() {
let contents = (0..64).map(|i| (vec![i], Some(vec![i]))).collect::<Vec<_>>();
Expand Down
60 changes: 60 additions & 0 deletions substrate/state-machine/src/trie_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,63 @@ impl TrieBackendStorage {
}
}
}

#[cfg(test)]
pub mod tests {
use super::*;

fn test_db() -> (MemoryDB, TrieH256) {
let mut root = TrieH256::default();
let mut mdb = MemoryDB::default();
{
let mut trie = TrieDBMut::new(&mut mdb, &mut root);
trie.insert(b"key", b"value").unwrap();
trie.insert(b"value1", &[42]).unwrap();
trie.insert(b"value2", &[24]).unwrap();
trie.insert(b":code", b"return 42").unwrap();
}
(mdb, root)
}

pub fn test_trie() -> TrieBackend {
let (mdb, root) = test_db();
TrieBackend::with_memorydb(mdb, root)
}

#[test]
fn read_from_storage_returns_some() {
assert_eq!(test_trie().storage(b"key").unwrap(), Some(b"value".to_vec()));
}

#[test]
fn read_from_storage_returns_none() {
assert_eq!(test_trie().storage(b"non-existing-key").unwrap(), None);
}

#[test]
fn pairs_are_not_empty_on_non_empty_storage() {
assert!(!test_trie().pairs().is_empty());
}

#[test]
fn pairs_are_empty_on_empty_storage() {
assert!(TrieBackend::with_memorydb(MemoryDB::new(), Default::default()).pairs().is_empty());
}

#[test]
fn storage_root_is_non_default() {
assert!(test_trie().storage_root(::std::iter::empty()).0 != [0; 32]);
}

#[test]
fn storage_root_transaction_is_empty() {
assert!(test_trie().storage_root(::std::iter::empty()).1.drain().is_empty());
}

#[test]
fn storage_root_transaction_is_non_empty() {
let (new_root, mut tx) = test_trie().storage_root(vec![(b"new-key".to_vec(), Some(b"new-value".to_vec()))]);
assert!(!tx.drain().is_empty());
assert!(new_root != test_trie().storage_root(::std::iter::empty()).0);
}
}

0 comments on commit 94fb5db

Please sign in to comment.