github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/miner/unconfirmed_test.go (about)

     1  // This file is part of the go-sberex library. The go-sberex library is 
     2  // free software: you can redistribute it and/or modify it under the terms 
     3  // of the GNU Lesser General Public License as published by the Free 
     4  // Software Foundation, either version 3 of the License, or (at your option)
     5  // any later version.
     6  //
     7  // The go-sberex library is distributed in the hope that it will be useful, 
     8  // but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 
    10  // General Public License <http://www.gnu.org/licenses/> for more details.
    11  
    12  package miner
    13  
    14  import (
    15  	"testing"
    16  
    17  	"github.com/Sberex/go-sberex/common"
    18  	"github.com/Sberex/go-sberex/core/types"
    19  )
    20  
    21  // noopHeaderRetriever is an implementation of headerRetriever that always
    22  // returns nil for any requested headers.
    23  type noopHeaderRetriever struct{}
    24  
    25  func (r *noopHeaderRetriever) GetHeaderByNumber(number uint64) *types.Header {
    26  	return nil
    27  }
    28  
    29  // Tests that inserting blocks into the unconfirmed set accumulates them until
    30  // the desired depth is reached, after which they begin to be dropped.
    31  func TestUnconfirmedInsertBounds(t *testing.T) {
    32  	limit := uint(10)
    33  
    34  	pool := newUnconfirmedBlocks(new(noopHeaderRetriever), limit)
    35  	for depth := uint64(0); depth < 2*uint64(limit); depth++ {
    36  		// Insert multiple blocks for the same level just to stress it
    37  		for i := 0; i < int(depth); i++ {
    38  			pool.Insert(depth, common.Hash([32]byte{byte(depth), byte(i)}))
    39  		}
    40  		// Validate that no blocks below the depth allowance are left in
    41  		pool.blocks.Do(func(block interface{}) {
    42  			if block := block.(*unconfirmedBlock); block.index+uint64(limit) <= depth {
    43  				t.Errorf("depth %d: block %x not dropped", depth, block.hash)
    44  			}
    45  		})
    46  	}
    47  }
    48  
    49  // Tests that shifting blocks out of the unconfirmed set works both for normal
    50  // cases as well as for corner cases such as empty sets, empty shifts or full
    51  // shifts.
    52  func TestUnconfirmedShifts(t *testing.T) {
    53  	// Create a pool with a few blocks on various depths
    54  	limit, start := uint(10), uint64(25)
    55  
    56  	pool := newUnconfirmedBlocks(new(noopHeaderRetriever), limit)
    57  	for depth := start; depth < start+uint64(limit); depth++ {
    58  		pool.Insert(depth, common.Hash([32]byte{byte(depth)}))
    59  	}
    60  	// Try to shift below the limit and ensure no blocks are dropped
    61  	pool.Shift(start + uint64(limit) - 1)
    62  	if n := pool.blocks.Len(); n != int(limit) {
    63  		t.Errorf("unconfirmed count mismatch: have %d, want %d", n, limit)
    64  	}
    65  	// Try to shift half the blocks out and verify remainder
    66  	pool.Shift(start + uint64(limit) - 1 + uint64(limit/2))
    67  	if n := pool.blocks.Len(); n != int(limit)/2 {
    68  		t.Errorf("unconfirmed count mismatch: have %d, want %d", n, limit/2)
    69  	}
    70  	// Try to shift all the remaining blocks out and verify emptyness
    71  	pool.Shift(start + 2*uint64(limit))
    72  	if n := pool.blocks.Len(); n != 0 {
    73  		t.Errorf("unconfirmed count mismatch: have %d, want %d", n, 0)
    74  	}
    75  	// Try to shift out from the empty set and make sure it doesn't break
    76  	pool.Shift(start + 3*uint64(limit))
    77  	if n := pool.blocks.Len(); n != 0 {
    78  		t.Errorf("unconfirmed count mismatch: have %d, want %d", n, 0)
    79  	}
    80  }