Skip to content

Commit f2064fa

Browse files
authored
feat: block-fetch BatchDone callback function support (#791)
1 parent a307899 commit f2064fa

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
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+
BatchDoneFunc BatchDoneFunc
9192
RequestRangeFunc RequestRangeFunc
9293
BatchStartTimeout time.Duration
9394
BlockTimeout time.Duration
@@ -102,6 +103,7 @@ type CallbackContext struct {
102103

103104
// Callback function types
104105
type BlockFunc func(CallbackContext, uint, ledger.Block) error
106+
type BatchDoneFunc func(CallbackContext) error
105107
type RequestRangeFunc func(CallbackContext, common.Point, common.Point) error
106108

107109
func New(protoOptions protocol.ProtocolOptions, cfg *Config) *BlockFetch {
@@ -132,6 +134,12 @@ func WithBlockFunc(blockFunc BlockFunc) BlockFetchOptionFunc {
132134
}
133135
}
134136

137+
func WithBatchDoneFunc(batchDoneFunc BatchDoneFunc) BlockFetchOptionFunc {
138+
return func(c *Config) {
139+
c.BatchDoneFunc = batchDoneFunc
140+
}
141+
}
142+
135143
func WithRequestRangeFunc(
136144
requestRangeFunc RequestRangeFunc,
137145
) BlockFetchOptionFunc {

protocol/blockfetch/client.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,12 @@ func (c *Client) handleBatchDone() error {
283283
"role", "client",
284284
"connection_id", c.callbackContext.ConnectionId.String(),
285285
)
286+
// Notify the user if requested
287+
if c.blockUseCallback && c.config.BatchDoneFunc != nil {
288+
if err := c.config.BatchDoneFunc(c.callbackContext); err != nil {
289+
return err
290+
}
291+
}
286292
c.busyMutex.Unlock()
287293
return nil
288294
}

0 commit comments

Comments
 (0)