Skip to content

Commit

Permalink
Fix setStorageAt in untouched addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
fvictorio committed Feb 15, 2024
1 parent ea39450 commit 8fba3bc
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
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

0 comments on commit 8fba3bc

Please sign in to comment.