From 67ca5fcf0398beba097b5ba27517d8079ad7ea58 Mon Sep 17 00:00:00 2001 From: meows Date: Wed, 17 Mar 2021 13:08:01 -0500 Subject: [PATCH] internal/ethapi: omit the transactions field when marshaling uncle blocks Create a dedicated type for this case, switching on it conditionally when inclTx is false; this parameter is false only for uncle blocks. Date: 2021-03-17 13:08:01-05:00 Signed-off-by: meows --- internal/ethapi/api.go | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index da8bc91e09..1277dc4485 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -1222,7 +1222,6 @@ type RPCMarshalBlockT struct { // RPCMarshalBlockTIR is the intermediate representation of RPCMarshalBlockT. // This exists to avoid a circular reference when overriding the json marshaling interface. -// RPCMarshalHeaderT is an embedded struct. type RPCMarshalBlockTIR struct { *RPCMarshalHeaderT Transactions []interface{} `json:"transactions"` @@ -1234,6 +1233,19 @@ type RPCMarshalBlockTIR struct { fullTx bool } +// RPCMarshalUncleTIR is the intermediate representation of RPCMarshalBlockT which excludes the transactions field. +// Transactions are excluded when the API returns block information for uncle blocks. +// This exists to avoid a circular reference when overriding the json marshaling interface. +type RPCMarshalUncleTIR struct { + *RPCMarshalHeaderT + Uncles []common.Hash `json:"uncles"` + + Error string `json:"error,omitempty"` + + inclTx bool + fullTx bool +} + // MarshalJSON marshals JSON for RPCMarshalBlockT. // If an error is present on the struct, an object with only the error is returned. // This logic follows the established logic at eth/api.go's handling of bad blocks. @@ -1241,9 +1253,19 @@ func (b *RPCMarshalBlockT) MarshalJSON() ([]byte, error) { if b.Error != "" { return json.Marshal(map[string]interface{}{"error": b.Error}) } - ir := &RPCMarshalBlockTIR{ + if b.inclTx { + ir := &RPCMarshalBlockTIR{ + RPCMarshalHeaderT: b.RPCMarshalHeaderT, + Transactions: b.Transactions, + Uncles: b.Uncles, + Error: "", + inclTx: b.inclTx, + fullTx: b.fullTx, + } + return json.Marshal(ir) + } + ir := &RPCMarshalUncleTIR{ RPCMarshalHeaderT: b.RPCMarshalHeaderT, - Transactions: b.Transactions, Uncles: b.Uncles, Error: "", inclTx: b.inclTx, @@ -1258,6 +1280,8 @@ func (b *RPCMarshalBlockT) MarshalJSON() ([]byte, error) { func RPCMarshalBlock(block *types.Block, inclTx bool, fullTx bool) (*RPCMarshalBlockT, error) { fields := &RPCMarshalBlockT{RPCMarshalHeaderT: NewRPCMarshalHeaderTFromHeader(block.Header())} fields.Size = hexutil.Uint64(block.Size()) + fields.inclTx = inclTx + fields.fullTx = fullTx if inclTx { formatTx := func(tx *types.Transaction) (interface{}, error) {