github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/utilities/miner/unconfirmed_test.go (about)

     1  package miner
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/neatlab/neatio/chain/core/types"
     7  	"github.com/neatlab/neatio/utilities/common"
     8  )
     9  
    10  type noopHeaderRetriever struct{}
    11  
    12  func (r *noopHeaderRetriever) GetHeaderByNumber(number uint64) *types.Header {
    13  	return nil
    14  }
    15  
    16  func TestUnconfirmedInsertBounds(t *testing.T) {
    17  	limit := uint(10)
    18  
    19  	pool := newUnconfirmedBlocks(new(noopHeaderRetriever), limit)
    20  	for depth := uint64(0); depth < 2*uint64(limit); depth++ {
    21  
    22  		for i := 0; i < int(depth); i++ {
    23  			pool.Insert(depth, common.Hash([32]byte{byte(depth), byte(i)}))
    24  		}
    25  
    26  		pool.blocks.Do(func(block interface{}) {
    27  			if block := block.(*unconfirmedBlock); block.index+uint64(limit) <= depth {
    28  				t.Errorf("depth %d: block %x not dropped", depth, block.hash)
    29  			}
    30  		})
    31  	}
    32  }
    33  
    34  func TestUnconfirmedShifts(t *testing.T) {
    35  
    36  	limit, start := uint(10), uint64(25)
    37  
    38  	pool := newUnconfirmedBlocks(new(noopHeaderRetriever), limit)
    39  	for depth := start; depth < start+uint64(limit); depth++ {
    40  		pool.Insert(depth, common.Hash([32]byte{byte(depth)}))
    41  	}
    42  
    43  	pool.Shift(start + uint64(limit) - 1)
    44  	if n := pool.blocks.Len(); n != int(limit) {
    45  		t.Errorf("unconfirmed count mismatch: have %d, want %d", n, limit)
    46  	}
    47  
    48  	pool.Shift(start + uint64(limit) - 1 + uint64(limit/2))
    49  	if n := pool.blocks.Len(); n != int(limit)/2 {
    50  		t.Errorf("unconfirmed count mismatch: have %d, want %d", n, limit/2)
    51  	}
    52  
    53  	pool.Shift(start + 2*uint64(limit))
    54  	if n := pool.blocks.Len(); n != 0 {
    55  		t.Errorf("unconfirmed count mismatch: have %d, want %d", n, 0)
    56  	}
    57  
    58  	pool.Shift(start + 3*uint64(limit))
    59  	if n := pool.blocks.Len(); n != 0 {
    60  		t.Errorf("unconfirmed count mismatch: have %d, want %d", n, 0)
    61  	}
    62  }