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

Commit 686ecfa

Browse files
committed
squamsh
1 parent b314f7c commit 686ecfa

File tree

3 files changed

+110
-8
lines changed

3 files changed

+110
-8
lines changed

src/chains/ethereum/ethereum/src/blockchain.ts

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ import type { Address as EthereumJsAddress } from "@ethereumjs/util";
2323
import type { InterpreterStep } from "@ethereumjs/evm";
2424
import { decode } from "@ganache/rlp";
2525
import { 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";
2731
import { EEI, VM } from "@ethereumjs/vm";
2832
import {
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+
109143
interface 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+
193243
export 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");

src/chains/ethereum/options/src/chain-options.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ export type ChainConfig = {
123123
hardfork: Hardfork;
124124
};
125125
};
126+
/**
127+
* Specify a relative filepath for a genesis.json to configure the @ethereumjs/vm
128+
*/
129+
readonly genesisPath: {
130+
type: string;
131+
};
126132

127133
/**
128134
* Whether to report runtime errors from EVM code as RPC errors.
@@ -140,6 +146,7 @@ export type ChainConfig = {
140146
};
141147
};
142148
};
149+
exclusiveGroups: [["chainId", "networkId", "genesisPath"]];
143150
};
144151

145152
export const ChainOptions: Definitions<ChainConfig> = {
@@ -164,7 +171,8 @@ export const ChainOptions: Definitions<ChainConfig> = {
164171
cliDescription: "The currently configured chain id.",
165172
default: () => 1337,
166173
legacyName: "chainId",
167-
cliType: "number"
174+
cliType: "number",
175+
conflicts: ["genesisPath"]
168176
},
169177
networkId: {
170178
normalize,
@@ -175,7 +183,8 @@ export const ChainOptions: Definitions<ChainConfig> = {
175183
"System time at process start or Network ID of forked blockchain if configured.",
176184
legacyName: "network_id",
177185
cliAliases: ["i", "networkId"],
178-
cliType: "number"
186+
cliType: "number",
187+
conflicts: ["genesisPath"]
179188
},
180189
time: {
181190
normalize: rawInput => (rawInput !== undefined ? new Date(rawInput) : null),
@@ -203,6 +212,13 @@ export const ChainOptions: Definitions<ChainConfig> = {
203212
cliType: "string",
204213
cliChoices: HARDFORKS as Writeable<typeof HARDFORKS>
205214
},
215+
genesisPath: {
216+
normalize,
217+
cliDescription: "Set a genesis.json file",
218+
cliAliases: ["j", "json"],
219+
cliType: "string",
220+
conflicts: ["networkId", "chainId"]
221+
},
206222
vmErrorsOnRPCResponse: {
207223
normalize,
208224
cliDescription:

src/packages/ganache/genesis.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"config": {
3+
"chainId": 19763,
4+
"homesteadBlock": 0,
5+
"eip150Block": 0,
6+
"eip155Block": 0,
7+
"eip158Block": 0,
8+
"byzantiumBlock": 0,
9+
"londonBlock": 0,
10+
"ethash": {}
11+
},
12+
"nonce": "0xdeadbeefdeadbeef",
13+
"timestamp": "0x0",
14+
"extraData": "0x0000000000000000000000000000000000000000000000000000000000000000",
15+
"gasLimit": "0x80000000",
16+
"difficulty": "0x20000",
17+
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
18+
"coinbase": "0x0000000000000000000000000000000000000000",
19+
"alloc": {
20+
"71562b71999873db5b286df957af199ec94617f7": {
21+
"balance": "0xffffffffffffffffffffffffff"
22+
}
23+
},
24+
"number": "0x0",
25+
"gasUsed": "0x0",
26+
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000"
27+
}

0 commit comments

Comments
 (0)