Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flatten (some) nested errors in ErrBalanceTx. #4418

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 24 additions & 42 deletions lib/balance-tx/lib/internal/Internal/Cardano/Write/Tx/Balance.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ module Internal.Cardano.Write.Tx.Balance
-- * Balancing transactions
balanceTransaction
, ErrBalanceTx (..)
, ErrBalanceTxAssetsInsufficientError (..)
, ErrBalanceTxInsufficientCollateralError (..)
, ErrBalanceTxInternalError (..)
, ErrBalanceTxOutputError (..)
, ErrBalanceTxOutputErrorInfo (..)
Expand Down Expand Up @@ -321,23 +319,6 @@ import qualified Data.Map.Strict.Extra as Map
import qualified Data.Sequence.Strict as StrictSeq
import qualified Data.Set as Set

-- | Indicates a failure to select a sufficient amount of collateral.
--
data ErrBalanceTxInsufficientCollateralError era =
ErrBalanceTxInsufficientCollateralError
{ largestCombinationAvailable :: UTxO era
-- ^ The largest available combination of pure ada UTxOs.
, minimumCollateralAmount :: Coin
-- ^ The minimum quantity of ada necessary for collateral.
}
deriving Generic

deriving instance IsRecentEra era =>
Eq (ErrBalanceTxInsufficientCollateralError era)

deriving instance IsRecentEra era =>
Show (ErrBalanceTxInsufficientCollateralError era)

-- | Indicates that there was not enough ada available to create change outputs.
--
-- When creating a change output, ada is required in order to pay for:
Expand All @@ -361,24 +342,6 @@ data ErrBalanceTxUnableToCreateChangeError =
}
deriving (Eq, Generic, Show)

-- | Indicates the insufficient availability of one or more assets.
--
-- This error is returned when the available quantity of one or more assets
-- is insufficient to balance the transaction.
--
-- The 'shortfall' field indicates the minimum extra quantity of each asset
-- that would be necessary to balance the transaction.
--
data ErrBalanceTxAssetsInsufficientError = ErrBalanceTxAssetsInsufficientError
{ available :: Value
-- ^ The total sum of all assets available.
, required :: Value
-- ^ The total sum of all assets required.
, shortfall :: Value
-- ^ The total shortfall between available and required assets.
}
deriving (Eq, Generic, Show)

data ErrBalanceTxInternalError era
= ErrUnderestimatedFee Coin (Tx era) KeyWitnessCounts
| ErrFailedBalancing Value
Expand All @@ -388,7 +351,22 @@ deriving instance IsRecentEra era => Show (ErrBalanceTxInternalError era)

-- | Errors that can occur when balancing transactions.
data ErrBalanceTx era
= ErrBalanceTxAssetsInsufficient ErrBalanceTxAssetsInsufficientError
= ErrBalanceTxAssetsInsufficient
{ available :: Value
-- ^ The total sum of all assets available.
, required :: Value
-- ^ The total sum of all assets required.
, shortfall :: Value
-- ^ The total shortfall between available and required assets.
}
-- ^ Indicates the insufficient availability of one or more assets.
--
-- This error is returned when the available quantity of one or more
-- assets is insufficient to balance the transaction.
--
-- The 'shortfall' field indicates the minimum extra quantity of each
-- asset that would be necessary to balance the transaction.
--
| ErrBalanceTxMaxSizeLimitExceeded
| ErrBalanceTxExistingKeyWitnesses Int
-- ^ Indicates that a transaction could not be balanced because a given
Expand All @@ -397,7 +375,13 @@ data ErrBalanceTx era
| ErrBalanceTxExistingTotalCollateral
| ErrBalanceTxExistingReturnCollateral
| ErrBalanceTxInsufficientCollateral
(ErrBalanceTxInsufficientCollateralError era)
{ largestCombinationAvailable :: UTxO era
-- ^ The largest available combination of pure ada UTxOs.
, minimumCollateralAmount :: Coin
-- ^ The minimum quantity of ada necessary for collateral.
}
-- ^ Indicates a failure to select a sufficient amount of collateral.
--
| ErrBalanceTxConflictingNetworks
| ErrBalanceTxAssignRedeemers ErrAssignRedeemers
| ErrBalanceTxInternalError (ErrBalanceTxInternalError era)
Expand Down Expand Up @@ -1522,8 +1506,7 @@ coinSelectionErrorToBalanceTxError = \case
SelectionBalanceErrorOf balanceErr ->
case balanceErr of
BalanceInsufficient e ->
ErrBalanceTxAssetsInsufficient $
ErrBalanceTxAssetsInsufficientError
ErrBalanceTxAssetsInsufficient
{ available =
Convert.toLedger (view #utxoBalanceAvailable e)
, required =
Expand All @@ -1545,7 +1528,6 @@ coinSelectionErrorToBalanceTxError = \case
, minimumSelectionAmount
} ->
ErrBalanceTxInsufficientCollateral
ErrBalanceTxInsufficientCollateralError
{ largestCombinationAvailable
= largestCombinationAvailable
& fmap W.TokenBundle.fromCoin
Expand Down
4 changes: 0 additions & 4 deletions lib/balance-tx/lib/main/Cardano/Write/Tx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ module Cardano.Write.Tx
balanceTransaction
, ErrAssignRedeemers (..)
, ErrBalanceTx (..)
, ErrBalanceTxAssetsInsufficientError (..)
, ErrBalanceTxInsufficientCollateralError (..)
, ErrBalanceTxInternalError (..)
, ErrBalanceTxOutputError (..)
, ErrBalanceTxOutputErrorInfo (..)
Expand All @@ -28,8 +26,6 @@ import Internal.Cardano.Write.Tx
import Internal.Cardano.Write.Tx.Balance
( ErrAssignRedeemers (..)
, ErrBalanceTx (..)
, ErrBalanceTxAssetsInsufficientError (..)
, ErrBalanceTxInsufficientCollateralError (..)
, ErrBalanceTxInternalError (..)
, ErrBalanceTxOutputError (..)
, ErrBalanceTxOutputErrorInfo (..)
Expand Down
40 changes: 20 additions & 20 deletions lib/balance-tx/test/data/balanceTx/1ada-payment/golden
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
0.000000,ErrBalanceTxAssetsInsufficient (ErrBalanceTxAssetsInsufficientError {available = MaryValue (Coin 0) (MultiAsset (fromList [])), required = MaryValue (Coin 1000000) (MultiAsset (fromList [])), shortfall = MaryValue (Coin 1000000) (MultiAsset (fromList []))})
0.050000,ErrBalanceTxAssetsInsufficient (ErrBalanceTxAssetsInsufficientError {available = MaryValue (Coin 50000) (MultiAsset (fromList [])), required = MaryValue (Coin 1000000) (MultiAsset (fromList [])), shortfall = MaryValue (Coin 950000) (MultiAsset (fromList []))})
0.100000,ErrBalanceTxAssetsInsufficient (ErrBalanceTxAssetsInsufficientError {available = MaryValue (Coin 100000) (MultiAsset (fromList [])), required = MaryValue (Coin 1000000) (MultiAsset (fromList [])), shortfall = MaryValue (Coin 900000) (MultiAsset (fromList []))})
0.150000,ErrBalanceTxAssetsInsufficient (ErrBalanceTxAssetsInsufficientError {available = MaryValue (Coin 150000) (MultiAsset (fromList [])), required = MaryValue (Coin 1000000) (MultiAsset (fromList [])), shortfall = MaryValue (Coin 850000) (MultiAsset (fromList []))})
0.200000,ErrBalanceTxAssetsInsufficient (ErrBalanceTxAssetsInsufficientError {available = MaryValue (Coin 200000) (MultiAsset (fromList [])), required = MaryValue (Coin 1000000) (MultiAsset (fromList [])), shortfall = MaryValue (Coin 800000) (MultiAsset (fromList []))})
0.250000,ErrBalanceTxAssetsInsufficient (ErrBalanceTxAssetsInsufficientError {available = MaryValue (Coin 250000) (MultiAsset (fromList [])), required = MaryValue (Coin 1000000) (MultiAsset (fromList [])), shortfall = MaryValue (Coin 750000) (MultiAsset (fromList []))})
0.300000,ErrBalanceTxAssetsInsufficient (ErrBalanceTxAssetsInsufficientError {available = MaryValue (Coin 300000) (MultiAsset (fromList [])), required = MaryValue (Coin 1000000) (MultiAsset (fromList [])), shortfall = MaryValue (Coin 700000) (MultiAsset (fromList []))})
0.350000,ErrBalanceTxAssetsInsufficient (ErrBalanceTxAssetsInsufficientError {available = MaryValue (Coin 350000) (MultiAsset (fromList [])), required = MaryValue (Coin 1000000) (MultiAsset (fromList [])), shortfall = MaryValue (Coin 650000) (MultiAsset (fromList []))})
0.400000,ErrBalanceTxAssetsInsufficient (ErrBalanceTxAssetsInsufficientError {available = MaryValue (Coin 400000) (MultiAsset (fromList [])), required = MaryValue (Coin 1000000) (MultiAsset (fromList [])), shortfall = MaryValue (Coin 600000) (MultiAsset (fromList []))})
0.450000,ErrBalanceTxAssetsInsufficient (ErrBalanceTxAssetsInsufficientError {available = MaryValue (Coin 450000) (MultiAsset (fromList [])), required = MaryValue (Coin 1000000) (MultiAsset (fromList [])), shortfall = MaryValue (Coin 550000) (MultiAsset (fromList []))})
0.500000,ErrBalanceTxAssetsInsufficient (ErrBalanceTxAssetsInsufficientError {available = MaryValue (Coin 500000) (MultiAsset (fromList [])), required = MaryValue (Coin 1000000) (MultiAsset (fromList [])), shortfall = MaryValue (Coin 500000) (MultiAsset (fromList []))})
0.550000,ErrBalanceTxAssetsInsufficient (ErrBalanceTxAssetsInsufficientError {available = MaryValue (Coin 550000) (MultiAsset (fromList [])), required = MaryValue (Coin 1000000) (MultiAsset (fromList [])), shortfall = MaryValue (Coin 450000) (MultiAsset (fromList []))})
0.600000,ErrBalanceTxAssetsInsufficient (ErrBalanceTxAssetsInsufficientError {available = MaryValue (Coin 600000) (MultiAsset (fromList [])), required = MaryValue (Coin 1000000) (MultiAsset (fromList [])), shortfall = MaryValue (Coin 400000) (MultiAsset (fromList []))})
0.650000,ErrBalanceTxAssetsInsufficient (ErrBalanceTxAssetsInsufficientError {available = MaryValue (Coin 650000) (MultiAsset (fromList [])), required = MaryValue (Coin 1000000) (MultiAsset (fromList [])), shortfall = MaryValue (Coin 350000) (MultiAsset (fromList []))})
0.700000,ErrBalanceTxAssetsInsufficient (ErrBalanceTxAssetsInsufficientError {available = MaryValue (Coin 700000) (MultiAsset (fromList [])), required = MaryValue (Coin 1000000) (MultiAsset (fromList [])), shortfall = MaryValue (Coin 300000) (MultiAsset (fromList []))})
0.750000,ErrBalanceTxAssetsInsufficient (ErrBalanceTxAssetsInsufficientError {available = MaryValue (Coin 750000) (MultiAsset (fromList [])), required = MaryValue (Coin 1000000) (MultiAsset (fromList [])), shortfall = MaryValue (Coin 250000) (MultiAsset (fromList []))})
0.800000,ErrBalanceTxAssetsInsufficient (ErrBalanceTxAssetsInsufficientError {available = MaryValue (Coin 800000) (MultiAsset (fromList [])), required = MaryValue (Coin 1000000) (MultiAsset (fromList [])), shortfall = MaryValue (Coin 200000) (MultiAsset (fromList []))})
0.850000,ErrBalanceTxAssetsInsufficient (ErrBalanceTxAssetsInsufficientError {available = MaryValue (Coin 850000) (MultiAsset (fromList [])), required = MaryValue (Coin 1000000) (MultiAsset (fromList [])), shortfall = MaryValue (Coin 150000) (MultiAsset (fromList []))})
0.900000,ErrBalanceTxAssetsInsufficient (ErrBalanceTxAssetsInsufficientError {available = MaryValue (Coin 900000) (MultiAsset (fromList [])), required = MaryValue (Coin 1000000) (MultiAsset (fromList [])), shortfall = MaryValue (Coin 100000) (MultiAsset (fromList []))})
0.950000,ErrBalanceTxAssetsInsufficient (ErrBalanceTxAssetsInsufficientError {available = MaryValue (Coin 950000) (MultiAsset (fromList [])), required = MaryValue (Coin 1000000) (MultiAsset (fromList [])), shortfall = MaryValue (Coin 50000) (MultiAsset (fromList []))})
0.000000,ErrBalanceTxAssetsInsufficient {available = MaryValue (Coin 0) (MultiAsset (fromList [])), required = MaryValue (Coin 1000000) (MultiAsset (fromList [])), shortfall = MaryValue (Coin 1000000) (MultiAsset (fromList []))}
0.050000,ErrBalanceTxAssetsInsufficient {available = MaryValue (Coin 50000) (MultiAsset (fromList [])), required = MaryValue (Coin 1000000) (MultiAsset (fromList [])), shortfall = MaryValue (Coin 950000) (MultiAsset (fromList []))}
0.100000,ErrBalanceTxAssetsInsufficient {available = MaryValue (Coin 100000) (MultiAsset (fromList [])), required = MaryValue (Coin 1000000) (MultiAsset (fromList [])), shortfall = MaryValue (Coin 900000) (MultiAsset (fromList []))}
0.150000,ErrBalanceTxAssetsInsufficient {available = MaryValue (Coin 150000) (MultiAsset (fromList [])), required = MaryValue (Coin 1000000) (MultiAsset (fromList [])), shortfall = MaryValue (Coin 850000) (MultiAsset (fromList []))}
0.200000,ErrBalanceTxAssetsInsufficient {available = MaryValue (Coin 200000) (MultiAsset (fromList [])), required = MaryValue (Coin 1000000) (MultiAsset (fromList [])), shortfall = MaryValue (Coin 800000) (MultiAsset (fromList []))}
0.250000,ErrBalanceTxAssetsInsufficient {available = MaryValue (Coin 250000) (MultiAsset (fromList [])), required = MaryValue (Coin 1000000) (MultiAsset (fromList [])), shortfall = MaryValue (Coin 750000) (MultiAsset (fromList []))}
0.300000,ErrBalanceTxAssetsInsufficient {available = MaryValue (Coin 300000) (MultiAsset (fromList [])), required = MaryValue (Coin 1000000) (MultiAsset (fromList [])), shortfall = MaryValue (Coin 700000) (MultiAsset (fromList []))}
0.350000,ErrBalanceTxAssetsInsufficient {available = MaryValue (Coin 350000) (MultiAsset (fromList [])), required = MaryValue (Coin 1000000) (MultiAsset (fromList [])), shortfall = MaryValue (Coin 650000) (MultiAsset (fromList []))}
0.400000,ErrBalanceTxAssetsInsufficient {available = MaryValue (Coin 400000) (MultiAsset (fromList [])), required = MaryValue (Coin 1000000) (MultiAsset (fromList [])), shortfall = MaryValue (Coin 600000) (MultiAsset (fromList []))}
0.450000,ErrBalanceTxAssetsInsufficient {available = MaryValue (Coin 450000) (MultiAsset (fromList [])), required = MaryValue (Coin 1000000) (MultiAsset (fromList [])), shortfall = MaryValue (Coin 550000) (MultiAsset (fromList []))}
0.500000,ErrBalanceTxAssetsInsufficient {available = MaryValue (Coin 500000) (MultiAsset (fromList [])), required = MaryValue (Coin 1000000) (MultiAsset (fromList [])), shortfall = MaryValue (Coin 500000) (MultiAsset (fromList []))}
0.550000,ErrBalanceTxAssetsInsufficient {available = MaryValue (Coin 550000) (MultiAsset (fromList [])), required = MaryValue (Coin 1000000) (MultiAsset (fromList [])), shortfall = MaryValue (Coin 450000) (MultiAsset (fromList []))}
0.600000,ErrBalanceTxAssetsInsufficient {available = MaryValue (Coin 600000) (MultiAsset (fromList [])), required = MaryValue (Coin 1000000) (MultiAsset (fromList [])), shortfall = MaryValue (Coin 400000) (MultiAsset (fromList []))}
0.650000,ErrBalanceTxAssetsInsufficient {available = MaryValue (Coin 650000) (MultiAsset (fromList [])), required = MaryValue (Coin 1000000) (MultiAsset (fromList [])), shortfall = MaryValue (Coin 350000) (MultiAsset (fromList []))}
0.700000,ErrBalanceTxAssetsInsufficient {available = MaryValue (Coin 700000) (MultiAsset (fromList [])), required = MaryValue (Coin 1000000) (MultiAsset (fromList [])), shortfall = MaryValue (Coin 300000) (MultiAsset (fromList []))}
0.750000,ErrBalanceTxAssetsInsufficient {available = MaryValue (Coin 750000) (MultiAsset (fromList [])), required = MaryValue (Coin 1000000) (MultiAsset (fromList [])), shortfall = MaryValue (Coin 250000) (MultiAsset (fromList []))}
0.800000,ErrBalanceTxAssetsInsufficient {available = MaryValue (Coin 800000) (MultiAsset (fromList [])), required = MaryValue (Coin 1000000) (MultiAsset (fromList [])), shortfall = MaryValue (Coin 200000) (MultiAsset (fromList []))}
0.850000,ErrBalanceTxAssetsInsufficient {available = MaryValue (Coin 850000) (MultiAsset (fromList [])), required = MaryValue (Coin 1000000) (MultiAsset (fromList [])), shortfall = MaryValue (Coin 150000) (MultiAsset (fromList []))}
0.900000,ErrBalanceTxAssetsInsufficient {available = MaryValue (Coin 900000) (MultiAsset (fromList [])), required = MaryValue (Coin 1000000) (MultiAsset (fromList [])), shortfall = MaryValue (Coin 100000) (MultiAsset (fromList []))}
0.950000,ErrBalanceTxAssetsInsufficient {available = MaryValue (Coin 950000) (MultiAsset (fromList [])), required = MaryValue (Coin 1000000) (MultiAsset (fromList [])), shortfall = MaryValue (Coin 50000) (MultiAsset (fromList []))}
1.000000,ErrBalanceTxUnableToCreateChange (ErrBalanceTxUnableToCreateChangeError {requiredCost = Coin 168845, shortfall = Coin 168845})
1.050000,ErrBalanceTxUnableToCreateChange (ErrBalanceTxUnableToCreateChangeError {requiredCost = Coin 168845, shortfall = Coin 118845})
1.100000,ErrBalanceTxUnableToCreateChange (ErrBalanceTxUnableToCreateChangeError {requiredCost = Coin 168845, shortfall = Coin 68845})
Expand Down
Loading
Loading