Skip to content

Commit

Permalink
Omit ethCompatible on RPC responses for non legacy txs (#2302)
Browse files Browse the repository at this point in the history
* Omit ethCompatible on RPC responses for non legacy txs

* feat(ethapi): omits additional fields when empty

Omits `feeCurrency`, `gatewayFeeRecipient`, and `gatewayFee` from the response when they are empty (effectively on Ethereum-compatible transactions).

When I call `eth_gasTransactionByHash` on an EIP-1559 transaction, the response object includes `feeCurrency`, `gatewayFeeRecipient`, and `gatewayFee` although they aren't relevant.

* Update monorepo_commit (GatewayFeeRecipient check)

to include celo-org/celo-monorepo@a63f0ec

---------

Co-authored-by: arthurgousset <[email protected]>
Co-authored-by: Karl Bartel <[email protected]>
  • Loading branch information
3 people authored Apr 30, 2024
1 parent ad952d1 commit e67c1fd
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
14 changes: 9 additions & 5 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1288,9 +1288,9 @@ type RPCTransaction struct {
GasPrice *hexutil.Big `json:"gasPrice"`
GasFeeCap *hexutil.Big `json:"maxFeePerGas,omitempty"`
GasTipCap *hexutil.Big `json:"maxPriorityFeePerGas,omitempty"`
FeeCurrency *common.Address `json:"feeCurrency"`
GatewayFeeRecipient *common.Address `json:"gatewayFeeRecipient"`
GatewayFee *hexutil.Big `json:"gatewayFee"`
FeeCurrency *common.Address `json:"feeCurrency,omitempty"`
GatewayFeeRecipient *common.Address `json:"gatewayFeeRecipient,omitempty"`
GatewayFee *hexutil.Big `json:"gatewayFee,omitempty"`
Hash common.Hash `json:"hash"`
Input hexutil.Bytes `json:"input"`
Nonce hexutil.Uint64 `json:"nonce"`
Expand All @@ -1303,7 +1303,7 @@ type RPCTransaction struct {
V *hexutil.Big `json:"v"`
R *hexutil.Big `json:"r"`
S *hexutil.Big `json:"s"`
EthCompatible bool `json:"ethCompatible"`
EthCompatible *bool `json:"ethCompatible,omitempty"`
}

// newRPCTransaction returns a transaction that will serialize to the RPC
Expand Down Expand Up @@ -1341,7 +1341,11 @@ func newRPCTransaction(tx *types.Transaction, blockHash common.Hash, blockNumber
V: (*hexutil.Big)(v),
R: (*hexutil.Big)(r),
S: (*hexutil.Big)(s),
EthCompatible: tx.EthCompatible(),
}
// Set eth compatible for legacy transactions only
if tx.Type() == types.LegacyTxType {
ec := tx.EthCompatible()
result.EthCompatible = &ec
}
if blockHash != (common.Hash{}) {
result.BlockHash = &blockHash
Expand Down
42 changes: 42 additions & 0 deletions internal/ethapi/api_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package ethapi

import (
"encoding/json"
"errors"
"fmt"
"math/big"
"testing"

"github.com/celo-org/celo-blockchain/common"
"github.com/celo-org/celo-blockchain/common/hexutil"
"github.com/celo-org/celo-blockchain/core/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// TestNewRPCTransactionCeloDynamicV2 tests the newRPCTransaction method with a celo dynamic fee tx v2 type.
Expand Down Expand Up @@ -159,3 +162,42 @@ func TestNewRPCTransactionDynamic(t *testing.T) {
assert.Equal(t, (*hexutil.Big)(bigFeeCap), rpcTx.GasPrice)
})
}

// TestNewRPCTransactionEthCompatible tests that only legacy transactions have the eth compatbile field set.
func TestNewRPCTransactionEthCompatible(t *testing.T) {
blockHash := common.BigToHash(big.NewInt(123456))
blockNumber := uint64(123456)
index := uint64(7)
baseFeeFn := func(curr *common.Address) (*big.Int, error) {
return big.NewInt(600), nil
}

t.Run("LegacyTx ethCompatible", func(*testing.T) {
rpcTx := newRPCTransaction(types.NewTx(&types.LegacyTx{
EthCompatible: true,
}), blockHash, blockNumber, index, baseFeeFn, true)
assert.Equal(t, true, jsonRoundtripToMap(t, rpcTx)["ethCompatible"])
})

t.Run("LegacyTx not ethCompatible", func(*testing.T) {
rpcTx := newRPCTransaction(types.NewTx(&types.LegacyTx{
EthCompatible: false,
}), blockHash, blockNumber, index, baseFeeFn, true)
assert.Equal(t, false, jsonRoundtripToMap(t, rpcTx)["ethCompatible"])
})

t.Run("Non legacy tx ethCompatible not set", func(*testing.T) {
rpcTx := newRPCTransaction(types.NewTx(&types.DynamicFeeTx{}), blockHash, blockNumber, index, baseFeeFn, true)
assert.NotContains(t, jsonRoundtripToMap(t, rpcTx), "ethCompatible")
fmt.Printf("%+v\n", jsonRoundtripToMap(t, rpcTx))
})
}

func jsonRoundtripToMap(t *testing.T, tx *RPCTransaction) map[string]interface{} {
marshaled, err := json.Marshal(tx)
require.NoError(t, err)
m := make(map[string]interface{})
err = json.Unmarshal(marshaled, &m)
require.NoError(t, err)
return m
}
2 changes: 1 addition & 1 deletion monorepo_commit
Original file line number Diff line number Diff line change
@@ -1 +1 @@
f08e269d88c6680641427952ce4743a7d26336a6
a63f0ec01ecd52b35dd01554eff22337e183da40

0 comments on commit e67c1fd

Please sign in to comment.