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"
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)
35 config.CommonConfig.Consensus.Signers = append(config.CommonConfig.Consensus.Signers, address)
37 config.CommonConfig.Consensus.Coinbase = "vsm1qkm743xmgnvh84pmjchq2s4tnfpgu9ae2f9slep"
38 address, err := common.DecodeAddress(config.CommonConfig.Consensus.Coinbase, &consensus.SoloNetParams)
43 dirPath, err := ioutil.TempDir(".", "")
47 defer os.RemoveAll(dirPath)
49 testDB := dbm.NewDB("testdb", "leveldb", "temp")
50 defer os.RemoveAll("temp")
52 store := leveldb.NewStore(testDB)
53 txPool := protocol.NewTxPool(store)
55 chain, err := protocol.NewChain(store, txPool)
60 accountManager := account.NewManager(testDB, chain)
61 hsm, err := pseudohsm.New(dirPath)
66 xpub1, _, err := hsm.XCreate("test_pub1", "password", "en")
71 testAccount, err := accountManager.Create([]chainkd.XPub{xpub1.XPub}, 1, "testAccount", signers.BIP0044)
76 controlProg, err := accountManager.CreateAddress(testAccount.ID, false)
81 controlProg.KeyIndex = 1
83 reg := asset.NewRegistry(testDB, chain)
84 asset, err := reg.Define([]chainkd.XPub{xpub1.XPub}, 1, nil, "TESTASSET", nil)
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)
95 _, txData, err := mockTxData(utxos, testAccount)
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)
111 if _, err := w.GetTransactionByTxID(tx.ID.String()); err != nil {
115 wants, err := w.GetTransactions("")
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
128 utxo.ControlProgram = controlProg.ControlProgram
129 utxo.AccountID = controlProg.AccountID
130 utxo.Address = controlProg.Address
131 utxo.ControlProgramIndex = controlProg.KeyIndex
135 func mockTxData(utxos []*account.UTXO, testAccount *account.Account) (*txbuilder.Template, *types.TxData, error) {
136 tplBuilder := txbuilder.NewBuilder(time.Now())
138 for _, utxo := range utxos {
139 txInput, sigInst, err := account.UtxoToInputs(testAccount.Signer, utxo)
143 tplBuilder.AddInput(txInput, sigInst)
145 out := &types.TxOutput{}
146 if utxo.AssetID == *consensus.BTMAssetID {
147 out = types.NewTxOutput(utxo.AssetID, 100, utxo.ControlProgram)
149 out = types.NewTxOutput(utxo.AssetID, utxo.Amount, utxo.ControlProgram)
151 tplBuilder.AddOutput(out)
154 return tplBuilder.Build()
157 func mockWallet(walletDB dbm.DB, account *account.Manager, asset *asset.Registry, chain *protocol.Chain, address common.Address) *Wallet {
163 RecoveryMgr: newRecoveryManager(walletDB, account),
164 dposAddress: address,
169 func mockSingleBlock(tx *types.Tx) *types.Block {
171 BlockHeader: types.BlockHeader{
175 Transactions: []*types.Tx{tx},