txpool_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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/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/event"
  29. "github.com/ethereum/go-ethereum/params"
  30. "github.com/ethereum/go-ethereum/pow"
  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. pow = new(pow.FakePow)
  73. sdb, _ = ethdb.NewMemDatabase()
  74. ldb, _ = ethdb.NewMemDatabase()
  75. gspec = core.Genesis{Alloc: core.GenesisAlloc{testBankAddress: {Balance: testBankFunds}}}
  76. genesis = gspec.MustCommit(sdb)
  77. )
  78. gspec.MustCommit(ldb)
  79. // Assemble the test environment
  80. blockchain, _ := core.NewBlockChain(sdb, testChainConfig(), pow, evmux, vm.Config{})
  81. chainConfig := &params.ChainConfig{HomesteadBlock: new(big.Int)}
  82. gchain, _ := core.GenerateChain(chainConfig, genesis, sdb, poolTestBlocks, txPoolTestChainGen)
  83. if _, err := blockchain.InsertChain(gchain); err != nil {
  84. panic(err)
  85. }
  86. odr := &testOdr{sdb: sdb, ldb: ldb}
  87. relay := &testTxRelay{
  88. send: make(chan int, 1),
  89. discard: make(chan int, 1),
  90. mined: make(chan int, 1),
  91. }
  92. lightchain, _ := NewLightChain(odr, testChainConfig(), pow, evmux)
  93. lightchain.SetValidator(bproc{})
  94. txPermanent = 50
  95. pool := NewTxPool(testChainConfig(), evmux, lightchain, relay)
  96. ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
  97. defer cancel()
  98. for ii, block := range gchain {
  99. i := ii + 1
  100. s := sentTx(i - 1)
  101. e := sentTx(i)
  102. for i := s; i < e; i++ {
  103. pool.Add(ctx, testTx[i])
  104. got := <-relay.send
  105. exp := 1
  106. if got != exp {
  107. t.Errorf("relay.Send expected len = %d, got %d", exp, got)
  108. }
  109. }
  110. if _, err := lightchain.InsertHeaderChain([]*types.Header{block.Header()}, 1); err != nil {
  111. panic(err)
  112. }
  113. got := <-relay.mined
  114. exp := minedTx(i) - minedTx(i-1)
  115. if got != exp {
  116. t.Errorf("relay.NewHead expected len(mined) = %d, got %d", exp, got)
  117. }
  118. exp = 0
  119. if i > int(txPermanent)+1 {
  120. exp = minedTx(i-int(txPermanent)-1) - minedTx(i-int(txPermanent)-2)
  121. }
  122. if exp != 0 {
  123. got = <-relay.discard
  124. if got != exp {
  125. t.Errorf("relay.Discard expected len = %d, got %d", exp, got)
  126. }
  127. }
  128. }
  129. }