@@ -8,11 +8,13 @@ import {
8
8
KECCAK256_RLP ,
9
9
Lock ,
10
10
MapDB ,
11
+ bigIntToHex ,
11
12
bytesToHex ,
12
13
bytesToUnprefixedHex ,
13
14
concatBytes ,
14
15
equalsBytes ,
15
16
} from '@ethereumjs/util'
17
+ import debugDefault from 'debug'
16
18
17
19
import { CasperConsensus } from './consensus/casper.js'
18
20
import {
@@ -36,6 +38,7 @@ import type {
36
38
import type { HeaderData } from '@ethereumjs/block'
37
39
import type { CliqueConfig } from '@ethereumjs/common'
38
40
import type { BigIntLike , DB , DBObject , GenesisState } from '@ethereumjs/util'
41
+ import type { Debugger } from 'debug'
39
42
40
43
/**
41
44
* Blockchain implementation to create and maintain a valid canonical chain
@@ -88,6 +91,9 @@ export class Blockchain implements BlockchainInterface {
88
91
*/
89
92
private _deletedBlocks : Block [ ] = [ ]
90
93
94
+ private DEBUG : boolean // Guard for debug logs
95
+ private _debug : Debugger
96
+
91
97
/**
92
98
* Creates new Blockchain object.
93
99
*
@@ -99,6 +105,10 @@ export class Blockchain implements BlockchainInterface {
99
105
* {@link BlockchainOptions}.
100
106
*/
101
107
constructor ( opts : BlockchainOptions = { } ) {
108
+ this . DEBUG =
109
+ typeof window === 'undefined' ? ( process ?. env ?. DEBUG ?. includes ( 'ethjs' ) ?? false ) : false
110
+ this . _debug = debugDefault ( 'blockchain' )
111
+
102
112
if ( opts . common ) {
103
113
this . common = opts . common
104
114
} else {
@@ -262,6 +272,8 @@ export class Blockchain implements BlockchainInterface {
262
272
for ( let i = 0 ; i < blocks . length ; i ++ ) {
263
273
await this . putBlock ( blocks [ i ] )
264
274
}
275
+
276
+ this . DEBUG && this . _debug ( `put ${ blocks . length } blocks` )
265
277
}
266
278
267
279
/**
@@ -289,6 +301,8 @@ export class Blockchain implements BlockchainInterface {
289
301
for ( let i = 0 ; i < headers . length ; i ++ ) {
290
302
await this . putHeader ( headers [ i ] )
291
303
}
304
+
305
+ this . DEBUG && this . _debug ( `put ${ headers . length } headers` )
292
306
}
293
307
294
308
/**
@@ -312,8 +326,10 @@ export class Blockchain implements BlockchainInterface {
312
326
*/
313
327
314
328
async resetCanonicalHead ( canonicalHead : bigint ) {
329
+ let hash : Uint8Array | undefined
330
+ const canonicalHeadHash = ( await this . getCanonicalHeadHeader ( ) ) . hash ( )
315
331
await this . runWithLock < void > ( async ( ) => {
316
- const hash = await this . dbManager . numberToHash ( canonicalHead )
332
+ hash = await this . dbManager . numberToHash ( canonicalHead )
317
333
if ( hash === undefined ) {
318
334
throw new Error ( `no block for ${ canonicalHead } found in DB` )
319
335
}
@@ -328,6 +344,17 @@ export class Blockchain implements BlockchainInterface {
328
344
} )
329
345
if ( this . _deletedBlocks . length > 0 ) {
330
346
this . events . emit ( 'deletedCanonicalBlocks' , this . _deletedBlocks )
347
+ for ( const block of this . _deletedBlocks )
348
+ this . DEBUG &&
349
+ this . _debug (
350
+ `deleted block along head reset: number ${ block . header . number } hash ${ bytesToHex ( block . hash ( ) ) } ` ,
351
+ )
352
+
353
+ this . DEBUG &&
354
+ this . _debug (
355
+ `Canonical head set from ${ bytesToHex ( canonicalHeadHash ) } to ${ bytesToHex ( hash ! ) } (number ${ bigIntToHex ( canonicalHead ) } )` ,
356
+ )
357
+
331
358
this . _deletedBlocks = [ ]
332
359
}
333
360
}
@@ -452,6 +479,8 @@ export class Blockchain implements BlockchainInterface {
452
479
await this . dbManager . batch ( ops )
453
480
454
481
await this . consensus ?. newBlock ( block , commonAncestor , ancestorHeaders )
482
+ this . DEBUG &&
483
+ this . _debug ( `put block number=${ block . header . number } hash=${ bytesToHex ( blockHash ) } ` )
455
484
} catch ( e ) {
456
485
// restore head to the previously sane state
457
486
this . _heads = oldHeads
@@ -462,6 +491,11 @@ export class Blockchain implements BlockchainInterface {
462
491
} )
463
492
if ( this . _deletedBlocks . length > 0 ) {
464
493
this . events . emit ( 'deletedCanonicalBlocks' , this . _deletedBlocks )
494
+ for ( const block of this . _deletedBlocks )
495
+ this . DEBUG &&
496
+ this . _debug (
497
+ `delete stale canonical block number=${ block . header . number } hash=${ bytesToHex ( block . hash ( ) ) } ` ,
498
+ )
465
499
this . _deletedBlocks = [ ]
466
500
}
467
501
}
@@ -829,6 +863,11 @@ export class Blockchain implements BlockchainInterface {
829
863
830
864
if ( this . _deletedBlocks . length > 0 ) {
831
865
this . events . emit ( 'deletedCanonicalBlocks' , this . _deletedBlocks )
866
+ for ( const block of this . _deletedBlocks )
867
+ this . DEBUG &&
868
+ this . _debug (
869
+ `delete stale canonical block number=${ block . header . number } hash=${ blockHash } )}` ,
870
+ )
832
871
this . _deletedBlocks = [ ]
833
872
}
834
873
}
@@ -1018,6 +1057,9 @@ export class Blockchain implements BlockchainInterface {
1018
1057
if ( ! equalsBytes ( header . hash ( ) , newHeader . hash ( ) ) ) {
1019
1058
throw new Error ( 'Failed to find ancient header' )
1020
1059
}
1060
+
1061
+ this . DEBUG && this . _debug ( `found common ancestor with hash=${ bytesToHex ( header . hash ( ) ) } ` )
1062
+ this . DEBUG && this . _debug ( `total ancestor headers num=${ ancestorHeaders . size } ` )
1021
1063
return {
1022
1064
commonAncestor : header ,
1023
1065
ancestorHeaders : Array . from ( ancestorHeaders ) ,
@@ -1080,6 +1122,10 @@ export class Blockchain implements BlockchainInterface {
1080
1122
1081
1123
hash = await this . safeNumberToHash ( blockNumber )
1082
1124
}
1125
+
1126
+ this . DEBUG &&
1127
+ this . _deletedBlocks . length > 0 &&
1128
+ this . _debug ( `deleted ${ this . _deletedBlocks . length } stale canonical blocks in total` )
1083
1129
} catch ( e ) {
1084
1130
// Ensure that if this method throws, `_deletedBlocks` is reset to the empty array
1085
1131
this . _deletedBlocks = [ ]
@@ -1162,6 +1208,8 @@ export class Blockchain implements BlockchainInterface {
1162
1208
if ( staleHeadBlock ) {
1163
1209
this . _headBlockHash = currentCanonicalHash
1164
1210
}
1211
+
1212
+ this . DEBUG && this . _debug ( `stale heads found num=${ staleHeads . length } ` )
1165
1213
}
1166
1214
1167
1215
/* Helper functions */
0 commit comments