Skip to content

Commit

Permalink
Encode version with serialized event.
Browse files Browse the repository at this point in the history
  So that we know what version to use for decoding an event. This is necessary because events are encoded according to the version of the era they're emitted from; which may be different across the entire file even for similar events.
  • Loading branch information
KtorZ committed Sep 22, 2023
1 parent 951a2b3 commit 9069536
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
28 changes: 15 additions & 13 deletions cardano-node/src/Cardano/Node/LedgerEvent.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ module Cardano.Node.LedgerEvent (

import Cardano.Prelude hiding (All, Sum)

import Cardano.Ledger.Binary (DecCBOR(..), EncCBOR(..), Version, serialize',
unsafeDeserialize, toPlainDecoder)
import Cardano.Ledger.Binary (DecCBOR(..), EncCBOR(..), Version, fromCBOR,
serialize', toCBOR, toPlainDecoder)
import Cardano.Ledger.Binary.Coders (Decode(..), Encode (..), encode, (!>),
(<!), decode)
import Cardano.Ledger.Coin (Coin (..), DeltaCoin (..))
Expand All @@ -53,10 +53,10 @@ import Cardano.Ledger.Shelley.Rules (RupdEvent (..),
ShelleyNewEpochEvent, ShelleyPoolreapEvent (..),
ShelleyTickEvent (..))
import Cardano.Slotting.Slot (SlotNo, EpochNo (..))
import qualified Codec.CBOR.Write as CBOR
import Codec.CBOR.Read(deserialiseFromBytes)
import Control.State.Transition (Event)
import Data.ByteString.Short(ShortByteString)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as LBS
import qualified Data.Map.Strict as Map
import Data.SOP.Strict (All, K (..), NS(..), hcmap, hcollapse)
Expand All @@ -67,7 +67,7 @@ import Ouroboros.Consensus.Cardano.Block (AllegraEra, AlonzoEra,
BabbageEra, CardanoEras, ConwayEra, HardForkBlock,
MaryEra, ShelleyEra)
import Ouroboros.Consensus.HardFork.Combinator.AcrossEras
(OneEraLedgerEvent(..), OneEraHash(..))
(OneEraLedgerEvent(..))
import Ouroboros.Consensus.Ledger.Abstract (AuxLedgerEvent,
LedgerState)
import Ouroboros.Consensus.Shelley.Ledger (ShelleyBlock,
Expand Down Expand Up @@ -468,19 +468,21 @@ instance DecCBOR AnchoredEvent where

serializeEvent :: Version -> AnchoredEvent -> ByteString
serializeEvent codecVersion event =
serialize' codecVersion event

deserializeEvent :: Version -> LBS.ByteString -> Maybe AnchoredEvent
deserializeEvent codecVersion bytes =
case deserialiseFromBytes (toPlainDecoder codecVersion decCBOR) bytes of
Right (_, event) -> Just event
Left _ -> Nothing

CBOR.toStrictByteString (toCBOR codecVersion) <> serialize' codecVersion event

deserializeEvent :: LBS.ByteString -> Maybe AnchoredEvent
deserializeEvent bytes = do
case deserialiseFromBytes @Version fromCBOR bytes of
Right (rest, version) ->
case deserialiseFromBytes (toPlainDecoder version decCBOR) rest of
Right (_, event) -> Just event
Left{} -> Nothing
Left{} -> Nothing

-- IO action to read ledger events in binary form
tailEvent :: FilePath -> IO ()
tailEvent eventsDb =
withFile eventsDb ReadMode $ \ h -> LBS.hGetContents h >>= go

where
go bytes = do
let version = maxBound
Expand Down
5 changes: 3 additions & 2 deletions cardano-node/test/Test/Cardano/Node/LedgerEvent.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import qualified Hedgehog.Range as Range
prop_roundtrip_LedgerEvent_CBOR :: Property
prop_roundtrip_LedgerEvent_CBOR =
Hedgehog.property $ do
version <- Hedgehog.forAll Gen.enumBounded
event <- Hedgehog.forAll genEvent
footnote ("serialized event: " <> show (Hex.encode $ serializeEvent maxBound event))
Hedgehog.tripping event (serializeEvent maxBound) (deserializeEvent maxBound . fromStrict)
footnote ("serialized event: " <> show (Hex.encode $ serializeEvent version event))
Hedgehog.tripping event (serializeEvent version) (deserializeEvent . fromStrict)

genEvent :: Hedgehog.Gen AnchoredEvent
genEvent =
Expand Down

0 comments on commit 9069536

Please sign in to comment.