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 lists decoding #17

Merged
merged 4 commits into from
Nov 27, 2018
Merged

fix lists decoding #17

merged 4 commits into from
Nov 27, 2018

Conversation

ayrat555
Copy link
Member

@ayrat555 ayrat555 commented Nov 26, 2018

In the previous PR (#15) tail optimizations were introduced to the decoding of RLP binaries. But ethereum common RLP tests did not cover all cases so a bug was introduced to the library which caused failing tests in mana (https://circleci.com/gh/mana-ethereum/mana/11778?utm_campaign=vcs-integration-link&utm_medium=referral&utm_source=github-build-link)

This PR fixes this bug
https:/mana-ethereum/ex_rlp/compare/ab-fix-decoding?expand=1#diff-8eb9d06c2f6b0c5b724e363538ef07c8R25


{item, new_tail}
end

@spec decode_hex(binary()) :: binary()
defp decode_hex(binary) do
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 think this is used anywhere?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's used on line 13


item_length = :binary.decode_unsigned(be)

<<item::binary-size(item_length), new_tail::binary()>> = data
<<item::binary-size(item_length), new_tail::binary>> = data
Copy link
Member

Choose a reason for hiding this comment

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

would there be an option to extract this into a function head?
if you compile this with bin_opt_info flags you get informed that
NOT OPTIMIZED: sub binary is used or returned
you can read more about this here http://erlang.org/documentation/doc-6.0/doc/efficiency_guide/binaryhandling.html

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think so because length is not constant

Copy link
Member

Choose a reason for hiding this comment

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

what the warning suggest is that it could delay the binary creation if it was passed in a function call.
so instead of returning {item, new_tail} you would return data and pattern match to decompose

@ayrat555 ayrat555 merged commit 73af773 into master Nov 27, 2018
@ayrat555 ayrat555 deleted the ab-fix-decoding branch November 27, 2018 08:39

decode_item(new_tail, [next_list | result])
Copy link
Contributor

Choose a reason for hiding this comment

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

This does remove a tail-call optimization, right?

Copy link
Member Author

@ayrat555 ayrat555 Nov 27, 2018

Choose a reason for hiding this comment

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

no, it's moved to add_decoded_list function

else
decode_long_binary(prefix, tail, 247)
end
def add_new_item(result, new_item) do
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure if this function is more clear than simply inline the list operation. Mostly, it obscures (in the code above) that this function is just a simple operation and nothing more complicated.

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, I agree. But we may have nil in the result variable.
I can change it to

if is_nil(result) do
  new_item
else
  [new_item | result]
end

in every place where this function is used

ayrat555 added a commit to ayrat555/tests that referenced this pull request Nov 27, 2018
We discovered that our implementation of RLP encoding decodes some RLP binaries incorrectly after recent PR (mana-ethereum/ex_rlp#17) but it passes all RLP test cases. 

This PR adds a new test case which is not covered by existing RLP tests
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.

3 participants