8 "github.com/bytom/vapor/account"
9 "github.com/bytom/vapor/asset"
10 "github.com/bytom/vapor/blockchain/pseudohsm"
11 "github.com/bytom/vapor/blockchain/query"
12 "github.com/bytom/vapor/blockchain/signers"
13 "github.com/bytom/vapor/consensus"
14 "github.com/bytom/vapor/crypto/ed25519/chainkd"
15 dbm "github.com/bytom/vapor/database/leveldb"
16 "github.com/bytom/vapor/event"
17 "github.com/bytom/vapor/protocol/bc"
18 "github.com/bytom/vapor/protocol/bc/types"
19 "github.com/bytom/vapor/testutil"
22 func TestWalletUnconfirmedTxs(t *testing.T) {
23 dirPath, err := ioutil.TempDir(".", "")
27 defer os.RemoveAll(dirPath)
29 testDB := dbm.NewDB("testdb", "leveldb", "temp")
30 testStore := NewMockWalletStore(testDB)
31 defer os.RemoveAll("temp")
33 accountStore := NewMockAccountStore(testDB)
34 accountManager := account.NewManager(accountStore, nil)
35 hsm, err := pseudohsm.New(dirPath)
40 xpub1, _, err := hsm.XCreate("test_pub1", "password", "en")
45 testAccount, err := accountManager.Create([]chainkd.XPub{xpub1.XPub}, 1, "testAccount", signers.BIP0044)
50 controlProg, err := accountManager.CreateAddress(testAccount.ID, false)
55 controlProg.KeyIndex = 1
57 reg := asset.NewRegistry(testDB, nil)
58 asset := bc.AssetID{V0: 5}
60 dispatcher := event.NewDispatcher()
61 w := mockWallet(testStore, accountManager, reg, nil, dispatcher, false)
62 utxos := []*account.UTXO{}
63 btmUtxo := mockUTXO(controlProg, consensus.BTMAssetID)
64 utxos = append(utxos, btmUtxo)
66 OtherUtxo := mockUTXO(controlProg, &asset)
67 utxos = append(utxos, OtherUtxo)
68 _, txData, err := mockTxData(utxos, testAccount)
72 testTx := types.NewTx(*txData)
73 w.saveUnconfirmedTx(testTx)
75 txs := AnnotatedTxs([]*types.Tx{testTx}, w)
77 gotTx, err := w.GetUnconfirmedTxByTxID(testTx.ID.String())
78 if !testutil.DeepEqual(gotTx.ID, wantTx.ID) {
79 t.Errorf(`transaction got=%#v; want=%#v`, gotTx.ID, wantTx.ID)
82 wantTxs := AnnotatedTxs([]*types.Tx{testTx}, w)
83 gotTxs, err := w.GetUnconfirmedTxs("")
84 for i, want := range wantTxs {
85 if !testutil.DeepEqual(gotTxs[i].ID, want.ID) {
86 t.Errorf(`the NO %d transaction, tx got=%#v; want=%#v`, i, gotTxs[i].ID.String(), want.ID.String())
89 for j, input := range want.Inputs {
90 if !testutil.DeepEqual(gotTxs[i].Inputs[j].AccountID, input.AccountID) {
91 t.Errorf(`the NO %d transaction input, accountID got=%#v; want=%#v`, j, gotTxs[i].Inputs[j].AccountID, input.AccountID)
94 if !testutil.DeepEqual(gotTxs[i].Inputs[j].AssetID, input.AssetID) {
95 t.Errorf(`the NO %d transaction input, assetID got=%#v; want=%#v`, j, gotTxs[i].Inputs[j].AssetID, input.AssetID)
99 for k, output := range want.Outputs {
100 if !testutil.DeepEqual(gotTxs[i].Outputs[k].AccountID, output.AccountID) {
101 t.Errorf(`the NO %d transaction input, accountID got=%#v; want=%#v`, k, gotTxs[i].Inputs[k].AccountID, output.AccountID)
104 if !testutil.DeepEqual(gotTxs[i].Outputs[k].AssetID, output.AssetID) {
105 t.Errorf(`the NO %d transaction input, assetID got=%#v; want=%#v`, k, gotTxs[i].Inputs[k].AssetID, output.AssetID)
111 func AnnotatedTxs(txs []*types.Tx, w *Wallet) []*query.AnnotatedTx {
112 // annotate account and asset
113 annotatedTxs := []*query.AnnotatedTx{}
114 for _, tx := range txs {
115 annotatedTx := &query.AnnotatedTx{
117 Inputs: make([]*query.AnnotatedInput, 0, len(tx.Inputs)),
118 Outputs: make([]*query.AnnotatedOutput, 0, len(tx.Outputs)),
119 Size: tx.SerializedSize,
122 for i := range tx.Inputs {
123 annotatedTx.Inputs = append(annotatedTx.Inputs, w.BuildAnnotatedInput(tx, uint32(i)))
125 for i := range tx.Outputs {
126 annotatedTx.Outputs = append(annotatedTx.Outputs, w.BuildAnnotatedOutput(tx, i))
128 annotatedTxs = append(annotatedTxs, annotatedTx)
131 w.annotateTxsAccount(annotatedTxs)
132 annotateTxsAsset(w, annotatedTxs)