foundry-rs / foundry

Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.
https://getfoundry.sh
Apache License 2.0
8.25k stars 1.73k forks source link

JSON parsing does not work for types of `bytes6` and `bytes6[]` #3490

Closed Sabnock01 closed 2 years ago

Sabnock01 commented 2 years ago

Component

Forge

Have you ensured that all of these are up to date?

What version of Foundry are you on?

forge 0.2.0 (28312e7 2022-10-13T00:10:05.477412592Z)

What command(s) is the bug in?

No response

Operating System

Linux

Describe the bug

JSON parsing does not appear to work for values of types bytes6 and bytes6[].

When trying to parse either of these types with forge test it results in an EVM Error: Revert. I noticed there's no tests for bytes<x> in the tests in Json.t.sol and that there's no helper functions for this type in the StdJson library in forge-std.

Is there some reason that it can't be used for this type?

gakonst commented 2 years ago

We haven't investigated and I haven't been using the feature. @odyslam @mds1 would know if there's a reason, otherwise PR welcome!

Sabnock01 commented 2 years ago

Did a bit more digging and it looks like while they are correctly encoded for the types bytes32 and bytes32[], they aren't for the others.

The encoding for a bytes6[] containing 3 elements 0x111111111111, 0x222222222222, and 0x333333333333 would be: 0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003111111111111000000000000000000000000000000000000000000000000000022222222222200000000000000000000000000000000000000000000000000003333333333330000000000000000000000000000000000000000000000000000

Yet parseJson returns instead: 0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000000611111111111100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006222222222222000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000063333333333330000000000000000000000000000000000000000000000000000

odyslam commented 2 years ago

Hey @Sabnock01,

Sorry for not responding over Twitter. Devcon has been overly chaotic.

The cheatcode doesn't recognize bytes. It will read everything as bytes, except if it's len=32, which is returned as a bytes32. The reason is that bytes32 is a very common structure while most bytes are not.

It's not possible to know whether something should be a bytes[], where the len of the bytes happens to be 6 or bytes6[]. The only reason we make the explicit decision is with bytes32, for the reason I mentioned above.

I would advise simply parsing it as bytes[] and then making a helper function (as I have created in forge-std for other things) that goes over the array and converts it to the type you want.

I think it's better to keep the cheatcode as-is

Sabnock01 commented 2 years ago

Ah, I hadn't yet thought of that but it's super easy. Will close the issue now. Thank you for clearing that up!