-
Notifications
You must be signed in to change notification settings - Fork 783
/
Copy pathclient.ts
168 lines (143 loc) · 4.56 KB
/
client.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
import { Chain } from './blockchain/index.js'
import { FullEthereumService } from './service/index.js'
import { Event } from './types.js'
import { getPackageJSON } from './util/index.js'
import type { Config } from './config.js'
import type { MultiaddrLike } from './types.js'
import type { Blockchain } from '@ethereumjs/blockchain'
import type { GenesisState } from '@ethereumjs/util'
import type { AbstractLevel } from 'abstract-level'
export interface EthereumClientOptions {
/** Client configuration */
config: Config
/** Custom blockchain (optional) */
blockchain?: Blockchain
/**
* Database to store blocks and metadata.
* Should be an abstract-leveldown compliant store.
*
* Default: Database created by the Blockchain class
*/
chainDB?: AbstractLevel<string | Uint8Array, string | Uint8Array, string | Uint8Array>
/**
* Database to store the state.
* Should be an abstract-leveldown compliant store.
*
* Default: Database created by the MerklePatriciaTrie class
*/
stateDB?: AbstractLevel<string | Uint8Array, string | Uint8Array, string | Uint8Array>
/**
* Database to store tx receipts, logs, and indexes.
* Should be an abstract-leveldown compliant store.
*
* Default: Database created in datadir folder
*/
metaDB?: AbstractLevel<string | Uint8Array, string | Uint8Array, string | Uint8Array>
/* List of bootnodes to use for discovery */
bootnodes?: MultiaddrLike[]
/* List of supported clients */
clientFilter?: string[]
/* How often to discover new peers */
refreshInterval?: number
/* custom genesisState if any for the chain */
genesisState?: GenesisState
/* custom genesisStateRoot to be used with post verkle genesis for stateless runs */
genesisStateRoot?: Uint8Array
/* if client can be run stateless post verkle, defaults to true for now */
statelessVerkle?: boolean
}
/**
* Represents the top-level ethereum node, and is responsible for managing the
* lifecycle of included services.
* @memberof module:node
*/
export class EthereumClient {
public config: Config
public chain: Chain
public service: FullEthereumService
public opened: boolean
public started: boolean
/**
* Main entrypoint for client initialization.
*
* Safe creation of a Chain object awaiting the initialization
* of the underlying Blockchain object.
*/
public static async create(options: EthereumClientOptions) {
const chain = await Chain.create(options)
return new this(chain, options)
}
/**
* Create new node
*/
protected constructor(chain: Chain, options: EthereumClientOptions) {
this.config = options.config
this.chain = chain
this.service = new FullEthereumService({
config: this.config,
chainDB: options.chainDB,
stateDB: options.stateDB,
metaDB: options.metaDB,
chain,
})
this.opened = false
this.started = false
}
/**
* Open node. Must be called before node is started
*/
async open() {
if (this.opened) {
return false
}
const name = this.config.chainCommon.chainName()
const chainId = this.config.chainCommon.chainId()
const packageJSON = getPackageJSON()
this.config.logger.info(
`Initializing Ethereumjs client version=v${packageJSON.version} network=${name} chainId=${chainId}`,
)
this.config.events.on(Event.SERVER_ERROR, (error) => {
this.config.logger.warn(`Server error: ${error.name} - ${error.message}`)
})
this.config.events.on(Event.SERVER_LISTENING, (details) => {
this.config.logger.info(
`Server listener up transport=${details.transport} url=${details.url}`,
)
})
await this.service.open()
this.opened = true
}
/**
* Starts node and all services and network servers.
*/
async start() {
if (this.started) {
return false
}
this.config.logger.info('Setup networking and services.')
await this.service.start()
this.config.server && (await this.config.server.start())
// Only call bootstrap if servers are actually started
this.config.server && this.config.server.started && (await this.config.server.bootstrap())
this.started = true
}
/**
* Stops node and all services and network servers.
*/
async stop() {
if (!this.started) {
return false
}
this.config.events.emit(Event.CLIENT_SHUTDOWN)
await this.service.stop()
this.config.server && this.config.server.started && (await this.config.server.stop())
this.started = false
}
/**
*
* @returns the RLPx server (if it exists)
*/
server() {
return this.config.server
}
}