forked from ethereumjs/ethereumjs-monorepo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.ts
169 lines (145 loc) · 4.49 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
169
import { version as packageVersion } from '../package.json'
import { Chain } from './blockchain'
import { SyncMode } from './config'
import { FullEthereumService, LightEthereumService } from './service'
import { Event } from './types'
import type { Config } from './config'
import type { MultiaddrLike } from './types'
import type { Blockchain } from '@ethereumjs/blockchain'
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 | Buffer | Uint8Array, string | Buffer, string | Buffer>
/**
* Database to store the state.
* Should be an abstract-leveldown compliant store.
*
* Default: Database created by the Trie class
*/
stateDB?: AbstractLevel<string | Buffer | Uint8Array, string | Buffer, string | Buffer>
/**
* Database to store tx receipts, logs, and indexes.
* Should be an abstract-leveldown compliant store.
*
* Default: Database created in datadir folder
*/
metaDB?: AbstractLevel<string | Buffer | Uint8Array, string | Buffer, string | Buffer>
/* List of bootnodes to use for discovery */
bootnodes?: MultiaddrLike[]
/* List of supported clients */
clientFilter?: string[]
/* How often to discover new peers */
refreshInterval?: number
}
/**
* 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 services: (FullEthereumService | LightEthereumService)[]
public opened: boolean
public started: boolean
/**
* Create new node
*/
constructor(options: EthereumClientOptions) {
this.config = options.config
this.chain = new Chain(options)
if (this.config.syncmode === SyncMode.Full) {
this.services = [
new FullEthereumService({
config: this.config,
chainDB: options.chainDB,
stateDB: options.stateDB,
metaDB: options.metaDB,
chain: this.chain,
}),
]
} else {
this.services = [
new LightEthereumService({
config: this.config,
chainDB: options.chainDB,
chain: this.chain,
}),
]
}
this.opened = false
this.started = false
}
/**
* Open node. Must be called before node is started
*/
async open() {
if (this.opened) {
return false
}
this.config.logger.info(
`Initializing Ethereumjs client version=v${packageVersion} network=${this.config.chainCommon.chainName()}`
)
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}`
)
})
this.config.events.on(Event.SYNC_SYNCHRONIZED, (height) => {
this.config.logger.info(`Synchronized blockchain at height=${height}`)
})
await Promise.all(this.services.map((s) => s.open()))
this.opened = true
}
/**
* Starts node and all services and network servers.
*/
async start() {
if (this.started) {
return false
}
this.config.logger.info('Connecting to network and synchronizing blockchain...')
await Promise.all(this.services.map((s) => s.start()))
await Promise.all(this.config.servers.map((s) => s.start()))
await Promise.all(this.config.servers.map((s) => s.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 Promise.all(this.services.map((s) => s.stop()))
await Promise.all(this.config.servers.map((s) => s.stop()))
this.started = false
}
/**
* Returns the service with the specified name.
* @param name name of service
*/
service(name: string) {
return this.services.find((s) => s.name === name)
}
/**
* Returns the server with the specified name.
* @param name name of server
*/
server(name: string) {
return this.config.servers.find((s) => s.name === name)
}
}