Skip to content

Commit

Permalink
fix size estimation due to optional script withdrawals
Browse files Browse the repository at this point in the history
  • Loading branch information
paweljakubas committed May 29, 2023
1 parent dcfb9a0 commit f0561b9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
6 changes: 4 additions & 2 deletions lib/wallet/src/Cardano/Wallet.hs
Expand Up @@ -1325,14 +1325,13 @@ readRewardAccount
=> DBLayer IO s
-> IO (RewardAccount, Maybe XPub, NonEmpty DerivationIndex)
readRewardAccount db = do
walletState <- getState <$> readWalletCheckpoint db
case walletFlavor @s of
ShelleyWallet -> do
walletState <- getState <$> readWalletCheckpoint db
let xpub = Seq.rewardAccountKey walletState
let path = stakeDerivationPath $ Seq.derivationPrefix walletState
pure (toRewardAccount xpub, Just $ getRawKey ShelleyKeyS xpub, path)
SharedWallet -> do
walletState <- getState <$> readWalletCheckpoint db
let path = stakeDerivationPath $ Shared.derivationPrefix walletState
case Shared.rewardAccountKey walletState of
Just rewardAcct -> pure $ (rewardAcct, Nothing, path)
Expand All @@ -1342,6 +1341,9 @@ readWalletCheckpoint
:: DBLayer IO s -> IO (Wallet s)
readWalletCheckpoint DBLayer{..} = liftIO $ atomically readCheckpoint

-- | Unsafe version of the `readRewardAccount` function
-- that throws error when applied to a non-shared
-- or a non-shared wallet state.
sharedOnlyReadRewardAccount
:: forall s
. WalletFlavor s
Expand Down
24 changes: 20 additions & 4 deletions lib/wallet/src/Cardano/Wallet/Shelley/Transaction.hs
Expand Up @@ -1446,6 +1446,7 @@ data TxSkeleton = TxSkeleton
, txOutputs :: ![TxOut]
, txChange :: ![Set AssetId]
, txPaymentTemplate :: !(Maybe (Script Cosigner))
, txDelegationTemplate :: !(Maybe (Script Cosigner))
, txMintOrBurnScripts :: [Script KeyHash]
, txAssetsToMintOrBurn :: Set AssetId
-- ^ The set of assets to mint or burn.
Expand All @@ -1466,6 +1467,7 @@ emptyTxSkeleton txWitnessTag = TxSkeleton
, txOutputs = []
, txChange = []
, txPaymentTemplate = Nothing
, txDelegationTemplate = Nothing
, txMintOrBurnScripts = []
, txAssetsToMintOrBurn = Set.empty
}
Expand All @@ -1491,6 +1493,9 @@ mkTxSkeleton witness context skeleton = TxSkeleton
, txPaymentTemplate =
template <$>
view #txPaymentCredentialScriptTemplate context
, txDelegationTemplate =
template <$>
view #txStakingCredentialScriptTemplate context
, txMintOrBurnScripts = (<>)
(Map.elems (snd $ view #txAssetsToMint context))
(Map.elems (snd $ view #txAssetsToBurn context))
Expand Down Expand Up @@ -1737,6 +1742,7 @@ estimateTxSize era skeleton =
, txOutputs
, txChange
, txPaymentTemplate
, txDelegationTemplate
, txMintOrBurnScripts
, txAssetsToMintOrBurn
} = skeleton
Expand All @@ -1754,23 +1760,33 @@ estimateTxSize era skeleton =
numberOf_MintingWitnesses
= intCast $ sumVia estimateMaxWitnessRequiredPerInput txMintOrBurnScripts

numberOf_ScriptVkeyWitnesses
numberOf_ScriptVkeyWitnessesForPayment
= intCast $ maybe 0 estimateMaxWitnessRequiredPerInput txPaymentTemplate

numberOf_ScriptVkeyWitnessesForDelegation
= intCast $ maybe 0 estimateMaxWitnessRequiredPerInput txDelegationTemplate

numberOf_VkeyWitnesses
= case txWitnessTag of
TxWitnessByronUTxO -> 0
TxWitnessShelleyUTxO ->
if numberOf_ScriptVkeyWitnesses == 0 then
-- there cannot be missing payment script if there is delegation script
-- the latter is optional
if numberOf_ScriptVkeyWitnessesForPayment == 0 then
numberOf_Inputs
+ numberOf_Withdrawals
+ numberOf_CertificateSignatures
+ numberOf_MintingWitnesses
else
(numberOf_Inputs * numberOf_ScriptVkeyWitnesses)
else if numberOf_ScriptVkeyWitnessesForDelegation == 0 then
(numberOf_Inputs * numberOf_ScriptVkeyWitnessesForPayment)
+ numberOf_Withdrawals
+ numberOf_CertificateSignatures
+ numberOf_MintingWitnesses
else
(numberOf_Inputs * numberOf_ScriptVkeyWitnessesForPayment)
+ (numberOf_Withdrawals * numberOf_ScriptVkeyWitnessesForDelegation)
+ numberOf_CertificateSignatures
+ numberOf_MintingWitnesses

numberOf_BootstrapWitnesses
= case txWitnessTag of
Expand Down

0 comments on commit f0561b9

Please sign in to comment.