github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/light/txpool_test.go (about)

     1  package light
     2  
     3  import (
     4  	"context"
     5  	"math"
     6  	"math/big"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/quickchainproject/quickchain/common"
    11  	"github.com/quickchainproject/quickchain/consensus/qcthash"
    12  	"github.com/quickchainproject/quickchain/core"
    13  	"github.com/quickchainproject/quickchain/core/types"
    14  	"github.com/quickchainproject/quickchain/core/vm"
    15  	"github.com/quickchainproject/quickchain/qctdb"
    16  	"github.com/quickchainproject/quickchain/params"
    17  )
    18  
    19  type testTxRelay struct {
    20  	send, discard, mined chan int
    21  }
    22  
    23  func (self *testTxRelay) Send(txs types.Transactions) {
    24  	self.send <- len(txs)
    25  }
    26  
    27  func (self *testTxRelay) NewHead(head common.Hash, mined []common.Hash, rollback []common.Hash) {
    28  	m := len(mined)
    29  	if m != 0 {
    30  		self.mined <- m
    31  	}
    32  }
    33  
    34  func (self *testTxRelay) Discard(hashes []common.Hash) {
    35  	self.discard <- len(hashes)
    36  }
    37  
    38  const poolTestTxs = 1000
    39  const poolTestBlocks = 100
    40  
    41  // test tx 0..n-1
    42  var testTx [poolTestTxs]*types.Transaction
    43  
    44  // txs sent before block i
    45  func sentTx(i int) int {
    46  	return int(math.Pow(float64(i)/float64(poolTestBlocks), 0.9) * poolTestTxs)
    47  }
    48  
    49  // txs included in block i or before that (minedTx(i) <= sentTx(i))
    50  func minedTx(i int) int {
    51  	return int(math.Pow(float64(i)/float64(poolTestBlocks), 1.1) * poolTestTxs)
    52  }
    53  
    54  func txPoolTestChainGen(i int, block *core.BlockGen) {
    55  	s := minedTx(i)
    56  	e := minedTx(i + 1)
    57  	for i := s; i < e; i++ {
    58  		block.AddTx(testTx[i])
    59  	}
    60  }
    61  
    62  func TestTxPool(t *testing.T) {
    63  	for i := range testTx {
    64  		testTx[i], _ = types.SignTx(types.NewTransaction(types.Binary, uint64(i), acc1Addr, big.NewInt(10000), params.TxGas, nil, nil), types.HomesteadSigner{}, testBankKey)
    65  	}
    66  
    67  	var (
    68  		sdb, _  = qctdb.NewMemDatabase()
    69  		ldb, _  = qctdb.NewMemDatabase()
    70  		gspec   = core.Genesis{Alloc: core.GenesisAlloc{testBankAddress: {Balance: testBankFunds}}}
    71  		genesis = gspec.MustCommit(sdb)
    72  	)
    73  	gspec.MustCommit(ldb)
    74  	// Assemble the test environment
    75  	blockchain, _ := core.NewBlockChain(sdb, nil, params.TestChainConfig, qcthash.NewFullFaker(), vm.Config{})
    76  	gchain, _ := core.GenerateChain(params.TestChainConfig, genesis, qcthash.NewFaker(), sdb, poolTestBlocks, txPoolTestChainGen)
    77  	if _, err := blockchain.InsertChain(gchain); err != nil {
    78  		panic(err)
    79  	}
    80  
    81  	odr := &testOdr{sdb: sdb, ldb: ldb}
    82  	relay := &testTxRelay{
    83  		send:    make(chan int, 1),
    84  		discard: make(chan int, 1),
    85  		mined:   make(chan int, 1),
    86  	}
    87  	lightchain, _ := NewLightChain(odr, params.TestChainConfig, qcthash.NewFullFaker())
    88  	txPermanent = 50
    89  	pool := NewTxPool(params.TestChainConfig, lightchain, relay)
    90  	ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
    91  	defer cancel()
    92  
    93  	for ii, block := range gchain {
    94  		i := ii + 1
    95  		s := sentTx(i - 1)
    96  		e := sentTx(i)
    97  		for i := s; i < e; i++ {
    98  			pool.Add(ctx, testTx[i])
    99  			got := <-relay.send
   100  			exp := 1
   101  			if got != exp {
   102  				t.Errorf("relay.Send expected len = %d, got %d", exp, got)
   103  			}
   104  		}
   105  
   106  		if _, err := lightchain.InsertHeaderChain([]*types.Header{block.Header()}, 1); err != nil {
   107  			panic(err)
   108  		}
   109  
   110  		got := <-relay.mined
   111  		exp := minedTx(i) - minedTx(i-1)
   112  		if got != exp {
   113  			t.Errorf("relay.NewHead expected len(mined) = %d, got %d", exp, got)
   114  		}
   115  
   116  		exp = 0
   117  		if i > int(txPermanent)+1 {
   118  			exp = minedTx(i-int(txPermanent)-1) - minedTx(i-int(txPermanent)-2)
   119  		}
   120  		if exp != 0 {
   121  			got = <-relay.discard
   122  			if got != exp {
   123  				t.Errorf("relay.Discard expected len = %d, got %d", exp, got)
   124  			}
   125  		}
   126  	}
   127  }