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

Do not query datums by hash in processConstraints #1634

Draft
wants to merge 2 commits into
base: develop
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
1 change: 0 additions & 1 deletion src/Contract/UnbalancedTx.purs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import Ctl.Internal.ProcessConstraints.Error
import Ctl.Internal.ProcessConstraints.Error
( MkUnbalancedTxError
( CannotFindDatum
, CannotQueryDatum
, CannotConvertPOSIXTimeRange
, CannotSolveTimeConstraints
, CannotGetMintingPolicyScriptIndex
Expand Down
32 changes: 19 additions & 13 deletions src/Internal/ProcessConstraints.purs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ import Ctl.Internal.ProcessConstraints.Error
, CannotMintZero
, ExpectedPlutusScriptGotNativeScript
, CannotFindDatum
, CannotQueryDatum
, CannotGetValidatorHashFromAddress
, TxOutRefWrongType
, CannotConvertPOSIXTimeRange
Expand Down Expand Up @@ -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)
Expand All @@ -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))`
Expand Down Expand Up @@ -226,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
Expand Down Expand Up @@ -345,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
Expand Down Expand Up @@ -548,15 +562,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
Expand Down
5 changes: 0 additions & 5 deletions src/Internal/ProcessConstraints/Error.purs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import Data.UInt as UInt

data MkUnbalancedTxError
= CannotFindDatum
| CannotQueryDatum DataHash
| CannotConvertPOSIXTimeRange POSIXTimeRange PosixTimeToSlotError
| CannotSolveTimeConstraints POSIXTimeRange POSIXTimeRange
| CannotGetMintingPolicyScriptIndex -- Should be impossible
Expand Down Expand Up @@ -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
Expand Down