forked from ethereumjs/ethereumjs-monorepo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.ts
executable file
·676 lines (636 loc) · 20.6 KB
/
cli.ts
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
#!/usr/bin/env node
import { homedir } from 'os'
import path from 'path'
import readline from 'readline'
import { randomBytes } from 'crypto'
import { ensureDirSync, readFileSync, removeSync } from 'fs-extra'
import { Server as RPCServer } from 'jayson/promise'
import Common, { Chain, Hardfork } from '@ethereumjs/common'
import { _getInitializedChains } from '@ethereumjs/common/dist/chains'
import { Address, toBuffer } from 'ethereumjs-util'
import { parseMultiaddrs, parseGenesisState, parseCustomParams, inspectParams } from '../lib/util'
import EthereumClient from '../lib/client'
import { Config, DataDirectory } from '../lib/config'
import { Logger, getLogger } from '../lib/logging'
import { RPCManager } from '../lib/rpc'
import * as modules from '../lib/rpc/modules'
import type { Chain as IChain, GenesisState } from '@ethereumjs/common/dist/types'
const level = require('level')
const yargs = require('yargs/yargs')
const { hideBin } = require('yargs/helpers')
type Account = [address: Address, privateKey: Buffer]
const networks = Object.entries(_getInitializedChains().names)
let logger: Logger
const args = yargs(hideBin(process.argv))
.option('network', {
describe: 'Network',
choices: networks.map((n) => n[1]),
default: 'mainnet',
})
.option('network-id', {
describe: 'Network ID',
choices: networks.map((n) => parseInt(n[0])),
default: undefined,
})
.option('syncmode', {
describe: 'Blockchain sync mode (light sync experimental)',
choices: ['light', 'full'],
default: Config.SYNCMODE_DEFAULT,
})
.option('lightserv', {
describe: 'Serve light peer requests',
boolean: true,
default: Config.LIGHTSERV_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),
})
.option('customGenesisState', {
describe: 'Path to custom genesis state json file (@ethereumjs/common format)',
coerce: (arg: string) => (arg ? path.resolve(arg) : undefined),
})
.option('gethGenesis', {
describe: 'Import a geth genesis file for running a custom network',
coerce: (arg: string) => (arg ? path.resolve(arg) : undefined),
})
.option('transports', {
describe: 'Network transports',
default: Config.TRANSPORTS_DEFAULT,
array: true,
})
.option('bootnodes', {
describe: 'Network bootnodes',
array: true,
})
.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,
})
.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: 8544,
})
.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: 8550,
})
.option('rpcEngineAddr', {
describe: 'HTTP-RPC server listening interface address for Engine namespace',
string: true,
default: 'localhost',
})
.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'],
default: 'info',
})
.option('logFile', {
describe: 'File to save log file (pass true for `ethereumjs.log`)',
})
.option('logLevelFile', {
describe: 'Log level for logFile',
choices: ['error', 'warn', 'info', 'debug'],
default: 'info',
})
.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('rpcDebug', {
describe: 'Additionally log complete RPC calls on log level debug (i.e. --loglevel=debug)',
boolean: true,
})
.option('maxPerRequest', {
describe: 'Max items per block or header request',
number: true,
default: Config.MAXPERREQUEST_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,
})
.option('executeBlocks', {
describe:
'Debug mode for reexecuting existing blocks (no services will be started), allowed input formats: 5,5-10',
string: true,
})
.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,
default: false,
})
.option('unlock', {
describe:
'Comma separated list of accounts to unlock - currently only the first account is used (for sealing PoA blocks and as the default coinbase). Beta, you will be promped for a 0x-prefixed private key until keystore functionality is added - FOR YOUR SAFETY PLEASE DO NOT USE ANY ACCOUNTS HOLDING SUBSTANTIAL AMOUNTS OF ETH',
string: true,
array: true,
})
.option('dev', {
describe: 'Start an ephemeral PoA blockchain with a single miner and prefunded accounts',
choices: [undefined, false, true, 'poa', 'pow'],
})
.option('minerCoinbase', {
describe:
'Address for mining rewards (etherbase). If not provided, defaults to the primary account',
string: true,
})
.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,
})
.option('txLookupLimit', {
describe:
'Number of recent blocks to maintain transactions index for (default = about one year, 0 = entire chain)',
number: true,
default: 2350000,
}).argv
/**
* Initializes and returns the databases needed for the client
*/
function initDBs(config: Config) {
// Chain DB
const chainDataDir = config.getDataDirectory(DataDirectory.Chain)
ensureDirSync(chainDataDir)
const chainDB = level(chainDataDir)
// State DB
const stateDataDir = config.getDataDirectory(DataDirectory.State)
ensureDirSync(stateDataDir)
const stateDB = level(stateDataDir)
// Meta DB (receipts, logs, indexes)
let metaDB
if (args.saveReceipts) {
const metaDataDir = config.getDataDirectory(DataDirectory.Meta)
ensureDirSync(metaDataDir)
metaDB = level(metaDataDir)
}
return { chainDB, stateDB, metaDB }
}
/**
* Special block execution debug mode (does not change any state)
*/
async function executeBlocks(client: EthereumClient) {
let first = 0
let last = 0
let txHashes = []
try {
const blockRange = (args.executeBlocks as string).split('-').map((val) => {
const reNum = /([0-9]+)/.exec(val)
const num = reNum ? parseInt(reNum[1]) : 0
const reTxs = /[0-9]+\[(.*)\]/.exec(val)
const txs = reTxs ? reTxs[1].split(',') : []
return [num, txs]
})
first = blockRange[0][0] as number
last = blockRange.length === 2 ? (blockRange[1][0] as number) : first
txHashes = blockRange[0][1] as string[]
if ((blockRange[0][1] as string[]).length > 0 && blockRange.length === 2) {
throw new Error('wrong input')
}
} catch (e: any) {
client.config.logger.error(
'Wrong input format for block execution, allowed format types: 5, 5-10, 5[0xba4b5fd92a26badad3cad22eb6f7c7e745053739b5f5d1e8a3afb00f8fb2a280,[TX_HASH_2],...], 5[*] (all txs in verbose mode)'
)
process.exit()
}
await client.executeBlocks(first, last, txHashes)
}
/**
* Starts and returns the {@link EthereumClient}
*/
async function startClient(config: Config) {
config.logger.info(`Data directory: ${config.datadir}`)
if (config.lightserv) {
config.logger.info(`Serving light peer requests`)
}
const dbs = initDBs(config)
const client = new EthereumClient({
config,
...dbs,
})
await client.open()
if (args.executeBlocks) {
// Special block execution debug mode (does not change any state)
await executeBlocks(client)
} else {
// Regular client start
await client.start()
}
return client
}
/**
* Starts and returns enabled RPCServers
*/
function startRPCServers(client: EthereumClient) {
const config = client.config
const onRequest = (request: any) => {
let msg = ''
if (args.rpcDebug) {
msg += `${request.method} called with params:\n${inspectParams(request.params)}`
} else {
msg += `${request.method} called with params: ${inspectParams(request.params, 125)}`
}
config.logger.debug(msg)
}
const handleResponse = (request: any, response: any, batchAddOn = '') => {
let msg = ''
if (args.rpcDebug) {
msg = `${request.method}${batchAddOn} responded with:\n${inspectParams(response)}`
} else {
msg = `${request.method}${batchAddOn} responded with: `
if (response.result) {
msg += inspectParams(response, 125)
}
if (response.error) {
msg += `error: ${response.error.message}`
}
}
config.logger.debug(msg)
}
const onBatchResponse = (request: any, response: any) => {
// Batch request
if (request.length !== undefined) {
if (response.length === undefined || response.length !== request.length) {
config.logger.debug('Invalid batch request received.')
return
}
for (let i = 0; i < request.length; i++) {
handleResponse(request[i], response[i], ' (batch request)')
}
} else {
handleResponse(request, response)
}
}
const servers: RPCServer[] = []
const { rpc, rpcaddr, rpcport, ws, wsPort, wsAddr, rpcEngine, rpcEngineAddr, rpcEnginePort } =
args
const manager = new RPCManager(client, config)
if (rpc || ws) {
const methods =
rpcEngine && rpcEnginePort === rpcport && rpcEngineAddr === rpcaddr
? { ...manager.getMethods(), ...manager.getMethods(true) }
: { ...manager.getMethods() }
const server = new RPCServer(methods)
server.on('request', onRequest)
server.on('response', onBatchResponse)
const namespaces = [...new Set(Object.keys(methods).map((m) => m.split('_')[0]))].join(',')
if (rpc) {
server.http().listen(rpcport)
config.logger.info(
`Started JSON RPC Server address=http://${rpcaddr}:${rpcport} namespaces=${namespaces}`
)
}
if (ws) {
server.websocket({ port: wsPort })
config.logger.info(
`Started JSON RPC Server address=ws://${wsAddr}:${wsPort} namespaces=${namespaces}`
)
}
servers.push(server)
}
if (rpcEngine) {
if (rpc && rpcport === rpcEnginePort && rpcaddr === rpcEngineAddr) {
return servers
}
const server = new RPCServer(manager.getMethods(true))
config.logger.info(
`Started JSON RPC server address=http://${rpcEngineAddr}:${rpcEnginePort} namespaces=engine`
)
server.http().listen(rpcEnginePort)
server.on('request', onRequest)
server.on('response', onBatchResponse)
servers.push(server)
}
return servers
}
/**
* Returns a configured common for devnet with a prefunded address
*/
async function setupDevnet(prefundAddress: Address) {
const addr = prefundAddress.toString().slice(2)
const consensusConfig =
args.dev === 'pow'
? { ethash: true }
: {
clique: {
period: 10,
epoch: 30000,
},
}
const defaultChainData = {
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,
}
const extraData = '0x' + '0'.repeat(64) + addr + '0'.repeat(130)
const chainData = {
...defaultChainData,
extraData,
alloc: { [addr]: { balance: '0x10000000000000000000' } },
}
const chainParams = await parseCustomParams(chainData, 'devnet')
const genesisState = await parseGenesisState(chainData)
const customChainParams: [IChain, GenesisState][] = [[chainParams, genesisState]]
return new Common({
chain: 'devnet',
customChains: customChainParams,
hardfork: Hardfork.London,
})
}
/**
* Accept account input from command line
*/
async function inputAccounts() {
const accounts: Account[] = []
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
})
// Hide key input
;(rl as any).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++) {
;(rl as any).output.write('*')
}
})
const question = (text: string) => {
return new Promise<string>((resolve) => {
rl.question(text, resolve)
})
}
try {
for (const addressString of args.unlock) {
const address = Address.fromString(addressString)
const inputKey = await question(
`Please enter the 0x-prefixed private key to unlock ${address}:\n`
)
;(rl as any).history = (rl as any).history.slice(1)
const privKey = toBuffer(inputKey)
const derivedAddress = Address.fromPrivateKey(privKey)
if (address.equals(derivedAddress)) {
accounts.push([address, privKey])
} else {
console.error(
`Private key does not match for ${address} (address derived: ${derivedAddress})`
)
process.exit()
}
}
} catch (e: any) {
console.error(`Encountered error unlocking account:\n${e.message}`)
process.exit()
}
rl.close()
return accounts
}
/**
* Output RPC help and exit
*/
function helprpc() {
console.log('-'.repeat(27))
console.log('JSON-RPC: Supported Methods')
console.log('-'.repeat(27))
console.log()
for (const modName of modules.list) {
console.log(`${modName}:`)
const methods = RPCManager.getMethodNames((modules as any)[modName])
for (const methodName of methods) {
console.log(`-> ${modName.toLowerCase()}_${methodName}`)
}
console.log()
}
console.log()
process.exit()
}
/**
* Returns a randomly generated account
*/
function generateAccount(): Account {
const privKey = randomBytes(32)
const address = Address.fromPrivateKey(privKey)
console.log('='.repeat(50))
console.log('Account generated for mining blocks:')
console.log(`Address: ${address}`)
console.log(`Private key: 0x${privKey.toString('hex')}`)
console.log('WARNING: Do not use this account for mainnet funds')
console.log('='.repeat(50))
return [address, privKey]
}
/**
* Main entry point to start a client
*/
async function run() {
if (args.helprpc) {
// Output RPC help and exit
return helprpc()
}
// Give network id precedence over network name
const chain = args.networkId ?? args.network ?? Chain.Mainnet
// Configure accounts for mining and prefunding in a local devnet
const accounts: Account[] = []
if (args.unlock) {
accounts.push(...(await inputAccounts()))
}
let common = new Common({ chain, hardfork: Hardfork.Chainstart })
if (args.dev) {
args.discDns = false
if (accounts.length === 0) {
// If generating new keys delete old chain data to prevent genesis block mismatch
removeSync(`${args.datadir}/devnet`)
// Create new account
accounts.push(generateAccount())
}
const prefundAddress = accounts[0][0]
common = await setupDevnet(prefundAddress)
}
// Configure common based on args given
if (
(args.customChainParams || args.customGenesisState || args.gethGenesis) &&
(!(args.network === 'mainnet') || args.networkId)
) {
console.error('cannot specify both custom chain parameters and preset network ID')
process.exit()
}
// Use custom chain parameters file if specified
if (args.customChain) {
if (!args.customGenesisState) {
console.error('cannot have custom chain parameters without genesis state')
process.exit()
}
try {
const customChainParams = JSON.parse(readFileSync(args.customChain, 'utf-8'))
const genesisState = JSON.parse(readFileSync(args.customGenesisState, 'utf-8'))
common = new Common({
chain: customChainParams.name,
customChains: [[customChainParams, genesisState]],
})
} catch (err: any) {
console.error(`invalid chain parameters: ${err.message}`)
process.exit()
}
} else if (args.gethGenesis) {
// 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]
const genesisParams = await parseCustomParams(genesisFile, chainName)
const genesisState = genesisFile.alloc ? await parseGenesisState(genesisFile) : {}
common = new Common({
chain: genesisParams.name,
customChains: [[genesisParams, genesisState]],
})
}
if (args.mine && accounts.length === 0) {
console.error(
'Please provide an account to mine blocks with `--unlock [address]` or use `--dev` to generate'
)
process.exit()
}
const datadir = args.datadir ?? Config.DATADIR_DEFAULT
const configDirectory = `${datadir}/${common.chainName()}/config`
ensureDirSync(configDirectory)
const key = await Config.getClientKey(datadir, common)
logger = getLogger(args)
const bootnodes = args.bootnodes ? parseMultiaddrs(args.bootnodes) : undefined
const multiaddrs = args.multiaddrs ? parseMultiaddrs(args.multiaddrs) : undefined
const config = new Config({
accounts,
bootnodes,
common,
datadir,
debugCode: args.debugCode,
discDns: args.discDns,
discV4: args.discV4,
dnsAddr: args.dnsAddr,
dnsNetworks: args.dnsNetworks,
extIP: args.extIP,
key,
lightserv: args.lightserv,
logger,
maxPeers: args.maxPeers,
maxPerRequest: args.maxPerRequest,
mine: args.mine || args.dev,
minerCoinbase: args.minerCoinbase,
minPeers: args.minPeers,
multiaddrs,
port: args.port,
saveReceipts: args.saveReceipts,
syncmode: args.syncmode,
transports: args.transports,
txLookupLimit: args.txLookupLimit,
})
config.events.setMaxListeners(50)
const client = await startClient(config)
const servers = args.rpc || args.rpcEngine ? startRPCServers(client) : []
process.on('SIGINT', async () => {
config.logger.info('Caught interrupt signal. Shutting down...')
servers.forEach((s) => s.http().close())
await client.stop()
config.logger.info('Exiting.')
process.exit()
})
}
run().catch((err) => logger?.error(err) ?? console.error(err))