Skip to content

Commit

Permalink
Add getblockfilter JSON-RPC client command
Browse files Browse the repository at this point in the history
  • Loading branch information
lindlof committed May 16, 2020
1 parent 8512aff commit 5e82cef
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
16 changes: 16 additions & 0 deletions btcjson/chainsvrcmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,21 @@ func NewGetBlockCountCmd() *GetBlockCountCmd {
return &GetBlockCountCmd{}
}

// GetBlockFilterCmd defines the getblockfilter JSON-RPC command.
type GetBlockFilterCmd struct {
BlockHash string
FilterType *string
}

// NewGetBlockFilterCmd returns a new instance which can be used to issue a
// getblockfilter JSON-RPC command.
func NewGetBlockFilterCmd(blockHash string, filterType *string) *GetBlockFilterCmd {
return &GetBlockFilterCmd{
BlockHash: blockHash,
FilterType: filterType,
}
}

// GetBlockHashCmd defines the getblockhash JSON-RPC command.
type GetBlockHashCmd struct {
Index int64
Expand Down Expand Up @@ -840,6 +855,7 @@ func init() {
MustRegisterCmd("getblock", (*GetBlockCmd)(nil), flags)
MustRegisterCmd("getblockchaininfo", (*GetBlockChainInfoCmd)(nil), flags)
MustRegisterCmd("getblockcount", (*GetBlockCountCmd)(nil), flags)
MustRegisterCmd("getblockfilter", (*GetBlockFilterCmd)(nil), flags)
MustRegisterCmd("getblockhash", (*GetBlockHashCmd)(nil), flags)
MustRegisterCmd("getblockheader", (*GetBlockHeaderCmd)(nil), flags)
MustRegisterCmd("getblockstats", (*GetBlockStatsCmd)(nil), flags)
Expand Down
22 changes: 22 additions & 0 deletions btcjson/chainsvrcmds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,28 @@ func TestChainSvrCmds(t *testing.T) {
marshalled: `{"jsonrpc":"1.0","method":"getblockcount","params":[],"id":1}`,
unmarshalled: &btcjson.GetBlockCountCmd{},
},
{
name: "getblockfilter",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("getblockfilter", "0000afaf")
},
staticCmd: func() interface{} {
return btcjson.NewGetBlockFilterCmd("0000afaf", nil)
},
marshalled: `{"jsonrpc":"1.0","method":"getblockfilter","params":["0000afaf"],"id":1}`,
unmarshalled: &btcjson.GetBlockFilterCmd{"0000afaf", nil},
},
{
name: "getblockfilter optional filtertype",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("getblockfilter", "0000afaf", "basic")
},
staticCmd: func() interface{} {
return btcjson.NewGetBlockFilterCmd("0000afaf", btcjson.String("basic"))
},
marshalled: `{"jsonrpc":"1.0","method":"getblockfilter","params":["0000afaf","basic"],"id":1}`,
unmarshalled: &btcjson.GetBlockFilterCmd{"0000afaf", btcjson.String("basic")},
},
{
name: "getblockhash",
newCmd: func() (interface{}, error) {
Expand Down
7 changes: 7 additions & 0 deletions btcjson/chainsvrresults.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,13 @@ type GetBlockChainInfoResult struct {
*UnifiedSoftForks
}

// GetBlockFilterResult models the data returned from the getblockfilter
// command.
type GetBlockFilterResult struct {
Filter string `json:"filter"`
Header string `json:"header"`
}

// GetBlockTemplateResultTx models the transactions field of the
// getblocktemplate command.
type GetBlockTemplateResultTx struct {
Expand Down
36 changes: 36 additions & 0 deletions rpcclient/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,42 @@ func (c *Client) GetBlockChainInfo() (*btcjson.GetBlockChainInfoResult, error) {
return c.GetBlockChainInfoAsync().Receive()
}

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

// Receive waits for the response promised by the future and returns block filter
// result provided by the server.
func (r FutureGetBlockFilterResult) Receive() (*btcjson.GetBlockFilterResult, error) {
res, err := receiveFuture(r)
if err != nil {
return nil, err
}

var blockFilter btcjson.GetBlockFilterResult
err = json.Unmarshal(res, &blockFilter)
if err != nil {
return nil, err
}

return &blockFilter, nil
}

// GetBlockFilterAsync 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 GetBlockFilter for the blocking version and more details.
func (c *Client) GetBlockFilterAsync(blockHash string, filterType *string) FutureGetBlockFilterResult {
cmd := btcjson.NewGetBlockFilterCmd(blockHash, filterType)
return c.sendCmd(cmd)
}

// GetBlockFilter retrieves a BIP 157 content filter for a particular block.
func (c *Client) GetBlockFilter(blockHash string, filterType *string) (*btcjson.GetBlockFilterResult, error) {
return c.GetBlockFilterAsync(blockHash, filterType).Receive()
}

// FutureGetBlockHashResult is a future promise to deliver the result of a
// GetBlockHashAsync RPC invocation (or an applicable error).
type FutureGetBlockHashResult chan *response
Expand Down

0 comments on commit 5e82cef

Please sign in to comment.