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