unconfirmed_test.go 3.3 KB

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