Skip to content

Commit e67c1fd

Browse files
piersyarthurgoussetkarlb
authored
Omit ethCompatible on RPC responses for non legacy txs (#2302)
* 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]>
1 parent ad952d1 commit e67c1fd

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

internal/ethapi/api.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,9 +1288,9 @@ type RPCTransaction struct {
12881288
GasPrice *hexutil.Big `json:"gasPrice"`
12891289
GasFeeCap *hexutil.Big `json:"maxFeePerGas,omitempty"`
12901290
GasTipCap *hexutil.Big `json:"maxPriorityFeePerGas,omitempty"`
1291-
FeeCurrency *common.Address `json:"feeCurrency"`
1292-
GatewayFeeRecipient *common.Address `json:"gatewayFeeRecipient"`
1293-
GatewayFee *hexutil.Big `json:"gatewayFee"`
1291+
FeeCurrency *common.Address `json:"feeCurrency,omitempty"`
1292+
GatewayFeeRecipient *common.Address `json:"gatewayFeeRecipient,omitempty"`
1293+
GatewayFee *hexutil.Big `json:"gatewayFee,omitempty"`
12941294
Hash common.Hash `json:"hash"`
12951295
Input hexutil.Bytes `json:"input"`
12961296
Nonce hexutil.Uint64 `json:"nonce"`
@@ -1303,7 +1303,7 @@ type RPCTransaction struct {
13031303
V *hexutil.Big `json:"v"`
13041304
R *hexutil.Big `json:"r"`
13051305
S *hexutil.Big `json:"s"`
1306-
EthCompatible bool `json:"ethCompatible"`
1306+
EthCompatible *bool `json:"ethCompatible,omitempty"`
13071307
}
13081308

13091309
// newRPCTransaction returns a transaction that will serialize to the RPC
@@ -1341,7 +1341,11 @@ func newRPCTransaction(tx *types.Transaction, blockHash common.Hash, blockNumber
13411341
V: (*hexutil.Big)(v),
13421342
R: (*hexutil.Big)(r),
13431343
S: (*hexutil.Big)(s),
1344-
EthCompatible: tx.EthCompatible(),
1344+
}
1345+
// Set eth compatible for legacy transactions only
1346+
if tx.Type() == types.LegacyTxType {
1347+
ec := tx.EthCompatible()
1348+
result.EthCompatible = &ec
13451349
}
13461350
if blockHash != (common.Hash{}) {
13471351
result.BlockHash = &blockHash

internal/ethapi/api_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
package ethapi
22

33
import (
4+
"encoding/json"
45
"errors"
6+
"fmt"
57
"math/big"
68
"testing"
79

810
"github.com/celo-org/celo-blockchain/common"
911
"github.com/celo-org/celo-blockchain/common/hexutil"
1012
"github.com/celo-org/celo-blockchain/core/types"
1113
"github.com/stretchr/testify/assert"
14+
"github.com/stretchr/testify/require"
1215
)
1316

1417
// TestNewRPCTransactionCeloDynamicV2 tests the newRPCTransaction method with a celo dynamic fee tx v2 type.
@@ -159,3 +162,42 @@ func TestNewRPCTransactionDynamic(t *testing.T) {
159162
assert.Equal(t, (*hexutil.Big)(bigFeeCap), rpcTx.GasPrice)
160163
})
161164
}
165+
166+
// TestNewRPCTransactionEthCompatible tests that only legacy transactions have the eth compatbile field set.
167+
func TestNewRPCTransactionEthCompatible(t *testing.T) {
168+
blockHash := common.BigToHash(big.NewInt(123456))
169+
blockNumber := uint64(123456)
170+
index := uint64(7)
171+
baseFeeFn := func(curr *common.Address) (*big.Int, error) {
172+
return big.NewInt(600), nil
173+
}
174+
175+
t.Run("LegacyTx ethCompatible", func(*testing.T) {
176+
rpcTx := newRPCTransaction(types.NewTx(&types.LegacyTx{
177+
EthCompatible: true,
178+
}), blockHash, blockNumber, index, baseFeeFn, true)
179+
assert.Equal(t, true, jsonRoundtripToMap(t, rpcTx)["ethCompatible"])
180+
})
181+
182+
t.Run("LegacyTx not ethCompatible", func(*testing.T) {
183+
rpcTx := newRPCTransaction(types.NewTx(&types.LegacyTx{
184+
EthCompatible: false,
185+
}), blockHash, blockNumber, index, baseFeeFn, true)
186+
assert.Equal(t, false, jsonRoundtripToMap(t, rpcTx)["ethCompatible"])
187+
})
188+
189+
t.Run("Non legacy tx ethCompatible not set", func(*testing.T) {
190+
rpcTx := newRPCTransaction(types.NewTx(&types.DynamicFeeTx{}), blockHash, blockNumber, index, baseFeeFn, true)
191+
assert.NotContains(t, jsonRoundtripToMap(t, rpcTx), "ethCompatible")
192+
fmt.Printf("%+v\n", jsonRoundtripToMap(t, rpcTx))
193+
})
194+
}
195+
196+
func jsonRoundtripToMap(t *testing.T, tx *RPCTransaction) map[string]interface{} {
197+
marshaled, err := json.Marshal(tx)
198+
require.NoError(t, err)
199+
m := make(map[string]interface{})
200+
err = json.Unmarshal(marshaled, &m)
201+
require.NoError(t, err)
202+
return m
203+
}

monorepo_commit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
f08e269d88c6680641427952ce4743a7d26336a6
1+
a63f0ec01ecd52b35dd01554eff22337e183da40

0 commit comments

Comments
 (0)