|
1 | 1 | package ethapi |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
4 | 5 | "errors" |
| 6 | + "fmt" |
5 | 7 | "math/big" |
6 | 8 | "testing" |
7 | 9 |
|
8 | 10 | "github.com/celo-org/celo-blockchain/common" |
9 | 11 | "github.com/celo-org/celo-blockchain/common/hexutil" |
10 | 12 | "github.com/celo-org/celo-blockchain/core/types" |
11 | 13 | "github.com/stretchr/testify/assert" |
| 14 | + "github.com/stretchr/testify/require" |
12 | 15 | ) |
13 | 16 |
|
14 | 17 | // TestNewRPCTransactionCeloDynamicV2 tests the newRPCTransaction method with a celo dynamic fee tx v2 type. |
@@ -159,3 +162,42 @@ func TestNewRPCTransactionDynamic(t *testing.T) { |
159 | 162 | assert.Equal(t, (*hexutil.Big)(bigFeeCap), rpcTx.GasPrice) |
160 | 163 | }) |
161 | 164 | } |
| 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 | +} |
0 commit comments