@@ -153,6 +153,7 @@ type (
153
153
ZcashRpcReplyGetblock1 struct {
154
154
Hash string
155
155
Tx []string
156
+ Hex string
156
157
Trees struct {
157
158
Sapling struct {
158
159
Size uint32
@@ -275,21 +276,38 @@ func getBlockFromRPC(height int) (*walletrpc.CompactBlock, error) {
275
276
}
276
277
params := make ([]json.RawMessage , 2 )
277
278
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
282
290
// by height in case a reorg occurs between the two getblock calls;
283
291
// using block hash ensures that we're fetching the same block.
284
- params [1 ] = json .RawMessage ("1 " )
292
+ params [1 ] = json .RawMessage ("3 " )
285
293
result , rpcErr := RawRequest ("getblock" , params )
286
294
if rpcErr != nil {
287
295
// Check to see if we are requesting a height the zcashd doesn't have yet
288
296
if (strings .Split (rpcErr .Error (), ":" ))[0 ] == "-8" {
289
297
return nil , nil
290
298
}
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
+ }
292
309
}
310
+ // This type works for both the verbose=1 or 3 reply.
293
311
var block1 ZcashRpcReplyGetblock1
294
312
err = json .Unmarshal (result , & block1 )
295
313
if err != nil {
@@ -299,26 +317,26 @@ func getBlockFromRPC(height int) (*walletrpc.CompactBlock, error) {
299
317
if err != nil {
300
318
Log .Fatal ("getBlockFromRPC bad block hash" , block1 .Hash )
301
319
}
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 )
305
325
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
+ }
310
330
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
+ }
315
335
}
316
-
317
- blockData , err := hex .DecodeString (blockDataHex )
336
+ blockData , err := hex .DecodeString (block1 .Hex )
318
337
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 " )
320
339
}
321
-
322
340
block := parser .NewBlock ()
323
341
rest , err := block .ParseFromSlice (blockData )
324
342
if err != nil {
0 commit comments