10 "github.com/davecgh/go-spew/spew"
12 "github.com/vapor/consensus"
13 dbm "github.com/vapor/database/leveldb"
14 "github.com/vapor/protocol"
15 "github.com/vapor/protocol/bc"
16 "github.com/vapor/protocol/bc/types"
17 "github.com/vapor/test/mock"
20 const txsNumber = 2000
22 func getTransactions() []*types.Tx {
24 for i := 0; i < txsNumber; i++ {
25 txInput := types.NewSpendInput(nil, bc.NewHash([32]byte{0x01}), *consensus.BTMAssetID, uint64(i), 1, []byte{0x51})
26 txInput.CommitmentSuffix = []byte{0, 1, 2}
27 txInput.WitnessSuffix = []byte{0, 1, 2}
32 //SerializedSize: uint64(i * 10),
33 Inputs: []*types.TxInput{
36 Outputs: []*types.TxOutput{
37 types.NewIntraChainOutput(*consensus.BTMAssetID, uint64(i), []byte{0x6a}),
41 ID: bc.Hash{V0: uint64(i), V1: uint64(i), V2: uint64(i), V3: uint64(i)},
49 func TestSyncMempool(t *testing.T) {
50 tmpDir, err := ioutil.TempDir(".", "")
52 t.Fatalf("failed to create temporary data folder: %v", err)
54 defer os.RemoveAll(tmpDir)
55 testDBA := dbm.NewDB("testdba", "leveldb", tmpDir)
56 testDBB := dbm.NewDB("testdbb", "leveldb", tmpDir)
58 blocks := mockBlocks(nil, 5)
59 a := mockSync(blocks, &mock.Mempool{}, testDBA)
60 b := mockSync(blocks, &mock.Mempool{}, testDBB)
62 netWork := NewNetWork()
63 netWork.Register(a, "192.168.0.1", "test node A", consensus.SFFullNode)
64 netWork.Register(b, "192.168.0.2", "test node B", consensus.SFFullNode)
65 if B2A, A2B, err := netWork.HandsShake(a, b); err != nil {
66 t.Errorf("fail on peer hands shake %v", err)
72 go a.syncMempoolLoop()
73 a.syncMempool("test node B")
74 wantTxs := getTransactions()
75 a.txSyncCh <- &txSyncMsg{"test node B", wantTxs}
77 timeout := time.NewTimer(2 * time.Second)
79 ticker := time.NewTicker(500 * time.Millisecond)
82 gotTxs := []*protocol.TxDesc{}
86 gotTxs = b.mempool.GetTransactions()
87 if len(gotTxs) >= txsNumber {
91 t.Fatalf("mempool sync timeout")
96 if len(gotTxs) != txsNumber {
97 t.Fatalf("mempool sync txs num err. got:%d want:%d", len(gotTxs), txsNumber)
100 for i, gotTx := range gotTxs {
101 index := gotTx.Tx.Inputs[0].Amount()
102 if !reflect.DeepEqual(gotTx.Tx.Inputs[0].Amount(), wantTxs[index].Inputs[0].Amount()) {
103 t.Fatalf("mempool tx err. index:%d\n,gotTx:%s\n,wantTx:%s", i, spew.Sdump(gotTx.Tx.Inputs), spew.Sdump(wantTxs[0].Inputs))
106 if !reflect.DeepEqual(gotTx.Tx.Outputs[0].AssetAmount(), wantTxs[index].Outputs[0].AssetAmount()) {
107 t.Fatalf("mempool tx err. index:%d\n,gotTx:%s\n,wantTx:%s", i, spew.Sdump(gotTx.Tx.Outputs), spew.Sdump(wantTxs[0].Outputs))