package wallet import ( "io/ioutil" "os" "testing" "time" dbm "github.com/tendermint/tmlibs/db" "github.com/vapor/blockchain/signers" "github.com/vapor/common" "github.com/vapor/config" "github.com/vapor/account" "github.com/vapor/asset" "github.com/vapor/blockchain/pseudohsm" "github.com/vapor/blockchain/txbuilder" "github.com/vapor/consensus" "github.com/vapor/crypto/ed25519/chainkd" "github.com/vapor/database/leveldb" "github.com/vapor/protocol" "github.com/vapor/protocol/bc" "github.com/vapor/protocol/bc/types" ) func TestWalletUpdate(t *testing.T) { config.CommonConfig = config.DefaultConfig() consensus.SoloNetParams.Signer = "78673764e0ba91a4c5ba9ec0c8c23c69e3d73bf27970e05e0a977e81e13bde475264d3b177a96646bc0ce517ae7fd63504c183ab6d330dea184331a4cf5912d5" config.CommonConfig.Consensus.Dpos.SelfVoteSigners = append(config.CommonConfig.Consensus.Dpos.SelfVoteSigners, "vsm1qkm743xmgnvh84pmjchq2s4tnfpgu9ae2f9slep") config.CommonConfig.Consensus.Dpos.XPrv = "a8e281b615809046698fb0b0f2804a36d824d48fa443350f10f1b80649d39e5f1e85cf9855548915e36137345910606cbc8e7dd8497c831dce899ee6ac112445" for _, v := range config.CommonConfig.Consensus.Dpos.SelfVoteSigners { address, err := common.DecodeAddress(v, &consensus.SoloNetParams) if err != nil { t.Fatal(err) } config.CommonConfig.Consensus.Dpos.Signers = append(config.CommonConfig.Consensus.Dpos.Signers, address) } config.CommonConfig.Consensus.Dpos.Coinbase = "vsm1qkm743xmgnvh84pmjchq2s4tnfpgu9ae2f9slep" address, err := common.DecodeAddress(config.CommonConfig.Consensus.Dpos.Coinbase, &consensus.SoloNetParams) if err != nil { t.Fatal(err) } dirPath, err := ioutil.TempDir(".", "") if err != nil { t.Fatal(err) } defer os.RemoveAll(dirPath) testDB := dbm.NewDB("testdb", "leveldb", "temp") defer os.RemoveAll("temp") store := leveldb.NewStore(testDB) txPool := protocol.NewTxPool(store) chain, err := protocol.NewChain(store, txPool) if err != nil { t.Fatal(err) } accountManager := account.NewManager(testDB, chain) hsm, err := pseudohsm.New(dirPath) if err != nil { t.Fatal(err) } xpub1, _, err := hsm.XCreate("test_pub1", "password", "en") if err != nil { t.Fatal(err) } testAccount, err := accountManager.Create([]chainkd.XPub{xpub1.XPub}, 1, "testAccount", signers.BIP0044) if err != nil { t.Fatal(err) } controlProg, err := accountManager.CreateAddress(testAccount.ID, false) if err != nil { t.Fatal(err) } controlProg.KeyIndex = 1 reg := asset.NewRegistry(testDB, chain) asset, err := reg.Define([]chainkd.XPub{xpub1.XPub}, 1, nil, "TESTASSET", nil) if err != nil { t.Fatal(err) } utxos := []*account.UTXO{} btmUtxo := mockUTXO(controlProg, consensus.BTMAssetID) utxos = append(utxos, btmUtxo) OtherUtxo := mockUTXO(controlProg, &asset.AssetID) utxos = append(utxos, OtherUtxo) _, txData, err := mockTxData(utxos, testAccount) if err != nil { t.Fatal(err) } tx := types.NewTx(*txData) block := mockSingleBlock(tx) txStatus := bc.NewTransactionStatus() txStatus.SetStatus(0, false) store.SaveBlock(block, txStatus) w := mockWallet(testDB, accountManager, reg, chain, address) err = w.AttachBlock(block) if err != nil { t.Fatal(err) } if _, err := w.GetTransactionByTxID(tx.ID.String()); err != nil { t.Fatal(err) } wants, err := w.GetTransactions("") if len(wants) != 1 { t.Fatal(err) } } func mockUTXO(controlProg *account.CtrlProgram, assetID *bc.AssetID) *account.UTXO { utxo := &account.UTXO{} utxo.OutputID = bc.Hash{V0: 1} utxo.SourceID = bc.Hash{V0: 2} utxo.AssetID = *assetID utxo.Amount = 1000000000 utxo.SourcePos = 0 utxo.ControlProgram = controlProg.ControlProgram utxo.AccountID = controlProg.AccountID utxo.Address = controlProg.Address utxo.ControlProgramIndex = controlProg.KeyIndex return utxo } func mockTxData(utxos []*account.UTXO, testAccount *account.Account) (*txbuilder.Template, *types.TxData, error) { tplBuilder := txbuilder.NewBuilder(time.Now()) for _, utxo := range utxos { txInput, sigInst, err := account.UtxoToInputs(testAccount.Signer, utxo) if err != nil { return nil, nil, err } tplBuilder.AddInput(txInput, sigInst) out := &types.TxOutput{} if utxo.AssetID == *consensus.BTMAssetID { out = types.NewTxOutput(utxo.AssetID, 100, utxo.ControlProgram) } else { out = types.NewTxOutput(utxo.AssetID, utxo.Amount, utxo.ControlProgram) } tplBuilder.AddOutput(out) } return tplBuilder.Build() } func mockWallet(walletDB dbm.DB, account *account.Manager, asset *asset.Registry, chain *protocol.Chain, address common.Address) *Wallet { wallet := &Wallet{ DB: walletDB, AccountMgr: account, AssetReg: asset, chain: chain, RecoveryMgr: newRecoveryManager(walletDB, account), dposAddress: address, } return wallet } func mockSingleBlock(tx *types.Tx) *types.Block { return &types.Block{ BlockHeader: types.BlockHeader{ Version: 1, Height: 1, }, Transactions: []*types.Tx{tx}, } }