Skip to content

Commit

Permalink
remove UnknownMeasureName and make MeasureName just a newtype around …
Browse files Browse the repository at this point in the history
…Text, so that network doesn't need to know about anything ledger-specific
  • Loading branch information
fraser-iohk committed Sep 4, 2024
1 parent 434861f commit d6eb7bb
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
Expand All @@ -16,7 +15,6 @@ module Ouroboros.Network.Protocol.LocalTxMonitor.Codec

import Control.Monad
import Control.Monad.Class.MonadST
import Data.Functor ((<&>))

import Network.TypedProtocol.Codec.CBOR

Expand Down Expand Up @@ -185,20 +183,10 @@ decodeMeasureMap = do
pure $ Map.fromList mapContents

encodeMeasureName :: MeasureName -> CBOR.Encoding
encodeMeasureName = CBOR.encodeString . \case
TransactionBytes -> "transaction_bytes"
ExUnitsMemory -> "ex_units_memory"
ExUnitsSteps -> "ex_units_steps"
ReferenceScriptsBytes -> "reference_scripts_bytes"
MeasureNameFromFuture (UnknownMeasureName n) -> n
encodeMeasureName (MeasureName t) = CBOR.encodeString t

decodeMeasureName :: CBOR.Decoder s MeasureName
decodeMeasureName = CBOR.decodeString <&> \case
"transaction_bytes" -> TransactionBytes
"ex_units_memory" -> ExUnitsMemory
"ex_units_steps" -> ExUnitsSteps
"reference_scripts_bytes" -> ReferenceScriptsBytes
unknownKey -> MeasureNameFromFuture (UnknownMeasureName unknownKey)
decodeMeasureName = MeasureName <$> CBOR.decodeString

encodeSizeAndCapacity :: SizeAndCapacity Integer -> CBOR.Encoding
encodeSizeAndCapacity sc =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,7 @@ data SizeAndCapacity a = SizeAndCapacity
instance Functor SizeAndCapacity where
fmap f (SizeAndCapacity s c) = SizeAndCapacity (f s) (f c)

data MeasureName
= TransactionBytes
| ExUnitsMemory
| ExUnitsSteps
| ReferenceScriptsBytes
| MeasureNameFromFuture !UnknownMeasureName
deriving (Generic, Eq, Ord, Show, NFData)

newtype UnknownMeasureName = UnknownMeasureName Text
newtype MeasureName = MeasureName Text
deriving (Generic, Eq, Ord, Show, NFData)

data MempoolMeasures = MempoolMeasures
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,30 +224,19 @@ instance Arbitrary MempoolMeasures where
<*> arbitrary

instance Arbitrary MeasureName where
arbitrary = frequency
[ (5, pure TransactionBytes)
, (5, pure ExUnitsMemory)
, (5, pure ExUnitsSteps)
, (5, pure ReferenceScriptsBytes)
, (1, MeasureNameFromFuture <$> genUnknownMeasureName)
arbitrary = MeasureName <$> frequency
[ (9, genKnownMeasureName)
, (1, genUnknownMeasureName)
]
where
knownMeasureNames =
genKnownMeasureName =
Text.pack <$> elements
[ "transaction_bytes"
, "reference_scripts"
, "ex_units_memory"
, "ex_units_steps"
]

-- We need to generate a measure name that is currently unknown (because
-- we support forward-compatibility in the protocol), because accidentally
-- generating a known measure name with `arbitrary` will cause the
-- deserialization to fail to roundtrip
genUnknownMeasureName = do
name <- arbitrary
if name `elem` knownMeasureNames
then discard
else pure $ UnknownMeasureName $ Text.pack name
genUnknownMeasureName = Text.pack <$> arbitrary

instance Arbitrary a => Arbitrary (SizeAndCapacity a) where
arbitrary =
Expand Down

0 comments on commit d6eb7bb

Please sign in to comment.