citahub / cita_trie

Rust implementation of the Modified Patricia Tree (aka Trie).
Apache License 2.0
71 stars 28 forks source link

fix(codec): decode pair #15

Closed yejiayu closed 5 years ago

yejiayu commented 5 years ago

fix #14

"pair" is an abstraction of leaf node and ext node. The value of the ext node is different from the value of the leaf node. It is not correct to decode the value into data.

mohanson commented 5 years ago

Still a problem is there. It seems that the error will still be triggered when the data is long(But I am not very sure, the following code can reproduce what I said):

[dependencies]
cita_trie = { path = "/src/cita-trie" }
hex = "*"
ethereum-types = "*"
use cita_trie::trie::Trie;

fn main() {
    let k0: ethereum_types::H256 = 0.into();
    let k1: ethereum_types::H256 = 1.into();
    let v: ethereum_types::H256 = 0x1234.into();

    let root1 = {
        let mut db = cita_trie::db::MemoryDB::new();
        let mut trie = cita_trie::trie::PatriciaTrie::new(&mut db, cita_trie::codec::RLPNodeCodec::default());
        trie.insert(&k0, &v).unwrap();
        trie.root().unwrap()
    };

    let root2 = {
        let mut db = cita_trie::db::MemoryDB::new();
        let mut trie = cita_trie::trie::PatriciaTrie::new(&mut db, cita_trie::codec::RLPNodeCodec::default());
        trie.insert(&k0, &v).unwrap();
        trie.insert(&k1, &v).unwrap();
        trie.root().unwrap();
        trie.remove(&k1).unwrap();
        trie.root().unwrap()
    };

    let root3 = {
        let mut db = cita_trie::db::MemoryDB::new();
        let mut t1 = cita_trie::trie::PatriciaTrie::new(&mut db, cita_trie::codec::RLPNodeCodec::default());
        t1.insert(&k0, &v).unwrap();
        t1.insert(&k1, &v).unwrap();
        t1.root().unwrap();
        let root = t1.root().unwrap();
        let mut t2 = cita_trie::trie::PatriciaTrie::from(&mut db, cita_trie::codec::RLPNodeCodec::default(), &root).unwrap();
        t2.remove(&k1).unwrap();
        t2.root().unwrap()
    };

    println!("{:?}", hex::encode(root1)); // "ca8f89e4444c7453e96568511298af8049553232cfdb9255be8799d68c28b297"
    println!("{:?}", hex::encode(root2)); // "ca8f89e4444c7453e96568511298af8049553232cfdb9255be8799d68c28b297"
    println!("{:?}", hex::encode(root3)); // "7e24703c900d74c90e8ae2c076c667d87ebdec162eb6e81e1574438a9b6d8b89"
}
yejiayu commented 5 years ago

Still a problem is there. It seems that the error will still be triggered when the data is long(But I am not very sure, the following code can reproduce what I said):

This bug is caused by a hash node that is not recovering from the database when degradation due to the removal of nodes.