github.com/snowblossomcoin/go-ethereum@v1.9.25/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  	"time"
    26  
    27  	"github.com/ethereum/go-ethereum/common"
    28  	"github.com/ethereum/go-ethereum/crypto"
    29  	"github.com/ethereum/go-ethereum/rlp"
    30  )
    31  
    32  // The values in those tests are from the Transaction Tests
    33  // at github.com/ethereum/tests.
    34  var (
    35  	emptyTx = NewTransaction(
    36  		0,
    37  		common.HexToAddress("095e7baea6a6c7c4c2dfeb977efac326af552d87"),
    38  		big.NewInt(0), 0, big.NewInt(0),
    39  		nil,
    40  	)
    41  
    42  	rightvrsTx, _ = NewTransaction(
    43  		3,
    44  		common.HexToAddress("b94f5374fce5edbc8e2a8697c15331677e6ebf0b"),
    45  		big.NewInt(10),
    46  		2000,
    47  		big.NewInt(1),
    48  		common.FromHex("5544"),
    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.Fatal(err)
    94  	}
    95  
    96  	from, err := Sender(HomesteadSigner{}, tx)
    97  	if err != nil {
    98  		t.Fatal(err)
    99  	}
   100  	if addr != from {
   101  		t.Fatal("derived address doesn't match")
   102  	}
   103  }
   104  
   105  func TestRecipientNormal(t *testing.T) {
   106  	_, addr := defaultTestKey()
   107  
   108  	tx, err := decodeTx(common.Hex2Bytes("f85d80808094000000000000000000000000000000000000000080011ca0527c0d8f5c63f7b9f41324a7c8a563ee1190bcbf0dac8ab446291bdbf32f5c79a0552c4ef0a09a04395074dab9ed34d3fbfb843c2f2546cc30fe89ec143ca94ca6"))
   109  	if err != nil {
   110  		t.Fatal(err)
   111  	}
   112  
   113  	from, err := Sender(HomesteadSigner{}, tx)
   114  	if err != nil {
   115  		t.Fatal(err)
   116  	}
   117  	if addr != from {
   118  		t.Fatal("derived address doesn't match")
   119  	}
   120  }
   121  
   122  // Tests that transactions can be correctly sorted according to their price in
   123  // decreasing order, but at the same time with increasing nonces when issued by
   124  // the same account.
   125  func TestTransactionPriceNonceSort(t *testing.T) {
   126  	// Generate a batch of accounts to start with
   127  	keys := make([]*ecdsa.PrivateKey, 25)
   128  	for i := 0; i < len(keys); i++ {
   129  		keys[i], _ = crypto.GenerateKey()
   130  	}
   131  	signer := HomesteadSigner{}
   132  
   133  	// Generate a batch of transactions with overlapping values, but shifted nonces
   134  	groups := map[common.Address]Transactions{}
   135  	for start, key := range keys {
   136  		addr := crypto.PubkeyToAddress(key.PublicKey)
   137  		for i := 0; i < 25; i++ {
   138  			tx, _ := SignTx(NewTransaction(uint64(start+i), common.Address{}, big.NewInt(100), 100, big.NewInt(int64(start+i)), nil), signer, key)
   139  			groups[addr] = append(groups[addr], tx)
   140  		}
   141  	}
   142  	// Sort the transactions and cross check the nonce ordering
   143  	txset := NewTransactionsByPriceAndNonce(signer, groups)
   144  
   145  	txs := Transactions{}
   146  	for tx := txset.Peek(); tx != nil; tx = txset.Peek() {
   147  		txs = append(txs, tx)
   148  		txset.Shift()
   149  	}
   150  	if len(txs) != 25*25 {
   151  		t.Errorf("expected %d transactions, found %d", 25*25, len(txs))
   152  	}
   153  	for i, txi := range txs {
   154  		fromi, _ := Sender(signer, txi)
   155  
   156  		// Make sure the nonce order is valid
   157  		for j, txj := range txs[i+1:] {
   158  			fromj, _ := Sender(signer, txj)
   159  			if fromi == fromj && txi.Nonce() > txj.Nonce() {
   160  				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())
   161  			}
   162  		}
   163  		// If the next tx has different from account, the price must be lower than the current one
   164  		if i+1 < len(txs) {
   165  			next := txs[i+1]
   166  			fromNext, _ := Sender(signer, next)
   167  			if fromi != fromNext && txi.GasPrice().Cmp(next.GasPrice()) < 0 {
   168  				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())
   169  			}
   170  		}
   171  	}
   172  }
   173  
   174  // Tests that if multiple transactions have the same price, the ones seen earlier
   175  // are prioritized to avoid network spam attacks aiming for a specific ordering.
   176  func TestTransactionTimeSort(t *testing.T) {
   177  	// Generate a batch of accounts to start with
   178  	keys := make([]*ecdsa.PrivateKey, 5)
   179  	for i := 0; i < len(keys); i++ {
   180  		keys[i], _ = crypto.GenerateKey()
   181  	}
   182  	signer := HomesteadSigner{}
   183  
   184  	// Generate a batch of transactions with overlapping prices, but different creation times
   185  	groups := map[common.Address]Transactions{}
   186  	for start, key := range keys {
   187  		addr := crypto.PubkeyToAddress(key.PublicKey)
   188  
   189  		tx, _ := SignTx(NewTransaction(0, common.Address{}, big.NewInt(100), 100, big.NewInt(1), nil), signer, key)
   190  		tx.time = time.Unix(0, int64(len(keys)-start))
   191  
   192  		groups[addr] = append(groups[addr], tx)
   193  	}
   194  	// Sort the transactions and cross check the nonce ordering
   195  	txset := NewTransactionsByPriceAndNonce(signer, groups)
   196  
   197  	txs := Transactions{}
   198  	for tx := txset.Peek(); tx != nil; tx = txset.Peek() {
   199  		txs = append(txs, tx)
   200  		txset.Shift()
   201  	}
   202  	if len(txs) != len(keys) {
   203  		t.Errorf("expected %d transactions, found %d", len(keys), len(txs))
   204  	}
   205  	for i, txi := range txs {
   206  		fromi, _ := Sender(signer, txi)
   207  		if i+1 < len(txs) {
   208  			next := txs[i+1]
   209  			fromNext, _ := Sender(signer, next)
   210  
   211  			if txi.GasPrice().Cmp(next.GasPrice()) < 0 {
   212  				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())
   213  			}
   214  			// Make sure time order is ascending if the txs have the same gas price
   215  			if txi.GasPrice().Cmp(next.GasPrice()) == 0 && txi.time.After(next.time) {
   216  				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)
   217  			}
   218  		}
   219  	}
   220  }
   221  
   222  // TestTransactionJSON tests serializing/de-serializing to/from JSON.
   223  func TestTransactionJSON(t *testing.T) {
   224  	key, err := crypto.GenerateKey()
   225  	if err != nil {
   226  		t.Fatalf("could not generate key: %v", err)
   227  	}
   228  	signer := NewEIP155Signer(common.Big1)
   229  
   230  	transactions := make([]*Transaction, 0, 50)
   231  	for i := uint64(0); i < 25; i++ {
   232  		var tx *Transaction
   233  		switch i % 2 {
   234  		case 0:
   235  			tx = NewTransaction(i, common.Address{1}, common.Big0, 1, common.Big2, []byte("abcdef"))
   236  		case 1:
   237  			tx = NewContractCreation(i, common.Big0, 1, common.Big2, []byte("abcdef"))
   238  		}
   239  		transactions = append(transactions, tx)
   240  
   241  		signedTx, err := SignTx(tx, signer, key)
   242  		if err != nil {
   243  			t.Fatalf("could not sign transaction: %v", err)
   244  		}
   245  
   246  		transactions = append(transactions, signedTx)
   247  	}
   248  
   249  	for _, tx := range transactions {
   250  		data, err := json.Marshal(tx)
   251  		if err != nil {
   252  			t.Fatalf("json.Marshal failed: %v", err)
   253  		}
   254  
   255  		var parsedTx *Transaction
   256  		if err := json.Unmarshal(data, &parsedTx); err != nil {
   257  			t.Fatalf("json.Unmarshal failed: %v", err)
   258  		}
   259  
   260  		// compare nonce, price, gaslimit, recipient, amount, payload, V, R, S
   261  		if tx.Hash() != parsedTx.Hash() {
   262  			t.Errorf("parsed tx differs from original tx, want %v, got %v", tx, parsedTx)
   263  		}
   264  		if tx.ChainId().Cmp(parsedTx.ChainId()) != 0 {
   265  			t.Errorf("invalid chain id, want %d, got %d", tx.ChainId(), parsedTx.ChainId())
   266  		}
   267  	}
   268  }