OSDN Git Service

60ece6041b21dc6fc04481e84645554990c07b20
[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         defer os.RemoveAll("temp")
31
32         accountManager := account.NewManager(testDB, nil)
33         hsm, err := pseudohsm.New(dirPath)
34         if err != nil {
35                 t.Fatal(err)
36         }
37
38         xpub1, _, err := hsm.XCreate("test_pub1", "password", "en")
39         if err != nil {
40                 t.Fatal(err)
41         }
42
43         testAccount, err := accountManager.Create([]chainkd.XPub{xpub1.XPub}, 1, "testAccount", signers.BIP0044)
44         if err != nil {
45                 t.Fatal(err)
46         }
47
48         controlProg, err := accountManager.CreateAddress(testAccount.ID, false)
49         if err != nil {
50                 t.Fatal(err)
51         }
52
53         controlProg.KeyIndex = 1
54
55         reg := asset.NewRegistry(testDB, nil)
56         asset := bc.AssetID{V0: 5}
57
58         dispatcher := event.NewDispatcher()
59         w := mockWallet(testDB, accountManager, reg, nil, dispatcher, false)
60         utxos := []*account.UTXO{}
61         btmUtxo := mockUTXO(controlProg, consensus.BTMAssetID)
62         utxos = append(utxos, btmUtxo)
63
64         OtherUtxo := mockUTXO(controlProg, &asset)
65         utxos = append(utxos, OtherUtxo)
66         _, txData, err := mockTxData(utxos, testAccount)
67         if err != nil {
68                 t.Fatal(err)
69         }
70         testTx := types.NewTx(*txData)
71         w.saveUnconfirmedTx(testTx)
72
73         txs := AnnotatedTxs([]*types.Tx{testTx}, w)
74         wantTx := txs[0]
75         gotTx, err := w.GetUnconfirmedTxByTxID(testTx.ID.String())
76         if !testutil.DeepEqual(gotTx.ID, wantTx.ID) {
77                 t.Errorf(`transaction got=%#v; want=%#v`, gotTx.ID, wantTx.ID)
78         }
79
80         wantTxs := AnnotatedTxs([]*types.Tx{testTx}, w)
81         gotTxs, err := w.GetUnconfirmedTxs("")
82         for i, want := range wantTxs {
83                 if !testutil.DeepEqual(gotTxs[i].ID, want.ID) {
84                         t.Errorf(`the NO %d transaction, tx got=%#v; want=%#v`, i, gotTxs[i].ID.String(), want.ID.String())
85                 }
86
87                 for j, input := range want.Inputs {
88                         if !testutil.DeepEqual(gotTxs[i].Inputs[j].AccountID, input.AccountID) {
89                                 t.Errorf(`the NO %d transaction input, accountID got=%#v; want=%#v`, j, gotTxs[i].Inputs[j].AccountID, input.AccountID)
90                         }
91
92                         if !testutil.DeepEqual(gotTxs[i].Inputs[j].AssetID, input.AssetID) {
93                                 t.Errorf(`the NO %d transaction input, assetID got=%#v; want=%#v`, j, gotTxs[i].Inputs[j].AssetID, input.AssetID)
94                         }
95                 }
96
97                 for k, output := range want.Outputs {
98                         if !testutil.DeepEqual(gotTxs[i].Outputs[k].AccountID, output.AccountID) {
99                                 t.Errorf(`the NO %d transaction input, accountID got=%#v; want=%#v`, k, gotTxs[i].Inputs[k].AccountID, output.AccountID)
100                         }
101
102                         if !testutil.DeepEqual(gotTxs[i].Outputs[k].AssetID, output.AssetID) {
103                                 t.Errorf(`the NO %d transaction input, assetID got=%#v; want=%#v`, k, gotTxs[i].Inputs[k].AssetID, output.AssetID)
104                         }
105                 }
106         }
107 }
108
109 func AnnotatedTxs(txs []*types.Tx, w *Wallet) []*query.AnnotatedTx {
110         // annotate account and asset
111         annotatedTxs := []*query.AnnotatedTx{}
112         for _, tx := range txs {
113                 annotatedTx := &query.AnnotatedTx{
114                         ID:      tx.ID,
115                         Inputs:  make([]*query.AnnotatedInput, 0, len(tx.Inputs)),
116                         Outputs: make([]*query.AnnotatedOutput, 0, len(tx.Outputs)),
117                         Size:    tx.SerializedSize,
118                 }
119
120                 for i := range tx.Inputs {
121                         annotatedTx.Inputs = append(annotatedTx.Inputs, w.BuildAnnotatedInput(tx, uint32(i)))
122                 }
123                 for i := range tx.Outputs {
124                         annotatedTx.Outputs = append(annotatedTx.Outputs, w.BuildAnnotatedOutput(tx, i))
125                 }
126                 annotatedTxs = append(annotatedTxs, annotatedTx)
127         }
128
129         annotateTxsAccount(annotatedTxs, w.DB)
130         annotateTxsAsset(w, annotatedTxs)
131
132         return annotatedTxs
133 }