From: oysheng Date: Mon, 4 Jun 2018 02:28:29 +0000 (+0800) Subject: optimise unconfirmed transaction API X-Git-Tag: v1.0.5~11^2~11^2~11 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=45d0f18bc43c15636754d322839d277d610cf2c6;p=bytom%2Fbytom.git optimise unconfirmed transaction API --- diff --git a/api/query.go b/api/query.go index e28e5c55..af067029 100644 --- a/api/query.go +++ b/api/query.go @@ -127,7 +127,7 @@ func (a *API) listUnconfirmedTxs(ctx context.Context, filter struct { transactions := []*query.AnnotatedTx{} var err error - transactions, err = a.wallet.GetUnconfirmedTxsByAccountID(filter.AccountID) + transactions, err = a.wallet.GetUnconfirmedTxs(filter.AccountID) if err != nil { log.Errorf("listTransactions: %v", err) return NewErrorResponse(err) diff --git a/cmd/bytomcli/commands/bytomcli.go b/cmd/bytomcli/commands/bytomcli.go index 9f45d877..f7efc4f3 100644 --- a/cmd/bytomcli/commands/bytomcli.go +++ b/cmd/bytomcli/commands/bytomcli.go @@ -198,6 +198,10 @@ func AddTemplateFunc() { getTransactionCmd.Name(), listTransactionsCmd.Name(), + + getUnconfirmedTransactionCmd.Name(), + listUnconfirmedTransactionsCmd.Name(), + listUnspentOutputsCmd.Name(), listBalancesCmd.Name(), } diff --git a/cmd/bytomcli/commands/transaction.go b/cmd/bytomcli/commands/transaction.go index 7ac034b6..abbbd01c 100644 --- a/cmd/bytomcli/commands/transaction.go +++ b/cmd/bytomcli/commands/transaction.go @@ -377,7 +377,7 @@ var listUnconfirmedTransactionsCmd = &cobra.Command{ os.Exit(exitCode) } - printJSON(data) + printJSONList(data) }, } diff --git a/netsync/block_keeper.go b/netsync/block_keeper.go index dbb938ab..6ea62d63 100644 --- a/netsync/block_keeper.go +++ b/netsync/block_keeper.go @@ -22,8 +22,7 @@ const ( maxtxsPending = 32768 maxQuitReq = 256 - // txChanSize is the size of channel listening to Txpool newTxCh. - maxTxChanSize = 1000 + maxTxChanSize = 1000 // txChanSize is the size of channel listening to Txpool newTxCh ) var ( diff --git a/wallet/unconfirmed.go b/wallet/unconfirmed.go index cc68422b..6e261dbd 100644 --- a/wallet/unconfirmed.go +++ b/wallet/unconfirmed.go @@ -103,9 +103,10 @@ func (w *Wallet) GetUnconfirmedTxByTxID(txID string) (*query.AnnotatedTx, error) return annotatedTx, nil } -// GetUnconfirmedTxsByAccountID get account unconfirmed txs by account ID -func (w *Wallet) GetUnconfirmedTxsByAccountID(accountID string) ([]*query.AnnotatedTx, error) { +// GetUnconfirmedTxs get account unconfirmed transactions, filter transactions by accountID when accountID is not empty +func (w *Wallet) GetUnconfirmedTxs(accountID string) ([]*query.AnnotatedTx, error) { annotatedTxs := []*query.AnnotatedTx{} + annotatedAccTxs := []*query.AnnotatedTx{} txIter := w.DB.IteratorPrefix([]byte(unconfirmedTxPrefix)) defer txIter.Release() @@ -115,15 +116,14 @@ func (w *Wallet) GetUnconfirmedTxsByAccountID(accountID string) ([]*query.Annota return nil, err } - if accountID == "" { - annotatedTxs = append(annotatedTxs, annotatedTx) - continue - } - - if findTransactionsByAccount(annotatedTx, accountID) { - annotatedTxs = append(annotatedTxs, annotatedTx) + annotatedTxs = append(annotatedTxs, annotatedTx) + if accountID != "" && findTransactionsByAccount(annotatedTx, accountID) { + annotatedAccTxs = append(annotatedAccTxs, annotatedTx) } } + if accountID != "" { + return annotatedAccTxs, nil + } return annotatedTxs, nil } diff --git a/wallet/wallet.go b/wallet/wallet.go index e3f87afc..16236fcc 100644 --- a/wallet/wallet.go +++ b/wallet/wallet.go @@ -18,8 +18,7 @@ const ( //SINGLE single sign SINGLE = 1 - // txChanSize is the size of channel listening to Txpool newTxCh. - maxTxChanSize = 1000 + maxTxChanSize = 1000 // txChanSize is the size of channel listening to Txpool newTxCh ) var walletKey = []byte("walletInfo") @@ -187,10 +186,15 @@ func (w *Wallet) getRescanNotification() { w.status.WorkHash = bc.Hash{} w.AttachBlock(block) default: - //return + return } } +//SetTxCh set wallet txCh +func (w *Wallet) SetTxCh(txCh *types.Tx) { + w.txCh <- txCh +} + func (w *Wallet) walletTxPoolUpdater() { for { // rescan txpool transaction and delete unconfirmed transactions from database @@ -216,8 +220,3 @@ func (w *Wallet) createProgram(account *account.Account, XPub *pseudohsm.XPub, i } return nil } - -//SetTxCh set wallet txCh -func (w *Wallet) SetTxCh(txCh *types.Tx) { - w.txCh <- txCh -}