OSDN Git Service

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