-
Notifications
You must be signed in to change notification settings - Fork 100
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
Refactor left_pad_zeros_up_to_size
to a parameter in the FixedSizeBytes
constructor
#1066
Conversation
That undo the very same idea we wanted to ibtroduce is to restrict assigment of fixed bytes of different sizes The point was to make it explicit, so we think about bytes size when writing the tests |
But isn't the parameter explicit enough? if no parameter is passed and the byte length is shorter, an error will be raised, so the tester has to explicitly:
I guess my point is that both are explicit ways to ensure that the tester knows what they are doing, just that the second one is a bit more convenient. |
@@ -308,7 +308,7 @@ def tx(pre: Alloc, caller_address: Address, callee_address: Address) -> Transact | |||
return Transaction( | |||
sender=pre.fund_eoa(), | |||
to=caller_address, | |||
data=left_pad_zeros_up_to_size(callee_address, 32), | |||
data=Hash(callee_address, left_padding=True), |
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.
again the number 32 is implicit
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.
Yes, but I think at this point the tester has already selected Hash
so they know they needed a 32-byte constructor.
Another entirely different discussion could be that the hashes become longer in the future, but in that case I think we need a completely new class.
I see, so to conclude:
it is a bit confusing if we provide no padding flag, we can write code that will throw an exception without realizing it. I agree that it is handy compared to importing a converter function. Shall the unit tests be rewritten back with the flag? because I put explicit long strings there |
Ah, can we overload constructor so if it takes only str, it will throw if string byte representation has incorrect size Hash("0x01", ZEROPAD::LEFT) = Hash(0x01) will make Hash ah I see python has no overload constuctors. |
I think they should be fine with the longer strings. We might need to add a new unit test that uses the padding flags if we decide to incorporate them. |
Yes exactly, the magic happens here (this is in the def __new__(
cls,
input_bytes: FixedSizeBytesConvertible | T,
*,
left_padding: bool = False,
right_padding: bool = False,
): The asterisk on the fourth line means that no other positional arguments to the function are accepted, so if you do The arguments after the asterisk are keyword arguments, which means they have to be explicitly named when the function called, so Also since they have a default of I thought about doing perhaps |
@@ -373,7 +373,7 @@ def test_self_destructing_account( | |||
sender=sender, | |||
gas_limit=100000, | |||
to=self_destruct_contract_address, | |||
data=left_pad_zeros_up_to_size(recipient, 32), | |||
data=Hash(recipient, left_padding=True), |
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.
lets name Hash Hash32 because in tests we have Hash8 (block nonce) and Hash20 (address)
Changes
left_pad_zeros_up_to_size
to be instead an optional parameter in all classes that inheritFixedSizeBytes
.So for example
Hash(left_pad_zeros_up_to_size("0x00", 32))
is equivalent toHash("0x00", left_padding=True)
.If neither
left_padding
orright_padding
are specified, and the input is shorter than the expected size, an exception is raised.