@@ -24,6 +24,8 @@ module Cardano.Api.LedgerState
24
24
, LedgerStateMary
25
25
, LedgerStateAlonzo
26
26
)
27
+ , encodeLedgerState
28
+ , decodeLedgerState
27
29
, initialLedgerState
28
30
, applyBlock
29
31
, ValidationMode (.. )
@@ -52,6 +54,8 @@ module Cardano.Api.LedgerState
52
54
)
53
55
where
54
56
57
+ import qualified Codec.CBOR.Decoding as CBOR
58
+ import qualified Codec.CBOR.Encoding as CBOR
55
59
import Control.Exception
56
60
import Control.Monad (when )
57
61
import Control.Monad.Trans.Class
@@ -78,7 +82,7 @@ import qualified Data.Sequence as Seq
78
82
import Data.Set (Set )
79
83
import qualified Data.Set as Set
80
84
import Data.Sharing (FromSharedCBOR , Interns , Share )
81
- import Data.SOP.Strict (NP (.. ))
85
+ import Data.SOP.Strict (K ( .. ), NP (.. ), fn , (:.:) ( Comp ))
82
86
import Data.Text (Text )
83
87
import qualified Data.Text as Text
84
88
import qualified Data.Text.Encoding as Text
@@ -142,6 +146,7 @@ import Cardano.Slotting.Slot (WithOrigin (At, Origin))
142
146
import qualified Cardano.Slotting.Slot as Slot
143
147
import qualified Ouroboros.Consensus.Block.Abstract as Consensus
144
148
import qualified Ouroboros.Consensus.Byron.Ledger.Block as Byron
149
+ import qualified Ouroboros.Consensus.Byron.Ledger.Ledger as Byron
145
150
import qualified Ouroboros.Consensus.Cardano as Consensus
146
151
import qualified Ouroboros.Consensus.Cardano.Block as Consensus
147
152
import qualified Ouroboros.Consensus.Cardano.CanHardFork as Consensus
@@ -150,6 +155,7 @@ import qualified Ouroboros.Consensus.Config as Consensus
150
155
import qualified Ouroboros.Consensus.HardFork.Combinator as Consensus
151
156
import qualified Ouroboros.Consensus.HardFork.Combinator.AcrossEras as HFC
152
157
import qualified Ouroboros.Consensus.HardFork.Combinator.Basics as HFC
158
+ import qualified Ouroboros.Consensus.HardFork.Combinator.Serialisation.Common as HFC
153
159
import qualified Ouroboros.Consensus.Ledger.Abstract as Ledger
154
160
import Ouroboros.Consensus.Ledger.Basics (LedgerResult (lrEvents ), lrResult )
155
161
import qualified Ouroboros.Consensus.Ledger.Extended as Ledger
@@ -866,6 +872,31 @@ newtype LedgerState = LedgerState
866
872
(Consensus. CardanoEras Consensus. StandardCrypto ))
867
873
}
868
874
875
+ encodeLedgerState :: LedgerState -> CBOR. Encoding
876
+ encodeLedgerState (LedgerState (HFC. HardForkLedgerState st)) =
877
+ HFC. encodeTelescope
878
+ (byron :* shelley :* allegra :* mary :* alonzo :* babbage :* Nil )
879
+ st
880
+ where
881
+ byron = fn (K . Byron. encodeByronLedgerState)
882
+ shelley = fn (K . Shelley. encodeShelleyLedgerState)
883
+ allegra = fn (K . Shelley. encodeShelleyLedgerState)
884
+ mary = fn (K . Shelley. encodeShelleyLedgerState)
885
+ alonzo = fn (K . Shelley. encodeShelleyLedgerState)
886
+ babbage = fn (K . Shelley. encodeShelleyLedgerState)
887
+
888
+ decodeLedgerState :: forall s . CBOR. Decoder s LedgerState
889
+ decodeLedgerState =
890
+ LedgerState . HFC. HardForkLedgerState
891
+ <$> HFC. decodeTelescope (byron :* shelley :* allegra :* mary :* alonzo :* babbage :* Nil )
892
+ where
893
+ byron = Comp Byron. decodeByronLedgerState
894
+ shelley = Comp Shelley. decodeShelleyLedgerState
895
+ allegra = Comp Shelley. decodeShelleyLedgerState
896
+ mary = Comp Shelley. decodeShelleyLedgerState
897
+ alonzo = Comp Shelley. decodeShelleyLedgerState
898
+ babbage = Comp Shelley. decodeShelleyLedgerState
899
+
869
900
type LedgerStateEvents = (LedgerState , [LedgerEvent ])
870
901
871
902
toLedgerStateEvents ::
0 commit comments