github.com/insight-chain/inb-go@v1.1.3-0.20191221022159-da049980ae38/core/types/transaction_test.go (about) 1 // Copyright 2014 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 types 18 19 import ( 20 "bytes" 21 "crypto/ecdsa" 22 "encoding/json" 23 "math/big" 24 "testing" 25 26 "github.com/insight-chain/inb-go/common" 27 "github.com/insight-chain/inb-go/crypto" 28 "github.com/insight-chain/inb-go/rlp" 29 ) 30 31 // The values in those tests are from the Transaction Tests 32 // at github.com/ethereum/tests. 33 var ( 34 emptyTx = NewTransaction( 35 0, 36 common.HexToAddress("095e7baea6a6c7c4c2dfeb977efac326af552d87"), 37 big.NewInt(0), 0, 38 nil, 39 Ordinary, 40 ) 41 42 rightvrsTx, _ = NewTransaction( 43 3, 44 common.HexToAddress("b94f5374fce5edbc8e2a8697c15331677e6ebf0b"), 45 big.NewInt(10), 46 2000, 47 common.FromHex("5544"), 48 Ordinary, 49 ).WithSignature( 50 HomesteadSigner{}, 51 common.Hex2Bytes("98ff921201554726367d2be8c804a7ff89ccf285ebc57dff8ae4c44b9c19ac4a8887321be575c8095f789dd4c743dfe42c1820f9231f98a962b210e3ac2452a301"), 52 ) 53 ) 54 55 func TestTransactionSigHash(t *testing.T) { 56 var homestead HomesteadSigner 57 if homestead.Hash(emptyTx) != common.HexToHash("c775b99e7ad12f50d819fcd602390467e28141316969f4b57f0626f74fe3b386") { 58 t.Errorf("empty transaction hash mismatch, got %x", emptyTx.Hash()) 59 } 60 if homestead.Hash(rightvrsTx) != common.HexToHash("fe7a79529ed5f7c3375d06b26b186a8644e0e16c373d7a12be41c62d6042b77a") { 61 t.Errorf("RightVRS transaction hash mismatch, got %x", rightvrsTx.Hash()) 62 } 63 } 64 65 func TestTransactionEncode(t *testing.T) { 66 txb, err := rlp.EncodeToBytes(rightvrsTx) 67 if err != nil { 68 t.Fatalf("encode error: %v", err) 69 } 70 should := common.FromHex("f86103018207d094b94f5374fce5edbc8e2a8697c15331677e6ebf0b0a8255441ca098ff921201554726367d2be8c804a7ff89ccf285ebc57dff8ae4c44b9c19ac4aa08887321be575c8095f789dd4c743dfe42c1820f9231f98a962b210e3ac2452a3") 71 if !bytes.Equal(txb, should) { 72 t.Errorf("encoded RLP mismatch, got %x", txb) 73 } 74 } 75 76 func decodeTx(data []byte) (*Transaction, error) { 77 var tx Transaction 78 t, err := &tx, rlp.Decode(bytes.NewReader(data), &tx) 79 80 return t, err 81 } 82 83 func defaultTestKey() (*ecdsa.PrivateKey, common.Address) { 84 key, _ := crypto.HexToECDSA("45a915e4d060149eb4365960e6a7a45f334393093061116b197e3240065ff2d8") 85 addr := crypto.PubkeyToAddress(key.PublicKey) 86 return key, addr 87 } 88 89 func TestRecipientEmpty(t *testing.T) { 90 _, addr := defaultTestKey() 91 tx, err := decodeTx(common.Hex2Bytes("f8498080808080011ca09b16de9d5bdee2cf56c28d16275a4da68cd30273e2525f3959f5d62557489921a0372ebd8fb3345f7db7b5a86d42e24d36e983e259b0664ceb8c227ec9af572f3d")) 92 if err != nil { 93 t.Error(err) 94 t.FailNow() 95 } 96 97 from, err := Sender(HomesteadSigner{}, tx) 98 if err != nil { 99 t.Error(err) 100 t.FailNow() 101 } 102 if addr != from { 103 t.Error("derived address doesn't match") 104 } 105 } 106 107 func TestRecipientNormal(t *testing.T) { 108 _, addr := defaultTestKey() 109 110 tx, err := decodeTx(common.Hex2Bytes("f85d80808094000000000000000000000000000000000000000080011ca0527c0d8f5c63f7b9f41324a7c8a563ee1190bcbf0dac8ab446291bdbf32f5c79a0552c4ef0a09a04395074dab9ed34d3fbfb843c2f2546cc30fe89ec143ca94ca6")) 111 if err != nil { 112 t.Error(err) 113 t.FailNow() 114 } 115 116 from, err := Sender(HomesteadSigner{}, tx) 117 if err != nil { 118 t.Error(err) 119 t.FailNow() 120 } 121 122 if addr != from { 123 t.Error("derived address doesn't match") 124 } 125 } 126 127 // Tests that transactions can be correctly sorted according to their price in 128 // decreasing order, but at the same time with increasing nonces when issued by 129 // the same account. 130 func TestTransactionPriceNonceSort(t *testing.T) { 131 // Generate a batch of accounts to start with 132 keys := make([]*ecdsa.PrivateKey, 25) 133 for i := 0; i < len(keys); i++ { 134 keys[i], _ = crypto.GenerateKey() 135 } 136 137 signer := HomesteadSigner{} 138 // Generate a batch of transactions with overlapping values, but shifted nonces 139 groups := map[common.Address]Transactions{} 140 for start, key := range keys { 141 addr := crypto.PubkeyToAddress(key.PublicKey) 142 for i := 0; i < 25; i++ { 143 tx, _ := SignTx(NewTransaction(uint64(start+i), common.Address{}, big.NewInt(100), 100, nil, Ordinary), signer, key) 144 groups[addr] = append(groups[addr], tx) 145 } 146 } 147 // Sort the transactions and cross check the nonce ordering 148 txset := NewTransactionsByPriceAndNonce(signer, groups) 149 150 txs := Transactions{} 151 for tx := txset.Peek(); tx != nil; tx = txset.Peek() { 152 txs = append(txs, tx) 153 txset.Shift() 154 } 155 if len(txs) != 25*25 { 156 t.Errorf("expected %d transactions, found %d", 25*25, len(txs)) 157 } 158 for i, txi := range txs { 159 fromi, _ := Sender(signer, txi) 160 161 // Make sure the nonce order is valid 162 for j, txj := range txs[i+1:] { 163 fromj, _ := Sender(signer, txj) 164 165 if fromi == fromj && txi.Nonce() > txj.Nonce() { 166 t.Errorf("invalid nonce ordering: tx #%d (A=%x N=%v) < tx #%d (A=%x N=%v)", i, fromi[:4], txi.Nonce(), i+j, fromj[:4], txj.Nonce()) 167 } 168 } 169 170 // If the next tx has different from account, the price must be lower than the current one 171 if i+1 < len(txs) { 172 next := txs[i+1] 173 fromNext, _ := Sender(signer, next) 174 //if fromi != fromNext && txi.GasPrice().Cmp(next.GasPrice()) < 0 { 175 if fromi != fromNext { 176 //t.Errorf("invalid gasprice ordering: tx #%d (A=%x P=%v) < tx #%d (A=%x P=%v)", i, fromi[:4], txi.GasPrice(), i+1, fromNext[:4], next.GasPrice()) 177 } 178 } 179 } 180 } 181 182 // TestTransactionJSON tests serializing/de-serializing to/from JSON. 183 func TestTransactionJSON(t *testing.T) { 184 key, err := crypto.GenerateKey() 185 if err != nil { 186 t.Fatalf("could not generate key: %v", err) 187 } 188 signer := NewEIP155Signer(common.Big1) 189 190 transactions := make([]*Transaction, 0, 50) 191 for i := uint64(0); i < 25; i++ { 192 var tx *Transaction 193 switch i % 2 { 194 case 0: 195 tx = NewTransaction(i, common.Address{1}, common.Big0, 1, []byte("abcdef"), Ordinary) 196 case 1: 197 tx = NewContractCreation(i, common.Big0, 1, []byte("abcdef")) 198 } 199 transactions = append(transactions, tx) 200 201 signedTx, err := SignTx(tx, signer, key) 202 if err != nil { 203 t.Fatalf("could not sign transaction: %v", err) 204 } 205 206 transactions = append(transactions, signedTx) 207 } 208 209 for _, tx := range transactions { 210 data, err := json.Marshal(tx) 211 if err != nil { 212 t.Fatalf("json.Marshal failed: %v", err) 213 } 214 215 var parsedTx *Transaction 216 if err := json.Unmarshal(data, &parsedTx); err != nil { 217 t.Fatalf("json.Unmarshal failed: %v", err) 218 } 219 220 // compare nonce, price, gaslimit, recipient, amount, payload, V, R, S 221 if tx.Hash() != parsedTx.Hash() { 222 t.Errorf("parsed tx differs from original tx, want %v, got %v", tx, parsedTx) 223 } 224 if tx.ChainId().Cmp(parsedTx.ChainId()) != 0 { 225 t.Errorf("invalid chain id, want %d, got %d", tx.ChainId(), parsedTx.ChainId()) 226 } 227 } 228 }