github.com/gochain-io/gochain@v2.2.26+incompatible/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/gochain-io/gochain/common"
    27  	"github.com/gochain-io/gochain/consensus/clique"
    28  	"github.com/gochain-io/gochain/core"
    29  	"github.com/gochain-io/gochain/core/types"
    30  	"github.com/gochain-io/gochain/core/vm"
    31  	"github.com/gochain-io/gochain/ethdb"
    32  	"github.com/gochain-io/gochain/params"
    33  )
    34  
    35  type testTxRelay struct {
    36  	send, discard, mined chan int
    37  }
    38  
    39  func (t *testTxRelay) Send(txs types.Transactions) {
    40  	t.send <- len(txs)
    41  }
    42  
    43  func (t *testTxRelay) NewHead(head common.Hash, mined []common.Hash, rollback []common.Hash) {
    44  	m := len(mined)
    45  	if m != 0 {
    46  		t.mined <- m
    47  	}
    48  }
    49  
    50  func (t *testTxRelay) Discard(hashes []common.Hash) {
    51  	t.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(ctx context.Context, i int, block *core.BlockGen) {
    71  	s := minedTx(i)
    72  	e := minedTx(i + 1)
    73  	for i := s; i < e; i++ {
    74  		block.AddTx(ctx, testTx[i])
    75  	}
    76  }
    77  
    78  func TestTxPool(t *testing.T) {
    79  	ctx := context.Background()
    80  	for i := range testTx {
    81  		testTx[i], _ = types.SignTx(types.NewTransaction(uint64(i), acc1Addr, big.NewInt(10000), params.TxGas, nil, nil), types.HomesteadSigner{}, testBankKey)
    82  	}
    83  
    84  	var (
    85  		sdb     = ethdb.NewMemDatabase()
    86  		ldb     = ethdb.NewMemDatabase()
    87  		gspec   = core.Genesis{Alloc: core.GenesisAlloc{testBankAddress: {Balance: testBankFunds}}}
    88  		genesis = gspec.MustCommit(sdb)
    89  	)
    90  	gspec.MustCommit(ldb)
    91  	// Assemble the test environment
    92  	blockchain, _ := core.NewBlockChain(ctx, sdb, nil, params.TestChainConfig, clique.NewFullFaker(), vm.Config{})
    93  	gchain, _ := core.GenerateChain(ctx, params.TestChainConfig, genesis, clique.NewFaker(), sdb, poolTestBlocks, txPoolTestChainGen)
    94  	if _, err := blockchain.InsertChain(ctx, gchain); err != nil {
    95  		panic(err)
    96  	}
    97  
    98  	odr := &testOdr{sdb: sdb, ldb: ldb}
    99  	relay := &testTxRelay{
   100  		send:    make(chan int, 1),
   101  		discard: make(chan int, 1),
   102  		mined:   make(chan int, 1),
   103  	}
   104  	lightchain, _ := NewLightChain(odr, params.TestChainConfig, clique.NewFullFaker())
   105  	txPermanent = 50
   106  	pool := NewTxPool(params.TestChainConfig, lightchain, relay)
   107  	ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
   108  	defer cancel()
   109  
   110  	for ii, block := range gchain {
   111  		i := ii + 1
   112  		s := sentTx(i - 1)
   113  		e := sentTx(i)
   114  		for i := s; i < e; i++ {
   115  			pool.Add(ctx, testTx[i])
   116  			got := <-relay.send
   117  			exp := 1
   118  			if got != exp {
   119  				t.Errorf("relay.Send expected len = %d, got %d", exp, got)
   120  			}
   121  		}
   122  
   123  		if _, err := lightchain.InsertHeaderChain(ctx, []*types.Header{block.Header()}, 1); err != nil {
   124  			panic(err)
   125  		}
   126  
   127  		got := <-relay.mined
   128  		exp := minedTx(i) - minedTx(i-1)
   129  		if got != exp {
   130  			t.Errorf("relay.NewHead expected len(mined) = %d, got %d", exp, got)
   131  		}
   132  
   133  		exp = 0
   134  		if i > int(txPermanent)+1 {
   135  			exp = minedTx(i-int(txPermanent)-1) - minedTx(i-int(txPermanent)-2)
   136  		}
   137  		if exp != 0 {
   138  			got = <-relay.discard
   139  			if got != exp {
   140  				t.Errorf("relay.Discard expected len = %d, got %d", exp, got)
   141  			}
   142  		}
   143  	}
   144  }