github.com/aergoio/aergo@v1.3.1/syncer/blockfetcher_test.go (about)

     1  package syncer
     2  
     3  import (
     4  	"github.com/aergoio/aergo/chain"
     5  	"github.com/aergoio/aergo/types"
     6  	"github.com/stretchr/testify/assert"
     7  	"testing"
     8  )
     9  
    10  // test blockfetcher without finder/hashfetcher
    11  // - must create SyncCtx manually, because finder will be skipped
    12  func TestBlockFetcher_simple(t *testing.T) {
    13  	remoteChainLen := 10
    14  	targetNo := uint64(5)
    15  
    16  	//ancestor = 0
    17  	remoteChain := chain.InitStubBlockChain(nil, remoteChainLen)
    18  	localChain := chain.InitStubBlockChain(remoteChain.Blocks[0:1], 0)
    19  
    20  	remoteChains := []*chain.StubBlockChain{remoteChain, remoteChain} //peer count = 2
    21  	peers := makeStubPeerSet(remoteChains)
    22  
    23  	//set debug property
    24  	testCfg := *SyncerCfg
    25  	testCfg.maxHashReqSize = TestMaxHashReqSize
    26  	testCfg.maxBlockReqSize = TestMaxBlockFetchSize
    27  	testCfg.debugContext = &SyncerDebug{t: t, expAncestor: 0}
    28  	testCfg.debugContext.targetNo = targetNo
    29  
    30  	syncer := NewTestSyncer(t, localChain, remoteChain, peers, &testCfg)
    31  
    32  	//set ctx manually because finder will be skipped
    33  	ctx := types.NewSyncCtx(1, "peer-0", targetNo, uint64(localChain.Best), nil)
    34  	ancestor := remoteChain.Blocks[0]
    35  	ctx.SetAncestor(ancestor)
    36  
    37  	//run blockFetcher direct
    38  	syncer.runTestBlockFetcher(ctx)
    39  
    40  	syncer.checkResultFn = func(stubSyncer *StubSyncer) {
    41  		//check blockFetcher status
    42  		bf := stubSyncer.realSyncer.blockFetcher
    43  		assert.Equal(stubSyncer.t, uint64(stubSyncer.cfg.debugContext.targetNo), bf.stat.getMaxChunkRsp().BlockNo(), "response mismatch")
    44  		assert.Equal(stubSyncer.t, uint64(stubSyncer.cfg.debugContext.targetNo), bf.stat.getLastAddBlock().BlockNo(), "last add block mismatch")
    45  	}
    46  
    47  	syncer.start()
    48  
    49  	testHashSet := func(prev *types.BlockInfo, count uint64) {
    50  		//push hashSet next from prev
    51  		hashes, _ := syncer.remoteChain.GetHashes(prev, count)
    52  
    53  		syncer.sendHashSetToBlockFetcher(&HashSet{len(hashes), hashes, prev.No + 1})
    54  	}
    55  
    56  	testHashSet(&types.BlockInfo{Hash: ancestor.GetHash(), No: ancestor.BlockNo()}, 3)
    57  
    58  	prevInfo := remoteChain.GetBlockInfo(ancestor.BlockNo() + 3)
    59  	testHashSet(prevInfo, 2)
    60  
    61  	syncer.waitStop()
    62  }
    63  
    64  func TestBlockFetcher_SortedQueue(t *testing.T) {
    65  	var squeue SortedTaskQueue
    66  
    67  	t1 := &FetchTask{count: 1, startNo: 1}
    68  	t2 := &FetchTask{count: 1, startNo: 2}
    69  	t3 := &FetchTask{count: 1, startNo: 3}
    70  
    71  	squeue.Init()
    72  	squeue.Push(t1)
    73  
    74  	for i := 1; i <= 1; i++ {
    75  		outTask := squeue.Pop()
    76  		assert.Equal(t, uint64(i), outTask.startNo, "task not sorted")
    77  	}
    78  	assert.Equal(t, 0, squeue.Len())
    79  
    80  	squeue.Init()
    81  	squeue.Push(t1)
    82  	squeue.Push(t2)
    83  	squeue.Push(t3)
    84  
    85  	for i := 1; i <= 3; i++ {
    86  		outTask := squeue.Pop()
    87  		assert.Equal(t, uint64(i), outTask.startNo, "task not sorted")
    88  	}
    89  	assert.Equal(t, 0, squeue.Len())
    90  
    91  	squeue.Init()
    92  	squeue.Push(t3)
    93  	squeue.Push(t2)
    94  	squeue.Push(t1)
    95  
    96  	for i := 1; i <= 3; i++ {
    97  		outTask := squeue.Pop()
    98  		assert.Equal(t, uint64(i), outTask.startNo, "task not sorted")
    99  	}
   100  	assert.Equal(t, 0, squeue.Len())
   101  }