Skip to content

Commit 88dd1ef

Browse files
author
Larry Ruane
committed
use getblock verbose=3 instead of two getblock calls
If available, use `getblock verbose=3`. If not available, use the old way, which requires making two `getblock` RPCs. This is only a performance improvement.
1 parent b805382 commit 88dd1ef

File tree

1 file changed

+39
-21
lines changed

1 file changed

+39
-21
lines changed

common/common.go

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ type (
153153
ZcashRpcReplyGetblock1 struct {
154154
Hash string
155155
Tx []string
156+
Hex string
156157
Trees struct {
157158
Sapling struct {
158159
Size uint32
@@ -275,21 +276,38 @@ func getBlockFromRPC(height int) (*walletrpc.CompactBlock, error) {
275276
}
276277
params := make([]json.RawMessage, 2)
277278
params[0] = heightJSON
278-
// Fetch the block using the verbose option ("1") because it provides
279-
// both the list of txids, which we're not yet able to compute for
280-
// Orchard (V5) transactions, and the block hash (block ID), which
281-
// we need to fetch the raw data format of the same block. Don't fetch
279+
280+
// We're not (yet) able to compute v5 transactions IDs (from the raw
281+
// serialized transaction), so we get this from zcashd 'getblock'.
282+
//
283+
// First try verbose=3, which returns a JSON object with exactly
284+
// what's needed: the block raw hex and the list of txids.
285+
//
286+
// If unsuccessful (zcashd isn't upgraded),
287+
// fetch the block using the verbose option ("1") because it provides
288+
// both the list of txids and the block hash (block ID), which we then
289+
// use to fetch the raw data format of the same block. Don't fetch
282290
// by height in case a reorg occurs between the two getblock calls;
283291
// using block hash ensures that we're fetching the same block.
284-
params[1] = json.RawMessage("1")
292+
params[1] = json.RawMessage("3")
285293
result, rpcErr := RawRequest("getblock", params)
286294
if rpcErr != nil {
287295
// Check to see if we are requesting a height the zcashd doesn't have yet
288296
if (strings.Split(rpcErr.Error(), ":"))[0] == "-8" {
289297
return nil, nil
290298
}
291-
return nil, errors.Wrap(rpcErr, "error requesting verbose block")
299+
// zcashd must not be upgraded to support verbose=3
300+
params[1] = json.RawMessage("1")
301+
result, rpcErr = RawRequest("getblock", params)
302+
if rpcErr != nil {
303+
// Check to see if we are requesting a height the zcashd doesn't have yet
304+
if (strings.Split(rpcErr.Error(), ":"))[0] == "-8" {
305+
return nil, nil
306+
}
307+
return nil, errors.Wrap(rpcErr, "error requesting verbose block")
308+
}
292309
}
310+
// This type works for both the verbose=1 or 3 reply.
293311
var block1 ZcashRpcReplyGetblock1
294312
err = json.Unmarshal(result, &block1)
295313
if err != nil {
@@ -299,26 +317,26 @@ func getBlockFromRPC(height int) (*walletrpc.CompactBlock, error) {
299317
if err != nil {
300318
Log.Fatal("getBlockFromRPC bad block hash", block1.Hash)
301319
}
302-
params[0] = blockHash
303-
params[1] = json.RawMessage("0") // non-verbose (raw hex)
304-
result, rpcErr = RawRequest("getblock", params)
320+
if block1.Hex == "" {
321+
// the getblock verbose=3 attempt must have failed, get the raw hex now
322+
params[0] = blockHash
323+
params[1] = json.RawMessage("0") // non-verbose (raw hex, non-JSON)
324+
result, rpcErr = RawRequest("getblock", params)
305325

306-
// For some reason, the error responses are not JSON
307-
if rpcErr != nil {
308-
return nil, errors.Wrap(rpcErr, "error requesting block")
309-
}
326+
// For some reason, the error responses are not JSON
327+
if rpcErr != nil {
328+
return nil, errors.Wrap(rpcErr, "error requesting block")
329+
}
310330

311-
var blockDataHex string
312-
err = json.Unmarshal(result, &blockDataHex)
313-
if err != nil {
314-
return nil, errors.Wrap(err, "error reading JSON response")
331+
err = json.Unmarshal(result, &block1.Hex)
332+
if err != nil {
333+
return nil, errors.Wrap(err, "error reading JSON response")
334+
}
315335
}
316-
317-
blockData, err := hex.DecodeString(blockDataHex)
336+
blockData, err := hex.DecodeString(block1.Hex)
318337
if err != nil {
319-
return nil, errors.Wrap(err, "error decoding getblock output")
338+
return nil, errors.Wrap(err, "error decoding getblock raw block hex string")
320339
}
321-
322340
block := parser.NewBlock()
323341
rest, err := block.ParseFromSlice(blockData)
324342
if err != nil {

0 commit comments

Comments
 (0)