github.com/turingchain2020/turingchain@v1.1.21/system/p2p/dht/protocol/broadcast/tx_test.go (about) 1 // Copyright Turing Corp. 2018 All Rights Reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 package broadcast 5 6 import ( 7 "encoding/hex" 8 "testing" 9 10 "github.com/turingchain2020/turingchain/queue" 11 "github.com/turingchain2020/turingchain/types" 12 "github.com/stretchr/testify/assert" 13 ) 14 15 func Test_sendTx(t *testing.T) { 16 17 proto := newTestProtocol() 18 proto.p2pCfg.LightTxTTL = 1 19 proto.p2pCfg.MaxTTL = 3 20 route := &types.P2PRoute{} 21 tx := &types.P2PTx{Tx: tx, Route: route} 22 sPid := testPid.Pretty() 23 _, ok := proto.handleSend(tx, sPid) 24 assert.True(t, ok) 25 _, ok = proto.handleSend(tx, sPid) 26 assert.False(t, ok) 27 route.TTL = 4 28 _, ok = proto.handleSend(tx, sPid) 29 assert.False(t, ok) 30 route.TTL = 1 31 tx.Tx = tx1 32 _, ok = proto.handleSend(tx, sPid) 33 assert.True(t, ok) 34 } 35 36 func Test_recvTx(t *testing.T) { 37 38 q := queue.New("test") 39 go q.Start() 40 defer q.Close() 41 42 proto := newTestProtocolWithQueue(q) 43 tx := &types.P2PTx{Tx: tx} 44 sPid := testPid.Pretty() 45 sendData, _ := proto.handleSend(tx, sPid) 46 47 newCli := q.Client() 48 newCli.Sub("mempool") 49 err := proto.handleReceive(sendData, sPid, testAddr, broadcastV1) 50 assert.Nil(t, err) 51 52 msg := <-newCli.Recv() 53 assert.Equal(t, types.EventTx, int(msg.Ty)) 54 tran, ok := msg.Data.(*types.Transaction) 55 assert.True(t, ok) 56 assert.Equal(t, tx.Tx.Hash(), tran.Hash()) 57 58 } 59 60 func Test_recvLtTx(t *testing.T) { 61 62 proto := newTestProtocol() 63 proto.p2pCfg.LightTxTTL = 0 64 tx := &types.P2PTx{Tx: tx} 65 sendData, _ := proto.handleSend(tx, testPidStr) 66 err := proto.handleReceive(sendData, testPidStr, testAddr, broadcastV1) 67 assert.Equal(t, errSendPeer, err) 68 69 proto.txFilter.Add(hex.EncodeToString(tx.Tx.Hash()), true) 70 err = proto.handleReceive(sendData, testPidStr, testAddr, broadcastV1) 71 assert.Equal(t, nil, err) 72 }