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"
22 block1000Hash = bc.NewHash([32]byte{0x01, 0x02})
23 block2000Hash = bc.NewHash([32]byte{0x02, 0x03})
24 block3000Hash = bc.NewHash([32]byte{0x03, 0x04})
27 type basePeer struct {
29 serviceFlag consensus.ServiceFlag
33 func (bp *basePeer) Addr() net.Addr {
37 func (bp *basePeer) ID() string {
41 func (bp *basePeer) Moniker() string {
45 func (bp *basePeer) RemoteAddrHost() string {
59 func (bp *basePeer) ServiceFlag() consensus.ServiceFlag {
63 func (bp *basePeer) TrafficStatus() (*flowrate.Status, *flowrate.Status) {
67 func (bp *basePeer) TrySend(byte, interface{}) bool {
71 func (bp *basePeer) IsLAN() bool {
75 func TestSetPeerStatus(t *testing.T) {
76 peer := newPeer(&basePeer{})
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)
85 func TestSetIrreversibleStatus(t *testing.T) {
86 peer := newPeer(&basePeer{})
88 hash := bc.NewHash([32]byte{0x1, 0x2})
89 peer.SetIrreversibleStatus(height, &hash)
90 if peer.IrreversibleHeight() != height {
91 t.Fatalf("test set Irreversible status err. got %d want %d", peer.Height(), height)
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")),
101 Outputs: []*types.TxOutput{
102 types.NewTxOutput(bc.NewAssetID([32]byte{3}), 8, []byte("outProgram")),
106 peer.AddFilterAddresses([][]byte{[]byte("spendProgram")})
107 if !peer.isRelatedTx(tx) {
108 t.Fatal("test filter addresses error.")
111 peer.AddFilterAddresses([][]byte{[]byte("testProgram")})
112 if peer.isRelatedTx(tx) {
113 t.Fatal("test filter addresses error.")
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")),
123 Outputs: []*types.TxOutput{
124 types.NewTxOutput(bc.NewAssetID([32]byte{3}), 8, []byte("outProgram")),
128 peer.AddFilterAddresses([][]byte{[]byte("spendProgram")})
129 if !peer.isRelatedTx(tx) {
130 t.Fatal("test filter addresses error.")
134 if peer.isRelatedTx(tx) {
135 t.Fatal("test filter addresses error.")
139 func TestGetRelatedTxAndStatus(t *testing.T) {
140 peer := newPeer(&basePeer{})
142 types.NewTx(types.TxData{
143 Inputs: []*types.TxInput{
144 types.NewSpendInput(nil, bc.Hash{}, bc.NewAssetID([32]byte{1}), 5, 1, []byte("spendProgram1")),
146 Outputs: []*types.TxOutput{
147 types.NewTxOutput(bc.NewAssetID([32]byte{3}), 8, []byte("outProgram1")),
150 types.NewTx(types.TxData{
151 Inputs: []*types.TxInput{
152 types.NewSpendInput(nil, bc.Hash{}, bc.NewAssetID([32]byte{1}), 5, 1, []byte("spendProgram2")),
154 Outputs: []*types.TxOutput{
155 types.NewTxOutput(bc.NewAssetID([32]byte{3}), 8, []byte("outProgram2")),
158 types.NewTx(types.TxData{
159 Inputs: []*types.TxInput{
160 types.NewSpendInput(nil, bc.Hash{}, bc.NewAssetID([32]byte{1}), 5, 1, []byte("spendProgram3")),
162 Outputs: []*types.TxOutput{
163 types.NewTxOutput(bc.NewAssetID([32]byte{3}), 8, []byte("outProgram3")),
167 txStatuses := &bc.TransactionStatus{
168 VerifyStatus: []*bc.TxVerifyResult{{StatusFail: true}, {StatusFail: false}, {StatusFail: false}},
170 peer.AddFilterAddresses([][]byte{[]byte("spendProgram1"), []byte("outProgram3")})
171 gotTxs, gotStatus := peer.getRelatedTxAndStatus(txs, txStatuses)
172 if len(gotTxs) != 2 {
173 t.Error("TestGetRelatedTxAndStatus txs size error")
176 if !reflect.DeepEqual(*gotTxs[0].Tx, *txs[0].Tx) {
177 t.Errorf("txs msg test err: got %s\nwant %s", spew.Sdump(gotTxs[0].Tx), spew.Sdump(txs[0].Tx))
180 if !reflect.DeepEqual(*gotTxs[1].Tx, *txs[2].Tx) {
181 t.Errorf("txs msg test err: got %s\nwant %s", spew.Sdump(gotTxs[1].Tx), spew.Sdump(txs[2].Tx))
184 if gotStatus[0].StatusFail != true || gotStatus[1].StatusFail != false {
185 t.Error("TestGetRelatedTxAndStatus txs status error")
189 type basePeerSet struct {
192 func (bp *basePeerSet) StopPeerGracefully(string) {
196 func (bp *basePeerSet) IsBanned(ip string, level byte, reason string) bool {
210 func TestMarkBlock(t *testing.T) {
211 ps := NewPeerSet(&basePeerSet{})
212 ps.AddPeer(&basePeer{id: peer1ID})
213 ps.AddPeer(&basePeer{id: peer2ID})
214 ps.AddPeer(&basePeer{id: peer3ID})
216 blockHash := bc.NewHash([32]byte{0x01, 0x02})
217 ps.MarkBlock(peer1ID, &blockHash)
218 targetPeers := []string{peer2ID, peer3ID}
220 peers := ps.PeersWithoutBlock(blockHash)
221 if len(peers) != len(targetPeers) {
222 t.Fatalf("test mark block err. Number of target peers %d got %d", 1, len(peers))
225 for _, targetPeer := range targetPeers {
227 for _, gotPeer := range peers {
228 if gotPeer == targetPeer {
234 t.Errorf("test mark block err. can't found target peer %s ", targetPeer)
239 func TestMarkStatus(t *testing.T) {
240 ps := NewPeerSet(&basePeerSet{})
241 ps.AddPeer(&basePeer{id: peer1ID})
242 ps.AddPeer(&basePeer{id: peer2ID})
243 ps.AddPeer(&basePeer{id: peer3ID})
245 height := uint64(1024)
246 ps.MarkStatus(peer1ID, height)
247 targetPeers := []string{peer2ID, peer3ID}
249 peers := ps.peersWithoutNewStatus(height)
250 if len(peers) != len(targetPeers) {
251 t.Fatalf("test mark status err. Number of target peers %d got %d", 1, len(peers))
254 for _, targetPeer := range targetPeers {
256 for _, gotPeer := range peers {
257 if gotPeer.ID() == targetPeer {
263 t.Errorf("test mark status err. can't found target peer %s ", targetPeer)
268 func TestMarkBlockSignature(t *testing.T) {
269 ps := NewPeerSet(&basePeerSet{})
270 ps.AddPeer(&basePeer{id: peer1ID})
271 ps.AddPeer(&basePeer{id: peer2ID})
272 ps.AddPeer(&basePeer{id: peer3ID})
274 signature := []byte{0x01, 0x02}
275 ps.MarkBlockSignature(peer1ID, signature)
276 targetPeers := []string{peer2ID, peer3ID}
278 peers := ps.PeersWithoutSignature(signature)
279 if len(peers) != len(targetPeers) {
280 t.Fatalf("test mark block signature err. Number of target peers %d got %d", 1, len(peers))
283 for _, targetPeer := range targetPeers {
285 for _, gotPeer := range peers {
286 if gotPeer == targetPeer {
292 t.Errorf("test mark block signature err. can't found target peer %s ", targetPeer)
297 func TestMarkTx(t *testing.T) {
298 ps := NewPeerSet(&basePeerSet{})
299 ps.AddPeer(&basePeer{id: peer1ID})
300 ps.AddPeer(&basePeer{id: peer2ID})
301 ps.AddPeer(&basePeer{id: peer3ID})
303 txHash := bc.NewHash([32]byte{0x01, 0x02})
304 ps.MarkTx(peer1ID, txHash)
305 peers := ps.peersWithoutTx(&txHash)
306 targetPeers := []string{peer2ID, peer3ID}
307 if len(peers) != len(targetPeers) {
308 t.Fatalf("test mark tx err. Number of target peers %d got %d", 1, len(peers))
311 for _, targetPeer := range targetPeers {
313 for _, gotPeer := range peers {
314 if gotPeer.ID() == targetPeer {
320 t.Errorf("test mark tx err. can't found target peer %s ", targetPeer)
325 func TestSetStatus(t *testing.T) {
326 ps := NewPeerSet(&basePeerSet{})
327 ps.AddPeer(&basePeer{id: peer1ID, serviceFlag: consensus.SFFullNode})
328 ps.AddPeer(&basePeer{id: peer2ID, serviceFlag: consensus.SFFullNode})
329 ps.AddPeer(&basePeer{id: peer3ID, serviceFlag: consensus.SFFastSync})
330 ps.AddPeer(&basePeer{id: peer4ID, serviceFlag: consensus.SFFullNode, isLan: true})
331 ps.SetStatus(peer1ID, 1000, &block1000Hash)
332 ps.SetStatus(peer2ID, 2000, &block2000Hash)
333 ps.SetStatus(peer3ID, 3000, &block3000Hash)
334 ps.SetStatus(peer4ID, 2000, &block2000Hash)
335 targetPeer := peer4ID
337 peer := ps.BestPeer(consensus.SFFullNode)
339 if peer.ID() != targetPeer {
340 t.Fatalf("test set status err. Name of target peer %s got %s", peer4ID, peer.ID())
344 func TestIrreversibleStatus(t *testing.T) {
345 ps := NewPeerSet(&basePeerSet{})
346 ps.AddPeer(&basePeer{id: peer1ID, serviceFlag: consensus.SFFullNode})
347 ps.AddPeer(&basePeer{id: peer2ID, serviceFlag: consensus.SFFullNode})
348 ps.AddPeer(&basePeer{id: peer3ID, serviceFlag: consensus.SFFastSync})
349 ps.AddPeer(&basePeer{id: peer4ID, serviceFlag: consensus.SFFastSync, isLan: true})
350 ps.SetIrreversibleStatus(peer1ID, 1000, &block1000Hash)
351 ps.SetIrreversibleStatus(peer2ID, 2000, &block2000Hash)
352 ps.SetIrreversibleStatus(peer3ID, 3000, &block3000Hash)
353 ps.SetIrreversibleStatus(peer4ID, 3000, &block3000Hash)
354 targetPeer := peer4ID
355 peer := ps.BestIrreversiblePeer(consensus.SFFastSync)
357 if peer.ID() != targetPeer {
358 t.Fatalf("test set status err. Name of target peer %s got %s", peer4ID, peer.ID())
362 func TestGetPeersByHeight(t *testing.T) {
363 ps := NewPeerSet(&basePeerSet{})
364 ps.AddPeer(&basePeer{id: peer1ID, serviceFlag: consensus.SFFullNode})
365 ps.AddPeer(&basePeer{id: peer2ID, serviceFlag: consensus.SFFullNode})
366 ps.AddPeer(&basePeer{id: peer3ID, serviceFlag: consensus.SFFastSync})
367 ps.AddPeer(&basePeer{id: peer4ID, serviceFlag: consensus.SFFullNode, isLan: true})
368 ps.SetStatus(peer1ID, 1000, &block1000Hash)
369 ps.SetStatus(peer2ID, 2000, &block2000Hash)
370 ps.SetStatus(peer3ID, 3000, &block3000Hash)
371 ps.SetStatus(peer4ID, 2000, &block2000Hash)
372 peers := ps.GetPeersByHeight(2000)
373 targetPeers := []string{peer2ID, peer3ID, peer4ID}
374 if len(peers) != len(targetPeers) {
375 t.Fatalf("test get peers by height err. Number of target peers %d got %d", 3, len(peers))
378 for _, targetPeer := range targetPeers {
380 for _, gotPeer := range peers {
381 if gotPeer.ID() == targetPeer {
387 t.Errorf("test get peers by height err. can't found target peer %s ", targetPeer)
392 func TestRemovePeer(t *testing.T) {
393 ps := NewPeerSet(&basePeerSet{})
394 ps.AddPeer(&basePeer{id: peer1ID})
395 ps.AddPeer(&basePeer{id: peer2ID})
397 ps.RemovePeer(peer1ID)
398 if peer := ps.GetPeer(peer1ID); peer != nil {
399 t.Fatalf("remove peer %s err", peer1ID)
402 if peer := ps.GetPeer(peer2ID); peer == nil {
403 t.Fatalf("Error remove peer %s err", peer2ID)
407 func TestProcessIllegal(t *testing.T) {
408 ps := NewPeerSet(&basePeerSet{})
409 ps.AddPeer(&basePeer{id: peer1ID})
410 ps.AddPeer(&basePeer{id: peer2ID})
412 ps.ProcessIllegal(peer1ID, security.LevelMsgIllegal, "test")
413 if peer := ps.GetPeer(peer1ID); peer != nil {
414 t.Fatalf("remove peer %s err", peer1ID)
417 ps.ProcessIllegal(peer2ID, security.LevelMsgIllegal, "test")
418 if peer := ps.GetPeer(peer2ID); peer == nil {
419 t.Fatalf("Error remove peer %s err", peer2ID)