From aa8169ea9d9cea247558581d16e13b815bc6b520 Mon Sep 17 00:00:00 2001 From: Dzmitry Shuysky Date: Tue, 16 Jul 2024 12:42:02 +0300 Subject: [PATCH 1/2] Do not query datums by hash in processConstraints --- src/Contract/UnbalancedTx.purs | 1 - src/Internal/ProcessConstraints.purs | 14 ++------------ src/Internal/ProcessConstraints/Error.purs | 5 ----- 3 files changed, 2 insertions(+), 18 deletions(-) diff --git a/src/Contract/UnbalancedTx.purs b/src/Contract/UnbalancedTx.purs index 4d13f7fb4..23b2f08f7 100644 --- a/src/Contract/UnbalancedTx.purs +++ b/src/Contract/UnbalancedTx.purs @@ -18,7 +18,6 @@ import Ctl.Internal.ProcessConstraints.Error import Ctl.Internal.ProcessConstraints.Error ( MkUnbalancedTxError ( CannotFindDatum - , CannotQueryDatum , CannotConvertPOSIXTimeRange , CannotSolveTimeConstraints , CannotGetMintingPolicyScriptIndex diff --git a/src/Internal/ProcessConstraints.purs b/src/Internal/ProcessConstraints.purs index 01b5cae7c..b07d4d0b7 100644 --- a/src/Internal/ProcessConstraints.purs +++ b/src/Internal/ProcessConstraints.purs @@ -81,7 +81,6 @@ import Ctl.Internal.ProcessConstraints.Error , CannotMintZero , ExpectedPlutusScriptGotNativeScript , CannotFindDatum - , CannotQueryDatum , CannotGetValidatorHashFromAddress , TxOutRefWrongType , CannotConvertPOSIXTimeRange @@ -173,7 +172,7 @@ import Ctl.Internal.Types.Val as Val import Data.Array (cons, partition, toUnfoldable, zip) import Data.Array (mapMaybe, singleton, (:)) as Array import Data.Bifunctor (lmap) -import Data.Either (Either(Left, Right), either, hush, isRight, note) +import Data.Either (Either(Left, Right), either, note) import Data.Foldable (foldM) import Data.Lens ((%=), (%~), (.=), (.~), (<>=)) import Data.Lens.Getter (to, use) @@ -192,7 +191,6 @@ import Effect.Aff.Class (liftAff) import Effect.Class (liftEffect) import Effect.Exception (throw) import Partial.Unsafe (unsafePartial) -import Prelude (join) as Bind -- The constraints don't precisely match those of Plutus: -- `forall v. (FromData (DatumType v), ToData (DatumType v), ToData (RedeemerType v))` @@ -548,15 +546,7 @@ processConstraint -- Use the datum hash inside the lookup case datum' of Just (OutputDatumHash dHash) -> do - dat <- ExceptT do - mDatumLookup <- lookupDatum dHash - if isRight mDatumLookup then - pure mDatumLookup - else - liftAff $ queryHandle.getDatumByHash dHash <#> hush - >>> Bind.join - >>> note - (CannotQueryDatum dHash) + dat <- ExceptT $ lookupDatum dHash lift $ addDatum dat Just (OutputDatum _) -> pure unit Nothing -> throwError CannotFindDatum diff --git a/src/Internal/ProcessConstraints/Error.purs b/src/Internal/ProcessConstraints/Error.purs index 519025660..1b2281e3c 100644 --- a/src/Internal/ProcessConstraints/Error.purs +++ b/src/Internal/ProcessConstraints/Error.purs @@ -37,7 +37,6 @@ import Data.UInt as UInt data MkUnbalancedTxError = CannotFindDatum - | CannotQueryDatum DataHash | CannotConvertPOSIXTimeRange POSIXTimeRange PosixTimeToSlotError | CannotSolveTimeConstraints POSIXTimeRange POSIXTimeRange | CannotGetMintingPolicyScriptIndex -- Should be impossible @@ -70,10 +69,6 @@ instance Show MkUnbalancedTxError where explainMkUnbalancedTxError :: MkUnbalancedTxError -> String explainMkUnbalancedTxError = case _ of CannotFindDatum -> "Cannot find datum" - CannotQueryDatum dh -> - "Querying for datum by datum hash (" - <> byteArrayToHex (unwrap $ encodeCbor dh) - <> ") failed: no datum found" CannotConvertPOSIXTimeRange tr ttsErr -> "Cannot convert POSIX time range to slot time range.\nRange: " <> show tr From b2b683ae9fdfcbab0ff199988cc153fd70dba820 Mon Sep 17 00:00:00 2001 From: Dzmitry Shuysky Date: Tue, 16 Jul 2024 13:40:45 +0300 Subject: [PATCH 2/2] Presort tx constraints to include all provided datums --- src/Internal/ProcessConstraints.purs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Internal/ProcessConstraints.purs b/src/Internal/ProcessConstraints.purs index b07d4d0b7..c5fb190e9 100644 --- a/src/Internal/ProcessConstraints.purs +++ b/src/Internal/ProcessConstraints.purs @@ -224,7 +224,8 @@ processLookupsAndConstraints timeConstraintsSolved <- except $ resumeTimeConstraints constraints - ExceptT $ foldConstraints (processConstraint ctx) timeConstraintsSolved + ExceptT $ foldConstraints (processConstraint ctx) $ sortConstraints + timeConstraintsSolved ExceptT addFakeScriptDataHash ExceptT addMissingValueSpent ExceptT updateUsedUtxos @@ -343,6 +344,21 @@ updateUsedUtxos = runExceptT do -- Left bias towards original map, hence `flip`: _cpsUsedUtxos %= flip union cTxOutputs +sortConstraints :: Array TxConstraint -> Array TxConstraint +sortConstraints constraints = + let + { yes: includeDatumConstraints, no: otherConstraints } = partition + isIncludeDatumConstraint + constraints + in + includeDatumConstraints <> otherConstraints + where + isIncludeDatumConstraint :: TxConstraint -> Boolean + isIncludeDatumConstraint = + case _ of + MustIncludeDatum _ -> true + _ -> false + resumeTimeConstraints :: Array TxConstraint -> Either MkUnbalancedTxError (Array TxConstraint) resumeTimeConstraints constraints = do