8 "github.com/vapor/account"
9 "github.com/vapor/asset"
10 "github.com/vapor/blockchain/pseudohsm"
11 "github.com/vapor/blockchain/query"
12 "github.com/vapor/blockchain/signers"
13 "github.com/vapor/consensus"
14 "github.com/vapor/crypto/ed25519/chainkd"
15 "github.com/vapor/database"
16 "github.com/vapor/event"
17 "github.com/vapor/protocol/bc"
18 "github.com/vapor/protocol/bc/types"
19 "github.com/vapor/testutil"
22 func TestWalletUnconfirmedTxs(t *testing.T) {
23 dirPath, err := ioutil.TempDir(".", "")
27 defer os.RemoveAll(dirPath)
29 testDB := database.NewDB("testdb", "leveldb", "temp")
30 defer os.RemoveAll("temp")
32 accountManager := account.NewManager(testDB, nil)
33 hsm, err := pseudohsm.New(dirPath)
38 xpub1, _, err := hsm.XCreate("test_pub1", "password", "en")
43 testAccount, err := accountManager.Create([]chainkd.XPub{xpub1.XPub}, 1, "testAccount", signers.BIP0044)
48 controlProg, err := accountManager.CreateAddress(testAccount.ID, false)
53 controlProg.KeyIndex = 1
55 reg := asset.NewRegistry(testDB, nil)
56 asset := bc.AssetID{V0: 5}
58 dispatcher := event.NewDispatcher()
59 w := mockWallet(testDB, accountManager, reg, nil, dispatcher, false)
60 utxos := []*account.UTXO{}
61 btmUtxo := mockUTXO(controlProg, consensus.BTMAssetID)
62 utxos = append(utxos, btmUtxo)
64 OtherUtxo := mockUTXO(controlProg, &asset)
65 utxos = append(utxos, OtherUtxo)
66 _, txData, err := mockTxData(utxos, testAccount)
70 testTx := types.NewTx(*txData)
71 w.saveUnconfirmedTx(testTx)
73 txs := AnnotatedTxs([]*types.Tx{testTx}, w)
75 gotTx, err := w.GetUnconfirmedTxByTxID(testTx.ID.String())
76 if !testutil.DeepEqual(gotTx.ID, wantTx.ID) {
77 t.Errorf(`transaction got=%#v; want=%#v`, gotTx.ID, wantTx.ID)
80 wantTxs := AnnotatedTxs([]*types.Tx{testTx}, w)
81 gotTxs, err := w.GetUnconfirmedTxs("")
82 for i, want := range wantTxs {
83 if !testutil.DeepEqual(gotTxs[i].ID, want.ID) {
84 t.Errorf(`the NO %d transaction, tx got=%#v; want=%#v`, i, gotTxs[i].ID.String(), want.ID.String())
87 for j, input := range want.Inputs {
88 if !testutil.DeepEqual(gotTxs[i].Inputs[j].AccountID, input.AccountID) {
89 t.Errorf(`the NO %d transaction input, accountID got=%#v; want=%#v`, j, gotTxs[i].Inputs[j].AccountID, input.AccountID)
92 if !testutil.DeepEqual(gotTxs[i].Inputs[j].AssetID, input.AssetID) {
93 t.Errorf(`the NO %d transaction input, assetID got=%#v; want=%#v`, j, gotTxs[i].Inputs[j].AssetID, input.AssetID)
97 for k, output := range want.Outputs {
98 if !testutil.DeepEqual(gotTxs[i].Outputs[k].AccountID, output.AccountID) {
99 t.Errorf(`the NO %d transaction input, accountID got=%#v; want=%#v`, k, gotTxs[i].Inputs[k].AccountID, output.AccountID)
102 if !testutil.DeepEqual(gotTxs[i].Outputs[k].AssetID, output.AssetID) {
103 t.Errorf(`the NO %d transaction input, assetID got=%#v; want=%#v`, k, gotTxs[i].Inputs[k].AssetID, output.AssetID)
109 func AnnotatedTxs(txs []*types.Tx, w *Wallet) []*query.AnnotatedTx {
110 // annotate account and asset
111 annotatedTxs := []*query.AnnotatedTx{}
112 for _, tx := range txs {
113 annotatedTx := &query.AnnotatedTx{
115 Inputs: make([]*query.AnnotatedInput, 0, len(tx.Inputs)),
116 Outputs: make([]*query.AnnotatedOutput, 0, len(tx.Outputs)),
117 Size: tx.SerializedSize,
120 for i := range tx.Inputs {
121 annotatedTx.Inputs = append(annotatedTx.Inputs, w.BuildAnnotatedInput(tx, uint32(i)))
123 for i := range tx.Outputs {
124 annotatedTx.Outputs = append(annotatedTx.Outputs, w.BuildAnnotatedOutput(tx, i))
126 annotatedTxs = append(annotatedTxs, annotatedTx)
129 annotateTxsAccount(annotatedTxs, w.DB)
130 annotateTxsAsset(w, annotatedTxs)