Skip to content

Commit

Permalink
Fix BalanceTx.Error
Browse files Browse the repository at this point in the history
  • Loading branch information
klntsky committed Mar 1, 2024
1 parent 959339f commit 6347c6c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
35 changes: 15 additions & 20 deletions src/Internal/BalanceTx/Error.purs
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,15 @@ module Ctl.Internal.BalanceTx.Error

import Prelude

import Cardano.Types (Coin, Redeemer(Redeemer), Transaction)
import Cardano.Types.BigNum as BigNum
import Cardano.Types.TransactionInput (TransactionInput)
import Cardano.Types.TransactionOutput (TransactionOutput)
import Cardano.Types.UtxoMap (UtxoMap, pprintUtxoMap)
import Cardano.Types.Value (Value)
import Cardano.Types.Value (Value, pprintValue)
import Ctl.Internal.BalanceTx.RedeemerIndex (UnindexedRedeemer)
import Ctl.Internal.Cardano.Types.Transaction
( Redeemer(Redeemer)
, Transaction
, _redeemers
, _witnessSet
)
import Ctl.Internal.Cardano.Types.Value (pprintValue)
import Ctl.Internal.Helpers (bugTrackerLink, pprintTagSet)
import Ctl.Internal.Lens (_redeemers, _witnessSet)
import Ctl.Internal.QueryM.Ogmios
( RedeemerPointer
, ScriptFailure
Expand All @@ -52,8 +48,6 @@ import Ctl.Internal.QueryM.Ogmios
)
, TxEvaluationFailure(UnparsedError, AdditionalUtxoOverlap, ScriptFailures)
) as Ogmios
import Ctl.Internal.Types.Natural (toBigInt) as Natural
import Ctl.Internal.Types.ProtocolParameters (CoinsPerUtxoUnit)
import Data.Array (catMaybes, filter, uncons) as Array
import Data.Bifunctor (bimap)
import Data.Either (Either(Left, Right), either, isLeft)
Expand All @@ -62,15 +56,16 @@ import Data.FoldableWithIndex (foldMapWithIndex)
import Data.Function (applyN)
import Data.Generic.Rep (class Generic)
import Data.Int (ceil, decimal, toNumber, toStringAs)
import Data.Lens (non, (^.))
import Data.Lens ((^.))
import Data.Maybe (Maybe(Just, Nothing))
import Data.Newtype (class Newtype, unwrap)
import Data.Show.Generic (genericShow)
import Data.String (Pattern(Pattern))
import Data.String.CodePoints (length) as String
import Data.String.Common (joinWith, split) as String
import Data.String.Utils (padEnd)
import JS.BigInt (toString) as BigInt
import Data.UInt as UInt
import JS.BigInt as BigInt

-- | Errors conditions that may possibly arise during transaction balancing
data BalanceTxError
Expand All @@ -80,7 +75,7 @@ data BalanceTxError
| InsufficientCollateralUtxos UtxoMap
| CouldNotGetUtxos
| CollateralReturnError
| CollateralReturnMinAdaValueCalcError CoinsPerUtxoUnit TransactionOutput
| CollateralReturnMinAdaValueCalcError Coin TransactionOutput
| ExUnitsEvaluationFailed Transaction Ogmios.TxEvaluationFailure
| InsufficientUtxoBalanceToCoverAsset String
| ReindexRedeemersError UnindexedRedeemer
Expand Down Expand Up @@ -188,10 +183,10 @@ number ary = freeze (foldl go [] ary)
biggestPrefix :: String
biggestPrefix = toStringAs decimal (length (Array.filter isLeft ary)) <> ". "

width :: Int
width :: Prim.Int
width = ceil (toNumber (String.length biggestPrefix) / 2.0) * 2

numberLine :: Int -> String -> String
numberLine :: Prim.Int -> String -> String
numberLine i l = padEnd width (toStringAs decimal (i + 1) <> ". ") <> l

indentLine :: String -> String
Expand All @@ -216,15 +211,15 @@ printTxEvaluationFailure transaction e =
lookupRedeemerPointer
:: Ogmios.RedeemerPointer -> Maybe Redeemer
lookupRedeemerPointer ptr =
flip find (transaction ^. _witnessSet <<< _redeemers <<< non [])
flip find (transaction ^. _witnessSet <<< _redeemers)
$ \(Redeemer { index, tag }) -> tag == ptr.redeemerTag && index ==
Natural.toBigInt ptr.redeemerIndex
(BigNum.fromInt $ UInt.toInt ptr.redeemerIndex)

printRedeemerPointer :: Ogmios.RedeemerPointer -> PrettyString
printRedeemerPointer ptr =
line
( show ptr.redeemerTag <> ":" <> BigInt.toString
(Natural.toBigInt ptr.redeemerIndex)
(BigInt.fromInt $ UInt.toInt ptr.redeemerIndex)
)

-- TODO Investigate if more details can be printed, for example minting
Expand Down Expand Up @@ -277,8 +272,8 @@ printTxEvaluationFailure transaction e =
Ogmios.IllFormedExecutionBudget (Just { memory, steps }) ->
line "Ill formed execution budget:"
<> bullet
( line ("Memory: " <> BigInt.toString (Natural.toBigInt memory))
<> line ("Steps: " <> BigInt.toString (Natural.toBigInt steps))
( line ("Memory: " <> BigNum.toString memory)
<> line ("Steps: " <> BigNum.toString steps)
)
Ogmios.NoCostModelForLanguage languages ->
line "No cost model for languages:"
Expand Down
5 changes: 3 additions & 2 deletions src/Internal/BalanceTx/RedeemerIndex.purs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ import Type.Proxy (Proxy(..))

attachRedeemers :: Array Redeemer -> Transaction -> Transaction
attachRedeemers redeemers =
_Newtype <<< prop (Proxy :: Proxy "witnessSet") <<<
_Newtype <<< prop (Proxy :: Proxy "redeemers") .~ redeemers
_Newtype <<< prop (Proxy :: Proxy "witnessSet")
<<< _Newtype
<<< prop (Proxy :: Proxy "redeemers") .~ redeemers

attachIndexedRedeemers :: Array IndexedRedeemer -> Transaction -> Transaction
attachIndexedRedeemers = attachRedeemers <<< map indexedRedeemerToRedeemer
Expand Down
15 changes: 15 additions & 0 deletions src/Internal/Lens.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Ctl.Internal.Lens (_witnessSet, _redeemers) where

import Prelude

import Cardano.Types (Redeemer, Transaction, TransactionWitnessSet)
import Data.Lens (Lens')
import Data.Lens.Iso.Newtype (_Newtype)
import Data.Lens.Record (prop)
import Type.Proxy (Proxy(..))

_witnessSet :: Lens' Transaction TransactionWitnessSet
_witnessSet = _Newtype <<< prop (Proxy :: Proxy "witnessSet")

_redeemers :: Lens' TransactionWitnessSet (Array Redeemer)
_redeemers = _Newtype <<< prop (Proxy :: Proxy "redeemers")

0 comments on commit 6347c6c

Please sign in to comment.