github.com/EgonCoin/EgonChain@v1.10.16/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 "fmt" 24 "math/big" 25 "math/rand" 26 "reflect" 27 "testing" 28 "time" 29 30 "github.com/EgonCoin/EgonChain/common" 31 "github.com/EgonCoin/EgonChain/crypto" 32 "github.com/EgonCoin/EgonChain/rlp" 33 ) 34 35 // The values in those tests are from the Transaction Tests 36 // at github.com/ethereum/tests. 37 var ( 38 testAddr = common.HexToAddress("b94f5374fce5edbc8e2a8697c15331677e6ebf0b") 39 40 emptyTx = NewTransaction( 41 0, 42 common.HexToAddress("095e7baea6a6c7c4c2dfeb977efac326af552d87"), 43 big.NewInt(0), 0, big.NewInt(0), 44 nil, 45 ) 46 47 rightvrsTx, _ = NewTransaction( 48 3, 49 testAddr, 50 big.NewInt(10), 51 2000, 52 big.NewInt(1), 53 common.FromHex("5544"), 54 ).WithSignature( 55 HomesteadSigner{}, 56 common.Hex2Bytes("98ff921201554726367d2be8c804a7ff89ccf285ebc57dff8ae4c44b9c19ac4a8887321be575c8095f789dd4c743dfe42c1820f9231f98a962b210e3ac2452a301"), 57 ) 58 59 emptyEip2718Tx = NewTx(&AccessListTx{ 60 ChainID: big.NewInt(1), 61 Nonce: 3, 62 To: &testAddr, 63 Value: big.NewInt(10), 64 Gas: 25000, 65 GasPrice: big.NewInt(1), 66 Data: common.FromHex("5544"), 67 }) 68 69 signedEip2718Tx, _ = emptyEip2718Tx.WithSignature( 70 NewEIP2930Signer(big.NewInt(1)), 71 common.Hex2Bytes("c9519f4f2b30335884581971573fadf60c6204f59a911df35ee8a540456b266032f1e8e2c5dd761f9e4f88f41c8310aeaba26a8bfcdacfedfa12ec3862d3752101"), 72 ) 73 ) 74 75 func TestDecodeEmptyTypedTx(t *testing.T) { 76 input := []byte{0x80} 77 var tx Transaction 78 err := rlp.DecodeBytes(input, &tx) 79 if err != errEmptyTypedTx { 80 t.Fatal("wrong error:", err) 81 } 82 } 83 84 func TestTransactionSigHash(t *testing.T) { 85 var homestead HomesteadSigner 86 if homestead.Hash(emptyTx) != common.HexToHash("c775b99e7ad12f50d819fcd602390467e28141316969f4b57f0626f74fe3b386") { 87 t.Errorf("empty transaction hash mismatch, got %x", emptyTx.Hash()) 88 } 89 if homestead.Hash(rightvrsTx) != common.HexToHash("fe7a79529ed5f7c3375d06b26b186a8644e0e16c373d7a12be41c62d6042b77a") { 90 t.Errorf("RightVRS transaction hash mismatch, got %x", rightvrsTx.Hash()) 91 } 92 } 93 94 func TestTransactionEncode(t *testing.T) { 95 txb, err := rlp.EncodeToBytes(rightvrsTx) 96 if err != nil { 97 t.Fatalf("encode error: %v", err) 98 } 99 should := common.FromHex("f86103018207d094b94f5374fce5edbc8e2a8697c15331677e6ebf0b0a8255441ca098ff921201554726367d2be8c804a7ff89ccf285ebc57dff8ae4c44b9c19ac4aa08887321be575c8095f789dd4c743dfe42c1820f9231f98a962b210e3ac2452a3") 100 if !bytes.Equal(txb, should) { 101 t.Errorf("encoded RLP mismatch, got %x", txb) 102 } 103 } 104 105 func TestEIP2718TransactionSigHash(t *testing.T) { 106 s := NewEIP2930Signer(big.NewInt(1)) 107 if s.Hash(emptyEip2718Tx) != common.HexToHash("49b486f0ec0a60dfbbca2d30cb07c9e8ffb2a2ff41f29a1ab6737475f6ff69f3") { 108 t.Errorf("empty EIP-2718 transaction hash mismatch, got %x", s.Hash(emptyEip2718Tx)) 109 } 110 if s.Hash(signedEip2718Tx) != common.HexToHash("49b486f0ec0a60dfbbca2d30cb07c9e8ffb2a2ff41f29a1ab6737475f6ff69f3") { 111 t.Errorf("signed EIP-2718 transaction hash mismatch, got %x", s.Hash(signedEip2718Tx)) 112 } 113 } 114 115 // This test checks signature operations on access list transactions. 116 func TestEIP2930Signer(t *testing.T) { 117 118 var ( 119 key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") 120 keyAddr = crypto.PubkeyToAddress(key.PublicKey) 121 signer1 = NewEIP2930Signer(big.NewInt(1)) 122 signer2 = NewEIP2930Signer(big.NewInt(2)) 123 tx0 = NewTx(&AccessListTx{Nonce: 1}) 124 tx1 = NewTx(&AccessListTx{ChainID: big.NewInt(1), Nonce: 1}) 125 tx2, _ = SignNewTx(key, signer2, &AccessListTx{ChainID: big.NewInt(2), Nonce: 1}) 126 ) 127 128 tests := []struct { 129 tx *Transaction 130 signer Signer 131 wantSignerHash common.Hash 132 wantSenderErr error 133 wantSignErr error 134 wantHash common.Hash // after signing 135 }{ 136 { 137 tx: tx0, 138 signer: signer1, 139 wantSignerHash: common.HexToHash("846ad7672f2a3a40c1f959cd4a8ad21786d620077084d84c8d7c077714caa139"), 140 wantSenderErr: ErrInvalidChainId, 141 wantHash: common.HexToHash("1ccd12d8bbdb96ea391af49a35ab641e219b2dd638dea375f2bc94dd290f2549"), 142 }, 143 { 144 tx: tx1, 145 signer: signer1, 146 wantSenderErr: ErrInvalidSig, 147 wantSignerHash: common.HexToHash("846ad7672f2a3a40c1f959cd4a8ad21786d620077084d84c8d7c077714caa139"), 148 wantHash: common.HexToHash("1ccd12d8bbdb96ea391af49a35ab641e219b2dd638dea375f2bc94dd290f2549"), 149 }, 150 { 151 // This checks what happens when trying to sign an unsigned tx for the wrong chain. 152 tx: tx1, 153 signer: signer2, 154 wantSenderErr: ErrInvalidChainId, 155 wantSignerHash: common.HexToHash("367967247499343401261d718ed5aa4c9486583e4d89251afce47f4a33c33362"), 156 wantSignErr: ErrInvalidChainId, 157 }, 158 { 159 // This checks what happens when trying to re-sign a signed tx for the wrong chain. 160 tx: tx2, 161 signer: signer1, 162 wantSenderErr: ErrInvalidChainId, 163 wantSignerHash: common.HexToHash("846ad7672f2a3a40c1f959cd4a8ad21786d620077084d84c8d7c077714caa139"), 164 wantSignErr: ErrInvalidChainId, 165 }, 166 } 167 168 for i, test := range tests { 169 sigHash := test.signer.Hash(test.tx) 170 if sigHash != test.wantSignerHash { 171 t.Errorf("test %d: wrong sig hash: got %x, want %x", i, sigHash, test.wantSignerHash) 172 } 173 sender, err := Sender(test.signer, test.tx) 174 if err != test.wantSenderErr { 175 t.Errorf("test %d: wrong Sender error %q", i, err) 176 } 177 if err == nil && sender != keyAddr { 178 t.Errorf("test %d: wrong sender address %x", i, sender) 179 } 180 signedTx, err := SignTx(test.tx, test.signer, key) 181 if err != test.wantSignErr { 182 t.Fatalf("test %d: wrong SignTx error %q", i, err) 183 } 184 if signedTx != nil { 185 if signedTx.Hash() != test.wantHash { 186 t.Errorf("test %d: wrong tx hash after signing: got %x, want %x", i, signedTx.Hash(), test.wantHash) 187 } 188 } 189 } 190 } 191 192 func TestEIP2718TransactionEncode(t *testing.T) { 193 // RLP representation 194 { 195 have, err := rlp.EncodeToBytes(signedEip2718Tx) 196 if err != nil { 197 t.Fatalf("encode error: %v", err) 198 } 199 want := common.FromHex("b86601f8630103018261a894b94f5374fce5edbc8e2a8697c15331677e6ebf0b0a825544c001a0c9519f4f2b30335884581971573fadf60c6204f59a911df35ee8a540456b2660a032f1e8e2c5dd761f9e4f88f41c8310aeaba26a8bfcdacfedfa12ec3862d37521") 200 if !bytes.Equal(have, want) { 201 t.Errorf("encoded RLP mismatch, got %x", have) 202 } 203 } 204 // Binary representation 205 { 206 have, err := signedEip2718Tx.MarshalBinary() 207 if err != nil { 208 t.Fatalf("encode error: %v", err) 209 } 210 want := common.FromHex("01f8630103018261a894b94f5374fce5edbc8e2a8697c15331677e6ebf0b0a825544c001a0c9519f4f2b30335884581971573fadf60c6204f59a911df35ee8a540456b2660a032f1e8e2c5dd761f9e4f88f41c8310aeaba26a8bfcdacfedfa12ec3862d37521") 211 if !bytes.Equal(have, want) { 212 t.Errorf("encoded RLP mismatch, got %x", have) 213 } 214 } 215 } 216 217 func decodeTx(data []byte) (*Transaction, error) { 218 var tx Transaction 219 t, err := &tx, rlp.Decode(bytes.NewReader(data), &tx) 220 return t, err 221 } 222 223 func defaultTestKey() (*ecdsa.PrivateKey, common.Address) { 224 key, _ := crypto.HexToECDSA("45a915e4d060149eb4365960e6a7a45f334393093061116b197e3240065ff2d8") 225 addr := crypto.PubkeyToAddress(key.PublicKey) 226 return key, addr 227 } 228 229 func TestRecipientEmpty(t *testing.T) { 230 _, addr := defaultTestKey() 231 tx, err := decodeTx(common.Hex2Bytes("f8498080808080011ca09b16de9d5bdee2cf56c28d16275a4da68cd30273e2525f3959f5d62557489921a0372ebd8fb3345f7db7b5a86d42e24d36e983e259b0664ceb8c227ec9af572f3d")) 232 if err != nil { 233 t.Fatal(err) 234 } 235 236 from, err := Sender(HomesteadSigner{}, tx) 237 if err != nil { 238 t.Fatal(err) 239 } 240 if addr != from { 241 t.Fatal("derived address doesn't match") 242 } 243 } 244 245 func TestRecipientNormal(t *testing.T) { 246 _, addr := defaultTestKey() 247 248 tx, err := decodeTx(common.Hex2Bytes("f85d80808094000000000000000000000000000000000000000080011ca0527c0d8f5c63f7b9f41324a7c8a563ee1190bcbf0dac8ab446291bdbf32f5c79a0552c4ef0a09a04395074dab9ed34d3fbfb843c2f2546cc30fe89ec143ca94ca6")) 249 if err != nil { 250 t.Fatal(err) 251 } 252 253 from, err := Sender(HomesteadSigner{}, tx) 254 if err != nil { 255 t.Fatal(err) 256 } 257 if addr != from { 258 t.Fatal("derived address doesn't match") 259 } 260 } 261 262 func TestTransactionPriceNonceSortLegacy(t *testing.T) { 263 testTransactionPriceNonceSort(t, nil) 264 } 265 266 func TestTransactionPriceNonceSort1559(t *testing.T) { 267 testTransactionPriceNonceSort(t, big.NewInt(0)) 268 testTransactionPriceNonceSort(t, big.NewInt(5)) 269 testTransactionPriceNonceSort(t, big.NewInt(50)) 270 } 271 272 // Tests that transactions can be correctly sorted according to their price in 273 // decreasing order, but at the same time with increasing nonces when issued by 274 // the same account. 275 func testTransactionPriceNonceSort(t *testing.T, baseFee *big.Int) { 276 // Generate a batch of accounts to start with 277 keys := make([]*ecdsa.PrivateKey, 25) 278 for i := 0; i < len(keys); i++ { 279 keys[i], _ = crypto.GenerateKey() 280 } 281 signer := LatestSignerForChainID(common.Big1) 282 283 // Generate a batch of transactions with overlapping values, but shifted nonces 284 groups := map[common.Address]Transactions{} 285 expectedCount := 0 286 for start, key := range keys { 287 addr := crypto.PubkeyToAddress(key.PublicKey) 288 count := 25 289 for i := 0; i < 25; i++ { 290 var tx *Transaction 291 gasFeeCap := rand.Intn(50) 292 if baseFee == nil { 293 tx = NewTx(&LegacyTx{ 294 Nonce: uint64(start + i), 295 To: &common.Address{}, 296 Value: big.NewInt(100), 297 Gas: 100, 298 GasPrice: big.NewInt(int64(gasFeeCap)), 299 Data: nil, 300 }) 301 } else { 302 tx = NewTx(&DynamicFeeTx{ 303 Nonce: uint64(start + i), 304 To: &common.Address{}, 305 Value: big.NewInt(100), 306 Gas: 100, 307 GasFeeCap: big.NewInt(int64(gasFeeCap)), 308 GasTipCap: big.NewInt(int64(rand.Intn(gasFeeCap + 1))), 309 Data: nil, 310 }) 311 if count == 25 && int64(gasFeeCap) < baseFee.Int64() { 312 count = i 313 } 314 } 315 tx, err := SignTx(tx, signer, key) 316 if err != nil { 317 t.Fatalf("failed to sign tx: %s", err) 318 } 319 groups[addr] = append(groups[addr], tx) 320 } 321 expectedCount += count 322 } 323 // Sort the transactions and cross check the nonce ordering 324 txset := NewTransactionsByPriceAndNonce(signer, groups, baseFee) 325 326 txs := Transactions{} 327 for tx := txset.Peek(); tx != nil; tx = txset.Peek() { 328 txs = append(txs, tx) 329 txset.Shift() 330 } 331 if len(txs) != expectedCount { 332 t.Errorf("expected %d transactions, found %d", expectedCount, len(txs)) 333 } 334 for i, txi := range txs { 335 fromi, _ := Sender(signer, txi) 336 337 // Make sure the nonce order is valid 338 for j, txj := range txs[i+1:] { 339 fromj, _ := Sender(signer, txj) 340 if fromi == fromj && txi.Nonce() > txj.Nonce() { 341 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()) 342 } 343 } 344 // If the next tx has different from account, the price must be lower than the current one 345 if i+1 < len(txs) { 346 next := txs[i+1] 347 fromNext, _ := Sender(signer, next) 348 tip, err := txi.EffectiveGasTip(baseFee) 349 nextTip, nextErr := next.EffectiveGasTip(baseFee) 350 if err != nil || nextErr != nil { 351 t.Errorf("error calculating effective tip") 352 } 353 if fromi != fromNext && tip.Cmp(nextTip) < 0 { 354 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()) 355 } 356 } 357 } 358 } 359 360 // Tests that if multiple transactions have the same price, the ones seen earlier 361 // are prioritized to avoid network spam attacks aiming for a specific ordering. 362 func TestTransactionTimeSort(t *testing.T) { 363 // Generate a batch of accounts to start with 364 keys := make([]*ecdsa.PrivateKey, 5) 365 for i := 0; i < len(keys); i++ { 366 keys[i], _ = crypto.GenerateKey() 367 } 368 signer := HomesteadSigner{} 369 370 // Generate a batch of transactions with overlapping prices, but different creation times 371 groups := map[common.Address]Transactions{} 372 for start, key := range keys { 373 addr := crypto.PubkeyToAddress(key.PublicKey) 374 375 tx, _ := SignTx(NewTransaction(0, common.Address{}, big.NewInt(100), 100, big.NewInt(1), nil), signer, key) 376 tx.time = time.Unix(0, int64(len(keys)-start)) 377 378 groups[addr] = append(groups[addr], tx) 379 } 380 // Sort the transactions and cross check the nonce ordering 381 txset := NewTransactionsByPriceAndNonce(signer, groups, nil) 382 383 txs := Transactions{} 384 for tx := txset.Peek(); tx != nil; tx = txset.Peek() { 385 txs = append(txs, tx) 386 txset.Shift() 387 } 388 if len(txs) != len(keys) { 389 t.Errorf("expected %d transactions, found %d", len(keys), len(txs)) 390 } 391 for i, txi := range txs { 392 fromi, _ := Sender(signer, txi) 393 if i+1 < len(txs) { 394 next := txs[i+1] 395 fromNext, _ := Sender(signer, next) 396 397 if txi.GasPrice().Cmp(next.GasPrice()) < 0 { 398 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()) 399 } 400 // Make sure time order is ascending if the txs have the same gas price 401 if txi.GasPrice().Cmp(next.GasPrice()) == 0 && txi.time.After(next.time) { 402 t.Errorf("invalid received time ordering: tx #%d (A=%x T=%v) > tx #%d (A=%x T=%v)", i, fromi[:4], txi.time, i+1, fromNext[:4], next.time) 403 } 404 } 405 } 406 } 407 408 // TestTransactionCoding tests serializing/de-serializing to/from rlp and JSON. 409 func TestTransactionCoding(t *testing.T) { 410 key, err := crypto.GenerateKey() 411 if err != nil { 412 t.Fatalf("could not generate key: %v", err) 413 } 414 var ( 415 signer = NewEIP2930Signer(common.Big1) 416 addr = common.HexToAddress("0x0000000000000000000000000000000000000001") 417 recipient = common.HexToAddress("095e7baea6a6c7c4c2dfeb977efac326af552d87") 418 accesses = AccessList{{Address: addr, StorageKeys: []common.Hash{{0}}}} 419 ) 420 for i := uint64(0); i < 500; i++ { 421 var txdata TxData 422 switch i % 5 { 423 case 0: 424 // Legacy tx. 425 txdata = &LegacyTx{ 426 Nonce: i, 427 To: &recipient, 428 Gas: 1, 429 GasPrice: big.NewInt(2), 430 Data: []byte("abcdef"), 431 } 432 case 1: 433 // Legacy tx contract creation. 434 txdata = &LegacyTx{ 435 Nonce: i, 436 Gas: 1, 437 GasPrice: big.NewInt(2), 438 Data: []byte("abcdef"), 439 } 440 case 2: 441 // Tx with non-zero access list. 442 txdata = &AccessListTx{ 443 ChainID: big.NewInt(1), 444 Nonce: i, 445 To: &recipient, 446 Gas: 123457, 447 GasPrice: big.NewInt(10), 448 AccessList: accesses, 449 Data: []byte("abcdef"), 450 } 451 case 3: 452 // Tx with empty access list. 453 txdata = &AccessListTx{ 454 ChainID: big.NewInt(1), 455 Nonce: i, 456 To: &recipient, 457 Gas: 123457, 458 GasPrice: big.NewInt(10), 459 Data: []byte("abcdef"), 460 } 461 case 4: 462 // Contract creation with access list. 463 txdata = &AccessListTx{ 464 ChainID: big.NewInt(1), 465 Nonce: i, 466 Gas: 123457, 467 GasPrice: big.NewInt(10), 468 AccessList: accesses, 469 } 470 } 471 tx, err := SignNewTx(key, signer, txdata) 472 if err != nil { 473 t.Fatalf("could not sign transaction: %v", err) 474 } 475 // RLP 476 parsedTx, err := encodeDecodeBinary(tx) 477 if err != nil { 478 t.Fatal(err) 479 } 480 assertEqual(parsedTx, tx) 481 482 // JSON 483 parsedTx, err = encodeDecodeJSON(tx) 484 if err != nil { 485 t.Fatal(err) 486 } 487 assertEqual(parsedTx, tx) 488 } 489 } 490 491 func encodeDecodeJSON(tx *Transaction) (*Transaction, error) { 492 data, err := json.Marshal(tx) 493 if err != nil { 494 return nil, fmt.Errorf("json encoding failed: %v", err) 495 } 496 var parsedTx = &Transaction{} 497 if err := json.Unmarshal(data, &parsedTx); err != nil { 498 return nil, fmt.Errorf("json decoding failed: %v", err) 499 } 500 return parsedTx, nil 501 } 502 503 func encodeDecodeBinary(tx *Transaction) (*Transaction, error) { 504 data, err := tx.MarshalBinary() 505 if err != nil { 506 return nil, fmt.Errorf("rlp encoding failed: %v", err) 507 } 508 var parsedTx = &Transaction{} 509 if err := parsedTx.UnmarshalBinary(data); err != nil { 510 return nil, fmt.Errorf("rlp decoding failed: %v", err) 511 } 512 return parsedTx, nil 513 } 514 515 func assertEqual(orig *Transaction, cpy *Transaction) error { 516 // compare nonce, price, gaslimit, recipient, amount, payload, V, R, S 517 if want, got := orig.Hash(), cpy.Hash(); want != got { 518 return fmt.Errorf("parsed tx differs from original tx, want %v, got %v", want, got) 519 } 520 if want, got := orig.ChainId(), cpy.ChainId(); want.Cmp(got) != 0 { 521 return fmt.Errorf("invalid chain id, want %d, got %d", want, got) 522 } 523 if orig.AccessList() != nil { 524 if !reflect.DeepEqual(orig.AccessList(), cpy.AccessList()) { 525 return fmt.Errorf("access list wrong!") 526 } 527 } 528 return nil 529 }