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

     1  package peers
     2  
     3  import (
     4  	"net"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/bytom/bytom/consensus"
     9  	"github.com/bytom/bytom/p2p/security"
    10  	"github.com/bytom/bytom/protocol/bc"
    11  	"github.com/bytom/bytom/protocol/bc/types"
    12  	"github.com/davecgh/go-spew/spew"
    13  	"github.com/tendermint/tmlibs/flowrate"
    14  )
    15  
    16  var (
    17  	peer1ID = "PEER1"
    18  	peer2ID = "PEER2"
    19  	peer3ID = "PEER3"
    20  	peer4ID = "PEER4"
    21  
    22  	block1000Hash = bc.NewHash([32]byte{0x01, 0x02})
    23  	block2000Hash = bc.NewHash([32]byte{0x02, 0x03})
    24  	block3000Hash = bc.NewHash([32]byte{0x03, 0x04})
    25  )
    26  
    27  type basePeer struct {
    28  	id          string
    29  	serviceFlag consensus.ServiceFlag
    30  	isLan       bool
    31  }
    32  
    33  func (bp *basePeer) Addr() net.Addr {
    34  	return nil
    35  }
    36  
    37  func (bp *basePeer) ID() string {
    38  	return bp.id
    39  }
    40  
    41  func (bp *basePeer) Moniker() string {
    42  	return ""
    43  }
    44  
    45  func (bp *basePeer) RemoteAddrHost() string {
    46  	switch bp.ID() {
    47  	case peer1ID:
    48  		return peer1ID
    49  	case peer2ID:
    50  		return peer2ID
    51  	case peer3ID:
    52  		return peer3ID
    53  	case peer4ID:
    54  		return peer4ID
    55  	}
    56  	return ""
    57  }
    58  
    59  func (bp *basePeer) ServiceFlag() consensus.ServiceFlag {
    60  	return bp.serviceFlag
    61  }
    62  
    63  func (bp *basePeer) TrafficStatus() (*flowrate.Status, *flowrate.Status) {
    64  	return nil, nil
    65  }
    66  
    67  func (bp *basePeer) TrySend(byte, interface{}) bool {
    68  	return true
    69  }
    70  
    71  func (bp *basePeer) IsLAN() bool {
    72  	return bp.isLan
    73  }
    74  
    75  func TestSetPeerStatus(t *testing.T) {
    76  	peer := newPeer(&basePeer{})
    77  	height := uint64(100)
    78  	hash := bc.NewHash([32]byte{0x1, 0x2})
    79  	peer.SetBestStatus(height, &hash)
    80  	if peer.Height() != height {
    81  		t.Fatalf("test set best status err. got %d want %d", peer.Height(), height)
    82  	}
    83  }
    84  
    85  func TestSetIrreversibleStatus(t *testing.T) {
    86  	peer := newPeer(&basePeer{})
    87  	height := uint64(100)
    88  	hash := bc.NewHash([32]byte{0x1, 0x2})
    89  	peer.SetJustifiedStatus(height, &hash)
    90  	if peer.JustifiedHeight() != height {
    91  		t.Fatalf("test set Irreversible status err. got %d want %d", peer.Height(), height)
    92  	}
    93  }
    94  
    95  func TestAddFilterAddresses(t *testing.T) {
    96  	peer := newPeer(&basePeer{})
    97  	tx := types.NewTx(types.TxData{
    98  		Inputs: []*types.TxInput{
    99  			types.NewSpendInput(nil, bc.Hash{}, bc.NewAssetID([32]byte{1}), 5, 1, []byte("spendProgram"), [][]byte{}),
   100  		},
   101  		Outputs: []*types.TxOutput{
   102  			types.NewOriginalTxOutput(bc.NewAssetID([32]byte{3}), 8, []byte("outProgram"), [][]byte{}),
   103  		},
   104  	})
   105  
   106  	peer.AddFilterAddresses([][]byte{[]byte("spendProgram")})
   107  	if !peer.isRelatedTx(tx) {
   108  		t.Fatal("test filter addresses error.")
   109  	}
   110  
   111  	peer.AddFilterAddresses([][]byte{[]byte("testProgram")})
   112  	if peer.isRelatedTx(tx) {
   113  		t.Fatal("test filter addresses error.")
   114  	}
   115  }
   116  
   117  func TestFilterClear(t *testing.T) {
   118  	peer := newPeer(&basePeer{})
   119  	tx := types.NewTx(types.TxData{
   120  		Inputs: []*types.TxInput{
   121  			types.NewSpendInput(nil, bc.Hash{}, bc.NewAssetID([32]byte{1}), 5, 1, []byte("spendProgram"), [][]byte{}),
   122  		},
   123  		Outputs: []*types.TxOutput{
   124  			types.NewOriginalTxOutput(bc.NewAssetID([32]byte{3}), 8, []byte("outProgram"), [][]byte{}),
   125  		},
   126  	})
   127  
   128  	peer.AddFilterAddresses([][]byte{[]byte("spendProgram")})
   129  	if !peer.isRelatedTx(tx) {
   130  		t.Fatal("test filter addresses error.")
   131  	}
   132  
   133  	peer.FilterClear()
   134  	if peer.isRelatedTx(tx) {
   135  		t.Fatal("test filter addresses error.")
   136  	}
   137  }
   138  
   139  func TestGetRelatedTxAndStatus(t *testing.T) {
   140  	peer := newPeer(&basePeer{})
   141  	txs := []*types.Tx{
   142  		types.NewTx(types.TxData{
   143  			Inputs: []*types.TxInput{
   144  				types.NewSpendInput(nil, bc.Hash{}, bc.NewAssetID([32]byte{1}), 5, 1, []byte("spendProgram1"), [][]byte{}),
   145  			},
   146  			Outputs: []*types.TxOutput{
   147  				types.NewOriginalTxOutput(bc.NewAssetID([32]byte{3}), 8, []byte("outProgram1"), [][]byte{}),
   148  			},
   149  		}),
   150  		types.NewTx(types.TxData{
   151  			Inputs: []*types.TxInput{
   152  				types.NewSpendInput(nil, bc.Hash{}, bc.NewAssetID([32]byte{1}), 5, 1, []byte("spendProgram2"), [][]byte{}),
   153  			},
   154  			Outputs: []*types.TxOutput{
   155  				types.NewOriginalTxOutput(bc.NewAssetID([32]byte{3}), 8, []byte("outProgram2"), [][]byte{}),
   156  			},
   157  		}),
   158  		types.NewTx(types.TxData{
   159  			Inputs: []*types.TxInput{
   160  				types.NewSpendInput(nil, bc.Hash{}, bc.NewAssetID([32]byte{1}), 5, 1, []byte("spendProgram3"), [][]byte{}),
   161  			},
   162  			Outputs: []*types.TxOutput{
   163  				types.NewOriginalTxOutput(bc.NewAssetID([32]byte{3}), 8, []byte("outProgram3"), [][]byte{}),
   164  			},
   165  		}),
   166  	}
   167  
   168  	peer.AddFilterAddresses([][]byte{[]byte("spendProgram1"), []byte("outProgram3")})
   169  	gotTxs := peer.getRelatedTxs(txs)
   170  	if len(gotTxs) != 2 {
   171  		t.Error("TestGetRelatedTxAndStatus txs size error")
   172  	}
   173  
   174  	if !reflect.DeepEqual(*gotTxs[0].Tx, *txs[0].Tx) {
   175  		t.Errorf("txs msg test err: got %s\nwant %s", spew.Sdump(gotTxs[0].Tx), spew.Sdump(txs[0].Tx))
   176  	}
   177  
   178  	if !reflect.DeepEqual(*gotTxs[1].Tx, *txs[2].Tx) {
   179  		t.Errorf("txs msg test err: got %s\nwant %s", spew.Sdump(gotTxs[1].Tx), spew.Sdump(txs[2].Tx))
   180  	}
   181  }
   182  
   183  type basePeerSet struct {
   184  }
   185  
   186  func (bp *basePeerSet) StopPeerGracefully(string) {
   187  
   188  }
   189  
   190  func (bp *basePeerSet) IsBanned(ip string, level byte, reason string) bool {
   191  	switch ip {
   192  	case peer1ID:
   193  		return true
   194  	case peer2ID:
   195  		return false
   196  	case peer3ID:
   197  		return true
   198  	case peer4ID:
   199  		return false
   200  	}
   201  	return false
   202  }
   203  
   204  func TestMarkBlock(t *testing.T) {
   205  	ps := NewPeerSet(&basePeerSet{})
   206  	ps.AddPeer(&basePeer{id: peer1ID})
   207  	ps.AddPeer(&basePeer{id: peer2ID})
   208  	ps.AddPeer(&basePeer{id: peer3ID})
   209  
   210  	blockHash := bc.NewHash([32]byte{0x01, 0x02})
   211  	ps.MarkBlock(peer1ID, &blockHash)
   212  	targetPeers := []string{peer2ID, peer3ID}
   213  
   214  	peers := ps.PeersWithoutBlock(blockHash)
   215  	if len(peers) != len(targetPeers) {
   216  		t.Fatalf("test mark block err. Number of target peers %d got %d", 1, len(peers))
   217  	}
   218  
   219  	for _, targetPeer := range targetPeers {
   220  		flag := false
   221  		for _, gotPeer := range peers {
   222  			if gotPeer == targetPeer {
   223  				flag = true
   224  				break
   225  			}
   226  		}
   227  		if !flag {
   228  			t.Errorf("test mark block err. can't found target peer %s ", targetPeer)
   229  		}
   230  	}
   231  }
   232  
   233  func TestMarkStatus(t *testing.T) {
   234  	ps := NewPeerSet(&basePeerSet{})
   235  	ps.AddPeer(&basePeer{id: peer1ID})
   236  	ps.AddPeer(&basePeer{id: peer2ID})
   237  	ps.AddPeer(&basePeer{id: peer3ID})
   238  
   239  	height := uint64(1024)
   240  	ps.MarkStatus(peer1ID, height)
   241  	targetPeers := []string{peer2ID, peer3ID}
   242  
   243  	peers := ps.peersWithoutNewStatus(height)
   244  	if len(peers) != len(targetPeers) {
   245  		t.Fatalf("test mark status err. Number of target peers %d got %d", 1, len(peers))
   246  	}
   247  
   248  	for _, targetPeer := range targetPeers {
   249  		flag := false
   250  		for _, gotPeer := range peers {
   251  			if gotPeer.ID() == targetPeer {
   252  				flag = true
   253  				break
   254  			}
   255  		}
   256  		if !flag {
   257  			t.Errorf("test mark status err. can't found target peer %s ", targetPeer)
   258  		}
   259  	}
   260  }
   261  
   262  func TestMarkBlockSignature(t *testing.T) {
   263  	ps := NewPeerSet(&basePeerSet{})
   264  	ps.AddPeer(&basePeer{id: peer1ID})
   265  	ps.AddPeer(&basePeer{id: peer2ID})
   266  	ps.AddPeer(&basePeer{id: peer3ID})
   267  
   268  	signature := []byte{0x01, 0x02}
   269  	ps.MarkBlockVerification(peer1ID, signature)
   270  	targetPeers := []string{peer2ID, peer3ID}
   271  
   272  	peers := ps.PeersWithoutSignature(signature)
   273  	if len(peers) != len(targetPeers) {
   274  		t.Fatalf("test mark block signature err. Number of target peers %d got %d", 1, len(peers))
   275  	}
   276  
   277  	for _, targetPeer := range targetPeers {
   278  		flag := false
   279  		for _, gotPeer := range peers {
   280  			if gotPeer == targetPeer {
   281  				flag = true
   282  				break
   283  			}
   284  		}
   285  		if !flag {
   286  			t.Errorf("test mark block signature err. can't found target peer %s ", targetPeer)
   287  		}
   288  	}
   289  }
   290  
   291  func TestMarkTx(t *testing.T) {
   292  	ps := NewPeerSet(&basePeerSet{})
   293  	ps.AddPeer(&basePeer{id: peer1ID})
   294  	ps.AddPeer(&basePeer{id: peer2ID})
   295  	ps.AddPeer(&basePeer{id: peer3ID})
   296  
   297  	txHash := bc.NewHash([32]byte{0x01, 0x02})
   298  	ps.MarkTx(peer1ID, txHash)
   299  	peers := ps.peersWithoutTx(&txHash)
   300  	targetPeers := []string{peer2ID, peer3ID}
   301  	if len(peers) != len(targetPeers) {
   302  		t.Fatalf("test mark tx err. Number of target peers %d got %d", 1, len(peers))
   303  	}
   304  
   305  	for _, targetPeer := range targetPeers {
   306  		flag := false
   307  		for _, gotPeer := range peers {
   308  			if gotPeer.ID() == targetPeer {
   309  				flag = true
   310  				break
   311  			}
   312  		}
   313  		if !flag {
   314  			t.Errorf("test mark tx err. can't found target peer %s ", targetPeer)
   315  		}
   316  	}
   317  }
   318  
   319  func TestSetStatus(t *testing.T) {
   320  	ps := NewPeerSet(&basePeerSet{})
   321  	ps.AddPeer(&basePeer{id: peer1ID, serviceFlag: consensus.SFFullNode})
   322  	ps.AddPeer(&basePeer{id: peer2ID, serviceFlag: consensus.SFFullNode})
   323  	ps.AddPeer(&basePeer{id: peer3ID, serviceFlag: consensus.SFFastSync})
   324  	ps.AddPeer(&basePeer{id: peer4ID, serviceFlag: consensus.SFFullNode, isLan: true})
   325  	ps.SetStatus(peer1ID, 1000, &block1000Hash)
   326  	ps.SetStatus(peer2ID, 2000, &block2000Hash)
   327  	ps.SetStatus(peer3ID, 3000, &block3000Hash)
   328  	ps.SetStatus(peer4ID, 2000, &block2000Hash)
   329  	targetPeer := peer4ID
   330  
   331  	peer := ps.BestPeer(consensus.SFFullNode)
   332  
   333  	if peer.ID() != targetPeer {
   334  		t.Fatalf("test set status err. Name of target peer %s got %s", peer4ID, peer.ID())
   335  	}
   336  }
   337  
   338  func TestIrreversibleStatus(t *testing.T) {
   339  	ps := NewPeerSet(&basePeerSet{})
   340  	ps.AddPeer(&basePeer{id: peer1ID, serviceFlag: consensus.SFFullNode})
   341  	ps.AddPeer(&basePeer{id: peer2ID, serviceFlag: consensus.SFFullNode})
   342  	ps.AddPeer(&basePeer{id: peer3ID, serviceFlag: consensus.SFFastSync})
   343  	ps.AddPeer(&basePeer{id: peer4ID, serviceFlag: consensus.SFFastSync, isLan: true})
   344  	ps.SetJustifiedStatus(peer1ID, 1000, &block1000Hash)
   345  	ps.SetJustifiedStatus(peer2ID, 2000, &block2000Hash)
   346  	ps.SetJustifiedStatus(peer3ID, 3000, &block3000Hash)
   347  	ps.SetJustifiedStatus(peer4ID, 3000, &block3000Hash)
   348  	targetPeer := peer4ID
   349  	peer := ps.BestPeer(consensus.SFFastSync)
   350  
   351  	if peer.ID() != targetPeer {
   352  		t.Fatalf("test set status err. Name of target peer %s got %s", peer4ID, peer.ID())
   353  	}
   354  }
   355  
   356  func TestGetPeersByHeight(t *testing.T) {
   357  	ps := NewPeerSet(&basePeerSet{})
   358  	ps.AddPeer(&basePeer{id: peer1ID, serviceFlag: consensus.SFFullNode})
   359  	ps.AddPeer(&basePeer{id: peer2ID, serviceFlag: consensus.SFFullNode})
   360  	ps.AddPeer(&basePeer{id: peer3ID, serviceFlag: consensus.SFFastSync})
   361  	ps.AddPeer(&basePeer{id: peer4ID, serviceFlag: consensus.SFFullNode, isLan: true})
   362  	ps.SetStatus(peer1ID, 1000, &block1000Hash)
   363  	ps.SetStatus(peer2ID, 2000, &block2000Hash)
   364  	ps.SetStatus(peer3ID, 3000, &block3000Hash)
   365  	ps.SetStatus(peer4ID, 2000, &block2000Hash)
   366  	peers := ps.GetPeersByHeight(2000)
   367  	targetPeers := []string{peer2ID, peer3ID, peer4ID}
   368  	if len(peers) != len(targetPeers) {
   369  		t.Fatalf("test get peers by height err. Number of target peers %d got %d", 3, len(peers))
   370  	}
   371  
   372  	for _, targetPeer := range targetPeers {
   373  		flag := false
   374  		for _, gotPeer := range peers {
   375  			if gotPeer.ID() == targetPeer {
   376  				flag = true
   377  				break
   378  			}
   379  		}
   380  		if !flag {
   381  			t.Errorf("test get peers by height err. can't found target peer %s ", targetPeer)
   382  		}
   383  	}
   384  }
   385  
   386  func TestRemovePeer(t *testing.T) {
   387  	ps := NewPeerSet(&basePeerSet{})
   388  	ps.AddPeer(&basePeer{id: peer1ID})
   389  	ps.AddPeer(&basePeer{id: peer2ID})
   390  
   391  	ps.RemovePeer(peer1ID)
   392  	if peer := ps.GetPeer(peer1ID); peer != nil {
   393  		t.Fatalf("remove peer %s err", peer1ID)
   394  	}
   395  
   396  	if peer := ps.GetPeer(peer2ID); peer == nil {
   397  		t.Fatalf("Error remove peer %s err", peer2ID)
   398  	}
   399  }
   400  
   401  func TestProcessIllegal(t *testing.T) {
   402  	ps := NewPeerSet(&basePeerSet{})
   403  	ps.AddPeer(&basePeer{id: peer1ID})
   404  	ps.AddPeer(&basePeer{id: peer2ID})
   405  
   406  	ps.ProcessIllegal(peer1ID, security.LevelMsgIllegal, "test")
   407  	if peer := ps.GetPeer(peer1ID); peer != nil {
   408  		t.Fatalf("remove peer %s err", peer1ID)
   409  	}
   410  
   411  	ps.ProcessIllegal(peer2ID, security.LevelMsgIllegal, "test")
   412  	if peer := ps.GetPeer(peer2ID); peer == nil {
   413  		t.Fatalf("Error remove peer %s err", peer2ID)
   414  	}
   415  }