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