@@ -8,15 +8,14 @@ const promisify = require('util-promisify')
8
8
9
9
const defaultOptions = {
10
10
logger : defaultLogger ,
11
- common : new Common ( 'mainnet' , 'chainstart' )
11
+ common : new Common ( 'mainnet' , 'chainstart' ) ,
12
12
}
13
13
14
14
/**
15
15
* Blockchain
16
16
* @memberof module:blockchain
17
17
*/
18
18
export class Chain extends events . EventEmitter {
19
-
20
19
public logger : any
21
20
public common : Common
22
21
public db : any
@@ -27,13 +26,13 @@ export class Chain extends events.EventEmitter {
27
26
private _headers = { }
28
27
private _blocks = { }
29
28
30
- private _getBlocks : Function | undefined
31
- private _getBlock : Function | undefined
32
- private _putBlocks : Function | undefined
33
- private _putHeaders : Function | undefined
34
- private _getLatestHeader : Function | undefined
35
- private _getLatestBlock : Function | undefined
36
- private _getTd : Function | undefined
29
+ private _getBlocks : Function | undefined
30
+ private _getBlock : Function | undefined
31
+ private _putBlocks : Function | undefined
32
+ private _putHeaders : Function | undefined
33
+ private _getLatestHeader : Function | undefined
34
+ private _getLatestBlock : Function | undefined
35
+ private _getTd : Function | undefined
37
36
38
37
/**
39
38
* Create new chain
@@ -42,7 +41,7 @@ export class Chain extends events.EventEmitter {
42
41
* @param {Common } [options.common] common parameters
43
42
* @param {Logger } [options.logger] Logger instance
44
43
*/
45
- constructor ( options ?: any ) {
44
+ constructor ( options ?: any ) {
46
45
super ( )
47
46
options = { ...defaultOptions , ...options }
48
47
@@ -53,12 +52,12 @@ export class Chain extends events.EventEmitter {
53
52
this . init ( )
54
53
}
55
54
56
- init ( ) {
55
+ init ( ) {
57
56
if ( ! this . blockchain ) {
58
57
this . blockchain = new Blockchain ( {
59
58
db : this . db ,
60
59
validate : false ,
61
- common : this . common
60
+ common : this . common ,
62
61
} )
63
62
if ( ! this . db ) {
64
63
this . db = this . blockchain . db
@@ -69,32 +68,34 @@ export class Chain extends events.EventEmitter {
69
68
this . opened = false
70
69
}
71
70
72
- reset ( ) {
71
+ reset ( ) {
73
72
this . _headers = {
74
73
latest : null ,
75
74
td : new BN ( 0 ) ,
76
- height : new BN ( 0 )
75
+ height : new BN ( 0 ) ,
77
76
}
78
77
this . _blocks = {
79
78
latest : null ,
80
79
td : new BN ( 0 ) ,
81
- height : new BN ( 0 )
80
+ height : new BN ( 0 ) ,
82
81
}
83
82
}
84
83
85
84
/**
86
85
* Network ID
87
86
*/
88
- get networkId ( ) : number {
87
+ get networkId ( ) : number {
89
88
return this . common . networkId ( )
90
89
}
91
90
92
91
/**
93
92
* Genesis block parameters
94
93
*/
95
- get genesis ( ) : object {
94
+ get genesis ( ) : object {
96
95
const genesis = this . common . genesis ( )
97
- Object . entries ( genesis ) . forEach ( ( [ k , v ] ) => { genesis [ k ] = hexToBuffer ( v as string ) } )
96
+ Object . entries ( genesis ) . forEach ( ( [ k , v ] ) => {
97
+ genesis [ k ] = hexToBuffer ( v as string )
98
+ } )
98
99
return genesis
99
100
}
100
101
@@ -103,7 +104,7 @@ export class Chain extends events.EventEmitter {
103
104
* the latest header in the chain, the ``td`` property is the total difficulty of
104
105
* headerchain, and the ``height`` is the height of the headerchain.
105
106
*/
106
- get headers ( ) : object {
107
+ get headers ( ) : object {
107
108
return { ...this . _headers }
108
109
}
109
110
@@ -112,14 +113,14 @@ export class Chain extends events.EventEmitter {
112
113
* the latest block in the chain, the ``td`` property is the total difficulty of
113
114
* blockchain, and the ``height`` is the height of the blockchain.
114
115
*/
115
- get blocks ( ) : object {
116
+ get blocks ( ) : object {
116
117
return { ...this . _blocks }
117
118
}
118
119
119
120
/**
120
121
* Open blockchain and wait for database to load
121
122
*/
122
- async open ( ) : Promise < boolean | void > {
123
+ async open ( ) : Promise < boolean | void > {
123
124
if ( this . opened ) {
124
125
return false
125
126
}
@@ -132,7 +133,7 @@ export class Chain extends events.EventEmitter {
132
133
/**
133
134
* Close blockchain and database
134
135
*/
135
- async close ( ) : Promise < boolean | void > {
136
+ async close ( ) : Promise < boolean | void > {
136
137
if ( ! this . opened ) {
137
138
return false
138
139
}
@@ -145,7 +146,7 @@ export class Chain extends events.EventEmitter {
145
146
* Update blockchain properties (latest block, td, height, etc...)
146
147
* @return {Promise }
147
148
*/
148
- async update ( ) : Promise < boolean | void > {
149
+ async update ( ) : Promise < boolean | void > {
149
150
if ( ! this . opened ) {
150
151
return false
151
152
}
@@ -171,7 +172,7 @@ export class Chain extends events.EventEmitter {
171
172
* @param skip number of blocks to skip
172
173
* @param reverse get blocks in reverse
173
174
*/
174
- async getBlocks ( block : Buffer | BN , max : number , skip : number , reverse : boolean ) : Promise < any [ ] > {
175
+ async getBlocks ( block : Buffer | BN , max : number , skip : number , reverse : boolean ) : Promise < any [ ] > {
175
176
if ( ! this . opened ) {
176
177
await this . open ( )
177
178
}
@@ -185,7 +186,7 @@ export class Chain extends events.EventEmitter {
185
186
* Gets a block by its hash or number
186
187
* @param blocks block hash or number
187
188
*/
188
- async getBlock ( block : Buffer | BN ) : Promise < any > {
189
+ async getBlock ( block : Buffer | BN ) : Promise < any > {
189
190
if ( ! this . opened ) {
190
191
await this . open ( )
191
192
}
@@ -201,7 +202,7 @@ export class Chain extends events.EventEmitter {
201
202
* @method putBlocks
202
203
* @param {Block[] } blocks list of blocks to add
203
204
*/
204
- async putBlocks ( blocks : object [ ] ) : Promise < void > {
205
+ async putBlocks ( blocks : object [ ] ) : Promise < void > {
205
206
if ( ! this . opened ) {
206
207
await this . open ( )
207
208
}
@@ -223,7 +224,12 @@ export class Chain extends events.EventEmitter {
223
224
* @param skip number of headers to skip
224
225
* @param reverse get headers in reverse
225
226
*/
226
- async getHeaders ( block : Buffer | BN , max : number , skip : number , reverse : boolean ) : Promise < any [ ] > {
227
+ async getHeaders (
228
+ block : Buffer | BN ,
229
+ max : number ,
230
+ skip : number ,
231
+ reverse : boolean
232
+ ) : Promise < any [ ] > {
227
233
const blocks = await this . getBlocks ( block , max , skip , reverse )
228
234
return blocks . map ( ( b : any ) => b . header )
229
235
}
@@ -233,7 +239,7 @@ export class Chain extends events.EventEmitter {
233
239
* @method putHeaders
234
240
* @param headers list of headers to add
235
241
*/
236
- async putHeaders ( headers : object [ ] ) : Promise < void > {
242
+ async putHeaders ( headers : object [ ] ) : Promise < void > {
237
243
if ( ! this . opened ) {
238
244
await this . open ( )
239
245
}
@@ -251,7 +257,7 @@ export class Chain extends events.EventEmitter {
251
257
/**
252
258
* Gets the latest header in the canonical chain
253
259
*/
254
- async getLatestHeader ( ) : Promise < any > {
260
+ async getLatestHeader ( ) : Promise < any > {
255
261
if ( ! this . opened ) {
256
262
await this . open ( )
257
263
}
@@ -265,7 +271,7 @@ export class Chain extends events.EventEmitter {
265
271
/**
266
272
* Gets the latest block in the canonical chain
267
273
*/
268
- async getLatestBlock ( ) : Promise < any > {
274
+ async getLatestBlock ( ) : Promise < any > {
269
275
if ( ! this . opened ) {
270
276
await this . open ( )
271
277
}
@@ -280,7 +286,7 @@ export class Chain extends events.EventEmitter {
280
286
* Gets total difficulty for a block
281
287
* @param hash block hash
282
288
*/
283
- async getTd ( hash : Buffer ) : Promise < any > {
289
+ async getTd ( hash : Buffer ) : Promise < any > {
284
290
if ( ! this . opened ) {
285
291
await this . open ( )
286
292
}
@@ -291,10 +297,9 @@ export class Chain extends events.EventEmitter {
291
297
}
292
298
}
293
299
294
- function hexToBuffer ( hexString : string ) : Buffer | string {
295
- if ( typeof ( hexString ) === 'string' && hexString . startsWith ( '0x' ) ) {
300
+ function hexToBuffer ( hexString : string ) : Buffer | string {
301
+ if ( typeof hexString === 'string' && hexString . startsWith ( '0x' ) ) {
296
302
return Buffer . from ( hexString . slice ( 2 ) , 'hex' )
297
303
}
298
304
return hexString
299
305
}
300
-
0 commit comments