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