github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/wallet/unconfirmed_test.go (about)

     1  package wallet
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/bytom/bytom/account"
     9  	"github.com/bytom/bytom/asset"
    10  	"github.com/bytom/bytom/blockchain/pseudohsm"
    11  	"github.com/bytom/bytom/blockchain/query"
    12  	"github.com/bytom/bytom/blockchain/signers"
    13  	"github.com/bytom/bytom/consensus"
    14  	"github.com/bytom/bytom/crypto/ed25519/chainkd"
    15  	dbm "github.com/bytom/bytom/database/leveldb"
    16  	"github.com/bytom/bytom/event"
    17  	"github.com/bytom/bytom/protocol/bc/types"
    18  	"github.com/bytom/bytom/testutil"
    19  )
    20  
    21  func TestWalletUnconfirmedTxs(t *testing.T) {
    22  	dirPath, err := ioutil.TempDir(".", "")
    23  	if err != nil {
    24  		t.Fatal(err)
    25  	}
    26  	defer os.RemoveAll(dirPath)
    27  
    28  	testDB := dbm.NewDB("testdb", "leveldb", "temp")
    29  	defer os.RemoveAll("temp")
    30  
    31  	accountManager := account.NewManager(testDB, nil)
    32  	hsm, err := pseudohsm.New(dirPath)
    33  	if err != nil {
    34  		t.Fatal(err)
    35  	}
    36  
    37  	xpub1, _, err := hsm.XCreate("test_pub1", "password", "en")
    38  	if err != nil {
    39  		t.Fatal(err)
    40  	}
    41  
    42  	testAccount, err := accountManager.Create([]chainkd.XPub{xpub1.XPub}, 1, "testAccount", signers.BIP0044)
    43  	if err != nil {
    44  		t.Fatal(err)
    45  	}
    46  
    47  	controlProg, err := accountManager.CreateAddress(testAccount.ID, false)
    48  	if err != nil {
    49  		t.Fatal(err)
    50  	}
    51  
    52  	controlProg.KeyIndex = 1
    53  
    54  	reg := asset.NewRegistry(testDB, nil)
    55  	asset, err := reg.Define([]chainkd.XPub{xpub1.XPub}, 1, nil, 0, "TESTASSET", nil)
    56  	if err != nil {
    57  		t.Fatal(err)
    58  	}
    59  
    60  	dispatcher := event.NewDispatcher()
    61  	w := mockWallet(testDB, accountManager, reg, nil, dispatcher, false)
    62  	utxos := []*account.UTXO{}
    63  	btmUtxo := mockUTXO(controlProg, consensus.BTMAssetID)
    64  	utxos = append(utxos, btmUtxo)
    65  
    66  	OtherUtxo := mockUTXO(controlProg, &asset.AssetID)
    67  	utxos = append(utxos, OtherUtxo)
    68  	_, txData, err := mockTxData(utxos, testAccount)
    69  	if err != nil {
    70  		t.Fatal(err)
    71  	}
    72  	testTx := types.NewTx(*txData)
    73  	w.saveUnconfirmedTx(testTx)
    74  
    75  	txs := AnnotatedTxs([]*types.Tx{testTx}, w)
    76  	wantTx := txs[0]
    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)
    80  	}
    81  
    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())
    87  		}
    88  
    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)
    92  			}
    93  
    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)
    96  			}
    97  		}
    98  
    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)
   102  			}
   103  
   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)
   106  			}
   107  		}
   108  	}
   109  }
   110  
   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{
   116  			ID:      tx.ID,
   117  			Inputs:  make([]*query.AnnotatedInput, 0, len(tx.Inputs)),
   118  			Outputs: make([]*query.AnnotatedOutput, 0, len(tx.Outputs)),
   119  			Size:    tx.SerializedSize,
   120  		}
   121  
   122  		for i := range tx.Inputs {
   123  			annotatedTx.Inputs = append(annotatedTx.Inputs, w.BuildAnnotatedInput(tx, uint32(i)))
   124  		}
   125  		for i := range tx.Outputs {
   126  			annotatedTx.Outputs = append(annotatedTx.Outputs, w.BuildAnnotatedOutput(tx, i))
   127  		}
   128  		annotatedTxs = append(annotatedTxs, annotatedTx)
   129  	}
   130  
   131  	annotateTxsAccount(annotatedTxs, w.DB)
   132  	annotateTxsAsset(w, annotatedTxs)
   133  
   134  	return annotatedTxs
   135  }