Skip to content

Commit

Permalink
Merge pull request #100 from OpenBazaar/query
Browse files Browse the repository at this point in the history
Limit Max inflight queries to blockbook.
  • Loading branch information
placer14 committed Jan 7, 2020
2 parents 3f646c5 + a993627 commit e187f7c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 25 deletions.
24 changes: 19 additions & 5 deletions client/blockbook/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import (
"golang.org/x/net/proxy"
)

const maxInfightQueries = 25

var Log = logging.MustGetLogger("client")

type wsWatchdog struct {
Expand Down Expand Up @@ -350,14 +352,19 @@ func (i *BlockBookClient) GetTransactions(addrs []btcutil.Address) ([]model.Tran
Txs []model.Transaction
Err error
}
txChan := make(chan txsOrError)
var (
txChan = make(chan txsOrError)
queryChan = make(chan struct{}, maxInfightQueries)
wg sync.WaitGroup
)
wg.Add(len(addrs))
go func() {
var wg sync.WaitGroup
wg.Add(len(addrs))
for _, addr := range addrs {
queryChan <- struct{}{}
go func(a btcutil.Address) {
txs, err := i.getTransactions(maybeConvertCashAddress(a))
txChan <- txsOrError{txs, err}
<-queryChan
wg.Done()
}(addr)
}
Expand Down Expand Up @@ -439,13 +446,20 @@ func (i *BlockBookClient) GetUtxos(addrs []btcutil.Address) ([]model.Utxo, error
Utxo *model.Utxo
Err error
}
utxoChan := make(chan utxoOrError)
var wg sync.WaitGroup
var (
wg sync.WaitGroup
queryChan = make(chan struct{}, maxInfightQueries)
utxoChan = make(chan utxoOrError)
)
wg.Add(len(addrs))
go func() {
for _, addr := range addrs {
queryChan <- struct{}{}
go func(addr btcutil.Address) {
defer wg.Done()
defer func() {
<-queryChan
}()
resp, err := i.RequestFunc("/utxo/"+maybeConvertCashAddress(addr), http.MethodGet, nil, nil)
if err != nil {
utxoChan <- utxoOrError{nil, err}
Expand Down
38 changes: 18 additions & 20 deletions litecoin/address/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,26 +160,24 @@ func DecodeAddress(addr string, defaultNet *chaincfg.Params) (Address, error) {
prefix := addr[:oneIndex+1]
if IsBech32SegwitPrefix(prefix) {
witnessVer, witnessProg, err := decodeSegWitAddress(addr)
if err != nil {
return nil, err
}

// We currently only support P2WPKH and P2WSH, which is
// witness version 0.
if witnessVer != 0 {
return nil, UnsupportedWitnessVerError(witnessVer)
}

// The HRP is everything before the found '1'.
hrp := prefix[:len(prefix)-1]

switch len(witnessProg) {
case 20:
return newAddressWitnessPubKeyHash(hrp, witnessProg)
case 32:
return newAddressWitnessScriptHash(hrp, witnessProg)
default:
return nil, UnsupportedWitnessProgLenError(len(witnessProg))
if err == nil {
// We currently only support P2WPKH and P2WSH, which is
// witness version 0.
if witnessVer != 0 {
return nil, UnsupportedWitnessVerError(witnessVer)
}

// The HRP is everything before the found '1'.
hrp := prefix[:len(prefix)-1]

switch len(witnessProg) {
case 20:
return newAddressWitnessPubKeyHash(hrp, witnessProg)
case 32:
return newAddressWitnessScriptHash(hrp, witnessProg)
default:
return nil, UnsupportedWitnessProgLenError(len(witnessProg))
}
}
}
}
Expand Down

0 comments on commit e187f7c

Please sign in to comment.