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

feat(abi): packed encoding #2104

Merged
merged 5 commits into from Feb 5, 2023
Merged

Conversation

DaniPopes
Copy link
Collaborator

@DaniPopes DaniPopes commented Feb 1, 2023

Motivation

Closes #1862

Solution

PR Checklist

  • Added Tests
  • Added Documentation
  • Updated the changelog
  • Breaking changes

@DaniPopes DaniPopes marked this pull request as draft February 1, 2023 20:07
Comment on lines 114 to 119
Int(n) | Uint(n) => {
// TODO: Different (u)int* padding
let mut buf = [0; 32];
n.to_big_endian(&mut buf);
out.extend_from_slice(&buf);
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

By using the same Token type as ethabi, we cannot differentiate between the different integer type sizes as Token::(U)Int stores only the U256 number and not its encoded size (since in normal abi-encoding this wouldn't have any effect).
Should this function use a different Token-like enum, or is there another way around this?

Copy link
Owner

@gakonst gakonst Feb 2, 2023

Choose a reason for hiding this comment

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

You can find the smallest value for which n >> BITS is 0, and mark that as the size?

e.g.
255 >> 8 == 0 --> put it in uint8
whereas 256 >> 8 == 1, putting it in uint16
etc.

maybe we have that logic somewhere in reth's compact encoding

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Right, but how does one then cast the number as something higher than its minimum bits? Like uint256(0) would need to pad all 32 bytes? We can have this as a minor limitation for the time being I guess

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Implemented this using U256::bits. Any 0 padding needs to be manually added with something like this:

let n = U256::from(...);
let mut buf = [0; 32];
n.to_big_endian(&mut buf);
// uint32 = 4 bytes -> 32 - 4 .. 32
let uint32 = Token::Bytes(buf[28..32]);

@DaniPopes DaniPopes marked this pull request as ready for review February 4, 2023 13:03
@gakonst gakonst merged commit cabb15e into gakonst:master Feb 5, 2023
@gakonst
Copy link
Owner

gakonst commented Feb 5, 2023

LGTM - ty!!

@DaniPopes DaniPopes deleted the feat/abi-encode-packed branch February 5, 2023 10:42
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.

Add abi::encode_packed
2 participants