OSDN Git Service

add unit test for unconfirmed transaction
[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         "chain/testutil"
11         "github.com/bytom/account"
12         "github.com/bytom/asset"
13         "github.com/bytom/blockchain/pseudohsm"
14         "github.com/bytom/blockchain/query"
15         "github.com/bytom/consensus"
16         "github.com/bytom/crypto/ed25519/chainkd"
17         "github.com/bytom/protocol/bc/types"
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(nil, []chainkd.XPub{xpub1.XPub}, 1, "testAccount")
42         if err != nil {
43                 t.Fatal(err)
44         }
45
46         controlProg, err := accountManager.CreateAddress(nil, 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         _, txData, err := mockTxData(utxos, testAccount)
65         if err != nil {
66                 t.Fatal(err)
67         }
68         testTx1 := types.NewTx(*txData)
69         w.SaveUnconfirmedTx(testTx1)
70
71         OtherUtxo := mockUTXO(controlProg, &asset.AssetID)
72         utxos = append(utxos, OtherUtxo)
73         _, txData, err = mockTxData(utxos, testAccount)
74         if err != nil {
75                 t.Fatal(err)
76         }
77         testTx2 := types.NewTx(*txData)
78         w.SaveUnconfirmedTx(testTx2)
79
80         txs := AnnotatedTxs([]*types.Tx{testTx1}, w)
81         wantTx := txs[0]
82         gotTx, err := w.GetUnconfirmedTxByTxID(testTx1.ID.String())
83         if !testutil.DeepEqual(gotTx.ID, wantTx.ID) {
84                 t.Errorf(`transaction got=%#v; want=%#v`, gotTx.ID, wantTx.ID)
85         }
86
87         wantTxs := AnnotatedTxs([]*types.Tx{testTx1, testTx2}, w)
88         gotTxs, err := w.GetUnconfirmedTxs("")
89         for i, want := range wantTxs {
90                 if !testutil.DeepEqual(gotTxs[i].ID, want.ID) {
91                         t.Errorf(`the NO %i transaction, tx got=%#v; want=%#v`, i, gotTxs[i].ID.String(), want.ID.String())
92                 }
93
94                 for j, input := range want.Inputs {
95                         if !testutil.DeepEqual(gotTxs[i].Inputs[j].AccountID, input.AccountID) {
96                                 t.Errorf(`the NO %i transaction input, accountID got=%#v; want=%#v`, j, gotTxs[i].Inputs[j].AccountID, input.AccountID)
97                         }
98
99                         if !testutil.DeepEqual(gotTxs[i].Inputs[j].AssetID, input.AssetID) {
100                                 t.Errorf(`the NO %i transaction input, assetID got=%#v; want=%#v`, j, gotTxs[i].Inputs[j].AssetID, input.AssetID)
101                         }
102                 }
103
104                 for k, output := range want.Outputs {
105                         if !testutil.DeepEqual(gotTxs[i].Outputs[k].AccountID, output.AccountID) {
106                                 t.Errorf(`the NO %i transaction input, accountID got=%#v; want=%#v`, k, gotTxs[i].Inputs[k].AccountID, output.AccountID)
107                         }
108
109                         if !testutil.DeepEqual(gotTxs[i].Outputs[k].AssetID, output.AssetID) {
110                                 t.Errorf(`the NO %i transaction input, assetID got=%#v; want=%#v`, k, gotTxs[i].Inputs[k].AssetID, output.AssetID)
111                         }
112                 }
113         }
114 }
115
116 func AnnotatedTxs(txs []*types.Tx, w *Wallet) []*query.AnnotatedTx {
117         // annotate account and asset
118         annotatedTxs := []*query.AnnotatedTx{}
119         for _, tx := range txs {
120                 annotatedTx := &query.AnnotatedTx{
121                         ID:      tx.ID,
122                         Inputs:  make([]*query.AnnotatedInput, 0, len(tx.Inputs)),
123                         Outputs: make([]*query.AnnotatedOutput, 0, len(tx.Outputs)),
124                         Size:    tx.SerializedSize,
125                 }
126
127                 for i := range tx.Inputs {
128                         annotatedTx.Inputs = append(annotatedTx.Inputs, w.BuildAnnotatedInput(tx, uint32(i)))
129                 }
130                 for i := range tx.Outputs {
131                         annotatedTx.Outputs = append(annotatedTx.Outputs, w.BuildAnnotatedOutput(tx, i))
132                 }
133                 annotatedTxs = append(annotatedTxs, annotatedTx)
134         }
135
136         annotateTxsAccount(annotatedTxs, w.DB)
137         annotateTxsAsset(w, annotatedTxs)
138
139         return annotatedTxs
140 }