Skip to content

Commit

Permalink
Merge pull request #56 from mlabs-haskell/euonymos/fix-era-summaries
Browse files Browse the repository at this point in the history
Last never-forking era, get chain points query
  • Loading branch information
euonymos authored Nov 22, 2024
2 parents 65334e5 + b80892e commit e9c4270
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 18 deletions.
22 changes: 10 additions & 12 deletions emulator/src/Cardano/Node/Socket/Emulator/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ import Cardano.Node.Socket.Emulator.Types (
AppState (..),
EmulatorMsg,
clbState,
getChainPointTime,
getTip,
runClbInIO',
socketEmulatorState,
)
import Cardano.Slotting.EpochInfo (epochInfoEpoch)
import Cardano.Slotting.Slot (WithOrigin (..))
import Cardano.Slotting.Time (RelativeTime (RelativeTime), SlotLength, mkSlotLength)
import Cardano.Slotting.Time (SlotLength, mkSlotLength)
import Clb (ClbT, chainState, clbConfig, getClbConfig, getCurrentSlot, getGlobals, getStakePools, getUtxosAt)
import Clb qualified as E
import Clb.Config (ClbConfig (..))
Expand All @@ -47,7 +48,7 @@ import Data.SOP.Counting qualified as Ouroboros
import Data.SOP.NonEmpty qualified as Ouroboros
import Data.SOP.Strict (NP (Nil, (:*)), NS (S, Z))
import Data.Set qualified as Set
import Ouroboros.Consensus.Block (EpochNo (EpochNo), EpochSize, GenesisWindow (..), SlotNo (SlotNo))
import Ouroboros.Consensus.Block (EpochSize, GenesisWindow (..))
import Ouroboros.Consensus.Cardano.Block (BlockQuery (..), CardanoBlock)
import Ouroboros.Consensus.HardFork.Combinator (QueryHardFork (..))
import Ouroboros.Consensus.HardFork.Combinator qualified as Consensus
Expand All @@ -57,6 +58,7 @@ import Ouroboros.Consensus.HardFork.History.Summary (
Bound (..),
EraSummary (..),
Summary (..),
neverForksSummary,
)
import Ouroboros.Consensus.Ledger.Query (Query (..))
import Ouroboros.Consensus.Protocol.Praos (Praos)
Expand Down Expand Up @@ -122,7 +124,11 @@ handleQueryConwayEra trace state q =
let ret = At curBlockNo
logInfo $ "Tip is: " <> show ret
pure ret
GetChainPoint -> printError "Unimplemented GetChainPoint was received"
query@GetChainPoint -> do
logInfo $ "Query was received (5): " ++ show query
ret <- getChainPointTime state
logInfo $ "Chain point time is: " <> show ret
pure ret

queryIfCurrentConway ::
(block ~ Shelley.ShelleyBlock (Praos StandardCrypto) (ConwayEra StandardCrypto)) =>
Expand Down Expand Up @@ -176,7 +182,7 @@ emulatorEraHistory params = C.EraHistory (Ouroboros.mkInterpreter $ Ouroboros.su
lastS =
Ouroboros.nonEmptyHead $
Ouroboros.getSummary $
skipSummary lastEndBound emulatorEpochSize (slotLength params) emulatorGenesisWindow
neverForksSummary emulatorEpochSize (slotLength params) emulatorGenesisWindow
list = Ouroboros.Exactly $ K one :* K one :* K one :* K one :* K one :* K one :* K lastS :* Nil

-- | 'Summary' for a ledger that never forks
Expand All @@ -196,14 +202,6 @@ skipSummary endBound epochSize slotLen genesisWindow =
}
}

lastEndBound :: Bound
lastEndBound =
Bound
{ boundTime = RelativeTime 50
, boundSlot = SlotNo 1
, boundEpoch = EpochNo 500
}

emulatorGenesisWindow :: GenesisWindow
emulatorGenesisWindow = GenesisWindow window
where
Expand Down
20 changes: 15 additions & 5 deletions emulator/src/Cardano/Node/Socket/Emulator/Server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ import Cardano.Node.Socket.Emulator.Types (
txSubmissionCodec,
)
import Cardano.Slotting.Slot (SlotNo (..), WithOrigin (..))
import Clb (ValidationResult (..))
import Clb (ValidationResult (..), chainState)
import Clb qualified as E
import Clb.EmulatedLedgerState qualified as E
import Clb.Era (IsCardanoLedgerEra)
import Clb.TimeSlot (Slot)
import Clb.TimeSlot (Slot, fromSlot)
import Clb.Tx (pattern CardanoEmulatorEraTx)
import Clb.Tx qualified as E (getEmulatorEraTx)
import Control.Monad.Freer.Extras (logInfo)
Expand Down Expand Up @@ -280,6 +280,14 @@ handleCommand trace CommandChannel {ccCommand, ccResponse} mvAppState = do
atomically (readTQueue ccCommand) >>= \case
ModifySlot f -> do
s <- runClb trace mvAppState (E.modifySlot f)
-- FIXME: This is unfortunate we have to look after two ledger envs.
liftIO $ modifyMVar mvAppState $ \appState -> do
let newState =
over
(socketEmulatorState . cachedState)
(E.setSlot $ fromSlot s)
appState
pure (newState, ())
atomically $
writeTQueue ccResponse (SlotChanged s)
TryProduceBlock -> do
Expand Down Expand Up @@ -318,10 +326,13 @@ processBlock trace mvAppState =
logInfo @EmulatorMsg $ "Block is: " <> show newBlock
logInfo @EmulatorMsg "Updating chain state..."
let s = appState ^. (socketEmulatorState . clbState)
let cachedState' = appState ^. (socketEmulatorState . cachedState)
let currentSlot =
E.getSlot (appState ^. socketEmulatorState . clbState . chainState)
(_, s') <-
runStateT
( modify $
over E.chainState (const $ appState ^. (socketEmulatorState . cachedState))
over E.chainState (const (E.setSlot currentSlot cachedState'))
. over E.knownDatums (`M.union` blockDatums)
)
s
Expand Down Expand Up @@ -489,8 +500,7 @@ nextState ::
(m (ServerStNext block (Point block) Tip m ()))
)
nextState localChannel@(LocalChannel channel') = do
chainState <- ask
tip' <- getTip chainState
tip' <- getTip =<< ask
(liftIO . atomically $ tryReadTChan channel') >>= \case
Nothing -> do
Right . pure <$> do
Expand Down
25 changes: 24 additions & 1 deletion emulator/src/Cardano/Node/Socket/Emulator/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE StrictData #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeApplications #-}
Expand Down Expand Up @@ -45,6 +46,7 @@ import Clb (
ClbT,
EmulatedLedgerState,
OnChainTx (..),
chainState,
)
import Clb qualified as E
import Clb.EmulatedLedgerState qualified as E (getSlot)
Expand Down Expand Up @@ -80,6 +82,7 @@ import Data.Foldable (toList, traverse_)
import Data.Functor ((<&>))
import Data.ListMap qualified as LM
import Data.Map qualified as Map
import Data.Maybe (listToMaybe)
import Data.Text qualified as Text
import Data.Time.Clock (UTCTime)
import Data.Time.Format.ISO8601 qualified as F
Expand All @@ -106,7 +109,11 @@ import Ouroboros.Consensus.Node.NetworkProtocolVersion (
import Ouroboros.Consensus.Protocol.Praos.Header qualified as Praos
import Ouroboros.Consensus.Shelley.Eras (StandardCrypto)
import Ouroboros.Consensus.Shelley.Ledger qualified as Shelley
import Ouroboros.Network.Block (Point)
import Ouroboros.Network.Block (
Point,
pattern BlockPoint,
pattern GenesisPoint,
)
import Ouroboros.Network.Block qualified as Ouroboros
import Ouroboros.Network.NodeToClient (
NodeToClientVersion (..),
Expand Down Expand Up @@ -255,6 +262,22 @@ getChannel mv = liftIO (readMVar mv) <&> view (socketEmulatorState . channel)
getTip :: (MonadIO m) => MVar (AppState era) -> m Tip
getTip mv = liftIO (readMVar mv) <&> view (socketEmulatorState . tip)

-- Get so-called "chain point" time
getChainPointTime ::
( MonadIO m
, block ~ CardanoBlock StandardCrypto
, CBOR.ToCBOR (Core.Tx (CardanoLedgerEra era))
) =>
MVar (AppState era) ->
m (Point block)
getChainPointTime mv = do
st <- liftIO (readMVar mv)
case listToMaybe $ st ^. socketEmulatorState . chainNewestFirst of
Just newest -> do
let slot = E.getSlot $ st ^. socketEmulatorState . clbState . chainState
pure $ BlockPoint slot (coerce $ blockId newest)
Nothing -> pure GenesisPoint

-- Set the new tip
setTip :: (MonadIO m, CBOR.ToCBOR (Core.Tx (CardanoLedgerEra era))) => MVar (AppState era) -> Block era -> m ()
setTip mv block = liftIO $ modifyMVar_ mv $ \oldState -> do
Expand Down

0 comments on commit e9c4270

Please sign in to comment.