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

Don't restore from storage after reboot #50

Merged
merged 2 commits into from Dec 6, 2018
Merged

Don't restore from storage after reboot #50

merged 2 commits into from Dec 6, 2018

Conversation

Reflejo
Copy link
Contributor

@Reflejo Reflejo commented Dec 5, 2018

If the device clock changes after reboot before Kronos is updated, the "trusted" time will be wrong since we'll apply the offset to the wrong ground time. This breaks the "trusted" guarantee.

@@ -26,8 +26,20 @@ class TimeStorageTests: XCTestCase {
var storage = TimeStorage(storagePolicy: .standard)
let sampleFreeze = TimeFreeze(offset: 5000.32423)
storage.stableTime = sampleFreeze

Copy link
Contributor

Choose a reason for hiding this comment

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

is this intentional?

Copy link
Contributor Author

@Reflejo Reflejo Dec 5, 2018

Choose a reason for hiding this comment

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

Yea; letting it breathe a bit ;)

@@ -10,7 +10,7 @@ struct TimeFreeze {
private let offset: TimeInterval

/// The stable timestamp adjusted by the most acurrate offset known so far.
var adjustedTimestamp: TimeInterval? {
var adjustedTimestamp: TimeInterval {
Copy link
Contributor

Choose a reason for hiding this comment

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

surprised this doesn't cause build failures, shouldn't making this non-optional also require us to remove some optional chaining somewhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's just a coincidence this didn't break anything. Is because we are using it on an optional chaining such as self.whatever?.adjustedTimestamp that as a whole returns an optional regardless

func testRetrievingTimeFreezeAfterReboot() {
let sampleFreeze = TimeFreeze(offset: 5000.32423)
var storedData = sampleFreeze.toDictionary()
storedData["Uptime"] = storedData["Uptime"]! + 10
Copy link
Contributor

Choose a reason for hiding this comment

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

+1 for liberally using ! in tests

@ryanrhee
Copy link
Contributor

ryanrhee commented Dec 5, 2018

What are your thoughts on making this a configurable option? Aka have a way to configure Kronos to either return nil or try best-effort in the case where we restore from storage after reboot.

@craffert0
Copy link
Contributor

I'm indifferent to whether you merge this or not. However, I'd just like to explain why we did this in the first place.

The system clock cannot be trusted because the user might be doing all sorts of weird things, like setting it to the wrong time zone, or purposely setting it nine minutes forward to keep themselves on time.

However, the assumption that this is making is that the weird offset of the system clock is going to be the same between reboots. If that is the case, then the existing code guesses right. The phone that's always nine minutes fast will work for this code.

Obviously the tradeoff is that we could be wrong, and nil is a safer bet.

I just wanted to give the reasoning from last year.

@ryanrhee
Copy link
Contributor

ryanrhee commented Dec 5, 2018

I'll add that I'm also indifferent, although I think we should at least consider whether it makes sense to make this a configurable option in Kronos.

@Reflejo
Copy link
Contributor Author

Reflejo commented Dec 5, 2018

@craffert0 Thanks for the context; that's what I assumed. I think as a library, the contract of giving a trustworthy clock or nothing is very powerful and one I wish we could keep. Hence why I think this change will be beneficial on the accuracy side.

Now I understand that most of the times not returning nil will still return an accurate estimation but my stance is that most might not be enough

@ryanrhee
Copy link
Contributor

ryanrhee commented Dec 5, 2018

FYI Leo is ok with this change on Android as well so we can go forward. I still think making this configurable on Kronos is the best answer in terms of this PR, but am also OK with landing this as-is.

If the device clock changes after reboot before Kronos is updated, the
"trusted" time will be wrong since we'll apply the offset to the wrong
ground time. This breaks the "trusted" guarantee.
@Reflejo Reflejo merged commit 2f7b48a into master Dec 6, 2018
@Reflejo Reflejo deleted the fix-reboot branch December 6, 2018 06:08
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.

None yet

3 participants