Skip to content

Commit

Permalink
Merge #3993
Browse files Browse the repository at this point in the history
3993: PLT-106: Add encoder and decoder for `LedgerState` r=zliu41 a=zliu41

One use case is for `foldBlocks` to checkpoint the ledger state to avoid having to start from Genisys. cc `@JaredCorduan` 

Co-authored-by: Ziyang Liu <unsafeFixIO@gmail.com>
  • Loading branch information
iohk-bors[bot] and zliu41 committed Feb 8, 2023
2 parents 23e4052 + 1896e34 commit 94b0341
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 2 additions & 0 deletions cardano-api/src/Cardano/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,8 @@ module Cardano.Api (
envSecurityParam,
LedgerState(..),
initialLedgerState,
encodeLedgerState,
decodeLedgerState,
applyBlock,
ValidationMode(..),

Expand Down
32 changes: 31 additions & 1 deletion cardano-api/src/Cardano/Api/LedgerState.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ module Cardano.Api.LedgerState
, LedgerStateMary
, LedgerStateAlonzo
)
, encodeLedgerState
, decodeLedgerState
, initialLedgerState
, applyBlock
, ValidationMode(..)
Expand Down Expand Up @@ -52,6 +54,7 @@ module Cardano.Api.LedgerState
)
where

import qualified Cardano.Binary as CBOR
import Control.Exception
import Control.Monad (when)
import Control.Monad.Trans.Class
Expand All @@ -78,7 +81,7 @@ import qualified Data.Sequence as Seq
import Data.Set (Set)
import qualified Data.Set as Set
import Data.Sharing (FromSharedCBOR, Interns, Share)
import Data.SOP.Strict (NP (..))
import Data.SOP.Strict (K (..), NP (..), fn, (:.:) (Comp))
import Data.Text (Text)
import qualified Data.Text as Text
import qualified Data.Text.Encoding as Text
Expand Down Expand Up @@ -142,6 +145,7 @@ import Cardano.Slotting.Slot (WithOrigin (At, Origin))
import qualified Cardano.Slotting.Slot as Slot
import qualified Ouroboros.Consensus.Block.Abstract as Consensus
import qualified Ouroboros.Consensus.Byron.Ledger.Block as Byron
import qualified Ouroboros.Consensus.Byron.Ledger.Ledger as Byron
import qualified Ouroboros.Consensus.Cardano as Consensus
import qualified Ouroboros.Consensus.Cardano.Block as Consensus
import qualified Ouroboros.Consensus.Cardano.CanHardFork as Consensus
Expand All @@ -150,6 +154,7 @@ import qualified Ouroboros.Consensus.Config as Consensus
import qualified Ouroboros.Consensus.HardFork.Combinator as Consensus
import qualified Ouroboros.Consensus.HardFork.Combinator.AcrossEras as HFC
import qualified Ouroboros.Consensus.HardFork.Combinator.Basics as HFC
import qualified Ouroboros.Consensus.HardFork.Combinator.Serialisation.Common as HFC
import qualified Ouroboros.Consensus.Ledger.Abstract as Ledger
import Ouroboros.Consensus.Ledger.Basics (LedgerResult (lrEvents), lrResult)
import qualified Ouroboros.Consensus.Ledger.Extended as Ledger
Expand Down Expand Up @@ -866,6 +871,31 @@ newtype LedgerState = LedgerState
(Consensus.CardanoEras Consensus.StandardCrypto))
}

encodeLedgerState :: LedgerState -> CBOR.Encoding
encodeLedgerState (LedgerState (HFC.HardForkLedgerState st)) =
HFC.encodeTelescope
(byron :* shelley :* allegra :* mary :* alonzo :* babbage :* Nil)
st
where
byron = fn (K . Byron.encodeByronLedgerState)
shelley = fn (K . Shelley.encodeShelleyLedgerState)
allegra = fn (K . Shelley.encodeShelleyLedgerState)
mary = fn (K . Shelley.encodeShelleyLedgerState)
alonzo = fn (K . Shelley.encodeShelleyLedgerState)
babbage = fn (K . Shelley.encodeShelleyLedgerState)

decodeLedgerState :: forall s. CBOR.Decoder s LedgerState
decodeLedgerState =
LedgerState . HFC.HardForkLedgerState
<$> HFC.decodeTelescope (byron :* shelley :* allegra :* mary :* alonzo :* babbage :* Nil)
where
byron = Comp Byron.decodeByronLedgerState
shelley = Comp Shelley.decodeShelleyLedgerState
allegra = Comp Shelley.decodeShelleyLedgerState
mary = Comp Shelley.decodeShelleyLedgerState
alonzo = Comp Shelley.decodeShelleyLedgerState
babbage = Comp Shelley.decodeShelleyLedgerState

type LedgerStateEvents = (LedgerState, [LedgerEvent])

toLedgerStateEvents ::
Expand Down

0 comments on commit 94b0341

Please sign in to comment.