8 dbm "github.com/tendermint/tmlibs/db"
9 "github.com/vapor/blockchain/signers"
10 "github.com/vapor/common"
11 "github.com/vapor/config"
13 "github.com/vapor/account"
14 "github.com/vapor/asset"
15 "github.com/vapor/blockchain/pseudohsm"
16 "github.com/vapor/blockchain/query"
17 "github.com/vapor/consensus"
18 "github.com/vapor/crypto/ed25519/chainkd"
19 "github.com/vapor/protocol/bc/types"
20 "github.com/vapor/testutil"
23 func TestWalletUnconfirmedTxs(t *testing.T) {
24 config.CommonConfig = config.DefaultConfig()
25 consensus.SoloNetParams.Signer = "78673764e0ba91a4c5ba9ec0c8c23c69e3d73bf27970e05e0a977e81e13bde475264d3b177a96646bc0ce517ae7fd63504c183ab6d330dea184331a4cf5912d5"
26 config.CommonConfig.Consensus.Dpos.SelfVoteSigners = append(config.CommonConfig.Consensus.Dpos.SelfVoteSigners, "vsm1qkm743xmgnvh84pmjchq2s4tnfpgu9ae2f9slep")
27 config.CommonConfig.Consensus.Dpos.XPrv = "a8e281b615809046698fb0b0f2804a36d824d48fa443350f10f1b80649d39e5f1e85cf9855548915e36137345910606cbc8e7dd8497c831dce899ee6ac112445"
28 for _, v := range config.CommonConfig.Consensus.Dpos.SelfVoteSigners {
29 address, err := common.DecodeAddress(v, &consensus.SoloNetParams)
33 config.CommonConfig.Consensus.Dpos.Signers = append(config.CommonConfig.Consensus.Dpos.Signers, address)
35 config.CommonConfig.Consensus.Dpos.Coinbase = "vsm1qkm743xmgnvh84pmjchq2s4tnfpgu9ae2f9slep"
36 address, err := common.DecodeAddress(config.CommonConfig.Consensus.Dpos.Coinbase, &consensus.SoloNetParams)
40 dirPath, err := ioutil.TempDir(".", "")
44 defer os.RemoveAll(dirPath)
46 testDB := dbm.NewDB("testdb", "leveldb", "temp")
47 defer os.RemoveAll("temp")
49 accountManager := account.NewManager(testDB, nil)
50 hsm, err := pseudohsm.New(dirPath)
55 xpub1, _, err := hsm.XCreate("test_pub1", "password", "en")
60 testAccount, err := accountManager.Create([]chainkd.XPub{xpub1.XPub}, 1, "testAccount", signers.BIP0044)
65 controlProg, err := accountManager.CreateAddress(testAccount.ID, false)
70 controlProg.KeyIndex = 1
72 reg := asset.NewRegistry(testDB, nil)
73 asset, err := reg.Define([]chainkd.XPub{xpub1.XPub}, 1, nil, "TESTASSET", nil)
78 w := mockWallet(testDB, accountManager, reg, nil, address)
79 utxos := []*account.UTXO{}
80 btmUtxo := mockUTXO(controlProg, consensus.BTMAssetID)
81 utxos = append(utxos, btmUtxo)
83 OtherUtxo := mockUTXO(controlProg, &asset.AssetID)
84 utxos = append(utxos, OtherUtxo)
85 _, txData, err := mockTxData(utxos, testAccount)
89 testTx := types.NewTx(*txData)
90 w.saveUnconfirmedTx(testTx)
92 txs := AnnotatedTxs([]*types.Tx{testTx}, w)
94 gotTx, err := w.GetUnconfirmedTxByTxID(testTx.ID.String())
95 if !testutil.DeepEqual(gotTx.ID, wantTx.ID) {
96 t.Errorf(`transaction got=%#v; want=%#v`, gotTx.ID, wantTx.ID)
99 wantTxs := AnnotatedTxs([]*types.Tx{testTx}, w)
100 gotTxs, err := w.GetUnconfirmedTxs("")
101 for i, want := range wantTxs {
102 if !testutil.DeepEqual(gotTxs[i].ID, want.ID) {
103 t.Errorf(`the NO %d transaction, tx got=%#v; want=%#v`, i, gotTxs[i].ID.String(), want.ID.String())
106 for j, input := range want.Inputs {
107 if !testutil.DeepEqual(gotTxs[i].Inputs[j].AccountID, input.AccountID) {
108 t.Errorf(`the NO %d transaction input, accountID got=%#v; want=%#v`, j, gotTxs[i].Inputs[j].AccountID, input.AccountID)
111 if !testutil.DeepEqual(gotTxs[i].Inputs[j].AssetID, input.AssetID) {
112 t.Errorf(`the NO %d transaction input, assetID got=%#v; want=%#v`, j, gotTxs[i].Inputs[j].AssetID, input.AssetID)
116 for k, output := range want.Outputs {
117 if !testutil.DeepEqual(gotTxs[i].Outputs[k].AccountID, output.AccountID) {
118 t.Errorf(`the NO %d transaction input, accountID got=%#v; want=%#v`, k, gotTxs[i].Inputs[k].AccountID, output.AccountID)
121 if !testutil.DeepEqual(gotTxs[i].Outputs[k].AssetID, output.AssetID) {
122 t.Errorf(`the NO %d transaction input, assetID got=%#v; want=%#v`, k, gotTxs[i].Inputs[k].AssetID, output.AssetID)
128 func AnnotatedTxs(txs []*types.Tx, w *Wallet) []*query.AnnotatedTx {
129 // annotate account and asset
130 annotatedTxs := []*query.AnnotatedTx{}
131 for _, tx := range txs {
132 annotatedTx := &query.AnnotatedTx{
134 Inputs: make([]*query.AnnotatedInput, 0, len(tx.Inputs)),
135 Outputs: make([]*query.AnnotatedOutput, 0, len(tx.Outputs)),
136 Size: tx.SerializedSize,
139 for i := range tx.Inputs {
140 annotatedTx.Inputs = append(annotatedTx.Inputs, w.BuildAnnotatedInput(tx, uint32(i)))
142 for i := range tx.Outputs {
143 annotatedTx.Outputs = append(annotatedTx.Outputs, w.BuildAnnotatedOutput(tx, i))
145 annotatedTxs = append(annotatedTxs, annotatedTx)
148 annotateTxsAccount(annotatedTxs, w.DB)
149 annotateTxsAsset(w, annotatedTxs)