Skip to content

Commit

Permalink
fix fee calculation unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paweljakubas committed Oct 28, 2022
1 parent aa87dd4 commit d4e4fb9
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 21 deletions.
Expand Up @@ -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
Expand Down
Expand Up @@ -145,6 +145,8 @@ import Fmt
( Buildable (..), blockListF', indentF )
import GHC.Generics
( Generic )
import Numeric.Natural
( Natural )
import Type.Reflection
( Typeable )

Expand Down Expand Up @@ -747,33 +749,33 @@ 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
in sum $ take (fromIntegral m) smallestReqFirst
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
Expand Down
8 changes: 3 additions & 5 deletions lib/wallet/src/Cardano/Wallet/Shelley/Transaction.hs
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
15 changes: 7 additions & 8 deletions lib/wallet/test/unit/Cardano/Wallet/Shelley/TransactionSpec.hs
Expand Up @@ -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 (..)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d4e4fb9

Please sign in to comment.