From 63fb910ecb8727c82b0e1a75a206d23bc78420b5 Mon Sep 17 00:00:00 2001 From: Martin Boehm Date: Mon, 11 Feb 2019 11:54:34 +0100 Subject: [PATCH] Add xpub handling to websocket interface --- api/xpub.go | 6 ++++-- server/public.go | 6 +++--- server/websocket.go | 20 +++++++++++++++----- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/api/xpub.go b/api/xpub.go index 6fa86932..4ae2d9d0 100644 --- a/api/xpub.go +++ b/api/xpub.go @@ -112,7 +112,7 @@ func (w *Worker) xpubCheckAndLoadTxids(ad *xpubAddress, filter *AddressFilter, m if ad.balance == nil { return nil } - // if completely read, check if there are not some new txs and load if necessary + // if completely loaded, check if there are not some new txs and load if necessary if ad.complete { if ad.balance.Txs != ad.txs { newTxids, _, err := w.xpubGetAddressTxids(ad.addrDesc, false, ad.maxHeight+1, maxHeight, maxInt) @@ -129,10 +129,12 @@ func (w *Worker) xpubCheckAndLoadTxids(ad *xpubAddress, filter *AddressFilter, m return nil } // unless the filter is completely off, load all txids + // could be optimized to reflect filter.FromHeight, filter.ToHeight but this way it is simple and robust + fromHeight := uint32(0) if filter.FromHeight != 0 || filter.ToHeight != 0 || filter.Vout != AddressFilterVoutOff { maxResults = maxInt } - newTxids, complete, err := w.xpubGetAddressTxids(ad.addrDesc, false, 0, maxHeight, maxResults) + newTxids, complete, err := w.xpubGetAddressTxids(ad.addrDesc, false, fromHeight, maxHeight, maxResults) if err != nil { return err } diff --git a/server/public.go b/server/public.go index e1ffec45..674e9820 100644 --- a/server/public.go +++ b/server/public.go @@ -600,7 +600,7 @@ func (s *PublicServer) explorerAddress(w http.ResponseWriter, r *http.Request) ( return addressTpl, data, nil } -func (s *PublicServer) getAddressForXpub(r *http.Request, xpub string, pageSize int, option api.GetAddressOption) (*api.Address, error) { +func (s *PublicServer) getXpubAddress(r *http.Request, xpub string, pageSize int, option api.GetAddressOption) (*api.Address, error) { var fn = api.AddressFilterVoutOff page, ec := strconv.Atoi(r.URL.Query().Get("page")) if ec != nil { @@ -633,7 +633,7 @@ func (s *PublicServer) explorerXpub(w http.ResponseWriter, r *http.Request) (tpl var err error s.metrics.ExplorerViews.With(common.Labels{"action": "xpub"}).Inc() if i := strings.LastIndexByte(r.URL.Path, '/'); i > 0 { - address, err = s.getAddressForXpub(r, r.URL.Path[i+1:], txsOnPage, api.TxHistoryLight) + address, err = s.getXpubAddress(r, r.URL.Path[i+1:], txsOnPage, api.TxHistoryLight) if err != nil { return errorTpl, nil, err } @@ -904,7 +904,7 @@ func (s *PublicServer) apiXpub(r *http.Request, apiVersion int) (interface{}, er var err error s.metrics.ExplorerViews.With(common.Labels{"action": "api-xpub"}).Inc() if i := strings.LastIndexByte(r.URL.Path, '/'); i > 0 { - address, err = s.getAddressForXpub(r, r.URL.Path[i+1:], txsInAPI, api.TxidHistory) + address, err = s.getXpubAddress(r, r.URL.Path[i+1:], txsInAPI, api.TxidHistory) if err == nil && apiVersion == apiV1 { return s.api.AddressToV1(address), nil } diff --git a/server/websocket.go b/server/websocket.go index 91bfbab8..5377910b 100644 --- a/server/websocket.go +++ b/server/websocket.go @@ -317,10 +317,10 @@ func (s *WebsocketServer) onRequest(c *websocketChannel, req *websocketReq) { } if err == nil { glog.V(1).Info("Client ", c.id, " onRequest ", req.Method, " success") - s.metrics.SocketIORequests.With(common.Labels{"method": req.Method, "status": "success"}).Inc() + s.metrics.WebsocketRequests.With(common.Labels{"method": req.Method, "status": "success"}).Inc() } else { glog.Error("Client ", c.id, " onMessage ", req.Method, ": ", errors.ErrorStack(err)) - s.metrics.SocketIORequests.With(common.Labels{"method": req.Method, "status": err.Error()}).Inc() + s.metrics.WebsocketRequests.With(common.Labels{"method": req.Method, "status": err.Error()}).Inc() e := resultError{} e.Error.Message = err.Error() data = e @@ -358,16 +358,26 @@ func (s *WebsocketServer) getAccountInfo(req *accountInfoReq) (res *api.Address, default: opt = api.Basic } - return s.api.GetAddress(req.Descriptor, req.Page, req.PageSize, opt, &api.AddressFilter{ + filter := api.AddressFilter{ FromHeight: uint32(req.FromHeight), ToHeight: uint32(req.ToHeight), Contract: req.ContractFilter, Vout: api.AddressFilterVoutOff, - }) + AllTokens: true, + } + a, err := s.api.GetXpubAddress(req.Descriptor, req.Page, req.PageSize, opt, &filter, 0) + if err != nil { + return s.api.GetAddress(req.Descriptor, req.Page, req.PageSize, opt, &filter) + } + return a, nil } func (s *WebsocketServer) getAccountUtxo(descriptor string) (interface{}, error) { - return s.api.GetAddressUtxo(descriptor, false) + utxo, err := s.api.GetXpubUtxo(descriptor, false, 0) + if err != nil { + return s.api.GetAddressUtxo(descriptor, false) + } + return utxo, nil } func (s *WebsocketServer) getTransaction(txid string) (interface{}, error) {