Skip to content

Commit

Permalink
Introduce 'applyBlockOpts'
Browse files Browse the repository at this point in the history
We do not introduce 'reapplyBlockOpts' since this already is a
customised version of 'applyBlockOpts'.

We also do not currently expose a mempool interface that allows event
logging.
  • Loading branch information
nc6 committed Aug 2, 2021
1 parent 74a4dd2 commit 494becd
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ module Control.State.Transition.Extended
liftSTS,
tellEvent,
tellEvents,
EventReturnTypeRep,
mapEventReturn,

-- * Apply STS
Expand All @@ -61,6 +62,7 @@ module Control.State.Transition.Extended
applySTS,
applySTSIndifferently,
reapplySTS,
globalAssertionPolicy,

-- * Exported to allow running rules independently
applySTSInternal,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
-- API.
module Shelley.Spec.Ledger.API.Validation
( ApplyBlock (..),
applyBlock,
applyTick,
TickTransitionError (..),
BlockTransitionError (..),
chainChecks,
Expand Down Expand Up @@ -71,46 +73,58 @@ class
--
-- This handles checks and updates that happen on a slot tick, as well as a
-- few header level checks, such as size constraints.
applyTick ::
applyTickOpts ::
ApplySTSOpts ep ->
Globals ->
NewEpochState era ->
SlotNo ->
NewEpochState era
default applyTick ::
EventReturnType ep (Core.EraRule "TICK" era) (NewEpochState era)
default applyTickOpts ::
ApplySTSOpts ep ->
Globals ->
NewEpochState era ->
SlotNo ->
NewEpochState era
applyTick globals state hdr =
either err id . flip runReader globals
. applySTS @(Core.EraRule "TICK" era)
EventReturnType ep (Core.EraRule "TICK" era) (NewEpochState era)
applyTickOpts opts globals state hdr =
either err id
. flip runReader globals
. applySTSOptsEither @(Core.EraRule "TICK" era) opts
$ TRC ((), state, hdr)
where
err :: Show a => a -> b
err msg = error $ "Panic! applyTick failed: " <> show msg

-- | Apply the block level ledger transition.
applyBlock ::
MonadError (BlockTransitionError era) m =>
applyBlockOpts ::
forall ep m.
(EventReturnTypeRep ep, MonadError (BlockTransitionError era) m) =>
ApplySTSOpts ep ->
Globals ->
NewEpochState era ->
Block era ->
m (NewEpochState era)
default applyBlock ::
(MonadError (BlockTransitionError era) m) =>
m (EventReturnType ep (Core.EraRule "BBODY" era) (NewEpochState era))
default applyBlockOpts ::
forall ep m.
(EventReturnTypeRep ep, MonadError (BlockTransitionError era) m) =>
ApplySTSOpts ep ->
Globals ->
NewEpochState era ->
Block era ->
m (NewEpochState era)
applyBlock globals state blk =
m (EventReturnType ep (Core.EraRule "BBODY" era) (NewEpochState era))
applyBlockOpts opts globals state blk =
liftEither
. right (updateNewEpochState state)
. left BlockTransitionError
. right
( mapEventReturn @ep @(Core.EraRule "BBODY" era) $
updateNewEpochState state
)
$ res
where
res =
flip runReader globals . applySTS @(Core.EraRule "BBODY" era) $
TRC (mkBbodyEnv state, bbs, blk)
flip runReader globals
. applySTSOptsEither @(Core.EraRule "BBODY" era)
opts
$ TRC (mkBbodyEnv state, bbs, blk)
bbs =
STS.BbodyState
(LedgerState.esLState $ LedgerState.nesEs state)
Expand Down Expand Up @@ -142,6 +156,36 @@ class
(LedgerState.esLState $ LedgerState.nesEs state)
(LedgerState.nesBcur state)

applyTick ::
ApplyBlock era =>
Globals ->
NewEpochState era ->
SlotNo ->
NewEpochState era
applyTick =
applyTickOpts $
ApplySTSOpts
{ asoAssertions = globalAssertionPolicy,
asoValidation = ValidateAll,
asoEvents = EPDiscard
}

applyBlock ::
( ApplyBlock era,
MonadError (BlockTransitionError era) m
) =>
Globals ->
NewEpochState era ->
Block era ->
m (NewEpochState era)
applyBlock =
applyBlockOpts $
ApplySTSOpts
{ asoAssertions = globalAssertionPolicy,
asoValidation = ValidateAll,
asoEvents = EPDiscard
}

instance PraosCrypto crypto => ApplyBlock (ShelleyEra crypto)

{-------------------------------------------------------------------------------
Expand Down

0 comments on commit 494becd

Please sign in to comment.