Skip to content

Commit

Permalink
Updates for dcrd JSON-RPC websocket API changes. (btcsuite#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrick authored Nov 8, 2016
1 parent 6ef6822 commit c872526
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 571 deletions.
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
language: go
go:
- 1.6.3
- 1.7
- 1.7.3
sudo: false
install:
- go get -d -t -v ./...
- go get github.com/Masterminds/glide
- glide install
- go install
- go get -v golang.org/x/tools/cmd/cover
- go get -v github.com/bradfitz/goimports
- go get -v github.com/golang/lint/golint
Expand Down
43 changes: 43 additions & 0 deletions chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,3 +496,46 @@ func (c *Client) GetTxOutAsync(txHash *chainhash.Hash, index uint32, mempool boo
func (c *Client) GetTxOut(txHash *chainhash.Hash, index uint32, mempool bool) (*dcrjson.GetTxOutResult, error) {
return c.GetTxOutAsync(txHash, index, mempool).Receive()
}

// FutureRescanResult is a future promise to deliver the result of a
// RescanAsynnc RPC invocation (or an applicable error).
type FutureRescanResult chan *response

// Receive waits for the response promised by the future and returns the
// discovered rescan data.
func (r FutureRescanResult) Receive() (*dcrjson.RescanResult, error) {
res, err := receiveFuture(r)
if err != nil {
return nil, err
}

var rescanResult *dcrjson.RescanResult
err = json.Unmarshal(res, &rescanResult)
if err != nil {
return nil, err
}

return rescanResult, nil
}

// RescanAsync returns an instance of a type that can be used to get the result
// of the RPC at some future time by invoking the Receive function on the
// returned instance.
//
// See Rescan for the blocking version and more details.
func (c *Client) RescanAsync(blockHashes []chainhash.Hash) FutureRescanResult {
concatenatedBlockHashes := make([]byte, chainhash.HashSize*len(blockHashes))
for i := range blockHashes {
copy(concatenatedBlockHashes[i*chainhash.HashSize:], blockHashes[i][:])
}

cmd := dcrjson.NewRescanCmd(hex.EncodeToString(concatenatedBlockHashes))
return c.sendCmd(cmd)
}

// Rescan rescans the blocks identified by blockHashes, in order, using the
// client's loaded transaction filter. The blocks do not need to be on the main
// chain, but they do need to be adjacent to each other.
func (c *Client) Rescan(blockHashes []chainhash.Hash) (*dcrjson.RescanResult, error) {
return c.RescanAsync(blockHashes).Receive()
}
16 changes: 16 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package: github.com/decred/dcrrpcclient
import:
- package: github.com/btcsuite/btclog
- package: github.com/btcsuite/go-socks
subpackages:
- socks
- package: github.com/btcsuite/websocket
- package: github.com/davecgh/go-spew
subpackages:
- spew
- package: github.com/decred/dcrd
subpackages:
- chaincfg/chainhash
- dcrjson
- wire
- package: github.com/decred/dcrutil
39 changes: 0 additions & 39 deletions infrastructure.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,17 +260,6 @@ func (c *Client) trackRegisteredNtfns(cmd interface{}) {
c.ntfnState.notifyNewTxVerbose = true
} else {
c.ntfnState.notifyNewTx = true

}

case *dcrjson.NotifySpentCmd:
for _, op := range bcmd.OutPoints {
c.ntfnState.notifySpent[op] = struct{}{}
}

case *dcrjson.NotifyReceivedCmd:
for _, addr := range bcmd.Addresses {
c.ntfnState.notifyReceived[addr] = struct{}{}
}
}
}
Expand Down Expand Up @@ -567,34 +556,6 @@ func (c *Client) reregisterNtfns() error {
}
}

// Reregister the combination of all previously registered notifyspent
// outpoints in one command if needed.
nslen := len(stateCopy.notifySpent)
if nslen > 0 {
outpoints := make([]dcrjson.OutPoint, 0, nslen)
for op := range stateCopy.notifySpent {
outpoints = append(outpoints, op)
}
log.Debugf("Reregistering [notifyspent] outpoints: %v", outpoints)
if err := c.notifySpentInternal(outpoints).Receive(); err != nil {
return err
}
}

// Reregister the combination of all previously registered
// notifyreceived addresses in one command if needed.
nrlen := len(stateCopy.notifyReceived)
if nrlen > 0 {
addresses := make([]string, 0, nrlen)
for addr := range stateCopy.notifyReceived {
addresses = append(addresses, addr)
}
log.Debugf("Reregistering [notifyreceived] addresses: %v", addresses)
if err := c.notifyReceivedInternal(addresses).Receive(); err != nil {
return err
}
}

return nil
}

Expand Down
Loading

0 comments on commit c872526

Please sign in to comment.