github.com/tacshi/go-ethereum@v0.0.0-20230616113857-84a434e20921/internal/ethapi/api_test.go (about) 1 // Copyright 2023 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package ethapi 18 19 import ( 20 "encoding/json" 21 "math/big" 22 "testing" 23 24 "github.com/tacshi/go-ethereum/common" 25 "github.com/tacshi/go-ethereum/core/types" 26 "github.com/tacshi/go-ethereum/crypto" 27 "github.com/tacshi/go-ethereum/params" 28 ) 29 30 func TestTransaction_RoundTripRpcJSON(t *testing.T) { 31 var ( 32 config = params.AllEthashProtocolChanges 33 signer = types.LatestSigner(config) 34 key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") 35 tests = allTransactionTypes(common.Address{0xde, 0xad}, config) 36 ) 37 t.Parallel() 38 for i, tt := range tests { 39 var tx2 types.Transaction 40 tx, err := types.SignNewTx(key, signer, tt) 41 if err != nil { 42 t.Fatalf("test %d: signing failed: %v", i, err) 43 } 44 // Regular transaction 45 if data, err := json.Marshal(tx); err != nil { 46 t.Fatalf("test %d: marshalling failed; %v", i, err) 47 } else if err = tx2.UnmarshalJSON(data); err != nil { 48 t.Fatalf("test %d: sunmarshal failed: %v", i, err) 49 } else if want, have := tx.Hash(), tx2.Hash(); want != have { 50 t.Fatalf("test %d: stx changed, want %x have %x", i, want, have) 51 } 52 53 // rpcTransaction 54 rpcTx := newRPCTransaction(tx, common.Hash{}, 0, 0, nil, config) 55 if data, err := json.Marshal(rpcTx); err != nil { 56 t.Fatalf("test %d: marshalling failed; %v", i, err) 57 } else if err = tx2.UnmarshalJSON(data); err != nil { 58 t.Fatalf("test %d: unmarshal failed: %v", i, err) 59 } else if want, have := tx.Hash(), tx2.Hash(); want != have { 60 t.Fatalf("test %d: tx changed, want %x have %x", i, want, have) 61 } 62 } 63 } 64 65 func allTransactionTypes(addr common.Address, config *params.ChainConfig) []types.TxData { 66 return []types.TxData{ 67 &types.LegacyTx{ 68 Nonce: 5, 69 GasPrice: big.NewInt(6), 70 Gas: 7, 71 To: &addr, 72 Value: big.NewInt(8), 73 Data: []byte{0, 1, 2, 3, 4}, 74 V: big.NewInt(9), 75 R: big.NewInt(10), 76 S: big.NewInt(11), 77 }, 78 &types.LegacyTx{ 79 Nonce: 5, 80 GasPrice: big.NewInt(6), 81 Gas: 7, 82 To: nil, 83 Value: big.NewInt(8), 84 Data: []byte{0, 1, 2, 3, 4}, 85 V: big.NewInt(32), 86 R: big.NewInt(10), 87 S: big.NewInt(11), 88 }, 89 &types.AccessListTx{ 90 ChainID: config.ChainID, 91 Nonce: 5, 92 GasPrice: big.NewInt(6), 93 Gas: 7, 94 To: &addr, 95 Value: big.NewInt(8), 96 Data: []byte{0, 1, 2, 3, 4}, 97 AccessList: types.AccessList{ 98 types.AccessTuple{ 99 Address: common.Address{0x2}, 100 StorageKeys: []common.Hash{types.EmptyRootHash}, 101 }, 102 }, 103 V: big.NewInt(32), 104 R: big.NewInt(10), 105 S: big.NewInt(11), 106 }, 107 &types.AccessListTx{ 108 ChainID: config.ChainID, 109 Nonce: 5, 110 GasPrice: big.NewInt(6), 111 Gas: 7, 112 To: nil, 113 Value: big.NewInt(8), 114 Data: []byte{0, 1, 2, 3, 4}, 115 AccessList: types.AccessList{ 116 types.AccessTuple{ 117 Address: common.Address{0x2}, 118 StorageKeys: []common.Hash{types.EmptyRootHash}, 119 }, 120 }, 121 V: big.NewInt(32), 122 R: big.NewInt(10), 123 S: big.NewInt(11), 124 }, 125 &types.DynamicFeeTx{ 126 ChainID: config.ChainID, 127 Nonce: 5, 128 GasTipCap: big.NewInt(6), 129 GasFeeCap: big.NewInt(9), 130 Gas: 7, 131 To: &addr, 132 Value: big.NewInt(8), 133 Data: []byte{0, 1, 2, 3, 4}, 134 AccessList: types.AccessList{ 135 types.AccessTuple{ 136 Address: common.Address{0x2}, 137 StorageKeys: []common.Hash{types.EmptyRootHash}, 138 }, 139 }, 140 V: big.NewInt(32), 141 R: big.NewInt(10), 142 S: big.NewInt(11), 143 }, 144 &types.DynamicFeeTx{ 145 ChainID: config.ChainID, 146 Nonce: 5, 147 GasTipCap: big.NewInt(6), 148 GasFeeCap: big.NewInt(9), 149 Gas: 7, 150 To: nil, 151 Value: big.NewInt(8), 152 Data: []byte{0, 1, 2, 3, 4}, 153 AccessList: types.AccessList{}, 154 V: big.NewInt(32), 155 R: big.NewInt(10), 156 S: big.NewInt(11), 157 }, 158 } 159 }