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

Commit

Permalink
refactor simulations to not use a class
Browse files Browse the repository at this point in the history
  • Loading branch information
MicaiahReid committed Aug 1, 2022
1 parent dcc64c4 commit bc77b3b
Show file tree
Hide file tree
Showing 2 changed files with 472 additions and 396 deletions.
49 changes: 17 additions & 32 deletions src/chains/ethereum/ethereum/src/blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,13 @@ import {
} from "./provider-events";

import mcl from "mcl-wasm";
import SimulationHandler, {
import {
CallOverrides,
createAccessList,
CreateAccessListResult,
runCall,
SimulationTransaction
} from "./helpers/simulations";
import { AccessList } from "@ganache/ethereum-transaction/src/access-lists";

const mclInitPromise = mcl.init(mcl.BLS12_381).then(() => {
mcl.setMapToMode(mcl.IRTF); // set the right map mode; otherwise mapToG2 will return wrong values.
Expand Down Expand Up @@ -1029,53 +1030,37 @@ export default class Blockchain extends Emittery<BlockchainTypedEvents> {
BigInt(transaction.block.header.number.toString())
)
: this.common;
const simHandler = new SimulationHandler();

// re-emit simulation events:
simHandler.on("ganache:vm:tx:before", event => {
this.emit("ganache:vm:tx:before", event);
});
simHandler.on("ganache:vm:tx:step", event => {
if (!this.#emitStepEvent) return;
this.emit("ganache:vm:tx:step", event);
});
simHandler.on("ganache:vm:tx:after", event => {
this.emit("ganache:vm:tx:after", event);
});
simHandler.on("ganache:vm:tx:console.log", event => {
options.logging.logger.log(...event.logs);
this.emit("ganache:vm:tx:console.log", event);
});

await simHandler.initialize(
this,
return await runCall({
blockchain: this,
common,
simulationBlock,
transaction,
options,
emitStepEvent: this.#emitStepEvent,
overrides
);

const callResult = await simHandler.runCall();
const callResultValue = callResult.execResult.returnValue;

return callResultValue === undefined
? Data.Empty
: Data.from(callResultValue);
});
}

public async createAccessList(
transaction: SimulationTransaction,
simulationBlock: Block
): Promise<CreateAccessListResult> {
const options = this.#options;
const common = this.fallback
? this.fallback.getCommonForBlockNumber(
this.common,
BigInt(transaction.block.header.number.toString())
)
: this.common;
const simHandler = new SimulationHandler();
await simHandler.initialize(this, common, simulationBlock, transaction);
return await simHandler.createAccessList(transaction.accessList);
return await createAccessList({
blockchain: this,
common,
simulationBlock,
transaction,
options,
emitStepEvent: this.#emitStepEvent
});
}

#traceTransaction = async (
Expand Down
Loading

0 comments on commit bc77b3b

Please sign in to comment.