Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
zktony opened this issue Aug 2, 2023 · 1 comment · Fixed by #198
Closed

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

zktony opened this issue Aug 2, 2023 · 1 comment · Fixed by #198
Assignees

Comments

@zktony
Copy link

zktony commented Aug 2, 2023

#[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 cheme self-assigned this Aug 2, 2023
@cheme
Copy link
Contributor

cheme commented Aug 2, 2023

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants