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.
This commit is contained in:
Henry 2020-05-14 18:27:59 -06:00 committed by GitHub
parent b470eee477
commit d38279ee74
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 21 additions and 9 deletions

View file

@ -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

View file

@ -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) {

View file

@ -151,7 +151,7 @@ func TestMethodUsageText(t *testing.T) {
{
name: "getblock",
method: "getblock",
expected: `getblock "hash" (verbosity=0)`,
expected: `getblock "hash" (verbosity=1)`,
},
}

View file

@ -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)
}

View file

@ -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)

View file

@ -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",