-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #277 from input-output-hk/nc/linearEpochInfo
Add `unsafeLinearExtendEpochInfo`.
- Loading branch information
Showing
7 changed files
with
180 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
module Cardano.Slotting.EpochInfo.Extend where | ||
|
||
import Cardano.Slotting.EpochInfo.API (EpochInfo (..)) | ||
import Cardano.Slotting.Slot (EpochNo (EpochNo), EpochSize (EpochSize), SlotNo (SlotNo)) | ||
import Cardano.Slotting.Time | ||
( SlotLength (getSlotLength), | ||
addRelativeTime, | ||
multNominalDiffTime, | ||
) | ||
|
||
-- | Given a basis point, use it and its slot length to impute a linear | ||
-- relationship between slots and time in order to extend an 'EpochInfo' to | ||
-- infinity. | ||
-- | ||
-- The returned `EpochInfo` may still fail (according to the semantics of the | ||
-- specified monad) if any of the underlying operations fail. For example, if we | ||
-- cannot translate the basis point. | ||
unsafeLinearExtendEpochInfo :: | ||
Monad m => | ||
SlotNo -> | ||
EpochInfo m -> | ||
EpochInfo m | ||
unsafeLinearExtendEpochInfo basisSlot underlyingEI = | ||
let lastKnownEpochM = epochInfoEpoch_ underlyingEI basisSlot | ||
|
||
goSize = \en -> do | ||
lke <- lastKnownEpochM | ||
if en <= lke | ||
then epochInfoSize_ underlyingEI en | ||
else epochInfoSize_ underlyingEI lke | ||
goFirst = \en -> do | ||
lke <- lastKnownEpochM | ||
if en <= lke | ||
then epochInfoFirst_ underlyingEI en | ||
else do | ||
SlotNo lkeStart <- epochInfoFirst_ underlyingEI lke | ||
EpochSize sz <- epochInfoSize_ underlyingEI en | ||
let EpochNo numEpochs = en - lke | ||
pure . SlotNo $ lkeStart + (numEpochs * sz) | ||
goEpoch = \sn -> | ||
if sn <= basisSlot | ||
then epochInfoEpoch_ underlyingEI sn | ||
else do | ||
lke <- lastKnownEpochM | ||
lkeStart <- epochInfoFirst_ underlyingEI lke | ||
EpochSize sz <- epochInfoSize_ underlyingEI lke | ||
let SlotNo slotsForward = sn - lkeStart | ||
pure . (lke +) . EpochNo $ slotsForward `div` sz | ||
goTime = \sn -> | ||
if sn <= basisSlot | ||
then epochInfoSlotToRelativeTime_ underlyingEI sn | ||
else do | ||
let SlotNo slotDiff = sn - basisSlot | ||
|
||
a1 <- epochInfoSlotToRelativeTime_ underlyingEI basisSlot | ||
lgth <- epochInfoSlotLength_ underlyingEI basisSlot | ||
|
||
pure $ | ||
addRelativeTime | ||
(multNominalDiffTime (getSlotLength lgth) slotDiff) | ||
a1 | ||
goLength = \sn -> | ||
if sn <= basisSlot | ||
then epochInfoSlotLength_ underlyingEI sn | ||
else epochInfoSlotLength_ underlyingEI basisSlot | ||
in EpochInfo | ||
{ epochInfoSize_ = goSize, | ||
epochInfoFirst_ = goFirst, | ||
epochInfoEpoch_ = goEpoch, | ||
epochInfoSlotToRelativeTime_ = goTime, | ||
epochInfoSlotLength_ = goLength | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import Test.Tasty | ||
import Test.Cardano.Slotting.EpochInfo (epochInfoTests) | ||
|
||
main :: IO () | ||
main = defaultMain tests | ||
|
||
tests :: TestTree | ||
tests = testGroup "EpochInfo" [epochInfoTests] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
module Test.Cardano.Slotting.EpochInfo where | ||
|
||
import Cardano.Slotting.EpochInfo.API (EpochInfo (..)) | ||
import Cardano.Slotting.EpochInfo.Extend (unsafeLinearExtendEpochInfo) | ||
import Cardano.Slotting.EpochInfo.Impl (fixedEpochInfo) | ||
import Cardano.Slotting.Slot (EpochNo (EpochNo), EpochSize (EpochSize), SlotNo (SlotNo)) | ||
import Cardano.Slotting.Time (slotLengthFromSec) | ||
import Data.Functor.Identity (Identity) | ||
import Test.Tasty (TestTree, testGroup) | ||
import Test.Tasty.QuickCheck as QC | ||
( Arbitrary (arbitrary), | ||
choose, | ||
testProperty, | ||
(===), | ||
) | ||
|
||
baseEpochInfo :: EpochInfo Identity | ||
baseEpochInfo = fixedEpochInfo (EpochSize 10) (slotLengthFromSec 10) | ||
|
||
-- An extended epoch info from a fixedEpochInfo should act as identity. | ||
extendedEpochInfo :: SlotNo -> EpochInfo Identity | ||
extendedEpochInfo sn = unsafeLinearExtendEpochInfo sn baseEpochInfo | ||
|
||
newtype TestSlotNo = TestSlotNo SlotNo | ||
deriving (Eq, Show) | ||
|
||
instance Arbitrary TestSlotNo where | ||
arbitrary = TestSlotNo . SlotNo <$> choose (1, 200) | ||
|
||
newtype TestEpochNo = TestEpochNo EpochNo | ||
deriving (Eq, Show) | ||
|
||
instance Arbitrary TestEpochNo where | ||
arbitrary = TestEpochNo . EpochNo <$> choose (0, 20) | ||
|
||
epochInfoTests :: TestTree | ||
epochInfoTests = | ||
testGroup | ||
"linearExtend" | ||
[ QC.testProperty "epochSize matches" $ \(TestSlotNo basisSlot, TestEpochNo sn) -> | ||
epochInfoSize_ baseEpochInfo sn === epochInfoSize_ (extendedEpochInfo basisSlot) sn, | ||
QC.testProperty "epochFirst matches" $ \(TestSlotNo basisSlot, TestEpochNo sn) -> | ||
epochInfoFirst_ baseEpochInfo sn === epochInfoFirst_ (extendedEpochInfo basisSlot) sn, | ||
QC.testProperty "epochEpoch matches" $ \(TestSlotNo basisSlot, TestSlotNo sn) -> | ||
epochInfoEpoch_ baseEpochInfo sn === epochInfoEpoch_ (extendedEpochInfo basisSlot) sn, | ||
QC.testProperty "epochTime matches" $ \(TestSlotNo basisSlot, TestSlotNo sn) -> | ||
epochInfoSlotToRelativeTime_ baseEpochInfo sn | ||
=== epochInfoSlotToRelativeTime_ (extendedEpochInfo basisSlot) sn | ||
] |