From d38279ee7446d9ee17cb7242afb5ccb32c3775ec Mon Sep 17 00:00:00 2001 From: Henry Date: Thu, 14 May 2020 18:27:59 -0600 Subject: [PATCH] btcjson: change getblock default verbosity to 1 This change makes btcd's getblock command match bitcoind's. Previously the default verbosity was 0, which caused errors when using the rpcclient library to connect to a bitcoind node - getblock would unmarshall incorrectly since it didn't expect a verbosity=1 result when it did not specify verbosity. --- btcjson/chainsvrcmds.go | 2 +- btcjson/chainsvrcmds_test.go | 14 ++++++++++++++ btcjson/cmdinfo_test.go | 2 +- rpcclient/chain.go | 2 +- rpcserver.go | 6 ++---- rpcserverhelp.go | 4 ++-- 6 files changed, 21 insertions(+), 9 deletions(-) diff --git a/btcjson/chainsvrcmds.go b/btcjson/chainsvrcmds.go index 90ab70ece7..2f3069fd15 100644 --- a/btcjson/chainsvrcmds.go +++ b/btcjson/chainsvrcmds.go @@ -130,7 +130,7 @@ func NewGetBestBlockHashCmd() *GetBestBlockHashCmd { // GetBlockCmd defines the getblock JSON-RPC command. type GetBlockCmd struct { Hash string - Verbosity *int `jsonrpcdefault:"0"` + Verbosity *int `jsonrpcdefault:"1"` } // NewGetBlockCmd returns a new instance which can be used to issue a getblock diff --git a/btcjson/chainsvrcmds_test.go b/btcjson/chainsvrcmds_test.go index dca2332860..8f3fd455c5 100644 --- a/btcjson/chainsvrcmds_test.go +++ b/btcjson/chainsvrcmds_test.go @@ -153,6 +153,20 @@ func TestChainSvrCmds(t *testing.T) { Verbosity: btcjson.Int(0), }, }, + { + name: "getblock default verbosity", + newCmd: func() (interface{}, error) { + return btcjson.NewCmd("getblock", "123") + }, + staticCmd: func() interface{} { + return btcjson.NewGetBlockCmd("123", nil) + }, + marshalled: `{"jsonrpc":"1.0","method":"getblock","params":["123"],"id":1}`, + unmarshalled: &btcjson.GetBlockCmd{ + Hash: "123", + Verbosity: btcjson.Int(1), + }, + }, { name: "getblock required optional1", newCmd: func() (interface{}, error) { diff --git a/btcjson/cmdinfo_test.go b/btcjson/cmdinfo_test.go index 471fccfc77..61a693e404 100644 --- a/btcjson/cmdinfo_test.go +++ b/btcjson/cmdinfo_test.go @@ -151,7 +151,7 @@ func TestMethodUsageText(t *testing.T) { { name: "getblock", method: "getblock", - expected: `getblock "hash" (verbosity=0)`, + expected: `getblock "hash" (verbosity=1)`, }, } diff --git a/rpcclient/chain.go b/rpcclient/chain.go index 707978ca65..a5ec9e5412 100644 --- a/rpcclient/chain.go +++ b/rpcclient/chain.go @@ -97,7 +97,7 @@ func (c *Client) GetBlockAsync(blockHash *chainhash.Hash) FutureGetBlockResult { hash = blockHash.String() } - cmd := btcjson.NewGetBlockCmd(hash, nil) + cmd := btcjson.NewGetBlockCmd(hash, btcjson.Int(0)) return c.sendCmd(cmd) } diff --git a/rpcserver.go b/rpcserver.go index 2da7e9f57e..89817db856 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -1081,14 +1081,12 @@ func handleGetBlock(s *rpcServer, cmd interface{}, closeChan <-chan struct{}) (i Message: "Block not found", } } - - // When the verbose flag isn't set, simply return the serialized block - // as a hex-encoded string. + // If verbosity is 0, return the serialized block as a hex encoded string. if c.Verbosity != nil && *c.Verbosity == 0 { return hex.EncodeToString(blkBytes), nil } - // The verbose flag is set, so generate the JSON object and return it. + // Otherwise, generate the JSON object and return it. // Deserialize the block. blk, err := btcutil.NewBlockFromBytes(blkBytes) diff --git a/rpcserverhelp.go b/rpcserverhelp.go index cee2407566..38a5351355 100644 --- a/rpcserverhelp.go +++ b/rpcserverhelp.go @@ -243,8 +243,8 @@ var helpDescsEnUS = map[string]string{ "getblockverboseresult-version": "The block version", "getblockverboseresult-versionHex": "The block version in hexadecimal", "getblockverboseresult-merkleroot": "Root hash of the merkle tree", - "getblockverboseresult-tx": "The transaction hashes (only when verbosetx=false)", - "getblockverboseresult-rawtx": "The transactions as JSON objects (only when verbosetx=true)", + "getblockverboseresult-tx": "The transaction hashes (only when verbosity=1)", + "getblockverboseresult-rawtx": "The transactions as JSON objects (only when verbosity=2)", "getblockverboseresult-time": "The block time in seconds since 1 Jan 1970 GMT", "getblockverboseresult-nonce": "The block nonce", "getblockverboseresult-bits": "The bits which represent the block difficulty",