From 3ff1a86ab1b0d9b8c4234688aefd40a2dce61062 Mon Sep 17 00:00:00 2001 From: Martin Boehm Date: Mon, 17 Dec 2018 13:20:10 +0100 Subject: [PATCH] Add option TxHistoryLight to api.GetAddress --- api/types.go | 2 ++ api/worker.go | 22 ++++++++++++---------- server/public.go | 2 +- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/api/types.go b/api/types.go index c753d986..2bcdc079 100644 --- a/api/types.go +++ b/api/types.go @@ -19,6 +19,8 @@ const ( Balance // TxidHistory - balances and txids, subject to paging TxidHistory + // TxHistoryLight - balances and easily obtained tx data (not requiring request to backend), subject to paging + TxHistoryLight // TxHistory - balances and full tx data, subject to paging TxHistory ) diff --git a/api/worker.go b/api/worker.go index b7019b98..7b63b315 100644 --- a/api/worker.go +++ b/api/worker.go @@ -564,7 +564,7 @@ func (w *Worker) GetAddress(address string, page int, txsOnPage int, option GetA } } // get tx history if requested by option or check mempool if there are some transactions for a new address - if option == TxidHistory || option == TxHistory || ba == nil { + if option >= TxidHistory || ba == nil { // convert the address to the format defined by the parser addresses, _, err := w.chainParser.GetAddressesFromAddrDesc(addrDesc) if err != nil { @@ -583,7 +583,7 @@ func (w *Worker) GetAddress(address string, page int, txsOnPage int, option GetA ba = &db.AddrBalance{} page = 0 } - if option == TxidHistory || option == TxHistory { + if option >= TxidHistory { txc, err := w.getAddressTxids(addrDesc, false, filter) if err != nil { return nil, errors.Annotatef(err, "getAddressTxids %v false", addrDesc) @@ -601,7 +601,7 @@ func (w *Worker) GetAddress(address string, page int, txsOnPage int, option GetA txs = make([]*Tx, len(txm)+to-from) } txi := 0 - // load mempool transactions + // get mempool transactions for _, txid := range txm { tx, err := w.GetTransaction(txid, false, false) // mempool transaction may fail @@ -620,17 +620,14 @@ func (w *Worker) GetAddress(address string, page int, txsOnPage int, option GetA } } } + // get confirmed transactions for i := from; i < to; i++ { txid := txc[i] if option == TxidHistory { txids[txi] = txid } else { - if w.chainType == bchain.ChainEthereumType { - txs[txi], err = w.GetTransaction(txid, false, true) - if err != nil { - return nil, errors.Annotatef(err, "GetTransaction %v", txid) - } - } else { + // only ChainBitcoinType supports TxHistoryLight + if option == TxHistoryLight && w.chainType == bchain.ChainBitcoinType { ta, err := w.db.GetTxAddresses(txid) if err != nil { return nil, errors.Annotatef(err, "GetTxAddresses %v", txid) @@ -648,13 +645,18 @@ func (w *Worker) GetAddress(address string, page int, txsOnPage int, option GetA continue } txs[txi] = w.txFromTxAddress(txid, ta, bi, bestheight) + } else { + txs[txi], err = w.GetTransaction(txid, false, true) + if err != nil { + return nil, errors.Annotatef(err, "GetTransaction %v", txid) + } } } txi++ } if option == TxidHistory { txids = txids[:txi] - } else if option == TxHistory { + } else if option >= TxHistoryLight { txs = txs[:txi] } } diff --git a/server/public.go b/server/public.go index 9da61b73..9302eb54 100644 --- a/server/public.go +++ b/server/public.go @@ -520,7 +520,7 @@ func (s *PublicServer) explorerAddress(w http.ResponseWriter, r *http.Request) ( } } } - address, err = s.api.GetAddress(r.URL.Path[i+1:], page, txsOnPage, api.TxHistory, &api.AddressFilter{Vout: fn}) + address, err = s.api.GetAddress(r.URL.Path[i+1:], page, txsOnPage, api.TxHistoryLight, &api.AddressFilter{Vout: fn}) if err != nil { return errorTpl, nil, err }