txpool_test.go 4.1 KB

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