Skip to content

Commit

Permalink
Add 'reapplyBlockTransition'.
Browse files Browse the repository at this point in the history
This can be used to implement `reapplyLedgerBlock` in ouroboros-network.
  • Loading branch information
nc6 committed Jul 8, 2020
1 parent 65e57d9 commit 1d1a937
Showing 1 changed file with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ module Shelley.Spec.Ledger.API.Validation
chainChecks,
applyTickTransition,
applyBlockTransition,
reapplyBlockTransition,
)
where

import Cardano.Prelude (NoUnexpectedThunks (..))
import Control.Arrow (left, right)
import Control.Monad.Except
import Control.Monad.Trans.Reader (runReader)
import Control.State.Transition.Extended (TRC (..), applySTS)
import Control.State.Transition.Extended (TRC (..), applySTS, reapplySTS)
import Data.Either (fromRight)
import GHC.Generics (Generic)
import Shelley.Spec.Ledger.BaseTypes (Globals (..))
Expand Down Expand Up @@ -134,3 +135,34 @@ applyBlockTransition globals state blk =
STS.BbodyState
(LedgerState.esLState $ LedgerState.nesEs state)
(LedgerState.nesBcur state)

-- | Re-apply a ledger block to the same state it has been applied to before.
--
-- This function does no validation of whether the block applies successfully;
-- the caller implicitly guarantees that they have previously called
-- `applyBlockTransition` on the same block and that this was successful.
reapplyBlockTransition ::
forall crypto.
( Crypto crypto,
DSignable crypto (Hash crypto (Tx.TxBody crypto))
) =>
Globals ->
ShelleyState crypto ->
Block crypto ->
ShelleyState crypto
reapplyBlockTransition globals state blk =
updateShelleyState state res
where
res =
flip runReader globals . reapplySTS @(STS.BBODY crypto) $
TRC (mkBbodyEnv state, bbs, blk)
updateShelleyState ::
ShelleyState crypto ->
STS.BbodyState crypto ->
ShelleyState crypto
updateShelleyState ss (STS.BbodyState ls bcur) =
LedgerState.updateNES ss bcur ls
bbs =
STS.BbodyState
(LedgerState.esLState $ LedgerState.nesEs state)
(LedgerState.nesBcur state)

0 comments on commit 1d1a937

Please sign in to comment.