OSDN Git Service

Fix format verbs in tests (#1239)
[bytom/bytom.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
10         "github.com/bytom/account"
11         "github.com/bytom/asset"
12         "github.com/bytom/blockchain/pseudohsm"
13         "github.com/bytom/blockchain/query"
14         "github.com/bytom/consensus"
15         "github.com/bytom/crypto/ed25519/chainkd"
16         "github.com/bytom/protocol/bc/types"
17         "github.com/bytom/testutil"
18 )
19
20 func TestWalletUnconfirmedTxs(t *testing.T) {
21         dirPath, err := ioutil.TempDir(".", "")
22         if err != nil {
23                 t.Fatal(err)
24         }
25         defer os.RemoveAll(dirPath)
26
27         testDB := dbm.NewDB("testdb", "leveldb", "temp")
28         defer os.RemoveAll("temp")
29
30         accountManager := account.NewManager(testDB, nil)
31         hsm, err := pseudohsm.New(dirPath)
32         if err != nil {
33                 t.Fatal(err)
34         }
35
36         xpub1, err := hsm.XCreate("test_pub1", "password")
37         if err != nil {
38                 t.Fatal(err)
39         }
40
41         testAccount, err := accountManager.Create([]chainkd.XPub{xpub1.XPub}, 1, "testAccount")
42         if err != nil {
43                 t.Fatal(err)
44         }
45
46         controlProg, err := accountManager.CreateAddress(testAccount.ID, false)
47         if err != nil {
48                 t.Fatal(err)
49         }
50
51         controlProg.KeyIndex = 1
52
53         reg := asset.NewRegistry(testDB, nil)
54         asset, err := reg.Define([]chainkd.XPub{xpub1.XPub}, 1, nil, "TESTASSET")
55         if err != nil {
56                 t.Fatal(err)
57         }
58
59         w := mockWallet(testDB, accountManager, reg, nil)
60         utxos := []*account.UTXO{}
61         btmUtxo := mockUTXO(controlProg, consensus.BTMAssetID)
62         utxos = append(utxos, btmUtxo)
63
64         OtherUtxo := mockUTXO(controlProg, &asset.AssetID)
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 }