github.com/SmartMeshFoundation/Spectrum@v0.0.0-20220621030607-452a266fee1e/miner/unconfirmed_test.go (about) 1 // Copyright 2016 The Spectrum Authors 2 // This file is part of the Spectrum library. 3 // 4 // The Spectrum 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 Spectrum 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 Spectrum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package miner 18 19 import ( 20 "fmt" 21 "sync" 22 "testing" 23 24 "github.com/SmartMeshFoundation/Spectrum/common" 25 "github.com/SmartMeshFoundation/Spectrum/core/types" 26 ) 27 28 // noopHeaderRetriever is an implementation of headerRetriever that always 29 // returns nil for any requested headers. 30 type noopHeaderRetriever struct{} 31 32 func (r *noopHeaderRetriever) GetHeaderByNumber(number uint64) *types.Header { 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(noopHeaderRetriever), 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, common.Hash([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(noopHeaderRetriever), limit) 64 for depth := start; depth < start+uint64(limit); depth++ { 65 pool.Insert(depth, common.Hash([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 } 88 89 func TestChannelClose(t *testing.T) { 90 ch := make(chan struct{}) 91 t.Log("isNull", ch == nil) 92 close(ch) 93 t.Log("isNull", ch == nil) 94 95 } 96 97 func TestSyncMap(t *testing.T) { 98 var m = new(sync.Map) 99 c1 := make(chan struct{}) 100 c2 := make(chan struct{}) 101 m.Store(c1, c1) 102 m.Store(c2, c2) 103 go func() { 104 <-c1 105 fmt.Println("111111111111111") 106 }() 107 go func() { 108 <-c2 109 fmt.Println("222222222222222") 110 }() 111 m.Range(func(k, v interface{}) bool { 112 fmt.Println(k, v) 113 close(k.(chan struct{})) 114 m.Delete(k) 115 return true 116 }) 117 _, ok := m.Load(c1) 118 fmt.Println(ok) 119 }