10 "github.com/davecgh/go-spew/spew"
12 "github.com/bytom/bytom/consensus"
13 dbm "github.com/bytom/bytom/database/leveldb"
14 "github.com/bytom/bytom/protocol"
15 core "github.com/bytom/bytom/protocol"
16 "github.com/bytom/bytom/protocol/bc"
17 "github.com/bytom/bytom/protocol/bc/types"
18 "github.com/bytom/bytom/test/mock"
21 const txsNumber = 2000
26 func (m *mempool) GetTransactions() []*core.TxDesc {
27 txs := []*core.TxDesc{}
28 for i := 0; i < txsNumber; i++ {
29 txInput := types.NewSpendInput(nil, bc.NewHash([32]byte{0x01}), *consensus.BTMAssetID, uint64(i), 1, []byte{0x51})
30 txInput.CommitmentSuffix = []byte{0, 1, 2}
31 txInput.WitnessSuffix = []byte{0, 1, 2}
36 //SerializedSize: uint64(i * 10),
37 Inputs: []*types.TxInput{
40 Outputs: []*types.TxOutput{
41 types.NewTxOutput(*consensus.BTMAssetID, uint64(i), []byte{0x6a}),
46 ID: bc.Hash{V0: uint64(i), V1: uint64(i), V2: uint64(i), V3: uint64(i)},
49 txs = append(txs, &core.TxDesc{Tx: tx})
54 func (m *mempool) IsDust(tx *types.Tx) bool {
58 func TestSyncMempool(t *testing.T) {
59 tmpDir, err := ioutil.TempDir(".", "")
61 t.Fatalf("failed to create temporary data folder: %v", err)
63 defer os.RemoveAll(tmpDir)
64 testDBA := dbm.NewDB("testdba", "leveldb", tmpDir)
65 testDBB := dbm.NewDB("testdbb", "leveldb", tmpDir)
67 blocks := mockBlocks(nil, 5)
68 a := mockSync(blocks, &mock.Mempool{}, testDBA)
69 b := mockSync(blocks, &mock.Mempool{}, testDBB)
70 a.mempool = &mempool{}
71 netWork := NewNetWork()
72 netWork.Register(a, "192.168.0.1", "test node A", consensus.SFFullNode)
73 netWork.Register(b, "192.168.0.2", "test node B", consensus.SFFullNode)
74 if B2A, A2B, err := netWork.HandsShake(a, b); err != nil {
75 t.Errorf("fail on peer hands shake %v", err)
81 go a.syncMempoolLoop()
82 a.syncMempool("test node B")
83 wantTxs := a.mempool.GetTransactions()
85 timeout := time.NewTimer(2 * time.Second)
87 ticker := time.NewTicker(500 * time.Millisecond)
90 gotTxs := []*protocol.TxDesc{}
94 gotTxs = b.mempool.GetTransactions()
95 if len(gotTxs) >= txsNumber {
99 t.Fatalf("mempool sync timeout")
104 if len(gotTxs) != txsNumber {
105 t.Fatalf("mempool sync txs num err. got:%d want:%d", len(gotTxs), txsNumber)
108 for i, gotTx := range gotTxs {
109 index := gotTx.Tx.Inputs[0].Amount()
110 if !reflect.DeepEqual(gotTx.Tx.Inputs[0].Amount(), wantTxs[index].Tx.Inputs[0].Amount()) {
111 t.Fatalf("mempool tx err. index:%d\n,gotTx:%s\n,wantTx:%s", i, spew.Sdump(gotTx.Tx.Inputs), spew.Sdump(wantTxs[0].Tx.Inputs))
114 if !reflect.DeepEqual(gotTx.Tx.Outputs[0].AssetAmount, wantTxs[index].Tx.Outputs[0].AssetAmount) {
115 t.Fatalf("mempool tx err. index:%d\n,gotTx:%s\n,wantTx:%s", i, spew.Sdump(gotTx.Tx.Outputs), spew.Sdump(wantTxs[0].Tx.Outputs))
120 func TestBroadcastTxsLoop(t *testing.T) {
121 tmpDir, err := ioutil.TempDir(".", "")
123 t.Fatalf("failed to create temporary data folder: %v", err)
125 defer os.RemoveAll(tmpDir)
126 testDBA := dbm.NewDB("testdba", "leveldb", tmpDir)
127 testDBB := dbm.NewDB("testdbb", "leveldb", tmpDir)
129 blocks := mockBlocks(nil, 5)
130 a := mockSync(blocks, &mock.Mempool{}, testDBA)
131 b := mockSync(blocks, &mock.Mempool{}, testDBB)
132 a.mempool = &mempool{}
133 netWork := NewNetWork()
134 netWork.Register(a, "192.168.0.1", "test node A", consensus.SFFullNode)
135 netWork.Register(b, "192.168.0.2", "test node B", consensus.SFFullNode)
136 if B2A, A2B, err := netWork.HandsShake(a, b); err != nil {
137 t.Errorf("fail on peer hands shake %v", err)
142 a.txMsgSub, err = a.eventDispatcher.Subscribe(core.TxMsgEvent{})
144 t.Fatal("txMsgSub subscribe err", err)
146 go a.broadcastTxsLoop()
147 wantTxs := a.mempool.GetTransactions()
149 for i, txD := range wantTxs {
153 a.eventDispatcher.Post(core.TxMsgEvent{TxMsg: &core.TxPoolMsg{TxDesc: txD, MsgType: core.MsgNewTx}})
155 timeout := time.NewTimer(2 * time.Second)
157 ticker := time.NewTicker(500 * time.Millisecond)
160 gotTxs := []*protocol.TxDesc{}
164 gotTxs = b.mempool.GetTransactions()
165 if len(gotTxs) >= txsNum {
169 t.Fatalf("mempool sync timeout")
174 if len(gotTxs) != txsNum {
175 t.Fatalf("mempool sync txs num err. got:%d want:%d", len(gotTxs), txsNumber)
178 for i, gotTx := range gotTxs {
179 index := gotTx.Tx.Inputs[0].Amount()
180 if !reflect.DeepEqual(gotTx.Tx.Inputs[0].Amount(), wantTxs[index].Tx.Inputs[0].Amount()) {
181 t.Fatalf("mempool tx err. index:%d\n,gotTx:%s\n,wantTx:%s", i, spew.Sdump(gotTx.Tx.Inputs), spew.Sdump(wantTxs[0].Tx.Inputs))
184 if !reflect.DeepEqual(gotTx.Tx.Outputs[0].AssetAmount, wantTxs[index].Tx.Outputs[0].AssetAmount) {
185 t.Fatalf("mempool tx err. index:%d\n,gotTx:%s\n,wantTx:%s", i, spew.Sdump(gotTx.Tx.Outputs), spew.Sdump(wantTxs[0].Tx.Outputs))