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

[bug] Wrong root hash when keys are removed #57

Open
ufoscout opened this issue Apr 4, 2024 · 1 comment
Open

[bug] Wrong root hash when keys are removed #57

ufoscout opened this issue Apr 4, 2024 · 1 comment

Comments

@ufoscout
Copy link

ufoscout commented Apr 4, 2024

The root hash calculation returns a wrong value when some keys are removed. This does not happen for all keys.
Here is a test that reproduces the bug:

    #[test]
    fn test_insert_and_revert() {
        // Arrange
        let db = Arc::new(MemoryDB::new(true));
        let mut trie = PatriciaTrie::new(db.clone(), Arc::new(HasherKeccak::new()));
        let root_0 = trie.commit().unwrap();

        // Insert "do"
        let root_1 = {
            trie.insert(b"do".to_vec(), b"verb".to_vec()).unwrap();
            trie.root().unwrap()
        };

        // Insert "doge"
        let root_2 = {
            trie.insert(b"doge".to_vec(), b"coin".to_vec()).unwrap();
            trie.root().unwrap()
        };

        // Insert "dog"
        {
            trie.insert(b"dog".to_vec(), b"puppy".to_vec()).unwrap();
            trie.root().unwrap()
        };

        // Remove "dog"
        {
            trie.remove(b"dog").unwrap();
            // It fails here, the root hash does not match the expected one
            assert_eq!(root_2, trie.root().unwrap());
        }

        // Remove "doge"
        {
            trie.remove(b"doge").unwrap();
            assert_eq!(root_1, trie.root().unwrap());
        }

        // Remove "do"
        {
            trie.remove(b"do").unwrap();
            // Now the trie is empty but the hash is not the one of an empty trie
            assert_eq!(root_0, trie.root().unwrap());
        }

    }
@ufoscout
Copy link
Author

ufoscout commented Apr 5, 2024

The bug was introduced by #43
The test succeeds the commit before it

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

No branches or pull requests

1 participant