Skip to content

Consolidate encoding and decoding methods, rename #161

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

Merged
merged 8 commits into from
Apr 22, 2022

Conversation

fselmo
Copy link
Contributor

@fselmo fselmo commented Mar 22, 2022

What was wrong?

  • encode_single() / decode_single() and encode_abi() / decode_abi() have very similar functions. It has been suggested that these APIs be consolidated into one and the changes in this PR reflect keeping the encode_abi() and decode_abi() functionality and renaming the methods to encode() and decode(), respectively. encode_abi_packed() has also been changed to encode_packed() - as is Solidity's.

  • I also changed, in a separate commit, the order in which the arguments are passed to these methods to be: values first; types second. This better mimics Solidity's abi methods for consistency and since these changes are breaking and significantly change the methods, this felt like a good time to make such a change. I'm also not tied to this idea and welcome any input if anyone feels strongly about not making this change.

Solidity

bytes32 a = 0x6100000000000000000000000000000000000000000000000000000000000000;
bytes32 b = 0x6200000000000000000000000000000000000000000000000000000000000000;
// abi.encode()
bytes public encoded_data = abi.encode(a, b);

function returnEncodedData() public view returns (bytes32[2] memory) {
    // can locally unpack encoded data with abi.decode() 
    (bytes32 decoded_a, bytes32 decoded_b) = abi.decode(encoded_data, (bytes32,bytes32));
    
    return [decoded_a, decoded_b];
}

Python

>>> from eth_abi import encode, decode

>>> # abi.encode()
>>> encoded_data = encode((b'a', b'b'), ('bytes32', 'bytes32'))
>>> encoded_data
b'a\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00b\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

>>> # unpack encoded data in a similar way to Solidity example above
>>> (decoded_a, decoded_b) = decode(encoded_data, ('bytes32', 'bytes32'))
>>> decoded_a
b'a\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
>>> decoded_b
b'b\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

closes #84

How was it fixed?

  • Removed encode_single() and decode_single() methods.
  • Renamed encode_abi() to encode() and decode_abi() to decode().
  • Fixed / refactored tests to accommodate for the above changes, including changing the encode_single() / decode_single() tests to use encode() and decode() with a single type passed in.
  • Some minor cleanup along the way where there were unused imports, etc.

To-Do

  • Clean up commit history
  • Consider reversing the arguments to the methods to even closer mimic the Solidity abi library methods (i.e. abi.decode(encoded_data, ('bytes32', 'bytes32'))).
  • Add entry to the release notes
  • Add cute animal picture

Cute Animal Picture

20220322_164125

Sorry, something went wrong.

@fselmo fselmo force-pushed the consolidate-encode-and-decode-methods branch 3 times, most recently from 043b472 to ed2e020 Compare March 22, 2022 18:29
@fselmo fselmo changed the title [WIP] Consolidate encode and decode methods [WIP] Consolidate encoding and decoding methods, rename Mar 22, 2022
fselmo added a commit to fselmo/eth-abi that referenced this pull request Mar 22, 2022
- Also add a breaking-change newsfragment for PR ethereum#161
@fselmo fselmo force-pushed the consolidate-encode-and-decode-methods branch 2 times, most recently from 4ec118b to 98dddc6 Compare March 22, 2022 21:39
@fselmo fselmo changed the title [WIP] Consolidate encoding and decoding methods, rename Consolidate encoding and decoding methods, rename Mar 22, 2022
@fselmo fselmo marked this pull request as ready for review March 22, 2022 21:41
fselmo added a commit to fselmo/eth-abi that referenced this pull request Mar 22, 2022
- Also add a breaking-change newsfragment for PR ethereum#161
@fselmo fselmo force-pushed the consolidate-encode-and-decode-methods branch from 98dddc6 to 419fbd0 Compare March 22, 2022 22:15
@fselmo fselmo requested review from wolovim, pacrob and kclowes March 22, 2022 22:18
Copy link
Contributor

@kclowes kclowes left a comment

Choose a reason for hiding this comment

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

Code looks really solid!

  • I could take or leave the argument order change. It looks like we will need to update/make breaking changes in web3.py, eth-tester, and eth-account, and maybe others that I missed. Github isn't correctly picking up libraries that depend on eth-abi for some reason.
  • I think the API would be a little bit nicer if we could pass arguments either with or without a list, for example, allowing: encode(234, 'uint'), but feel free to take or leave that feedback :)

We should also talk about whether or not we should do a beta release for these changes, but we can talk about that in our sync on Monday! 💥

@kclowes
Copy link
Contributor

kclowes commented Apr 1, 2022

Oh! I forgot to mention that we'll need to cut a v3 stable branch and deprecate the methods in that branch. So let's wait to merge this until I have a v3 stable branch out. I'll circle back and post a comment here once that's up!

@charles-cooper
Copy link
Contributor

FWIW I'd say keeping the existing argument order makes some sense. I think the existing way works well if you think in terms of currying - the first argument is "more slowly" changing, the second argument "more quickly". Plus, and maybe this is more important, downstream users don't need to change. Trying to figure out what abi_encode("string", "(string)") encodes to will require less mental gymnastics, for instance.

@fselmo
Copy link
Contributor Author

fselmo commented Apr 7, 2022

FWIW I'd say keeping the existing argument order makes some sense. I think the existing way works well if you think in terms of currying - the first argument is "more slowly" changing, the second argument "more quickly". Plus, and maybe this is more important, downstream users don't need to change. Trying to figure out what abi_encode("string", "(string)") encodes to will require less mental gymnastics, for instance.

@charles-cooper thanks for the feedback. I could also take or leave it but figured we could consider it given we're already introducing breaking changes. It did feel like it made more sense to me reversed / the way it is in Solidity: "decode this data as these types". For better or worse I do think leaving the current argument order would cause less headaches and maybe we should just leave it alone.

fselmo added a commit to fselmo/eth-abi that referenced this pull request Apr 7, 2022
…better match Solidity"

This reverts commit a2dbdeb. See conversation in PR ethereum#161 for reference.
fselmo added a commit to fselmo/eth-abi that referenced this pull request Apr 7, 2022
…better match Solidity"

This reverts commit a2dbdeb. See conversation in PR ethereum#161 for reference.
@fselmo fselmo force-pushed the consolidate-encode-and-decode-methods branch from 5fc87f9 to 7ea3ca0 Compare April 7, 2022 18:41
fselmo added a commit to fselmo/eth-abi that referenced this pull request Apr 7, 2022
…better match Solidity"

This reverts commit a2dbdeb. See conversation in PR ethereum#161 for reference.
@fselmo fselmo force-pushed the consolidate-encode-and-decode-methods branch from 7ea3ca0 to 58dd3a4 Compare April 7, 2022 18:49
Copy link
Contributor

@kclowes kclowes left a comment

Choose a reason for hiding this comment

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

LGTM! The only thing that sticks out to me while looking over this again is: what happens if type_str is passed without an array? Is the error clear enough or should we add our own? I was thinking about it in the context of people migrating from v2/v3 🤷

fselmo added 6 commits April 7, 2022 16:33
…()``

- Update docs and rename ``encode_abi()`` to ``encode()`` and ``decode_abi()`` to ``decode()``
- Work TODOs from previous commit: turn ``encode_single()`` and ``decode_single()`` tests back on and refactor to use ``encode()`` / ``decode()``, respectively
- Also add a breaking-change newsfragment for PR ethereum#161
…better match Solidity"

This reverts commit a2dbdeb. See conversation in PR ethereum#161 for reference.
@fselmo fselmo force-pushed the consolidate-encode-and-decode-methods branch from 58dd3a4 to a282457 Compare April 7, 2022 22:36
@fselmo fselmo force-pushed the consolidate-encode-and-decode-methods branch from 48cfde4 to ecd5372 Compare April 12, 2022 12:51
@fselmo
Copy link
Contributor Author

fselmo commented Apr 12, 2022

LGTM! The only thing that sticks out to me while looking over this again is: what happens if type_str is passed without an array? Is the error clear enough or should we add our own? I was thinking about it in the context of people migrating from v2/v3 🤷

Updated with some better messaging and tests in this commit. Good thought. Could you do another pass through and let me know?

Copy link
Contributor

@kclowes kclowes left a comment

Choose a reason for hiding this comment

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

LGTM! 🚢

@fselmo fselmo merged commit 4e58c17 into ethereum:master Apr 22, 2022
@pacrob pacrob mentioned this pull request Sep 6, 2022
2 tasks
Uxio0 added a commit to safe-global/safe-eth-py that referenced this pull request Apr 12, 2023
- Update eth-abi (methods changed name and some were deleted: ethereum/eth-abi#161)
- Rename parity related Web3 methods/types
- Rename some Web3 methods from camelcase to snakecase
Uxio0 added a commit to safe-global/safe-eth-py that referenced this pull request Apr 19, 2023
- Update eth-abi (methods changed name and some were deleted: ethereum/eth-abi#161)
- Rename parity related Web3 methods/types
- Rename some Web3 methods from camelcase to snakecase
Uxio0 added a commit to safe-global/safe-eth-py that referenced this pull request Apr 25, 2023
- Update eth-abi (methods changed name and some were deleted: ethereum/eth-abi#161)
- Rename parity related Web3 methods/types
- Rename some Web3 methods from camelcase to snakecase
mochaaP added a commit to mcha-forks/qiling that referenced this pull request Jan 7, 2024
mochaaP added a commit to mcha-forks/qiling that referenced this pull request Jan 7, 2024
mochaaP added a commit to mcha-forks/qiling that referenced this pull request Jan 7, 2024
mochaaP added a commit to mcha-forks/qiling that referenced this pull request Jan 7, 2024
mochaaP added a commit to mcha-forks/qiling that referenced this pull request Jan 8, 2024
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.

Remove encode_abi and decode_abi and remove _single suffix from other coding functions
3 participants