-
Notifications
You must be signed in to change notification settings - Fork 846
Expand file tree
/
Copy pathutils.ts
More file actions
908 lines (871 loc) · 32.9 KB
/
utils.ts
File metadata and controls
908 lines (871 loc) · 32.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
/* eslint-disable @typescript-eslint/ban-ts-comment */
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'fs'
import * as http from 'http'
import { homedir } from 'os'
import * as path from 'path'
import * as readline from 'readline'
import * as url from 'url'
import {
Chain,
Common,
Hardfork,
Mainnet,
createCommonFromGethGenesis,
createCustomCommon,
getPresetChainConfig,
parseGethGenesisState,
} from '@ethereumjs/common'
import {
EthereumJSErrorWithoutCode,
bytesToBigInt,
bytesToHex,
calculateSigRecovery,
concatBytes,
createAddressFromPrivateKey,
createAddressFromString,
ecrecover,
hexToBytes,
randomBytes,
setLengthLeft,
} from '@ethereumjs/util'
import { trustedSetup } from '@paulmillr/trusted-setups/fast.js'
import {
keccak256 as keccak256WASM,
secp256k1Expand,
secp256k1Recover,
secp256k1Sign,
waitReady as waitReadyPolkadotSha256,
sha256 as wasmSha256,
} from '@polkadot/wasm-crypto'
import { keccak256 } from 'ethereum-cryptography/keccak.js'
import { secp256k1 } from 'ethereum-cryptography/secp256k1.js'
import { sha256 } from 'ethereum-cryptography/sha256.js'
import { KZG as microEthKZG } from 'micro-eth-signer/kzg'
import * as verkle from 'micro-eth-signer/verkle'
import * as promClient from 'prom-client'
import * as yargs from 'yargs'
import { hideBin } from 'yargs/helpers'
import { Config, SyncMode } from '../src/config.ts'
import { getLogger } from '../src/logging.ts'
import { Event } from '../src/types.ts'
import { parseMultiaddrs } from '../src/util/index.ts'
import { setupMetrics } from '../src/util/metrics.ts'
import type { CustomCrypto, GenesisState, GethGenesis } from '@ethereumjs/common'
import type { Address, PrefixedHexString } from '@ethereumjs/util'
import type { Logger } from '../src/logging.ts'
import type { ClientOpts } from '../src/types.ts'
export type Account = [address: Address, privateKey: Uint8Array]
const networks = Object.keys(Chain).map((network) => network.toLowerCase())
export function getArgs(): ClientOpts {
return (
yargs
.default(hideBin(process.argv))
.parserConfiguration({
'dot-notation': false,
})
.option('network', {
describe: 'Network',
choices: networks,
coerce: (arg: string) => arg.toLowerCase(),
default: 'mainnet',
})
.option('chainId', {
describe: 'Chain ID',
choices: Object.entries(Chain)
.map((n) => (typeof n[1] === 'string' ? parseInt(n[1]) : n[1]))
.filter((el) => !isNaN(el)),
default: undefined,
conflicts: ['customChain', 'customGenesisState', 'gethGenesis'], // Disallows custom chain data and chainId
})
.option('networkId', {
describe: 'Network ID',
deprecated: true,
deprecate: 'use --chainId instead',
choices: Object.entries(Chain)
.map((n) => (typeof n[1] === 'string' ? parseInt(n[1]) : n[1]))
.filter((el) => !isNaN(el)),
default: undefined,
conflicts: ['customChain', 'customGenesisState', 'gethGenesis'], // Disallows custom chain data and networkId
})
.option('sync', {
describe: 'Blockchain sync mode (light sync experimental)',
choices: Object.values(SyncMode),
default: Config.SYNCMODE_DEFAULT,
})
.option('dataDir', {
describe: 'Data directory for the blockchain',
default: `${homedir()}/Library/Ethereum/ethereumjs`,
})
.option('customChain', {
describe: 'Path to custom chain parameters json file (@ethereumjs/common format)',
coerce: (arg: string) => (arg ? path.resolve(arg) : undefined),
implies: 'customGenesisState',
})
.option('customGenesisState', {
describe: 'Path to custom genesis state json file (@ethereumjs/common format)',
coerce: (arg: string) => (arg ? path.resolve(arg) : undefined),
implies: 'customChain',
})
.option('verkleGenesisStateRoot', {
describe: 'State root of the verkle genesis genesis (experimental)',
string: true,
coerce: (customGenesisStateRoot: PrefixedHexString) => hexToBytes(customGenesisStateRoot),
})
.option('gethGenesis', {
describe: 'Import a geth genesis file for running a custom network',
coerce: (arg: string) => (arg ? path.resolve(arg) : undefined),
})
.option('trustedSetup', {
describe: 'A custom trusted setup txt file for initializing the kzg library',
coerce: (arg: string) => (arg ? path.resolve(arg) : undefined),
})
.option('bootnodes', {
describe:
'Comma-separated list of network bootnodes (format: "enode://<id>@<host:port>,enode://..." ("[?discport=<port>]" not supported) or path to a bootnode.txt file',
array: true,
coerce: (arr) => arr.filter((el: any) => el !== undefined).map((el: any) => el.toString()),
})
.option('port', {
describe: 'RLPx listening port',
default: Config.PORT_DEFAULT,
})
.option('extIP', {
describe: 'RLPx external IP',
string: true,
})
.option('multiaddrs', {
describe: 'Network multiaddrs',
array: true,
coerce: (arr) => arr.filter((el: any) => el !== undefined).map((el: any) => el.toString()),
})
.option('rpc', {
describe: 'Enable the JSON-RPC server with HTTP endpoint',
boolean: true,
})
.option('rpcPort', {
describe: 'HTTP-RPC server listening port',
default: 8545,
})
.option('rpcAddr', {
describe: 'HTTP-RPC server listening interface address',
default: 'localhost',
})
.option('ws', {
describe: 'Enable the JSON-RPC server with WS endpoint',
boolean: true,
})
.option('wsPort', {
describe: 'WS-RPC server listening port',
default: 8546,
})
.option('wsAddr', {
describe: 'WS-RPC server listening address',
default: 'localhost',
})
.option('rpcEngine', {
describe: 'Enable the JSON-RPC server for Engine namespace',
boolean: true,
})
.option('rpcEnginePort', {
describe: 'HTTP-RPC server listening port for Engine namespace',
number: true,
default: 8551,
})
.option('rpcEngineAddr', {
describe: 'HTTP-RPC server listening interface address for Engine namespace',
string: true,
default: 'localhost',
})
.option('wsEnginePort', {
describe: 'WS-RPC server listening port for Engine namespace',
number: true,
default: 8552,
})
.option('wsEngineAddr', {
describe: 'WS-RPC server listening interface address for Engine namespace',
string: true,
default: 'localhost',
})
.option('rpcEngineAuth', {
describe: 'Enable jwt authentication for Engine RPC server',
boolean: true,
default: true,
})
.option('rpcEthMaxPayload', {
describe: 'Define max JSON payload size for eth/debug RPC requests',
string: true,
default: Config.RPC_ETH_MAXPAYLOAD_DEFAULT,
})
.option('rpcEngineMaxPayload', {
describe: 'Define max JSON payload size for engine RPC requests',
string: true,
default: Config.RPC_ENGINE_MAXPAYLOAD_DEFAULT,
})
.option('jwtSecret', {
describe: 'Provide a file containing a hex encoded jwt secret for Engine RPC server',
coerce: (arg: string) => (arg ? path.resolve(arg) : undefined),
})
.option('helpRPC', {
describe: 'Display the JSON RPC help with a list of all RPC methods implemented (and exit)',
boolean: true,
})
.option('logLevel', {
describe: 'Logging verbosity',
choices: ['error', 'warn', 'info', 'debug', 'off'],
default: 'info',
})
.option('logFile', {
describe:
'File to save log file (default logs to `$dataDir/ethereumjs.log`, pass false to disable)',
default: true,
})
.option('logLevelFile', {
describe: 'Log level for logFile',
choices: ['error', 'warn', 'info', 'debug'],
default: 'debug',
})
.option('logRotate', {
describe: 'Rotate log file daily',
boolean: true,
default: true,
})
.option('logMaxFiles', {
describe: 'Maximum number of log files when rotating (older will be deleted)',
number: true,
default: 5,
})
.option('prometheus', {
describe: 'Enable the Prometheus metrics server with HTTP endpoint',
boolean: true,
default: false,
})
.option('prometheusPort', {
describe: 'Enable the Prometheus metrics server with HTTP endpoint',
number: true,
default: 8000,
})
.option('rpcDebug', {
describe:
'Additionally log truncated RPC calls filtered by name (prefix), e.g.: "eth,engine_getPayload" (use "all" for all methods). Truncated by default, add verbosity using "rpcDebugVerbose"',
default: '',
string: true,
})
.option('rpcDebugVerbose', {
describe:
'Additionally log complete RPC calls filtered by name (prefix), e.g.: "eth,engine_getPayload" (use "all" for all methods).',
default: '',
string: true,
})
.option('rpcCors', {
describe: 'Configure the Access-Control-Allow-Origin CORS header for RPC server',
string: true,
default: '*',
})
.option('maxPerRequest', {
describe: 'Max items per block or header request',
number: true,
default: Config.MAXPERREQUEST_DEFAULT,
})
.option('maxFetcherJobs', {
describe: 'Max tasks or jobs to be created for a fetcher at a time',
number: true,
default: Config.MAXFETCHERJOBS_DEFAULT,
})
.option('minPeers', {
describe: 'Peers needed before syncing',
number: true,
default: Config.MINPEERS_DEFAULT,
})
.option('maxPeers', {
describe: 'Maximum peers to sync with',
number: true,
default: Config.MAXPEERS_DEFAULT,
})
.option('dnsAddr', {
describe: 'IPv4 address of DNS server to use when acquiring peer discovery targets',
string: true,
default: Config.DNSADDR_DEFAULT,
})
.option('dnsNetworks', {
describe: 'EIP-1459 ENR tree urls to query for peer discovery targets',
array: true,
coerce: (arr) => arr.filter((el: any) => el !== undefined).map((el: any) => el.toString()),
})
.option('execution', {
describe: 'Start continuous VM execution (pre-Merge setting)',
boolean: true,
default: Config.EXECUTION,
})
.option('numBlocksPerIteration', {
describe: 'Number of blocks to execute in batch mode and logged to console',
number: true,
default: Config.NUM_BLOCKS_PER_ITERATION,
})
.option('executeBlocks', {
describe:
'Debug mode for reexecuting existing blocks (no services will be started), allowed input formats: 5,5-10',
string: true,
})
.option('accountCache', {
describe: 'Size for the account cache (max number of accounts)',
number: true,
default: Config.ACCOUNT_CACHE,
})
.option('storageCache', {
describe: 'Size for the storage cache (max number of contracts)',
number: true,
default: Config.STORAGE_CACHE,
})
.option('codeCache', {
describe: 'Size for the code cache (max number of contracts)',
number: true,
default: Config.CODE_CACHE,
})
.option('trieCache', {
describe: 'Size for the trie cache (max number of trie nodes)',
number: true,
default: Config.TRIE_CACHE,
})
.option('debugCode', {
describe: 'Generate code for local debugging (internal usage mostly)',
boolean: true,
default: Config.DEBUGCODE_DEFAULT,
})
.option('discDns', {
describe: 'Query EIP-1459 DNS TXT records for peer discovery',
boolean: true,
})
.option('discV4', {
describe: 'Use v4 ("findneighbour" node requests) for peer discovery',
boolean: true,
})
.option('mine', {
describe: 'Enable private custom network mining (beta)',
boolean: true,
})
.option('unlock', {
describe: `Path to file where private key (without 0x) is stored or comma separated list of accounts to unlock -
currently only the first account is used (for sealing PoA blocks and as the default coinbase).
You will be prompted for a 0x-prefixed private key if you pass a list of accounts
FOR YOUR SAFETY PLEASE DO NOT USE ANY ACCOUNTS HOLDING SUBSTANTIAL AMOUNTS OF ETH`,
string: true,
})
.option('dev', {
describe: 'Start an ephemeral PoA blockchain with a single miner and prefunded accounts',
choices: ['false', 'true', 'poa', 'pow'],
})
.option('minerCoinbase', {
describe:
'Address for mining rewards (etherbase). If not provided, defaults to the primary account',
string: true,
coerce: (coinbase) => createAddressFromString(coinbase),
})
.option('saveReceipts', {
describe:
'Save tx receipts and logs in the meta db (warning: may use a large amount of storage). With `--rpc` allows querying via eth_getLogs (max 10000 logs per request) and eth_getTransactionReceipt (within `--txLookupLimit`)',
boolean: true,
default: true,
})
.option('snap', {
describe: 'Enable snap state sync (for testing and development purposes)',
boolean: true,
})
.option('prefixStorageTrieKeys', {
describe:
'Enable/Disable storage trie prefixes (specify `false` for backward compatibility with previous states synced without prefixes)',
boolean: true,
default: true,
deprecated:
'Support for `--prefixStorageTrieKeys=false` is temporary. Please sync new instances with `prefixStorageTrieKeys` enabled',
})
.options('useStringValueTrieDB', {
describe:
'Use string values in the trie DB. This is old behavior, new behavior uses Uint8Arrays in the DB (more performant)',
boolean: true,
default: false,
deprecated:
'Usage of old DBs which uses string-values is temporary. Please sync new instances without this option.',
})
.option('txLookupLimit', {
describe:
'Number of recent blocks to maintain transactions index for (default = about one year, 0 = entire chain)',
number: true,
default: 2350000,
})
.option('startBlock', {
describe:
'Block number to start syncing from. Must be lower than the local chain tip. Note: this is destructive and removes blocks from the blockchain, please back up your datadir before using.',
number: true,
})
.option('startExecutionFrom', {
describe:
'Block number to start/restart execution from. For merkle based state, parent state should be present in the the db while in verkle stateless mode the chain should be synced till the block and witnesses available this block onwards',
number: true,
})
.option('startExecution', {
describe:
'Start execution of unexecuted blocks without waiting for the CL fcU, set to `true` if `startExecutionFrom` provided',
boolean: true,
})
.option('isSingleNode', {
describe:
'To run client in single node configuration without need to discover the sync height from peer. Particularly useful in test configurations. This flag is automatically activated in the "dev" mode',
boolean: true,
})
.option('vmProfileBlocks', {
describe: 'Report the VM profile after running each block',
boolean: true,
default: false,
})
.option('vmProfileTxs', {
describe: 'Report the VM profile after running each tx',
boolean: true,
default: false,
})
.option('loadBlocksFromRlp', {
describe: 'path to a file of RLP encoded blocks',
string: true,
array: true,
})
.option('pruneEngineCache', {
describe:
'Enable/Disable pruning engine block cache (disable for testing against hive etc)',
boolean: true,
default: true,
})
.option('statelessVerkle', {
describe: 'Run verkle+ hardforks using stateless verkle stateManager (experimental)',
boolean: true,
default: false,
})
.option('statefulVerkle', {
describe: 'Run verkle+ hardforks using stateful verkle stateManager (experimental)',
boolean: true,
default: false,
})
.option('engineNewpayloadMaxExecute', {
describe:
'Number of unexecuted blocks (including ancestors) that can be executed per-block in engine`s new payload (if required and possible) to determine the validity of the block',
number: true,
})
.option('skipEngineExec', {
describe:
'Skip executing blocks in new payload calls in engine, alias for --engineNewpayloadMaxExecute=0 and overrides any engineNewpayloadMaxExecute if also provided',
boolean: true,
})
.option('ignoreStatelessInvalidExecs', {
describe:
'Ignore stateless execution failures and keep moving the vm execution along using execution witnesses available in block (verkle). Sets/overrides --statelessVerkle=true and --engineNewpayloadMaxExecute=0 to prevent engine newPayload direct block execution where block execution failures may stall the CL client. Useful for debugging the verkle. The invalid blocks will be stored in dataDir/network/invalidPayloads which one may use later for debugging',
boolean: true,
hidden: true,
})
.option('useJsCrypto', {
describe: 'Use pure Javascript cryptography functions',
boolean: true,
default: false,
})
.completion()
// strict() ensures that yargs throws when an invalid arg is provided
.strict()
.check((argv, _options) => {
const usedPorts = new Set()
let collision = false
if (argv.ws === true) {
usedPorts.add(argv.wsPort)
if (!usedPorts.has(argv.wsEnginePort)) {
usedPorts.add(argv.wsEnginePort)
}
}
if (argv.rpc === true && usedPorts.has(argv.rpcPort)) collision = true
if (argv.rpcEngine === true && usedPorts.has(argv.rpcEnginePort)) collision = true
if (collision) throw EthereumJSErrorWithoutCode('cannot reuse ports between RPC instances')
return true
})
.parseSync()
)
}
/**
* Returns a configured blockchain and common for devnet with a prefunded address
*/
async function setupDevnet(prefundAddress: Address, args: ClientOpts) {
const addr = prefundAddress.toString().slice(2)
const consensusConfig =
args.dev === 'pow'
? { ethash: true }
: {
clique: {
period: 10,
epoch: 30000,
},
}
const defaultChainData: GethGenesis = {
config: {
chainId: 123456,
homesteadBlock: 0,
eip150Block: 0,
eip150Hash: '0x0000000000000000000000000000000000000000000000000000000000000000',
eip155Block: 0,
eip158Block: 0,
byzantiumBlock: 0,
constantinopleBlock: 0,
petersburgBlock: 0,
istanbulBlock: 0,
berlinBlock: 0,
londonBlock: 0,
...consensusConfig,
},
nonce: '0x0',
timestamp: '0x614b3731',
gasLimit: '0x47b760',
difficulty: '0x1',
mixHash: '0x0000000000000000000000000000000000000000000000000000000000000000',
coinbase: '0x0000000000000000000000000000000000000000',
number: '0x0',
gasUsed: '0x0',
parentHash: '0x0000000000000000000000000000000000000000000000000000000000000000',
baseFeePerGas: 7,
alloc: {},
}
const extraData =
args.dev === 'pow' ? '0x' + '0'.repeat(32) : '0x' + '0'.repeat(64) + addr + '0'.repeat(130)
const chainData = {
...defaultChainData,
extraData,
alloc: { [addr]: { balance: '0x10000000000000000000' } },
}
const common = createCommonFromGethGenesis(chainData, {
chain: 'devnet',
hardfork: Hardfork.London,
})
const customGenesisState = parseGethGenesisState(chainData)
return { common, customGenesisState }
}
/**
* Accept account input from command line
*/
async function inputAccounts(args: ClientOpts) {
const accounts: Account[] = []
const rl = readline.createInterface({
// @ts-ignore node/types has a mismatch and readline is typed incorrectly
input: process.stdin,
// @ts-ignore node/types has a mismatch and readline is typed incorrectly
output: process.stdout,
})
// Hide key input
// TODO: Investigate why type doesn't match & if this actually works
// @ts-expect-error -- Absent from type
rl['input'].on('keypress', function () {
// get the number of characters entered so far:
const len = (rl as any).line.length
// move cursor back to the beginning of the input:
readline.moveCursor((rl as any).output, -len, 0)
// clear everything to the right of the cursor:
readline.clearLine((rl as any).output, 1)
// replace the original input with asterisks:
for (let i = 0; i < len; i++) {
// TODO: Investigate why type doesn't match & if this actually works
// @ts-expect-error -- Absent from type
rl['output'].write('*')
}
})
const question = (text: string) => {
return new Promise<string>((resolve) => {
rl.question(text, resolve)
})
}
try {
const addresses = args.unlock!.split(',')
const isFile = existsSync(path.resolve(addresses[0]))
if (!isFile) {
for (const addressString of addresses) {
const address = createAddressFromString(addressString)
const inputKey = (await question(
`Please enter the 0x-prefixed private key to unlock ${address}:\n`,
)) as PrefixedHexString
// TODO: Investigate why type doesn't match & if this actually works
// @ts-expect-error -- -- Property not present on type
rl['history'] = rl['history'].slice(1)
const privKey = hexToBytes(inputKey)
const derivedAddress = createAddressFromPrivateKey(privKey)
if (address.equals(derivedAddress) === true) {
accounts.push([address, privKey])
} else {
throw EthereumJSErrorWithoutCode(
`Private key does not match for ${address} (address derived: ${derivedAddress})`,
)
}
}
} else {
const acc = readFileSync(path.resolve(args.unlock!), 'utf-8').replace(/(\r\n|\n|\r)/gm, '')
const privKey = hexToBytes(`0x${acc}`) // See docs: acc has to be non-zero prefixed in the file
const derivedAddress = createAddressFromPrivateKey(privKey)
accounts.push([derivedAddress, privKey])
}
} catch (e: any) {
throw EthereumJSErrorWithoutCode(`Encountered error unlocking account:\n${e.message}`)
}
rl.close()
return accounts
}
/**
* Returns a randomly generated account
*/
function generateAccount(): Account {
const privKey = randomBytes(32)
const address = createAddressFromPrivateKey(privKey)
/* eslint-disable no-console */
console.log('='.repeat(50))
console.log('Account generated for mining blocks:')
console.log(`Address: ${address}`)
console.log(`Private key: ${bytesToHex(privKey)}`)
console.log('WARNING: Do not use this account for mainnet funds')
console.log('='.repeat(50))
/* eslint-enable no-console */
return [address, privKey]
}
export async function getCryptoFunctions(useJsCrypto: boolean): Promise<CustomCrypto> {
const cryptoFunctions: CustomCrypto = {}
const kzg = new microEthKZG(trustedSetup)
// Initialize WASM crypto if JS crypto is not specified
if (useJsCrypto === false) {
await waitReadyPolkadotSha256()
cryptoFunctions.keccak256 = keccak256WASM
cryptoFunctions.ecrecover = (
msgHash: Uint8Array,
v: bigint,
r: Uint8Array,
s: Uint8Array,
chainID?: bigint,
) =>
secp256k1Expand(
secp256k1Recover(
msgHash,
concatBytes(setLengthLeft(r, 32), setLengthLeft(s, 32)),
Number(calculateSigRecovery(v, chainID)),
),
).slice(1)
cryptoFunctions.sha256 = wasmSha256
cryptoFunctions.ecsign = (msg: Uint8Array, pk: Uint8Array) => {
const buf = secp256k1Sign(msg, pk)
const r = bytesToBigInt(buf.slice(0, 32))
const s = bytesToBigInt(buf.slice(32, 64))
const recovery = buf[64]
return { r, s, recovery }
}
cryptoFunctions.ecdsaRecover = (sig: Uint8Array, recId: number, hash: Uint8Array) => {
return secp256k1Recover(hash, sig, recId)
}
} else {
cryptoFunctions.keccak256 = keccak256
cryptoFunctions.ecrecover = ecrecover
cryptoFunctions.sha256 = sha256
cryptoFunctions.ecsign = secp256k1.sign
cryptoFunctions.ecdsaRecover = (sig: Uint8Array, recId: number, hash: Uint8Array) => {
// Adapted from @noble/curves docs
const sign = secp256k1.Signature.fromCompact(sig)
const point = sign.addRecoveryBit(recId).recoverPublicKey(hash)
const address = point.toRawBytes(true)
return address
}
}
cryptoFunctions.kzg = kzg
cryptoFunctions.verkle = verkle
return cryptoFunctions
}
export async function generateClientConfig(args: ClientOpts) {
// Give chainId priority over networkId
// Give networkId precedence over network name
const chainName = args.chainId ?? args.networkId ?? args.network ?? Chain.Mainnet
const chain = getPresetChainConfig(chainName)
// `useJsCrypto` defaults to `false` in the CLI defaults
const cryptoFunctions = await getCryptoFunctions(args.useJsCrypto ?? false)
// Configure accounts for mining and prefunding in a local devnet
const accounts: Account[] = []
if (typeof args.unlock === 'string') {
accounts.push(...(await inputAccounts(args)))
}
let customGenesisState: GenesisState | undefined
let common = new Common({ chain, hardfork: Hardfork.Chainstart, customCrypto: cryptoFunctions })
if (args.dev === true || typeof args.dev === 'string') {
args.discDns = false
if (accounts.length === 0) {
// If generating new keys delete old chain data to prevent genesis block mismatch
rmSync(`${args.dataDir}/devnet`, { recursive: true, force: true })
// Create new account
accounts.push(generateAccount())
}
const prefundAddress = accounts[0][0]
;({ common, customGenesisState } = await setupDevnet(prefundAddress, args))
}
// Configure common based on args given
// Use custom chain parameters file if specified
if (typeof args.customChain === 'string') {
try {
const customChainParams = JSON.parse(readFileSync(args.customChain, 'utf-8'))
customGenesisState = JSON.parse(readFileSync(args.customGenesisState!, 'utf-8'))
common = createCustomCommon(customChainParams, Mainnet, {
customCrypto: cryptoFunctions,
})
} catch (err: any) {
throw EthereumJSErrorWithoutCode(`invalid chain parameters: ${err.message}`)
}
} else if (typeof args.gethGenesis === 'string') {
// Use geth genesis parameters file if specified
const genesisFile = JSON.parse(readFileSync(args.gethGenesis, 'utf-8'))
const chainName = path.parse(args.gethGenesis).base.split('.')[0]
common = createCommonFromGethGenesis(genesisFile, {
chain: chainName,
})
// @ts-expect-error -- Assign to read-only property
common.customCrypto = cryptoFunctions
customGenesisState = parseGethGenesisState(genesisFile)
}
if (args.mine === true && accounts.length === 0) {
throw EthereumJSErrorWithoutCode(
'Please provide an account to mine blocks with `--unlock [address]` or use `--dev` to generate',
)
}
const datadir = args.dataDir ?? Config.DATADIR_DEFAULT
const networkDir = `${datadir}/${common.chainName()}`
const configDirectory = `${networkDir}/config`
mkdirSync(configDirectory, {
recursive: true,
})
const invalidPayloadsDir = `${networkDir}/invalidPayloads`
mkdirSync(invalidPayloadsDir, {
recursive: true,
})
const key = await Config.getClientKey(datadir, common)
// logFile is either filename or boolean true or false to enable (with default) or disable
if (typeof args.logFile === 'boolean') {
args.logFile = args.logFile ? `${networkDir}/ethereumjs.log` : undefined
}
const logger: Logger | undefined = getLogger(args)
let bootnodes
if (args.bootnodes !== undefined) {
// File path passed, read bootnodes from disk
if (
Array.isArray(args.bootnodes) &&
args.bootnodes.length === 1 &&
args.bootnodes[0].includes('.txt')
) {
const file = readFileSync(args.bootnodes[0], 'utf-8')
let nodeURLs = file.split(/\r?\n/).filter((url) => (url !== '' ? true : false))
nodeURLs = nodeURLs.map((url) => {
const discportIndex = url.indexOf('?discport')
if (discportIndex > 0) {
return url.substring(0, discportIndex)
} else {
return url
}
})
bootnodes = parseMultiaddrs(nodeURLs)
logger?.info(`Reading bootnodes file=${args.bootnodes[0]} num=${nodeURLs.length}`)
} else {
bootnodes = parseMultiaddrs(args.bootnodes)
}
}
const multiaddrs = args.multiaddrs !== undefined ? parseMultiaddrs(args.multiaddrs) : undefined
const mine = args.mine ?? args.dev !== undefined
const isSingleNode = args.isSingleNode ?? args.dev !== undefined
let prometheusMetrics = undefined
let metricsServer: http.Server | undefined
if (args.prometheus === true) {
// Create custom metrics
prometheusMetrics = setupMetrics()
const register = new promClient.Registry()
register.setDefaultLabels({
app: 'ethereumjs-client',
})
promClient.collectDefaultMetrics({ register })
for (const [_, metric] of Object.entries(prometheusMetrics)) {
register.registerMetric(metric)
}
metricsServer = http.createServer(async (req, res) => {
if (req.url === undefined) {
res.statusCode = 400
res.end('Bad Request: URL is missing')
return
}
const reqUrl = new url.URL(req.url, `http://${req.headers.host}`)
const route = reqUrl.pathname
switch (route) {
case '/metrics':
// Return all metrics in the Prometheus exposition format
res.setHeader('Content-Type', register.contentType)
res.end(await register.metrics())
break
default:
res.statusCode = 404
res.end('Not found')
return
}
})
// Start the HTTP server which exposes the metrics on http://localhost:${args.prometheusPort}/metrics
logger?.info(`Starting Metrics Server on port ${args.prometheusPort}`)
metricsServer.listen(args.prometheusPort)
}
const config = new Config({
accounts,
bootnodes,
common,
datadir,
debugCode: args.debugCode,
discDns: args.discDns,
discV4: args.discV4,
dnsAddr: args.dnsAddr,
execution: args.execution,
numBlocksPerIteration: args.numBlocksPerIteration,
accountCache: args.accountCache,
storageCache: args.storageCache,
codeCache: args.codeCache,
trieCache: args.trieCache,
dnsNetworks: args.dnsNetworks,
extIP: args.extIP,
key,
logger,
maxPeers: args.maxPeers,
maxPerRequest: args.maxPerRequest,
maxFetcherJobs: args.maxFetcherJobs,
mine,
minerCoinbase: args.minerCoinbase,
isSingleNode,
vmProfileBlocks: args.vmProfileBlocks,
vmProfileTxs: args.vmProfileTxs,
minPeers: args.minPeers,
multiaddrs,
port: args.port,
saveReceipts: args.saveReceipts,
syncmode: args.sync,
prefixStorageTrieKeys: args.prefixStorageTrieKeys,
enableSnapSync: args.snap,
useStringValueTrieDB: args.useStringValueTrieDB,
txLookupLimit: args.txLookupLimit,
pruneEngineCache: args.pruneEngineCache,
statelessVerkle: args.ignoreStatelessInvalidExecs === true ? true : args.statelessVerkle,
statefulVerkle: args.statefulVerkle,
startExecution: args.startExecutionFrom !== undefined ? true : args.startExecution,
engineNewpayloadMaxExecute:
args.ignoreStatelessInvalidExecs === true || args.skipEngineExec === true
? 0
: args.engineNewpayloadMaxExecute,
ignoreStatelessInvalidExecs: args.ignoreStatelessInvalidExecs,
prometheusMetrics,
})
config.events.on(Event.SERVER_LISTENING, (details) => {
const networkDir = config.getNetworkDirectory()
// Write the transport into a file
try {
writeFileSync(`${networkDir}/${details.transport}`, details.url)
} catch (e) {
// In case dir is not really setup, mostly to take care of mockserver in test
config.logger?.error(`Error writing listener details to disk: ${(e as Error).message}`)
}
})
if (customGenesisState !== undefined) {
const numAccounts = Object.keys(customGenesisState).length
config.logger?.info(`Reading custom genesis state accounts=${numAccounts}`)
}
const customGenesisStateRoot = args.verkleGenesisStateRoot
return { config, customGenesisState, customGenesisStateRoot, metricsServer, common }
}