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