From 1add44c40621fa3dd783b805e27038ff62e773c0 Mon Sep 17 00:00:00 2001 From: Jonathan Knowles Date: Tue, 23 Nov 2021 05:43:14 +0000 Subject: [PATCH] Replace function `coinToInteger` with safer equivalent. We use `intCast` instead of `fromIntegral`, which statically checks that the conversion is safe. We also use the same naming convention as the existing safe conversion functions. --- lib/core/src/Cardano/Wallet.hs | 4 ++-- lib/core/src/Cardano/Wallet/Primitive/Types/Coin.hs | 12 +++++++----- .../unit/Cardano/Wallet/Shelley/TransactionSpec.hs | 6 +++--- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/core/src/Cardano/Wallet.hs b/lib/core/src/Cardano/Wallet.hs index dd3e7452c70..e706334fe5c 100644 --- a/lib/core/src/Cardano/Wallet.hs +++ b/lib/core/src/Cardano/Wallet.hs @@ -373,7 +373,7 @@ import Cardano.Wallet.Primitive.Types import Cardano.Wallet.Primitive.Types.Address ( Address (..), AddressState (..) ) import Cardano.Wallet.Primitive.Types.Coin - ( Coin (..), addCoin, coinToInteger, sumCoins ) + ( Coin (..), addCoin, sumCoins ) import Cardano.Wallet.Primitive.Types.Hash ( Hash (..) ) import Cardano.Wallet.Primitive.Types.Redeemer @@ -1160,7 +1160,7 @@ readNextWithdrawal ctx (Coin withdrawal) = do calcMinimumCost tl pp (mkTxCtx $ Coin 0) emptySkeleton let costOfWithdrawal = - coinToInteger costWith - coinToInteger costWithout + Coin.toInteger costWith - Coin.toInteger costWithout if toInteger withdrawal < 2 * costOfWithdrawal then pure (Coin 0) diff --git a/lib/core/src/Cardano/Wallet/Primitive/Types/Coin.hs b/lib/core/src/Cardano/Wallet/Primitive/Types/Coin.hs index f06c9bac9b4..6e173b3b705 100644 --- a/lib/core/src/Cardano/Wallet/Primitive/Types/Coin.hs +++ b/lib/core/src/Cardano/Wallet/Primitive/Types/Coin.hs @@ -18,6 +18,7 @@ module Cardano.Wallet.Primitive.Types.Coin -- * Conversions (Safe) , fromIntegral , fromWord64 + , toInteger , toQuantity , toWord64 @@ -27,7 +28,6 @@ module Cardano.Wallet.Primitive.Types.Coin , unsafeToWord64 -- * Compatibility - , coinToInteger , coinToNatural , unsafeNaturalToCoin @@ -49,7 +49,7 @@ module Cardano.Wallet.Primitive.Types.Coin ) where import Prelude hiding - ( fromIntegral ) + ( fromIntegral, toInteger ) import Cardano.Numeric.Util ( equipartitionNatural, partitionNatural ) @@ -144,6 +144,11 @@ fromIntegral i = Coin <$> intCastMaybe i fromWord64 :: Word64 -> Coin fromWord64 = Coin . intCast +-- | Converts a 'Coin' to an 'Integer' value. +-- +toInteger :: Coin -> Integer +toInteger = intCast . unCoin + -- | Converts a 'Coin' to a 'Quantity'. -- -- Returns 'Nothing' if the given value does not fit within the bounds of @@ -224,9 +229,6 @@ unsafeToWord64 c = fromMaybe onError (toWord64 c) Compatibility -------------------------------------------------------------------------------} -coinToInteger :: Coin -> Integer -coinToInteger = Prelude.fromIntegral . unCoin - coinToNatural :: Coin -> Natural coinToNatural = unCoin diff --git a/lib/shelley/test/unit/Cardano/Wallet/Shelley/TransactionSpec.hs b/lib/shelley/test/unit/Cardano/Wallet/Shelley/TransactionSpec.hs index 647caa4d7c9..11b57e89d39 100644 --- a/lib/shelley/test/unit/Cardano/Wallet/Shelley/TransactionSpec.hs +++ b/lib/shelley/test/unit/Cardano/Wallet/Shelley/TransactionSpec.hs @@ -84,7 +84,7 @@ import Cardano.Wallet.Primitive.Types import Cardano.Wallet.Primitive.Types.Address ( Address (..) ) import Cardano.Wallet.Primitive.Types.Coin - ( Coin (..), coinToInteger ) + ( Coin (..) ) import Cardano.Wallet.Primitive.Types.Coin.Gen ( genCoinPositive, shrinkCoinPositive ) import Cardano.Wallet.Primitive.Types.Hash @@ -661,11 +661,11 @@ feeCalculationSpec = describe "fee calculations" $ do fp = LinearFee (Quantity 100_000) (Quantity 100) minFee :: TransactionCtx -> Integer - minFee ctx = coinToInteger $ calcMinimumCost testTxLayer pp ctx sel + minFee ctx = Coin.toInteger $ calcMinimumCost testTxLayer pp ctx sel where sel = emptySkeleton minFeeSkeleton :: TxSkeleton -> Integer - minFeeSkeleton = coinToInteger . estimateTxCost pp + minFeeSkeleton = Coin.toInteger . estimateTxCost pp estimateTxSize' :: TxSkeleton -> Integer estimateTxSize' = fromIntegral . unTxSize . estimateTxSize