|
| 1 | +module BotPlutusInterface.TimeSlot ( |
| 2 | + TimeSlotConversionError, |
| 3 | + slotToPOSIXTimeImpl, |
| 4 | + posixTimeToSlotImpl, |
| 5 | +) where |
| 6 | + |
| 7 | +import Cardano.Ledger.Alonzo.TxInfo (slotToPOSIXTime) |
| 8 | +import Ledger qualified |
| 9 | +import Prelude |
| 10 | + |
| 11 | +import BotPlutusInterface.QueryNode (NodeInfo (NodeInfo), queryEraHistory, querySystemStart) |
| 12 | +import BotPlutusInterface.Types (PABConfig, pcNetwork, pcProtocolParams) |
| 13 | +import Cardano.Api qualified as CAPI |
| 14 | +import Cardano.Ledger.Alonzo.PParams (_protocolVersion) |
| 15 | +import Cardano.Ledger.Slot (EpochInfo) |
| 16 | +import Cardano.Slotting.EpochInfo (hoistEpochInfo) |
| 17 | +import Control.Monad.Except (runExcept) |
| 18 | +import Control.Monad.IO.Class (liftIO) |
| 19 | +import Control.Monad.Trans.Either (EitherT, firstEitherT, hoistEither, newEitherT, runEitherT) |
| 20 | +import Data.Bifunctor (first) |
| 21 | +import Data.Text (Text) |
| 22 | +import Data.Text qualified as Text |
| 23 | +import Ouroboros.Consensus.HardFork.History qualified as Consensus |
| 24 | +import System.Environment (getEnv) |
| 25 | + |
| 26 | +import Cardano.Slotting.Time (RelativeTime, toRelativeTime) |
| 27 | +import Data.Time (secondsToNominalDiffTime) |
| 28 | +import Data.Time.Clock.POSIX (posixSecondsToUTCTime) |
| 29 | +import Ouroboros.Consensus.HardFork.History.Qry qualified as HF |
| 30 | + |
| 31 | +data TimeSlotConversionError |
| 32 | + = TimeSlotConversionError !Text |
| 33 | + deriving stock (Show) |
| 34 | + |
| 35 | +slotToPOSIXTimeImpl :: PABConfig -> Ledger.Slot -> IO (Either TimeSlotConversionError Ledger.POSIXTime) |
| 36 | +slotToPOSIXTimeImpl pabConf (Ledger.Slot s) = runEitherT $ do |
| 37 | + let pparams = |
| 38 | + CAPI.toLedgerPParams |
| 39 | + CAPI.ShelleyBasedEraAlonzo -- TODO: should era be passed as an argument? |
| 40 | + (pcProtocolParams pabConf) |
| 41 | + |
| 42 | + sock <- liftIO $ getEnv "CARDANO_NODE_SOCKET_PATH" |
| 43 | + let nodeInfo = NodeInfo (pcNetwork pabConf) sock |
| 44 | + |
| 45 | + epochInfo <- toLedgerEpochInfo <$> newET (queryEraHistory nodeInfo) |
| 46 | + sysStart <- newET $ querySystemStart nodeInfo |
| 47 | + |
| 48 | + let slotNo = CAPI.SlotNo $ fromInteger s |
| 49 | + firstEitherT toError $ |
| 50 | + hoistEither $ |
| 51 | + slotToPOSIXTime pparams epochInfo sysStart slotNo |
| 52 | + |
| 53 | +toLedgerEpochInfo :: |
| 54 | + CAPI.EraHistory mode -> |
| 55 | + EpochInfo (Either CAPI.TransactionValidityError) |
| 56 | +toLedgerEpochInfo (CAPI.EraHistory _ interpreter) = |
| 57 | + hoistEpochInfo (first CAPI.TransactionValidityIntervalError . runExcept) $ |
| 58 | + Consensus.interpreterToEpochInfo interpreter |
| 59 | + |
| 60 | +posixTimeToSlotImpl :: PABConfig -> Ledger.POSIXTime -> IO (Either TimeSlotConversionError Ledger.Slot) |
| 61 | +posixTimeToSlotImpl pabConf pTime = runEitherT $ do |
| 62 | + sock <- liftIO $ getEnv "CARDANO_NODE_SOCKET_PATH" |
| 63 | + let nodeInfo = NodeInfo (pcNetwork pabConf) sock |
| 64 | + |
| 65 | + (CAPI.EraHistory _ interpreter) <- newET (queryEraHistory nodeInfo) |
| 66 | + sysStart <- newET $ querySystemStart nodeInfo |
| 67 | + |
| 68 | + let time :: RelativeTime = toRelativeTime sysStart (toUtc pTime) |
| 69 | + timeQuery = HF.wallclockToSlot time |
| 70 | + int = HF.interpretQuery interpreter timeQuery |
| 71 | + (CAPI.SlotNo s, _, _) <- firstEitherT toError $ hoistEither int |
| 72 | + |
| 73 | + return $ Ledger.Slot (toInteger s) |
| 74 | + where |
| 75 | + toUtc (Ledger.POSIXTime milliseconds) = |
| 76 | + posixSecondsToUTCTime $ |
| 77 | + secondsToNominalDiffTime |
| 78 | + (fromInteger $ milliseconds `div` 1000) -- FIXME: is it safe? |
| 79 | + |
| 80 | +newET :: Show e => IO (Either e a) -> EitherT TimeSlotConversionError IO a |
| 81 | +newET = firstEitherT toError . newEitherT |
| 82 | + |
| 83 | +toError :: Show e => e -> TimeSlotConversionError |
| 84 | +toError = TimeSlotConversionError . Text.pack . show |
0 commit comments