Skip to content

Commit

Permalink
btcjson: change getblock default verbosity to 1
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
henryperson authored May 15, 2020
1 parent b470eee commit d38279e
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion btcjson/chainsvrcmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions btcjson/chainsvrcmds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion btcjson/cmdinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func TestMethodUsageText(t *testing.T) {
{
name: "getblock",
method: "getblock",
expected: `getblock "hash" (verbosity=0)`,
expected: `getblock "hash" (verbosity=1)`,
},
}

Expand Down
2 changes: 1 addition & 1 deletion rpcclient/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
6 changes: 2 additions & 4 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions rpcserverhelp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit d38279e

Please sign in to comment.