Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix verbocity in getblock command #1112

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions btcjson/chainsvrcmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,20 +128,18 @@ func NewGetBestBlockHashCmd() *GetBestBlockHashCmd {
// GetBlockCmd defines the getblock JSON-RPC command.
type GetBlockCmd struct {
Hash string
Verbose *bool `jsonrpcdefault:"true"`
VerboseTx *bool `jsonrpcdefault:"false"`
Verbosity *uint32 `jsonrpcdefault:"1"`
}

// NewGetBlockCmd returns a new instance which can be used to issue a getblock
// JSON-RPC command.
//
// The parameters which are pointers indicate they are optional. Passing nil
// for optional parameters will use the default value.
func NewGetBlockCmd(hash string, verbose, verboseTx *bool) *GetBlockCmd {
func NewGetBlockCmd(hash string, verbosity *uint32) *GetBlockCmd {
return &GetBlockCmd{
Hash: hash,
Verbose: verbose,
VerboseTx: verboseTx,
Verbosity: verbosity,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rest of this file uses Verbose as a variable name, not Verbosity. For example,

Verbose *bool `jsonrpcdefault:"false"`

}
}

Expand Down
29 changes: 13 additions & 16 deletions btcjson/chainsvrcmds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,15 @@ func TestChainSvrCmds(t *testing.T) {
{
name: "getblock",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("getblock", "123")
return btcjson.NewCmd("getblock", "123", 0)
},
staticCmd: func() interface{} {
return btcjson.NewGetBlockCmd("123", nil, nil)
return btcjson.NewGetBlockCmd("123", btcjson.Uint32(0))
},
marshalled: `{"jsonrpc":"1.0","method":"getblock","params":["123"],"id":1}`,
marshalled: `{"jsonrpc":"1.0","method":"getblock","params":["123",0],"id":1}`,
unmarshalled: &btcjson.GetBlockCmd{
Hash: "123",
Verbose: btcjson.Bool(true),
VerboseTx: btcjson.Bool(false),
Verbosity: btcjson.Uint32(0),
},
},
{
Expand All @@ -159,32 +158,30 @@ func TestChainSvrCmds(t *testing.T) {
// Intentionally use a source param that is
// more pointers than the destination to
// exercise that path.
verbosePtr := btcjson.Bool(true)
return btcjson.NewCmd("getblock", "123", &verbosePtr)
verbosityPtr := btcjson.Uint32(1)
return btcjson.NewCmd("getblock", "123", &verbosityPtr)
},
staticCmd: func() interface{} {
return btcjson.NewGetBlockCmd("123", btcjson.Bool(true), nil)
return btcjson.NewGetBlockCmd("123", btcjson.Uint32(1))
},
marshalled: `{"jsonrpc":"1.0","method":"getblock","params":["123",true],"id":1}`,
marshalled: `{"jsonrpc":"1.0","method":"getblock","params":["123",1],"id":1}`,
unmarshalled: &btcjson.GetBlockCmd{
Hash: "123",
Verbose: btcjson.Bool(true),
VerboseTx: btcjson.Bool(false),
Verbosity: btcjson.Uint32(1),
},
},
{
name: "getblock required optional2",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("getblock", "123", true, true)
return btcjson.NewCmd("getblock", "123", 2)
},
staticCmd: func() interface{} {
return btcjson.NewGetBlockCmd("123", btcjson.Bool(true), btcjson.Bool(true))
return btcjson.NewGetBlockCmd("123", btcjson.Uint32(2))
},
marshalled: `{"jsonrpc":"1.0","method":"getblock","params":["123",true,true],"id":1}`,
marshalled: `{"jsonrpc":"1.0","method":"getblock","params":["123",2],"id":1}`,
unmarshalled: &btcjson.GetBlockCmd{
Hash: "123",
Verbose: btcjson.Bool(true),
VerboseTx: btcjson.Bool(true),
Verbosity: btcjson.Uint32(2),
},
},
{
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" (verbose=true verbosetx=false)`,
expected: `getblock "hash" (verbosity=1)`,
},
}

Expand Down
19 changes: 6 additions & 13 deletions btcjson/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,9 @@ import (
// This example demonstrates how to create and marshal a command into a JSON-RPC
// request.
func ExampleMarshalCmd() {
// Create a new getblock command. Notice the nil parameter indicates
// to use the default parameter for that fields. This is a common
// pattern used in all of the New<Foo>Cmd functions in this package for
// optional fields. Also, notice the call to btcjson.Bool which is a
// convenience function for creating a pointer out of a primitive for
// optional parameters.
// Create a new getblock command.
blockHash := "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"
gbCmd := btcjson.NewGetBlockCmd(blockHash, btcjson.Bool(false), nil)
gbCmd := btcjson.NewGetBlockCmd(blockHash, btcjson.Uint32(0))

// Marshal the command to the format suitable for sending to the RPC
// server. Typically the client would increment the id here which is
Expand All @@ -38,15 +33,15 @@ func ExampleMarshalCmd() {
fmt.Printf("%s\n", marshalledBytes)

// Output:
// {"jsonrpc":"1.0","method":"getblock","params":["000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f",false],"id":1}
// {"jsonrpc":"1.0","method":"getblock","params":["000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f",0],"id":1}
}

// This example demonstrates how to unmarshal a JSON-RPC request and then
// unmarshal the concrete request into a concrete command.
func ExampleUnmarshalCmd() {
// Ordinarily this would be read from the wire, but for this example,
// it is hard coded here for clarity.
data := []byte(`{"jsonrpc":"1.0","method":"getblock","params":["000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f",false],"id":1}`)
data := []byte(`{"jsonrpc":"1.0","method":"getblock","params":["000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f",0],"id":1}`)

// Unmarshal the raw bytes from the wire into a JSON-RPC request.
var request btcjson.Request
Expand Down Expand Up @@ -84,13 +79,11 @@ func ExampleUnmarshalCmd() {

// Display the fields in the concrete command.
fmt.Println("Hash:", gbCmd.Hash)
fmt.Println("Verbose:", *gbCmd.Verbose)
fmt.Println("VerboseTx:", *gbCmd.VerboseTx)
fmt.Println("Verbosity:", *gbCmd.Verbosity)

// Output:
// Hash: 000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f
// Verbose: false
// VerboseTx: false
// Verbosity: 0
}

// This example demonstrates how to marshal a JSON-RPC response.
Expand Down
7 changes: 4 additions & 3 deletions 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, btcjson.Bool(false), nil)
cmd := btcjson.NewGetBlockCmd(hash, btcjson.Uint32(0))
return c.sendCmd(cmd)
}

Expand Down Expand Up @@ -141,7 +141,7 @@ func (c *Client) GetBlockVerboseAsync(blockHash *chainhash.Hash) FutureGetBlockV
hash = blockHash.String()
}

cmd := btcjson.NewGetBlockCmd(hash, btcjson.Bool(true), nil)
cmd := btcjson.NewGetBlockCmd(hash, btcjson.Uint32(1))
return c.sendCmd(cmd)
}

Expand All @@ -165,7 +165,8 @@ func (c *Client) GetBlockVerboseTxAsync(blockHash *chainhash.Hash) FutureGetBloc
hash = blockHash.String()
}

cmd := btcjson.NewGetBlockCmd(hash, btcjson.Bool(true), btcjson.Bool(true))
cmd := btcjson.NewGetBlockCmd(hash, btcjson.Uint32(2))

return c.sendCmd(cmd)
}

Expand Down
9 changes: 5 additions & 4 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -1054,13 +1054,13 @@ func handleGetBlock(s *rpcServer, cmd interface{}, closeChan <-chan struct{}) (i
}
}

// When the verbose flag isn't set, simply return the serialized block
// When the verbosity value setted to 0, simply return the serialized block

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

grammar nitpick: "When the verbose flag is set to 0"

// as a hex-encoded string.
if c.Verbose != nil && !*c.Verbose {
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.
// Generate the JSON object and return it.

// Deserialize the block.
blk, err := btcutil.NewBlockFromBytes(blkBytes)
Expand Down Expand Up @@ -1109,7 +1109,8 @@ func handleGetBlock(s *rpcServer, cmd interface{}, closeChan <-chan struct{}) (i
NextHash: nextHashString,
}

if c.VerboseTx == nil || !*c.VerboseTx {
// When the verbosity value isn't set, setted to 1 or not equal 0 or 2
if c.Verbosity == nil || *c.Verbosity == 1 || (*c.Verbosity != 0 && *c.Verbosity != 2) {
Copy link

@justinmoon justinmoon Jun 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't this line equivalent to if *c.Verbosity != 0 && *c.Verbosity != 2?

transactions := blk.Transactions()
txNames := make([]string, len(transactions))
for i, tx := range transactions {
Expand Down
10 changes: 6 additions & 4 deletions rpcserverhelp.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,13 @@ var helpDescsEnUS = map[string]string{
// GetBlockCmd help.
"getblock--synopsis": "Returns information about a block given its hash.",
"getblock-hash": "The hash of the block",
"getblock-verbose": "Specifies the block is returned as a JSON object instead of hex-encoded string",
"getblock-verbosetx": "Specifies that each transaction is returned as a JSON object and only applies if the verbose flag is true (btcd extension)",
"getblock--condition0": "verbose=false",
"getblock--condition1": "verbose=true",
"getblock-verbosity": "Specifies the block format returns",
Copy link

@justinmoon justinmoon Jun 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rest of the help uses command-verbose and verbose=n patterns, not a command-verbosity and verbosity=n. For example,

btcd/rpcserverhelp.go

Lines 463 to 466 in ab8fa7b

"getrawmempool-verbose": "Returns JSON object when true or an array of transaction hashes when false",
"getrawmempool--condition0": "verbose=false",
"getrawmempool--condition1": "verbose=true",
"getrawmempool--result0": "Array of transaction hashes",

"getblock--condition0": "verbosity=0",
"getblock--condition1": "verbosity=1",
"getblock--condition2": "verbosity=2",
"getblock--result0": "Hex-encoded bytes of the serialized block",
"getblock--result1": "JSON object with information about block",
"getblock--result2": "JSON object with information about block and information about each transaction.",

// GetBlockChainInfoCmd help.
"getblockchaininfo--synopsis": "Returns information about the current blockchain state and the status of any active soft-fork deployments.",
Expand Down