Skip to content

Commit

Permalink
integrating review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mmontin committed Sep 4, 2024
1 parent 87913ba commit 114e0a4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
28 changes: 13 additions & 15 deletions src/Cooked/MockChain/Balancing.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ type Fee = Integer

type Collaterals = Set Api.TxOutRef

type MCollaterals = Maybe (Collaterals, Wallet)

type BalancingOutputs = [(Api.TxOutRef, Api.TxOut)]

-- | This is the main entry point of our balancing mechanism. This function
Expand All @@ -55,7 +53,7 @@ type BalancingOutputs = [(Api.TxOutRef, Api.TxOut)]
-- be empty when no script is involved in the transaction. The options from the
-- skeleton control whether it should be balanced, and how to compute its
-- associated elements.
balanceTxSkel :: (MonadBlockChainBalancing m) => TxSkel -> m (TxSkel, Fee, MCollaterals)
balanceTxSkel :: (MonadBlockChainBalancing m) => TxSkel -> m (TxSkel, Fee, Maybe (Collaterals, Wallet))
balanceTxSkel skelUnbal@TxSkel {..} = do
-- We retrieve the possible balancing wallet. Any extra payment will be
-- redirected to them, and utxos will be taken from their wallet if associated
Expand Down Expand Up @@ -94,7 +92,7 @@ balanceTxSkel skelUnbal@TxSkel {..} = do

-- At this point, the presence (or absence) of balancing wallet dictates
-- whether the transaction should be automatically balanced or not.
(txSkelBal, fee, adjustedMCollaterals) <- case balancingWallet of
(txSkelBal, fee, adjustedColsAndWallet) <- case balancingWallet of
Nothing ->
-- The balancing should not be performed. We still adjust the collaterals
-- though around a provided fee, or the maximum fee.
Expand Down Expand Up @@ -126,11 +124,11 @@ balanceTxSkel skelUnbal@TxSkel {..} = do
-- If fee are provided manually, we adjust the collaterals and the
-- skeleton around them directly.
ManualFee fee -> do
adjustedMCollaterals <- collateralsFromFees fee mCollaterals
adjustedColsAndWallet <- collateralsFromFees fee mCollaterals
attemptedSkel <- computeBalancedTxSkel bWallet balancingUtxos skelUnbal fee
return (attemptedSkel, fee, adjustedMCollaterals)
return (attemptedSkel, fee, adjustedColsAndWallet)

return (txSkelBal, fee, adjustedMCollaterals)
return (txSkelBal, fee, adjustedColsAndWallet)
where
filterAndWarn f s l
| (ok, toInteger . length -> koLength) <- partition f l =
Expand Down Expand Up @@ -163,15 +161,15 @@ getMinAndMaxFee = do

-- | Computes optimal fee for a given skeleton and balances it around those fees.
-- This uses a dichotomic search for an optimal "balanceable around" fee.
computeFeeAndBalance :: (MonadBlockChainBalancing m) => Wallet -> Fee -> Fee -> BalancingOutputs -> MCollaterals -> TxSkel -> m (TxSkel, Fee, MCollaterals)
computeFeeAndBalance :: (MonadBlockChainBalancing m) => Wallet -> Fee -> Fee -> BalancingOutputs -> Maybe (Collaterals, Wallet) -> TxSkel -> m (TxSkel, Fee, Maybe (Collaterals, Wallet))
computeFeeAndBalance _ minFee maxFee _ _ _
| minFee > maxFee =
throwError $ FailWith "Unreachable case, please report a bug at https://github.com/tweag/cooked-validators/issues"
computeFeeAndBalance balancingWallet minFee maxFee balancingUtxos mCollaterals skel
| minFee == maxFee = do
-- The fee interval is reduced to a single element, we balance around it
(adjustedMCollaterals, attemptedSkel) <- attemptBalancingAndCollaterals balancingWallet balancingUtxos minFee mCollaterals skel
return (attemptedSkel, minFee, adjustedMCollaterals)
(adjustedColsAndWallet, attemptedSkel) <- attemptBalancingAndCollaterals balancingWallet balancingUtxos minFee mCollaterals skel
return (attemptedSkel, minFee, adjustedColsAndWallet)
computeFeeAndBalance balancingWallet minFee maxFee balancingUtxos mCollaterals skel
| fee <- (minFee + maxFee) `div` 2 = do
-- The fee interval is larger than a single element. We attempt to balance
Expand All @@ -193,8 +191,8 @@ computeFeeAndBalance balancingWallet minFee maxFee balancingUtxos mCollaterals s
Nothing -> return (minFee, fee - 1)
-- The skeleton was balanceable, we compute and analyse the resulting
-- fee to seach upwards or downwards for an optimal solution
Just (adjustedMCollaterals, attemptedSkel) -> do
newFee <- estimateTxSkelFee attemptedSkel fee adjustedMCollaterals
Just (adjustedColsAndWallet, attemptedSkel) -> do
newFee <- estimateTxSkelFee attemptedSkel fee adjustedColsAndWallet
return $ case fee - newFee of
-- Current fee is insufficient, we look on the right (strictly)
n | n < 0 -> (fee + 1, maxFee)
Expand All @@ -217,7 +215,7 @@ computeFeeAndBalance balancingWallet minFee maxFee balancingUtxos mCollaterals s

-- | Helper function to group the two real steps of the balancing: balance a
-- skeleton around a given fee, and compute the associated collateral inputs
attemptBalancingAndCollaterals :: (MonadBlockChainBalancing m) => Wallet -> BalancingOutputs -> Fee -> MCollaterals -> TxSkel -> m (MCollaterals, TxSkel)
attemptBalancingAndCollaterals :: (MonadBlockChainBalancing m) => Wallet -> BalancingOutputs -> Fee -> Maybe (Collaterals, Wallet) -> TxSkel -> m (Maybe (Collaterals, Wallet), TxSkel)
attemptBalancingAndCollaterals balancingWallet balancingUtxos fee mCollaterals skel = do
adjustedCollateralIns <- collateralsFromFees fee mCollaterals
attemptedSkel <- computeBalancedTxSkel balancingWallet balancingUtxos skel fee
Expand Down Expand Up @@ -248,7 +246,7 @@ collateralInsFromFees fee collateralIns returnCollateralWallet = do
Set.fromList . fst <$> getOptimalCandidate candidatesRaw returnCollateralWallet noSuitableCollateralError

-- | This adjusts collateral inputs when necessary
collateralsFromFees :: (MonadBlockChainBalancing m) => Fee -> MCollaterals -> m MCollaterals
collateralsFromFees :: (MonadBlockChainBalancing m) => Fee -> Maybe (Collaterals, Wallet) -> m (Maybe (Collaterals, Wallet))
collateralsFromFees _ Nothing = return Nothing
collateralsFromFees fee (Just (collateralIns, returnCollateralWallet)) =
Just . (,returnCollateralWallet) <$> collateralInsFromFees fee collateralIns returnCollateralWallet
Expand Down Expand Up @@ -297,7 +295,7 @@ getOptimalCandidate candidates paymentTarget mceError = do

-- | This function is essentially a copy of
-- https://github.com/input-output-hk/plutus-apps/blob/d4255f05477fd8477ee9673e850ebb9ebb8c9657/plutus-ledger/src/Ledger/Fee.hs#L19
estimateTxSkelFee :: (MonadBlockChainBalancing m) => TxSkel -> Fee -> MCollaterals -> m Fee
estimateTxSkelFee :: (MonadBlockChainBalancing m) => TxSkel -> Fee -> Maybe (Collaterals, Wallet) -> m Fee
estimateTxSkelFee skel fee mCollaterals = do
-- We retrieve the necessary data to generate the transaction body
params <- getParams
Expand Down
4 changes: 3 additions & 1 deletion src/Cooked/MockChain/BlockChain.hs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ data MockChainLogEntry
-- discarded for a specific reason.
MCLogDiscardedUtxos Integer String
| -- | Logging the fact that utxos provided as collaterals will not be used
-- because the transaction does not involve scripts.
-- because the transaction does not involve scripts. There are 2 cases,
-- depending on whether the user has provided an explicit wallet or a set of
-- utxos to be used as collaterals.
MCLogUnusedCollaterals (Either Wallet (Set Api.TxOutRef))

-- | Contains methods needed for balancing.
Expand Down

0 comments on commit 114e0a4

Please sign in to comment.