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

Add prefix to MissingTrieNode exception #98

Merged
merged 4 commits into from
May 7, 2020

Conversation

carver
Copy link
Contributor

@carver carver commented May 2, 2020

What was wrong?

Some future requirements are to:

  • get the prefix of the key used to navigate down to a trie node that then raises a MissingTrieNode exception
  • traverse the trie directly to a node, using a series of nibbles

How was it fixed?

Requires traversing down the nodes in a different way.

Includes another breaking change: Drop KeyError as a superclass of MissingTrieNode.

(This is an extraction from #95 )

TODO:

Cute Animal Picture

Cute animal picture

@carver carver force-pushed the add-prefix-to-missing-node-exception branch 2 times, most recently from c32637e to b4b8fd3 Compare May 5, 2020 16:34
@carver carver marked this pull request as ready for review May 5, 2020 16:37
@carver carver force-pushed the add-prefix-to-missing-node-exception branch 3 times, most recently from 74605bc to 54e91a8 Compare May 5, 2020 19:09
@carver carver requested a review from gsalgado May 5, 2020 19:10
@carver
Copy link
Contributor Author

carver commented May 5, 2020

This is a step toward improving the trie iterator. After #95 then I should be able to simplify (and improve performance) of NodeIterator.

@carver carver mentioned this pull request May 6, 2020
10 tasks
@@ -18,6 +22,11 @@ def validate_length(value, length):
raise ValidationError("Value is of length {0}. Must be {1}".format(len(value), length))


def validate_is_nibbles(value):
Copy link
Member

Choose a reason for hiding this comment

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

This is where we'd benefit from having:

class Nibble(enum.IntEnum):
    N0 = 0
    N1 = 1
    ...
    NE = 14
    NF = 15

Then we can either have things already typed as a nibble and thus not require any validation or just do Nibble(value) and it will raise a TypeError if the value is out of range. For all other purposes, I believe each of the values for an IntEnum just evaluates as if it was an integer.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, tried it. Mixed feelings. I'll leave it in, because I'm pretty neutral on it. The biggest downside I see is that when you validate, you will get a confusing error if you try to use a byte-string instead of a tuple of nibbles for the prefix. So I ended up adding an extra tuple type check.

Copy link
Member

Choose a reason for hiding this comment

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

I did a quick scan in the code but wasn't able to identify where/what you were referring to. Would it be possible to address this by simply catching and re-raising a less confusing exception?

carver added 3 commits May 6, 2020 16:53
Requires traversing down the nodes in a different way.

Includes another breaking change: Drop KeyError as a superclass of
MissingTrieNode.
Later, it will be possible for callers to save the returned node,
and supply it on the next traverse() call. This avoids re-traversing the
trie from the root to the saved node.
@carver carver force-pushed the add-prefix-to-missing-node-exception branch 3 times, most recently from 6ddbc87 to ff329b4 Compare May 7, 2020 00:32
@carver carver force-pushed the add-prefix-to-missing-node-exception branch from ff329b4 to 1ca1c79 Compare May 7, 2020 16:50
Comment on lines +30 to +35
class Nibbles(tuple):
def __new__(cls, nibbles):
if not is_list_like(nibbles):
raise TypeError(f"Must pass in a tuple of nibbles, but got {nibbles!r}")
else:
return tuple.__new__(cls, (Nibble(maybe_nibble) for maybe_nibble in nibbles))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hm, I don't think there's a good way to catch an exception from Nibble and clarify it, because the same exception would be raised by the following snippets:

for n in b'A':
  Nibble(n)

for n in (0x41, ):
  Nibble(n)

So I just added a tiny wrapper for a series of nibbles, plus a test.

Copy link
Member

@pipermerriam pipermerriam May 7, 2020

Choose a reason for hiding this comment

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

I was just thinking:

try:
    ...  # code that does `Nibble(value)`
except TypeError as err:
    raise TypeError("Some message that is more informative") from err

👍 with whatever you think is best. This might just be one of those ideas that I have that isn't actually useful in practice 🤷

@carver carver merged commit 3c56a75 into ethereum:master May 7, 2020
@carver carver deleted the add-prefix-to-missing-node-exception branch May 7, 2020 22:51
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 this pull request may close these issues.

2 participants