|
| 1 | +package types |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "math/big" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/ethereum/go-ethereum/common" |
| 9 | + "github.com/ethereum/go-ethereum/common/hexutil" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +func TestPostExecTxUnmarshalJSON(t *testing.T) { |
| 14 | + var tx Transaction |
| 15 | + err := json.Unmarshal([]byte(`{ |
| 16 | + "type":"0x7d", |
| 17 | + "gas":"0x0", |
| 18 | + "value":"0x0", |
| 19 | + "input":"0xc201c0" |
| 20 | + }`), &tx) |
| 21 | + require.NoError(t, err) |
| 22 | + require.Equal(t, uint8(PostExecTxType), tx.Type()) |
| 23 | + |
| 24 | + inner, ok := tx.inner.(*PostExecTx) |
| 25 | + require.True(t, ok) |
| 26 | + require.Equal(t, hexutil.MustDecode("0xc201c0"), inner.Data) |
| 27 | +} |
| 28 | + |
| 29 | +func TestPostExecTxUnmarshalJSONWithRPCMetadata(t *testing.T) { |
| 30 | + var tx Transaction |
| 31 | + err := json.Unmarshal([]byte(`{ |
| 32 | + "type":"0x7d", |
| 33 | + "from":"0x0000000000000000000000000000000000000000", |
| 34 | + "nonce":"0x0", |
| 35 | + "gasPrice":"0x1", |
| 36 | + "maxFeePerGas":"0x2", |
| 37 | + "maxPriorityFeePerGas":"0x3", |
| 38 | + "input":"0xc201c0", |
| 39 | + "v":"0x0", |
| 40 | + "r":"0x0", |
| 41 | + "s":"0x0" |
| 42 | + }`), &tx) |
| 43 | + require.NoError(t, err) |
| 44 | + require.Equal(t, uint8(PostExecTxType), tx.Type()) |
| 45 | +} |
| 46 | + |
| 47 | +func TestPostExecTxUnmarshalJSONErrors(t *testing.T) { |
| 48 | + tests := []struct { |
| 49 | + name string |
| 50 | + json string |
| 51 | + expectedError string |
| 52 | + }{ |
| 53 | + { |
| 54 | + name: "non-empty accessList", |
| 55 | + json: `{"type":"0x7d","input":"0xc201c0","accessList":[{"address":"0x0000000000000000000000000000000000000001","storageKeys":[]}]}`, |
| 56 | + expectedError: "unexpected field(s) in post-exec transaction", |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "unepxpected isSystemTx field", |
| 60 | + json: `{"type":"0x7d","input":"0xc201c0","isSystemTx":true}`, |
| 61 | + expectedError: "unexpected field(s) in post-exec transaction", |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "unexpected to field", |
| 65 | + json: `{"type":"0x7d","input":"0xc201c0","to":"0x0000000000000000000000000000000000000001"}`, |
| 66 | + expectedError: "unexpected field(s) in post-exec transaction", |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "non-zero from", |
| 70 | + json: `{"type":"0x7d","input":"0xc201c0","from":"0x0000000000000000000000000000000000000001"}`, |
| 71 | + expectedError: "post-exec transaction from must be zero address or unset", |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "non-zero nonce", |
| 75 | + json: `{"type":"0x7d","input":"0xc201c0","nonce":"0x1"}`, |
| 76 | + expectedError: "post-exec transaction nonce must be 0 or unset", |
| 77 | + }, |
| 78 | + { |
| 79 | + name: "non-zero value", |
| 80 | + json: `{"type":"0x7d","input":"0xc201c0","value":"0x1"}`, |
| 81 | + expectedError: "post-exec transaction value must be 0", |
| 82 | + }, |
| 83 | + { |
| 84 | + name: "non-zero gas", |
| 85 | + json: `{"type":"0x7d","input":"0xc201c0","gas":"0x1"}`, |
| 86 | + expectedError: "post-exec transaction gas must be 0", |
| 87 | + }, |
| 88 | + { |
| 89 | + name: "non-zero v", |
| 90 | + json: `{"type":"0x7d","input":"0xc201c0","v":"0x1","r":"0x0","s":"0x0"}`, |
| 91 | + expectedError: "post-exec transaction signature must be 0 or unset", |
| 92 | + }, |
| 93 | + { |
| 94 | + name: "non-zero r", |
| 95 | + json: `{"type":"0x7d","input":"0xc201c0","v":"0x0","r":"0x1","s":"0x0"}`, |
| 96 | + expectedError: "post-exec transaction signature must be 0 or unset", |
| 97 | + }, |
| 98 | + { |
| 99 | + name: "non-zero s", |
| 100 | + json: `{"type":"0x7d","input":"0xc201c0","v":"0x0","r":"0x0","s":"0x1"}`, |
| 101 | + expectedError: "post-exec transaction signature must be 0 or unset", |
| 102 | + }, |
| 103 | + { |
| 104 | + name: "missing input", |
| 105 | + json: `{"type":"0x7d"}`, |
| 106 | + expectedError: "missing required field 'input'", |
| 107 | + }, |
| 108 | + } |
| 109 | + for _, test := range tests { |
| 110 | + t.Run(test.name, func(t *testing.T) { |
| 111 | + var tx Transaction |
| 112 | + err := json.Unmarshal([]byte(test.json), &tx) |
| 113 | + require.ErrorContains(t, err, test.expectedError) |
| 114 | + }) |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +func TestPostExecTxRoundTrips(t *testing.T) { |
| 119 | + original := NewTx(&PostExecTx{Data: hexutil.MustDecode("0xc201c0")}) |
| 120 | + |
| 121 | + jsonBytes, err := original.MarshalJSON() |
| 122 | + require.NoError(t, err) |
| 123 | + |
| 124 | + var fromJSON Transaction |
| 125 | + require.NoError(t, fromJSON.UnmarshalJSON(jsonBytes)) |
| 126 | + require.Equal(t, original.Type(), fromJSON.Type()) |
| 127 | + require.Equal(t, original.Hash(), fromJSON.Hash()) |
| 128 | + require.Equal(t, original.Data(), fromJSON.Data()) |
| 129 | + |
| 130 | + bin, err := original.MarshalBinary() |
| 131 | + require.NoError(t, err) |
| 132 | + require.Equal(t, hexutil.MustDecode("0x7dc201c0"), bin) |
| 133 | + |
| 134 | + var fromBinary Transaction |
| 135 | + require.NoError(t, fromBinary.UnmarshalBinary(bin)) |
| 136 | + require.Equal(t, original.Type(), fromBinary.Type()) |
| 137 | + require.Equal(t, original.Hash(), fromBinary.Hash()) |
| 138 | + require.Equal(t, original.Data(), fromBinary.Data()) |
| 139 | +} |
| 140 | + |
| 141 | +func TestPostExecTxSenderIsZeroAddress(t *testing.T) { |
| 142 | + tx := NewTx(&PostExecTx{Data: hexutil.MustDecode("0xc201c0")}) |
| 143 | + |
| 144 | + signer := NewLondonSigner(big.NewInt(123)) |
| 145 | + sender, err := signer.Sender(tx) |
| 146 | + require.NoError(t, err) |
| 147 | + require.Equal(t, common.Address{}, sender) |
| 148 | +} |
0 commit comments