unconfirmed_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 miner
  17. import (
  18. "testing"
  19. "github.com/ethereum/go-ethereum/core/types"
  20. )
  21. // noopChainRetriever is an implementation of headerRetriever that always
  22. // returns nil for any requested headers.
  23. type noopChainRetriever struct{}
  24. func (r *noopChainRetriever) GetHeaderByNumber(number uint64) *types.Header {
  25. return nil
  26. }
  27. func (r *noopChainRetriever) GetBlockByNumber(number uint64) *types.Block {
  28. return nil
  29. }
  30. // Tests that inserting blocks into the unconfirmed set accumulates them until
  31. // the desired depth is reached, after which they begin to be dropped.
  32. func TestUnconfirmedInsertBounds(t *testing.T) {
  33. limit := uint(10)
  34. pool := newUnconfirmedBlocks(new(noopChainRetriever), limit)
  35. for depth := uint64(0); depth < 2*uint64(limit); depth++ {
  36. // Insert multiple blocks for the same level just to stress it
  37. for i := 0; i < int(depth); i++ {
  38. pool.Insert(depth, [32]byte{byte(depth), byte(i)})
  39. }
  40. // Validate that no blocks below the depth allowance are left in
  41. pool.blocks.Do(func(block interface{}) {
  42. if block := block.(*unconfirmedBlock); block.index+uint64(limit) <= depth {
  43. t.Errorf("depth %d: block %x not dropped", depth, block.hash)
  44. }
  45. })
  46. }
  47. }
  48. // Tests that shifting blocks out of the unconfirmed set works both for normal
  49. // cases as well as for corner cases such as empty sets, empty shifts or full
  50. // shifts.
  51. func TestUnconfirmedShifts(t *testing.T) {
  52. // Create a pool with a few blocks on various depths
  53. limit, start := uint(10), uint64(25)
  54. pool := newUnconfirmedBlocks(new(noopChainRetriever), limit)
  55. for depth := start; depth < start+uint64(limit); depth++ {
  56. pool.Insert(depth, [32]byte{byte(depth)})
  57. }
  58. // Try to shift below the limit and ensure no blocks are dropped
  59. pool.Shift(start + uint64(limit) - 1)
  60. if n := pool.blocks.Len(); n != int(limit) {
  61. t.Errorf("unconfirmed count mismatch: have %d, want %d", n, limit)
  62. }
  63. // Try to shift half the blocks out and verify remainder
  64. pool.Shift(start + uint64(limit) - 1 + uint64(limit/2))
  65. if n := pool.blocks.Len(); n != int(limit)/2 {
  66. t.Errorf("unconfirmed count mismatch: have %d, want %d", n, limit/2)
  67. }
  68. // Try to shift all the remaining blocks out and verify emptyness
  69. pool.Shift(start + 2*uint64(limit))
  70. if n := pool.blocks.Len(); n != 0 {
  71. t.Errorf("unconfirmed count mismatch: have %d, want %d", n, 0)
  72. }
  73. // Try to shift out from the empty set and make sure it doesn't break
  74. pool.Shift(start + 3*uint64(limit))
  75. if n := pool.blocks.Len(); n != 0 {
  76. t.Errorf("unconfirmed count mismatch: have %d, want %d", n, 0)
  77. }
  78. }