Skip to content

Commit

Permalink
Apply suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
errfrom committed Sep 28, 2023
1 parent ce5c267 commit d995f3a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 30 deletions.
27 changes: 14 additions & 13 deletions src/Internal/BalanceTx/ExUnitsAndMinFee.purs
Original file line number Diff line number Diff line change
Expand Up @@ -84,30 +84,31 @@ evalTxExecutionUnits tx = do
networkId <- askNetworkId
additionalUtxos <-
fromPlutusUtxoMap networkId <$> asksConstraints Constraints._additionalUtxos
worker additionalUtxos
worker $ toOgmiosAdditionalUtxos additionalUtxos
where
worker :: UtxoMap -> BalanceTxM Ogmios.TxEvaluationResult
toOgmiosAdditionalUtxos :: UtxoMap -> Ogmios.AdditionalUtxoSet
toOgmiosAdditionalUtxos additionalUtxos =
wrap $ Map.fromFoldable
( bimap transactionInputToTxOutRef transactionOutputToOgmiosTxOut
<$> (Map.toUnfoldable :: _ -> Array _) additionalUtxos
)

worker :: Ogmios.AdditionalUtxoSet -> BalanceTxM Ogmios.TxEvaluationResult
worker additionalUtxos = do
queryHandle <- liftContract getQueryHandle
evalResult <-
unwrap <$> liftContract
(liftAff $ queryHandle.evaluateTx tx ogmiosAdditionalUtxos)
(liftAff $ queryHandle.evaluateTx tx additionalUtxos)
case evalResult of
Right a -> pure a
Left (Ogmios.AdditionalUtxoOverlap overlappingUtxos) ->
-- Remove overlapping additional utxos and retry evaluation:
worker $ Map.filterKeys (flip Array.notElem overlappingUtxos)
additionalUtxos
worker $ wrap $ Map.filterKeys (flip Array.notElem overlappingUtxos)
(unwrap additionalUtxos)
Left evalFailure | tx ^. _isValid ->
throwError $ ExUnitsEvaluationFailed tx evalFailure
Left _ -> pure $ wrap Map.empty
where
ogmiosAdditionalUtxos :: Ogmios.AdditionalUtxoSet
ogmiosAdditionalUtxos =
wrap $ Map.fromFoldable
( bimap transactionInputToTxOutRef transactionOutputToOgmiosTxOut
<$> (Map.toUnfoldable :: _ -> Array _) additionalUtxos
)
Left _ ->
pure $ wrap Map.empty

-- Calculates the execution units needed for each script in the transaction
-- and the minimum fee, including the script fees.
Expand Down
19 changes: 2 additions & 17 deletions src/Internal/QueryM/Ogmios.purs
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ instance Show ScriptFailure where
-- CannotCreateEvaluationContext
data TxEvaluationFailure
= UnparsedError String
| AdditionalUtxoOverlap (Array TransactionInput)
| AdditionalUtxoOverlap (Array OgmiosTxOutRef)
| ScriptFailures (Map RedeemerPointer (Array ScriptFailure))

derive instance Generic TxEvaluationFailure _
Expand Down Expand Up @@ -873,22 +873,7 @@ instance DecodeAeson TxEvaluationFailure where
decodeAdditionalUtxoOverlap = ReaderT \o -> do
ogmiosOrefs <-
flip getField "AdditionalUtxoOverlap" =<< getField o "EvaluationFailure"
orefs <-
note orefConversionError $
traverse ogmiosOrefToTransactionInput ogmiosOrefs
pure $ AdditionalUtxoOverlap orefs
where
orefConversionError :: JsonDecodeError
orefConversionError =
TypeMismatch "Could not convert OgmiosTxOutRef to TransactionInput"

ogmiosOrefToTransactionInput :: OgmiosTxOutRef -> Maybe TransactionInput
ogmiosOrefToTransactionInput { txId, index } =
hexToByteArray txId <#> \transactionId ->
wrap
{ transactionId: wrap transactionId
, index
}
pure $ AdditionalUtxoOverlap ogmiosOrefs

---------------- PROTOCOL PARAMETERS QUERY RESPONSE & PARSING

Expand Down

0 comments on commit d995f3a

Please sign in to comment.