From 67b5c2fb7ea691fb40dd6beb1b6fbc8501bf4d1b Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Thu, 26 Dec 2013 11:11:12 -0600 Subject: [PATCH] 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. --- rpcserver.go | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/rpcserver.go b/rpcserver.go index 8ea66978..7e47aafe 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -730,12 +730,32 @@ func handleGetBlock(s *rpcServer, cmd btcjson.Cmd, walletNotification chan []byt rpcsLog.Errorf("Error fetching sha: %v", err) 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() if err != nil { 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() if err != nil { @@ -743,16 +763,6 @@ func handleGetBlock(s *rpcServer, cmd btcjson.Cmd, walletNotification chan []byt 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 blockReply := btcjson.BlockResult{ Hash: c.Hash,