txpool_test.go 4.1 KB

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