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