gitee.com/liu-zhao234568/cntest@v1.0.0/light/txpool_test.go (about)

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