github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/miner/unconfirmed_test.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 19:16:40</date> 10 //</624450100638257152> 11 12 13 package miner 14 15 import ( 16 "testing" 17 18 "github.com/ethereum/go-ethereum/common" 19 "github.com/ethereum/go-ethereum/core/types" 20 ) 21 22 //noopchainretriever是headerretriever的一个实现,它始终 23 //对于任何请求的头返回nil。 24 type noopChainRetriever struct{} 25 26 func (r *noopChainRetriever) GetHeaderByNumber(number uint64) *types.Header { 27 return nil 28 } 29 func (r *noopChainRetriever) GetBlockByNumber(number uint64) *types.Block { 30 return nil 31 } 32 33 //将块插入未确认集的测试将累积这些块,直到 34 //达到所需深度后,开始下降。 35 func TestUnconfirmedInsertBounds(t *testing.T) { 36 limit := uint(10) 37 38 pool := newUnconfirmedBlocks(new(noopChainRetriever), limit) 39 for depth := uint64(0); depth < 2*uint64(limit); depth++ { 40 //为同一级别插入多个块以强调它 41 for i := 0; i < int(depth); i++ { 42 pool.Insert(depth, common.Hash([32]byte{byte(depth), byte(i)})) 43 } 44 //确认深度允许范围以下没有块留在 45 pool.blocks.Do(func(block interface{}) { 46 if block := block.(*unconfirmedBlock); block.index+uint64(limit) <= depth { 47 t.Errorf("depth %d: block %x not dropped", depth, block.hash) 48 } 49 }) 50 } 51 } 52 53 //将块移出未确认集的测试正常工作 54 //箱,以及角箱,如空箱、空班或满箱 55 //轮班。 56 func TestUnconfirmedShifts(t *testing.T) { 57 //在不同深度上创建带有几个块的池 58 limit, start := uint(10), uint64(25) 59 60 pool := newUnconfirmedBlocks(new(noopChainRetriever), limit) 61 for depth := start; depth < start+uint64(limit); depth++ { 62 pool.Insert(depth, common.Hash([32]byte{byte(depth)})) 63 } 64 //试着移到极限以下,确保没有掉块。 65 pool.Shift(start + uint64(limit) - 1) 66 if n := pool.blocks.Len(); n != int(limit) { 67 t.Errorf("unconfirmed count mismatch: have %d, want %d", n, limit) 68 } 69 //试着把一半的积木移走,并核实剩余部分。 70 pool.Shift(start + uint64(limit) - 1 + uint64(limit/2)) 71 if n := pool.blocks.Len(); n != int(limit)/2 { 72 t.Errorf("unconfirmed count mismatch: have %d, want %d", n, limit/2) 73 } 74 //尝试将所有剩余的块移出并验证是否为空。 75 pool.Shift(start + 2*uint64(limit)) 76 if n := pool.blocks.Len(); n != 0 { 77 t.Errorf("unconfirmed count mismatch: have %d, want %d", n, 0) 78 } 79 //试着从空的那套换出来,确保它不坏 80 pool.Shift(start + 3*uint64(limit)) 81 if n := pool.blocks.Len(); n != 0 { 82 t.Errorf("unconfirmed count mismatch: have %d, want %d", n, 0) 83 } 84 } 85