Add xpub handling to websocket interface

This commit is contained in:
Martin Boehm 2019-02-11 11:54:34 +01:00
parent 6b0a4960fd
commit 63fb910ecb
3 changed files with 22 additions and 10 deletions

View File

@ -112,7 +112,7 @@ func (w *Worker) xpubCheckAndLoadTxids(ad *xpubAddress, filter *AddressFilter, m
if ad.balance == nil { if ad.balance == nil {
return 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.complete {
if ad.balance.Txs != ad.txs { if ad.balance.Txs != ad.txs {
newTxids, _, err := w.xpubGetAddressTxids(ad.addrDesc, false, ad.maxHeight+1, maxHeight, maxInt) 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 return nil
} }
// unless the filter is completely off, load all txids // 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 { if filter.FromHeight != 0 || filter.ToHeight != 0 || filter.Vout != AddressFilterVoutOff {
maxResults = maxInt 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 { if err != nil {
return err return err
} }

View File

@ -600,7 +600,7 @@ func (s *PublicServer) explorerAddress(w http.ResponseWriter, r *http.Request) (
return addressTpl, data, nil 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 var fn = api.AddressFilterVoutOff
page, ec := strconv.Atoi(r.URL.Query().Get("page")) page, ec := strconv.Atoi(r.URL.Query().Get("page"))
if ec != nil { if ec != nil {
@ -633,7 +633,7 @@ func (s *PublicServer) explorerXpub(w http.ResponseWriter, r *http.Request) (tpl
var err error var err error
s.metrics.ExplorerViews.With(common.Labels{"action": "xpub"}).Inc() s.metrics.ExplorerViews.With(common.Labels{"action": "xpub"}).Inc()
if i := strings.LastIndexByte(r.URL.Path, '/'); i > 0 { 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 { if err != nil {
return errorTpl, nil, err return errorTpl, nil, err
} }
@ -904,7 +904,7 @@ func (s *PublicServer) apiXpub(r *http.Request, apiVersion int) (interface{}, er
var err error var err error
s.metrics.ExplorerViews.With(common.Labels{"action": "api-xpub"}).Inc() s.metrics.ExplorerViews.With(common.Labels{"action": "api-xpub"}).Inc()
if i := strings.LastIndexByte(r.URL.Path, '/'); i > 0 { 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 { if err == nil && apiVersion == apiV1 {
return s.api.AddressToV1(address), nil return s.api.AddressToV1(address), nil
} }

View File

@ -317,10 +317,10 @@ func (s *WebsocketServer) onRequest(c *websocketChannel, req *websocketReq) {
} }
if err == nil { if err == nil {
glog.V(1).Info("Client ", c.id, " onRequest ", req.Method, " success") 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 { } else {
glog.Error("Client ", c.id, " onMessage ", req.Method, ": ", errors.ErrorStack(err)) 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 := resultError{}
e.Error.Message = err.Error() e.Error.Message = err.Error()
data = e data = e
@ -358,16 +358,26 @@ func (s *WebsocketServer) getAccountInfo(req *accountInfoReq) (res *api.Address,
default: default:
opt = api.Basic opt = api.Basic
} }
return s.api.GetAddress(req.Descriptor, req.Page, req.PageSize, opt, &api.AddressFilter{ filter := api.AddressFilter{
FromHeight: uint32(req.FromHeight), FromHeight: uint32(req.FromHeight),
ToHeight: uint32(req.ToHeight), ToHeight: uint32(req.ToHeight),
Contract: req.ContractFilter, Contract: req.ContractFilter,
Vout: api.AddressFilterVoutOff, 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) { 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) { func (s *WebsocketServer) getTransaction(txid string) (interface{}, error) {