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
} 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 {
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
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
}
}
}
"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
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)
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{}
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)
}
}
import (
"encoding/json"
+ "fmt"
"github.com/bytom/blockchain/query"
- "github.com/bytom/errors"
"github.com/bytom/protocol/bc/types"
)
annotatedTxs := []*query.AnnotatedTx{}
annotatedTxs = append(annotatedTxs, annotatedTx)
annotateTxsAccount(annotatedTxs, w.DB)
- annotateTxsAsset(w, annotatedTxs)
rawTx, err := json.Marshal(annotatedTxs[0])
if err != nil {
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 {
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)
}
}