8 dbm "github.com/tendermint/tmlibs/db"
9 "github.com/vapor/blockchain/signers"
11 "github.com/vapor/account"
12 "github.com/vapor/asset"
13 "github.com/vapor/blockchain/pseudohsm"
14 "github.com/vapor/blockchain/query"
15 "github.com/vapor/consensus"
16 "github.com/vapor/crypto/ed25519/chainkd"
17 "github.com/vapor/protocol/bc/types"
18 "github.com/vapor/testutil"
21 func TestWalletUnconfirmedTxs(t *testing.T) {
22 dirPath, err := ioutil.TempDir(".", "")
26 defer os.RemoveAll(dirPath)
28 testDB := dbm.NewDB("testdb", "leveldb", "temp")
29 defer os.RemoveAll("temp")
31 accountManager := account.NewManager(testDB, nil)
32 hsm, err := pseudohsm.New(dirPath)
37 xpub1, _, err := hsm.XCreate("test_pub1", "password", "en")
42 testAccount, err := accountManager.Create([]chainkd.XPub{xpub1.XPub}, 1, "testAccount", signers.BIP0044)
47 controlProg, err := accountManager.CreateAddress(testAccount.ID, false)
52 controlProg.KeyIndex = 1
54 reg := asset.NewRegistry(testDB, nil)
55 asset, err := reg.Define([]chainkd.XPub{xpub1.XPub}, 1, nil, "TESTASSET", nil)
60 w := mockWallet(testDB, accountManager, reg, nil)
61 utxos := []*account.UTXO{}
62 btmUtxo := mockUTXO(controlProg, consensus.BTMAssetID)
63 utxos = append(utxos, btmUtxo)
65 OtherUtxo := mockUTXO(controlProg, &asset.AssetID)
66 utxos = append(utxos, OtherUtxo)
67 _, txData, err := mockTxData(utxos, testAccount)
71 testTx := types.NewTx(*txData)
72 w.saveUnconfirmedTx(testTx)
74 txs := AnnotatedTxs([]*types.Tx{testTx}, w)
76 gotTx, err := w.GetUnconfirmedTxByTxID(testTx.ID.String())
77 if !testutil.DeepEqual(gotTx.ID, wantTx.ID) {
78 t.Errorf(`transaction got=%#v; want=%#v`, gotTx.ID, wantTx.ID)
81 wantTxs := AnnotatedTxs([]*types.Tx{testTx}, w)
82 gotTxs, err := w.GetUnconfirmedTxs("")
83 for i, want := range wantTxs {
84 if !testutil.DeepEqual(gotTxs[i].ID, want.ID) {
85 t.Errorf(`the NO %d transaction, tx got=%#v; want=%#v`, i, gotTxs[i].ID.String(), want.ID.String())
88 for j, input := range want.Inputs {
89 if !testutil.DeepEqual(gotTxs[i].Inputs[j].AccountID, input.AccountID) {
90 t.Errorf(`the NO %d transaction input, accountID got=%#v; want=%#v`, j, gotTxs[i].Inputs[j].AccountID, input.AccountID)
93 if !testutil.DeepEqual(gotTxs[i].Inputs[j].AssetID, input.AssetID) {
94 t.Errorf(`the NO %d transaction input, assetID got=%#v; want=%#v`, j, gotTxs[i].Inputs[j].AssetID, input.AssetID)
98 for k, output := range want.Outputs {
99 if !testutil.DeepEqual(gotTxs[i].Outputs[k].AccountID, output.AccountID) {
100 t.Errorf(`the NO %d transaction input, accountID got=%#v; want=%#v`, k, gotTxs[i].Inputs[k].AccountID, output.AccountID)
103 if !testutil.DeepEqual(gotTxs[i].Outputs[k].AssetID, output.AssetID) {
104 t.Errorf(`the NO %d transaction input, assetID got=%#v; want=%#v`, k, gotTxs[i].Inputs[k].AssetID, output.AssetID)
110 func AnnotatedTxs(txs []*types.Tx, w *Wallet) []*query.AnnotatedTx {
111 // annotate account and asset
112 annotatedTxs := []*query.AnnotatedTx{}
113 for _, tx := range txs {
114 annotatedTx := &query.AnnotatedTx{
116 Inputs: make([]*query.AnnotatedInput, 0, len(tx.Inputs)),
117 Outputs: make([]*query.AnnotatedOutput, 0, len(tx.Outputs)),
118 Size: tx.SerializedSize,
121 for i := range tx.Inputs {
122 annotatedTx.Inputs = append(annotatedTx.Inputs, w.BuildAnnotatedInput(tx, uint32(i)))
124 for i := range tx.Outputs {
125 annotatedTx.Outputs = append(annotatedTx.Outputs, w.BuildAnnotatedOutput(tx, i))
127 annotatedTxs = append(annotatedTxs, annotatedTx)
130 annotateTxsAccount(annotatedTxs, w.DB)
131 annotateTxsAsset(w, annotatedTxs)