OSDN Git Service

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