unconfirmed_test.go 3.2 KB

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