-
Notifications
You must be signed in to change notification settings - Fork 324
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
[util] implement block time calculator #3951
Conversation
return &BlockTimeCalculator{ | ||
getBlockInterval: getBlockInterval, | ||
getTipHeight: getTipHeight, | ||
getHistoryBlockTime: getHistoryBlockTime, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
check nil for 3 input funcs
} | ||
|
||
getBlockIntervalFn func(uint64) time.Duration | ||
getTipHeightFn func() uint64 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: below get block time func may return error, should define getTipHeightFn (uint64, error)
as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently, the method TipHeight() uint64
of the blockchain does not return an error. We can keep it this way.
|
||
getBlockIntervalFn func(uint64) time.Duration | ||
getTipHeightFn func() uint64 | ||
getHistoryblockTimeFn func(uint64) (time.Time, error) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think call it getBlockTimeFn
is good and clear enough?
if err != nil { | ||
return time.Time{}, err | ||
} | ||
return tipBlockTime.Add(time.Duration(height-tipHeight) * btc.getBlockInterval(height)), nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
height
is in the future, so should usebtc.getBlockInterval(tipHeight)
- should check possible overflow of the result,
time.Duration
isint64
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For 2, If it overflows, it means it will be about 300 years later, so there is no need to consider
Codecov Report
@@ Coverage Diff @@
## master #3951 +/- ##
==========================================
+ Coverage 75.38% 76.18% +0.80%
==========================================
Files 303 329 +26
Lines 25923 28005 +2082
==========================================
+ Hits 19541 21335 +1794
- Misses 5360 5572 +212
- Partials 1022 1098 +76
... and 3 files with indirect coverage changes 📣 Codecov offers a browser extension for seamless coverage viewing on GitHub. Try it in Chrome or Firefox today! |
Kudos, SonarCloud Quality Gate passed! 0 Bugs No Coverage information |
Description
There are several reasons for introcuding block time calculator:
It calculates the corresponding time for a given height. It supports accurate querying of historical blocks as well as prediction for future blocks. The prediction for future blocks is calculated based on the latest height and block interval.
Fixes #(issue)
Type of change
Please delete options that are not relevant.
How Has This Been Tested?
Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration
Test Configuration:
Checklist: