core: improve getBadBlocks to return full block rlp

This commit is contained in:
Martin Holst Swende 2018-06-05 21:58:17 +02:00
parent 400332b99d
commit caf0e1fc37
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0

View file

@ -1387,25 +1387,37 @@ func (bc *BlockChain) update() {
// BadBlockArgs represents the entries in the list returned when bad blocks are queried. // BadBlockArgs represents the entries in the list returned when bad blocks are queried.
type BadBlockArgs struct { type BadBlockArgs struct {
Hash common.Hash `json:"hash"` Hash common.Hash `json:"hash"`
Header *types.Header `json:"header"` Header *types.Header `json:"header"`
BlockRLP string `json:"blockrlp"`
} }
// BadBlocks returns a list of the last 'bad blocks' that the client has seen on the network // BadBlocks returns a list of the last 'bad blocks' that the client has seen on the network
func (bc *BlockChain) BadBlocks() ([]BadBlockArgs, error) { func (bc *BlockChain) BadBlocks() ([]BadBlockArgs, error) {
headers := make([]BadBlockArgs, 0, bc.badBlocks.Len()) blocks := make([]BadBlockArgs, 0, bc.badBlocks.Len())
var (
err error
rlpData []byte
)
for _, hash := range bc.badBlocks.Keys() { for _, hash := range bc.badBlocks.Keys() {
if hdr, exist := bc.badBlocks.Peek(hash); exist { if blk, exist := bc.badBlocks.Peek(hash); exist {
header := hdr.(*types.Header) block := blk.(*types.Block)
headers = append(headers, BadBlockArgs{header.Hash(), header}) rlpData, err = rlp.EncodeToBytes(block)
if err != nil {
break
}
blocks = append(blocks, BadBlockArgs{
Hash: block.Hash(),
Header: block.Header(),
BlockRLP: fmt.Sprintf("%x", rlpData)})
} }
} }
return headers, nil return blocks, err
} }
// addBadBlock adds a bad block to the bad-block LRU cache // addBadBlock adds a bad block to the bad-block LRU cache
func (bc *BlockChain) addBadBlock(block *types.Block) { func (bc *BlockChain) addBadBlock(block *types.Block) {
bc.badBlocks.Add(block.Header().Hash(), block.Header()) bc.badBlocks.Add(block.Hash(), block)
} }
// reportBlock logs a bad block error. // reportBlock logs a bad block error.