From 622bde1bc948b7ece246b3bf30129ef92e27fab0 Mon Sep 17 00:00:00 2001 From: Alexey Kuleshevich Date: Fri, 10 Jan 2025 16:50:20 -0700 Subject: [PATCH] Switch `TxIx` and `CertIx` to `Word16` --- .../src/Cardano/Ledger/Babbage/Collateral.hs | 4 +-- .../Ledger/Shelley/API/ByronTranslation.hs | 3 +- .../src/Cardano/Ledger/Address.hs | 12 +++---- .../src/Cardano/Ledger/BaseTypes.hs | 36 +++++-------------- .../Constrained/Conway/Instances/Ledger.hs | 4 +-- .../Test/Cardano/Ledger/Generic/PrettyCore.hs | 2 +- 6 files changed, 20 insertions(+), 41 deletions(-) diff --git a/eras/babbage/impl/src/Cardano/Ledger/Babbage/Collateral.hs b/eras/babbage/impl/src/Cardano/Ledger/Babbage/Collateral.hs index a3d05220e50..dfefd7de1ac 100644 --- a/eras/babbage/impl/src/Cardano/Ledger/Babbage/Collateral.hs +++ b/eras/babbage/impl/src/Cardano/Ledger/Babbage/Collateral.hs @@ -22,7 +22,7 @@ import Cardano.Ledger.UTxO (UTxO (..), coinBalance) import Cardano.Ledger.Val ((<->)) import qualified Data.Map.Strict as Map import Data.Maybe.Strict (StrictMaybe (..)) -import Data.Word (Word16, Word64) +import Data.Word (Word16) import Lens.Micro -- ============================================================ @@ -54,4 +54,4 @@ collOuts txBody = -- In the impossible event that there are more transaction outputs -- in the transaction than will fit into a Word16 (which backs the TxIx), -- we give the collateral return output an index of maxBound. - Nothing -> TxIx ((fromIntegral :: Word16 -> Word64) (maxBound :: Word16)) + Nothing -> TxIx (maxBound :: Word16) diff --git a/eras/shelley/impl/src/Cardano/Ledger/Shelley/API/ByronTranslation.hs b/eras/shelley/impl/src/Cardano/Ledger/Shelley/API/ByronTranslation.hs index e3058c3626a..b858a1ab0cc 100644 --- a/eras/shelley/impl/src/Cardano/Ledger/Shelley/API/ByronTranslation.hs +++ b/eras/shelley/impl/src/Cardano/Ledger/Shelley/API/ByronTranslation.hs @@ -36,7 +36,6 @@ import Cardano.Ledger.Val (zero, (<->)) import qualified Data.ByteString.Short as SBS import Data.Default (def) import qualified Data.Map.Strict as Map -import Data.Word import GHC.Stack (HasCallStack) import Lens.Micro ((&), (.~), (^.)) import Lens.Micro.Extras (view) @@ -73,7 +72,7 @@ translateCompactTxInByronToShelley :: translateCompactTxInByronToShelley (Byron.CompactTxInUtxo compactTxId idx) = TxIn (translateTxIdByronToShelley (Byron.fromCompactTxId compactTxId)) - (TxIx ((fromIntegral :: Word16 -> Word64) idx)) + (TxIx idx) translateUTxOByronToShelley :: Byron.UTxO -> diff --git a/libs/cardano-ledger-core/src/Cardano/Ledger/Address.hs b/libs/cardano-ledger-core/src/Cardano/Ledger/Address.hs index 95fbb3bd2ab..c9af4dfc406 100644 --- a/libs/cardano-ledger-core/src/Cardano/Ledger/Address.hs +++ b/libs/cardano-ledger-core/src/Cardano/Ledger/Address.hs @@ -315,8 +315,8 @@ isBootstrapRedeemer _ = False putPtr :: Ptr -> Put putPtr (Ptr (SlotNo slot) (TxIx txIx) (CertIx certIx)) = do putVariableLengthWord64 slot - putVariableLengthWord64 txIx - putVariableLengthWord64 certIx + putVariableLengthWord64 (fromIntegral txIx) -- TODO: switch to using MemPack for compacting Address at which point + putVariableLengthWord64 (fromIntegral certIx) -- this conversion from Word16 to Word64 will no longer be necessary newtype Word7 = Word7 Word8 deriving (Eq, Show) @@ -721,8 +721,8 @@ decodePtr :: decodePtr buf = Ptr <$> (SlotNo . (fromIntegral :: Word32 -> Word64) <$> decodeVariableLengthWord32 "SlotNo" buf) - <*> (TxIx . (fromIntegral :: Word16 -> Word64) <$> decodeVariableLengthWord16 "TxIx" buf) - <*> (CertIx . (fromIntegral :: Word16 -> Word64) <$> decodeVariableLengthWord16 "CertIx" buf) + <*> (TxIx <$> decodeVariableLengthWord16 "TxIx" buf) + <*> (CertIx <$> decodeVariableLengthWord16 "CertIx" buf) {-# INLINE decodePtr #-} decodePtrLenient :: @@ -732,8 +732,8 @@ decodePtrLenient :: decodePtrLenient buf = Ptr <$> (SlotNo <$> decodeVariableLengthWord64 "SlotNo" buf) - <*> (TxIx <$> decodeVariableLengthWord64 "TxIx" buf) - <*> (CertIx <$> decodeVariableLengthWord64 "CertIx" buf) + <*> (TxIx <$> decodeVariableLengthWord16 "TxIx" buf) + <*> (CertIx <$> decodeVariableLengthWord16 "CertIx" buf) {-# INLINE decodePtrLenient #-} guardLength :: diff --git a/libs/cardano-ledger-core/src/Cardano/Ledger/BaseTypes.hs b/libs/cardano-ledger-core/src/Cardano/Ledger/BaseTypes.hs index c679725924c..0baa1b4396c 100644 --- a/libs/cardano-ledger-core/src/Cardano/Ledger/BaseTypes.hs +++ b/libs/cardano-ledger-core/src/Cardano/Ledger/BaseTypes.hs @@ -806,28 +806,15 @@ newtype BlocksMade = BlocksMade deriving (Show) via Quiet BlocksMade deriving newtype (NoThunks, NFData, ToJSON, FromJSON, EncCBOR, DecCBOR) --- TODO: It is unfeasable to have 65535 outputs in a transaction, --- but 255 is right on the border of a maximum TxIx on Mainnet at the moment, --- that is why `Word16` was chosen as the smallest upper bound. Use --- `txIxFromIntegral` in order to construct this index safely from anything --- other than `Word16`. There is also `mkTxIxPartial` that can be used for --- testing. - -- | Transaction index. -newtype TxIx = TxIx {unTxIx :: Word64} +newtype TxIx = TxIx {unTxIx :: Word16} deriving stock (Eq, Ord, Show, Generic) - deriving newtype (NFData, Enum, Bounded, NoThunks, FromCBOR, ToCBOR, EncCBOR, ToJSON) - -instance DecCBOR TxIx where - decCBOR = - ifDecoderVersionAtLeast - (natVersion @9) - (TxIx . fromIntegral @Word16 @Word64 <$> decCBOR) - (TxIx <$> decCBOR) + deriving newtype (NFData, Enum, Bounded, NoThunks, FromCBOR, ToCBOR, EncCBOR, DecCBOR, ToJSON) -- | Construct a `TxIx` from a 16 bit unsigned integer mkTxIx :: Word16 -> TxIx mkTxIx = TxIx . fromIntegral +{-# DEPRECATED mkTxIx "In favor of `TxIx`" #-} txIxToInt :: TxIx -> Int txIxToInt (TxIx w16) = fromIntegral w16 @@ -843,23 +830,16 @@ mkTxIxPartial i = fromMaybe (error $ "Value for TxIx is out of a valid range: " ++ show i) $ txIxFromIntegral i --- | Certificate index. Use `certIxFromIntegral` in order to construct this --- index safely from anything other than `Word16`. There is also --- `mkCertIxPartial` that can be used for testing. -newtype CertIx = CertIx {unCertIx :: Word64} +-- | Certificate index. There is `mkCertIxPartial` that can be used for testing when constructing +-- from other integral types that are larger than `Word16` +newtype CertIx = CertIx {unCertIx :: Word16} deriving stock (Eq, Ord, Show) - deriving newtype (NFData, Enum, Bounded, NoThunks, EncCBOR, ToCBOR, FromCBOR, ToJSON) - -instance DecCBOR CertIx where - decCBOR = - ifDecoderVersionAtLeast - (natVersion @9) - (CertIx . fromIntegral @Word16 @Word64 <$> decCBOR) - (CertIx <$> decCBOR) + deriving newtype (NFData, Enum, Bounded, NoThunks, EncCBOR, DecCBOR, ToCBOR, FromCBOR, ToJSON) -- | Construct a `CertIx` from a 16 bit unsigned integer mkCertIx :: Word16 -> CertIx mkCertIx = CertIx . fromIntegral +{-# DEPRECATED mkCertIx "In favor of `CertIx`" #-} certIxToInt :: CertIx -> Int certIxToInt (CertIx w16) = fromIntegral w16 diff --git a/libs/cardano-ledger-test/src/Test/Cardano/Ledger/Constrained/Conway/Instances/Ledger.hs b/libs/cardano-ledger-test/src/Test/Cardano/Ledger/Constrained/Conway/Instances/Ledger.hs index cfe287260bf..2e257485043 100644 --- a/libs/cardano-ledger-test/src/Test/Cardano/Ledger/Constrained/Conway/Instances/Ledger.hs +++ b/libs/cardano-ledger-test/src/Test/Cardano/Ledger/Constrained/Conway/Instances/Ledger.hs @@ -588,8 +588,8 @@ instance IsConwayUniv fn => HasSpec fn Ptr instance HasSimpleRep CertIx where type SimpleRep CertIx = Word16 - toSimpleRep (CertIx w) = fromIntegral w - fromSimpleRep = mkCertIx + toSimpleRep = unCertIx + fromSimpleRep = CertIx instance IsConwayUniv fn => HasSpec fn CertIx instance HasSimpleRep (Credential r) diff --git a/libs/cardano-ledger-test/src/Test/Cardano/Ledger/Generic/PrettyCore.hs b/libs/cardano-ledger-test/src/Test/Cardano/Ledger/Generic/PrettyCore.hs index 2bbcfa8356c..ff7c81844b9 100644 --- a/libs/cardano-ledger-test/src/Test/Cardano/Ledger/Generic/PrettyCore.hs +++ b/libs/cardano-ledger-test/src/Test/Cardano/Ledger/Generic/PrettyCore.hs @@ -2501,7 +2501,7 @@ pcTxId (TxId safehash) = trim (ppSafeHash safehash) instance PrettyA TxId where prettyA = pcTxId pcTxIn :: TxIn -> PDoc -pcTxIn (TxIn (TxId h) (TxIx i)) = parens (hsep [ppString "TxIn", trim (ppSafeHash h), ppWord64 i]) +pcTxIn (TxIn (TxId h) (TxIx i)) = parens (hsep [ppString "TxIn", trim (ppSafeHash h), ppWord16 i]) instance PrettyA TxIn where prettyA = pcTxIn