OSDN Git Service

Peer add announces new block message num limit
[bytom/vapor.git] / wallet / unconfirmed_test.go
1 package wallet
2
3 import (
4         "io/ioutil"
5         "os"
6         "testing"
7
8         "github.com/vapor/account"
9         "github.com/vapor/asset"
10         "github.com/vapor/blockchain/pseudohsm"
11         "github.com/vapor/blockchain/query"
12         "github.com/vapor/blockchain/signers"
13         "github.com/vapor/consensus"
14         "github.com/vapor/crypto/ed25519/chainkd"
15         dbm "github.com/vapor/database/leveldb"
16         "github.com/vapor/event"
17         "github.com/vapor/protocol/bc"
18         "github.com/vapor/protocol/bc/types"
19         "github.com/vapor/testutil"
20 )
21
22 func TestWalletUnconfirmedTxs(t *testing.T) {
23         dirPath, err := ioutil.TempDir(".", "")
24         if err != nil {
25                 t.Fatal(err)
26         }
27         defer os.RemoveAll(dirPath)
28
29         testDB := dbm.NewDB("testdb", "leveldb", "temp")
30         testStore := NewMockWalletStore(testDB)
31         defer os.RemoveAll("temp")
32
33         accountStore := NewMockAccountStore(testDB)
34         accountManager := account.NewManager(accountStore, nil)
35         hsm, err := pseudohsm.New(dirPath)
36         if err != nil {
37                 t.Fatal(err)
38         }
39
40         xpub1, _, err := hsm.XCreate("test_pub1", "password", "en")
41         if err != nil {
42                 t.Fatal(err)
43         }
44
45         testAccount, err := accountManager.Create([]chainkd.XPub{xpub1.XPub}, 1, "testAccount", signers.BIP0044)
46         if err != nil {
47                 t.Fatal(err)
48         }
49
50         controlProg, err := accountManager.CreateAddress(testAccount.ID, false)
51         if err != nil {
52                 t.Fatal(err)
53         }
54
55         controlProg.KeyIndex = 1
56
57         reg := asset.NewRegistry(testDB, nil)
58         asset := bc.AssetID{V0: 5}
59
60         dispatcher := event.NewDispatcher()
61         w := mockWallet(testStore, accountManager, reg, nil, dispatcher, false)
62         utxos := []*account.UTXO{}
63         btmUtxo := mockUTXO(controlProg, consensus.BTMAssetID)
64         utxos = append(utxos, btmUtxo)
65
66         OtherUtxo := mockUTXO(controlProg, &asset)
67         utxos = append(utxos, OtherUtxo)
68         _, txData, err := mockTxData(utxos, testAccount)
69         if err != nil {
70                 t.Fatal(err)
71         }
72         testTx := types.NewTx(*txData)
73         w.saveUnconfirmedTx(testTx)
74
75         txs := AnnotatedTxs([]*types.Tx{testTx}, w)
76         wantTx := txs[0]
77         gotTx, err := w.GetUnconfirmedTxByTxID(testTx.ID.String())
78         if !testutil.DeepEqual(gotTx.ID, wantTx.ID) {
79                 t.Errorf(`transaction got=%#v; want=%#v`, gotTx.ID, wantTx.ID)
80         }
81
82         wantTxs := AnnotatedTxs([]*types.Tx{testTx}, w)
83         gotTxs, err := w.GetUnconfirmedTxs("")
84         for i, want := range wantTxs {
85                 if !testutil.DeepEqual(gotTxs[i].ID, want.ID) {
86                         t.Errorf(`the NO %d transaction, tx got=%#v; want=%#v`, i, gotTxs[i].ID.String(), want.ID.String())
87                 }
88
89                 for j, input := range want.Inputs {
90                         if !testutil.DeepEqual(gotTxs[i].Inputs[j].AccountID, input.AccountID) {
91                                 t.Errorf(`the NO %d transaction input, accountID got=%#v; want=%#v`, j, gotTxs[i].Inputs[j].AccountID, input.AccountID)
92                         }
93
94                         if !testutil.DeepEqual(gotTxs[i].Inputs[j].AssetID, input.AssetID) {
95                                 t.Errorf(`the NO %d transaction input, assetID got=%#v; want=%#v`, j, gotTxs[i].Inputs[j].AssetID, input.AssetID)
96                         }
97                 }
98
99                 for k, output := range want.Outputs {
100                         if !testutil.DeepEqual(gotTxs[i].Outputs[k].AccountID, output.AccountID) {
101                                 t.Errorf(`the NO %d transaction input, accountID got=%#v; want=%#v`, k, gotTxs[i].Inputs[k].AccountID, output.AccountID)
102                         }
103
104                         if !testutil.DeepEqual(gotTxs[i].Outputs[k].AssetID, output.AssetID) {
105                                 t.Errorf(`the NO %d transaction input, assetID got=%#v; want=%#v`, k, gotTxs[i].Inputs[k].AssetID, output.AssetID)
106                         }
107                 }
108         }
109 }
110
111 func AnnotatedTxs(txs []*types.Tx, w *Wallet) []*query.AnnotatedTx {
112         // annotate account and asset
113         annotatedTxs := []*query.AnnotatedTx{}
114         for _, tx := range txs {
115                 annotatedTx := &query.AnnotatedTx{
116                         ID:      tx.ID,
117                         Inputs:  make([]*query.AnnotatedInput, 0, len(tx.Inputs)),
118                         Outputs: make([]*query.AnnotatedOutput, 0, len(tx.Outputs)),
119                         Size:    tx.SerializedSize,
120                 }
121
122                 for i := range tx.Inputs {
123                         annotatedTx.Inputs = append(annotatedTx.Inputs, w.BuildAnnotatedInput(tx, uint32(i)))
124                 }
125                 for i := range tx.Outputs {
126                         annotatedTx.Outputs = append(annotatedTx.Outputs, w.BuildAnnotatedOutput(tx, i))
127                 }
128                 annotatedTxs = append(annotatedTxs, annotatedTx)
129         }
130
131         w.annotateTxsAccount(annotatedTxs)
132         annotateTxsAsset(w, annotatedTxs)
133
134         return annotatedTxs
135 }