github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/netsync/consensusmgr/handle_test.go (about)

     1  package consensusmgr
     2  
     3  import (
     4  	"math/rand"
     5  	"net"
     6  	"reflect"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/tendermint/tmlibs/flowrate"
    11  
    12  	"github.com/bytom/bytom/consensus"
    13  	"github.com/bytom/bytom/event"
    14  	"github.com/bytom/bytom/netsync/peers"
    15  	"github.com/bytom/bytom/p2p"
    16  	"github.com/bytom/bytom/protocol/bc"
    17  	"github.com/bytom/bytom/protocol/bc/types"
    18  	"github.com/bytom/bytom/protocol/casper"
    19  )
    20  
    21  type p2peer struct {
    22  }
    23  
    24  func (p *p2peer) Addr() net.Addr {
    25  	return nil
    26  }
    27  
    28  func (p *p2peer) ID() string {
    29  	return ""
    30  }
    31  
    32  func (p *p2peer) Moniker() string {
    33  	return ""
    34  }
    35  
    36  func (p *p2peer) RemoteAddrHost() string {
    37  	return ""
    38  }
    39  func (p *p2peer) ServiceFlag() consensus.ServiceFlag {
    40  	return 0
    41  }
    42  func (p *p2peer) TrafficStatus() (*flowrate.Status, *flowrate.Status) {
    43  	return nil, nil
    44  }
    45  func (p *p2peer) TrySend(byte, interface{}) bool {
    46  	return true
    47  }
    48  func (p *p2peer) IsLAN() bool {
    49  	return false
    50  }
    51  
    52  func mockBlocks(startBlock *types.Block, height uint64) []*types.Block {
    53  	blocks := []*types.Block{}
    54  	indexBlock := &types.Block{}
    55  	if startBlock == nil {
    56  		indexBlock = &types.Block{BlockHeader: types.BlockHeader{Version: uint64(rand.Uint32())}}
    57  		blocks = append(blocks, indexBlock)
    58  	} else {
    59  		indexBlock = startBlock
    60  	}
    61  
    62  	for indexBlock.Height < height {
    63  		block := &types.Block{
    64  			BlockHeader: types.BlockHeader{
    65  				Height:            indexBlock.Height + 1,
    66  				PreviousBlockHash: indexBlock.Hash(),
    67  				Version:           uint64(rand.Uint32()),
    68  			},
    69  		}
    70  		blocks = append(blocks, block)
    71  		indexBlock = block
    72  	}
    73  	return blocks
    74  }
    75  
    76  type mockSW struct {
    77  }
    78  
    79  func (s *mockSW) AddReactor(name string, reactor p2p.Reactor) p2p.Reactor {
    80  	return nil
    81  }
    82  
    83  type mockChain struct {
    84  }
    85  
    86  func (c *mockChain) BestBlockHeight() uint64 {
    87  	return 0
    88  }
    89  
    90  func (c *mockChain) GetHeaderByHash(*bc.Hash) (*types.BlockHeader, error) {
    91  	return nil, nil
    92  }
    93  
    94  func (c *mockChain) ProcessBlock(*types.Block) (bool, error) {
    95  	return false, nil
    96  }
    97  
    98  func (c *mockChain) ProcessBlockVerification(*casper.ValidCasperSignMsg) error {
    99  	return nil
   100  }
   101  
   102  type mockPeers struct {
   103  	msgCount       *int
   104  	knownBlock     *bc.Hash
   105  	blockHeight    *uint64
   106  	knownSignature *[]byte
   107  }
   108  
   109  func newMockPeers(msgCount *int, knownBlock *bc.Hash, blockHeight *uint64, signature *[]byte) *mockPeers {
   110  	return &mockPeers{
   111  		msgCount:       msgCount,
   112  		knownBlock:     knownBlock,
   113  		blockHeight:    blockHeight,
   114  		knownSignature: signature,
   115  	}
   116  }
   117  
   118  func (ps *mockPeers) AddPeer(peer peers.BasePeer) {
   119  
   120  }
   121  
   122  func (ps *mockPeers) BroadcastMsg(bm peers.BroadcastMsg) error {
   123  	*ps.msgCount++
   124  	return nil
   125  }
   126  func (ps *mockPeers) GetPeer(id string) *peers.Peer {
   127  	return &peers.Peer{BasePeer: &p2peer{}}
   128  }
   129  func (ps *mockPeers) MarkBlock(peerID string, hash *bc.Hash) {
   130  	*ps.knownBlock = *hash
   131  }
   132  
   133  func (ps *mockPeers) MarkBlockVerification(peerID string, signature []byte) {
   134  	*ps.knownSignature = append(*ps.knownSignature, signature...)
   135  }
   136  
   137  func (ps *mockPeers) ProcessIllegal(peerID string, level byte, reason string) {
   138  
   139  }
   140  func (p *mockPeers) RemovePeer(peerID string) {
   141  
   142  }
   143  func (ps *mockPeers) SetStatus(peerID string, height uint64, hash *bc.Hash) {
   144  	*ps.blockHeight = height
   145  }
   146  
   147  func TestBlockProposeMsgBroadcastLoop(t *testing.T) {
   148  	dispatcher := event.NewDispatcher()
   149  	msgCount := 0
   150  	blockHeight := 100
   151  	mgr := NewManager(&mockSW{}, &mockChain{}, newMockPeers(&msgCount, nil, nil, nil), dispatcher)
   152  	blocks := mockBlocks(nil, uint64(blockHeight))
   153  
   154  	mgr.Start()
   155  	defer mgr.Stop()
   156  	time.Sleep(10 * time.Millisecond)
   157  	for _, block := range blocks {
   158  		mgr.eventDispatcher.Post(event.NewProposedBlockEvent{Block: *block})
   159  	}
   160  	time.Sleep(10 * time.Millisecond)
   161  	if msgCount != blockHeight+1 {
   162  		t.Fatalf("broad propose block msg err. got:%d\n want:%d", msgCount, blockHeight+1)
   163  	}
   164  }
   165  
   166  func TestBlockVerificationMsgBroadcastLoop(t *testing.T) {
   167  	dispatcher := event.NewDispatcher()
   168  	msgCount := 0
   169  	blockHeight := 100
   170  	mgr := NewManager(&mockSW{}, &mockChain{}, newMockPeers(&msgCount, nil, nil, nil), dispatcher)
   171  	blocks := mockBlocks(nil, uint64(blockHeight))
   172  
   173  	mgr.Start()
   174  	defer mgr.Stop()
   175  	time.Sleep(10 * time.Millisecond)
   176  	for _, block := range blocks {
   177  		mgr.eventDispatcher.Post(casper.ValidCasperSignMsg{TargetHash: block.Hash(), Signature: []byte{0x1, 0x2}, PubKey: "011022"})
   178  	}
   179  	time.Sleep(10 * time.Millisecond)
   180  	if msgCount != blockHeight+1 {
   181  		t.Fatalf("broad propose block msg err. got:%d\n want:%d", msgCount, blockHeight+1)
   182  	}
   183  }
   184  
   185  func TestProcessBlockProposeMsg(t *testing.T) {
   186  	dispatcher := event.NewDispatcher()
   187  	msgCount := 0
   188  	var knownBlock bc.Hash
   189  	blockHeight := uint64(0)
   190  	peerID := "Peer1"
   191  	mgr := NewManager(&mockSW{}, &mockChain{}, newMockPeers(&msgCount, &knownBlock, &blockHeight, nil), dispatcher)
   192  	block := &types.Block{
   193  		BlockHeader: types.BlockHeader{
   194  			Height:            100,
   195  			PreviousBlockHash: bc.NewHash([32]byte{0x1}),
   196  			Version:           uint64(rand.Uint32()),
   197  		},
   198  	}
   199  	msg, err := NewBlockProposeMsg(block)
   200  	if err != nil {
   201  		t.Fatal("create new block propose msg err", err)
   202  	}
   203  
   204  	mgr.processMsg(peerID, 0, msg)
   205  	if knownBlock != block.Hash() {
   206  		t.Fatalf("mark propose block msg err. got:%d\n want:%d", knownBlock, block.Hash())
   207  	}
   208  
   209  	if blockHeight != block.Height {
   210  		t.Fatalf("set peer status err. got:%d\n want:%d", blockHeight, block.Height)
   211  	}
   212  }
   213  
   214  func TestProcessBlockVerificationMsg(t *testing.T) {
   215  	dispatcher := event.NewDispatcher()
   216  	msgCount := 0
   217  	knownSignature := []byte{}
   218  	peerID := "Peer1"
   219  	mgr := NewManager(&mockSW{}, &mockChain{}, newMockPeers(&msgCount, nil, nil, &knownSignature), dispatcher)
   220  	block := &types.Block{
   221  		BlockHeader: types.BlockHeader{
   222  			Height:            100,
   223  			PreviousBlockHash: bc.NewHash([32]byte{0x1}),
   224  			Version:           uint64(rand.Uint32()),
   225  		},
   226  	}
   227  
   228  	signature := []byte{0x01, 0x02}
   229  	msg := NewBlockVerificationMsg(block.Hash(), block.Hash(), []byte{0x03, 0x04}, signature)
   230  
   231  	mgr.processMsg(peerID, 0, msg)
   232  
   233  	if !reflect.DeepEqual(knownSignature, signature) {
   234  		t.Fatalf("set peer status err. got:%d\n want:%d", knownSignature, signature)
   235  	}
   236  }