paritytech / trie

Base-16 Modified Patricia Merkle Tree (aka Trie)
Apache License 2.0
251 stars 67 forks source link

Not able to retrieve value for given key when new key pair inserted #197

Closed zktony closed 1 year ago

zktony commented 1 year ago
#[test]
fn test_two_assets_memory_db() {
    let mut memdb = MemoryDB::<BlakeTwo256>::new(&[0u8]);
    let mut root = H256::zero();

    let mut state = TrieDBMutBuilderV1::new(&mut memdb, &mut root).build();

    let key1 = [1u8;3];
    let data1 = [1u8;2];
    state.insert(key1.as_ref(), &data1).unwrap();
        assert_eq!(state.get(key1.as_ref()).unwrap().unwrap(),data1); //PASSING
    let key2 = [2u8;3];
    let data2 = [2u8;2];
    state.insert(key2.as_ref(), &data2).unwrap();
    assert_eq!(state.get(key1.as_ref()).unwrap().unwrap(),data1); //ERROR:- 'tests::test_two_assets_memory_db' panicked at 'called `Option::unwrap()` on a `None` value.
    state.commit();
}

The test case test_two_assets_memory_db fails at the line assert_eq!(state.get(key1.as_ref()).unwrap().unwrap(),data1);when trying to fetch and assert the data associated with key1 after the second insertion operation. It appears that after the insertion of data associated with key2, the data tied to key1 is either removed or not accessible, leading to the Option::unwrap() call on a None value, which is causing the test to panic. The state database should ideally support multiple insertions and retrievals, however, in this instance, it fails to retrieve the value associated with key1 after a new key-value pair (key2, data2) has been inserted, could you please help me with this?

cheme commented 1 year ago

I suspect TrieDBMut read access got some issue in this case. I will check that this afternoon, maybe just deprecate the get api from triedbmut. Note that substrate never does read access from TrieDBMut and only use TrieDB (TrieDBBuilder) to access data (modification are done in batch on commit only).