Skip to content

Commit 10745c9

Browse files
authored
feat: optional blockfetch callback for raw block (#828)
1 parent 861cf60 commit 10745c9

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

protocol/blockfetch/blockfetch.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ type BlockFetch struct {
8888

8989
type Config struct {
9090
BlockFunc BlockFunc
91+
BlockRawFunc BlockRawFunc
9192
BatchDoneFunc BatchDoneFunc
9293
RequestRangeFunc RequestRangeFunc
9394
BatchStartTimeout time.Duration
@@ -103,6 +104,7 @@ type CallbackContext struct {
103104

104105
// Callback function types
105106
type BlockFunc func(CallbackContext, uint, ledger.Block) error
107+
type BlockRawFunc func(CallbackContext, uint, []byte) error
106108
type BatchDoneFunc func(CallbackContext) error
107109
type RequestRangeFunc func(CallbackContext, common.Point, common.Point) error
108110

@@ -134,6 +136,12 @@ func WithBlockFunc(blockFunc BlockFunc) BlockFetchOptionFunc {
134136
}
135137
}
136138

139+
func WithBlockRawFunc(blockRawFunc BlockRawFunc) BlockFetchOptionFunc {
140+
return func(c *Config) {
141+
c.BlockRawFunc = blockRawFunc
142+
}
143+
}
144+
137145
func WithBatchDoneFunc(batchDoneFunc BatchDoneFunc) BlockFetchOptionFunc {
138146
return func(c *Config) {
139147
c.BatchDoneFunc = batchDoneFunc

protocol/blockfetch/client.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -254,12 +254,16 @@ func (c *Client) handleBlock(msgGeneric protocol.Message) error {
254254
if _, err := cbor.Decode(msg.WrappedBlock, &wrappedBlock); err != nil {
255255
return fmt.Errorf("%s: decode error: %s", ProtocolName, err)
256256
}
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+
}
263267
}
264268
// Check for shutdown
265269
select {
@@ -269,11 +273,21 @@ func (c *Client) handleBlock(msgGeneric protocol.Message) error {
269273
}
270274
// We use the callback when requesting ranges and the internal channel for a single block
271275
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+
)
274288
}
275289
} else {
276-
c.blockChan <- blk
290+
c.blockChan <- block
277291
}
278292
return nil
279293
}

0 commit comments

Comments
 (0)