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