github.com/Bytom/bytom@v1.1.2-0.20210127130405-ae40204c0b09/netsync/block_fetcher_test.go (about)

     1  package netsync
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/bytom/bytom/protocol/bc"
     8  	"github.com/bytom/bytom/protocol/bc/types"
     9  )
    10  
    11  type chain struct {
    12  	blocks []uint64
    13  }
    14  
    15  func newChain() *chain {
    16  	blocks := make([]uint64, 1, 1)
    17  	blocks[0] = 99
    18  	return &chain{
    19  		blocks: blocks,
    20  	}
    21  }
    22  
    23  func (c *chain) BestBlockHeader() *types.BlockHeader {
    24  	return nil
    25  }
    26  func (c *chain) CalcNextSeed(*bc.Hash) (*bc.Hash, error) {
    27  	return nil, nil
    28  }
    29  func (c *chain) GetHeaderByHeight(uint64) (*types.BlockHeader, error) {
    30  	return nil, nil
    31  }
    32  func (c *chain) GetTransactionStatus(*bc.Hash) (*bc.TransactionStatus, error) {
    33  	return nil, nil
    34  }
    35  func (c *chain) InMainChain(bc.Hash) bool {
    36  	return true
    37  }
    38  func (c *chain) ValidateTx(*types.Tx) (bool, error) {
    39  	return true, nil
    40  }
    41  func (c *chain) GetBlockByHeight(uint64) (*types.Block, error) {
    42  	return nil, nil
    43  }
    44  
    45  func (c *chain) BestBlockHeight() uint64 {
    46  	return c.blocks[len(c.blocks)-1]
    47  }
    48  
    49  func (c *chain) GetBlockByHash(*bc.Hash) (*types.Block, error) {
    50  	return nil, nil
    51  }
    52  
    53  func (c *chain) GetHeaderByHash(*bc.Hash) (*types.BlockHeader, error) {
    54  	return nil, nil
    55  }
    56  
    57  func (c *chain) ProcessBlock(block *types.Block) (bool, error) {
    58  	c.blocks = append(c.blocks, block.Height)
    59  	return false, nil
    60  }
    61  
    62  func (c *chain) ProcessBlockSignature(signature, pubkey []byte, blockHeight uint64, blockHash *bc.Hash) error {
    63  	return nil
    64  }
    65  
    66  func TestBlockFetcher(t *testing.T) {
    67  	peers := newPeerSet(NewPeerSet())
    68  	testCase := []struct {
    69  		blockMsg *blockMsg
    70  		height   uint64
    71  	}{
    72  		{
    73  			blockMsg: &blockMsg{
    74  				block: &types.Block{
    75  					BlockHeader: types.BlockHeader{
    76  						Height: 100,
    77  					},
    78  				},
    79  			},
    80  			height: 100,
    81  		},
    82  		{
    83  			blockMsg: &blockMsg{
    84  				block: &types.Block{
    85  					BlockHeader: types.BlockHeader{
    86  						Height: 101,
    87  					},
    88  				},
    89  			},
    90  			height: 101,
    91  		},
    92  		{
    93  			blockMsg: &blockMsg{
    94  				block: &types.Block{
    95  					BlockHeader: types.BlockHeader{
    96  						Height: 105,
    97  					},
    98  				},
    99  			},
   100  			height: 101,
   101  		},
   102  		{
   103  			blockMsg: &blockMsg{
   104  				block: &types.Block{
   105  					BlockHeader: types.BlockHeader{
   106  						Height: 200,
   107  					},
   108  				},
   109  			},
   110  			height: 101,
   111  		},
   112  		{
   113  			blockMsg: &blockMsg{
   114  				block: &types.Block{
   115  					BlockHeader: types.BlockHeader{
   116  						Height: 104,
   117  					},
   118  				},
   119  			},
   120  			height: 101,
   121  		},
   122  		{
   123  			blockMsg: &blockMsg{
   124  				block: &types.Block{
   125  					BlockHeader: types.BlockHeader{
   126  						Height: 103,
   127  					},
   128  				},
   129  			},
   130  			height: 101,
   131  		},
   132  		{
   133  			blockMsg: &blockMsg{
   134  				block: &types.Block{
   135  					BlockHeader: types.BlockHeader{
   136  						Height: 102,
   137  					},
   138  				},
   139  			},
   140  			height: 105,
   141  		},
   142  	}
   143  	fetcher := newBlockFetcher(newChain(), peers)
   144  
   145  	for i, c := range testCase {
   146  		fetcher.processNewBlock(c.blockMsg)
   147  		time.Sleep(10 * time.Millisecond)
   148  		chainHeight := fetcher.chain.BestBlockHeight()
   149  		if chainHeight != c.height {
   150  			t.Fatalf("test block fetcher error. index %d expected chain height %d but got %d", i, chainHeight, c.height)
   151  		}
   152  	}
   153  }