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 how the state of the fork block is handled #1016

Merged
merged 2 commits into from
Nov 24, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ const notSupportedError = (method: string) =>

export class ForkStateManager implements PStateManager {
private _state: State = ImmutableMap();
private _stateRoot: string = randomHash();
private _initialStateRoot: string = randomHash();
private _stateRoot: string = this._initialStateRoot;
private _stateRootToState: Map<string, State> = new Map();
private _originalStorageCache: Map<string, Buffer> = new Map();
private _stateCheckpoints: string[] = [];
Expand All @@ -50,6 +51,15 @@ export class ForkStateManager implements PStateManager {
this._state = ImmutableMap();
}

/**
* The forked block needs special handling because the genesis accounts are added to it.
* This is meant to be called when the initial state is final, so that _initialStateRoot
* points to this starting fork state.
*/
public updateInitialStateRoot() {
this._stateRootToState.set(this._initialStateRoot, this._state);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of adding this function and a long comment explaining its purpose I would put all of the initialization logic inside the FSM constructor. In other words, move this:
https://github.com/nomiclabs/hardhat/blob/b21474ce1df891edd7fb7b94f8bbc079c652b85a/packages/hardhat-core/src/internal/hardhat-network/provider/utils/putGenesisAccounts.ts#L10-L13
and make genesisAccounts a constructor parameter.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, putAccount is async only for the purpose of compliance with the PStateManager interface. I'd add a private synchronous _putAccount method and call it in the constructor and in the original public putAccount.


public copy(): ForkStateManager {
const fsm = new ForkStateManager(
this._jsonRpcClient,
Expand Down Expand Up @@ -319,6 +329,10 @@ export class ForkStateManager implements PStateManager {
if (this._stateCheckpoints.length !== 0) {
throw checkpointedError("setBlockContext");
}
if (blockNumber.eq(this._forkBlockNumber)) {
this._setStateRoot(toBuffer(this._initialStateRoot));
return;
}
Comment on lines +320 to +323

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +320 to +323

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of adding this if statement, we could make the this._initialStateRoot equal to the actual stateRoot of the fork block. However this would require fetching the block before FSM construction and would slow down the HardhatNode creation. Having considered this I would leave this the way it is.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did exactly the same reasoning, I'm glad that you agree 😅

if (blockNumber.gt(this._forkBlockNumber)) {
this._setStateRoot(stateRoot);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ export async function putGenesisAccounts(
const { address, account } = makeAccount(ga);
await stateManager.putAccount(address, account);
}

stateManager.updateInitialStateRoot();
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ export async function deployContract(
}

export async function sendTxToZeroAddress(
provider: EthereumProvider
provider: EthereumProvider,
from?: string
): Promise<string> {
const accounts = await provider.send("eth_accounts");

const burnTxParams = {
from: accounts[0],
from: from ?? accounts[0],
to: zeroAddress(),
value: numberToRpcQuantity(1),
gas: numberToRpcQuantity(21000),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,25 @@ describe("Eth module", function () {
`Received invalid block number ${futureBlock}. Latest block number is ${firstBlock}`
);
});

it("Should return the initial balance for the genesis accounts in the previous block after a transaction", async function () {
const blockNumber = await this.provider.send("eth_blockNumber");
const account = DEFAULT_ACCOUNTS_ADDRESSES[0];

const initialBalanceBeforeTx = await this.provider.send(
"eth_getBalance",
[account, blockNumber]
);
assert.equal(initialBalanceBeforeTx, "0xde0b6b3a7640000");

await sendTxToZeroAddress(this.provider, account);

const initialBalanceAfterTx = await this.provider.send(
"eth_getBalance",
[account, blockNumber]
);
assert.equal(initialBalanceAfterTx, "0xde0b6b3a7640000");
});
});

describe("eth_getBlockByHash", async function () {
Expand Down