Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
fix?
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmurdoch committed Oct 11, 2023
1 parent 4841850 commit dcd8ddf
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 51 deletions.
1 change: 1 addition & 0 deletions packages/ethereum/ethereum/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,7 @@ export default class EthereumApi implements Api {
const vm = await blockchain.createVmFromStateTrie(
blockchain.trie.shallowCopy(false),
options.chain.allowUnlimitedContractSize,
options.chain.allowUnlimitedInitCodeSize,
false
);
return vm;
Expand Down
27 changes: 15 additions & 12 deletions packages/ethereum/ethereum/src/blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ import { dumpTrieStorageDetails } from "./helpers/storage-range-at";
import { GanacheStateManager } from "./state-manager";
import { TrieDB } from "./trie-db";
import { Trie } from "@ethereumjs/trie";
import { removeEIP3860InitCodeSizeLimitCheck } from "./helpers/common-helpers";
import { Journal } from "@ethereumjs/evm/dist/cjs/journal";

const mclInitPromise = mcl.init(mcl.BLS12_381).then(() => {
Expand Down Expand Up @@ -253,10 +252,6 @@ export default class Blockchain extends Emittery<BlockchainTypedEvents> {
options.chain.networkId,
options.chain.hardfork
);

if (options.chain.allowUnlimitedInitCodeSize) {
removeEIP3860InitCodeSizeLimitCheck(common);
}
}

this.isPostParis = this.common.gteHardfork("paris");
Expand Down Expand Up @@ -302,6 +297,7 @@ export default class Blockchain extends Emittery<BlockchainTypedEvents> {
this.vm = await this.createVmFromStateTrie(
this.trie,
options.chain.allowUnlimitedContractSize,
options.chain.allowUnlimitedInitCodeSize,
true
);

Expand Down Expand Up @@ -678,6 +674,7 @@ export default class Blockchain extends Emittery<BlockchainTypedEvents> {
createVmFromStateTrie = async (
stateTrie: GanacheTrie | ForkTrie,
allowUnlimitedContractSize: boolean,
allowUnlimitedInitCodeSize: boolean,
activatePrecompile: boolean,
common?: Common
) => {
Expand Down Expand Up @@ -710,6 +707,7 @@ export default class Blockchain extends Emittery<BlockchainTypedEvents> {
const evm = new EVM({
common,
allowUnlimitedContractSize,
allowUnlimitedInitCodeSize,
stateManager,
blockchain
});
Expand Down Expand Up @@ -1143,6 +1141,7 @@ export default class Blockchain extends Emittery<BlockchainTypedEvents> {
const vm = await this.createVmFromStateTrie(
stateTrie,
options.chain.allowUnlimitedContractSize,
options.chain.allowUnlimitedInitCodeSize,
false, // precompiles have already been initialized in the stateTrie
common
);
Expand Down Expand Up @@ -1274,10 +1273,12 @@ export default class Blockchain extends Emittery<BlockchainTypedEvents> {
})
: new GanacheStateManager({ trie, prefixCodeHashes: false });

const { allowUnlimitedContractSize, allowUnlimitedInitCodeSize } =
this.#options.chain;
const evm = new EVM({
common,
allowUnlimitedContractSize:
this.#options.chain.allowUnlimitedContractSize,
allowUnlimitedContractSize,
allowUnlimitedInitCodeSize,
stateManager,
blockchain
});
Expand Down Expand Up @@ -1312,14 +1313,15 @@ export default class Blockchain extends Emittery<BlockchainTypedEvents> {
this.emit("ganache:vm:tx:before", {
context: transactionEventContext
});
await vm.runTx({
const g = vm.runTx({
skipHardForkValidation: true,
skipNonce: true,
skipBalance: true,
skipBlockGasLimitValidation: true,
tx,
block: block as any
});
await g;
this.emit("ganache:vm:tx:after", {
context: transactionEventContext
});
Expand Down Expand Up @@ -1646,13 +1648,11 @@ export default class Blockchain extends Emittery<BlockchainTypedEvents> {
if (!rawAccount) {
throw new Error(`account ${contractAddress} doesn't exist`);
}
const [, , stateRoot] = decode<EthereumRawAccount>(Buffer.from(rawAccount));
let storageTrie: Trie;
if (txIndex === 0) {
// there are no transactions to run, so let's just grab what we need
// from the last block's trie
const [, , stateRoot] = decode<EthereumRawAccount>(
Buffer.from(rawAccount)
);
trie.setContext(
stateRoot,
contractAddressBuffer,
Expand All @@ -1670,7 +1670,10 @@ export default class Blockchain extends Emittery<BlockchainTypedEvents> {
// run every transaction in that block prior to the requested transaction
const vm = await this.#createFastForwardVm(txIndex, trie, newBlock);

storageTrie = await vm.stateManager.getStorageTrie(contractAddressBuffer);
storageTrie = await vm.stateManager.getStorageTrie(
contractAddressBuffer,
await vm.stateManager.getStateRoot()
);
}

return await dumpTrieStorageDetails(
Expand Down
8 changes: 1 addition & 7 deletions packages/ethereum/ethereum/src/forking/fork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import BlockManager from "../data-managers/block-manager";
import { ProviderHandler } from "./handlers/provider-handler";
import { PersistentCache } from "./persistent-cache/persistent-cache";
import { URL } from "url";
import { removeEIP3860InitCodeSizeLimitCheck } from "../helpers/common-helpers";

async function fetchChainId(fork: Fork) {
const chainIdHex = await fork.request<string>("eth_chainId", []);
Expand Down Expand Up @@ -128,9 +127,7 @@ export class Fork {
},
{ baseChain: KNOWN_CHAINIDS.has(chainId) ? chainId : 1 }
);
if (this.#options.chain.allowUnlimitedInitCodeSize) {
removeEIP3860InitCodeSizeLimitCheck(this.common);
}

// disable listeners to common since we don't actually cause any `emit`s,
// but other EVM parts to listen and will make node complain about too
// many listeners.
Expand Down Expand Up @@ -327,9 +324,6 @@ export class Fork {
{ baseChain: 1 }
);
}
if (this.#options.chain.allowUnlimitedInitCodeSize) {
removeEIP3860InitCodeSizeLimitCheck(forkCommon);
}
return forkCommon;
} else {
return common;
Expand Down
22 changes: 0 additions & 22 deletions packages/ethereum/ethereum/src/helpers/common-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,5 @@
import type { Common } from "@ethereumjs/common";

/**
* Effectively removes the 3860 InitCode Size Limit check by increasing the init
* code size to Number.MAX_SAFE_INTEGER.
*
* This number works because an initcode can practically never be that large
* (approx 9 PetaBytes!).
*
* @param common
* @returns
*/
export function removeEIP3860InitCodeSizeLimitCheck(common: Common) {
// this is a hack until EJS ships `allowUnlimitedInitCodeSize` option https://github.com/ethereumjs/ethereumjs-monorepo/issues/2588
return changeCommonParamValue(
common,
3860,
"vm",
"maxInitCodeSize",
// we'd use Infinity if we could, but that's not a valid BigInt
BigInt(Number.MAX_SAFE_INTEGER)
);
}

export function changeCommonParamValue(
common: Common,
eip: number,
Expand Down
19 changes: 9 additions & 10 deletions packages/ethereum/ethereum/src/state-manager.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import {
DefaultStateManager,
DefaultStateManagerOpts
} from "@ethereumjs/statemanager";
import { DefaultStateManager } from "@ethereumjs/statemanager";
import { Trie } from "@ethereumjs/trie";
import { KECCAK256_RLP } from "@ethereumjs/util";

export class GanacheStateManager extends DefaultStateManager {
async getStorageTrie(address: Buffer): Promise<Trie> {
return await (this as any)._getStorageTrie(
async getStorageTrie(
address: Buffer,
storageRoot: Uint8Array
): Promise<Trie> {
return await super._getStorageTrie(
{
bytes: address
},
} as any,
{
storageRoot: KECCAK256_RLP
}
storageRoot
} as any
);
}
}
1 change: 1 addition & 0 deletions packages/ethereum/ethereum/tests/api/eth/call.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,7 @@ describe("api", () => {
trie,
false,
false,
false,
blockchain.common
);
const fromState = await vm.stateManager.getAccount(vmFromAddress);
Expand Down

0 comments on commit dcd8ddf

Please sign in to comment.