PollBlockChanges opens a polling loop to fetch the L1 block reference with the given label, on provided interval and with request timeout. Results are returned with provided callback fn, which may block to pause/back-pressure polling.
(ctx context.Context, lg log.Logger, src *PollingClient, fn HeadSignalFn, label rpc.BlockNumber, interval time.Duration, timeout time.Duration)
| 55 | // on provided interval and with request timeout. Results are returned with provided callback fn, |
| 56 | // which may block to pause/back-pressure polling. |
| 57 | func PollBlockChanges(ctx context.Context, lg log.Logger, src *PollingClient, fn HeadSignalFn, |
| 58 | label rpc.BlockNumber, interval time.Duration, timeout time.Duration) ethereum.Subscription { |
| 59 | return event.NewSubscription(func(quit <-chan struct{}) error { |
| 60 | if interval <= 0 { |
| 61 | lg.Warn("Polling of block is disabled", "interval", interval, "label", label) |
| 62 | <-quit |
| 63 | return nil |
| 64 | } |
| 65 | getBlockByLabel := func() { |
| 66 | reqCtx, reqCancel := context.WithTimeout(ctx, timeout) |
| 67 | ref, err := L1BlockRefByLabel(src, reqCtx, label) |
| 68 | reqCancel() |
| 69 | if err != nil { |
| 70 | lg.Warn("Failed to poll L1 block", "label", label, "err", err) |
| 71 | } else { |
| 72 | fn(ctx, ref) |
| 73 | } |
| 74 | } |
| 75 | getBlockByLabel() |
| 76 | ticker := time.NewTicker(interval) |
| 77 | defer ticker.Stop() |
| 78 | for { |
| 79 | select { |
| 80 | case <-ticker.C: |
| 81 | getBlockByLabel() |
| 82 | case <-ctx.Done(): |
| 83 | return ctx.Err() |
| 84 | case <-quit: |
| 85 | return nil |
| 86 | } |
| 87 | } |
| 88 | }) |
| 89 | } |
| 90 | |
| 91 | // L1BlockRefByLabel returns the [eth.L1BlockRef] for the given block label. |
| 92 | // Notice, we cannot cache a block reference by label because labels are not guaranteed to be unique. |
no test coverage detected