OSDN Git Service

optimise unconfirmed transaction API
authoroysheng <oysheng@bytom.io>
Mon, 4 Jun 2018 02:28:29 +0000 (10:28 +0800)
committeroysheng <oysheng@bytom.io>
Mon, 4 Jun 2018 02:40:56 +0000 (10:40 +0800)
api/query.go
cmd/bytomcli/commands/bytomcli.go
cmd/bytomcli/commands/transaction.go
netsync/block_keeper.go
wallet/unconfirmed.go
wallet/wallet.go

index e28e5c5..af06702 100644 (file)
@@ -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)
index 9f45d87..f7efc4f 100644 (file)
@@ -198,6 +198,10 @@ func AddTemplateFunc() {
 
                getTransactionCmd.Name(),
                listTransactionsCmd.Name(),
+
+               getUnconfirmedTransactionCmd.Name(),
+               listUnconfirmedTransactionsCmd.Name(),
+
                listUnspentOutputsCmd.Name(),
                listBalancesCmd.Name(),
        }
index 7ac034b..abbbd01 100644 (file)
@@ -377,7 +377,7 @@ var listUnconfirmedTransactionsCmd = &cobra.Command{
                        os.Exit(exitCode)
                }
 
-               printJSON(data)
+               printJSONList(data)
        },
 }
 
index dbb938a..6ea62d6 100644 (file)
@@ -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 (
index cc68422..6e261db 100644 (file)
@@ -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
 }
index e3f87af..16236fc 100644 (file)
@@ -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
-}