Skip to content

Commit

Permalink
update block time calculation (smartcontractkit#10576)
Browse files Browse the repository at this point in the history
* update block time calculation

* update
  • Loading branch information
infiloop2 authored Sep 11, 2023
1 parent dc3272d commit 36db6a4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

var (
defaultSampleSize = int64(200)
defaultSampleSize = int64(10000)
defaultBlockTime = time.Second * 1
)

Expand All @@ -34,39 +34,28 @@ func (r *blockTimeResolver) BlockTime(ctx context.Context, blockSampleSize int64
if err != nil {
return 0, fmt.Errorf("failed to get latest block from poller: %w", err)
}
if latest < blockSampleSize {
if latest <= blockSampleSize {
return defaultBlockTime, nil
}
blockTimes, err := r.getSampleTimestamps(ctx, blockSampleSize, latest)
start, end := latest-blockSampleSize, latest
startTime, endTime, err := r.getSampleTimestamps(ctx, uint64(start), uint64(end))
if err != nil {
return 0, err
}

var sumDiff time.Duration
for i := range blockTimes {
if i != int(blockSampleSize-1) {
sumDiff += blockTimes[i].Sub(blockTimes[i+1])
}
}

return sumDiff / time.Duration(blockSampleSize-1), nil
return endTime.Sub(startTime) / time.Duration(blockSampleSize), nil
}

func (r *blockTimeResolver) getSampleTimestamps(ctx context.Context, blockSampleSize, latest int64) ([]time.Time, error) {
blockSample := make([]uint64, blockSampleSize)
for i := range blockSample {
blockSample[i] = uint64(latest - blockSampleSize + int64(i))
}
blocks, err := r.poller.GetBlocksRange(ctx, blockSample)
func (r *blockTimeResolver) getSampleTimestamps(ctx context.Context, start, end uint64) (time.Time, time.Time, error) {
blocks, err := r.poller.GetBlocksRange(ctx, []uint64{start, end})
if err != nil {
return nil, fmt.Errorf("failed to get block range from poller: %w", err)
return time.Time{}, time.Time{}, fmt.Errorf("failed to get block range from poller: %w", err)
}
sort.Slice(blocks, func(i, j int) bool {
return blocks[i].BlockNumber > blocks[j].BlockNumber
return blocks[i].BlockNumber < blocks[j].BlockNumber
})
blockTimes := make([]time.Time, blockSampleSize)
for i, b := range blocks {
blockTimes[i] = b.BlockTimestamp
if len(blocks) < 2 {
return time.Time{}, time.Time{}, fmt.Errorf("failed to fetch blocks %d, %d from log poller", start, end)
}
return blockTimes, nil
return blocks[0].BlockTimestamp, blocks[1].BlockTimestamp, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,8 @@ func TestBlockTimeResolver_BlockTime(t *testing.T) {
20,
nil,
[]logpoller.LogPollerBlock{
{BlockTimestamp: now.Add(-time.Second * (2 * 4)), BlockNumber: 1},
{BlockTimestamp: now.Add(-time.Second * (2 * 3)), BlockNumber: 2},
{BlockTimestamp: now.Add(-time.Second * (2 * 2)), BlockNumber: 3},
{BlockTimestamp: now.Add(-time.Second * 2), BlockNumber: 4},
{BlockTimestamp: now.Add(-time.Second * (2 * 4)), BlockNumber: 16},
{BlockTimestamp: now, BlockNumber: 20},
},
nil,
2 * time.Second,
Expand Down

0 comments on commit 36db6a4

Please sign in to comment.