OSDN Git Service

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