From d4e4fb947638204d517b5a5f688b7209a2f68673 Mon Sep 17 00:00:00 2001 From: Pawel Jakubas Date: Fri, 28 Oct 2022 17:20:53 +0200 Subject: [PATCH] fix fee calculation unit tests --- .../Cardano/Wallet/Api/Http/Shelley/Server.hs | 2 +- .../Wallet/Primitive/AddressDiscovery/Shared.hs | 16 +++++++++------- .../src/Cardano/Wallet/Shelley/Transaction.hs | 8 +++----- .../Cardano/Wallet/Shelley/TransactionSpec.hs | 15 +++++++-------- 4 files changed, 20 insertions(+), 21 deletions(-) diff --git a/lib/wallet/api/http/Cardano/Wallet/Api/Http/Shelley/Server.hs b/lib/wallet/api/http/Cardano/Wallet/Api/Http/Shelley/Server.hs index dbe9d3e34c3..8735158110c 100644 --- a/lib/wallet/api/http/Cardano/Wallet/Api/Http/Shelley/Server.hs +++ b/lib/wallet/api/http/Cardano/Wallet/Api/Http/Shelley/Server.hs @@ -3065,7 +3065,7 @@ submitSharedTransaction ctx apiw@(ApiT wid) apitx = do filter isInpOurs $ (apiDecoded ^. #inputs) ++ (apiDecoded ^. #collateral) let totalNumberOfWits = length $ getSealedTxWitnesses sealedTx - let allWitsRequired = witsPerInput * witsRequiredForInputs + let allWitsRequired = fromIntegral witsPerInput * witsRequiredForInputs when (allWitsRequired > totalNumberOfWits) $ liftHandler $ throwE $ ErrSubmitTransactionPartiallySignedOrNoSignedTx allWitsRequired totalNumberOfWits diff --git a/lib/wallet/src/Cardano/Wallet/Primitive/AddressDiscovery/Shared.hs b/lib/wallet/src/Cardano/Wallet/Primitive/AddressDiscovery/Shared.hs index 57ff7d5c5b2..5d5ef4d240c 100644 --- a/lib/wallet/src/Cardano/Wallet/Primitive/AddressDiscovery/Shared.hs +++ b/lib/wallet/src/Cardano/Wallet/Primitive/AddressDiscovery/Shared.hs @@ -145,6 +145,8 @@ import Fmt ( Buildable (..), blockListF', indentF ) import GHC.Generics ( Generic ) +import Numeric.Natural + ( Natural ) import Type.Reflection ( Typeable ) @@ -747,13 +749,13 @@ instance ( key ~ SharedKey , pwd ) (Nothing, _) -> Nothing -estimateMinWitnessRequiredPerInput :: Script k -> Int +estimateMinWitnessRequiredPerInput :: Script k -> Natural estimateMinWitnessRequiredPerInput = \case RequireSignatureOf _ -> 1 RequireAllOf xs -> sum $ map estimateMinWitnessRequiredPerInput xs RequireAnyOf xs -> - optimum minimum $ map estimateMinWitnessRequiredPerInput xs + optimumIfNotEmpty minimum $ map estimateMinWitnessRequiredPerInput xs RequireSomeOf m xs -> let smallestReqFirst = L.sort $ map estimateMinWitnessRequiredPerInput xs @@ -761,19 +763,19 @@ estimateMinWitnessRequiredPerInput = \case ActiveFromSlot _ -> 0 ActiveUntilSlot _ -> 0 -optimum :: (Foldable t, Num p) => (t a -> p) -> t a -> p -optimum f xs = - if length xs == 0 then +optimumIfNotEmpty :: (Foldable t, Num p) => (t a -> p) -> t a -> p +optimumIfNotEmpty f xs = + if null xs then 0 else f xs -estimateMaxWitnessRequiredPerInput :: Script k -> Int +estimateMaxWitnessRequiredPerInput :: Script k -> Natural estimateMaxWitnessRequiredPerInput = \case RequireSignatureOf _ -> 1 RequireAllOf xs -> sum $ map estimateMaxWitnessRequiredPerInput xs RequireAnyOf xs -> - optimum maximum $ map estimateMaxWitnessRequiredPerInput xs + optimumIfNotEmpty maximum $ map estimateMaxWitnessRequiredPerInput xs RequireSomeOf m xs -> let smallestReqFirst = L.sort $ map estimateMaxWitnessRequiredPerInput xs diff --git a/lib/wallet/src/Cardano/Wallet/Shelley/Transaction.hs b/lib/wallet/src/Cardano/Wallet/Shelley/Transaction.hs index 4252964f31d..b834ca2473a 100644 --- a/lib/wallet/src/Cardano/Wallet/Shelley/Transaction.hs +++ b/lib/wallet/src/Cardano/Wallet/Shelley/Transaction.hs @@ -112,7 +112,7 @@ import Cardano.Wallet.Primitive.AddressDerivation.Shared import Cardano.Wallet.Primitive.AddressDerivation.Shelley ( ShelleyKey, toRewardAccountRaw ) import Cardano.Wallet.Primitive.AddressDiscovery.Shared - ( estimateMinWitnessRequiredPerInput ) + ( estimateMaxWitnessRequiredPerInput ) import Cardano.Wallet.Primitive.Passphrase ( Passphrase (..) ) import Cardano.Wallet.Primitive.Slotting @@ -1830,12 +1830,10 @@ estimateTxSize era skeleton = -- Total number of signatures the scripts require numberOf_MintingWitnesses - = fromIntegral $ - sumVia estimateMinWitnessRequiredPerInput txMintOrBurnScripts + = intCast $ sumVia estimateMaxWitnessRequiredPerInput txMintOrBurnScripts numberOf_ScriptVkeyWitnesses - = fromIntegral $ - maybe 0 estimateMinWitnessRequiredPerInput txPaymentTemplate + = intCast $ maybe 0 estimateMaxWitnessRequiredPerInput txPaymentTemplate numberOf_VkeyWitnesses = case txWitnessTag of diff --git a/lib/wallet/test/unit/Cardano/Wallet/Shelley/TransactionSpec.hs b/lib/wallet/test/unit/Cardano/Wallet/Shelley/TransactionSpec.hs index 1bd14c2dee8..383e0303624 100644 --- a/lib/wallet/test/unit/Cardano/Wallet/Shelley/TransactionSpec.hs +++ b/lib/wallet/test/unit/Cardano/Wallet/Shelley/TransactionSpec.hs @@ -30,12 +30,7 @@ import Prelude import Cardano.Address.Derivation ( XPrv, XPub, toXPub, xprvFromBytes, xprvToBytes, xpubPublicKey ) import Cardano.Address.Script - ( KeyHash (..) - , KeyRole (Delegation, Payment) - , Script - , foldScript - , serializeScript - ) + ( KeyHash (..), KeyRole (Delegation, Payment), Script, serializeScript ) import Cardano.Api ( AnyCardanoEra (..) , CardanoEra (..) @@ -128,6 +123,8 @@ import Cardano.Wallet.Primitive.AddressDerivation.Shelley ( ShelleyKey, generateKeyFromSeed ) import Cardano.Wallet.Primitive.AddressDiscovery.Sequential ( SeqState, defaultAddressPoolGap, mkSeqStateFromRootXPrv, purposeCIP1852 ) +import Cardano.Wallet.Primitive.AddressDiscovery.Shared + ( estimateMaxWitnessRequiredPerInput ) import Cardano.Wallet.Primitive.Model ( Wallet (..), unsafeInitWallet ) import Cardano.Wallet.Primitive.Passphrase @@ -1209,7 +1206,8 @@ feeCalculationSpec era = describe "fee calculations" $ do $ property $ \scripts -> let -- Number of signatures required in the script - numWitnesses = sum $ (foldScript (const (+ 1)) 0) <$> scripts + numWitnesses = fromIntegral $ sum $ + estimateMaxWitnessRequiredPerInput <$> scripts sizeWitness = 1 -- small array + 34 -- vkey + 66 -- signature @@ -1373,7 +1371,8 @@ feeCalculationSpec era = describe "fee calculations" $ do $ property $ \scripts -> let -- Number of signatures required in the script - numWitnesses = sum $ (foldScript (const (+ 1)) 0) <$> scripts + numWitnesses = fromIntegral $ sum $ + estimateMaxWitnessRequiredPerInput <$> scripts sizeWitness = 1 -- small array + 34 -- vkey + 66 -- signature