Comments (9)
I see the problem is on my side. I should not be treating the values as a tuple in encode
. v4.0.0
yields the correct result
from eth-abi.
@Uxio0 what version of eth-abi
are you using? There was an encoding bug that was also fixed for dynamic encoding. The most recent version of eth-abi
lines up with Solidity encoding of empty bytes. Neither of what you posted is what Solidity returns. My suggestion is to try to update your library to use the latest eth-abi
. If you need some time to transition to the latest version then, yeah, we need to figure out how / why encode
is differing from encode_single
within the same version.
I'm also assuming that you used the different methods since both of your code snippets are using (I already confirmed this discrepancy in version encode
and neither use encode_single
(though the second imports it). I assumed that was just a typo.3.0.1
below)
Solidity encoding used for reference:
contract Contract {
function encodeEmptyBytes() public pure returns (bytes memory) {
bytes memory _b;
return abi.encode(_b);
}
}
which returns (consistent with the latest version of eth-abi
):
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 \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\x00'
from eth-abi.
Hmm I do see the discrepancy in 3.0.1
which is definitely a bug as they should yield the same results. I think this is a result of the bug that was in the encoding. We can and should definitely put up a patch version for 3.0.1 with this bugfix but, again, neither of these are correct compared to what Solidity returns. I suggest upgrading to the latest version of eth-abi
as soon as you can.
Thanks for submitting this.
from eth-abi.
On second thought, and looking at this again, I don't see any easy way to fix this since
-
The way
encode
(previouslyencode_abi
) was written, it is inherently doing something different thanencode_single
and results in a different result. -
Modifying
encode()
in this version to returnencode_single()
if the length is1
breaksencode()
's behavior (breaking change). This is because if you usedencode_abi()
in the past, with a single item, it would produce a different result. This is the most unfortunate part of this change. -
They are really both wrong and this bug is fixed in the latest
eth-abi
version.
The only answer I see here really is unfortunately just skipping this version altogether and updating the docs to reflect this and encourage users to only use the latest version. Thankfully we've moved away from decoupling encoding a single item from many. This just leads to more surface + room for errors like these.
Is there any other option I missed that would be better for you as you are transitioning to the new eth-abi
version? @Uxio0
from eth-abi.
Hi @fselmo and thanks for the detailed answer:
I was using v2.2.0
and v3.0.1
. Indeed:
len(eth_abi.encode(['bytes'], [b'']))
96
len(eth_abi.encode_single('bytes', b''))
64
but with 4.0.0
len(eth_abi.encode(['bytes'], [b'']))
64
You are right, I wanted to make sure everything was working before migrating to v4.0.0
and indeed it was fixed in the last version. I will update to v4.0.0
and then do the refactor in the same step.
Thank you very much
from eth-abi.
@fselmo I am still observing this behavior with v4.0.0
, and it's causing problems upgrading
Example:
with v2.2.0
encode_single(
'(address,uint256,uint256,uint256,bytes)',
["0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", 1000, 1, 0, b'']
)
with v4.0.0
encode(
['(address,uint256,uint256,uint256,bytes)'],
[("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", 1000, 1, 0, b'')]
)
These should return the same result, but they do not.
from eth-abi.
@fselmo I am still observing this behavior with
v4.0.0
, and it's causing problems upgradingExample:
with
v2.2.0
encode_single( '(address,uint256,uint256,uint256,bytes)', ["0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", 1000, 1, 0, b''] )
with
v4.0.0
encode( ['(address,uint256,uint256,uint256,bytes)'], [("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", 1000, 1, 0, b'')] )
These should return the same result, but they do not.
2.2.0
had a bug. Do not compare anything to those results. Instead compare the latest encode
to Solidity.
from eth-abi.
@fselmo Hmm, using v2.2.0
, v4.0.0
and solidity I'm getting 3 different answers
Using the above examples, I get
0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc200000000000000000000000000000000000000000000000000000000000003e80000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000
for v4.0.0
0x000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc200000000000000000000000000000000000000000000000000000000000003e80000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
for v2.2.0
and
0x000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc200000000000000000000000000000000000000000000000000000000000003e80000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000
with solidity abi.encode(address(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2), uint256(1000), uint256(1), uint256(0), bytes(""));
from eth-abi.
closing as resolved / completed / stale
from eth-abi.
Related Issues (20)
- Unable to decode Arrays of Structs HOT 4
- ImportError: cannot import name 'encode' from 'eth_abi' HOT 2
- Is there a way to match calldata/memory encoding conventions? HOT 1
- Decoding data of type byte[] array results in an an empty element at the end HOT 3
- Byte value of length 32b results in decoding error using 'decode' HOT 2
- Decoding an array type returns a tuple instead of list HOT 5
- `ImportError: cannot import name 'getargspec' from 'inspect'` in Python >= 3.11 due to outdated `parsimonious` dependency version
- Wrong encoding output when using uint256[] HOT 1
- Decoding arbitrary length bytes fails due to expected padding HOT 2
- encoding tuple with nested tuple containing dynamic types HOT 3
- [Project Management] When will eth-abi>=4.0.0 be released? HOT 2
- Dependency errors for newer versions of uth-utils>=2.0.0 and eth-abi>=3.0.0
- typing/linting tools review
- Add utility method for getting an acceptable byte range from a list of ABI types
- OverflowError: Python int too large to convert to C ssize_t HOT 5
- decode failing on input that is not divided by 32 HOT 1
- Replace `functools.lru_cache`
- OverflowError when decode an event data with empty fields HOT 4
- Decode input data with bad padding HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from eth-abi.