{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE NoFieldSelectors #-}

-- | In-process access to the node kernel. The RPC server reads chain
-- state, genesis and mempool straight from the running node, with no
-- Node-to-Client round trip.
module Cardano.Rpc.Server.NodeKernelAccess
  ( -- * Access handle
    Type.NodeKernelAccess
  , mkNodeKernelAccess
  , grabNodeKernelAccess

    -- * Values fixed at startup
  , nodeKernelSystemStart
  , securityParam
  , genesisConfig
  , GenesisBundle (..)

    -- * Era history
  , readHardForkSummary
  , readEraHistory
  , readCurrentEra

    -- * Chain reads
  , readChainTipHeader
  , readChainTipPoint
  , fetchBlock

    -- * Mempool
  , readMempoolTxs
  , readMempoolSnapshot
  , MempoolWatchSnapshot (..)
  , watchMempoolSnapshot
  , nextMempoolWatchSnapshot

    -- * Chain follower
  , ChainChange (..)
  , ChainFollower (..)
  , withFollower
  , withBlockFollower
  , positionFollowerAtCurrentTip
  )
where

import Cardano.Api
import Cardano.Api.Consensus qualified as Consensus
import Cardano.Rpc.Server.Internal.Monad (MonadRpc, grab)
import Cardano.Rpc.Server.Internal.TimedCache (newTimedCache)
import Cardano.Rpc.Server.Internal.Tracing
import Cardano.Rpc.Server.NodeKernelAccess.Internal.Follower
import Cardano.Rpc.Server.NodeKernelAccess.Internal.Type (GenesisBundle (..))
import Cardano.Rpc.Server.NodeKernelAccess.Internal.Type qualified as Type

import Ouroboros.Consensus.Cardano.Block (CardanoEras)
import Ouroboros.Consensus.HardFork.Combinator qualified as Consensus
  ( LedgerState (hardForkLedgerStatePerEra)
  , eraIndexFromNS
  )
import Ouroboros.Consensus.HardFork.Combinator.State qualified as HardForkState (tip)
import Ouroboros.Consensus.HardFork.History qualified as History
import Ouroboros.Consensus.Ledger.SupportsMempool qualified as Consensus (txForgetValidated)
import Ouroboros.Consensus.Mempool.API qualified as Consensus
  ( MempoolSnapshot (snapshotTxs, snapshotTxsAfter)
  , TicketNo
  , getSnapshot
  )

import RIO (atomically, throwIO)

import Control.Monad.STM (STM, check)
import Control.Tracer (Tracer, traceWith)
import Data.ByteString (ByteString)
import Data.ByteString.Lazy qualified as BSL
import Data.IORef
import Data.SOP.Strict (NP (..))
import Data.Text (pack)
import Data.Time.Clock (DiffTime)
-- Imported narrowly: grpc-spec exports an unrelated ':*' which would otherwise
-- make the 'NP' pattern match in 'readGenesisBundle' ambiguous.
import Network.GRPC.Spec (GrpcError (..), GrpcException (..))

-- | Construct 'NodeKernelAccess' from a consensus 'Consensus.NodeKernel'.
-- Returns 'Nothing' and traces the block type for non-Cardano block types.
mkNodeKernelAccess
  :: MonadIO m
  => Tracer m TraceRpc
  -- ^ Tracer for RPC events
  -> GenesisHashShelley
  -- ^ Boot-time Shelley genesis hash
  -> ShelleyGenesisFile In
  -- ^ Path to the Shelley genesis file the node was configured with
  -> Consensus.BlockType blk
  -- ^ Block type witness
  -> Consensus.NodeKernel IO addrNTN addrNTC blk
  -- ^ Consensus node kernel
  -> m (Maybe Type.NodeKernelAccess)
mkNodeKernelAccess :: forall (m :: * -> *) blk addrNTN addrNTC.
MonadIO m =>
Tracer m TraceRpc
-> GenesisHashShelley
-> ShelleyGenesisFile 'In
-> BlockType blk
-> NodeKernel IO addrNTN addrNTC blk
-> m (Maybe NodeKernelAccess)
mkNodeKernelAccess Tracer m TraceRpc
tracer GenesisHashShelley
shelleyGenesisHash ShelleyGenesisFile 'In
shelleyGenesisFile BlockType blk
blockType NodeKernel IO addrNTN addrNTC blk
kernel = case BlockType blk
blockType of
  BlockType blk
Consensus.CardanoBlockType -> do
    genesisBundle <- GenesisHashShelley
-> ShelleyGenesisFile 'In
-> TopLevelConfig (CardanoBlock StandardCrypto)
-> m GenesisBundle
forall (m :: * -> *).
MonadIO m =>
GenesisHashShelley
-> ShelleyGenesisFile 'In
-> TopLevelConfig (CardanoBlock StandardCrypto)
-> m GenesisBundle
readGenesisBundle GenesisHashShelley
shelleyGenesisHash ShelleyGenesisFile 'In
shelleyGenesisFile TopLevelConfig blk
TopLevelConfig (CardanoBlock StandardCrypto)
topLevelConfig
    pure $
      Just
        Type.NodeKernelAccess
          { Type.chainDb = chainDb
          , Type.mempool = Consensus.getMempool kernel
          , Type.systemStart = Consensus.nodeSystemStart topLevelConfig
          , Type.readHardForkSummary = readHardForkSummary'
          , Type.securityParam = Consensus.configSecurityParam topLevelConfig
          , Type.genesisConfig = genesisBundle
          }
   where
    chainDb :: ChainDB IO blk
chainDb = NodeKernel IO addrNTN addrNTC blk -> ChainDB IO blk
forall (m :: * -> *) addrNTN addrNTC blk.
NodeKernel m addrNTN addrNTC blk -> ChainDB m blk
Consensus.getChainDB NodeKernel IO addrNTN addrNTC blk
kernel
    topLevelConfig :: TopLevelConfig blk
topLevelConfig = NodeKernel IO addrNTN addrNTC blk -> TopLevelConfig blk
forall (m :: * -> *) addrNTN addrNTC blk.
NodeKernel m addrNTN addrNTC blk -> TopLevelConfig blk
Consensus.getTopLevelConfig NodeKernel IO addrNTN addrNTC blk
kernel
    ledgerConfig :: LedgerConfig blk
ledgerConfig = TopLevelConfig blk -> LedgerConfig blk
forall blk. TopLevelConfig blk -> LedgerConfig blk
Consensus.configLedger TopLevelConfig blk
topLevelConfig
    -- Primed because 'Cardano.Rpc.Server.NodeKernelAccess' also exports an
    -- accessor of the same name; this is the local action that feeds the
    -- corresponding record field above.
    --
    -- Read the current ledger state (cheap STM TVar read) and recompute
    -- the era summary on every call - O(number_of_eras).
    -- This is the same approach consensus uses for GetInterpreter queries
    -- (interpretQueryHardFork); neither path caches the summary.
    -- RunWithCachedSummary exists but is private to the blockchain time thread.
    readHardForkSummary'
      :: MonadIO n
      => n (History.Summary (CardanoEras Consensus.StandardCrypto))
    readHardForkSummary' :: forall (m :: * -> *).
MonadIO m =>
m (Summary (CardanoEras StandardCrypto))
readHardForkSummary' = do
      extLedger <- STM (ExtLedgerState (CardanoBlock StandardCrypto) EmptyMK)
-> n (ExtLedgerState (CardanoBlock StandardCrypto) EmptyMK)
forall (m :: * -> *) a. MonadIO m => STM a -> m a
atomically (STM (ExtLedgerState (CardanoBlock StandardCrypto) EmptyMK)
 -> n (ExtLedgerState (CardanoBlock StandardCrypto) EmptyMK))
-> STM (ExtLedgerState (CardanoBlock StandardCrypto) EmptyMK)
-> n (ExtLedgerState (CardanoBlock StandardCrypto) EmptyMK)
forall a b. (a -> b) -> a -> b
$ ChainDB IO blk -> STM IO (ExtLedgerState blk EmptyMK)
forall (m :: * -> *) blk.
ChainDB m blk -> STM m (ExtLedgerState blk EmptyMK)
Consensus.getCurrentLedger ChainDB IO blk
chainDb
      pure $ Consensus.hardForkSummary ledgerConfig (Consensus.ledgerState extLedger)
  BlockType blk
_ -> do
    -- unsupported block type
    Tracer m TraceRpc -> TraceRpc -> m ()
forall (m :: * -> *) a. Monad m => Tracer m a -> a -> m ()
traceWith Tracer m TraceRpc
tracer (TraceRpc -> m ()) -> (String -> TraceRpc) -> String -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TraceRpcNodeKernelAccess -> TraceRpc
forall t s. Inject t s => t -> s
inject (TraceRpcNodeKernelAccess -> TraceRpc)
-> (String -> TraceRpcNodeKernelAccess) -> String -> TraceRpc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> TraceRpcNodeKernelAccess
TraceRpcUnsupportedBlockType (Text -> TraceRpcNodeKernelAccess)
-> (String -> Text) -> String -> TraceRpcNodeKernelAccess
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
pack (String -> m ()) -> String -> m ()
forall a b. (a -> b) -> a -> b
$ BlockType blk -> String
forall a. Show a => a -> String
show BlockType blk
blockType
    Maybe NodeKernelAccess -> m (Maybe NodeKernelAccess)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe NodeKernelAccess
forall a. Maybe a
Nothing

-- | How long the resolved Shelley genesis is kept after the request that last
-- needed it: five minutes.
--
-- Long enough that a client walking through several genesis queries pays the
-- re-read once, short enough that an idle node is back to retaining nothing
-- soon after being left alone.
shelleyGenesisExpiryTimeout :: DiffTime
shelleyGenesisExpiryTimeout :: DiffTime
shelleyGenesisExpiryTimeout = DiffTime
5 DiffTime -> DiffTime -> DiffTime
forall a. Num a => a -> a -> a
* DiffTime
60

-- | Gather the network's genesis configuration out of the node kernel's ledger
-- config, so that the RPC server shares the node's own genesis values instead of
-- holding a second copy alive for the lifetime of the process.
--
-- The per-era ledger configs are matched positionally and exhaustively, so a new
-- Cardano era is a compile error here rather than a silently misread genesis.
-- The Shelley slot is matched but not read. The node only has a compacted copy
-- with the initial funds erased, so the file is the only useful source and the
-- cache reads it when a caller asks.
--
-- The only thing allocated here is that empty cache. Nothing is read from disk
-- and no thread is started.
readGenesisBundle
  :: MonadIO m
  => GenesisHashShelley
  -> ShelleyGenesisFile In
  -> Consensus.TopLevelConfig (Consensus.CardanoBlock Consensus.StandardCrypto)
  -> m GenesisBundle
readGenesisBundle :: forall (m :: * -> *).
MonadIO m =>
GenesisHashShelley
-> ShelleyGenesisFile 'In
-> TopLevelConfig (CardanoBlock StandardCrypto)
-> m GenesisBundle
readGenesisBundle GenesisHashShelley
shelleyGenesisHash ShelleyGenesisFile 'In
shelleyGenesisFile TopLevelConfig (CardanoBlock StandardCrypto)
topLevelConfig =
  case PerEraLedgerConfig (CardanoEras StandardCrypto)
-> NP WrapPartialLedgerConfig (CardanoEras StandardCrypto)
forall (xs :: [*]).
PerEraLedgerConfig xs -> NP WrapPartialLedgerConfig xs
Consensus.getPerEraLedgerConfig PerEraLedgerConfig (CardanoEras StandardCrypto)
perEraLedgerConfig of
    Consensus.WrapPartialLedgerConfig PartialLedgerConfig x
byron
      :* WrapPartialLedgerConfig x
_shelley
      :* WrapPartialLedgerConfig x
_allegra
      :* WrapPartialLedgerConfig x
_mary
      :* Consensus.WrapPartialLedgerConfig PartialLedgerConfig x
alonzo
      :* WrapPartialLedgerConfig x
_babbage
      :* Consensus.WrapPartialLedgerConfig PartialLedgerConfig x
conway
      :* WrapPartialLedgerConfig x
_dijkstra
      :* NP WrapPartialLedgerConfig xs1
Nil -> do
        shelleyGenesisCache <- DiffTime -> m (TimedCache ShelleyGenesis)
forall (m :: * -> *) a. MonadIO m => DiffTime -> m (TimedCache a)
newTimedCache DiffTime
shelleyGenesisExpiryTimeout
        pure
          GenesisBundle
            { byronConfig = Consensus.byronLedgerConfig byron
            , shelleyGenesisHash
            , shelleyGenesis = (shelleyGenesisFile, shelleyGenesisCache)
            , alonzoGenesis =
                Consensus.shelleyLedgerTranslationContext $ Consensus.shelleyLedgerConfig alonzo
            , conwayGenesis =
                Consensus.shelleyLedgerTranslationContext $ Consensus.shelleyLedgerConfig conway
            }
 where
  perEraLedgerConfig :: PerEraLedgerConfig (CardanoEras StandardCrypto)
perEraLedgerConfig = HardForkLedgerConfig (CardanoEras StandardCrypto)
-> PerEraLedgerConfig (CardanoEras StandardCrypto)
forall (xs :: [*]).
HardForkLedgerConfig xs -> PerEraLedgerConfig xs
Consensus.hardForkLedgerConfigPerEra (HardForkLedgerConfig (CardanoEras StandardCrypto)
 -> PerEraLedgerConfig (CardanoEras StandardCrypto))
-> HardForkLedgerConfig (CardanoEras StandardCrypto)
-> PerEraLedgerConfig (CardanoEras StandardCrypto)
forall a b. (a -> b) -> a -> b
$ TopLevelConfig (CardanoBlock StandardCrypto)
-> LedgerConfig (CardanoBlock StandardCrypto)
forall blk. TopLevelConfig blk -> LedgerConfig blk
Consensus.configLedger TopLevelConfig (CardanoBlock StandardCrypto)
topLevelConfig

-- | Grab the current 'NodeKernelAccess' from the environment, or throw
-- gRPC UNAVAILABLE if the node kernel has not yet initialised.
grabNodeKernelAccess
  :: MonadRpc e m
  => m Type.NodeKernelAccess
grabNodeKernelAccess :: forall e (m :: * -> *). MonadRpc e m => m NodeKernelAccess
grabNodeKernelAccess =
  m (IORef (Maybe NodeKernelAccess))
forall field env (m :: * -> *).
(Has field env, MonadReader env m) =>
m field
grab m (IORef (Maybe NodeKernelAccess))
-> (IORef (Maybe NodeKernelAccess) -> m (Maybe NodeKernelAccess))
-> m (Maybe NodeKernelAccess)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= IO (Maybe NodeKernelAccess) -> m (Maybe NodeKernelAccess)
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Maybe NodeKernelAccess) -> m (Maybe NodeKernelAccess))
-> (IORef (Maybe NodeKernelAccess) -> IO (Maybe NodeKernelAccess))
-> IORef (Maybe NodeKernelAccess)
-> m (Maybe NodeKernelAccess)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IORef (Maybe NodeKernelAccess) -> IO (Maybe NodeKernelAccess)
forall a. IORef a -> IO a
readIORef m (Maybe NodeKernelAccess)
-> (Maybe NodeKernelAccess -> m NodeKernelAccess)
-> m NodeKernelAccess
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
    Maybe NodeKernelAccess
Nothing ->
      GrpcException -> m NodeKernelAccess
forall (m :: * -> *) e a. (MonadIO m, Exception e) => e -> m a
throwIO
        GrpcException
          { grpcError :: GrpcError
grpcError = GrpcError
GrpcUnavailable
          , grpcErrorMessage :: Maybe Text
grpcErrorMessage = Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"Node kernel not yet initialised"
          , grpcErrorDetails :: Maybe ByteString
grpcErrorDetails = Maybe ByteString
forall a. Maybe a
Nothing
          , grpcErrorMetadata :: [CustomMetadata]
grpcErrorMetadata = []
          }
    Just NodeKernelAccess
nodeKernelAccess ->
      NodeKernelAccess -> m NodeKernelAccess
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure NodeKernelAccess
nodeKernelAccess

-- | The network's system start time, extracted from genesis config.
-- Used together with 'readEraHistory' to convert slots to wall-clock time.
nodeKernelSystemStart :: Type.NodeKernelAccess -> SystemStart
nodeKernelSystemStart :: NodeKernelAccess -> SystemStart
nodeKernelSystemStart Type.NodeKernelAccess{systemStart :: NodeKernelAccess -> SystemStart
Type.systemStart = SystemStart
value} = SystemStart
value

-- | The protocol security parameter /k/: consensus never rolls back more
-- than /k/ blocks.
securityParam :: Type.NodeKernelAccess -> Consensus.SecurityParam
securityParam :: NodeKernelAccess -> SecurityParam
securityParam Type.NodeKernelAccess{securityParam :: NodeKernelAccess -> SecurityParam
Type.securityParam = SecurityParam
value} = SecurityParam
value

-- | The network's genesis configuration.
genesisConfig :: Type.NodeKernelAccess -> GenesisBundle
genesisConfig :: NodeKernelAccess -> GenesisBundle
genesisConfig Type.NodeKernelAccess{genesisConfig :: NodeKernelAccess -> GenesisBundle
Type.genesisConfig = GenesisBundle
value} = GenesisBundle
value

-- | Read the raw hard-fork era summary from the current ledger state, with
-- the era boundaries directly accessible.
readHardForkSummary
  :: MonadIO m
  => Type.NodeKernelAccess
  -> m (History.Summary (CardanoEras Consensus.StandardCrypto))
readHardForkSummary :: forall (m :: * -> *).
MonadIO m =>
NodeKernelAccess -> m (Summary (CardanoEras StandardCrypto))
readHardForkSummary Type.NodeKernelAccess{readHardForkSummary :: NodeKernelAccess
-> forall (m :: * -> *).
   MonadIO m =>
   m (Summary (CardanoEras StandardCrypto))
Type.readHardForkSummary = forall (m :: * -> *).
MonadIO m =>
m (Summary (CardanoEras StandardCrypto))
action} = m (Summary (CardanoEras StandardCrypto))
forall (m :: * -> *).
MonadIO m =>
m (Summary (CardanoEras StandardCrypto))
action

-- | Read current era history from the ledger state: the hard-fork era
-- summary wrapped into the opaque interpreter used for slot/time conversion
-- queries.
readEraHistory :: MonadIO m => Type.NodeKernelAccess -> m EraHistory
readEraHistory :: forall (m :: * -> *). MonadIO m => NodeKernelAccess -> m EraHistory
readEraHistory NodeKernelAccess
access = Interpreter (CardanoEras StandardCrypto) -> EraHistory
forall (xs :: [*]).
(CardanoBlock StandardCrypto ~ HardForkBlock xs) =>
Interpreter xs -> EraHistory
EraHistory (Interpreter (CardanoEras StandardCrypto) -> EraHistory)
-> (Summary (CardanoEras StandardCrypto)
    -> Interpreter (CardanoEras StandardCrypto))
-> Summary (CardanoEras StandardCrypto)
-> EraHistory
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Summary (CardanoEras StandardCrypto)
-> Interpreter (CardanoEras StandardCrypto)
forall (xs :: [*]). Summary xs -> Interpreter xs
Consensus.mkInterpreter (Summary (CardanoEras StandardCrypto) -> EraHistory)
-> m (Summary (CardanoEras StandardCrypto)) -> m EraHistory
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> NodeKernelAccess -> m (Summary (CardanoEras StandardCrypto))
forall (m :: * -> *).
MonadIO m =>
NodeKernelAccess -> m (Summary (CardanoEras StandardCrypto))
readHardForkSummary NodeKernelAccess
access

-- | Read the era of the current ledger state, the era the mempool stores
-- transactions in. An STM read, so a caller can combine it with others.
readCurrentEra :: Type.NodeKernelAccess -> STM AnyCardanoEra
readCurrentEra :: NodeKernelAccess -> STM AnyCardanoEra
readCurrentEra Type.NodeKernelAccess{chainDb :: NodeKernelAccess -> ChainDB IO (CardanoBlock StandardCrypto)
Type.chainDb = ChainDB IO (CardanoBlock StandardCrypto)
chainDb} =
  EraIndex (CardanoEras StandardCrypto) -> AnyCardanoEra
fromConsensusEraIndex
    (EraIndex (CardanoEras StandardCrypto) -> AnyCardanoEra)
-> (ExtLedgerState (CardanoBlock StandardCrypto) EmptyMK
    -> EraIndex (CardanoEras StandardCrypto))
-> ExtLedgerState (CardanoBlock StandardCrypto) EmptyMK
-> AnyCardanoEra
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NS (Flip LedgerState EmptyMK) (CardanoEras StandardCrypto)
-> EraIndex (CardanoEras StandardCrypto)
forall (xs :: [*]) (f :: * -> *).
SListI xs =>
NS f xs -> EraIndex xs
Consensus.eraIndexFromNS
    (NS (Flip LedgerState EmptyMK) (CardanoEras StandardCrypto)
 -> EraIndex (CardanoEras StandardCrypto))
-> (ExtLedgerState (CardanoBlock StandardCrypto) EmptyMK
    -> NS (Flip LedgerState EmptyMK) (CardanoEras StandardCrypto))
-> ExtLedgerState (CardanoBlock StandardCrypto) EmptyMK
-> EraIndex (CardanoEras StandardCrypto)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HardForkState
  (Flip LedgerState EmptyMK) (CardanoEras StandardCrypto)
-> NS (Flip LedgerState EmptyMK) (CardanoEras StandardCrypto)
forall (xs :: [*]) (f :: * -> *).
SListI xs =>
HardForkState f xs -> NS f xs
HardForkState.tip
    (HardForkState
   (Flip LedgerState EmptyMK) (CardanoEras StandardCrypto)
 -> NS (Flip LedgerState EmptyMK) (CardanoEras StandardCrypto))
-> (ExtLedgerState (CardanoBlock StandardCrypto) EmptyMK
    -> HardForkState
         (Flip LedgerState EmptyMK) (CardanoEras StandardCrypto))
-> ExtLedgerState (CardanoBlock StandardCrypto) EmptyMK
-> NS (Flip LedgerState EmptyMK) (CardanoEras StandardCrypto)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LedgerState (CardanoBlock StandardCrypto) EmptyMK
-> HardForkState
     (Flip LedgerState EmptyMK) (CardanoEras StandardCrypto)
forall (xs :: [*]) (mk :: MapKind).
LedgerState (HardForkBlock xs) mk
-> HardForkState (Flip LedgerState mk) xs
Consensus.hardForkLedgerStatePerEra
    (LedgerState (CardanoBlock StandardCrypto) EmptyMK
 -> HardForkState
      (Flip LedgerState EmptyMK) (CardanoEras StandardCrypto))
-> (ExtLedgerState (CardanoBlock StandardCrypto) EmptyMK
    -> LedgerState (CardanoBlock StandardCrypto) EmptyMK)
-> ExtLedgerState (CardanoBlock StandardCrypto) EmptyMK
-> HardForkState
     (Flip LedgerState EmptyMK) (CardanoEras StandardCrypto)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ExtLedgerState (CardanoBlock StandardCrypto) EmptyMK
-> LedgerState (CardanoBlock StandardCrypto) EmptyMK
forall blk (mk :: MapKind).
ExtLedgerState blk mk -> LedgerState blk mk
Consensus.ledgerState
    (ExtLedgerState (CardanoBlock StandardCrypto) EmptyMK
 -> AnyCardanoEra)
-> STM (ExtLedgerState (CardanoBlock StandardCrypto) EmptyMK)
-> STM AnyCardanoEra
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ChainDB IO (CardanoBlock StandardCrypto)
-> STM IO (ExtLedgerState (CardanoBlock StandardCrypto) EmptyMK)
forall (m :: * -> *) blk.
ChainDB m blk -> STM m (ExtLedgerState blk EmptyMK)
Consensus.getCurrentLedger ChainDB IO (CardanoBlock StandardCrypto)
chainDb

-- | Read the current chain tip header from ChainDB, or 'Nothing' at origin.
readChainTipHeader
  :: MonadIO m
  => Type.NodeKernelAccess
  -> m (Maybe (Consensus.Header (Consensus.CardanoBlock Consensus.StandardCrypto)))
readChainTipHeader :: forall (m :: * -> *).
MonadIO m =>
NodeKernelAccess
-> m (Maybe (Header (CardanoBlock StandardCrypto)))
readChainTipHeader Type.NodeKernelAccess{chainDb :: NodeKernelAccess -> ChainDB IO (CardanoBlock StandardCrypto)
Type.chainDb = ChainDB IO (CardanoBlock StandardCrypto)
chainDb} = IO (Maybe (Header (CardanoBlock StandardCrypto)))
-> m (Maybe (Header (CardanoBlock StandardCrypto)))
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Maybe (Header (CardanoBlock StandardCrypto)))
 -> m (Maybe (Header (CardanoBlock StandardCrypto))))
-> IO (Maybe (Header (CardanoBlock StandardCrypto)))
-> m (Maybe (Header (CardanoBlock StandardCrypto)))
forall a b. (a -> b) -> a -> b
$ ChainDB IO (CardanoBlock StandardCrypto)
-> IO (Maybe (Header (CardanoBlock StandardCrypto)))
forall (m :: * -> *) blk. ChainDB m blk -> m (Maybe (Header blk))
Consensus.getTipHeader ChainDB IO (CardanoBlock StandardCrypto)
chainDb

-- | Fetch a raw block and its parsed era-contextualised form from ChainDB
-- by slot and header hash.
fetchBlock
  :: MonadIO m
  => Type.NodeKernelAccess
  -- ^ Node kernel access handle
  -> SlotNo
  -- ^ Block slot number
  -> Hash BlockHeader
  -- ^ Block header hash
  -> m (Maybe (ByteString, BlockInMode))
  -- ^ Raw CBOR bytes and the block in era context, or 'Nothing' if not found
fetchBlock :: forall (m :: * -> *).
MonadIO m =>
NodeKernelAccess
-> SlotNo
-> Hash BlockHeader
-> m (Maybe (ByteString, BlockInMode))
fetchBlock Type.NodeKernelAccess{chainDb :: NodeKernelAccess -> ChainDB IO (CardanoBlock StandardCrypto)
Type.chainDb = ChainDB IO (CardanoBlock StandardCrypto)
chainDb} SlotNo
slot (HeaderHash ShortByteString
shortHash) = do
  let point :: RealPoint (CardanoBlock StandardCrypto)
point = SlotNo
-> HeaderHash (CardanoBlock StandardCrypto)
-> RealPoint (CardanoBlock StandardCrypto)
forall blk. SlotNo -> HeaderHash blk -> RealPoint blk
Consensus.RealPoint SlotNo
slot (ShortByteString -> OneEraHash (CardanoEras StandardCrypto)
forall k (xs :: [k]). ShortByteString -> OneEraHash xs
Consensus.OneEraHash ShortByteString
shortHash)
      component :: BlockComponent
  (CardanoBlock StandardCrypto) (ByteString, BlockInMode)
component = (,) (ByteString -> BlockInMode -> (ByteString, BlockInMode))
-> BlockComponent (CardanoBlock StandardCrypto) ByteString
-> BlockComponent
     (CardanoBlock StandardCrypto)
     (BlockInMode -> (ByteString, BlockInMode))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (ByteString -> ByteString)
-> BlockComponent (CardanoBlock StandardCrypto) ByteString
-> BlockComponent (CardanoBlock StandardCrypto) ByteString
forall a b.
(a -> b)
-> BlockComponent (CardanoBlock StandardCrypto) a
-> BlockComponent (CardanoBlock StandardCrypto) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteString -> ByteString
BSL.toStrict BlockComponent (CardanoBlock StandardCrypto) ByteString
forall blk. BlockComponent blk ByteString
Consensus.GetRawBlock BlockComponent
  (CardanoBlock StandardCrypto)
  (BlockInMode -> (ByteString, BlockInMode))
-> BlockComponent (CardanoBlock StandardCrypto) BlockInMode
-> BlockComponent
     (CardanoBlock StandardCrypto) (ByteString, BlockInMode)
forall a b.
BlockComponent (CardanoBlock StandardCrypto) (a -> b)
-> BlockComponent (CardanoBlock StandardCrypto) a
-> BlockComponent (CardanoBlock StandardCrypto) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (CardanoBlock StandardCrypto -> BlockInMode)
-> BlockComponent
     (CardanoBlock StandardCrypto) (CardanoBlock StandardCrypto)
-> BlockComponent (CardanoBlock StandardCrypto) BlockInMode
forall a b.
(a -> b)
-> BlockComponent (CardanoBlock StandardCrypto) a
-> BlockComponent (CardanoBlock StandardCrypto) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap CardanoBlock StandardCrypto -> BlockInMode
forall block.
(CardanoBlock StandardCrypto ~ block) =>
block -> BlockInMode
fromConsensusBlock BlockComponent
  (CardanoBlock StandardCrypto) (CardanoBlock StandardCrypto)
forall blk. BlockComponent blk blk
Consensus.GetBlock
  IO (Maybe (ByteString, BlockInMode))
-> m (Maybe (ByteString, BlockInMode))
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Maybe (ByteString, BlockInMode))
 -> m (Maybe (ByteString, BlockInMode)))
-> IO (Maybe (ByteString, BlockInMode))
-> m (Maybe (ByteString, BlockInMode))
forall a b. (a -> b) -> a -> b
$ ChainDB IO (CardanoBlock StandardCrypto)
-> forall b.
   BlockComponent (CardanoBlock StandardCrypto) b
   -> RealPoint (CardanoBlock StandardCrypto) -> IO (Maybe b)
forall (m :: * -> *) blk.
ChainDB m blk
-> forall b. BlockComponent blk b -> RealPoint blk -> m (Maybe b)
Consensus.getBlockComponent ChainDB IO (CardanoBlock StandardCrypto)
chainDb BlockComponent
  (CardanoBlock StandardCrypto) (ByteString, BlockInMode)
component RealPoint (CardanoBlock StandardCrypto)
point

-- | Read the current mempool contents as a point-in-time snapshot:
-- transactions added or removed after the read are not reflected.
-- Ticket numbers and mempool capacity accounting are dropped here, since
-- 'readMempoolTxs' only serves the one-shot @ReadMempool@ method.
readMempoolTxs
  :: MonadIO m
  => Type.NodeKernelAccess
  -> m [Consensus.GenTx (Consensus.CardanoBlock Consensus.StandardCrypto)]
readMempoolTxs :: forall (m :: * -> *).
MonadIO m =>
NodeKernelAccess -> m [GenTx (CardanoBlock StandardCrypto)]
readMempoolTxs NodeKernelAccess
nodeKernelAccess = do
  snapshot <- STM (MempoolSnapshot (CardanoBlock StandardCrypto))
-> m (MempoolSnapshot (CardanoBlock StandardCrypto))
forall (m :: * -> *) a. MonadIO m => STM a -> m a
atomically (STM (MempoolSnapshot (CardanoBlock StandardCrypto))
 -> m (MempoolSnapshot (CardanoBlock StandardCrypto)))
-> STM (MempoolSnapshot (CardanoBlock StandardCrypto))
-> m (MempoolSnapshot (CardanoBlock StandardCrypto))
forall a b. (a -> b) -> a -> b
$ NodeKernelAccess
-> STM (MempoolSnapshot (CardanoBlock StandardCrypto))
readMempoolSnapshot NodeKernelAccess
nodeKernelAccess
  pure
    [Consensus.txForgetValidated tx | (tx, _ticketNo, _txMeasure) <- Consensus.snapshotTxs snapshot]

-- | Read the current mempool snapshot. An STM read, so a caller can wait for
-- the mempool to change.
readMempoolSnapshot
  :: Type.NodeKernelAccess
  -> STM (Consensus.MempoolSnapshot (Consensus.CardanoBlock Consensus.StandardCrypto))
readMempoolSnapshot :: NodeKernelAccess
-> STM (MempoolSnapshot (CardanoBlock StandardCrypto))
readMempoolSnapshot Type.NodeKernelAccess{mempool :: NodeKernelAccess -> Mempool IO (CardanoBlock StandardCrypto)
Type.mempool = Mempool IO (CardanoBlock StandardCrypto)
mempool} = Mempool IO (CardanoBlock StandardCrypto)
-> STM IO (MempoolSnapshot (CardanoBlock StandardCrypto))
forall (m :: * -> *) blk.
Mempool m blk -> STM m (MempoolSnapshot blk)
Consensus.getSnapshot Mempool IO (CardanoBlock StandardCrypto)
mempool

-- | A point-in-time view of the mempool for @WatchMempool@: a pure projection
-- of the entries newer than a given ticket, already unwrapped into 'TxInMode'
-- so tests can build one from 'TxInMode' fixtures without a real mempool.
newtype MempoolWatchSnapshot = MempoolWatchSnapshot
  { MempoolWatchSnapshot -> TicketNo -> [(TxInMode, TicketNo)]
mempoolWatchTxsAfter :: Consensus.TicketNo -> [(TxInMode, Consensus.TicketNo)]
  -- ^ Entries with a ticket number greater than the given one, oldest to
  -- newest.
  }

-- | Read the current mempool watch snapshot, without blocking.
watchMempoolSnapshot
  :: MonadIO m
  => Type.NodeKernelAccess
  -> m MempoolWatchSnapshot
watchMempoolSnapshot :: forall (m :: * -> *).
MonadIO m =>
NodeKernelAccess -> m MempoolWatchSnapshot
watchMempoolSnapshot NodeKernelAccess
nodeKernelAccess =
  MempoolSnapshot (CardanoBlock StandardCrypto)
-> MempoolWatchSnapshot
toMempoolWatchSnapshot (MempoolSnapshot (CardanoBlock StandardCrypto)
 -> MempoolWatchSnapshot)
-> m (MempoolSnapshot (CardanoBlock StandardCrypto))
-> m MempoolWatchSnapshot
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> STM (MempoolSnapshot (CardanoBlock StandardCrypto))
-> m (MempoolSnapshot (CardanoBlock StandardCrypto))
forall (m :: * -> *) a. MonadIO m => STM a -> m a
atomically (NodeKernelAccess
-> STM (MempoolSnapshot (CardanoBlock StandardCrypto))
readMempoolSnapshot NodeKernelAccess
nodeKernelAccess)

-- | Block until the mempool holds an entry newer than the given ticket, then
-- return the snapshot. Tickets are never reused, so this sees every addition;
-- removals and slot changes do not wake the caller.
nextMempoolWatchSnapshot
  :: MonadIO m
  => Type.NodeKernelAccess
  -> Consensus.TicketNo
  -> m MempoolWatchSnapshot
nextMempoolWatchSnapshot :: forall (m :: * -> *).
MonadIO m =>
NodeKernelAccess -> TicketNo -> m MempoolWatchSnapshot
nextMempoolWatchSnapshot NodeKernelAccess
nodeKernelAccess TicketNo
lastSeenTicket =
  STM MempoolWatchSnapshot -> m MempoolWatchSnapshot
forall (m :: * -> *) a. MonadIO m => STM a -> m a
atomically (STM MempoolWatchSnapshot -> m MempoolWatchSnapshot)
-> STM MempoolWatchSnapshot -> m MempoolWatchSnapshot
forall a b. (a -> b) -> a -> b
$ do
    candidate <- NodeKernelAccess
-> STM (MempoolSnapshot (CardanoBlock StandardCrypto))
readMempoolSnapshot NodeKernelAccess
nodeKernelAccess
    check . not . null $ Consensus.snapshotTxsAfter candidate lastSeenTicket
    pure $ toMempoolWatchSnapshot candidate

toMempoolWatchSnapshot
  :: Consensus.MempoolSnapshot (Consensus.CardanoBlock Consensus.StandardCrypto)
  -> MempoolWatchSnapshot
toMempoolWatchSnapshot :: MempoolSnapshot (CardanoBlock StandardCrypto)
-> MempoolWatchSnapshot
toMempoolWatchSnapshot MempoolSnapshot (CardanoBlock StandardCrypto)
snapshot =
  MempoolWatchSnapshot
    { mempoolWatchTxsAfter :: TicketNo -> [(TxInMode, TicketNo)]
mempoolWatchTxsAfter = \TicketNo
ticketNo ->
        [ (GenTx (CardanoBlock StandardCrypto) -> TxInMode
forall block.
(CardanoBlock StandardCrypto ~ block) =>
GenTx block -> TxInMode
fromConsensusGenTx (Validated (GenTx (CardanoBlock StandardCrypto))
-> GenTx (CardanoBlock StandardCrypto)
forall blk.
LedgerSupportsMempool blk =>
Validated (GenTx blk) -> GenTx blk
Consensus.txForgetValidated Validated (GenTx (CardanoBlock StandardCrypto))
tx), TicketNo
ticketNo')
        | (Validated (GenTx (CardanoBlock StandardCrypto))
tx, TicketNo
ticketNo', TxMeasure (CardanoBlock StandardCrypto)
_txMeasure) <- MempoolSnapshot (CardanoBlock StandardCrypto)
-> TicketNo
-> [(Validated (GenTx (CardanoBlock StandardCrypto)), TicketNo,
     TxMeasure (CardanoBlock StandardCrypto))]
forall blk.
MempoolSnapshot blk
-> TicketNo -> [(Validated (GenTx blk), TicketNo, TxMeasure blk)]
Consensus.snapshotTxsAfter MempoolSnapshot (CardanoBlock StandardCrypto)
snapshot TicketNo
ticketNo
        ]
    }