OSDN Git Service

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