Skip to content

Commit

Permalink
Merge pull request #269 from geniusyield/268-handle-multiple-parallel…
Browse files Browse the repository at this point in the history
…-build-errors

Feat #268: Handle all balancing errors failure in parallel tx build logic
  • Loading branch information
brunjlar authored Jan 2, 2024
2 parents 8e84b45 + 80ce2b5 commit 04366fb
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 12 deletions.
11 changes: 3 additions & 8 deletions src/GeniusYield/TxBuilder/Common.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{-# LANGUAGE PatternSynonyms #-}
{-|
Module : GeniusYield.TxBuilder.Common
Copyright : (c) 2023 GYELD GMBH
Expand All @@ -9,7 +8,6 @@ Stability : develop
-}
module GeniusYield.TxBuilder.Common
( GYTxBuildResult (..)
, pattern InsufficientFundsErr
, buildTxCore
, collateralLovelace
, collateralValue
Expand Down Expand Up @@ -43,9 +41,9 @@ data GYTxBuildResult f
-- | All given 'GYTxSkeleton's were successfully built.
= GYTxBuildSuccess !(NonEmpty (f GYTxBody))
-- | Some of the given 'GYTxSkeleton's were successfully built, but the rest failed due to _insufficient funds_.
| GYTxBuildPartialSuccess !GYValue !(NonEmpty (f GYTxBody))
| GYTxBuildPartialSuccess !BalancingError !(NonEmpty (f GYTxBody))
-- | None of the given 'GYTxSkeleton's could be built due to _insufficient funds_.
| GYTxBuildFailure !GYValue
| GYTxBuildFailure !BalancingError
-- | Input did not contain any 'GYTxSkeleton's.
| GYTxBuildNoInputs

Expand Down Expand Up @@ -167,7 +165,7 @@ buildTxCore ss eh pp ps cstrat ownUtxoUpdateF addrs change reservedCollateral ac
case res of
{- Not enough funds for this transaction
We assume it's not worth continuing with the next transactions (which is often the case) -}
Left (InsufficientFundsErr v) -> pure $ Right $ reverseResult $ updateBuildRes (Left v) acc
Left (BuildTxBalancingError be) -> pure $ Right $ reverseResult $ updateBuildRes (Left be) acc
-- Any other exception is fatal. TODO: To think more on whether collateral error can be handled here.
Left err -> pure $ Left err
Right fres -> do
Expand Down Expand Up @@ -202,9 +200,6 @@ buildTxCore ss eh pp ps cstrat ownUtxoUpdateF addrs change reservedCollateral ac
reverseResult (GYTxBuildPartialSuccess v ne) = GYTxBuildPartialSuccess v $ NE.reverse ne
reverseResult anyOther = anyOther

pattern InsufficientFundsErr :: GYValue -> BuildTxException
pattern InsufficientFundsErr v = BuildTxBalancingError (BalancingErrorInsufficientFunds v)

collateralLovelace :: Integer
collateralLovelace = 5_000_000

Expand Down
4 changes: 2 additions & 2 deletions src/GeniusYield/TxBuilder/Node.hs
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ runGYTxMonadNodeF cstrat nid providers addrs change collateral m = do
x <- runGYTxMonadNodeCore (const id) cstrat nid providers addrs change collateral $ (:[]) <$> m
case x of
GYTxBuildSuccess ne -> pure $ NE.head ne
GYTxBuildPartialSuccess gv _ -> throwIO $ InsufficientFundsErr gv
GYTxBuildFailure gv -> throwIO $ InsufficientFundsErr gv
GYTxBuildPartialSuccess be _ -> throwIO $ BuildTxBalancingError be
GYTxBuildFailure be -> throwIO $ BuildTxBalancingError be
-- We know there is precisely one input.
GYTxBuildNoInputs -> error "runGYTxMonadNodeF: absurd"

Expand Down
4 changes: 2 additions & 2 deletions src/GeniusYield/TxBuilder/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ import qualified PlutusTx.Builtins.Internal as Plutus
import qualified Cardano.Simple.PlutusLedgerApi.V1.Scripts as Fork
import Data.Sequence (ViewR (..), viewr)
import GeniusYield.Imports
import GeniusYield.Transaction (GYCoinSelectionStrategy (GYRandomImproveMultiAsset))
import GeniusYield.Transaction (GYCoinSelectionStrategy (GYRandomImproveMultiAsset), BuildTxException (BuildTxBalancingError))
import GeniusYield.Transaction.Common (adjustTxOut,
minimumUTxO)
import GeniusYield.TxBuilder.Class
Expand Down Expand Up @@ -466,7 +466,7 @@ skeletonToTxBody skeleton = do
Left err -> throwAppError err
Right res -> case res of
GYTxBuildSuccess (Identity body :| _) -> return body
GYTxBuildFailure v -> throwAppError $ InsufficientFundsErr v
GYTxBuildFailure be -> throwAppError $ BuildTxBalancingError be
GYTxBuildPartialSuccess _ _ -> error "impossible case"
GYTxBuildNoInputs -> error "impossible case"

Expand Down

0 comments on commit 04366fb

Please sign in to comment.