OSDN Git Service

fix bug
[bytom/vapor.git] / wallet / wallet_test.go
1 package wallet
2
3 import (
4         "io/ioutil"
5         "os"
6         "testing"
7         "time"
8
9         dbm "github.com/tendermint/tmlibs/db"
10         "github.com/vapor/account"
11         "github.com/vapor/asset"
12         "github.com/vapor/blockchain/pseudohsm"
13         "github.com/vapor/blockchain/signers"
14         "github.com/vapor/blockchain/txbuilder"
15         "github.com/vapor/common"
16         "github.com/vapor/config"
17         "github.com/vapor/consensus"
18         "github.com/vapor/crypto/ed25519/chainkd"
19         "github.com/vapor/database/leveldb"
20         "github.com/vapor/protocol"
21         "github.com/vapor/protocol/bc"
22         "github.com/vapor/protocol/bc/types"
23 )
24
25 func TestWalletUpdate(t *testing.T) {
26         config.CommonConfig = config.DefaultConfig()
27         consensus.SoloNetParams.Signer = "78673764e0ba91a4c5ba9ec0c8c23c69e3d73bf27970e05e0a977e81e13bde475264d3b177a96646bc0ce517ae7fd63504c183ab6d330dea184331a4cf5912d5"
28         config.CommonConfig.Consensus.SelfVoteSigners = append(config.CommonConfig.Consensus.SelfVoteSigners, "vsm1qkm743xmgnvh84pmjchq2s4tnfpgu9ae2f9slep")
29         config.CommonConfig.Consensus.XPrv = "a8e281b615809046698fb0b0f2804a36d824d48fa443350f10f1b80649d39e5f1e85cf9855548915e36137345910606cbc8e7dd8497c831dce899ee6ac112445"
30         for _, v := range config.CommonConfig.Consensus.SelfVoteSigners {
31                 address, err := common.DecodeAddress(v, &consensus.SoloNetParams)
32                 if err != nil {
33                         t.Fatal(err)
34                 }
35                 config.CommonConfig.Consensus.Signers = append(config.CommonConfig.Consensus.Signers, address)
36         }
37         config.CommonConfig.Consensus.Coinbase = "vsm1qkm743xmgnvh84pmjchq2s4tnfpgu9ae2f9slep"
38         address, err := common.DecodeAddress(config.CommonConfig.Consensus.Coinbase, &consensus.SoloNetParams)
39         if err != nil {
40                 t.Fatal(err)
41         }
42
43         dirPath, err := ioutil.TempDir(".", "")
44         if err != nil {
45                 t.Fatal(err)
46         }
47         defer os.RemoveAll(dirPath)
48
49         testDB := dbm.NewDB("testdb", "leveldb", "temp")
50         defer os.RemoveAll("temp")
51
52         store := leveldb.NewStore(testDB)
53         txPool := protocol.NewTxPool(store)
54
55         chain, err := protocol.NewChain(store, txPool)
56         if err != nil {
57                 t.Fatal(err)
58         }
59
60         accountManager := account.NewManager(testDB, chain)
61         hsm, err := pseudohsm.New(dirPath)
62         if err != nil {
63                 t.Fatal(err)
64         }
65
66         xpub1, _, err := hsm.XCreate("test_pub1", "password", "en")
67         if err != nil {
68                 t.Fatal(err)
69         }
70
71         testAccount, err := accountManager.Create([]chainkd.XPub{xpub1.XPub}, 1, "testAccount", signers.BIP0044)
72         if err != nil {
73                 t.Fatal(err)
74         }
75
76         controlProg, err := accountManager.CreateAddress(testAccount.ID, false)
77         if err != nil {
78                 t.Fatal(err)
79         }
80
81         controlProg.KeyIndex = 1
82
83         reg := asset.NewRegistry(testDB, chain)
84         asset, err := reg.Define([]chainkd.XPub{xpub1.XPub}, 1, nil, "TESTASSET", nil)
85         if err != nil {
86                 t.Fatal(err)
87         }
88
89         utxos := []*account.UTXO{}
90         btmUtxo := mockUTXO(controlProg, consensus.BTMAssetID)
91         utxos = append(utxos, btmUtxo)
92         OtherUtxo := mockUTXO(controlProg, &asset.AssetID)
93         utxos = append(utxos, OtherUtxo)
94
95         _, txData, err := mockTxData(utxos, testAccount)
96         if err != nil {
97                 t.Fatal(err)
98         }
99
100         tx := types.NewTx(*txData)
101         block := mockSingleBlock(tx)
102         txStatus := bc.NewTransactionStatus()
103         txStatus.SetStatus(0, false)
104         store.SaveBlock(block, txStatus)
105         w := mockWallet(testDB, accountManager, reg, chain, address)
106         err = w.AttachBlock(block)
107         if err != nil {
108                 t.Fatal(err)
109         }
110
111         if _, err := w.GetTransactionByTxID(tx.ID.String()); err != nil {
112                 t.Fatal(err)
113         }
114
115         wants, err := w.GetTransactions("")
116         if len(wants) != 1 {
117                 t.Fatal(err)
118         }
119 }
120
121 func mockUTXO(controlProg *account.CtrlProgram, assetID *bc.AssetID) *account.UTXO {
122         utxo := &account.UTXO{}
123         utxo.OutputID = bc.Hash{V0: 1}
124         utxo.SourceID = bc.Hash{V0: 2}
125         utxo.AssetID = *assetID
126         utxo.Amount = 1000000000
127         utxo.SourcePos = 0
128         utxo.ControlProgram = controlProg.ControlProgram
129         utxo.AccountID = controlProg.AccountID
130         utxo.Address = controlProg.Address
131         utxo.ControlProgramIndex = controlProg.KeyIndex
132         return utxo
133 }
134
135 func mockTxData(utxos []*account.UTXO, testAccount *account.Account) (*txbuilder.Template, *types.TxData, error) {
136         tplBuilder := txbuilder.NewBuilder(time.Now())
137
138         for _, utxo := range utxos {
139                 txInput, sigInst, err := account.UtxoToInputs(testAccount.Signer, utxo)
140                 if err != nil {
141                         return nil, nil, err
142                 }
143                 tplBuilder.AddInput(txInput, sigInst)
144
145                 out := &types.TxOutput{}
146                 if utxo.AssetID == *consensus.BTMAssetID {
147                         out = types.NewTxOutput(utxo.AssetID, 100, utxo.ControlProgram)
148                 } else {
149                         out = types.NewTxOutput(utxo.AssetID, utxo.Amount, utxo.ControlProgram)
150                 }
151                 tplBuilder.AddOutput(out)
152         }
153
154         return tplBuilder.Build()
155 }
156
157 func mockWallet(walletDB dbm.DB, account *account.Manager, asset *asset.Registry, chain *protocol.Chain, address common.Address) *Wallet {
158         wallet := &Wallet{
159                 DB:          walletDB,
160                 AccountMgr:  account,
161                 AssetReg:    asset,
162                 chain:       chain,
163                 RecoveryMgr: newRecoveryManager(walletDB, account),
164                 dposAddress: address,
165         }
166         return wallet
167 }
168
169 func mockSingleBlock(tx *types.Tx) *types.Block {
170         return &types.Block{
171                 BlockHeader: types.BlockHeader{
172                         Version: 1,
173                         Height:  1,
174                 },
175                 Transactions: []*types.Tx{tx},
176         }
177 }