Skip to content

Commit

Permalink
Simplify utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexey committed Mar 25, 2023
1 parent 42247ee commit bcea6d2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 88 deletions.
28 changes: 13 additions & 15 deletions src/Internal/Contract/WaitUntilSlot.purs
Expand Up @@ -8,8 +8,8 @@ module Ctl.Internal.Contract.WaitUntilSlot
import Prelude

import Contract.Log (logTrace')
import Contract.Monad (liftContractE)
import Contract.Prelude (Either(..), Maybe(..))
import Contract.Monad (liftContractE, liftContractM)
import Contract.Prelude (Maybe(..), maybe)
import Control.Monad.Error.Class (liftEither)
import Control.Monad.Reader (asks)
import Ctl.Internal.Contract (getChainTip)
Expand All @@ -22,8 +22,8 @@ import Ctl.Internal.Types.EraSummaries (EraSummaries)
import Ctl.Internal.Types.Interval
( POSIXTime(POSIXTime)
, findSlotEraSummary
, findSlotEraSummaryOrHighest
, getSlotLength
, highestEndSlotInEraSummaries
, slotToPosixTime
)
import Ctl.Internal.Types.Natural (Natural)
Expand Down Expand Up @@ -58,24 +58,22 @@ waitUntilSlot futureSlot = do
queryHandle.getEraSummaries
>>= either (liftEffect <<< throw <<< show) pure
eraSummaries <- getEraSummaries
eraSummaryLookupResult <-
liftContractE
$ lmap (show >>> append "Can't find any Era summary slot: ")
$ findSlotEraSummaryOrHighest eraSummaries futureSlot
highestSlot <- liftContractM "Can't find any Era summary" $
highestEndSlotInEraSummaries eraSummaries
let
asSlot = BigNum.fromInt >>> wrap
toWait = case eraSummaryLookupResult of
Left { highestEndSlot }
toWait = case highestSlot of
Just highestEndSlot
| Just waitThen <- futureSlot `sub` highestEndSlot
, Just waitNow <- highestEndSlot `sub` asSlot 1 ->
{ waitNow, waitThen }
, Just waitNow <-
highestEndSlot `sub` wrap (BigNum.fromInt 1) ->
{ waitNow, waitThen: Just waitThen }
_ ->
{ waitNow: futureSlot
, waitThen: asSlot 0
, waitThen: Nothing
}
logTrace' $
"waitUntilSlot: toWait: " <> show toWait <> " " <> show
eraSummaryLookupResult
highestSlot
slotLengthMs <- map getSlotLength $ liftEither
$ lmap (const $ error "Unable to get current Era summary")
$ findSlotEraSummary eraSummaries slot
Expand Down Expand Up @@ -104,7 +102,7 @@ waitUntilSlot futureSlot = do
liftAff $ delay retryDelay
fetchRepeatedly
_ <- fetchRepeatedly
waitUntilSlot toWait.waitThen
maybe (pure tip) waitUntilSlot toWait.waitThen
Chain.TipAtGenesis -> do
-- We just retry until the tip moves from genesis
liftAff $ delay retryDelay
Expand Down
81 changes: 8 additions & 73 deletions src/Internal/Types/Interval.purs
Expand Up @@ -21,14 +21,13 @@ module Ctl.Internal.Types.Interval
, beginningOfTime
, contains
, findSlotEraSummary
, findSlotEraSummaryOrHighest
, findTimeEraSummary
, from
, genFiniteInterval
, genLowerRay
, genUpperRay
, getSlotLength
, highestSlotInEraSummaries
, highestEndSlotInEraSummaries
, hull
, intersection
, isEmpty
Expand Down Expand Up @@ -68,7 +67,7 @@ import Aeson
, partialFiniteNumber
, (.:)
)
import Contract.Prelude (foldl, fromMaybe, maximum)
import Contract.Prelude (maximum)
import Control.Monad.Error.Class (throwError)
import Control.Monad.Except.Trans (ExceptT(ExceptT), runExceptT)
import Ctl.Internal.FromData (class FromData, fromData, genericFromData)
Expand Down Expand Up @@ -108,7 +107,6 @@ import Ctl.Internal.Types.PlutusData (PlutusData(Constr))
import Ctl.Internal.Types.SystemStart (SystemStart, sysStartUnixTime)
import Data.Argonaut.Encode.Encoders (encodeString)
import Data.Array (find, head, index, length)
import Data.Array as Array
import Data.Bifunctor (bimap, lmap)
import Data.BigInt (BigInt)
import Data.BigInt (fromInt, fromNumber, fromString, toNumber) as BigInt
Expand Down Expand Up @@ -717,76 +715,13 @@ slotToPosixTime eraSummaries sysStart slot = runExceptT do
_transTime :: BigInt -> BigInt
_transTime = (*) $ BigInt.fromInt 1000

highestSlotInEraSummaries
highestEndSlotInEraSummaries
:: EraSummaries
-> Maybe Slot
highestSlotInEraSummaries (EraSummaries eraSummaries) =
maximum $ map highestMentionedSlot eraSummaries
where
highestMentionedSlot :: EraSummary -> Slot
highestMentionedSlot (EraSummary summary) =
_.slot $ unwrap $ fromMaybe summary.start summary.end

-- | Finds the `EraSummary` an `Slot` lies inside (if any).
findSlotEraSummaryOrHighest
:: EraSummaries
-> Slot -- Slot we are testing and trying to find inside `EraSummaries`
-> Either
SlotToPosixTimeError
( Either
{ summary :: EraSummary
, highestEndSlot :: Slot
}
EraSummary
)
findSlotEraSummaryOrHighest (EraSummaries eraSummaries) slot =
note (CannotFindSlotInEraSummaries slot)
$ interpretResult <<< lookup
<$> Array.uncons eraSummaries

where
interpretResult = case _ of
{ wrongSlot: Just highestEndSlot, summary } ->
Left { highestEndSlot, summary }
{ summary } ->
Right summary

lookup unsonsed =
foldl withElem (lookInSummary unsonsed.head) unsonsed.tail

slotNumber :: Slot -> BigInt
slotNumber = unwrap >>> BigNum.toBigInt

withElem
:: { wrongSlot :: Maybe Slot, summary :: EraSummary }
-> EraSummary
-> { wrongSlot :: Maybe Slot, summary :: EraSummary }
withElem found@{ wrongSlot: Nothing } _ = found
withElem acc@{ wrongSlot: Just highestSlot } summary =
case lookInSummary summary of
{ wrongSlot: Just wrongSlot }
| wrongSlot <= highestSlot -> acc
lookupResult -> lookupResult

-- Storing slot to compare and find highest, (Nothing /\ _) means "found"
lookInSummary
:: EraSummary
-> { wrongSlot :: Maybe Slot
, summary :: EraSummary
}
lookInSummary summary@(EraSummary { start, end }) =
let
aboveLowerBound =
slotNumber (unwrap start).slot <= slotNumber slot
wrongSlot =
case map unwrap end of
Just { slot: endSlot }
| aboveTheEnd <- slotNumber slot > slotNumber endSlot
, not aboveLowerBound || aboveTheEnd ->
Just endSlot
_ -> Nothing -- right summary has been found
in
{ wrongSlot, summary }
-> Maybe (Maybe Slot)
highestEndSlotInEraSummaries =
unwrap
>>> map (unwrap >>> _.end >>> map (unwrap >>> _.slot))
>>> maximum

-- | Finds the `EraSummary` an `Slot` lies inside (if any).
findSlotEraSummary
Expand Down

0 comments on commit bcea6d2

Please sign in to comment.