The merkle trie used by CodeChain and Foundry.
The rust-merkle-trie crate provides merkle trie implementations. CodeChain and Foundry use a merkle trie to hold the states with authentication. We differ from the merkle patricia trie in Ethereum; details can be found here.
Let's use the merkle trie to show an example:
use merkle_trie::{Trie, TrieFactory, TrieMut};
use cdb::MemoryDB;
use primitives::H256;
// initialize
let mut memdb = MemoryDB::new();
let mut root = H256::zero();
let mut trie = TrieDBMut::new(&mut memdb, &mut root);
// insert
trie.insert(&[0x01u8, 0x23], &[0x01u8, 0x23]).unwrap();
assert_eq!(*trie.root(), trie_root(vec![(vec![0x01u8, 0x23], vec![0x01u8, 0x23])]));
// get
assert_eq!(trie.get(&0x01u8, 0x23]).unwrap().unwrap(), vec!0x01u8, 0x23]);
assert_eq!(t.get(&[0x02u8]), Ok(None));
// remove
trie.remove(&0x01u8, 0x23]).unwrap();
assert_eq!(t.get(&0x01u8, 0x23]), Ok(None));
Download foundry code
git clone [email protected]:CodeChain-io/rust-merkle-trie.git
cd rust-merkle-trie
Build in debug mode
cargo build
Developers are strongly encouraged to write unit tests for new code, and to submit new unit tests for old code. Unit tests can be compiled and run with: cargo test --all
. For more details, please refer to Unit Tests.
Official documentation for usage of cargo bench:
https://doc.rust-lang.org/1.15.1/book/benchmark-tests.html
We can run the benchmark test with cargo bench
:
cargo bench