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

Add epochCeiling function to Primitive.Types. #1114

Merged
merged 6 commits into from
Dec 5, 2019

Conversation

jonathanknowles
Copy link
Member

@jonathanknowles jonathanknowles commented Dec 4, 2019

Issue Number

#1086

Overview

This PR:

  • Adds the functions epoch{Ceiling,Floor}
    • epochCeiling calculates the earliest EpochNo whose start time is greater than or equal to the specified time. (Required by issue Create epochCeiling function. #1086.)
    • epochFloor calculates the latest EpochNo whose start time is less than or equal to the specified time. (Added for completeness.)
  • Adds the epochStartTime and epoch{Pred,Succ} helper functions.
  • Adds a set of property tests for the above.

Comments

The functions added by this PR follow the same general pattern as the following existing functions:

  • slot{Ceiling,Floor}
  • slot{Pred,Succ}
  • slotStartTime

| t > timeMax = Just maxBound
| otherwise = epochNumber <$> slotFloor sps t
where
timeMin = epochStartTime sps minBound
Copy link
Contributor

Choose a reason for hiding this comment

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

[suggestion]

   timeMin = epochStartTime sps minBound
    timeMax = epochStartTime sps maxBound

is also in lines 1134-5 - maybe it is good idea to not duplicate this and put outside where once?

Copy link
Member Author

Choose a reason for hiding this comment

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

Well spotted!

I was thinking about doing this. However, I'm not (yet) convinced that it would actually help us much. Let's suppose we did define the following functions:

-- | Calculates the start time of the earliest representable epoch.
epochStartTimeMinBound :: SlotParameters -> UTCTime
epochStartTimeMinBound = flip epochStartTime minBound

--- | Calculates the start time of the latest representable epoch.
epochStartTimeMaxBound :: SlotParameters -> UTCTime
epochStartTimeMaxBound = flip epochStartTime maxBound

We'd then be able to replace all instances of:

epochStartTime sps minBound

With:

epochStartTimeMinBound sps

Do you think this would be worth it?

(I'm trying to think of a compelling reason to convince myself. Feel free to suggest a reason if I've missed something obvious!)

checkCoverage $
cover 10 withinBounds "within bounds" $
cover 10 (not withinBounds) "out of bounds" $
expectedResult === predN n (succN n $ Just epoch)
Copy link
Contributor

@paweljakubas paweljakubas Dec 4, 2019

Choose a reason for hiding this comment

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

[suggestion]

Wouldn't be better to add properties in those two boundary cases where we apply n times epochPred and n times epochSucc but randomly picked (rather than one special case n times epochPred and later timesepochSucc`) and also require certain coverage of out-of-bounds cases?

Copy link
Member Author

Choose a reason for hiding this comment

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

[suggestion]

Wouldn't be better to add properties in those two boundary cases where we apply n times epochPred and n times epochSucc but randomly picked (rather than one special case n times epochPred and later timesepochSucc`) and also require certain coverage of out-of-bounds cases?

I'm not sure I understand what you mean here. Would you be able to give an example?

Copy link
Contributor

@paweljakubas paweljakubas left a comment

Choose a reason for hiding this comment

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

Very nice, comprehensive testing! Left some small remarks/questions

This function computes the time at which an epoch starts.
This enables generation of arbitrary slot parameters together with an arbitrary
point in time, where the point in time falls into one of three categories:

  1. occurs during the lifetime of the blockchain;
  2. occurs before the earliest representable slot;
  3. occurs after the latest representable slot.
These functions find the predecessor and successor of an epoch,
respectively.
@jonathanknowles jonathanknowles force-pushed the jonathanknowles/epoch-ceiling-floor branch from 4294df7 to 93d713f Compare December 5, 2019 02:45
@jonathanknowles
Copy link
Member Author

bors r+

iohk-bors bot added a commit that referenced this pull request Dec 5, 2019
1114: Add `epochCeiling` function to `Primitive.Types`. r=jonathanknowles a=jonathanknowles

# Issue Number

#1086 

# Overview

This PR:

- [x] Adds the functions `epoch{Ceiling,Floor}`
  - `epochCeiling` calculates the _earliest_ `EpochNo` whose start time is _greater than or equal to_ the specified time. (Required by issue #1086.)
  - `epochFloor`  calculates the _latest_ `EpochNo` whose start time is _less than or equal to_ the specified time. (Added for completeness.)
- [x] Adds the `epochStartTime` and `epoch{Pred,Succ}` helper functions.
- [x] Adds a set of property tests for the above.

# Comments

The functions added by this PR follow the same general pattern as the following existing functions:
* `slot{Ceiling,Floor}`
* `slot{Pred,Succ}`
* `slotStartTime`

Co-authored-by: Jonathan Knowles <jonathan.knowles@iohk.io>
@iohk-bors
Copy link
Contributor

iohk-bors bot commented Dec 5, 2019

Build failed

@jonathanknowles
Copy link
Member Author

bors r+

iohk-bors bot added a commit that referenced this pull request Dec 5, 2019
1114: Add `epochCeiling` function to `Primitive.Types`. r=jonathanknowles a=jonathanknowles

# Issue Number

#1086 

# Overview

This PR:

- [x] Adds the functions `epoch{Ceiling,Floor}`
  - `epochCeiling` calculates the _earliest_ `EpochNo` whose start time is _greater than or equal to_ the specified time. (Required by issue #1086.)
  - `epochFloor`  calculates the _latest_ `EpochNo` whose start time is _less than or equal to_ the specified time. (Added for completeness.)
- [x] Adds the `epochStartTime` and `epoch{Pred,Succ}` helper functions.
- [x] Adds a set of property tests for the above.

# Comments

The functions added by this PR follow the same general pattern as the following existing functions:
* `slot{Ceiling,Floor}`
* `slot{Pred,Succ}`
* `slotStartTime`

Co-authored-by: Jonathan Knowles <jonathan.knowles@iohk.io>
@iohk-bors
Copy link
Contributor

iohk-bors bot commented Dec 5, 2019

Build succeeded

@iohk-bors iohk-bors bot merged commit 93d713f into master Dec 5, 2019
@jonathanknowles
Copy link
Member Author

@paweljakubas

Thanks for your review, and very helpful feedback! I've added replies to your comments.

I have some further changes to make in response to your feedback, and will add those changes in PR #1121 (so they can be reviewed separately).

@jonathanknowles jonathanknowles deleted the jonathanknowles/epoch-ceiling-floor branch December 5, 2019 07:41
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.

3 participants