From 9488451e42edb01b8739b2b272821cb4fd3972c9 Mon Sep 17 00:00:00 2001 From: Fraser Murray Date: Wed, 4 Sep 2024 16:30:27 +0100 Subject: [PATCH] remove UnknownMeasureName and make MeasureName just a newtype around Text, so that network doesn't need to know about anything ledger-specific --- .../Network/Protocol/LocalTxMonitor/Codec.hs | 16 ++----------- .../Network/Protocol/LocalTxMonitor/Type.hs | 10 +------- .../Network/Protocol/LocalTxMonitor/Test.hs | 23 +++++-------------- 3 files changed, 9 insertions(+), 40 deletions(-) diff --git a/ouroboros-network-protocols/src/Ouroboros/Network/Protocol/LocalTxMonitor/Codec.hs b/ouroboros-network-protocols/src/Ouroboros/Network/Protocol/LocalTxMonitor/Codec.hs index 0db0c60cbc6..f64c377e8af 100644 --- a/ouroboros-network-protocols/src/Ouroboros/Network/Protocol/LocalTxMonitor/Codec.hs +++ b/ouroboros-network-protocols/src/Ouroboros/Network/Protocol/LocalTxMonitor/Codec.hs @@ -2,7 +2,6 @@ {-# LANGUAGE GADTs #-} {-# LANGUAGE LambdaCase #-} {-# LANGUAGE NamedFieldPuns #-} -{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE PolyKinds #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE ScopedTypeVariables #-} @@ -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 @@ -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 = diff --git a/ouroboros-network-protocols/src/Ouroboros/Network/Protocol/LocalTxMonitor/Type.hs b/ouroboros-network-protocols/src/Ouroboros/Network/Protocol/LocalTxMonitor/Type.hs index a5ef3c4011d..34317e2e2f9 100644 --- a/ouroboros-network-protocols/src/Ouroboros/Network/Protocol/LocalTxMonitor/Type.hs +++ b/ouroboros-network-protocols/src/Ouroboros/Network/Protocol/LocalTxMonitor/Type.hs @@ -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 diff --git a/ouroboros-network-protocols/testlib/Ouroboros/Network/Protocol/LocalTxMonitor/Test.hs b/ouroboros-network-protocols/testlib/Ouroboros/Network/Protocol/LocalTxMonitor/Test.hs index f21da46c9dc..ed0c6fdf254 100644 --- a/ouroboros-network-protocols/testlib/Ouroboros/Network/Protocol/LocalTxMonitor/Test.hs +++ b/ouroboros-network-protocols/testlib/Ouroboros/Network/Protocol/LocalTxMonitor/Test.hs @@ -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 =