8 dbm "github.com/tendermint/tmlibs/db"
11 "github.com/bytom/account"
12 "github.com/bytom/asset"
13 "github.com/bytom/blockchain/pseudohsm"
14 "github.com/bytom/blockchain/query"
15 "github.com/bytom/consensus"
16 "github.com/bytom/crypto/ed25519/chainkd"
17 "github.com/bytom/protocol/bc/types"
20 func TestWalletUnconfirmedTxs(t *testing.T) {
21 dirPath, err := ioutil.TempDir(".", "")
25 defer os.RemoveAll(dirPath)
27 testDB := dbm.NewDB("testdb", "leveldb", "temp")
28 defer os.RemoveAll("temp")
30 accountManager := account.NewManager(testDB, nil)
31 hsm, err := pseudohsm.New(dirPath)
36 xpub1, err := hsm.XCreate("test_pub1", "password")
41 testAccount, err := accountManager.Create(nil, []chainkd.XPub{xpub1.XPub}, 1, "testAccount")
46 controlProg, err := accountManager.CreateAddress(nil, testAccount.ID, false)
51 controlProg.KeyIndex = 1
53 reg := asset.NewRegistry(testDB, nil)
54 asset, err := reg.Define([]chainkd.XPub{xpub1.XPub}, 1, nil, "TESTASSET")
59 w := mockWallet(testDB, accountManager, reg, nil)
60 utxos := []*account.UTXO{}
61 btmUtxo := mockUTXO(controlProg, consensus.BTMAssetID)
62 utxos = append(utxos, btmUtxo)
64 _, txData, err := mockTxData(utxos, testAccount)
68 testTx1 := types.NewTx(*txData)
69 w.SaveUnconfirmedTx(testTx1)
71 OtherUtxo := mockUTXO(controlProg, &asset.AssetID)
72 utxos = append(utxos, OtherUtxo)
73 _, txData, err = mockTxData(utxos, testAccount)
77 testTx2 := types.NewTx(*txData)
78 w.SaveUnconfirmedTx(testTx2)
80 txs := AnnotatedTxs([]*types.Tx{testTx1}, w)
82 gotTx, err := w.GetUnconfirmedTxByTxID(testTx1.ID.String())
83 if !testutil.DeepEqual(gotTx.ID, wantTx.ID) {
84 t.Errorf(`transaction got=%#v; want=%#v`, gotTx.ID, wantTx.ID)
87 wantTxs := AnnotatedTxs([]*types.Tx{testTx1, testTx2}, w)
88 gotTxs, err := w.GetUnconfirmedTxs("")
89 for i, want := range wantTxs {
90 if !testutil.DeepEqual(gotTxs[i].ID, want.ID) {
91 t.Errorf(`the NO %i transaction, tx got=%#v; want=%#v`, i, gotTxs[i].ID.String(), want.ID.String())
94 for j, input := range want.Inputs {
95 if !testutil.DeepEqual(gotTxs[i].Inputs[j].AccountID, input.AccountID) {
96 t.Errorf(`the NO %i transaction input, accountID got=%#v; want=%#v`, j, gotTxs[i].Inputs[j].AccountID, input.AccountID)
99 if !testutil.DeepEqual(gotTxs[i].Inputs[j].AssetID, input.AssetID) {
100 t.Errorf(`the NO %i transaction input, assetID got=%#v; want=%#v`, j, gotTxs[i].Inputs[j].AssetID, input.AssetID)
104 for k, output := range want.Outputs {
105 if !testutil.DeepEqual(gotTxs[i].Outputs[k].AccountID, output.AccountID) {
106 t.Errorf(`the NO %i transaction input, accountID got=%#v; want=%#v`, k, gotTxs[i].Inputs[k].AccountID, output.AccountID)
109 if !testutil.DeepEqual(gotTxs[i].Outputs[k].AssetID, output.AssetID) {
110 t.Errorf(`the NO %i transaction input, assetID got=%#v; want=%#v`, k, gotTxs[i].Inputs[k].AssetID, output.AssetID)
116 func AnnotatedTxs(txs []*types.Tx, w *Wallet) []*query.AnnotatedTx {
117 // annotate account and asset
118 annotatedTxs := []*query.AnnotatedTx{}
119 for _, tx := range txs {
120 annotatedTx := &query.AnnotatedTx{
122 Inputs: make([]*query.AnnotatedInput, 0, len(tx.Inputs)),
123 Outputs: make([]*query.AnnotatedOutput, 0, len(tx.Outputs)),
124 Size: tx.SerializedSize,
127 for i := range tx.Inputs {
128 annotatedTx.Inputs = append(annotatedTx.Inputs, w.BuildAnnotatedInput(tx, uint32(i)))
130 for i := range tx.Outputs {
131 annotatedTx.Outputs = append(annotatedTx.Outputs, w.BuildAnnotatedOutput(tx, i))
133 annotatedTxs = append(annotatedTxs, annotatedTx)
136 annotateTxsAccount(annotatedTxs, w.DB)
137 annotateTxsAsset(w, annotatedTxs)