-
Notifications
You must be signed in to change notification settings - Fork 389
feat: block time average #5504
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: block time average #5504
Changes from all commits
2afb897
deaf6d8
340915f
f344a6f
12b6863
51d5dc4
e74fbec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,11 +18,14 @@ import ( | |
| "github.com/ethersphere/bee/v2/pkg/transaction/wrapped/cache" | ||
| ) | ||
|
|
||
| const maxAverageBlockTime = 30 * time.Second | ||
|
|
||
| var _ transaction.Backend = (*wrappedBackend)(nil) | ||
|
|
||
| type blockNumberAnchor struct { | ||
| number uint64 | ||
| timestamp time.Time | ||
| number uint64 | ||
| timestamp time.Time | ||
| averageBlockTime time.Duration | ||
| } | ||
|
|
||
| type wrappedBackend struct { | ||
|
|
@@ -86,7 +89,7 @@ func (b *wrappedBackend) BlockNumber(ctx context.Context) (uint64, error) { | |
| return elapsedBlocks < b.blockSyncInterval | ||
| } | ||
|
|
||
| loadFreshBlockFn := func() (blockNumberAnchor, error) { | ||
| loadFreshBlockFn := func(prev blockNumberAnchor) (blockNumberAnchor, error) { | ||
| b.metrics.TotalRPCCalls.Inc() | ||
| b.metrics.BlockHeaderAsBlockNumberCalls.Inc() | ||
|
|
||
|
|
@@ -99,9 +102,16 @@ func (b *wrappedBackend) BlockNumber(ctx context.Context) (uint64, error) { | |
| b.metrics.TotalRPCErrors.Inc() | ||
| return blockNumberAnchor{}, errors.New("latest block header unavailable") | ||
| } | ||
|
|
||
| newNumber := header.Number.Uint64() | ||
| newTime := time.Unix(int64(header.Time), 0).UTC() | ||
| averageBlockTime := b.computeAverageBlockTime(prev, newNumber, newTime) | ||
| b.metrics.AverageBlockTimeSeconds.Set(averageBlockTime.Seconds()) | ||
|
|
||
| return blockNumberAnchor{ | ||
| number: header.Number.Uint64(), | ||
| timestamp: time.Unix(int64(header.Time), 0).UTC(), | ||
| number: newNumber, | ||
| timestamp: newTime, | ||
| averageBlockTime: averageBlockTime, | ||
| }, nil | ||
| } | ||
|
|
||
|
|
@@ -126,10 +136,35 @@ func (b *wrappedBackend) estimatedBlockNumberWithElapsed(anchor blockNumberAncho | |
| return anchor.number, 0 | ||
| } | ||
|
|
||
| elapsedBlocks := uint64(now.Sub(anchor.timestamp) / b.blockTime) | ||
| blockTime := anchor.averageBlockTime | ||
| if blockTime == 0 { | ||
| blockTime = b.blockTime | ||
| } | ||
|
|
||
| elapsedBlocks := uint64(now.Sub(anchor.timestamp) / blockTime) | ||
| return anchor.number + elapsedBlocks, elapsedBlocks | ||
| } | ||
|
|
||
| // computeAverageBlockTime returns the observed block time between prev and the | ||
| // freshly loaded header. Falls back to the configured block time when prev is | ||
| // unset, the block number did not increase, or the header timestamp did not | ||
| // strictly increase. | ||
| func (b *wrappedBackend) computeAverageBlockTime(prev blockNumberAnchor, newNumber uint64, newTime time.Time) time.Duration { | ||
| if prev.number == 0 || newNumber <= prev.number || !newTime.After(prev.timestamp) { | ||
| return b.blockTime | ||
| } | ||
|
|
||
| elapsed := newTime.Sub(prev.timestamp) | ||
| blocks := newNumber - prev.number | ||
|
|
||
| averageBlockTime := elapsed / time.Duration(blocks) | ||
| if averageBlockTime > maxAverageBlockTime { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we also have a lower bound as
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do not think it is actually possible... |
||
| return maxAverageBlockTime | ||
| } | ||
|
|
||
| return averageBlockTime | ||
| } | ||
|
|
||
| func (b *wrappedBackend) HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) { | ||
| b.metrics.TotalRPCCalls.Inc() | ||
| b.metrics.BlockHeaderCalls.Inc() | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.