core, eth, ethapi: changes to getBadBlocks formatting

This commit is contained in:
Martin Holst Swende 2018-06-06 22:23:41 +02:00
parent caf0e1fc37
commit 904e903392
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0
3 changed files with 50 additions and 30 deletions

View file

@ -1385,34 +1385,16 @@ func (bc *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"`
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() []*types.Block {
blocks := make([]BadBlockArgs, 0, bc.badBlocks.Len()) blocks := make([]*types.Block, 0, bc.badBlocks.Len())
var (
err error
rlpData []byte
)
for _, hash := range bc.badBlocks.Keys() { for _, hash := range bc.badBlocks.Keys() {
if blk, exist := bc.badBlocks.Peek(hash); exist { if blk, exist := bc.badBlocks.Peek(hash); exist {
block := blk.(*types.Block) block := blk.(*types.Block)
rlpData, err = rlp.EncodeToBytes(block) blocks = append(blocks, block)
if err != nil {
break
}
blocks = append(blocks, BadBlockArgs{
Hash: block.Hash(),
Header: block.Header(),
BlockRLP: fmt.Sprintf("%x", rlpData)})
} }
} }
return blocks, err return blocks
} }
// addBadBlock adds a bad block to the bad-block LRU cache // addBadBlock adds a bad block to the bad-block LRU cache

View file

@ -32,6 +32,7 @@ import (
"github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/internal/ethapi"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/miner" "github.com/ethereum/go-ethereum/miner"
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"
@ -351,10 +352,39 @@ func (api *PrivateDebugAPI) Preimage(ctx context.Context, hash common.Hash) (hex
return nil, errors.New("unknown preimage") return nil, errors.New("unknown preimage")
} }
// BadBlockArgs represents the entries in the list returned when bad blocks are queried.
type BadBlockArgs struct {
Hash common.Hash `json:"hash"`
Block map[string]interface{} `json:"block"`
RLP string `json:"rlp"`
}
// GetBadBLocks returns a list of the last 'bad blocks' that the client has seen on the network // 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 // and returns them as a JSON list of block-hashes
func (api *PrivateDebugAPI) GetBadBlocks(ctx context.Context) ([]core.BadBlockArgs, error) { func (api *PrivateDebugAPI) GetBadBlocks(ctx context.Context) ([]BadBlockArgs, error) {
return api.eth.BlockChain().BadBlocks() blocks := api.eth.BlockChain().BadBlocks()
responseBlocks := make([]BadBlockArgs, 0, len(blocks))
for _, block := range blocks {
rlpData, err := rlp.EncodeToBytes(block)
if err != nil {
return responseBlocks, err
}
var (
blockdata map[string]interface{}
berr error
)
// A bad block may be so malformed that it the expanded format fails to generate
// correctly. If so, provide the caller with error message and RLP anyway
if blockdata, berr = ethapi.RpcMarshalBlock(block, true, true); berr != nil {
blockdata["error"] = berr.Error()
}
responseBlocks = append(responseBlocks, BadBlockArgs{
Hash: block.Hash(),
Block: blockdata,
RLP: fmt.Sprintf("%x", rlpData),
})
}
return responseBlocks, nil
} }
// StorageRangeResult is the result of a debug_storageRangeAt API call. // StorageRangeResult is the result of a debug_storageRangeAt API call.

View file

@ -792,10 +792,10 @@ func FormatLogs(logs []vm.StructLog) []StructLogRes {
return formatted return formatted
} }
// rpcOutputBlock converts the given block to the RPC output which depends on fullTx. If inclTx is true transactions are // RpcMarshalBlock converts the given block to the RPC output which depends on fullTx. If inclTx is true transactions are
// returned. When fullTx is true the returned block contains full transaction details, otherwise it will only contain // returned. When fullTx is true the returned block contains full transaction details, otherwise it will only contain
// transaction hashes. // transaction hashes.
func (s *PublicBlockChainAPI) rpcOutputBlock(b *types.Block, inclTx bool, fullTx bool) (map[string]interface{}, error) { func RpcMarshalBlock(b *types.Block, inclTx bool, fullTx bool) (map[string]interface{}, error) {
head := b.Header() // copies the header once head := b.Header() // copies the header once
fields := map[string]interface{}{ fields := map[string]interface{}{
"number": (*hexutil.Big)(head.Number), "number": (*hexutil.Big)(head.Number),
@ -808,7 +808,6 @@ func (s *PublicBlockChainAPI) rpcOutputBlock(b *types.Block, inclTx bool, fullTx
"stateRoot": head.Root, "stateRoot": head.Root,
"miner": head.Coinbase, "miner": head.Coinbase,
"difficulty": (*hexutil.Big)(head.Difficulty), "difficulty": (*hexutil.Big)(head.Difficulty),
"totalDifficulty": (*hexutil.Big)(s.b.GetTd(b.Hash())),
"extraData": hexutil.Bytes(head.Extra), "extraData": hexutil.Bytes(head.Extra),
"size": hexutil.Uint64(b.Size()), "size": hexutil.Uint64(b.Size()),
"gasLimit": hexutil.Uint64(head.GasLimit), "gasLimit": hexutil.Uint64(head.GasLimit),
@ -822,17 +821,15 @@ func (s *PublicBlockChainAPI) rpcOutputBlock(b *types.Block, inclTx bool, fullTx
formatTx := func(tx *types.Transaction) (interface{}, error) { formatTx := func(tx *types.Transaction) (interface{}, error) {
return tx.Hash(), nil return tx.Hash(), nil
} }
if fullTx { if fullTx {
formatTx = func(tx *types.Transaction) (interface{}, error) { formatTx = func(tx *types.Transaction) (interface{}, error) {
return newRPCTransactionFromBlockHash(b, tx.Hash()), nil return newRPCTransactionFromBlockHash(b, tx.Hash()), nil
} }
} }
txs := b.Transactions() txs := b.Transactions()
transactions := make([]interface{}, len(txs)) transactions := make([]interface{}, len(txs))
var err error var err error
for i, tx := range b.Transactions() { for i, tx := range txs {
if transactions[i], err = formatTx(tx); err != nil { if transactions[i], err = formatTx(tx); err != nil {
return nil, err return nil, err
} }
@ -850,6 +847,17 @@ func (s *PublicBlockChainAPI) rpcOutputBlock(b *types.Block, inclTx bool, fullTx
return fields, nil return fields, nil
} }
// rpcOutputBlock uses the generalized output filler, then adds the total difficulty field, which requires
// a `PublicBlockchainAPI`.
func (s *PublicBlockChainAPI) rpcOutputBlock(b *types.Block, inclTx bool, fullTx bool) (map[string]interface{}, error) {
fields, err := RpcMarshalBlock(b, inclTx, fullTx)
if err != nil {
return nil, err
}
fields["totalDifficulty"] = (*hexutil.Big)(s.b.GetTd(b.Hash()))
return fields, err
}
// RPCTransaction represents a transaction that will serialize to the RPC representation of a transaction // RPCTransaction represents a transaction that will serialize to the RPC representation of a transaction
type RPCTransaction struct { type RPCTransaction struct {
BlockHash common.Hash `json:"blockHash"` BlockHash common.Hash `json:"blockHash"`