Skip to content

Commit

Permalink
Merge pull request #4 from breez/jssdwt-fallback-to-chain-peer
Browse files Browse the repository at this point in the history
fallback to chain peer
  • Loading branch information
JssDWt committed Apr 22, 2024
2 parents 24fc09d + 113a090 commit 01a2d89
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
9 changes: 4 additions & 5 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -897,9 +897,6 @@ func (s *ChainService) GetCFilter(blockHash chainhash.Hash,
return nil, err
}

// We will first check if the node supports rest API
// and try to get the filter from there

// With all the necessary items retrieved, we'll launch our concurrent
// query to the set of connected peers.
log.Debugf("Fetching filters for heights=[%v, %v], stophash=%v",
Expand All @@ -912,9 +909,11 @@ func (s *ChainService) GetCFilter(blockHash chainhash.Hash,
defer s.mtxCFilter.Unlock()
defer close(query.filterChan)

if len(s.restPeers) > 0 {
s.queryRestPeers(query)
restPeerIndex, err := s.selectRestPeerIndex()
if err == nil {
s.queryRestPeers(query, restPeerIndex)
} else {
log.Infof("Could not select rest peer: %v", err)
s.queryPeers(
// Send a wire.MsgGetCFilters.
query.queryMsg(),
Expand Down
30 changes: 17 additions & 13 deletions query_rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,9 @@ import (
"github.com/btcsuite/btcd/wire"
)

func (s *ChainService) queryRestPeers(query *cfiltersQuery) {
func (s *ChainService) queryRestPeers(query *cfiltersQuery, restPeerIndex int) {
quit := make(chan struct{})
client := &http.Client{}
validPeers := make([]int, 0, len(s.restPeers))
for i, p := range s.restPeers {
if p.failures == 0 || time.Since(p.lastFailure) > 10*time.Second {
validPeers = append(validPeers, i)
}
}
if len(validPeers) == 0 {
log.Errorf("queryRestPeers - No valid rest peer")
return
}
// #nosec G404 -- No need to have true randomness when selecting restpeer.
restPeerIndex := validPeers[rand.Intn(len(validPeers))]
URL := fmt.Sprintf("%v/rest/blockfilter/basic/%v.bin?count=%v", s.restPeers[restPeerIndex].URL, query.stopHash.String(), query.stopHeight-query.startHeight+1)
log.Infof("getting %v filters from height %v to height %v, using URL: %v", query.stopHeight-query.startHeight+1, query.startHeight, query.stopHeight, URL)
res, err := client.Get(URL)
Expand Down Expand Up @@ -65,3 +53,19 @@ func (s *ChainService) queryRestPeers(query *cfiltersQuery) {
}
log.Infof("Called handleCFilterRestponse for %v filter received from URL: %v", count, URL)
}

func (s *ChainService) selectRestPeerIndex() (int, error) {
validPeers := make([]int, 0, len(s.restPeers))
for i, p := range s.restPeers {
if p.failures == 0 || time.Since(p.lastFailure) > 10*time.Second {
validPeers = append(validPeers, i)
}
}

if len(validPeers) == 0 {
return -1, fmt.Errorf("no valid rest peer")
}

// #nosec G404 -- No need to have true randomness when selecting restpeer.
return validPeers[rand.Intn(len(validPeers))], nil
}

0 comments on commit 01a2d89

Please sign in to comment.