File tree Expand file tree Collapse file tree 2 files changed +31
-9
lines changed Expand file tree Collapse file tree 2 files changed +31
-9
lines changed Original file line number Diff line number Diff line change @@ -88,6 +88,7 @@ type BlockFetch struct {
88
88
89
89
type Config struct {
90
90
BlockFunc BlockFunc
91
+ BlockRawFunc BlockRawFunc
91
92
BatchDoneFunc BatchDoneFunc
92
93
RequestRangeFunc RequestRangeFunc
93
94
BatchStartTimeout time.Duration
@@ -103,6 +104,7 @@ type CallbackContext struct {
103
104
104
105
// Callback function types
105
106
type BlockFunc func (CallbackContext , uint , ledger.Block ) error
107
+ type BlockRawFunc func (CallbackContext , uint , []byte ) error
106
108
type BatchDoneFunc func (CallbackContext ) error
107
109
type RequestRangeFunc func (CallbackContext , common.Point , common.Point ) error
108
110
@@ -134,6 +136,12 @@ func WithBlockFunc(blockFunc BlockFunc) BlockFetchOptionFunc {
134
136
}
135
137
}
136
138
139
+ func WithBlockRawFunc (blockRawFunc BlockRawFunc ) BlockFetchOptionFunc {
140
+ return func (c * Config ) {
141
+ c .BlockRawFunc = blockRawFunc
142
+ }
143
+ }
144
+
137
145
func WithBatchDoneFunc (batchDoneFunc BatchDoneFunc ) BlockFetchOptionFunc {
138
146
return func (c * Config ) {
139
147
c .BatchDoneFunc = batchDoneFunc
Original file line number Diff line number Diff line change @@ -254,12 +254,16 @@ func (c *Client) handleBlock(msgGeneric protocol.Message) error {
254
254
if _ , err := cbor .Decode (msg .WrappedBlock , & wrappedBlock ); err != nil {
255
255
return fmt .Errorf ("%s: decode error: %s" , ProtocolName , err )
256
256
}
257
- blk , err := ledger .NewBlockFromCbor (
258
- wrappedBlock .Type ,
259
- wrappedBlock .RawBlock ,
260
- )
261
- if err != nil {
262
- return err
257
+ var block ledger.Block
258
+ if ! c .blockUseCallback || c .config .BlockFunc != nil {
259
+ var err error
260
+ block , err = ledger .NewBlockFromCbor (
261
+ wrappedBlock .Type ,
262
+ wrappedBlock .RawBlock ,
263
+ )
264
+ if err != nil {
265
+ return err
266
+ }
263
267
}
264
268
// Check for shutdown
265
269
select {
@@ -269,11 +273,21 @@ func (c *Client) handleBlock(msgGeneric protocol.Message) error {
269
273
}
270
274
// We use the callback when requesting ranges and the internal channel for a single block
271
275
if c .blockUseCallback {
272
- if err := c .config .BlockFunc (c .callbackContext , wrappedBlock .Type , blk ); err != nil {
273
- return err
276
+ if c .config .BlockRawFunc != nil {
277
+ if err := c .config .BlockRawFunc (c .callbackContext , wrappedBlock .Type , wrappedBlock .RawBlock ); err != nil {
278
+ return err
279
+ }
280
+ } else if c .config .BlockFunc != nil {
281
+ if err := c .config .BlockFunc (c .callbackContext , wrappedBlock .Type , block ); err != nil {
282
+ return err
283
+ }
284
+ } else {
285
+ return fmt .Errorf (
286
+ "received block-fetch Block message but no callback function is defined" ,
287
+ )
274
288
}
275
289
} else {
276
- c .blockChan <- blk
290
+ c .blockChan <- block
277
291
}
278
292
return nil
279
293
}
You can’t perform that action at this time.
0 commit comments