foundry icon indicating copy to clipboard operation
foundry copied to clipboard

Cannot keys or values from json object using stdJson.readStringArray

Open lnist opened this issue 1 year ago • 5 comments

Component

Forge

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

  • [X] Foundry
  • [X] 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 https://github.com/foundry-rs/foundry/pull/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 avatar Apr 28 '23 08:04 lnist