{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}

-- | Interface to the ledger layer
--
-- This module defines how to apply blocks to a ledger state, and re-exports
-- (from "Ouroboros.Consensus.Ledger.Basics") how to tick ledger states. These
-- are the two main operations we can do with a 'LedgerState'.
module Ouroboros.Consensus.Ledger.Abstract (
    -- * Type-level validation marker
    Validated
    -- * Apply block
  , ApplyBlock (..)
  , ComputeLedgerEvents (..)
  , UpdateLedger
  , defaultApplyBlockLedgerResult
  , defaultReapplyBlockLedgerResult
    -- * Derived
  , applyLedgerBlock
  , foldLedger
  , reapplyLedgerBlock
  , refoldLedger
  , tickThenApply
  , tickThenApplyLedgerResult
  , tickThenReapply
  , tickThenReapplyLedgerResult
    -- ** Short-hand
  , ledgerTipHash
  , ledgerTipPoint
  , ledgerTipSlot
    -- * Re-exports
  , module Ouroboros.Consensus.Ledger.Basics
  ) where

import           Control.Monad.Except
import qualified Control.State.Transition.Extended as STS
import           Data.Kind (Type)
import           GHC.Stack (HasCallStack)
import           Ouroboros.Consensus.Block.Abstract
import           Ouroboros.Consensus.Ledger.Basics
import           Ouroboros.Consensus.Ledger.Tables.Utils
import           Ouroboros.Consensus.Ticked
import           Ouroboros.Consensus.Util

-- | " Validated " transaction or block
--
-- The ledger defines how to validate transactions and blocks. It's possible the
-- type before and after validation may be distinct (eg Alonzo transactions),
-- which originally motivated this family.
--
-- We also gain the related benefit that certain interface functions, such as
-- those that /reapply/ blocks, can have a more precise type now. TODO
--
-- Similarly, the Node-to-Client mini protocols can explicitly indicate that the
-- client trusts the blocks from the local server, by having the server send
-- 'Validated' blocks to the client. TODO
--
-- Note that validation has different implications for a transaction than for a
-- block. In particular, a validated transaction can be " reapplied " to
-- different ledger states, whereas a validated block must only be " reapplied "
-- to the exact same ledger state (eg as part of rebuilding from an on-disk
-- ledger snapshot).
--
-- Since the ledger defines validation, see the ledger details for concrete
-- examples of what determines the validity (wrt to a 'LedgerState') of a
-- transaction and/or block. Example properties include: a transaction's claimed
-- inputs exist and are still unspent, a block carries a sufficient
-- cryptographic signature, etc.
data family Validated x :: Type

{-------------------------------------------------------------------------------
  Apply block to ledger state
-------------------------------------------------------------------------------}

class ( IsLedger l
      , HeaderHash l ~ HeaderHash blk
      , HasHeader blk
      , HasHeader (Header blk)
      , HasLedgerTables l
      , HasLedgerTables (Ticked l)
      ) => ApplyBlock l blk where

  -- | Apply a block to the ledger state.
  --
  -- This is passed the ledger state ticked to the slot of the given block, so
  -- 'applyChainTickLedgerResult' has already been called.
  --
  -- Users of this function can set any validation level allowed by the
  -- @small-steps@ package. See "Control.State.Transition.Extended".
  applyBlockLedgerResultWithValidation ::
       HasCallStack
    => STS.ValidationPolicy
    -> ComputeLedgerEvents
    -> LedgerCfg l
    -> blk
    -> Ticked l ValuesMK
    -> Except (LedgerErr l) (LedgerResult l (l DiffMK))

  -- | Apply a block to the ledger state.
  --
  -- This is passed the ledger state ticked to the slot of the given block, so
  -- 'applyChainTickLedgerResult' has already been called.
  --
  -- This function will use 'ValidateAll' policy for calling the ledger rules.
  applyBlockLedgerResult ::
       HasCallStack
    => ComputeLedgerEvents
    -> LedgerCfg l
    -> blk
    -> Ticked l ValuesMK
    -> Except (LedgerErr l) (LedgerResult l (l DiffMK))

  -- | Re-apply a block to the very same ledger state it was applied in before.
  --
  -- Since a block can only be applied to a single, specific, ledger state,
  -- if we apply a previously applied block again it will be applied in the
  -- very same ledger state, and therefore can't possibly fail.
  --
  -- It is worth noting that since we already know that the block is valid in
  -- the provided ledger state, the ledger layer should not perform /any/
  -- validation checks. Thus this function will call the ledger rules with
  -- 'ValidateNone' policy.
  reapplyBlockLedgerResult ::
       HasCallStack
    => ComputeLedgerEvents
    -> LedgerCfg l
    -> blk
    -> Ticked l ValuesMK
    -> LedgerResult l (l DiffMK)

  -- | Given a block, get the key-sets that we need to apply it to a ledger
  -- state.
  getBlockKeySets :: blk -> LedgerTables l KeysMK

defaultApplyBlockLedgerResult ::
     (HasCallStack, ApplyBlock l blk)
  => ComputeLedgerEvents
  -> LedgerCfg l
  -> blk
  -> Ticked l ValuesMK
  -> Except (LedgerErr l) (LedgerResult l (l DiffMK))
defaultApplyBlockLedgerResult :: forall (l :: LedgerStateKind) blk.
(HasCallStack, ApplyBlock l blk) =>
ComputeLedgerEvents
-> LedgerCfg l
-> blk
-> Ticked l ValuesMK
-> Except (LedgerErr l) (LedgerResult l (l DiffMK))
defaultApplyBlockLedgerResult =
  ValidationPolicy
-> ComputeLedgerEvents
-> LedgerCfg l
-> blk
-> Ticked l ValuesMK
-> ExceptT (LedgerErr l) Identity (LedgerResult l (l DiffMK))
forall (l :: LedgerStateKind) blk.
(ApplyBlock l blk, HasCallStack) =>
ValidationPolicy
-> ComputeLedgerEvents
-> LedgerCfg l
-> blk
-> Ticked l ValuesMK
-> Except (LedgerErr l) (LedgerResult l (l DiffMK))
applyBlockLedgerResultWithValidation ValidationPolicy
STS.ValidateAll

defaultReapplyBlockLedgerResult ::
     (HasCallStack, ApplyBlock l blk)
  => (LedgerErr l -> LedgerResult l (l DiffMK))
  -> ComputeLedgerEvents
  -> LedgerCfg l
  -> blk
  -> Ticked l ValuesMK
  -> LedgerResult l (l DiffMK)
defaultReapplyBlockLedgerResult :: forall (l :: LedgerStateKind) blk.
(HasCallStack, ApplyBlock l blk) =>
(LedgerErr l -> LedgerResult l (l DiffMK))
-> ComputeLedgerEvents
-> LedgerCfg l
-> blk
-> Ticked l ValuesMK
-> LedgerResult l (l DiffMK)
defaultReapplyBlockLedgerResult LedgerErr l -> LedgerResult l (l DiffMK)
throwReapplyError =
       ((LedgerErr l -> LedgerResult l (l DiffMK))
-> (LedgerResult l (l DiffMK) -> LedgerResult l (l DiffMK))
-> Either (LedgerErr l) (LedgerResult l (l DiffMK))
-> LedgerResult l (l DiffMK)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either LedgerErr l -> LedgerResult l (l DiffMK)
throwReapplyError LedgerResult l (l DiffMK) -> LedgerResult l (l DiffMK)
forall a. a -> a
id (Either (LedgerErr l) (LedgerResult l (l DiffMK))
 -> LedgerResult l (l DiffMK))
-> (Except (LedgerErr l) (LedgerResult l (l DiffMK))
    -> Either (LedgerErr l) (LedgerResult l (l DiffMK)))
-> Except (LedgerErr l) (LedgerResult l (l DiffMK))
-> LedgerResult l (l DiffMK)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Except (LedgerErr l) (LedgerResult l (l DiffMK))
-> Either (LedgerErr l) (LedgerResult l (l DiffMK))
forall e a. Except e a -> Either e a
runExcept)
  (Except (LedgerErr l) (LedgerResult l (l DiffMK))
 -> LedgerResult l (l DiffMK))
-> (ComputeLedgerEvents
    -> LedgerCfg l
    -> blk
    -> Ticked l ValuesMK
    -> Except (LedgerErr l) (LedgerResult l (l DiffMK)))
-> ComputeLedgerEvents
-> LedgerCfg l
-> blk
-> Ticked l ValuesMK
-> LedgerResult l (l DiffMK)
forall y z x0 x1 x2 x3.
(y -> z)
-> (x0 -> x1 -> x2 -> x3 -> y) -> x0 -> x1 -> x2 -> x3 -> z
...: ValidationPolicy
-> ComputeLedgerEvents
-> LedgerCfg l
-> blk
-> Ticked l ValuesMK
-> Except (LedgerErr l) (LedgerResult l (l DiffMK))
forall (l :: LedgerStateKind) blk.
(ApplyBlock l blk, HasCallStack) =>
ValidationPolicy
-> ComputeLedgerEvents
-> LedgerCfg l
-> blk
-> Ticked l ValuesMK
-> Except (LedgerErr l) (LedgerResult l (l DiffMK))
applyBlockLedgerResultWithValidation ValidationPolicy
STS.ValidateNone

-- | Interaction with the ledger layer
class ApplyBlock (LedgerState blk) blk => UpdateLedger blk

{-------------------------------------------------------------------------------
  Derived functionality
-------------------------------------------------------------------------------}

-- | 'lrResult' after 'applyBlockLedgerResult'
applyLedgerBlock ::
     forall l blk.
     (ApplyBlock l blk, HasCallStack)
  => ComputeLedgerEvents
  -> LedgerCfg l
  -> blk
  -> Ticked l ValuesMK
  -> Except (LedgerErr l) (l DiffMK)
applyLedgerBlock :: forall (l :: LedgerStateKind) blk.
(ApplyBlock l blk, HasCallStack) =>
ComputeLedgerEvents
-> LedgerCfg l
-> blk
-> Ticked l ValuesMK
-> Except (LedgerErr l) (l DiffMK)
applyLedgerBlock = (LedgerResult l (l DiffMK) -> l DiffMK)
-> ExceptT (LedgerErr l) Identity (LedgerResult l (l DiffMK))
-> ExceptT (LedgerErr l) Identity (l DiffMK)
forall a b.
(a -> b)
-> ExceptT (LedgerErr l) Identity a
-> ExceptT (LedgerErr l) Identity b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap LedgerResult l (l DiffMK) -> l DiffMK
forall (l :: LedgerStateKind) a. LedgerResult l a -> a
lrResult (ExceptT (LedgerErr l) Identity (LedgerResult l (l DiffMK))
 -> ExceptT (LedgerErr l) Identity (l DiffMK))
-> (ComputeLedgerEvents
    -> LedgerCfg l
    -> blk
    -> Ticked l ValuesMK
    -> ExceptT (LedgerErr l) Identity (LedgerResult l (l DiffMK)))
-> ComputeLedgerEvents
-> LedgerCfg l
-> blk
-> Ticked l ValuesMK
-> ExceptT (LedgerErr l) Identity (l DiffMK)
forall y z x0 x1 x2 x3.
(y -> z)
-> (x0 -> x1 -> x2 -> x3 -> y) -> x0 -> x1 -> x2 -> x3 -> z
...: ComputeLedgerEvents
-> LedgerCfg l
-> blk
-> Ticked l ValuesMK
-> ExceptT (LedgerErr l) Identity (LedgerResult l (l DiffMK))
forall (l :: LedgerStateKind) blk.
(ApplyBlock l blk, HasCallStack) =>
ComputeLedgerEvents
-> LedgerCfg l
-> blk
-> Ticked l ValuesMK
-> Except (LedgerErr l) (LedgerResult l (l DiffMK))
applyBlockLedgerResult

-- | 'lrResult' after 'reapplyBlockLedgerResult'
reapplyLedgerBlock ::
     forall l blk.
     (ApplyBlock l blk, HasCallStack)
  => ComputeLedgerEvents
  -> LedgerCfg l
  -> blk
  -> Ticked l ValuesMK
  -> l DiffMK
reapplyLedgerBlock :: forall (l :: LedgerStateKind) blk.
(ApplyBlock l blk, HasCallStack) =>
ComputeLedgerEvents
-> LedgerCfg l -> blk -> Ticked l ValuesMK -> l DiffMK
reapplyLedgerBlock = LedgerResult l (l DiffMK) -> l DiffMK
forall (l :: LedgerStateKind) a. LedgerResult l a -> a
lrResult (LedgerResult l (l DiffMK) -> l DiffMK)
-> (ComputeLedgerEvents
    -> LedgerCfg l
    -> blk
    -> Ticked l ValuesMK
    -> LedgerResult l (l DiffMK))
-> ComputeLedgerEvents
-> LedgerCfg l
-> blk
-> Ticked l ValuesMK
-> l DiffMK
forall y z x0 x1 x2 x3.
(y -> z)
-> (x0 -> x1 -> x2 -> x3 -> y) -> x0 -> x1 -> x2 -> x3 -> z
...: ComputeLedgerEvents
-> LedgerCfg l
-> blk
-> Ticked l ValuesMK
-> LedgerResult l (l DiffMK)
forall (l :: LedgerStateKind) blk.
(ApplyBlock l blk, HasCallStack) =>
ComputeLedgerEvents
-> LedgerCfg l
-> blk
-> Ticked l ValuesMK
-> LedgerResult l (l DiffMK)
reapplyBlockLedgerResult

tickThenApplyLedgerResult ::
     ApplyBlock l blk
  => ComputeLedgerEvents
  -> LedgerCfg l
  -> blk
  -> l ValuesMK
  -> Except (LedgerErr l) (LedgerResult l (l DiffMK))
tickThenApplyLedgerResult :: forall (l :: LedgerStateKind) blk.
ApplyBlock l blk =>
ComputeLedgerEvents
-> LedgerCfg l
-> blk
-> l ValuesMK
-> Except (LedgerErr l) (LedgerResult l (l DiffMK))
tickThenApplyLedgerResult ComputeLedgerEvents
evs LedgerCfg l
cfg blk
blk l ValuesMK
l = do
  let lrTick :: LedgerResult l (Ticked l DiffMK)
lrTick = ComputeLedgerEvents
-> LedgerCfg l
-> SlotNo
-> l EmptyMK
-> LedgerResult l (Ticked l DiffMK)
forall (l :: LedgerStateKind).
IsLedger l =>
ComputeLedgerEvents
-> LedgerCfg l
-> SlotNo
-> l EmptyMK
-> LedgerResult l (Ticked l DiffMK)
applyChainTickLedgerResult ComputeLedgerEvents
evs LedgerCfg l
cfg (blk -> SlotNo
forall b. HasHeader b => b -> SlotNo
blockSlot blk
blk) (l ValuesMK -> l EmptyMK
forall (l :: LedgerStateKind) (mk :: MapKind).
HasLedgerTables l =>
l mk -> l EmptyMK
forgetLedgerTables l ValuesMK
l)
  lrBlock <-   ComputeLedgerEvents
-> LedgerCfg l
-> blk
-> Ticked l ValuesMK
-> ExceptT (LedgerErr l) Identity (LedgerResult l (l DiffMK))
forall (l :: LedgerStateKind) blk.
(ApplyBlock l blk, HasCallStack) =>
ComputeLedgerEvents
-> LedgerCfg l
-> blk
-> Ticked l ValuesMK
-> Except (LedgerErr l) (LedgerResult l (l DiffMK))
applyBlockLedgerResult     ComputeLedgerEvents
evs LedgerCfg l
cfg            blk
blk  (l ValuesMK
-> LedgerTables l KeysMK -> Ticked l DiffMK -> Ticked l ValuesMK
forall (l :: LedgerStateKind) (l' :: LedgerStateKind).
(SameUtxoTypes l l', HasLedgerTables l, HasLedgerTables l') =>
l ValuesMK -> LedgerTables l KeysMK -> l' DiffMK -> l' ValuesMK
applyDiffForKeys l ValuesMK
l (blk -> LedgerTables l KeysMK
forall (l :: LedgerStateKind) blk.
ApplyBlock l blk =>
blk -> LedgerTables l KeysMK
getBlockKeySets blk
blk) (LedgerResult l (Ticked l DiffMK) -> Ticked l DiffMK
forall (l :: LedgerStateKind) a. LedgerResult l a -> a
lrResult LedgerResult l (Ticked l DiffMK)
lrTick))
  pure LedgerResult {
      lrEvents = lrEvents lrTick <> lrEvents lrBlock
    , lrResult = prependDiffs (lrResult lrTick) (lrResult lrBlock)
    }

tickThenReapplyLedgerResult ::
     forall l blk.
     ApplyBlock l blk
  => ComputeLedgerEvents
  -> LedgerCfg l
  -> blk
  -> l ValuesMK
  -> LedgerResult l (l DiffMK)
tickThenReapplyLedgerResult :: forall (l :: LedgerStateKind) blk.
ApplyBlock l blk =>
ComputeLedgerEvents
-> LedgerCfg l -> blk -> l ValuesMK -> LedgerResult l (l DiffMK)
tickThenReapplyLedgerResult ComputeLedgerEvents
evs LedgerCfg l
cfg blk
blk l ValuesMK
l =
  let lrTick :: LedgerResult l (Ticked l DiffMK)
lrTick  = ComputeLedgerEvents
-> LedgerCfg l
-> SlotNo
-> l EmptyMK
-> LedgerResult l (Ticked l DiffMK)
forall (l :: LedgerStateKind).
IsLedger l =>
ComputeLedgerEvents
-> LedgerCfg l
-> SlotNo
-> l EmptyMK
-> LedgerResult l (Ticked l DiffMK)
applyChainTickLedgerResult ComputeLedgerEvents
evs LedgerCfg l
cfg (blk -> SlotNo
forall b. HasHeader b => b -> SlotNo
blockSlot blk
blk) (l ValuesMK -> l EmptyMK
forall (l :: LedgerStateKind) (mk :: MapKind).
HasLedgerTables l =>
l mk -> l EmptyMK
forgetLedgerTables l ValuesMK
l)
      lrBlock :: LedgerResult l (l DiffMK)
lrBlock = ComputeLedgerEvents
-> LedgerCfg l
-> blk
-> Ticked l ValuesMK
-> LedgerResult l (l DiffMK)
forall (l :: LedgerStateKind) blk.
(ApplyBlock l blk, HasCallStack) =>
ComputeLedgerEvents
-> LedgerCfg l
-> blk
-> Ticked l ValuesMK
-> LedgerResult l (l DiffMK)
reapplyBlockLedgerResult   ComputeLedgerEvents
evs LedgerCfg l
cfg            blk
blk  (l ValuesMK
-> LedgerTables l KeysMK -> Ticked l DiffMK -> Ticked l ValuesMK
forall (l :: LedgerStateKind) (l' :: LedgerStateKind).
(SameUtxoTypes l l', HasLedgerTables l, HasLedgerTables l') =>
l ValuesMK -> LedgerTables l KeysMK -> l' DiffMK -> l' ValuesMK
applyDiffForKeys l ValuesMK
l (blk -> LedgerTables l KeysMK
forall (l :: LedgerStateKind) blk.
ApplyBlock l blk =>
blk -> LedgerTables l KeysMK
getBlockKeySets blk
blk) (LedgerResult l (Ticked l DiffMK) -> Ticked l DiffMK
forall (l :: LedgerStateKind) a. LedgerResult l a -> a
lrResult LedgerResult l (Ticked l DiffMK)
lrTick))
  in LedgerResult {
      lrEvents :: [AuxLedgerEvent l]
lrEvents = LedgerResult l (Ticked l DiffMK) -> [AuxLedgerEvent l]
forall (l :: LedgerStateKind) a.
LedgerResult l a -> [AuxLedgerEvent l]
lrEvents LedgerResult l (Ticked l DiffMK)
lrTick [AuxLedgerEvent l] -> [AuxLedgerEvent l] -> [AuxLedgerEvent l]
forall a. Semigroup a => a -> a -> a
<> LedgerResult l (l DiffMK) -> [AuxLedgerEvent l]
forall (l :: LedgerStateKind) a.
LedgerResult l a -> [AuxLedgerEvent l]
lrEvents LedgerResult l (l DiffMK)
lrBlock
    , lrResult :: l DiffMK
lrResult = Ticked l DiffMK -> l DiffMK -> l DiffMK
forall (l :: LedgerStateKind) (l' :: LedgerStateKind).
(SameUtxoTypes l l', HasLedgerTables l, HasLedgerTables l') =>
l DiffMK -> l' DiffMK -> l' DiffMK
prependDiffs (LedgerResult l (Ticked l DiffMK) -> Ticked l DiffMK
forall (l :: LedgerStateKind) a. LedgerResult l a -> a
lrResult LedgerResult l (Ticked l DiffMK)
lrTick) (LedgerResult l (l DiffMK) -> l DiffMK
forall (l :: LedgerStateKind) a. LedgerResult l a -> a
lrResult LedgerResult l (l DiffMK)
lrBlock)
    }

tickThenApply ::
     forall l blk.
     ApplyBlock l blk
  => ComputeLedgerEvents
  -> LedgerCfg l
  -> blk
  -> l ValuesMK
  -> Except (LedgerErr l) (l DiffMK)
tickThenApply :: forall (l :: LedgerStateKind) blk.
ApplyBlock l blk =>
ComputeLedgerEvents
-> LedgerCfg l
-> blk
-> l ValuesMK
-> Except (LedgerErr l) (l DiffMK)
tickThenApply = (LedgerResult l (l DiffMK) -> l DiffMK)
-> ExceptT (LedgerErr l) Identity (LedgerResult l (l DiffMK))
-> ExceptT (LedgerErr l) Identity (l DiffMK)
forall a b.
(a -> b)
-> ExceptT (LedgerErr l) Identity a
-> ExceptT (LedgerErr l) Identity b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap LedgerResult l (l DiffMK) -> l DiffMK
forall (l :: LedgerStateKind) a. LedgerResult l a -> a
lrResult (ExceptT (LedgerErr l) Identity (LedgerResult l (l DiffMK))
 -> ExceptT (LedgerErr l) Identity (l DiffMK))
-> (ComputeLedgerEvents
    -> LedgerCfg l
    -> blk
    -> l ValuesMK
    -> ExceptT (LedgerErr l) Identity (LedgerResult l (l DiffMK)))
-> ComputeLedgerEvents
-> LedgerCfg l
-> blk
-> l ValuesMK
-> ExceptT (LedgerErr l) Identity (l DiffMK)
forall y z x0 x1 x2 x3.
(y -> z)
-> (x0 -> x1 -> x2 -> x3 -> y) -> x0 -> x1 -> x2 -> x3 -> z
...: ComputeLedgerEvents
-> LedgerCfg l
-> blk
-> l ValuesMK
-> ExceptT (LedgerErr l) Identity (LedgerResult l (l DiffMK))
forall (l :: LedgerStateKind) blk.
ApplyBlock l blk =>
ComputeLedgerEvents
-> LedgerCfg l
-> blk
-> l ValuesMK
-> Except (LedgerErr l) (LedgerResult l (l DiffMK))
tickThenApplyLedgerResult

tickThenReapply ::
     forall l blk.
     ApplyBlock l blk
  => ComputeLedgerEvents
  -> LedgerCfg l
  -> blk
  -> l ValuesMK
  -> l DiffMK
tickThenReapply :: forall (l :: LedgerStateKind) blk.
ApplyBlock l blk =>
ComputeLedgerEvents -> LedgerCfg l -> blk -> l ValuesMK -> l DiffMK
tickThenReapply = LedgerResult l (l DiffMK) -> l DiffMK
forall (l :: LedgerStateKind) a. LedgerResult l a -> a
lrResult (LedgerResult l (l DiffMK) -> l DiffMK)
-> (ComputeLedgerEvents
    -> LedgerCfg l -> blk -> l ValuesMK -> LedgerResult l (l DiffMK))
-> ComputeLedgerEvents
-> LedgerCfg l
-> blk
-> l ValuesMK
-> l DiffMK
forall y z x0 x1 x2 x3.
(y -> z)
-> (x0 -> x1 -> x2 -> x3 -> y) -> x0 -> x1 -> x2 -> x3 -> z
...: ComputeLedgerEvents
-> LedgerCfg l -> blk -> l ValuesMK -> LedgerResult l (l DiffMK)
forall (l :: LedgerStateKind) blk.
ApplyBlock l blk =>
ComputeLedgerEvents
-> LedgerCfg l -> blk -> l ValuesMK -> LedgerResult l (l DiffMK)
tickThenReapplyLedgerResult

foldLedger ::
     ApplyBlock l blk
  => ComputeLedgerEvents -> LedgerCfg l -> [blk] -> l ValuesMK -> Except (LedgerErr l) (l ValuesMK)
foldLedger :: forall (l :: LedgerStateKind) blk.
ApplyBlock l blk =>
ComputeLedgerEvents
-> LedgerCfg l
-> [blk]
-> l ValuesMK
-> Except (LedgerErr l) (l ValuesMK)
foldLedger ComputeLedgerEvents
evs LedgerCfg l
cfg = (blk -> l ValuesMK -> ExceptT (LedgerErr l) Identity (l ValuesMK))
-> [blk]
-> l ValuesMK
-> ExceptT (LedgerErr l) Identity (l ValuesMK)
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m b) -> [a] -> b -> m b
repeatedlyM (\blk
blk l ValuesMK
state -> l ValuesMK -> LedgerTables l KeysMK -> l DiffMK -> l ValuesMK
forall (l :: LedgerStateKind) (l' :: LedgerStateKind).
(SameUtxoTypes l l', HasLedgerTables l, HasLedgerTables l') =>
l ValuesMK -> LedgerTables l KeysMK -> l' DiffMK -> l' ValuesMK
applyDiffForKeys l ValuesMK
state (blk -> LedgerTables l KeysMK
forall (l :: LedgerStateKind) blk.
ApplyBlock l blk =>
blk -> LedgerTables l KeysMK
getBlockKeySets blk
blk) (l DiffMK -> l ValuesMK)
-> ExceptT (LedgerErr l) Identity (l DiffMK)
-> ExceptT (LedgerErr l) Identity (l ValuesMK)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ComputeLedgerEvents
-> LedgerCfg l
-> blk
-> l ValuesMK
-> ExceptT (LedgerErr l) Identity (l DiffMK)
forall (l :: LedgerStateKind) blk.
ApplyBlock l blk =>
ComputeLedgerEvents
-> LedgerCfg l
-> blk
-> l ValuesMK
-> Except (LedgerErr l) (l DiffMK)
tickThenApply ComputeLedgerEvents
evs LedgerCfg l
cfg blk
blk l ValuesMK
state)

refoldLedger ::
     ApplyBlock l blk
  => ComputeLedgerEvents -> LedgerCfg l -> [blk] -> l ValuesMK -> l ValuesMK
refoldLedger :: forall (l :: LedgerStateKind) blk.
ApplyBlock l blk =>
ComputeLedgerEvents
-> LedgerCfg l -> [blk] -> l ValuesMK -> l ValuesMK
refoldLedger ComputeLedgerEvents
evs LedgerCfg l
cfg = (blk -> l ValuesMK -> l ValuesMK)
-> [blk] -> l ValuesMK -> l ValuesMK
forall a b. (a -> b -> b) -> [a] -> b -> b
repeatedly (\blk
blk l ValuesMK
state -> l ValuesMK -> LedgerTables l KeysMK -> l DiffMK -> l ValuesMK
forall (l :: LedgerStateKind) (l' :: LedgerStateKind).
(SameUtxoTypes l l', HasLedgerTables l, HasLedgerTables l') =>
l ValuesMK -> LedgerTables l KeysMK -> l' DiffMK -> l' ValuesMK
applyDiffForKeys l ValuesMK
state (blk -> LedgerTables l KeysMK
forall (l :: LedgerStateKind) blk.
ApplyBlock l blk =>
blk -> LedgerTables l KeysMK
getBlockKeySets blk
blk) (l DiffMK -> l ValuesMK) -> l DiffMK -> l ValuesMK
forall a b. (a -> b) -> a -> b
$ ComputeLedgerEvents -> LedgerCfg l -> blk -> l ValuesMK -> l DiffMK
forall (l :: LedgerStateKind) blk.
ApplyBlock l blk =>
ComputeLedgerEvents -> LedgerCfg l -> blk -> l ValuesMK -> l DiffMK
tickThenReapply ComputeLedgerEvents
evs LedgerCfg l
cfg blk
blk l ValuesMK
state)

{-------------------------------------------------------------------------------
  Short-hand
-------------------------------------------------------------------------------}

ledgerTipPoint ::
     UpdateLedger blk
  => LedgerState blk mk -> Point blk
ledgerTipPoint :: forall blk (mk :: MapKind).
UpdateLedger blk =>
LedgerState blk mk -> Point blk
ledgerTipPoint = Point (LedgerState blk) -> Point blk
forall {k1} {k2} (b :: k1) (b' :: k2).
Coercible (HeaderHash b) (HeaderHash b') =>
Point b -> Point b'
castPoint (Point (LedgerState blk) -> Point blk)
-> (LedgerState blk mk -> Point (LedgerState blk))
-> LedgerState blk mk
-> Point blk
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LedgerState blk mk -> Point (LedgerState blk)
forall (mk :: MapKind).
LedgerState blk mk -> Point (LedgerState blk)
forall (l :: LedgerStateKind) (mk :: MapKind).
GetTip l =>
l mk -> Point l
getTip

ledgerTipHash ::
     UpdateLedger blk
  => LedgerState blk mk -> ChainHash blk
ledgerTipHash :: forall blk (mk :: MapKind).
UpdateLedger blk =>
LedgerState blk mk -> ChainHash blk
ledgerTipHash = Point blk -> ChainHash blk
forall {k} (block :: k). Point block -> ChainHash block
pointHash (Point blk -> ChainHash blk)
-> (LedgerState blk mk -> Point blk)
-> LedgerState blk mk
-> ChainHash blk
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LedgerState blk mk -> Point blk
forall blk (mk :: MapKind).
UpdateLedger blk =>
LedgerState blk mk -> Point blk
ledgerTipPoint

ledgerTipSlot ::
     UpdateLedger blk
  => LedgerState blk mk -> WithOrigin SlotNo
ledgerTipSlot :: forall blk (mk :: MapKind).
UpdateLedger blk =>
LedgerState blk mk -> WithOrigin SlotNo
ledgerTipSlot = Point blk -> WithOrigin SlotNo
forall {k} (block :: k). Point block -> WithOrigin SlotNo
pointSlot (Point blk -> WithOrigin SlotNo)
-> (LedgerState blk mk -> Point blk)
-> LedgerState blk mk
-> WithOrigin SlotNo
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LedgerState blk mk -> Point blk
forall blk (mk :: MapKind).
UpdateLedger blk =>
LedgerState blk mk -> Point blk
ledgerTipPoint