github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/light/txpool_test.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 12:09:41</date>
    10  //</624342646172684288>
    11  
    12  
    13  package light
    14  
    15  import (
    16  	"context"
    17  	"math"
    18  	"math/big"
    19  	"testing"
    20  	"time"
    21  
    22  	"github.com/ethereum/go-ethereum/common"
    23  	"github.com/ethereum/go-ethereum/consensus/ethash"
    24  	"github.com/ethereum/go-ethereum/core"
    25  	"github.com/ethereum/go-ethereum/core/types"
    26  	"github.com/ethereum/go-ethereum/core/vm"
    27  	"github.com/ethereum/go-ethereum/ethdb"
    28  	"github.com/ethereum/go-ethereum/params"
    29  )
    30  
    31  type testTxRelay struct {
    32  	send, discard, mined chan int
    33  }
    34  
    35  func (self *testTxRelay) Send(txs types.Transactions) {
    36  	self.send <- len(txs)
    37  }
    38  
    39  func (self *testTxRelay) NewHead(head common.Hash, mined []common.Hash, rollback []common.Hash) {
    40  	m := len(mined)
    41  	if m != 0 {
    42  		self.mined <- m
    43  	}
    44  }
    45  
    46  func (self *testTxRelay) Discard(hashes []common.Hash) {
    47  	self.discard <- len(hashes)
    48  }
    49  
    50  const poolTestTxs = 1000
    51  const poolTestBlocks = 100
    52  
    53  //测试TX 0…N-1
    54  var testTx [poolTestTxs]*types.Transaction
    55  
    56  //一区前发送的TXS
    57  func sentTx(i int) int {
    58  	return int(math.Pow(float64(i)/float64(poolTestBlocks), 0.9) * poolTestTxs)
    59  }
    60  
    61  //一区或之前包含的Txs(minedtx(i)<=sentx(i))。
    62  func minedTx(i int) int {
    63  	return int(math.Pow(float64(i)/float64(poolTestBlocks), 1.1) * poolTestTxs)
    64  }
    65  
    66  func txPoolTestChainGen(i int, block *core.BlockGen) {
    67  	s := minedTx(i)
    68  	e := minedTx(i + 1)
    69  	for i := s; i < e; i++ {
    70  		block.AddTx(testTx[i])
    71  	}
    72  }
    73  
    74  func TestTxPool(t *testing.T) {
    75  	for i := range testTx {
    76  		testTx[i], _ = types.SignTx(types.NewTransaction(uint64(i), acc1Addr, big.NewInt(10000), params.TxGas, nil, nil), types.HomesteadSigner{}, testBankKey)
    77  	}
    78  
    79  	var (
    80  		sdb     = ethdb.NewMemDatabase()
    81  		ldb     = ethdb.NewMemDatabase()
    82  		gspec   = core.Genesis{Alloc: core.GenesisAlloc{testBankAddress: {Balance: testBankFunds}}}
    83  		genesis = gspec.MustCommit(sdb)
    84  	)
    85  	gspec.MustCommit(ldb)
    86  //组装测试环境
    87  	blockchain, _ := core.NewBlockChain(sdb, nil, params.TestChainConfig, ethash.NewFullFaker(), vm.Config{})
    88  	gchain, _ := core.GenerateChain(params.TestChainConfig, genesis, ethash.NewFaker(), sdb, poolTestBlocks, txPoolTestChainGen)
    89  	if _, err := blockchain.InsertChain(gchain); err != nil {
    90  		panic(err)
    91  	}
    92  
    93  	odr := &testOdr{sdb: sdb, ldb: ldb}
    94  	relay := &testTxRelay{
    95  		send:    make(chan int, 1),
    96  		discard: make(chan int, 1),
    97  		mined:   make(chan int, 1),
    98  	}
    99  	lightchain, _ := NewLightChain(odr, params.TestChainConfig, ethash.NewFullFaker())
   100  	txPermanent = 50
   101  	pool := NewTxPool(params.TestChainConfig, lightchain, relay)
   102  	ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
   103  	defer cancel()
   104  
   105  	for ii, block := range gchain {
   106  		i := ii + 1
   107  		s := sentTx(i - 1)
   108  		e := sentTx(i)
   109  		for i := s; i < e; i++ {
   110  			pool.Add(ctx, testTx[i])
   111  			got := <-relay.send
   112  			exp := 1
   113  			if got != exp {
   114  				t.Errorf("relay.Send expected len = %d, got %d", exp, got)
   115  			}
   116  		}
   117  
   118  		if _, err := lightchain.InsertHeaderChain([]*types.Header{block.Header()}, 1); err != nil {
   119  			panic(err)
   120  		}
   121  
   122  		got := <-relay.mined
   123  		exp := minedTx(i) - minedTx(i-1)
   124  		if got != exp {
   125  			t.Errorf("relay.NewHead expected len(mined) = %d, got %d", exp, got)
   126  		}
   127  
   128  		exp = 0
   129  		if i > int(txPermanent)+1 {
   130  			exp = minedTx(i-int(txPermanent)-1) - minedTx(i-int(txPermanent)-2)
   131  		}
   132  		if exp != 0 {
   133  			got = <-relay.discard
   134  			if got != exp {
   135  				t.Errorf("relay.Discard expected len = %d, got %d", exp, got)
   136  			}
   137  		}
   138  	}
   139  }
   140