Skip to content

Commit

Permalink
internal/ethapi: omit the transactions field when marshaling uncle bl…
Browse files Browse the repository at this point in the history
…ocks

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 <[email protected]>
  • Loading branch information
meowsbits committed Mar 17, 2021
1 parent 78e602b commit 67ca5fc
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -1234,16 +1233,39 @@ 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.
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,
Expand All @@ -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) {
Expand Down

0 comments on commit 67ca5fc

Please sign in to comment.