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

Fix setStorageAt in untouched addresses #4864

Merged
merged 3 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/new-rocks-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"hardhat": patch
---

Fixed a bug when `hardhat_setStorageAt` was used in untouched addresses
2 changes: 2 additions & 0 deletions .github/workflows/hardhat-network-helpers-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ on:
push:
branches: [$default-branch]
paths:
- "packages/hardhat-core/**"
- "packages/hardhat-network-helpers/**"
- "packages/hardhat-common/**"
- "config/**"
pull_request:
branches:
- "**"
paths:
- "packages/hardhat-core/**"
- "packages/hardhat-network-helpers/**"
- "packages/hardhat-common/**"
- "config/**"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1430,6 +1430,12 @@ Hardhat Network's forking functionality only works with blocks from at least spu
positionIndex: bigint,
value: Buffer
) {
// create the account if it doesn't exist
const account = await this._stateManager.getAccount(address);
if (account === undefined) {
await this._stateManager.putAccount(address, new Account());
}

await this._stateManager.putContractStorage(
address,
setLengthLeft(bigIntToBytes(positionIndex), 32),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
import { compileLiteral } from "../../stack-traces/compilation";
import { getPendingBaseFeePerGas } from "../../helpers/getPendingBaseFeePerGas";
import { RpcBlockOutput } from "../../../../../src/internal/hardhat-network/provider/output";
import { randomAddressString } from "../../../../../src/internal/hardhat-network/provider/utils/random";
import * as BigIntUtils from "../../../../../src/internal/util/bigint";
import { EXAMPLE_DIFFICULTY_CONTRACT } from "../../helpers/contracts";
import { HardhatMetadata } from "../../../../../src/internal/core/jsonrpc/types/output/metadata";
Expand Down Expand Up @@ -1697,6 +1698,21 @@ describe("Hardhat module", function () {
);
assert.strictEqual(balancePreviousBlockAfterRevert, targetBalance1);
});

it("should work with accounts that weren't interacted with before", async function () {
const targetAddress = randomAddressString();
await this.provider.send("hardhat_setBalance", [
targetAddress,
numberToRpcQuantity(123),
]);

const resultingBalance = await this.provider.send("eth_getBalance", [
targetAddress,
"latest",
]);

assert.equal(BigInt(resultingBalance), 123n);
});
});

describe("hardhat_setCode", function () {
Expand Down Expand Up @@ -1893,6 +1909,22 @@ describe("Hardhat module", function () {
).stateRoot;
assert.equal(newStateRoot, oldStateRoot);
});

it("should work with accounts that weren't interacted with before", async function () {
const targetAddress = randomAddressString();
const targetCode = "0x0123456789abcdef";
await this.provider.send("hardhat_setCode", [
targetAddress,
targetCode,
]);

const actualCode = await this.provider.send("eth_getCode", [
targetAddress,
"latest",
]);

assert.equal(actualCode, targetCode);
});
});

describe("hardhat_setNonce", function () {
Expand Down Expand Up @@ -2076,6 +2108,21 @@ describe("Hardhat module", function () {
"Cannot set account nonce when the transaction pool is not empty"
);
});

it("should work with accounts that weren't interacted with before", async function () {
const targetAddress = randomAddressString();
await this.provider.send("hardhat_setNonce", [
targetAddress,
numberToRpcQuantity(123),
]);

const resultingNonce = await this.provider.send(
"eth_getTransactionCount",
[targetAddress, "latest"]
);

assert.equal(BigInt(resultingNonce), 123n);
});
});

describe("hardhat_setStorageAt", function () {
Expand Down Expand Up @@ -2253,6 +2300,23 @@ describe("Hardhat module", function () {
targetStorageValue
);
});

it("should work with accounts that weren't interacted with before", async function () {
const targetAddress = randomAddressString();
const targetStorageValue = 99;
await this.provider.send("hardhat_setStorageAt", [
targetAddress,
numberToRpcQuantity(0),
`0x${BigIntUtils.toEvmWord(targetStorageValue)}`,
]);

const resultingStorageValue = await this.provider.send(
"eth_getStorageAt",
[targetAddress, numberToRpcStorageSlot(0), "latest"]
);

assert.equal(resultingStorageValue, targetStorageValue);
});
});

describe("hardhat_dropTransaction", function () {
Expand Down
Loading