ethapi: address review concerns

This commit is contained in:
Martin Holst Swende 2018-06-07 12:27:59 +02:00
parent 904e903392
commit df7022b179
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0
2 changed files with 18 additions and 23 deletions

View file

@ -361,30 +361,25 @@ type BadBlockArgs struct {
// 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) ([]BadBlockArgs, error) { func (api *PrivateDebugAPI) GetBadBlocks(ctx context.Context) ([]*BadBlockArgs, error) {
blocks := api.eth.BlockChain().BadBlocks() blocks := api.eth.BlockChain().BadBlocks()
responseBlocks := make([]BadBlockArgs, 0, len(blocks)) results := make([]*BadBlockArgs, len(blocks))
for _, block := range blocks {
rlpData, err := rlp.EncodeToBytes(block) var err error
if err != nil { for i, block := range blocks {
return responseBlocks, err results[i] = &BadBlockArgs{
}
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(), Hash: block.Hash(),
Block: blockdata,
RLP: fmt.Sprintf("%x", rlpData),
})
} }
return responseBlocks, nil if rlpBytes, err := rlp.EncodeToBytes(block); err != nil {
results[i].RLP = err.Error() // Hacky, but hey, it works
} else {
results[i].RLP = fmt.Sprintf("0x%x", rlpBytes)
}
if results[i].Block, err = ethapi.RPCMarshalBlock(block, true, true); err != nil {
results[i].Block = map[string]interface{}{"error": err.Error()}
}
}
return results, 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
} }
// RpcMarshalBlock 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 RpcMarshalBlock(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),
@ -850,7 +850,7 @@ func RpcMarshalBlock(b *types.Block, inclTx bool, fullTx bool) (map[string]inter
// rpcOutputBlock uses the generalized output filler, then adds the total difficulty field, which requires // rpcOutputBlock uses the generalized output filler, then adds the total difficulty field, which requires
// a `PublicBlockchainAPI`. // a `PublicBlockchainAPI`.
func (s *PublicBlockChainAPI) rpcOutputBlock(b *types.Block, inclTx bool, fullTx bool) (map[string]interface{}, error) { func (s *PublicBlockChainAPI) rpcOutputBlock(b *types.Block, inclTx bool, fullTx bool) (map[string]interface{}, error) {
fields, err := RpcMarshalBlock(b, inclTx, fullTx) fields, err := RPCMarshalBlock(b, inclTx, fullTx)
if err != nil { if err != nil {
return nil, err return nil, err
} }