Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core,eth,internal: Added debug_getBadBlocks() method #3654

Merged
merged 4 commits into from
Feb 13, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const (
// must be bumped when consensus algorithm is changed, this forces the upgradedb
// command to be run (forces the blocks to be imported again using the new algorithm)
BlockChainVersion = 3
badBlockLimit = 10
)

// BlockChain represents the canonical chain given a database with a genesis
Expand Down Expand Up @@ -108,6 +109,8 @@ type BlockChain struct {
processor Processor // block processor interface
validator Validator // block and state validator interface
vmConfig vm.Config

badBlocks *lru.Cache // Bad block cache
}

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

bc := &BlockChain{
config: config,
Expand All @@ -130,6 +134,7 @@ func NewBlockChain(chainDb ethdb.Database, config *params.ChainConfig, pow pow.P
futureBlocks: futureBlocks,
pow: pow,
vmConfig: vmConfig,
badBlocks: badBlocks,
}
bc.SetValidator(NewBlockValidator(config, bc, pow))
bc.SetProcessor(NewStateProcessor(config, bc))
Expand Down Expand Up @@ -893,7 +898,6 @@ func (self *BlockChain) InsertChain(chain types.Blocks) (int, error) {
glog.V(logger.Debug).Infoln("Premature abort during block chain processing")
break
}

bstart := time.Now()
// Wait for block i's nonce to be verified before processing
// its state transition.
Expand Down Expand Up @@ -1242,8 +1246,30 @@ func (self *BlockChain) update() {
}
}

// BadBlockArgs represents the entries in the list returned when bad blocks are queried.
type BadBlockArgs struct {
Hash common.Hash `json:"hash"`
Header *types.Header `json:"header"`
}

// BadBlocks returns a list of the last 'bad blocks' that the client has seen on the network
func (bc *BlockChain) BadBlocks() ([]BadBlockArgs, error) {
headers := make([]BadBlockArgs, 0, bc.badBlocks.Len())
for _, hash := range bc.badBlocks.Keys() {
if hdr, exist := bc.badBlocks.Peek(hash); exist {
headers = append(headers, BadBlockArgs{hdr.(*types.Header).Hash(), hdr.(*types.Header)})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe assign hdr.(*types.Header) first so you don't need to type assert twice.

}
}
return headers, nil
}
// Adds a 'bad lock' to the LRU
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please change comment to addBadBlock adds a bad block to the bad-block LRU cache.

func (bc *BlockChain) addBadBlock(block *types.Block) {
bc.badBlocks.Add(block.Header().Hash(), block.Header())
}

// reportBlock logs a bad block error.
func (bc *BlockChain) reportBlock(block *types.Block, receipts types.Receipts, err error) {
bc.addBadBlock(block)
if glog.V(logger.Error) {
var receiptString string
for _, receipt := range receipts {
Expand Down
6 changes: 6 additions & 0 deletions eth/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -566,3 +566,9 @@ func (api *PrivateDebugAPI) Preimage(ctx context.Context, hash common.Hash) (hex
db := core.PreimageTable(api.eth.ChainDb())
return db.Get(hash.Bytes())
}

// GetBadBLocks returns a list of the last 'bad blocks' that the client has seen on the network
// and returns them as a JSON list of block-hashes
func (api *PrivateDebugAPI) GetBadBlocks(ctx context.Context) ([]core.BadBlockArgs, error) {
return api.eth.BlockChain().BadBlocks()
}
7 changes: 6 additions & 1 deletion internal/web3ext/web3ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,12 @@ web3._extend({
call: 'debug_preimage',
params: 1,
inputFormatter: [null]
})
}),
new web3._extend.Method({
name: 'getBadBlocks',
call: 'debug_getBadBlocks',
params: 0,
}),
],
properties: []
});
Expand Down