Skip to content

Commit 72dcd3c

Browse files
holimanobscuren
authored andcommitted
core, eth, internal: Added debug_getBadBlocks() method (bnb-chain#3654)
* core,eth,internal: Added `debug_getBadBlocks()` method When bad blocks are discovered, these are stored within geth. An RPC-endpoint makes them availablewithin the `debug` namespace. This feature makes it easier to discover network forks. ``` * core, api: go format + docs * core/blockchain: Documentation, fix minor nitpick * core: fix failing blockchain test
1 parent 4ece9c6 commit 72dcd3c

4 files changed

Lines changed: 42 additions & 2 deletions

File tree

‎core/blockchain.go‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ const (
6060
// must be bumped when consensus algorithm is changed, this forces the upgradedb
6161
// command to be run (forces the blocks to be imported again using the new algorithm)
6262
BlockChainVersion = 3
63+
badBlockLimit = 10
6364
)
6465

6566
// BlockChain represents the canonical chain given a database with a genesis
@@ -108,6 +109,8 @@ type BlockChain struct {
108109
processor Processor // block processor interface
109110
validator Validator // block and state validator interface
110111
vmConfig vm.Config
112+
113+
badBlocks *lru.Cache // Bad block cache
111114
}
112115

113116
// NewBlockChain returns a fully initialised block chain using information
@@ -118,6 +121,7 @@ func NewBlockChain(chainDb ethdb.Database, config *params.ChainConfig, pow pow.P
118121
bodyRLPCache, _ := lru.New(bodyCacheLimit)
119122
blockCache, _ := lru.New(blockCacheLimit)
120123
futureBlocks, _ := lru.New(maxFutureBlocks)
124+
badBlocks, _ := lru.New(badBlockLimit)
121125

122126
bc := &BlockChain{
123127
config: config,
@@ -130,6 +134,7 @@ func NewBlockChain(chainDb ethdb.Database, config *params.ChainConfig, pow pow.P
130134
futureBlocks: futureBlocks,
131135
pow: pow,
132136
vmConfig: vmConfig,
137+
badBlocks: badBlocks,
133138
}
134139
bc.SetValidator(NewBlockValidator(config, bc, pow))
135140
bc.SetProcessor(NewStateProcessor(config, bc))
@@ -893,7 +898,6 @@ func (self *BlockChain) InsertChain(chain types.Blocks) (int, error) {
893898
glog.V(logger.Debug).Infoln("Premature abort during block chain processing")
894899
break
895900
}
896-
897901
bstart := time.Now()
898902
// Wait for block i's nonce to be verified before processing
899903
// its state transition.
@@ -1242,8 +1246,32 @@ func (self *BlockChain) update() {
12421246
}
12431247
}
12441248

1249+
// BadBlockArgs represents the entries in the list returned when bad blocks are queried.
1250+
type BadBlockArgs struct {
1251+
Hash common.Hash `json:"hash"`
1252+
Header *types.Header `json:"header"`
1253+
}
1254+
1255+
// BadBlocks returns a list of the last 'bad blocks' that the client has seen on the network
1256+
func (bc *BlockChain) BadBlocks() ([]BadBlockArgs, error) {
1257+
headers := make([]BadBlockArgs, 0, bc.badBlocks.Len())
1258+
for _, hash := range bc.badBlocks.Keys() {
1259+
if hdr, exist := bc.badBlocks.Peek(hash); exist {
1260+
header := hdr.(*types.Header)
1261+
headers = append(headers, BadBlockArgs{header.Hash(), header})
1262+
}
1263+
}
1264+
return headers, nil
1265+
}
1266+
1267+
// addBadBlock adds a bad block to the bad-block LRU cache
1268+
func (bc *BlockChain) addBadBlock(block *types.Block) {
1269+
bc.badBlocks.Add(block.Header().Hash(), block.Header())
1270+
}
1271+
12451272
// reportBlock logs a bad block error.
12461273
func (bc *BlockChain) reportBlock(block *types.Block, receipts types.Receipts, err error) {
1274+
bc.addBadBlock(block)
12471275
if glog.V(logger.Error) {
12481276
var receiptString string
12491277
for _, receipt := range receipts {

‎core/blockchain_test.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,7 @@ func chm(genesis *types.Block, db ethdb.Database) *BlockChain {
485485
bc.bodyRLPCache, _ = lru.New(100)
486486
bc.blockCache, _ = lru.New(100)
487487
bc.futureBlocks, _ = lru.New(100)
488+
bc.badBlocks, _ = lru.New(10)
488489
bc.SetValidator(bproc{})
489490
bc.SetProcessor(bproc{})
490491
bc.ResetWithGenesisBlock(genesis)

‎eth/api.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,3 +566,9 @@ func (api *PrivateDebugAPI) Preimage(ctx context.Context, hash common.Hash) (hex
566566
db := core.PreimageTable(api.eth.ChainDb())
567567
return db.Get(hash.Bytes())
568568
}
569+
570+
// GetBadBLocks returns a list of the last 'bad blocks' that the client has seen on the network
571+
// and returns them as a JSON list of block-hashes
572+
func (api *PrivateDebugAPI) GetBadBlocks(ctx context.Context) ([]core.BadBlockArgs, error) {
573+
return api.eth.BlockChain().BadBlocks()
574+
}

‎internal/web3ext/web3ext.go‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,12 @@ web3._extend({
294294
call: 'debug_preimage',
295295
params: 1,
296296
inputFormatter: [null]
297-
})
297+
}),
298+
new web3._extend.Method({
299+
name: 'getBadBlocks',
300+
call: 'debug_getBadBlocks',
301+
params: 0,
302+
}),
298303
],
299304
properties: []
300305
});

0 commit comments

Comments
 (0)