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 dbm "github.com/vapor/database/leveldb"
17 "github.com/vapor/event"
18 "github.com/vapor/protocol/bc"
19 "github.com/vapor/protocol/bc/types"
20 "github.com/vapor/testutil"
23 func TestWalletUnconfirmedTxs(t *testing.T) {
24 dirPath, err := ioutil.TempDir(".", "")
28 defer os.RemoveAll(dirPath)
30 testDB := dbm.NewDB("testdb", "leveldb", "temp")
31 testStore := database.NewWalletStore(testDB)
32 defer os.RemoveAll("temp")
34 accountStore := database.NewAccountStore(testDB)
35 accountManager := account.NewManager(accountStore, nil)
36 hsm, err := pseudohsm.New(dirPath)
41 xpub1, _, err := hsm.XCreate("test_pub1", "password", "en")
46 testAccount, err := accountManager.Create([]chainkd.XPub{xpub1.XPub}, 1, "testAccount", signers.BIP0044)
51 controlProg, err := accountManager.CreateAddress(testAccount.ID, false)
56 controlProg.KeyIndex = 1
58 reg := asset.NewRegistry(testDB, nil)
59 asset := bc.AssetID{V0: 5}
61 dispatcher := event.NewDispatcher()
62 w := mockWallet(testStore, accountManager, reg, nil, dispatcher, false)
63 utxos := []*account.UTXO{}
64 btmUtxo := mockUTXO(controlProg, consensus.BTMAssetID)
65 utxos = append(utxos, btmUtxo)
67 OtherUtxo := mockUTXO(controlProg, &asset)
68 utxos = append(utxos, OtherUtxo)
69 _, txData, err := mockTxData(utxos, testAccount)
73 testTx := types.NewTx(*txData)
74 w.saveUnconfirmedTx(testTx)
76 txs := AnnotatedTxs([]*types.Tx{testTx}, w)
78 gotTx, err := w.GetUnconfirmedTxByTxID(testTx.ID.String())
79 if !testutil.DeepEqual(gotTx.ID, wantTx.ID) {
80 t.Errorf(`transaction got=%#v; want=%#v`, gotTx.ID, wantTx.ID)
83 wantTxs := AnnotatedTxs([]*types.Tx{testTx}, w)
84 gotTxs, err := w.GetUnconfirmedTxs("")
85 for i, want := range wantTxs {
86 if !testutil.DeepEqual(gotTxs[i].ID, want.ID) {
87 t.Errorf(`the NO %d transaction, tx got=%#v; want=%#v`, i, gotTxs[i].ID.String(), want.ID.String())
90 for j, input := range want.Inputs {
91 if !testutil.DeepEqual(gotTxs[i].Inputs[j].AccountID, input.AccountID) {
92 t.Errorf(`the NO %d transaction input, accountID got=%#v; want=%#v`, j, gotTxs[i].Inputs[j].AccountID, input.AccountID)
95 if !testutil.DeepEqual(gotTxs[i].Inputs[j].AssetID, input.AssetID) {
96 t.Errorf(`the NO %d transaction input, assetID got=%#v; want=%#v`, j, gotTxs[i].Inputs[j].AssetID, input.AssetID)
100 for k, output := range want.Outputs {
101 if !testutil.DeepEqual(gotTxs[i].Outputs[k].AccountID, output.AccountID) {
102 t.Errorf(`the NO %d transaction input, accountID got=%#v; want=%#v`, k, gotTxs[i].Inputs[k].AccountID, output.AccountID)
105 if !testutil.DeepEqual(gotTxs[i].Outputs[k].AssetID, output.AssetID) {
106 t.Errorf(`the NO %d transaction input, assetID got=%#v; want=%#v`, k, gotTxs[i].Inputs[k].AssetID, output.AssetID)
112 func AnnotatedTxs(txs []*types.Tx, w *Wallet) []*query.AnnotatedTx {
113 // annotate account and asset
114 annotatedTxs := []*query.AnnotatedTx{}
115 for _, tx := range txs {
116 annotatedTx := &query.AnnotatedTx{
118 Inputs: make([]*query.AnnotatedInput, 0, len(tx.Inputs)),
119 Outputs: make([]*query.AnnotatedOutput, 0, len(tx.Outputs)),
120 Size: tx.SerializedSize,
123 for i := range tx.Inputs {
124 annotatedTx.Inputs = append(annotatedTx.Inputs, w.BuildAnnotatedInput(tx, uint32(i)))
126 for i := range tx.Outputs {
127 annotatedTx.Outputs = append(annotatedTx.Outputs, w.BuildAnnotatedOutput(tx, i))
129 annotatedTxs = append(annotatedTxs, annotatedTx)
132 annotateTxsAccount(annotatedTxs, w.store)
133 annotateTxsAsset(w, annotatedTxs)