Skip to content

Commit

Permalink
Add details to ErrBalanceTxMaxSizeLimitExceeded
Browse files Browse the repository at this point in the history
  • Loading branch information
Anviking committed Dec 19, 2024
1 parent e699b27 commit 30ccd31
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/api/src/Cardano/Wallet/Api/Http/Server/Error.hs
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ instance (Write.IsRecentEra era, IsServerError (ErrAssignRedeemers era))
ErrBalanceTxInsufficientCollateral e ->
toServerError e
ErrBalanceTxInternalError e -> toServerError e
ErrBalanceTxMaxSizeLimitExceeded ->
ErrBalanceTxMaxSizeLimitExceeded _ _ ->
apiError err403 TransactionIsTooBig $ T.unwords
[ "I was not able to balance the transaction without exceeding"
, "the maximum transaction size."
Expand Down
11 changes: 6 additions & 5 deletions lib/balance-tx/lib/internal/Internal/Cardano/Write/Tx/Balance.hs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ deriving instance IsRecentEra era => Show (ErrBalanceTxInternalError era)
-- | Errors that can occur when balancing transactions.
data ErrBalanceTx era
= ErrBalanceTxAssetsInsufficient ErrBalanceTxAssetsInsufficientError
| ErrBalanceTxMaxSizeLimitExceeded
| ErrBalanceTxMaxSizeLimitExceeded { size :: W.TxSize, maxSize :: W.TxSize }
| ErrBalanceTxExistingKeyWitnesses Int
-- ^ Indicates that a transaction could not be balanced because a given
-- number of existing key witnesses would be rendered invalid.
Expand Down Expand Up @@ -713,7 +713,7 @@ balanceTx
-- using this strategy might allow us to generate a transaction within
-- the size limit.
maxSizeLimitExceeded = case e of
ErrBalanceTxMaxSizeLimitExceeded ->
ErrBalanceTxMaxSizeLimitExceeded{} ->
True
_someOtherError ->
False
Expand Down Expand Up @@ -907,9 +907,10 @@ balanceTxInner
-> Tx era
-> ExceptT (ErrBalanceTx era) m (Tx era)
guardTxSize witCount tx = do
let maxSize = W.TxSize $ intCast (pp ^. ppMaxTxSizeL)
when (estimateSignedTxSize pp witCount tx > maxSize) $
throwE ErrBalanceTxMaxSizeLimitExceeded
let maxSize = W.TxSize $ intCast (pp ^. ppMaxTxSizeL)
let size = estimateSignedTxSize pp witCount tx
when (size > maxSize) $
throwE ErrBalanceTxMaxSizeLimitExceeded{size, maxSize}
pure tx

guardTxBalanced
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,11 @@ spec_balanceTx = describe "balanceTx" $ do
[ W.TxOut dummyAddr
(W.TokenBundle.fromCoin (W.Coin 200_000_000))
]
tx `shouldBe` Left ErrBalanceTxMaxSizeLimitExceeded
tx `shouldBe` Left
(ErrBalanceTxMaxSizeLimitExceeded
{ size = W.TxSize 28_226
, maxSize = W.TxSize 16_384
})

describe "stake key deposit lookup" $ do
let stakeCred = KeyHashObj $ KeyHash
Expand Down Expand Up @@ -1354,7 +1358,7 @@ prop_balanceTxValid
label "existing total collateral" True
Left (ErrBalanceTxExistingReturnCollateral) ->
label "existing collateral return outputs" True
Left ErrBalanceTxMaxSizeLimitExceeded ->
Left ErrBalanceTxMaxSizeLimitExceeded{} ->
label "maxTxSize limit exceeded" $ property True
Left ErrBalanceTxUnableToCreateInput ->
label "unable to create input" $ property True
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ import Cardano.Wallet.Deposit.Write
, TxBody (..)
, Value
)
import Cardano.Wallet.Primitive.Types.Tx.Constraints
( TxSize (..)
)
import Cardano.Wallet.Read
( AssetID (AdaID)
, Coin (..)
Expand Down Expand Up @@ -90,7 +93,7 @@ data ErrCreatePayment
-- We should ideally split out 'TooManyPayments' from this error.
-- We should ideally also be able to create payments even when dust causes
-- us to need preparatory txs.
| ErrTxMaxSizeLimitExceeded
| ErrTxMaxSizeLimitExceeded{ size :: TxSize, maxSize :: TxSize }
deriving (Eq, Show)

translateBalanceTxError :: Write.ErrBalanceTx Write.Conway -> ErrCreatePayment
Expand All @@ -100,8 +103,8 @@ translateBalanceTxError = \case
ErrNotEnoughAda
{ shortfall = fromLedgerValue shortfall
}
Write.ErrBalanceTxMaxSizeLimitExceeded ->
ErrTxMaxSizeLimitExceeded
Write.ErrBalanceTxMaxSizeLimitExceeded{size, maxSize} ->
ErrTxMaxSizeLimitExceeded{size, maxSize}
Write.ErrBalanceTxExistingKeyWitnesses _ ->
impossible "ErrBalanceTxExistingKeyWitnesses"
Write.ErrBalanceTxExistingCollateral ->
Expand Down Expand Up @@ -159,9 +162,10 @@ instance ToText ErrCreatePayment where
, "is below the required minimum."
, "Suggested minimum amount:", prettyCoin suggestedMinimum
]
ErrTxMaxSizeLimitExceeded -> T.unwords
ErrTxMaxSizeLimitExceeded{size, maxSize} -> T.unlines
[ "Exceeded the maximum size limit when creating the transaction."
, "Potential solutions:"
<> " (size: ", prettyTxSize size, " max size: ", prettyTxSize maxSize <> ")"
, "\nPotential solutions:"
, "1) Make fewer payments at the same time."
, "2) Send smaller amounts of ada in total."
, "3) Fund wallet with more ada."
Expand All @@ -180,6 +184,9 @@ instance ToText ErrCreatePayment where
c' :: Fixed E6
c' = toEnum $ fromEnum c

prettyTxSize :: TxSize -> Text
prettyTxSize (TxSize s) = T.pack (show s)

type CurrentEraResolvedTx = ResolvedTx Read.Conway

resolveCurrentEraTx :: Tx -> WalletState -> CurrentEraResolvedTx
Expand Down

0 comments on commit 30ccd31

Please sign in to comment.