Skip to content
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

Use MonotonicTime as Instant shifted by 10 years forward #2322

Merged
merged 1 commit into from
Jun 29, 2023

Conversation

andrei-21
Copy link
Contributor

Such implementation allows MonotonicTime to go backward up to 10 years on all platforms. On some platforms (e.g. iOS) Instant is represented as u64 of nanoseconds since the boot of the system. Obviously such implementation does not allow to go backward before the time of the boot.
Fixes #2311.

@codecov-commenter
Copy link

codecov-commenter commented May 27, 2023

Codecov Report

Patch coverage: 81.25% and no project coverage change.

Comparison is base (bd12067) 90.32% compared to head (189b070) 90.33%.

❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2322   +/-   ##
=======================================
  Coverage   90.32%   90.33%           
=======================================
  Files         106      106           
  Lines       54951    54965   +14     
  Branches    54951    54965   +14     
=======================================
+ Hits        49633    49651   +18     
+ Misses       5318     5314    -4     
Impacted Files Coverage Δ
lightning/src/ln/outbound_payment.rs 89.47% <0.00%> (ø)
lightning/src/routing/scoring.rs 94.09% <ø> (ø)
lightning/src/util/time.rs 89.85% <86.66%> (+4.40%) ⬆️

... and 1 file with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@andrei-21 andrei-21 force-pushed the fix/instant-overflow branch 2 times, most recently from a9f5498 to 3a54cff Compare May 28, 2023 20:13
@TheBlueMatt TheBlueMatt added this to the 0.0.116 milestone May 30, 2023
@TheBlueMatt
Copy link
Collaborator

Awesome, thanks! ISTM we should simply drop the Time impl for Instant and always use MonotonicTime, no?

@andrei-21 andrei-21 marked this pull request as ready for review June 2, 2023 16:07
@andrei-21
Copy link
Contributor Author

Awesome, thanks! ISTM we should simply drop the Time impl for Instant and always use MonotonicTime, no?

Makes sense. Now Instant is only used in tests.

wpaulino
wpaulino previously approved these changes Jun 2, 2023
}

fn elapsed(&self) -> Duration {
self.0.elapsed()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this result in an underflow? Instant::elapsed uses Instant::now() - *self.

Also, note that this method is used in Writeable for ChannelLiquidity<T>.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this result in an underflow? Instant::elapsed uses Instant::now() - *self.

Ah, actually that's not a problem since you are adding -- not subtracting -- 10 years. My mistake.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you were right. I changed the implementation and added tests (the tests would fail without the fix).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol, yeah, I confused myself. The subtraction occurs in Instant::elapsed, but the test that I wrote wasn't catching the failure.

lightning/src/util/time.rs Outdated Show resolved Hide resolved
lightning/src/routing/scoring.rs Show resolved Hide resolved
@jkczyz
Copy link
Contributor

jkczyz commented Jun 27, 2023

Such implementation allows MonotonicTime to go backward up to 10 years on all platforms. On some platforms (e.g. iOS) Instant is represented as u64 of nanoseconds since the boot of the system. Obviously such implementation does not allow to go backward before the time of the boot. Fixes #2311.

I don't think this fix pertains to the system time not being monotonic. That should already be fixed in the Readable implementation for ChannelLiquidity. Instead, this fixes a problem where a platform can't represent an Instance sometime prior to the system boot. I'll push a commit adding docs to say as much.

@jkczyz
Copy link
Contributor

jkczyz commented Jun 27, 2023

Such implementation allows MonotonicTime to go backward up to 10 years on all platforms. On some platforms (e.g. iOS) Instant is represented as u64 of nanoseconds since the boot of the system. Obviously such implementation does not allow to go backward before the time of the boot. Fixes #2311.

I don't think this fix pertains to the system time not being monotonic. That should already be fixed in the Readable implementation for ChannelLiquidity. Instead, this fixes a problem where a platform can't represent an Instance sometime prior to the system boot. I'll push a commit adding docs to say as much.

Ah, you can disregard this argument. I see what you were saying, but I'll push the doc change regardless.

Copy link
Contributor

@jkczyz jkczyz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please go ahead and squash the fixup and the forthcoming test rename. Otherwise, seems good to go.

@@ -180,7 +180,10 @@ pub mod tests {
#[cfg(not(feature = "no-std"))]
fn monotonic_time_go_backward() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you name this monotonic_time_subtracts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}

fn elapsed(&self) -> Duration {
self.0.elapsed()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol, yeah, I confused myself. The subtraction occurs in Instant::elapsed, but the test that I wrote wasn't catching the failure.

Such implementation allows `MonotonicTime` to go backward up to 10
years on all platforms. On some platforms (e.g. iOS) `Instant` is
represented as `u64` of nanoseconds since the boot of the system.
Obviously such implementation does not allow to go backward before the
time of the boot.

Co-authored-by: Andrei <[email protected]>
Co-authored-by: Jeffrey Czyz <[email protected]>
jkczyz
jkczyz previously approved these changes Jun 29, 2023
lightning/src/util/time.rs Outdated Show resolved Hide resolved
lightning/src/util/time.rs Outdated Show resolved Hide resolved
@TheBlueMatt TheBlueMatt merged commit ab80fcf into lightningdevkit:main Jun 29, 2023
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: On iOS LDK panics on reading scorer
5 participants