9 dbm "github.com/tendermint/tmlibs/db"
10 "github.com/vapor/blockchain/signers"
11 "github.com/vapor/common"
12 "github.com/vapor/config"
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"
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)
36 config.CommonConfig.Consensus.Dpos.Signers = append(config.CommonConfig.Consensus.Dpos.Signers, address)
38 config.CommonConfig.Consensus.Dpos.Coinbase = "vsm1qkm743xmgnvh84pmjchq2s4tnfpgu9ae2f9slep"
39 address, err := common.DecodeAddress(config.CommonConfig.Consensus.Dpos.Coinbase, &consensus.SoloNetParams)
44 dirPath, err := ioutil.TempDir(".", "")
48 defer os.RemoveAll(dirPath)
50 testDB := dbm.NewDB("testdb", "leveldb", "temp")
51 defer os.RemoveAll("temp")
53 store := leveldb.NewStore(testDB)
54 txPool := protocol.NewTxPool(store)
56 chain, err := protocol.NewChain(store, txPool)
61 accountManager := account.NewManager(testDB, chain)
62 hsm, err := pseudohsm.New(dirPath)
67 xpub1, _, err := hsm.XCreate("test_pub1", "password", "en")
72 testAccount, err := accountManager.Create([]chainkd.XPub{xpub1.XPub}, 1, "testAccount", signers.BIP0044)
77 controlProg, err := accountManager.CreateAddress(testAccount.ID, false)
82 controlProg.KeyIndex = 1
84 reg := asset.NewRegistry(testDB, chain)
85 asset, err := reg.Define([]chainkd.XPub{xpub1.XPub}, 1, nil, "TESTASSET", nil)
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)
96 _, txData, err := mockTxData(utxos, testAccount)
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)
112 if _, err := w.GetTransactionByTxID(tx.ID.String()); err != nil {
116 wants, err := w.GetTransactions("")
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
129 utxo.ControlProgram = controlProg.ControlProgram
130 utxo.AccountID = controlProg.AccountID
131 utxo.Address = controlProg.Address
132 utxo.ControlProgramIndex = controlProg.KeyIndex
136 func mockTxData(utxos []*account.UTXO, testAccount *account.Account) (*txbuilder.Template, *types.TxData, error) {
137 tplBuilder := txbuilder.NewBuilder(time.Now())
139 for _, utxo := range utxos {
140 txInput, sigInst, err := account.UtxoToInputs(testAccount.Signer, utxo)
144 tplBuilder.AddInput(txInput, sigInst)
146 out := &types.TxOutput{}
147 if utxo.AssetID == *consensus.BTMAssetID {
148 out = types.NewTxOutput(utxo.AssetID, 100, utxo.ControlProgram)
150 out = types.NewTxOutput(utxo.AssetID, utxo.Amount, utxo.ControlProgram)
152 tplBuilder.AddOutput(out)
155 return tplBuilder.Build()
158 func mockWallet(walletDB dbm.DB, account *account.Manager, asset *asset.Registry, chain *protocol.Chain, address common.Address) *Wallet {
164 RecoveryMgr: newRecoveryManager(walletDB, account),
165 dposAddress: address,
170 func mockSingleBlock(tx *types.Tx) *types.Block {
172 BlockHeader: types.BlockHeader{
176 Transactions: []*types.Tx{tx},