github.com/core-coin/go-core/v2@v2.1.9/cmd/devp2p/internal/xcbtest/transaction.go (about)

     1  // Copyright 2020 by the Authors
     2  // This file is part of go-core.
     3  //
     4  // go-core is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU 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  // go-core 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 General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU General Public License
    15  // along with go-core. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package xcbtest
    18  
    19  import (
    20  	"math/big"
    21  	"time"
    22  
    23  	"github.com/core-coin/go-core/v2/common"
    24  	"github.com/core-coin/go-core/v2/core/types"
    25  	"github.com/core-coin/go-core/v2/crypto"
    26  	"github.com/core-coin/go-core/v2/internal/utesting"
    27  )
    28  
    29  // var faucetAddr = common.HexToAddress("0x71562b71999873DB5b286dF957af199Ec94617F7")
    30  var faucetKey, _ = crypto.UnmarshalPrivateKeyHex("89bdfaa2b6f9c30b94ee98fec96c58ff8507fabf49d36a6267e6cb5516eaa2a9e854eccc041f9f67e109d0eb4f653586855355c5b2b87bb313")
    31  
    32  func sendSuccessfulTx(t *utesting.T, s *Suite, tx *types.Transaction) {
    33  	sendConn := s.setupConnection(t)
    34  	t.Logf("sending tx: %v %v %v\n", tx.Hash().String(), tx.EnergyPrice(), tx.Energy())
    35  	// Send the transaction
    36  	if err := sendConn.Write(Transactions([]*types.Transaction{tx})); err != nil {
    37  		t.Fatal(err)
    38  	}
    39  	time.Sleep(100 * time.Millisecond)
    40  	recvConn := s.setupConnection(t)
    41  	// Wait for the transaction announcement
    42  	switch msg := recvConn.ReadAndServe(s.chain, timeout).(type) {
    43  	case *Transactions:
    44  		recTxs := *msg
    45  		if len(recTxs) < 1 {
    46  			t.Fatalf("received transactions do not match send: %v", recTxs)
    47  		}
    48  		if tx.Hash() != recTxs[len(recTxs)-1].Hash() {
    49  			t.Fatalf("received transactions do not match send: got %v want %v", recTxs, tx)
    50  		}
    51  	case *NewPooledTransactionHashes:
    52  		txHashes := *msg
    53  		if len(txHashes) < 1 {
    54  			t.Fatalf("received transactions do not match send: %v", txHashes)
    55  		}
    56  		if tx.Hash() != txHashes[len(txHashes)-1] {
    57  			t.Fatalf("wrong announcement received, wanted %v got %v", tx.Hash().Hex(), common.Bytes2Hex(txHashes[len(txHashes)-1][:]))
    58  		}
    59  	default:
    60  		t.Fatalf("unexpected message in sendSuccessfulTx: %s", pretty.Sdump(msg))
    61  	}
    62  }
    63  
    64  func sendFailingTx(t *utesting.T, s *Suite, tx *types.Transaction) {
    65  	sendConn, recvConn := s.setupConnection(t), s.setupConnection(t)
    66  	// Wait for a transaction announcement
    67  	switch msg := recvConn.ReadAndServe(s.chain, timeout).(type) {
    68  	case *NewPooledTransactionHashes:
    69  		break
    70  	default:
    71  		t.Logf("unexpected message, logging: %v", pretty.Sdump(msg))
    72  	}
    73  	// Send the transaction
    74  	if err := sendConn.Write(Transactions([]*types.Transaction{tx})); err != nil {
    75  		t.Fatal(err)
    76  	}
    77  	// Wait for another transaction announcement
    78  	switch msg := recvConn.ReadAndServe(s.chain, timeout).(type) {
    79  	case *Transactions:
    80  		t.Fatalf("Received unexpected transaction announcement: %v", msg)
    81  	case *NewPooledTransactionHashes:
    82  		t.Fatalf("Received unexpected pooledTx announcement: %v", msg)
    83  	case *Error:
    84  		// Transaction should not be announced -> wait for timeout
    85  		return
    86  	default:
    87  		t.Fatalf("unexpected message in sendFailingTx: %s", pretty.Sdump(msg))
    88  	}
    89  }
    90  
    91  func unknownTx(t *utesting.T, s *Suite) *types.Transaction {
    92  	tx := getNextTxFromChain(t, s)
    93  	var to common.Address
    94  	if tx.To() != nil {
    95  		to = *tx.To()
    96  	}
    97  	txNew := types.NewTransaction(tx.Nonce()+1, to, tx.Value(), tx.Energy(), tx.EnergyPrice(), tx.Data())
    98  	return signWithFaucet(t, txNew, s.chain.chainConfig.NetworkID)
    99  }
   100  
   101  func getNextTxFromChain(t *utesting.T, s *Suite) *types.Transaction {
   102  	// Get a new transaction
   103  	var tx *types.Transaction
   104  	for _, blocks := range s.fullChain.blocks[s.chain.Len():] {
   105  		txs := blocks.Transactions()
   106  		if txs.Len() != 0 {
   107  			tx = txs[0]
   108  			break
   109  		}
   110  	}
   111  	if tx == nil {
   112  		t.Fatal("could not find transaction")
   113  	}
   114  	return tx
   115  }
   116  
   117  func getOldTxFromChain(t *utesting.T, s *Suite) *types.Transaction {
   118  	var tx *types.Transaction
   119  	for _, blocks := range s.fullChain.blocks[:s.chain.Len()-1] {
   120  		txs := blocks.Transactions()
   121  		if txs.Len() != 0 {
   122  			tx = txs[0]
   123  			break
   124  		}
   125  	}
   126  	if tx == nil {
   127  		t.Fatal("could not find transaction")
   128  	}
   129  	return tx
   130  }
   131  
   132  func invalidNonceTx(t *utesting.T, s *Suite) *types.Transaction {
   133  	tx := getNextTxFromChain(t, s)
   134  	var to common.Address
   135  	if tx.To() != nil {
   136  		to = *tx.To()
   137  	}
   138  	txNew := types.NewTransaction(tx.Nonce()-2, to, tx.Value(), tx.Energy(), tx.EnergyPrice(), tx.Data())
   139  	return signWithFaucet(t, txNew, s.chain.chainConfig.NetworkID)
   140  }
   141  
   142  func hugeAmount(t *utesting.T, s *Suite) *types.Transaction {
   143  	tx := getNextTxFromChain(t, s)
   144  	amount := largeNumber(2)
   145  	var to common.Address
   146  	if tx.To() != nil {
   147  		to = *tx.To()
   148  	}
   149  	txNew := types.NewTransaction(tx.Nonce(), to, amount, tx.Energy(), tx.EnergyPrice(), tx.Data())
   150  	return signWithFaucet(t, txNew, s.chain.chainConfig.NetworkID)
   151  }
   152  
   153  func hugeEnergyPrice(t *utesting.T, s *Suite) *types.Transaction {
   154  	tx := getNextTxFromChain(t, s)
   155  	energyPrice := largeNumber(2)
   156  	var to common.Address
   157  	if tx.To() != nil {
   158  		to = *tx.To()
   159  	}
   160  	txNew := types.NewTransaction(tx.Nonce(), to, tx.Value(), tx.Energy(), energyPrice, tx.Data())
   161  	return signWithFaucet(t, txNew, s.chain.chainConfig.NetworkID)
   162  }
   163  
   164  func hugeData(t *utesting.T, s *Suite) *types.Transaction {
   165  	tx := getNextTxFromChain(t, s)
   166  	var to common.Address
   167  	if tx.To() != nil {
   168  		to = *tx.To()
   169  	}
   170  	txNew := types.NewTransaction(tx.Nonce(), to, tx.Value(), tx.Energy(), tx.EnergyPrice(), largeBuffer(2))
   171  	return signWithFaucet(t, txNew, s.chain.chainConfig.NetworkID)
   172  }
   173  
   174  func signWithFaucet(t *utesting.T, tx *types.Transaction, networkID *big.Int) *types.Transaction {
   175  	signer := types.NewNucleusSigner(networkID)
   176  	signedTx, err := types.SignTx(tx, signer, faucetKey)
   177  	if err != nil {
   178  		t.Fatalf("could not sign tx: %v\n", err)
   179  	}
   180  	return signedTx
   181  }