8 dbm "github.com/tendermint/tmlibs/db"
10 "github.com/bytom/account"
11 "github.com/bytom/asset"
12 "github.com/bytom/blockchain/pseudohsm"
13 "github.com/bytom/blockchain/query"
14 "github.com/bytom/blockchain/signers"
15 "github.com/bytom/consensus"
16 "github.com/bytom/crypto/ed25519/chainkd"
17 "github.com/bytom/event"
18 "github.com/bytom/protocol/bc/types"
19 "github.com/bytom/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 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, err := reg.Define([]chainkd.XPub{xpub1.XPub}, 1, nil, "TESTASSET", nil)
61 dispatcher := event.NewDispatcher()
62 w := mockWallet(testDB, accountManager, reg, nil, dispatcher)
63 utxos := []*account.UTXO{}
64 btmUtxo := mockUTXO(controlProg, consensus.BTMAssetID)
65 utxos = append(utxos, btmUtxo)
67 OtherUtxo := mockUTXO(controlProg, &asset.AssetID)
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.DB)
133 annotateTxsAsset(w, annotatedTxs)