-
Notifications
You must be signed in to change notification settings - Fork 37.7k
refactor address relay time #24697
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
refactor address relay time #24697
The head ref may contain hidden characters: "2203-refactor-addr-relay-time-\u{1F326}"
Conversation
fa34ccf
to
fa0e4eb
Compare
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. ConflictsReviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. |
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.
Code Review ACK fa0e4eb
As far as I can see all refactors are correct, and I agree that it makes sense to use std::chrono
. I verified that the log message is unchanged.
I guess I'm not entirely seeing the point of this? If we're trying to keep AdjustedTime, shouldn't we have a EDIT: I suppose you're not really doing much more than that anyway... Would be nice to have a motivation in the PR description I guess; the code changes don't really seem obviously better to me. |
fa0e4eb
to
fadf5a7
Compare
I assume that adjusted time will be removed from addrman (see concept acks in the conflicting pull).
Sure that is possible, but I don't see where it could make sense.
I've added some text to each commit to motivate it, no code changes. Can be checked with
There are four commit, and I am happy to drop any or all of them. |
Could have accessors that convert to chrono, or convert from chrono to uint32_t when serializing (and back when deserializing). [EDIT: can do the conversion when de/serializing as something like: READWRITECONVERT(obj.chrono_time, uint32_t,
[](const auto& t) { return static_cast<uint32_t>(count_seconds(t)); },
[](const uint32_t t) { return std::chrono::seconds{t}; }
); with #define READWRITECONVERT(obj, type, from, to) (::ReadWriteConvert<type>(s, ser_action, obj, from, to))
template<typename X, typename From, typename To, typename Y, typename Stream> void ReadWriteConvert(Stream& s, CSerActionSerialize ser_action, const Y& obj, From from, To)
{
X dummy{from(obj)};
::Serialize(s, dummy);
}
template<typename X, typename From, typename To, typename Y, typename Stream> void ReadWriteConvert(Stream& s, CSerActionUnserialize ser_action, Y& obj, From, To to)
{
X dummy;
::Unserialize(s, dummy);
obj = to(dummy);
} ] |
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.
ACK fadf5a7
I think the advantage of using std::chrono is type checking and brevity/simplicity -- being able to say "now + 2h" and have the compiler ensure that everything's using seconds or microseconds or whatever correctly.
So I think it's a problem if we're having to do a lot of manual conversions (via std::chrono::seconds{nTime}
and count_seconds(now)
), especially doing them in the middle of expressions (eg pinfo->nTime < addr.nTime - count_seconds(update_interval) - nTimePenalty
). Most of the type changes here are making the code longer and harder to read, rather than shorter and simpler, for instance...
That's okay while we're in the middle of changing things, I guess; but I think the endgame should be that we're only manually converting to/from microseconds/seconds/hours when we're logging/serializing...
fadf5a7
to
fa62c1b
Compare
Thanks for the review. At this point it seems easier to include the commits in the conflicting pull request. |
Going to reopen this with all refactoring changes split out. Switching adjusted time to system time can then be done with a trivial scripted-diff. |
fa62c1b
to
fa01898
Compare
Skimmed the changes so far, concept ACK. |
fa01898
to
fad2021
Compare
Concept ACK, but the "Remove default time arguments" seems to also be switching from adjusted time to GetTime, which seems like a bit more than a refactor for type safety?
template<class Rep, class Period>
std::chrono::duration<Rep,Period> rand_dur(std::chrono::duration<Rep,Period> max) { ... }
template<class T>
auto rand_seconds(T max) { return rand_dur<std::chrono::seconds>(max); } |
/** ... in at least this many days */ | ||
static constexpr int64_t ADDRMAN_MIN_FAIL_DAYS{7}; | ||
/** ... in at least this duration */ | ||
static constexpr auto ADDRMAN_MIN_FAIL{7 * 24h}; |
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.
perhaps it's a good opportunity to give a better variable name here.
ADDRMAN_FAIL_DUR_UNIT
or something
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.
Sure, happy to do if people agree. Please leave a comment here or upvote glebs comment.
fa9070a
to
fad1f55
Compare
Rebased |
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.
ACK fad1f55.
I agree that using std::chrono
instead of raw ints makes the code easier to read and allows for a better type checking of timestamps.
-BEGIN VERIFY SCRIPT- ren() { sed -i "s:\<$1\>:$2:g" $(git grep -l "\<$1\>" ./src ./test); } ren nLastTry m_last_try ren nLastSuccess m_last_success ren nLastGood m_last_good ren nLastCountAttempt m_last_count_attempt ren nSinceLastTry since_last_try ren nTimePenalty time_penalty ren nUpdateInterval update_interval ren fCurrentlyOnline currently_online -END VERIFY SCRIPT-
Also, fix includes. The getter will be used in a future commit.
fad1f55
to
fa64dd6
Compare
if (nTime - info.nTime > update_interval) | ||
info.nTime = nTime; | ||
const auto update_interval{20min}; | ||
if (time - info.nTime > update_interval) { |
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.
it's unfortunate github highlights time
as it's a keyword.
@@ -990,8 +994,9 @@ int AddrManImpl::CheckAddrman() const | |||
int n = entry.first; | |||
const AddrInfo& info = entry.second; | |||
if (info.fInTried) { | |||
if (!info.m_last_success) | |||
if (!TicksSinceEpoch<std::chrono::seconds>(info.m_last_success)) { |
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'm generally not a big fan of casting 0 to bool :)
utACK fa64dd6 |
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.
Code review ACK fa64dd6
{ | ||
double fChance = 1.0; | ||
int64_t nSinceLastTry = std::max<int64_t>(nNow - nLastTry, 0); |
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 (feel free to ignore): This variable is still being renamed in the scripted diff in the next commit but it no longer exists.
fa64dd6 refactor: Use type-safe std::chrono for addrman time (MarcoFalke) fa2ae37 Add type-safe AdjustedTime() getter to timedata (MarcoFalke) fa5103a Add ChronoFormatter to serialize (MarcoFalke) fa253d3 util: Add HoursDouble (MarcoFalke) fa21fc6 scripted-diff: Rename addrman time symbols (MarcoFalke) fa9284c refactor: Remove not needed std::max (MacroFake) Pull request description: Those refactors are overlapping with, but otherwise largely unrelated to bitcoin#24662. ACKs for top commit: naumenkogs: utACK fa64dd6 dergoegge: Code review ACK fa64dd6 Tree-SHA512: a50625e78036e7220a11997e6d9b6c6b317cb38ce02b1835fb41cbee2d8bfb1faf29b29d8990be78d6b5e15e9a9d8dec33bf25fa439b47610ef708950969724b
Those refactors are overlapping with, but otherwise largely unrelated to #24662.