hardhat 2.8.0 → 2.8.1-dev.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/builtin-tasks/test.js +1 -0
  2. package/builtin-tasks/test.js.map +1 -1
  3. package/internal/cli/cli.js +2 -0
  4. package/internal/cli/cli.js.map +1 -1
  5. package/internal/cli/project-creation.js +1 -1
  6. package/internal/cli/project-creation.js.map +1 -1
  7. package/internal/core/config/config-loading.d.ts +3 -2
  8. package/internal/core/config/config-loading.d.ts.map +1 -1
  9. package/internal/core/config/config-loading.js +19 -2
  10. package/internal/core/config/config-loading.js.map +1 -1
  11. package/internal/hardhat-network/jsonrpc/handler.d.ts +1 -1
  12. package/internal/hardhat-network/jsonrpc/handler.d.ts.map +1 -1
  13. package/internal/hardhat-network/jsonrpc/handler.js +2 -1
  14. package/internal/hardhat-network/jsonrpc/handler.js.map +1 -1
  15. package/internal/hardhat-network/jsonrpc/server.js +2 -2
  16. package/internal/hardhat-network/jsonrpc/server.js.map +1 -1
  17. package/internal/hardhat-network/provider/node.d.ts.map +1 -1
  18. package/internal/hardhat-network/provider/node.js +8 -6
  19. package/internal/hardhat-network/provider/node.js.map +1 -1
  20. package/internal/hardhat-network/provider/utils/makeCommon.d.ts +1 -2
  21. package/internal/hardhat-network/provider/utils/makeCommon.d.ts.map +1 -1
  22. package/internal/hardhat-network/provider/utils/makeCommon.js +2 -2
  23. package/internal/hardhat-network/provider/utils/makeCommon.js.map +1 -1
  24. package/package.json +5 -5
  25. package/src/builtin-tasks/test.ts +2 -0
  26. package/src/internal/cli/cli.ts +19 -8
  27. package/src/internal/cli/project-creation.ts +1 -1
  28. package/src/internal/core/config/config-loading.ts +32 -2
  29. package/src/internal/core/errors-list.ts +15 -0
  30. package/src/internal/hardhat-network/jsonrpc/handler.ts +1 -1
  31. package/src/internal/hardhat-network/jsonrpc/server.ts +1 -1
  32. package/src/internal/hardhat-network/provider/node.ts +8 -7
  33. package/src/internal/hardhat-network/provider/utils/makeCommon.ts +2 -2
  34. package/internal/hardhat-network/provider/HardhatStateManager.d.ts +0 -50
  35. package/internal/hardhat-network/provider/HardhatStateManager.d.ts.map +0 -1
  36. package/internal/hardhat-network/provider/HardhatStateManager.js +0 -249
  37. package/internal/hardhat-network/provider/HardhatStateManager.js.map +0 -1
  38. package/src/internal/hardhat-network/provider/HardhatStateManager.ts +0 -349
@@ -1 +0,0 @@
1
- {"version":3,"file":"HardhatStateManager.d.ts","sourceRoot":"","sources":["../../../src/internal/hardhat-network/provider/HardhatStateManager.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,mBAAmB,EAAE,MAAM,qCAAqC,CAAC;AAC1E,OAAO,EACL,OAAO,EACP,OAAO,EAOR,MAAM,iBAAiB,CAAC;AAOzB,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAoB9C,qBAAa,mBAAoB,YAAW,mBAAmB;IAC7D,OAAO,CAAC,MAAM,CAAgE;IAC9E,OAAO,CAAC,iBAAiB,CAAwB;IACjD,OAAO,CAAC,UAAU,CAAkC;IACpD,OAAO,CAAC,iBAAiB,CAAiC;IAC1D,OAAO,CAAC,qBAAqB,CAAkC;IAC/D,OAAO,CAAC,iBAAiB,CAAgB;IACzC,OAAO,CAAC,eAAe,CAAS;IAGhC,OAAO,CAAC,gBAAgB,CAAgD;IACxE,OAAO,CAAC,wBAAwB,CAE9B;;IAQW,yBAAyB,CAAC,eAAe,EAAE,cAAc,EAAE;IASjE,IAAI,IAAI,mBAAmB;IAUrB,UAAU,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC;IAqB9C,UAAU,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;IAInE,YAAY,CAAC,QAAQ,EAAE,OAAO,GAAG,IAAI;IAI/B,eAAe,CAAC,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,GAAG,O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AAO,CAAC,MAAM,CAAC;IAgBX,eAAe,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO;IAIzC,gBAAgB,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAIvC,eAAe,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO;IAQvD,gBAAgB,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI;IAQrD,mBAAmB,IAAI,IAAI;IAIlC,OAAO,CAAC,WAAW;IAiBnB,OAAO,CAAC,aAAa;CAStB"}
@@ -1,249 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.HardhatStateManager = void 0;
4
- const state_1 = require("@ethereumjs/vm/dist/state");
5
- const ethereumjs_util_1 = require("ethereumjs-util");
6
- const immutable_1 = require("immutable");
7
- const errors_1 = require("../../core/providers/errors");
8
- const AccountState_1 = require("./fork/AccountState");
9
- const random_1 = require("./fork/random");
10
- const makeAccount_1 = require("./utils/makeAccount");
11
- const encodeStorageKey = (address, position) => {
12
- return `${address.toString("hex")}${(0, ethereumjs_util_1.unpadBuffer)(position).toString("hex")}`;
13
- };
14
- const checkpointedError = (method) => new Error(`${method} called when checkpointed`);
15
- const notCheckpointedError = (method) => new Error(`${method} called when not checkpointed`);
16
- const notSupportedError = (method) => new Error(`${method} is not supported when forking from remote network`);
17
- class HardhatStateManager {
18
- constructor() {
19
- this._state = (0, immutable_1.Map)();
20
- this._initialStateRoot = (0, random_1.randomHash)();
21
- this._stateRoot = this._initialStateRoot;
22
- this._stateRootToState = new Map();
23
- this._originalStorageCache = new Map();
24
- this._stateCheckpoints = [];
25
- this._contextChanged = false;
26
- // used by the DefaultStateManager calls
27
- this._accessedStorage = [new Map()];
28
- this._accessedStorageReverted = [
29
- new Map(),
30
- ];
31
- this._state = (0, immutable_1.Map)();
32
- this._stateRootToState.set(this._initialStateRoot, this._state);
33
- }
34
- async initializeGenesisAccounts(genesisAccounts) {
35
- for (const ga of genesisAccounts) {
36
- const { address, account } = (0, makeAccount_1.makeAccount)(ga);
37
- this._putAccount(address, account);
38
- }
39
- this._stateRootToState.set(this._initialStateRoot, this._state);
40
- }
41
- copy() {
42
- const hsm = new HardhatStateManager();
43
- hsm._state = this._state;
44
- hsm._stateRoot = this._stateRoot;
45
- // because this map is append-only we don't need to copy it
46
- hsm._stateRootToState = this._stateRootToState;
47
- return hsm;
48
- }
49
- async getAccount(address) {
50
- const localAccount = this._state.get(address.toString());
51
- const localNonce = localAccount === null || localAccount === void 0 ? void 0 : localAccount.get("nonce");
52
- const localBalance = localAccount === null || localAccount === void 0 ? void 0 : localAccount.get("balance");
53
- const localCode = localAccount === null || localAccount === void 0 ? void 0 : localAccount.get("code");
54
- const nonce = localNonce !== undefined ? (0, ethereumjs_util_1.toBuffer)(localNonce) : new ethereumjs_util_1.BN(0);
55
- const balance = localBalance !== undefined ? (0, ethereumjs_util_1.toBuffer)(localBalance) : new ethereumjs_util_1.BN(0);
56
- const code = localCode !== undefined ? (0, ethereumjs_util_1.toBuffer)(localCode) : (0, ethereumjs_util_1.toBuffer)([]);
57
- const codeHash = (0, ethereumjs_util_1.keccak256)(code);
58
- // We ignore stateRoot since we found that it is not used anywhere of interest to us
59
- return ethereumjs_util_1.Account.fromAccountData({ nonce, balance, codeHash });
60
- }
61
- async putAccount(address, account) {
62
- this._putAccount(address, account);
63
- }
64
- touchAccount(_address) {
65
- // We don't do anything here. See cleanupTouchedAccounts for explanation
66
- }
67
- async putContractCode(address, value) {
68
- var _a;
69
- const hexAddress = address.toString();
70
- const account = ((_a = this._state.get(hexAddress)) !== null && _a !== void 0 ? _a : (0, AccountState_1.makeAccountState)()).set("code", (0, ethereumjs_util_1.bufferToHex)(value));
71
- this._state = this._state.set(hexAddress, account);
72
- }
73
- async getContractCode(address) {
74
- var _a;
75
- const localCode = (_a = this._state.get(address.toString())) === null || _a === void 0 ? void 0 : _a.get("code");
76
- if (localCode !== undefined) {
77
- return (0, ethereumjs_util_1.toBuffer)(localCode);
78
- }
79
- return (0, ethereumjs_util_1.toBuffer)([]);
80
- }
81
- async getContractStorage(address, key) {
82
- if (key.length !== 32) {
83
- throw new Error("Storage key must be 32 bytes long");
84
- }
85
- const account = this._state.get(address.toString());
86
- const localValue = account === null || account === void 0 ? void 0 : account.get("storage").get((0, ethereumjs_util_1.bufferToHex)(key));
87
- if (localValue !== undefined) {
88
- return (0, ethereumjs_util_1.toBuffer)(localValue);
89
- }
90
- return (0, ethereumjs_util_1.toBuffer)([]);
91
- }
92
- async putContractStorage(address, key, value) {
93
- var _a;
94
- if (key.length !== 32) {
95
- throw new Error("Storage key must be 32 bytes long");
96
- }
97
- if (value.length > 32) {
98
- throw new Error("Storage value cannot be longer than 32 bytes");
99
- }
100
- const unpaddedValue = (0, ethereumjs_util_1.unpadBuffer)(value);
101
- const hexAddress = address.toString();
102
- let account = (_a = this._state.get(hexAddress)) !== null && _a !== void 0 ? _a : (0, AccountState_1.makeAccountState)();
103
- const currentStorage = account.get("storage");
104
- let newValue;
105
- if (unpaddedValue.length === 0) {
106
- // if the value is an empty array or only zeros, the storage is deleted
107
- newValue = null;
108
- }
109
- else {
110
- newValue = (0, ethereumjs_util_1.bufferToHex)(unpaddedValue);
111
- }
112
- const newStorage = currentStorage.set((0, ethereumjs_util_1.bufferToHex)(key), newValue);
113
- account = account.set("storage", newStorage);
114
- this._state = this._state.set(hexAddress, account);
115
- }
116
- async clearContractStorage(address) {
117
- var _a;
118
- const hexAddress = address.toString();
119
- let account = (_a = this._state.get(hexAddress)) !== null && _a !== void 0 ? _a : (0, AccountState_1.makeAccountState)();
120
- account = account
121
- .set("storageCleared", true)
122
- .set("storage", (0, immutable_1.Map)());
123
- this._state = this._state.set(hexAddress, account);
124
- }
125
- async checkpoint() {
126
- const stateRoot = await this.getStateRoot();
127
- this._stateCheckpoints.push((0, ethereumjs_util_1.bufferToHex)(stateRoot));
128
- }
129
- async commit() {
130
- if (this._stateCheckpoints.length === 0) {
131
- throw notCheckpointedError("commit");
132
- }
133
- this._stateCheckpoints.pop();
134
- }
135
- async revert() {
136
- const checkpointedRoot = this._stateCheckpoints.pop();
137
- if (checkpointedRoot === undefined) {
138
- throw notCheckpointedError("revert");
139
- }
140
- await this.setStateRoot((0, ethereumjs_util_1.toBuffer)(checkpointedRoot));
141
- }
142
- async getStateRoot() {
143
- if (this._stateRootToState.get(this._stateRoot) !== this._state) {
144
- this._stateRoot = (0, random_1.randomHash)();
145
- this._stateRootToState.set(this._stateRoot, this._state);
146
- }
147
- return (0, ethereumjs_util_1.toBuffer)(this._stateRoot);
148
- }
149
- async setStateRoot(stateRoot) {
150
- this._setStateRoot(stateRoot);
151
- }
152
- async dumpStorage(_address) {
153
- throw notSupportedError("dumpStorage");
154
- }
155
- async hasGenesisState() {
156
- throw notSupportedError("hasGenesisState");
157
- }
158
- async generateCanonicalGenesis() {
159
- throw notSupportedError("generateCanonicalGenesis");
160
- }
161
- async generateGenesis(_initState) {
162
- throw notSupportedError("generateGenesis");
163
- }
164
- async accountIsEmpty(address) {
165
- const account = await this.getAccount(address);
166
- // From https://eips.ethereum.org/EIPS/eip-161
167
- // An account is considered empty when it has no code and zero nonce and zero balance.
168
- return (new ethereumjs_util_1.BN(account.nonce).eqn(0) &&
169
- new ethereumjs_util_1.BN(account.balance).eqn(0) &&
170
- account.codeHash.equals(ethereumjs_util_1.KECCAK256_NULL));
171
- }
172
- async cleanupTouchedAccounts() {
173
- // We do not do anything here, because cleaning accounts only affects the
174
- // stateRoot. Since the stateRoot is fake anyway there is no need to
175
- // perform this operation.
176
- }
177
- restoreForkBlockContext(stateRoot) {
178
- if (this._stateCheckpoints.length !== 0) {
179
- throw checkpointedError("restoreForkBlockContext");
180
- }
181
- this._setStateRoot(stateRoot);
182
- if (this._contextChanged) {
183
- this._contextChanged = false;
184
- }
185
- }
186
- accountExists(_address) {
187
- throw new errors_1.InternalError("Hardhat Network can't fork from networks running a hardfork older than Spurious Dragon");
188
- }
189
- async deleteAccount(address) {
190
- this._state = this._state.delete(address.toString());
191
- }
192
- clearOriginalStorageCache() {
193
- this._originalStorageCache = new Map();
194
- }
195
- async getOriginalContractStorage(address, key) {
196
- const storageKey = encodeStorageKey(address.toBuffer(), key);
197
- const cachedValue = this._originalStorageCache.get(storageKey);
198
- if (cachedValue !== undefined) {
199
- return cachedValue;
200
- }
201
- const value = await this.getContractStorage(address, key);
202
- this._originalStorageCache.set(storageKey, value);
203
- return value;
204
- }
205
- // the following methods are copied verbatim from
206
- // DefaultStateManager
207
- isWarmedAddress(address) {
208
- return state_1.DefaultStateManager.prototype.isWarmedAddress.call(this, address);
209
- }
210
- addWarmedAddress(address) {
211
- return state_1.DefaultStateManager.prototype.addWarmedAddress.call(this, address);
212
- }
213
- isWarmedStorage(address, slot) {
214
- return state_1.DefaultStateManager.prototype.isWarmedStorage.call(this, address, slot);
215
- }
216
- addWarmedStorage(address, slot) {
217
- return state_1.DefaultStateManager.prototype.addWarmedStorage.call(this, address, slot);
218
- }
219
- clearWarmedAccounts() {
220
- return state_1.DefaultStateManager.prototype.clearWarmedAccounts.call(this);
221
- }
222
- _putAccount(address, account) {
223
- var _a;
224
- // Because the vm only ever modifies the nonce, balance and codeHash using this
225
- // method we ignore the stateRoot property
226
- const hexAddress = address.toString();
227
- let localAccount = (_a = this._state.get(hexAddress)) !== null && _a !== void 0 ? _a : (0, AccountState_1.makeAccountState)();
228
- localAccount = localAccount
229
- .set("nonce", (0, ethereumjs_util_1.bufferToHex)(account.nonce.toBuffer()))
230
- .set("balance", (0, ethereumjs_util_1.bufferToHex)(account.balance.toBuffer()));
231
- // Code is set to empty string here to prevent unnecessary
232
- // JsonRpcClient.getCode calls in getAccount method
233
- if (account.codeHash.equals(ethereumjs_util_1.KECCAK256_NULL)) {
234
- localAccount = localAccount.set("code", "0x");
235
- }
236
- this._state = this._state.set(hexAddress, localAccount);
237
- }
238
- _setStateRoot(stateRoot) {
239
- const newRoot = (0, ethereumjs_util_1.bufferToHex)(stateRoot);
240
- const state = this._stateRootToState.get(newRoot);
241
- if (state === undefined) {
242
- throw new Error("Unknown state root");
243
- }
244
- this._stateRoot = newRoot;
245
- this._state = state;
246
- }
247
- }
248
- exports.HardhatStateManager = HardhatStateManager;
249
- //# sourceMappingURL=HardhatStateManager.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"HardhatStateManager.js","sourceRoot":"","sources":["../../../src/internal/hardhat-network/provider/HardhatStateManager.ts"],"names":[],"mappings":";;;AAAA,qDAAgE;AAEhE,qDASyB;AACzB,yCAA2E;AAE3E,wDAA4D;AAE5D,sDAAqE;AACrE,0CAA2C;AAE3C,qDAAkD;AAElD,MAAM,gBAAgB,GAAG,CAAC,OAAe,EAAE,QAAgB,EAAU,EAAE;IACrE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAC,GAAG,IAAA,6BAAW,EAAC,QAAQ,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;AAC9E,CAAC,CAAC;AAMF,MAAM,iBAAiB,GAAG,CAAC,MAAc,EAAE,EAAE,CAC3C,IAAI,KAAK,CAAC,GAAG,MAAM,2BAA2B,CAAC,CAAC;AAElD,MAAM,oBAAoB,GAAG,CAAC,MAAc,EAAE,EAAE,CAC9C,IAAI,KAAK,CAAC,GAAG,MAAM,+BAA+B,CAAC,CAAC;AAEtD,MAAM,iBAAiB,GAAG,CAAC,MAAc,EAAE,EAAE,CAC3C,IAAI,KAAK,CAAC,GAAG,MAAM,oDAAoD,CAAC,CAAC;AAE3E,MAAa,mBAAmB;IAe9B;QAdQ,WAAM,GAAU,IAAA,eAAY,GAAyC,CAAC;QACtE,sBAAiB,GAAW,IAAA,mBAAU,GAAE,CAAC;QACzC,eAAU,GAAW,IAAI,CAAC,iBAAiB,CAAC;QAC5C,sBAAiB,GAAuB,IAAI,GAAG,EAAE,CAAC;QAClD,0BAAqB,GAAwB,IAAI,GAAG,EAAE,CAAC;QACvD,sBAAiB,GAAa,EAAE,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;CACF;AAtTD,kDAsTC"}
@@ -1,349 +0,0 @@
1
- import { DefaultStateManager } from "@ethereumjs/vm/dist/state";
2
- import { EIP2929StateManager } from "@ethereumjs/vm/dist/state/interface";
3
- import {
4
- Account,
5
- Address,
6
- BN,
7
- bufferToHex,
8
- keccak256,
9
- KECCAK256_NULL,
10
- toBuffer,
11
- unpadBuffer,
12
- } from "ethereumjs-util";
13
- import { Map as ImmutableMap, Record as ImmutableRecord } from "immutable";
14
-
15
- import { InternalError } from "../../core/providers/errors";
16
-
17
- import { AccountState, makeAccountState } from "./fork/AccountState";
18
- import { randomHash } from "./fork/random";
19
- import { GenesisAccount } from "./node-types";
20
- import { makeAccount } from "./utils/makeAccount";
21
-
22
- const encodeStorageKey = (address: Buffer, position: Buffer): string => {
23
- return `${address.toString("hex")}${unpadBuffer(position).toString("hex")}`;
24
- };
25
-
26
- /* eslint-disable @nomiclabs/hardhat-internal-rules/only-hardhat-error */
27
-
28
- type State = ImmutableMap<string, ImmutableRecord<AccountState>>;
29
-
30
- const checkpointedError = (method: string) =>
31
- new Error(`${method} called when checkpointed`);
32
-
33
- const notCheckpointedError = (method: string) =>
34
- new Error(`${method} called when not checkpointed`);
35
-
36
- const notSupportedError = (method: string) =>
37
- new Error(`${method} is not supported when forking from remote network`);
38
-
39
- export class HardhatStateManager implements EIP2929StateManager {
40
- private _state: State = ImmutableMap<string, ImmutableRecord<AccountState>>();
41
- private _initialStateRoot: string = randomHash();
42
- private _stateRoot: string = this._initialStateRoot;
43
- private _stateRootToState: Map<string, State> = new Map();
44
- private _originalStorageCache: Map<string, Buffer> = new Map();
45
- private _stateCheckpoints: string[] = [];
46
- private _contextChanged = false;
47
-
48
- // used by the DefaultStateManager calls
49
- private _accessedStorage: Array<Map<string, Set<string>>> = [new Map()];
50
- private _accessedStorageReverted: Array<Map<string, Set<string>>> = [
51
- new Map(),
52
- ];
53
-
54
- constructor() {
55
- this._state = ImmutableMap<string, ImmutableRecord<AccountState>>();
56
-
57
- this._stateRootToState.set(this._initialStateRoot, this._state);
58
- }
59
-
60
- public async initializeGenesisAccounts(genesisAccounts: GenesisAccount[]) {
61
- for (const ga of genesisAccounts) {
62
- const { address, account } = makeAccount(ga);
63
- this._putAccount(address, account);
64
- }
65
-
66
- this._stateRootToState.set(this._initialStateRoot, this._state);
67
- }
68
-
69
- public copy(): HardhatStateManager {
70
- const hsm = new HardhatStateManager();
71
- hsm._state = this._state;
72
- hsm._stateRoot = this._stateRoot;
73
-
74
- // because this map is append-only we don't need to copy it
75
- hsm._stateRootToState = this._stateRootToState;
76
- return hsm;
77
- }
78
-
79
- public async getAccount(address: Address): Promise<Account> {
80
- const localAccount = this._state.get(address.toString());
81
-
82
- const localNonce = localAccount?.get("nonce");
83
- const localBalance = localAccount?.get("balance");
84
- const localCode = localAccount?.get("code");
85
-
86
- const nonce: Buffer | BN =
87
- localNonce !== undefined ? toBuffer(localNonce) : new BN(0);
88
-
89
- const balance: Buffer | BN =
90
- localBalance !== undefined ? toBuffer(localBalance) : new BN(0);
91
-
92
- const code: Buffer =
93
- localCode !== undefined ? toBuffer(localCode) : toBuffer([]);
94
-
95
- const codeHash = keccak256(code);
96
- // We ignore stateRoot since we found that it is not used anywhere of interest to us
97
- return Account.fromAccountData({ nonce, balance, codeHash });
98
- }
99
-
100
- public async putAccount(address: Address, account: Account): Promise<void> {
101
- this._putAccount(address, account);
102
- }
103
-
104
- public touchAccount(_address: Address): void {
105
- // We don't do anything here. See cleanupTouchedAccounts for explanation
106
- }
107
-
108
- public async putContractCode(address: Address, value: Buffer): Promise<void> {
109
- const hexAddress = address.toString();
110
- const account = (this._state.get(hexAddress) ?? makeAccountState()).set(
111
- "code",
112
- bufferToHex(value)
113
- );
114
- this._state = this._state.set(hexAddress, account);
115
- }
116
-
117
- public async getContractCode(address: Address): Promise<Buffer> {
118
- const localCode = this._state.get(address.toString())?.get("code");
119
- if (localCode !== undefined) {
120
- return toBuffer(localCode);
121
- }
122
-
123
- return toBuffer([]);
124
- }
125
-
126
- public async getContractStorage(
127
- address: Address,
128
- key: Buffer
129
- ): Promise<Buffer> {
130
- if (key.length !== 32) {
131
- throw new Error("Storage key must be 32 bytes long");
132
- }
133
-
134
- const account = this._state.get(address.toString());
135
- const localValue = account?.get("storage").get(bufferToHex(key));
136
-
137
- if (localValue !== undefined) {
138
- return toBuffer(localValue);
139
- }
140
-
141
- return toBuffer([]);
142
- }
143
-
144
- public async putContractStorage(
145
- address: Address,
146
- key: Buffer,
147
- value: Buffer
148
- ): Promise<void> {
149
- if (key.length !== 32) {
150
- throw new Error("Storage key must be 32 bytes long");
151
- }
152
-
153
- if (value.length > 32) {
154
- throw new Error("Storage value cannot be longer than 32 bytes");
155
- }
156
-
157
- const unpaddedValue = unpadBuffer(value);
158
-
159
- const hexAddress = address.toString();
160
- let account = this._state.get(hexAddress) ?? makeAccountState();
161
- const currentStorage = account.get("storage");
162
-
163
- let newValue: string | null;
164
- if (unpaddedValue.length === 0) {
165
- // if the value is an empty array or only zeros, the storage is deleted
166
- newValue = null;
167
- } else {
168
- newValue = bufferToHex(unpaddedValue);
169
- }
170
-
171
- const newStorage = currentStorage.set(bufferToHex(key), newValue);
172
-
173
- account = account.set("storage", newStorage);
174
-
175
- this._state = this._state.set(hexAddress, account);
176
- }
177
-
178
- public async clearContractStorage(address: Address): Promise<void> {
179
- const hexAddress = address.toString();
180
- let account = this._state.get(hexAddress) ?? makeAccountState();
181
- account = account
182
- .set("storageCleared", true)
183
- .set("storage", ImmutableMap<string, string | null>());
184
- this._state = this._state.set(hexAddress, account);
185
- }
186
-
187
- public async checkpoint(): Promise<void> {
188
- const stateRoot = await this.getStateRoot();
189
- this._stateCheckpoints.push(bufferToHex(stateRoot));
190
- }
191
-
192
- public async commit(): Promise<void> {
193
- if (this._stateCheckpoints.length === 0) {
194
- throw notCheckpointedError("commit");
195
- }
196
- this._stateCheckpoints.pop();
197
- }
198
-
199
- public async revert(): Promise<void> {
200
- const checkpointedRoot = this._stateCheckpoints.pop();
201
- if (checkpointedRoot === undefined) {
202
- throw notCheckpointedError("revert");
203
- }
204
- await this.setStateRoot(toBuffer(checkpointedRoot));
205
- }
206
-
207
- public async getStateRoot(): Promise<Buffer> {
208
- if (this._stateRootToState.get(this._stateRoot) !== this._state) {
209
- this._stateRoot = randomHash();
210
- this._stateRootToState.set(this._stateRoot, this._state);
211
- }
212
- return toBuffer(this._stateRoot);
213
- }
214
-
215
- public async setStateRoot(stateRoot: Buffer): Promise<void> {
216
- this._setStateRoot(stateRoot);
217
- }
218
-
219
- public async dumpStorage(_address: Address): Promise<Record<string, string>> {
220
- throw notSupportedError("dumpStorage");
221
- }
222
-
223
- public async hasGenesisState(): Promise<boolean> {
224
- throw notSupportedError("hasGenesisState");
225
- }
226
-
227
- public async generateCanonicalGenesis(): Promise<void> {
228
- throw notSupportedError("generateCanonicalGenesis");
229
- }
230
-
231
- public async generateGenesis(_initState: any): Promise<void> {
232
- throw notSupportedError("generateGenesis");
233
- }
234
-
235
- public async accountIsEmpty(address: Address): Promise<boolean> {
236
- const account = await this.getAccount(address);
237
- // From https://eips.ethereum.org/EIPS/eip-161
238
- // An account is considered empty when it has no code and zero nonce and zero balance.
239
- return (
240
- new BN(account.nonce).eqn(0) &&
241
- new BN(account.balance).eqn(0) &&
242
- account.codeHash.equals(KECCAK256_NULL)
243
- );
244
- }
245
-
246
- public async cleanupTouchedAccounts(): Promise<void> {
247
- // We do not do anything here, because cleaning accounts only affects the
248
- // stateRoot. Since the stateRoot is fake anyway there is no need to
249
- // perform this operation.
250
- }
251
-
252
- public restoreForkBlockContext(stateRoot: Buffer) {
253
- if (this._stateCheckpoints.length !== 0) {
254
- throw checkpointedError("restoreForkBlockContext");
255
- }
256
- this._setStateRoot(stateRoot);
257
- if (this._contextChanged) {
258
- this._contextChanged = false;
259
- }
260
- }
261
-
262
- public accountExists(_address: Address): never {
263
- throw new InternalError(
264
- "Hardhat Network can't fork from networks running a hardfork older than Spurious Dragon"
265
- );
266
- }
267
-
268
- public async deleteAccount(address: Address): Promise<void> {
269
- this._state = this._state.delete(address.toString());
270
- }
271
-
272
- public clearOriginalStorageCache(): void {
273
- this._originalStorageCache = new Map();
274
- }
275
-
276
- public async getOriginalContractStorage(
277
- address: Address,
278
- key: Buffer
279
- ): Promise<Buffer> {
280
- const storageKey = encodeStorageKey(address.toBuffer(), key);
281
- const cachedValue = this._originalStorageCache.get(storageKey);
282
- if (cachedValue !== undefined) {
283
- return cachedValue;
284
- }
285
-
286
- const value = await this.getContractStorage(address, key);
287
- this._originalStorageCache.set(storageKey, value);
288
-
289
- return value;
290
- }
291
-
292
- // the following methods are copied verbatim from
293
- // DefaultStateManager
294
-
295
- public isWarmedAddress(address: Buffer): boolean {
296
- return DefaultStateManager.prototype.isWarmedAddress.call(this, address);
297
- }
298
-
299
- public addWarmedAddress(address: Buffer): void {
300
- return DefaultStateManager.prototype.addWarmedAddress.call(this, address);
301
- }
302
-
303
- public isWarmedStorage(address: Buffer, slot: Buffer): boolean {
304
- return DefaultStateManager.prototype.isWarmedStorage.call(
305
- this,
306
- address,
307
- slot
308
- );
309
- }
310
-
311
- public addWarmedStorage(address: Buffer, slot: Buffer): void {
312
- return DefaultStateManager.prototype.addWarmedStorage.call(
313
- this,
314
- address,
315
- slot
316
- );
317
- }
318
-
319
- public clearWarmedAccounts(): void {
320
- return DefaultStateManager.prototype.clearWarmedAccounts.call(this);
321
- }
322
-
323
- private _putAccount(address: Address, account: Account): void {
324
- // Because the vm only ever modifies the nonce, balance and codeHash using this
325
- // method we ignore the stateRoot property
326
- const hexAddress = address.toString();
327
- let localAccount = this._state.get(hexAddress) ?? makeAccountState();
328
- localAccount = localAccount
329
- .set("nonce", bufferToHex(account.nonce.toBuffer()))
330
- .set("balance", bufferToHex(account.balance.toBuffer()));
331
-
332
- // Code is set to empty string here to prevent unnecessary
333
- // JsonRpcClient.getCode calls in getAccount method
334
- if (account.codeHash.equals(KECCAK256_NULL)) {
335
- localAccount = localAccount.set("code", "0x");
336
- }
337
- this._state = this._state.set(hexAddress, localAccount);
338
- }
339
-
340
- private _setStateRoot(stateRoot: Buffer) {
341
- const newRoot = bufferToHex(stateRoot);
342
- const state = this._stateRootToState.get(newRoot);
343
- if (state === undefined) {
344
- throw new Error("Unknown state root");
345
- }
346
- this._stateRoot = newRoot;
347
- this._state = state;
348
- }
349
- }