OSDN Git Service

add test case (#1636)
[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/blockchain/signers"
15         "github.com/bytom/consensus"
16         "github.com/bytom/crypto/ed25519/chainkd"
17         "github.com/bytom/event"
18         "github.com/bytom/protocol/bc/types"
19         "github.com/bytom/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, err := reg.Define([]chainkd.XPub{xpub1.XPub}, 1, nil, "TESTASSET", nil)
57         if err != nil {
58                 t.Fatal(err)
59         }
60
61         dispatcher := event.NewDispatcher()
62         w := mockWallet(testDB, accountManager, reg, nil, dispatcher)
63         utxos := []*account.UTXO{}
64         btmUtxo := mockUTXO(controlProg, consensus.BTMAssetID)
65         utxos = append(utxos, btmUtxo)
66
67         OtherUtxo := mockUTXO(controlProg, &asset.AssetID)
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.DB)
133         annotateTxsAsset(w, annotatedTxs)
134
135         return annotatedTxs
136 }