From df7022b179ca3c8341d8dc6c500bb7cc449530fe Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Thu, 7 Jun 2018 12:27:59 +0200 Subject: [PATCH] ethapi: address review concerns --- eth/api.go | 35 +++++++++++++++-------------------- internal/ethapi/api.go | 6 +++--- 2 files changed, 18 insertions(+), 23 deletions(-) diff --git a/eth/api.go b/eth/api.go index fce8c551c9..446161cc4a 100644 --- a/eth/api.go +++ b/eth/api.go @@ -361,30 +361,25 @@ type BadBlockArgs struct { // 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) ([]BadBlockArgs, error) { +func (api *PrivateDebugAPI) GetBadBlocks(ctx context.Context) ([]*BadBlockArgs, error) { 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 + results := make([]*BadBlockArgs, len(blocks)) + + var err error + for i, block := range blocks { + results[i] = &BadBlockArgs{ + Hash: block.Hash(), } - 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() + 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()} } - responseBlocks = append(responseBlocks, BadBlockArgs{ - Hash: block.Hash(), - Block: blockdata, - RLP: fmt.Sprintf("%x", rlpData), - }) } - return responseBlocks, nil + return results, nil } // StorageRangeResult is the result of a debug_storageRangeAt API call. diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index bb7673ad29..3c99203a85 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -792,10 +792,10 @@ func FormatLogs(logs []vm.StructLog) []StructLogRes { 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 // 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 fields := map[string]interface{}{ "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 // a `PublicBlockchainAPI`. 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 { return nil, err }