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