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

fix: missing KeyError and incorrect existence checking when changing default node #150

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions tests/test_smt.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from hypothesis import (
assume,
given,
strategies as st,
)
Expand All @@ -11,12 +12,25 @@
)


@st.composite
def binary_tuples(draw):
size = draw(st.integers(min_value=1, max_value=32))
v = draw(st.binary(min_size=size, max_size=size))
default = draw(st.binary(min_size=size, max_size=size))

# Ensure v and default are not equal
assume(v != default)

return (v, default)


@given(
k=st.binary(min_size=1, max_size=32),
v=st.binary(min_size=1, max_size=32),
values=binary_tuples(),
)
def test_simple_kv(k, v):
smt = SparseMerkleTree(key_size=len(k))
def test_simple_kv(k, values):
v, default = values
smt = SparseMerkleTree(key_size=len(k), default=default)
empty_root = smt.root_hash

# Nothing has been added yet
Expand Down
4 changes: 2 additions & 2 deletions trie/smt.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def get(self, key: bytes) -> bytes:
value, _ = self._get(key)

# Ensure that it isn't blank!
if value == BLANK_NODE:
if value == self._default:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Realizing this is not probably, wanted.

If this default is anything other than a blank
node, then all keys "exist" in the database, which mimics the behavior of
Ethereum on-chain datastores.

(from the docs)

will change

raise KeyError("Key does not exist")

return value
Expand All @@ -261,7 +261,7 @@ def branch(self, key: bytes) -> Tuple[Hash32]:
value, branch = self._get(key)

# Ensure that it isn't blank!
if value == BLANK_NODE:
if value == self._default:
raise KeyError("Key does not exist")

return branch
Expand Down