txpool_test.go 4.0 KB

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