@@ -23,7 +23,11 @@ import type { Address as EthereumJsAddress } from "@ethereumjs/util";
2323import type { InterpreterStep } from "@ethereumjs/evm" ;
2424import { decode } from "@ganache/rlp" ;
2525import { KECCAK256_RLP } from "@ethereumjs/util" ;
26- import { Common } from "@ethereumjs/common" ;
26+ import {
27+ Common ,
28+ parseGethGenesis ,
29+ Hardfork as ejsHardfork
30+ } from "@ethereumjs/common" ;
2731import { EEI , VM } from "@ethereumjs/vm" ;
2832import {
2933 EvmError as VmError ,
@@ -106,6 +110,36 @@ type BlockchainTypedEvents = {
106110 stop : undefined ;
107111} ;
108112
113+ const json = {
114+ config : {
115+ chainId : 19763 ,
116+ homesteadBlock : 0 ,
117+ eip150Block : 0 ,
118+ eip155Block : 0 ,
119+ eip158Block : 0 ,
120+ byzantiumBlock : 0 ,
121+ londonBlock : 0 ,
122+ ethash : { }
123+ } ,
124+ nonce : "0xdeadbeefdeadbeef" ,
125+ timestamp : "0x0" ,
126+ extraData :
127+ "0x0000000000000000000000000000000000000000000000000000000000000000" ,
128+ gasLimit : "0x80000000" ,
129+ difficulty : "0x20000" ,
130+ mixHash : "0x0000000000000000000000000000000000000000000000000000000000000000" ,
131+ coinbase : "0x0000000000000000000000000000000000000000" ,
132+ alloc : {
133+ "71562b71999873db5b286df957af199ec94617f7" : {
134+ balance : "0xffffffffffffffffffffffffff"
135+ }
136+ } ,
137+ number : "0x0" ,
138+ gasUsed : "0x0" ,
139+ parentHash :
140+ "0x0000000000000000000000000000000000000000000000000000000000000000"
141+ } ;
142+
109143interface Logger {
110144 log ( message ?: any , ...optionalParams : any [ ] ) : void ;
111145}
@@ -190,6 +224,22 @@ function createCommon(chainId: number, networkId: number, hardfork: Hardfork) {
190224 return common ;
191225}
192226
227+ function createCommonFromGethGenesis ( path : string , hardfork : Hardfork ) {
228+ let json ;
229+ try {
230+ json = require ( path ) ;
231+ } catch ( e ) {
232+ console . log ( "Error: Could not load genesis.json at " + path ) ;
233+ }
234+ // .fromGethGenesis treats 'chain' the same as .custom treats 'name'.
235+ const common = Common . fromGethGenesis ( json , { chain : "ganache" , hardfork } ) ;
236+
237+ // as in createCommon, we do not support changing hardforks, remove hardfork listeners.
238+ ( common . on as any ) = ( ) => { } ;
239+
240+ return common ;
241+ }
242+
193243export default class Blockchain extends Emittery < BlockchainTypedEvents > {
194244 #state: Status = Status . starting ;
195245 #miner: Miner ;
@@ -259,11 +309,20 @@ export default class Blockchain extends Emittery<BlockchainTypedEvents> {
259309 options . chain . chainId = Number ( common . chainId ( ) ) ;
260310 } else {
261311 await database . initialize ( ) ;
262- common = this . common = createCommon (
263- options . chain . chainId ,
264- options . chain . networkId ,
265- options . chain . hardfork
266- ) ;
312+
313+ if ( options . chain . genesisPath ) {
314+ // From here, a valid path to a genesis.json runs -> unsure if/what breaks in ganache
315+ common = this . common = createCommonFromGethGenesis (
316+ process . cwd ( ) + "/" + options . chain . genesisPath ,
317+ options . chain . hardfork
318+ ) ;
319+ } else {
320+ common = this . common = createCommon (
321+ options . chain . chainId ,
322+ options . chain . networkId ,
323+ options . chain . hardfork
324+ ) ;
325+ }
267326 }
268327
269328 this . isPostMerge = this . common . gteHardfork ( "merge" ) ;
0 commit comments