-
Notifications
You must be signed in to change notification settings - Fork 651
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 get_merkle_root
#1689
Add get_merkle_root
#1689
Conversation
eth/beacon/_utils/hash.py
Outdated
from eth_typing import Hash32 | ||
from eth_hash.auto import keccak | ||
|
||
|
||
def hash_eth2(data: bytes) -> Hash32: | ||
def hash_eth2(data: Hashable) -> Hash32: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I might miss some historical discussion. But is Hashable
what we want here?
According to typing documentation, Hashable
is an alias to collections.abc.Hashable
, which checks if an object has the abstract method __hash__
. That is in the sense of Python hash
rather then if a Serializable
object could be hash_eth2-ed or tree-hashed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ohh I changed it to Hashable
just for resolving the lint error with merkle
utils.
eth_hash.auto.keccak
takes Union[bytes, bytearray]
as input. Could we just replace Hashable
with Union[bytes, bytearray]
? Ping @jannikluhn to confirm it. 🙏
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree to Hashable
--> Union[bytes, bytearray]
(even though bytearray
doesn't seem to be used in py-evm much, so just bytes
might be fine too).
eth/_utils/merkle.py
Outdated
n_layers, | ||
iterate(_hash_layer, leaves), | ||
) | ||
)[::-1] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIUC this is reversing the iterable. I think reversed(...)
reads a lot more naturally.
43cff80
to
67b4973
Compare
In what context do we expect to use |
@djrtwo this module was written for proof of custody a long time ago.
Not sure if we will need |
Right. So do we want to leave both in for now? That said, I'm not too opposed to leaving in the old function.. |
eth/_utils/merkle.py
Outdated
|
||
leaves = tuple(keccak(item) for item in items) | ||
tree = cast(MerkleTree, tuple(take(n_layers, iterate(_hash_layer, leaves)))[::-1]) | ||
tree = cast( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't have to use cast
here, MerkleTree(tuple(...))
works as well (and mypy can check that the argument has the right type).
eth/_utils/merkle.py
Outdated
MerkleTree, | ||
tuple( | ||
reversed( | ||
tuple( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This expression is getting quite long. Can we maybe split it up, e.g.:
reversed_tree = tuple(take(n_layers, iterate(_hash_layer, leaves)))
return MerkleTree(tuple(reversed(reversed_tree)))
67b4973
to
e5c1205
Compare
@djrtwo @jannikluhn |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
What was wrong?
Replace #1674
Fix #1673
It's #1674 without
get_updated_merkle_accumulator
How was it fixed?
eth._utils.merkle
APIsget_merkle_root
Cute Animal Picture