@@ -22,6 +22,8 @@ module Cardano.Api.LedgerState
22
22
, LedgerStateMary
23
23
, LedgerStateAlonzo
24
24
)
25
+ , encodeLedgerState
26
+ , decodeLedgerState
25
27
, initialLedgerState
26
28
, applyBlock
27
29
, ValidationMode (.. )
@@ -52,6 +54,8 @@ module Cardano.Api.LedgerState
52
54
53
55
import Prelude
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
@@ -140,6 +144,7 @@ import Cardano.Slotting.Slot (WithOrigin (At, Origin))
140
144
import qualified Cardano.Slotting.Slot as Slot
141
145
import qualified Ouroboros.Consensus.Block.Abstract as Consensus
142
146
import qualified Ouroboros.Consensus.Byron.Ledger.Block as Byron
147
+ import qualified Ouroboros.Consensus.Byron.Ledger.Ledger as Byron
143
148
import qualified Ouroboros.Consensus.Cardano as Consensus
144
149
import qualified Ouroboros.Consensus.Cardano.Block as Consensus
145
150
import qualified Ouroboros.Consensus.Cardano.CanHardFork as Consensus
@@ -148,6 +153,7 @@ import qualified Ouroboros.Consensus.Config as Consensus
148
153
import qualified Ouroboros.Consensus.HardFork.Combinator as Consensus
149
154
import qualified Ouroboros.Consensus.HardFork.Combinator.AcrossEras as HFC
150
155
import qualified Ouroboros.Consensus.HardFork.Combinator.Basics as HFC
156
+ import qualified Ouroboros.Consensus.HardFork.Combinator.Serialisation.Common as HFC
151
157
import qualified Ouroboros.Consensus.Ledger.Abstract as Ledger
152
158
import Ouroboros.Consensus.Ledger.Basics (LedgerResult (lrEvents ), lrResult )
153
159
import qualified Ouroboros.Consensus.Ledger.Extended as Ledger
@@ -864,6 +870,31 @@ newtype LedgerState = LedgerState
864
870
(Consensus. CardanoEras Consensus. StandardCrypto ))
865
871
}
866
872
873
+ encodeLedgerState :: LedgerState -> CBOR. Encoding
874
+ encodeLedgerState (LedgerState (HFC. HardForkLedgerState st)) =
875
+ HFC. encodeTelescope
876
+ (byron :* shelley :* allegra :* mary :* alonzo :* babbage :* Nil )
877
+ st
878
+ where
879
+ byron = fn (K . Byron. encodeByronLedgerState)
880
+ shelley = fn (K . Shelley. encodeShelleyLedgerState)
881
+ allegra = fn (K . Shelley. encodeShelleyLedgerState)
882
+ mary = fn (K . Shelley. encodeShelleyLedgerState)
883
+ alonzo = fn (K . Shelley. encodeShelleyLedgerState)
884
+ babbage = fn (K . Shelley. encodeShelleyLedgerState)
885
+
886
+ decodeLedgerState :: forall s . CBOR. Decoder s LedgerState
887
+ decodeLedgerState =
888
+ LedgerState . HFC. HardForkLedgerState
889
+ <$> HFC. decodeTelescope (byron :* shelley :* allegra :* mary :* alonzo :* babbage :* Nil )
890
+ where
891
+ byron = Comp Byron. decodeByronLedgerState
892
+ shelley = Comp Shelley. decodeShelleyLedgerState
893
+ allegra = Comp Shelley. decodeShelleyLedgerState
894
+ mary = Comp Shelley. decodeShelleyLedgerState
895
+ alonzo = Comp Shelley. decodeShelleyLedgerState
896
+ babbage = Comp Shelley. decodeShelleyLedgerState
897
+
867
898
type LedgerStateEvents = (LedgerState , [LedgerEvent ])
868
899
869
900
toLedgerStateEvents ::
0 commit comments