From 32100bda9a38d626723050ea744fc22b9513446f Mon Sep 17 00:00:00 2001 From: mmontin Date: Thu, 11 Jul 2024 17:51:46 +0200 Subject: [PATCH] logging of unused collateral option --- CHANGELOG.md | 3 +++ src/Cooked/MockChain/Balancing.hs | 19 ++++++++++--------- src/Cooked/MockChain/BlockChain.hs | 5 ++++- src/Cooked/Pretty/Cooked.hs | 8 ++++++++ 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f1a349d8..91d90340 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,10 +30,13 @@ * it is now a component of `MonadBlockChainBalancing` * it can be turned on/off in skeleton options * it now displays the discarding of utxos during balancing. + * it now displays when the users specifies useless collateral utxos ### Fixed - All kinds of scripts can now be used as reference scripts. +- Transactions that do not involve script are now properly generated without any + collateral. ## [[4.0.0]](https://github.com/tweag/cooked-validators/releases/tag/v4.0.0) - 2024-06-28 diff --git a/src/Cooked/MockChain/Balancing.hs b/src/Cooked/MockChain/Balancing.hs index da0ad120..26ff46af 100644 --- a/src/Cooked/MockChain/Balancing.hs +++ b/src/Cooked/MockChain/Balancing.hs @@ -79,15 +79,16 @@ balanceTxSkel skelUnbal@TxSkel {..} = do -- We retrieve the various kinds of scripts spendingScripts <- txSkelInputValidators skelUnbal -- The transaction will only require collaterals when involving scripts - if Map.null txSkelMints && null (mapMaybe txSkelProposalWitness txSkelProposals) && Map.null spendingScripts - then return Nothing - else - Just <$> case txOptCollateralUtxos txSkelOpts of - CollateralUtxosFromBalancingWallet -> case balancingWallet of - Nothing -> fail "Can't select collateral utxos from a balancing wallet because it does not exist." - Just bWallet -> (,bWallet) . Set.fromList . map fst <$> runUtxoSearch (onlyValueOutputsAtSearch bWallet) - CollateralUtxosFromWallet cWallet -> (,cWallet) . Set.fromList . map fst <$> runUtxoSearch (onlyValueOutputsAtSearch cWallet) - CollateralUtxosFromSet utxos rWallet -> return (utxos, rWallet) + let noScriptInvolved = Map.null txSkelMints && null (mapMaybe txSkelProposalWitness txSkelProposals) && Map.null spendingScripts + case (noScriptInvolved, txOptCollateralUtxos txSkelOpts) of + (True, CollateralUtxosFromSet utxos _) -> publish (MCLogUnusedCollaterals $ Right utxos) >> return Nothing + (True, CollateralUtxosFromWallet cWallet) -> publish (MCLogUnusedCollaterals $ Left cWallet) >> return Nothing + (True, CollateralUtxosFromBalancingWallet) -> return Nothing + (False, CollateralUtxosFromSet utxos rWallet) -> return $ Just (utxos, rWallet) + (False, CollateralUtxosFromWallet cWallet) -> Just . (,cWallet) . Set.fromList . map fst <$> runUtxoSearch (onlyValueOutputsAtSearch cWallet) + (False, CollateralUtxosFromBalancingWallet) -> case balancingWallet of + Nothing -> fail "Can't select collateral utxos from a balancing wallet because it does not exist." + Just bWallet -> Just . (,bWallet) . Set.fromList . map fst <$> runUtxoSearch (onlyValueOutputsAtSearch bWallet) -- At this point, the presence (or absence) of balancing wallet dictates -- whether the transaction should be automatically balanced or not. diff --git a/src/Cooked/MockChain/BlockChain.hs b/src/Cooked/MockChain/BlockChain.hs index a57026aa..33c3ce26 100644 --- a/src/Cooked/MockChain/BlockChain.hs +++ b/src/Cooked/MockChain/BlockChain.hs @@ -126,8 +126,11 @@ data MockChainLogEntry where -- successfully sent for validation. MCLogNewTx :: Api.TxId -> MockChainLogEntry -- | Logging the fact that utxos provided by the user for balancing have to be - -- discarded for a specific reason. + -- discarded for a given reason. MCLogDiscardedUtxos :: Integer -> String -> MockChainLogEntry + -- | Logging the fact that utxos provided as collaterals will not be used + -- because the transaction does not need involve scripts. + MCLogUnusedCollaterals :: Either Wallet (Set Api.TxOutRef) -> MockChainLogEntry -- | Contains methods needed for balancing. class (MonadFail m, MonadError MockChainError m) => MonadBlockChainBalancing m where diff --git a/src/Cooked/Pretty/Cooked.hs b/src/Cooked/Pretty/Cooked.hs index eb22e88d..646b37ce 100644 --- a/src/Cooked/Pretty/Cooked.hs +++ b/src/Cooked/Pretty/Cooked.hs @@ -152,6 +152,14 @@ instance PrettyCooked MockChainLogEntry where ] prettyCookedOpt opts (MCLogNewTx txId) = "New transaction:" <+> prettyCookedOpt opts txId prettyCookedOpt opts (MCLogDiscardedUtxos n s) = prettyCookedOpt opts n <+> "balancing utxos were discarded:" <+> PP.pretty s + prettyCookedOpt opts (MCLogUnusedCollaterals (Left cWallet)) = + "Specific request to fetch collateral utxos from" + <+> prettyCookedOpt opts (walletPKHash cWallet) + <+> "has been disregarded because the transaction does not require collaterals" + prettyCookedOpt opts (MCLogUnusedCollaterals (Right (length -> n))) = + "Specific request to fetch collateral utxos from the given set of" + <+> prettyCookedOpt opts n + <+> "elements has been disregarded because the transaction does not require collaterals" prettyTxSkel :: PrettyCookedOpts -> SkelContext -> TxSkel -> DocCooked prettyTxSkel opts skelContext (TxSkel lbl txopts mints signers validityRange ins insReference outs proposals) =