Skip to content
This repository has been archived by the owner on Jul 1, 2021. It is now read-only.

Randao validation #214

Closed
wants to merge 5 commits into from
Closed

Conversation

jannikluhn
Copy link
Contributor

This is a copy of ethereum/py-evm#1686 by @prateekreddy that I only rebased to Trinity.

Note that merging it right now is not a good idea as there are still unaddressed comments and it soon needs to be updated anyways once ethereum/consensus-specs#483 is merged.

What was wrong?

Implement validate_randao as per Eth 2.0 spec https://github.com/ethereum/eth2.0-specs/blob/master/specs/core/0_beacon-chain.md#randao

Cute Animal Picture

put a cute animal picture link inside the parentheses

@hwwhww
Copy link
Contributor

hwwhww commented Jan 22, 2019

ethereum/consensus-specs#483 is merged. 🎉

)


def int_to_bytes32(value: Union[int, bool]) -> Hash32:
Copy link
Member

Choose a reason for hiding this comment

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

It appears that this should be value: int since the next line explicitely disallows bools.


def get_highest_bit_index(value: int) -> int:
value >>= 1
for bit_length in itertools.count():
Copy link
Member

Choose a reason for hiding this comment

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

Can we set a reasonable upper bound on this loop?

Bound the given ``value`` between ``inclusive_lower_bound`` and
``inclusive_upper_bound``.
"""
if value <= inclusive_lower_bound:
Copy link
Member

Choose a reason for hiding this comment

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

I don't know how performance critical this is, but maybe it's better to optimize the common case? Assuming the common case is that the value is within range then maybe start this with:

if inclusive_lower_bound <= value <= inclusive_upper_bound:
     return value
elif value <= ...:
    ...
elif value >= ...:
    ...
else:
    raise Exception("Unreachable")


x = value
y = (x + 1) // 2
while y < x:
Copy link
Member

Choose a reason for hiding this comment

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

I think we worked up a faster version of this using a combination of decimal.Decimal and logarithms.

)
c = b''
for i in range(32):
c += bytes([a[i] ^ b[i]])
Copy link
Member

Choose a reason for hiding this comment

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

Same, we had a better version of this in some other PR.

if repeats == 0:
return Hash32(data)
else:
return repeat_hash_eth2(bytes(hash_eth2(data)), repeats - 1)
Copy link
Member

Choose a reason for hiding this comment

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

And a non-recursive version of this using cytoolz.pipe and itertools.repeat(hash_eth, iterations)

@jannikluhn
Copy link
Contributor Author

Replaced by #252

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

Successfully merging this pull request may close these issues.

4 participants