OSDN Git Service

optimise
authoroysheng <oysheng@bytom.io>
Tue, 5 Jun 2018 11:31:12 +0000 (19:31 +0800)
committeroysheng <oysheng@bytom.io>
Tue, 5 Jun 2018 11:31:12 +0000 (19:31 +0800)
api/query.go
node/node.go
wallet/indexer.go
wallet/unconfirmed.go

index 9405b48..f7a58dd 100644 (file)
@@ -71,13 +71,19 @@ func (a *API) listBalances(ctx context.Context) Response {
 func (a *API) getTransaction(ctx context.Context, txInfo struct {
        TxID string `json:"tx_id"`
 }) Response {
-       transaction, err := a.wallet.GetTransaction(txInfo.TxID)
+       var annotatedTx *query.AnnotatedTx
+       var err error
+
+       annotatedTx, err = a.wallet.GetTransactionByTxID(txInfo.TxID)
        if err != nil {
-               log.Errorf("getTransaction error: %v", err)
-               return NewErrorResponse(err)
+               // transaction not found in blockchain db, search it from unconfirmed db
+               annotatedTx, err = a.wallet.GetUnconfirmedTxByTxID(txInfo.TxID)
+               if err != nil {
+                       return NewErrorResponse(err)
+               }
        }
 
-       return NewSuccessResponse(transaction)
+       return NewSuccessResponse(annotatedTx)
 }
 
 // POST /list-transactions
@@ -97,11 +103,6 @@ func (a *API) listTransactions(ctx context.Context, filter struct {
                } else {
                        transaction, err = a.wallet.GetTransactionByTxID(filter.ID)
                }
-
-               if err != nil {
-                       log.Errorf("GetTransaction: %v", err)
-                       return NewErrorResponse(err)
-               }
                transactions = []*query.AnnotatedTx{transaction}
        } else {
                if filter.Unconfirmed {
index a751f27..398adc8 100644 (file)
@@ -126,7 +126,7 @@ func NewNode(config *cfg.Config) *Node {
        syncManager, _ := netsync.NewSyncManager(config, chain, txPool, newBlockCh)
 
        // get transaction from txPool and send it to syncManager and wallet
-       go syncTxPoolTransaction(txPool, syncManager, wallet)
+       go newPoolTxListener(txPool, syncManager, wallet)
 
        // run the profile server
        profileHost := config.ProfListenAddress
@@ -157,19 +157,16 @@ func NewNode(config *cfg.Config) *Node {
        return node
 }
 
-// syncTxPoolTransaction sync transaction from txPool, and send it to syncManager and wallet
-func syncTxPoolTransaction(txPool *protocol.TxPool, syncManager *netsync.SyncManager, wallet *w.Wallet) {
+// newPoolTxListener listener transaction from txPool, and send it to syncManager and wallet
+func newPoolTxListener(txPool *protocol.TxPool, syncManager *netsync.SyncManager, wallet *w.Wallet) {
        newTxCh := txPool.GetNewTxCh()
+       syncManagerTxCh := syncManager.GetNewTxCh()
+       walletTxCh := wallet.GetNewTxCh()
        for {
-               select {
-               case newTx := <-newTxCh:
-                       txCh := syncManager.GetNewTxCh()
-                       txCh <- newTx
-                       if wallet != nil {
-                               txCh := wallet.GetNewTxCh()
-                               txCh <- newTx
-                       }
-               default:
+               newTx := <-newTxCh
+               syncManagerTxCh <- newTx
+               if wallet != nil {
+                       walletTxCh <- newTx
                }
        }
 }
index 0c2d10e..bbef489 100644 (file)
@@ -20,11 +20,6 @@ import (
        "github.com/bytom/protocol/bc/types"
 )
 
-var (
-       // ErrNotFoundTx means errors occurred in actions
-       ErrNotFoundTx = errors.New("not found transaction in the wallet db")
-)
-
 type rawOutput struct {
        OutputID bc.Hash
        bc.AssetAmount
@@ -194,7 +189,6 @@ func (w *Wallet) indexTransactions(batch db.Batch, b *types.Block, txStatus *bc.
        annotatedTxs := w.filterAccountTxs(b, txStatus)
        saveExternalAssetDefinition(b, w.DB)
        annotateTxsAccount(annotatedTxs, w.DB)
-       annotateTxsAsset(w, annotatedTxs)
 
        for _, tx := range annotatedTxs {
                rawTx, err := json.Marshal(tx)
@@ -415,29 +409,11 @@ transactionLoop:
        return annotatedTxs
 }
 
-// GetTransaction search confirmed or unconfirmed transaction by txID
-func (w *Wallet) GetTransaction(txID string) (*query.AnnotatedTx, error) {
-       annotatedTx, err := w.GetTransactionByTxID(txID)
-       if errors.Root(err) == ErrNotFoundTx {
-               // transaction not found in blockchain db, search it from unconfirmed db
-               annotatedTx, err = w.GetUnconfirmedTxByTxID(txID)
-               if err != nil {
-                       return nil, err
-               }
-               return annotatedTx, nil
-       }
-
-       if err != nil {
-               return nil, err
-       }
-       return annotatedTx, nil
-}
-
 // GetTransactionByTxID get transaction by txID
 func (w *Wallet) GetTransactionByTxID(txID string) (*query.AnnotatedTx, error) {
        formatKey := w.DB.Get(calcTxIndexKey(txID))
        if formatKey == nil {
-               return nil, errors.WithData(ErrNotFoundTx, "not found tx=%s from blockchain", txID)
+               return nil, fmt.Errorf("No transaction(tx_id=%s) ", txID)
        }
 
        annotatedTx := &query.AnnotatedTx{}
@@ -513,12 +489,8 @@ func (w *Wallet) GetTransactions(accountID string) ([]*query.AnnotatedTx, error)
                        return nil, err
                }
 
-               if accountID == "" {
-                       annotatedTxs = append(annotatedTxs, annotatedTx)
-                       continue
-               }
-
-               if findTransactionsByAccount(annotatedTx, accountID) {
+               if accountID == "" || findTransactionsByAccount(annotatedTx, accountID) {
+                       annotateTxsAsset(w, annotatedTxs)
                        annotatedTxs = append(annotatedTxs, annotatedTx)
                }
        }
index 1aba2e8..ef214e4 100644 (file)
@@ -2,9 +2,9 @@ package wallet
 
 import (
        "encoding/json"
+       "fmt"
 
        "github.com/bytom/blockchain/query"
-       "github.com/bytom/errors"
        "github.com/bytom/protocol/bc/types"
 )
 
@@ -37,7 +37,6 @@ func (w *Wallet) SaveUnconfirmedTx(tx *types.Tx) error {
        annotatedTxs := []*query.AnnotatedTx{}
        annotatedTxs = append(annotatedTxs, annotatedTx)
        annotateTxsAccount(annotatedTxs, w.DB)
-       annotateTxsAsset(w, annotatedTxs)
 
        rawTx, err := json.Marshal(annotatedTxs[0])
        if err != nil {
@@ -53,7 +52,7 @@ func (w *Wallet) GetUnconfirmedTxByTxID(txID string) (*query.AnnotatedTx, error)
        annotatedTx := &query.AnnotatedTx{}
        txInfo := w.DB.Get(calcUnconfirmedTxKey(txID))
        if txInfo == nil {
-               return nil, errors.WithData(ErrNotFoundTx, "not found tx=%s from txpool", txID)
+               return nil, fmt.Errorf("No transaction(tx_id=%s) from txpool", txID)
        }
 
        if err := json.Unmarshal(txInfo, annotatedTx); err != nil {
@@ -75,12 +74,8 @@ func (w *Wallet) GetUnconfirmedTxs(accountID string) ([]*query.AnnotatedTx, erro
                        return nil, err
                }
 
-               if accountID == "" {
-                       annotatedTxs = append(annotatedTxs, annotatedTx)
-                       continue
-               }
-
-               if findTransactionsByAccount(annotatedTx, accountID) {
+               if accountID == "" || findTransactionsByAccount(annotatedTx, accountID) {
+                       annotateTxsAsset(w, annotatedTxs)
                        annotatedTxs = append(annotatedTxs, annotatedTx)
                }
        }