From 9d7bd871e7c92144deb47ad256b459c5e783c030 Mon Sep 17 00:00:00 2001 From: Mikael Lindlof Date: Tue, 26 May 2020 23:52:23 +0100 Subject: [PATCH] Add type for second getblockfilter param --- btcjson/chainsvrcmds.go | 15 ++++++++++++--- btcjson/chainsvrcmds_test.go | 4 ++-- btcjson/helpers.go | 8 ++++++++ rpcclient/chain.go | 6 +++--- 4 files changed, 25 insertions(+), 8 deletions(-) diff --git a/btcjson/chainsvrcmds.go b/btcjson/chainsvrcmds.go index 5619248a5b..a9a881d401 100644 --- a/btcjson/chainsvrcmds.go +++ b/btcjson/chainsvrcmds.go @@ -163,10 +163,19 @@ func NewGetBlockCountCmd() *GetBlockCountCmd { return &GetBlockCountCmd{} } +// FilterTypeName defines the type used in the getblockfilter JSON-RPC command for the +// filter type field. +type FilterTypeName string + +const ( + // FilterTypeBasic is the basic filter type defined in BIP0158. + FilterTypeBasic FilterTypeName = "basic" +) + // GetBlockFilterCmd defines the getblockfilter JSON-RPC command. type GetBlockFilterCmd struct { - BlockHash string // The hash of the block - FilterType *string // The type name of the filter, default=basic + BlockHash string // The hash of the block + FilterType *FilterTypeName // The type name of the filter, default=basic } // NewGetBlockFilterCmd returns a new instance which can be used to issue a @@ -174,7 +183,7 @@ type GetBlockFilterCmd struct { // // The parameters which are pointers indicate they are optional. Passing nil // for optional parameters will use the default value. -func NewGetBlockFilterCmd(blockHash string, filterType *string) *GetBlockFilterCmd { +func NewGetBlockFilterCmd(blockHash string, filterType *FilterTypeName) *GetBlockFilterCmd { return &GetBlockFilterCmd{ BlockHash: blockHash, FilterType: filterType, diff --git a/btcjson/chainsvrcmds_test.go b/btcjson/chainsvrcmds_test.go index bd759f6c17..8c10867501 100644 --- a/btcjson/chainsvrcmds_test.go +++ b/btcjson/chainsvrcmds_test.go @@ -220,10 +220,10 @@ func TestChainSvrCmds(t *testing.T) { return btcjson.NewCmd("getblockfilter", "0000afaf", "basic") }, staticCmd: func() interface{} { - return btcjson.NewGetBlockFilterCmd("0000afaf", btcjson.String("basic")) + return btcjson.NewGetBlockFilterCmd("0000afaf", btcjson.NewFilterTypeName(btcjson.FilterTypeBasic)) }, marshalled: `{"jsonrpc":"1.0","method":"getblockfilter","params":["0000afaf","basic"],"id":1}`, - unmarshalled: &btcjson.GetBlockFilterCmd{"0000afaf", btcjson.String("basic")}, + unmarshalled: &btcjson.GetBlockFilterCmd{"0000afaf", btcjson.NewFilterTypeName(btcjson.FilterTypeBasic)}, }, { name: "getblockhash", diff --git a/btcjson/helpers.go b/btcjson/helpers.go index d9b452e7c3..eda26cb885 100644 --- a/btcjson/helpers.go +++ b/btcjson/helpers.go @@ -75,3 +75,11 @@ func String(v string) *string { *p = v return p } + +// NewFilterTypeName is a helper routine that allocates a new FilterTypeName value to store v and +// returns a pointer to it. This is useful when assigning optional parameters. +func NewFilterTypeName(v FilterTypeName) *FilterTypeName { + p := new(FilterTypeName) + *p = v + return p +} diff --git a/rpcclient/chain.go b/rpcclient/chain.go index 84689e271e..dbcc0621e1 100644 --- a/rpcclient/chain.go +++ b/rpcclient/chain.go @@ -388,15 +388,15 @@ func (r FutureGetBlockFilterResult) Receive() (*btcjson.GetBlockFilterResult, er // returned instance. // // See GetBlockFilter for the blocking version and more details. -func (c *Client) GetBlockFilterAsync(blockHash chainhash.Hash, filterType *string) FutureGetBlockFilterResult { +func (c *Client) GetBlockFilterAsync(blockHash chainhash.Hash, filterType *btcjson.FilterTypeName) FutureGetBlockFilterResult { hash := blockHash.String() cmd := btcjson.NewGetBlockFilterCmd(hash, filterType) return c.sendCmd(cmd) } -// GetBlockFilter retrieves a BIP 157 content filter for a particular block. -func (c *Client) GetBlockFilter(blockHash chainhash.Hash, filterType *string) (*btcjson.GetBlockFilterResult, error) { +// GetBlockFilter retrieves a BIP0157 content filter for a particular block. +func (c *Client) GetBlockFilter(blockHash chainhash.Hash, filterType *btcjson.FilterTypeName) (*btcjson.GetBlockFilterResult, error) { return c.GetBlockFilterAsync(blockHash, filterType).Receive() }