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

Cannot keys or values from json object using stdJson.readStringArray #4844

Open
2 tasks done
Tracked by #3801
lnist opened this issue Apr 28, 2023 · 5 comments
Open
2 tasks done
Tracked by #3801

Cannot keys or values from json object using stdJson.readStringArray #4844

lnist opened this issue Apr 28, 2023 · 5 comments
Labels
C-forge Command: forge Cmd-forge-test Command: forge test P-low Priority: low T-bug Type: bug T-to-investigate Type: to investigate

Comments

@lnist
Copy link

lnist commented Apr 28, 2023

Component

Forge

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

  • Foundry
  • Foundryup

What version of Foundry are you on?

forge 0.2.0 (8f246e0 2023-04-28T00:03:59.802167368Z)

What command(s) is the bug in?

forge test

Operating System

Linux

Describe the bug

Due to #4833 we tried to update lib/forge-std from 4a79aca83f8075f8b1b4fe9153945fef08375630 to b971f66b8416e3b09f240f6ee7230ad3bdb13c19 in order to set the number of expected calls.

However, that breaks our usage of readStringArray . We try to read the methodIdentifiers from the ABI output in the "out" files. We have previously been able to use code similar to the following:

// SPDX-License-Identifier:	AGPL-3.0
pragma solidity ^0.8.17;

import {stdJson} from "forge-std/StdJson.sol";
import {Test, console2} from "forge-std/Test.sol";

contract JsonTest is Test {
  function test_readStringArray() public view {
    string memory json = "{ \"abi\": [ ], \"methodIdentifiers\": { \"function1()\": \"abcdabcd\", \"function2()\": \"def0def0\" } } ";
    string[] memory methodIdentifiers = stdJson.readStringArray(json, ".methodIdentifiers[*]~");
    bytes[] memory selectors = new bytes[](methodIdentifiers.length);
    for (uint i = 0; i < methodIdentifiers.length; i++) {
      selectors[i] = vm.parseBytes(methodIdentifiers[i]);
    }
    console2.log(vm.toString(selectors[0]));
    console2.log(vm.toString(selectors[1]));
  }
}

Which outputs the following on the old version of the lib

[PASS] test_readStringArray() (gas: 15952)
Logs:
  0xabcdabcd
  0xdef0def0

But with the updated lib/forge-std we get:

 [FAIL. Reason: EvmError: Revert] test_readStringArray() (gas: 4281)
Traces:
  [4281] JsonTest::test_readStringArray() 
    ├─ [0] VM::parseJsonStringArray({ "abi": [ ], "methodIdentifiers": { "function1()": "abcdabcd", "function2()": "def0def0" } } , .methodIdentifiers[*]~) 
    │   └─ ← 0x000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000086162636461626364000000000000000000000000000000000000000000000000
    └─ ← "EvmError: Revert"

Note, the ~ at the end of .methodIdentifiers[*]~ should actually make it output the keys and not the values, but that is a separate and not important issue for us. Without it it still does not work.

Looking at https://github.com/foundry-rs/foundry/blob/8f246e07c89129b6effa89f0d71c4ac67758a155/testdata/cheats/Json.t.sol it seems reading all the keys or values of objects is not in the test suite, so could it be broken or are we using it wrong?

@lnist lnist added the T-bug Type: bug label Apr 28, 2023
@gakonst gakonst added this to Foundry Apr 28, 2023
@github-project-automation github-project-automation bot moved this to Todo in Foundry Apr 28, 2023
lnist added a commit to mangrovedao/mangrove-core that referenced this issue Apr 28, 2023
lnist added a commit to mangrovedao/mangrove-core that referenced this issue Apr 28, 2023
@mds1
Copy link
Collaborator

mds1 commented Apr 28, 2023

Hmm interesting, I don't recall anything touching this functionality recently that would have broken it. Are you certain it's the forge-std version bump that causes the issue, as opposed to a forge version bump? cc'ing @odyslam here too

@lnist
Copy link
Author

lnist commented Apr 28, 2023

It works before "forge update" but not after. Isn't that only the libs that get updated?

@0xMelkor
Copy link
Contributor

0xMelkor commented May 1, 2023

I've the same error. Let me investigate..

@0xMelkor
Copy link
Contributor

0xMelkor commented May 1, 2023

I believe #294 introduced some issue. Need to investigate more..

@lnist this workaround can help for the moment:

// SPDX-License-Identifier:	AGPL-3.0
pragma solidity ^0.8.17;

import {stdJson} from "forge-std/StdJson.sol";
import {Test, console2} from "forge-std/Test.sol";

contract JsonTest is Test {
  function test_readStringArray() public view {
    string memory json = "{ \"abi\": [ ], \"methodIdentifiers\": { \"function1()\": \"abcdabcd\", \"function2()\": \"def0def0\" } } ";
    string memory key = ".methodIdentifiers[*]~";

    bytes memory encoded = vm.parseJson(json, key);
    string[] memory methodIdentifiers = abi.decode(encoded, (string[]));

    bytes[] memory selectors = new bytes[](methodIdentifiers.length);
    for (uint i = 0; i < methodIdentifiers.length; i++) {
      selectors[i] = vm.parseBytes(methodIdentifiers[i]);
    }
    console2.log(vm.toString(selectors[0]));
    console2.log(vm.toString(selectors[1]));
  }
}

lnist added a commit to mangrovedao/mangrove-core that referenced this issue May 2, 2023
lnist added a commit to mangrovedao/mangrove-core that referenced this issue May 2, 2023
@lnist
Copy link
Author

lnist commented May 2, 2023

Thanks! the workaround fixes the problem for us. Will switch back to the other implementation when it works for this scenario :)

@zerosnacks zerosnacks added Cmd-forge-test Command: forge test C-forge Command: forge labels Jun 28, 2024
@zerosnacks zerosnacks added this to the v1.0.0 milestone Jul 26, 2024
@grandizzy grandizzy added the P-low Priority: low label Oct 31, 2024
@grandizzy grandizzy added the T-to-investigate Type: to investigate label Nov 19, 2024
@zerosnacks zerosnacks self-assigned this Dec 2, 2024
@zerosnacks zerosnacks moved this from Todo to In Progress in Foundry Dec 2, 2024
@zerosnacks zerosnacks removed their assignment Jan 27, 2025
@grandizzy grandizzy removed this from the v1.0.0 milestone Feb 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-forge Command: forge Cmd-forge-test Command: forge test P-low Priority: low T-bug Type: bug T-to-investigate Type: to investigate
Projects
Status: In Progress
Development

Successfully merging a pull request may close this issue.

5 participants