Skip to content
This repository has been archived by the owner on Dec 2, 2024. It is now read-only.

Commit

Permalink
PLT-807 Change behavior of MustPayToPubKeyAddress and MustPayToOtherS…
Browse files Browse the repository at this point in the history
…cript w.r.t datum in transaction body

* Changed `MustPayToPubKeyAddress` and `MustPayToOtherScript` so that
  they don't add the datum in the transaction body by default. Need to
  use `MustIncludeDatumInTx` instead.

* Changed the name of the constraint `MustIncludeDatum` to
  `MustIncludeDatumInTx`. This constraint adds the datum in the
  transaction body iif there's a transaction output with the same datum
  hash.
  • Loading branch information
koslambrou committed Sep 13, 2022
1 parent 725185d commit 058ebbe
Show file tree
Hide file tree
Showing 22 changed files with 993 additions and 382 deletions.
2 changes: 1 addition & 1 deletion plutus-contract/src/Plutus/Contract/Oracle.hs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ checkHashConstraints ::
checkHashConstraints SignedMessage{osmMessageHash, osmDatum=Datum dt} =
maybe
(trace "Li" {-"DecodingError"-} $ Left DecodingError)
(\a -> pure (a, Constraints.mustHashDatum osmMessageHash (Datum dt)))
(\a -> pure (a, Constraints.mustIncludeDatumInTxWithHash osmMessageHash (Datum dt)))
(fromBuiltinData dt)

{-# INLINABLE verifySignedMessageConstraints #-}
Expand Down
12 changes: 10 additions & 2 deletions plutus-contract/test/Spec/Balancing.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@ balanceTxnMinAda =
contract :: Contract () EmptySchema ContractError ()
contract = do
let constraints1 = L.Constraints.mustPayToOtherScript vHash unitDatum (Value.scale 100 ff <> Ada.toValue Ledger.minAdaTxOut)
<> L.Constraints.mustIncludeDatumInTx unitDatum
utx1 = either (error . show) id $ L.Constraints.mkTx @Void mempty constraints1
submitTxConfirmed utx1
utxo <- utxosAt someAddress
let txOutRef = head (Map.keys utxo)
constraints2 = L.Constraints.mustSpendScriptOutput txOutRef unitRedeemer
<> L.Constraints.mustPayToOtherScript vHash unitDatum (Value.scale 200 ee)
<> L.Constraints.mustIncludeDatumInTx unitDatum
lookups2 = L.Constraints.unspentOutputs utxo <> L.Constraints.plutusV1OtherScript someValidator
utx2 <- Con.adjustUnbalancedTx $ either (error . show) id $ L.Constraints.mkTx @Void lookups2 constraints2
submitTxConfirmed utx2
Expand Down Expand Up @@ -83,7 +85,10 @@ balanceTxnMinAda2 =
-- Make sure there is a utxo with 1 A, 1 B, and 4 ada at w2
submitTxConfirmed =<< mkTx mempty (payToWallet w2 (vA 1 <> vB 1 <> Value.scale 2 (Ada.toValue Ledger.minAdaTxOut)))
-- Make sure there is a UTxO with 1 B and datum () at the script
submitTxConfirmed =<< mkTx mempty (L.Constraints.mustPayToOtherScript vHash unitDatum (vB 1))
submitTxConfirmed
=<< mkTx mempty ( L.Constraints.mustPayToOtherScript vHash unitDatum (vB 1)
<> L.Constraints.mustIncludeDatumInTx unitDatum
)
-- utxo0 @ wallet2 = 1 A, 1 B, 4 Ada
-- utxo1 @ script = 1 B, 2 Ada

Expand All @@ -94,9 +99,12 @@ balanceTxnMinAda2 =
lookups = L.Constraints.unspentOutputs utxos
<> L.Constraints.plutusV1OtherScript someValidator
<> L.Constraints.plutusV1MintingPolicy mps
datum = Datum $ PlutusTx.toBuiltinData (0 :: Integer)
constraints = L.Constraints.mustSpendScriptOutput txOutRef unitRedeemer -- spend utxo1
<> L.Constraints.mustPayToOtherScript vHash unitDatum (vB 1) -- 2 ada and 1 B to script
<> L.Constraints.mustPayToOtherScript vHash (Datum $ PlutusTx.toBuiltinData (0 :: Integer)) (vB 1) -- 2 ada and 1 B to script (different datum)
<> L.Constraints.mustPayToOtherScript vHash datum (vB 1) -- 2 ada and 1 B to script (different datum)
<> L.Constraints.mustIncludeDatumInTx unitDatum
<> L.Constraints.mustIncludeDatumInTx datum
<> L.Constraints.mustMintValue (vL 1) -- 1 L and 2 ada to wallet2
submitTxConfirmed =<< mkTx lookups constraints

Expand Down
16 changes: 12 additions & 4 deletions plutus-contract/test/Spec/Contract/TxConstraints.hs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ mustReferenceOutputV1ConTest = do
let ((utxoRef, utxo), (utxoRefForBalance1, _), (utxoRefForBalance2, _)) = get3 $ Map.toList utxos
vh = fromJust $ Addr.toValidatorHash mustReferenceOutputV1ValidatorAddress
lookups = TC.unspentOutputs utxos
tx = TC.mustPayToOtherScript vh (Datum $ PlutusTx.toBuiltinData utxoRef) (Ada.adaValueOf 5)
datum = Datum $ PlutusTx.toBuiltinData utxoRef
tx = TC.mustPayToOtherScript vh datum (Ada.adaValueOf 5)
<> TC.mustIncludeDatumInTx datum
<> TC.mustSpendPubKeyOutput utxoRefForBalance1
mkTxConstraints @Void lookups tx >>= submitTxConfirmed

Expand All @@ -152,7 +154,9 @@ mustReferenceOutputTxV1ConTest = do
let ((utxoRef, utxo), (utxoRefForBalance1, _), (utxoRefForBalance2, _)) = get3 $ Map.toList utxos
vh = fromJust $ Addr.toValidatorHash mustReferenceOutputV1ValidatorAddress
lookups = Tx.Constraints.unspentOutputs utxos
tx = Tx.Constraints.mustPayToOtherScript vh (Datum $ PlutusTx.toBuiltinData utxoRef) (Ada.adaValueOf 5)
datum = Datum $ PlutusTx.toBuiltinData utxoRef
tx = Tx.Constraints.mustPayToOtherScript vh datum (Ada.adaValueOf 5)
<> Tx.Constraints.mustIncludeDatumInTx datum
<> Tx.Constraints.mustSpendPubKeyOutput utxoRefForBalance1
<> Tx.Constraints.mustUseOutputAsCollateral utxoRefForBalance1
submitTxConfirmed $ mkTx lookups tx
Expand Down Expand Up @@ -197,7 +201,9 @@ mustReferenceOutputV2ConTest = do
let ((utxoRef, utxo), (utxoRefForBalance1, _), (utxoRefForBalance2, _)) = get3 $ Map.toList utxos
vh = fromJust $ Addr.toValidatorHash mustReferenceOutputV2ValidatorAddress
lookups = TC.unspentOutputs utxos
tx = TC.mustPayToOtherScript vh (Datum $ PlutusTx.toBuiltinData utxoRef) (Ada.adaValueOf 5)
datum = Datum $ PlutusTx.toBuiltinData utxoRef
tx = TC.mustPayToOtherScript vh datum (Ada.adaValueOf 5)
<> TC.mustIncludeDatumInTx datum
<> TC.mustSpendPubKeyOutput utxoRefForBalance1
mkTxConstraints @Void lookups tx >>= submitTxConfirmed

Expand All @@ -221,7 +227,9 @@ mustReferenceOutputTxV2ConTest = do
let ((utxoRef, utxo), (utxoRefForBalance1, _), (utxoRefForBalance2, _)) = get3 $ Map.toList utxos
vh = fromJust $ Addr.toValidatorHash mustReferenceOutputV2ValidatorAddress
lookups = Tx.Constraints.unspentOutputs utxos
tx = Tx.Constraints.mustPayToOtherScript vh (Datum $ PlutusTx.toBuiltinData utxoRef) (Ada.adaValueOf 5)
datum = Datum $ PlutusTx.toBuiltinData utxoRef
tx = Tx.Constraints.mustPayToOtherScript vh datum (Ada.adaValueOf 5)
<> Tx.Constraints.mustIncludeDatumInTx datum
<> Tx.Constraints.mustSpendPubKeyOutput utxoRefForBalance1
<> Tx.Constraints.mustUseOutputAsCollateral utxoRefForBalance1
submitTxConfirmed $ mkTx lookups tx
Expand Down
6 changes: 4 additions & 2 deletions plutus-contract/test/Spec/ErrorChecking.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import Data.Row
import Test.Tasty

import Ledger.Ada qualified as Ada
import Ledger.Constraints (collectFromTheScript, mustPayToOtherScript)
import Ledger.Constraints (collectFromTheScript, mustIncludeDatumInTx, mustPayToOtherScript)
import Ledger.Tx (getCardanoTxId)
import Ledger.Typed.Scripts qualified as Scripts hiding (validatorHash)
import Plutus.Contract as Contract
Expand Down Expand Up @@ -141,7 +141,9 @@ contract = selectList [failFalseC, failHeadNilC, divZeroC, divZeroTraceC, succes
run validator = void $ do
let addr = mkValidatorAddress (validatorScript validator)
hash = validatorHash (validatorScript validator)
tx = mustPayToOtherScript hash (Datum $ toBuiltinData ()) (Ada.adaValueOf 10)
datum = Datum $ toBuiltinData ()
tx = mustPayToOtherScript hash datum (Ada.adaValueOf 10)
<> mustIncludeDatumInTx datum
r <- submitTx tx
awaitTxConfirmed (getCardanoTxId r)
utxos <- utxosAt addr
Expand Down
Loading

0 comments on commit 058ebbe

Please sign in to comment.