Correct getblock RPC handling and optimize.

The getblock RPC call should return a hex-encoded string of the block when
verbose is false instead of a BlockResult object with a Hex field set to
be compatible with the Sathoshi client.  This commit, along with a recent
commit to btcjson corrects this.

Also, while here, move code which only applies to verbose mode after the
call which handles the non-verbose logic.  This saves a few cycles since
the non-verbose logic doesn't need the extra information.
This commit is contained in:
Dave Collins 2013-12-26 11:11:12 -06:00
parent ca4cf29e49
commit 67b5c2fb7e

View file

@ -730,12 +730,32 @@ func handleGetBlock(s *rpcServer, cmd btcjson.Cmd, walletNotification chan []byt
rpcsLog.Errorf("Error fetching sha: %v", err) rpcsLog.Errorf("Error fetching sha: %v", err)
return nil, btcjson.ErrBlockNotFound return nil, btcjson.ErrBlockNotFound
} }
idx := blk.Height()
// When the verbose flag isn't set, simply return the network-serialized
// block as a hex-encoded string.
if !c.Verbose {
var wireBuf bytes.Buffer
err := blk.MsgBlock().BtcEncode(&wireBuf, btcwire.ProtocolVersion)
if err != nil {
return nil, btcjson.Error{
Code: btcjson.ErrInternal.Code,
Message: err.Error(),
}
}
blkHex := hex.EncodeToString(wireBuf.Bytes())
return blkHex, nil
}
// The verbose flag is set, so generate the JSON object and return it.
buf, err := blk.Bytes() buf, err := blk.Bytes()
if err != nil { if err != nil {
rpcsLog.Errorf("Error fetching block: %v", err) rpcsLog.Errorf("Error fetching block: %v", err)
return nil, btcjson.ErrBlockNotFound return nil, btcjson.Error{
Code: btcjson.ErrInternal.Code,
Message: err.Error(),
} }
}
idx := blk.Height()
_, maxidx, err := s.server.db.NewestSha() _, maxidx, err := s.server.db.NewestSha()
if err != nil { if err != nil {
@ -743,16 +763,6 @@ func handleGetBlock(s *rpcServer, cmd btcjson.Cmd, walletNotification chan []byt
return nil, btcjson.ErrBlockNotFound return nil, btcjson.ErrBlockNotFound
} }
if !c.Verbose {
var wireBuf bytes.Buffer
err := blk.MsgBlock().BtcEncode(&wireBuf, btcwire.ProtocolVersion)
if err != nil {
return nil, btcjson.Error{Code: btcjson.ErrInternal.Code, Message: err.Error()}
}
blkHex := hex.EncodeToString(wireBuf.Bytes())
return btcjson.BlockResult{Hex: blkHex}, nil
}
blockHeader := &blk.MsgBlock().Header blockHeader := &blk.MsgBlock().Header
blockReply := btcjson.BlockResult{ blockReply := btcjson.BlockResult{
Hash: c.Hash, Hash: c.Hash,