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

getStorageAt() rejects properly padded storage addresses starting with zeros #2709

Closed
cameel opened this issue May 13, 2022 · 4 comments
Closed
Assignees

Comments

@cameel
Copy link

cameel commented May 13, 2022

Og Hardhat 2.9.5 this code causes an error despite the storage address being correctly padded to 66 characters

await ethers.provider.getStorageAt(
    address,
    "0x0000000000000000000000000000000000000000000000000000000000000006"
)
InvalidArgumentsError: Errors encountered in param 1: Storage slot argument must have a length of 66 ("0x" + 32 bytes), but '0x6' has a length of 3

As I understand it something like "0x6" should be rejected but a number with all the leading zeros should not cause this error. Currently both are rejected.

How to reproduce

Run this in the shell:

mkdir test/ contracts/

cat <<EOF > hardhat.config.js
require("@nomiclabs/hardhat-waffle");

module.exports = {
    solidity: {
        compilers: [{
            version: "0.8.13",
            settings: {
                viaIR: true,
                optimizer: { enabled: true },
            },
        }]
    }
};
EOF

cat <<EOF > test/GetStorageAtTest.js
const { ethers } = require("hardhat");

const address = "0x1111111111222222222233333333334444444444";

it("getStorageAt() without leading zeros", async () => {
    await ethers.provider.getStorageAt(address, "0x12");
});

it("getStorageAt() with leading zeros", async () => {
    await ethers.provider.getStorageAt(address, "0x00" + "".padEnd(60, "0") + "12");
});

it("getStorageAt() with leading non-zeros", async () => {
    await ethers.provider.getStorageAt(address, "0x12" + "".padEnd(60, "0") + "12");;
});
EOF

cat <<EOF > contracts/C.sol
contract C {}
EOF

npm install hardhat @nomiclabs/hardhat-waffle chai
npx hardhat test

Output:

  1) getStorageAt() without leading zeros
  2) getStorageAt() with leading zeros
  ✔ getStorageAt() with leading non-zeros

  1 passing (537ms)
  2 failing

  1) getStorageAt() without leading zeros:
     InvalidArgumentsError: Errors encountered in param 1: Storage slot argument must have a length of 66 ("0x" + 32 bytes), but '0x12' has a length of 4
      at validateParams (node_modules/hardhat/src/internal/core/jsonrpc/types/input/validation.ts:64:13)
      at EthModule._getStorageAtParams (node_modules/hardhat/src/internal/hardhat-network/provider/modules/eth.ts:695:26)
      at EthModule.processRequest (node_modules/hardhat/src/internal/hardhat-network/provider/modules/eth.ts:185:49)
      at HardhatNetworkProvider._send (node_modules/hardhat/src/internal/hardhat-network/provider/provider.ts:195:31)
      at processTicksAndRejections (node:internal/process/task_queues:96:5)
      at runNextTicks (node:internal/process/task_queues:65:3)
      at listOnTimeout (node:internal/timers:528:9)
      at processTimers (node:internal/timers:502:7)
      at HardhatNetworkProvider.request (node_modules/hardhat/src/internal/hardhat-network/provider/provider.ts:118:18)
      at EthersProviderWrapper.send (node_modules/@nomiclabs/hardhat-ethers/src/internal/ethers-provider-wrapper.ts:13:20)

  2) getStorageAt() with leading zeros:
     InvalidArgumentsError: Errors encountered in param 1: Storage slot argument must have a length of 66 ("0x" + 32 bytes), but '0x12' has a length of 4
      at validateParams (node_modules/hardhat/src/internal/core/jsonrpc/types/input/validation.ts:64:13)
      at EthModule._getStorageAtParams (node_modules/hardhat/src/internal/hardhat-network/provider/modules/eth.ts:695:26)
      at EthModule.processRequest (node_modules/hardhat/src/internal/hardhat-network/provider/modules/eth.ts:185:49)
      at HardhatNetworkProvider._send (node_modules/hardhat/src/internal/hardhat-network/provider/provider.ts:195:31)
      at processTicksAndRejections (node:internal/process/task_queues:96:5)
      at runNextTicks (node:internal/process/task_queues:65:3)
      at listOnTimeout (node:internal/timers:528:9)
      at processTimers (node:internal/timers:502:7)
      at HardhatNetworkProvider.request (node_modules/hardhat/src/internal/hardhat-network/provider/provider.ts:118:18)
      at EthersProviderWrapper.send (node_modules/@nomiclabs/hardhat-ethers/src/internal/ethers-provider-wrapper.ts:13:20)
@github-actions
Copy link
Contributor

This issue is also being tracked on Linear.

We use Linear to manage our development process, but we keep the conversations on Github.

LINEAR-ID: 90203a2a-e723-4825-a069-2ee3a443d523

@haardikk21
Copy link

Facing the same issue, looks like ethers.js is actually trimming zeroes from the padded string.

@stobiewan
Copy link

stobiewan commented May 18, 2022

hardhat-ethers 2.0.6 strips the zeros in index.ts:hexValue(), and hardhat 2.9.5 changed the type of slot from quantity to Storageslot. The Storageslot validation function requires all the leading zeros so getStorageAt() always fails.

@fvictorio
Copy link
Member

I think this comes from ethers. See this issue. This was working before because both us and ethers were going together against the spec 😅

By the way, we recently released a new package (in beta) to help with things like these. If anyone is interested in trying it, check this doc.

I'm going to close this issue now since there's not much we can do on our side.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants