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/config"
16 "github.com/vapor/consensus"
17 "github.com/vapor/crypto/ed25519/chainkd"
18 "github.com/vapor/database"
19 dbm "github.com/vapor/database/leveldb"
20 "github.com/vapor/event"
21 "github.com/vapor/protocol"
22 "github.com/vapor/protocol/bc"
23 "github.com/vapor/protocol/bc/types"
24 wt "github.com/vapor/wallet"
27 func TestWalletUpdate(t *testing.T) {
28 dirPath, err := ioutil.TempDir(".", "")
32 defer os.RemoveAll(dirPath)
34 config.CommonConfig = config.DefaultConfig()
35 testDB := dbm.NewDB("testdb", "leveldb", "temp")
41 store := database.NewStore(testDB)
42 walletStore := database.NewWalletStore(testDB)
43 dispatcher := event.NewDispatcher()
44 txPool := protocol.NewTxPool(store, dispatcher)
46 chain, err := protocol.NewChain(store, txPool, dispatcher)
51 accountStore := database.NewAccountStore(testDB)
52 accountManager := account.NewManager(accountStore, chain)
53 hsm, err := pseudohsm.New(dirPath)
58 xpub1, _, err := hsm.XCreate("test_pub1", "password", "en")
63 testAccount, err := accountManager.Create([]chainkd.XPub{xpub1.XPub}, 1, "testAccount", signers.BIP0044)
68 controlProg, err := accountManager.CreateAddress(testAccount.ID, false)
73 controlProg.KeyIndex = 1
75 reg := asset.NewRegistry(testDB, chain)
76 // reg := asset.NewRegistry(testDB, nil)
77 asset := bc.AssetID{V0: 5}
79 utxos := []*account.UTXO{}
80 btmUtxo := mockUTXO(controlProg, consensus.BTMAssetID)
81 utxos = append(utxos, btmUtxo)
82 OtherUtxo := mockUTXO(controlProg, &asset)
83 utxos = append(utxos, OtherUtxo)
85 _, txData, err := mockTxData(utxos, testAccount)
90 tx := types.NewTx(*txData)
91 block := mockSingleBlock(tx)
92 txStatus := bc.NewTransactionStatus()
93 txStatus.SetStatus(0, false)
94 txStatus.SetStatus(1, false)
95 store.SaveBlock(block, txStatus)
97 w := newMockWallet(walletStore, accountManager, reg, chain, dispatcher, true)
98 err = w.Wallet.AttachBlock(block)
103 if _, err := w.Wallet.GetTransactionByTxID(tx.ID.String()); err != nil {
107 wants, err := w.Wallet.GetTransactions(testAccount.ID, "", 1, false)
112 if wants[0].ID != tx.ID {
113 t.Fatal("account txID mismatch")
116 for position, tx := range block.Transactions {
117 get := testDB.Get(database.CalcGlobalTxIndexKey(tx.ID.String()))
118 bh := block.BlockHeader.Hash()
119 expect := database.CalcGlobalTxIndex(&bh, uint64(position))
120 if !reflect.DeepEqual(get, expect) {
121 t.Fatalf("position#%d: compare retrieved globalTxIdx err", position)
126 // func TestRescanWallet(t *testing.T) {
127 // // prepare wallet & db
128 // dirPath, err := ioutil.TempDir(".", "")
132 // defer os.RemoveAll(dirPath)
134 // config.CommonConfig = config.DefaultConfig()
135 // testDB := dbm.NewDB("testdb", "leveldb", "temp")
136 // walletStore := database.NewWalletStore(testDB)
139 // os.RemoveAll("temp")
142 // store := database.NewStore(testDB)
143 // dispatcher := event.NewDispatcher()
144 // txPool := protocol.NewTxPool(store, dispatcher)
145 // chain, err := protocol.NewChain(store, txPool, dispatcher)
150 // statusInfo := wt.StatusInfo{
152 // WorkHash: bc.Hash{V0: 0xff},
154 // if err := walletStore.SetWalletInfo(&statusInfo); err != nil {
157 // walletInfo, err := walletStore.GetWalletInfo()
163 // // rawWallet, err := json.Marshal(statusInfo)
164 // // if err != nil {
165 // // t.Fatal("save wallet info")
168 // w := newMockWallet(walletStore, nil, nil, chain, dispatcher, false)
170 // // w.store.SetWalletInfo(rawWallet)
171 // // rawWallet = w.store.GetWalletInfo()
172 // // if rawWallet == nil {
173 // // t.Fatal("fail to load wallet StatusInfo")
176 // if err := json.Unmarshal(rawWallet, &w.status); err != nil {
181 // if err := w.LoadWalletInfo(); err != nil {
185 // block := config.GenesisBlock()
186 // if w.status.WorkHash != block.Hash() {
187 // t.Fatal("reattach from genesis block")
191 func mockUTXO(controlProg *account.CtrlProgram, assetID *bc.AssetID) *account.UTXO {
192 utxo := &account.UTXO{}
193 utxo.OutputID = bc.Hash{V0: 1}
194 utxo.SourceID = bc.Hash{V0: 2}
195 utxo.AssetID = *assetID
196 utxo.Amount = 1000000000
198 utxo.ControlProgram = controlProg.ControlProgram
199 utxo.AccountID = controlProg.AccountID
200 utxo.Address = controlProg.Address
201 utxo.ControlProgramIndex = controlProg.KeyIndex
205 func mockTxData(utxos []*account.UTXO, testAccount *account.Account) (*txbuilder.Template, *types.TxData, error) {
206 tplBuilder := txbuilder.NewBuilder(time.Now())
208 for _, utxo := range utxos {
209 txInput, sigInst, err := account.UtxoToInputs(testAccount.Signer, utxo)
213 tplBuilder.AddInput(txInput, sigInst)
215 out := &types.TxOutput{}
216 if utxo.AssetID == *consensus.BTMAssetID {
217 out = types.NewIntraChainOutput(utxo.AssetID, 100, utxo.ControlProgram)
219 out = types.NewIntraChainOutput(utxo.AssetID, utxo.Amount, utxo.ControlProgram)
221 tplBuilder.AddOutput(out)
224 return tplBuilder.Build()
227 type mockWallet struct {
231 func newMockWallet(store wt.WalletStore, account *account.Manager, asset *asset.Registry, chain *protocol.Chain, dispatcher *event.Dispatcher, txIndexFlag bool) *mockWallet {
232 w, err := wt.NewWallet(store, account, asset, nil, chain, dispatcher, txIndexFlag)
236 return &mockWallet{w}
239 func mockSingleBlock(tx *types.Tx) *types.Block {
241 BlockHeader: types.BlockHeader{
244 PreviousBlockHash: bc.Hash{
245 V0: uint64(14952355164967094643),
246 V1: uint64(2960316323621296529),
247 V2: uint64(4202730371317372163),
248 V3: uint64(7569611631634777133),
251 Transactions: []*types.Tx{config.GenesisTx(), tx},