Skip to content

Commit b112a60

Browse files
Merge #4238
4238: Add V2 script context equivalence test shell scripts r=Jimbo4350 a=Jimbo4350 This pr adds two plutus scripts with accompanying shell scripts: `scripts/babbage/script-context-equivalance-test.sh` `scripts/babbage/script-mint-context-equivalance-test.sh` The plutus scripts test various fields in the `ScriptContext` for equivalence in our redeemer that is created by the `create-script-context` executable (from `plutus-example` in `plutus-apps`). The shell script succeed depending on if the fields in the `ScriptContext` are actually equivalent to what is in our redeemer. Co-authored-by: Jordan Millar <[email protected]>
2 parents 7038de9 + 289b7e4 commit b112a60

13 files changed

+361
-15
lines changed

cardano-api/src/Cardano/Api.hs

+2
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ module Cardano.Api (
237237

238238
-- ** Fee calculation
239239
transactionFee,
240+
toLedgerEpochInfo,
240241
estimateTransactionFee,
241242
evaluateTransactionFee,
242243
estimateTransactionKeyWitnessCount,
@@ -578,6 +579,7 @@ module Cardano.Api (
578579
-- *** Local state query
579580
LocalStateQueryClient(..),
580581
QueryInMode(..),
582+
SystemStart(..),
581583
QueryInEra(..),
582584
QueryInShelleyBasedEra(..),
583585
QueryUTxOFilter(..),

cardano-api/src/Cardano/Api/Fees.hs

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
-- | Fee calculation
1515
--
1616
module Cardano.Api.Fees (
17-
1817
-- * Transaction fees
1918
transactionFee,
2019
estimateTransactionFee,
@@ -40,6 +39,7 @@ module Cardano.Api.Fees (
4039

4140
-- * Internal helpers
4241
mapTxScriptWitnesses,
42+
toLedgerEpochInfo,
4343
) where
4444

4545
import Prelude
@@ -578,10 +578,6 @@ evaluateTransactionExecutionUnits _eraInMode systemstart history pparams utxo tx
578578
of Left err -> Left (TransactionValidityTranslationError err)
579579
Right exmap -> Right (fromLedgerScriptExUnitsMap exmap)
580580

581-
toLedgerEpochInfo :: EraHistory mode -> EpochInfo (Either Text.Text)
582-
toLedgerEpochInfo (EraHistory _ interpreter) =
583-
hoistEpochInfo (first (Text.pack . show) . runExcept) $
584-
Consensus.interpreterToEpochInfo interpreter
585581

586582
toAlonzoCostModelsArray
587583
:: Map AnyPlutusScriptVersion CostModel
@@ -637,6 +633,10 @@ evaluateTransactionExecutionUnits _eraInMode systemstart history pparams utxo tx
637633
obtainHasFieldConstraint CollateralInAlonzoEra f = f
638634
obtainHasFieldConstraint CollateralInBabbageEra f = f
639635

636+
toLedgerEpochInfo :: EraHistory mode -> EpochInfo (Either Text.Text)
637+
toLedgerEpochInfo (EraHistory _ interpreter) =
638+
hoistEpochInfo (first (Text.pack . show) . runExcept) $
639+
Consensus.interpreterToEpochInfo interpreter
640640

641641
-- ----------------------------------------------------------------------------
642642
-- Transaction balance

cardano-cli/src/Cardano/CLI/Shelley/Run/Transaction.hs

-2
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,8 @@ import Ouroboros.Consensus.Shelley.Eras (StandardAllegra, StandardCryp
4343

4444
--TODO: do this nicely via the API too:
4545
import qualified Cardano.Binary as CBOR
46-
4746
--TODO: following import needed for orphan Eq Script instance
4847
import Cardano.Ledger.Shelley.Scripts ()
49-
import Cardano.Ledger.ShelleyMA.TxBody ()
5048

5149
import Cardano.CLI.Environment (EnvSocketError, readEnvSocketPath, renderEnvSocketError)
5250
import Cardano.CLI.Run.Friendly (friendlyTxBS, friendlyTxBodyBS)

scripts/babbage/example-babbage-script-usage.sh

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ export UTXO_SKEY="${UTXO_SKEY:-example/utxo-keys/utxo1.skey}"
1515
export RESULT_FILE="${RESULT_FILE:-$WORK/result.out}"
1616

1717
echo "Socket path: $CARDANO_NODE_SOCKET_PATH"
18-
echo "Socket path: $(pwd)"
1918

2019
ls -al "$CARDANO_NODE_SOCKET_PATH"
2120

@@ -110,7 +109,7 @@ $CARDANO_CLI query utxo --address $plutusspendingscriptaddr --testnet-magic "$TE
110109
plutuslockedutxotxin=$(jq -r 'keys[0]' $WORK/plutusutxo.json)
111110
lovelaceatplutusspendingscriptaddr=$(jq -r ".[\"$plutuslockedutxotxin\"].value.lovelace" $WORK/plutusutxo.json)
112111

113-
#Get read only reference input
112+
# Get read only reference input
114113
$CARDANO_CLI query utxo --address "$readonlyaddress" --cardano-mode \
115114
--testnet-magic "$TESTNET_MAGIC" --out-file $WORK/read-only-ref-input-utxo.json
116115
readonlyrefinput=$(jq -r 'keys[0]' $WORK/read-only-ref-input-utxo.json)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
#!/usr/bin/env bash
2+
3+
# Unofficial bash strict mode.
4+
# See: http://redsymbol.net/articles/unofficial-bash-strict-mode/
5+
set -e
6+
set -o pipefail
7+
8+
export WORK="${WORK:-example/work}"
9+
export BASE="${BASE:-.}"
10+
export CARDANO_CLI="${CARDANO_CLI:-cardano-cli}"
11+
export CARDANO_NODE_SOCKET_PATH="${CARDANO_NODE_SOCKET_PATH:-example/main.sock}"
12+
export TESTNET_MAGIC="${TESTNET_MAGIC:-42}"
13+
export UTXO_VKEY="${UTXO_VKEY:-example/utxo-keys/utxo1.vkey}"
14+
export UTXO_SKEY="${UTXO_SKEY:-example/utxo-keys/utxo1.skey}"
15+
export RESULT_FILE="${RESULT_FILE:-$WORK/result.out}"
16+
17+
mkdir -p "$WORK"
18+
19+
echo "Socket path: $CARDANO_NODE_SOCKET_PATH"
20+
21+
ls -al "$CARDANO_NODE_SOCKET_PATH"
22+
23+
# NB: This plutus script uses a "typed" redeemer and "typed" datum.
24+
plutusscriptinuse="$BASE/scripts/plutus/scripts/v2/context-equivalence-test.plutus"
25+
# This datum hash is the hash of the typed 42
26+
scriptdatumhash="fcaa61fb85676101d9e3398a484674e71c45c3fd41b492682f3b0054f4cf3273"
27+
datumfilepath="$BASE/scripts/plutus/data/typed-42.datum"
28+
redeemerfilepath="$BASE/scripts/plutus/data/script-context.redeemer"
29+
certifyingscript="scripts/plutus/scripts/v2/stake-script.plutus"
30+
31+
# Create certificate
32+
cardano-cli stake-address registration-certificate \
33+
--stake-script-file "$certifyingscript" \
34+
--out-file "$WORK/script.regcert"
35+
36+
# Step 1: Create a tx output with a datum hash at the script address. In order for a tx output to be locked
37+
# by a plutus script, it must have a datahash. We also need collateral tx inputs so we split the utxo
38+
# in order to accommodate this.
39+
40+
41+
plutusscriptaddr=$($CARDANO_CLI address build --payment-script-file "$plutusscriptinuse" --testnet-magic "$TESTNET_MAGIC")
42+
# The input at the readonlyaddress will be used as a reference input
43+
readonlyaddress=addr_test1vz3t3f2kgy2re66tnhgxc4t8jgylw2cqfnxdwlrq9agfmtstxxkm5
44+
45+
mkdir -p "$WORK"
46+
47+
utxoaddr=$($CARDANO_CLI address build --testnet-magic "$TESTNET_MAGIC" --payment-verification-key-file "$UTXO_VKEY")
48+
49+
$CARDANO_CLI query utxo --address "$utxoaddr" --cardano-mode --testnet-magic "$TESTNET_MAGIC" --out-file $WORK/utxo-1.json
50+
cat $WORK/utxo-1.json
51+
52+
txin=$(jq -r 'keys[]' $WORK/utxo-1.json)
53+
lovelaceattxin=$(jq -r ".[\"$txin\"].value.lovelace" $WORK/utxo-1.json)
54+
lovelaceattxindiv6=$(expr $lovelaceattxin / 6)
55+
56+
$CARDANO_CLI query protocol-parameters --testnet-magic "$TESTNET_MAGIC" --out-file $WORK/pparams.json
57+
58+
$CARDANO_CLI transaction build \
59+
--babbage-era \
60+
--cardano-mode \
61+
--testnet-magic "$TESTNET_MAGIC" \
62+
--change-address "$utxoaddr" \
63+
--tx-in "$txin" \
64+
--tx-out "$readonlyaddress+$lovelaceattxindiv6" \
65+
--tx-out "$plutusscriptaddr+$lovelaceattxindiv6" \
66+
--tx-out-datum-hash "$scriptdatumhash" \
67+
--tx-out "$utxoaddr+$lovelaceattxindiv6" \
68+
--protocol-params-file "$WORK/pparams.json" \
69+
--out-file "$WORK/create-datum-output.body"
70+
71+
$CARDANO_CLI transaction sign \
72+
--tx-body-file $WORK/create-datum-output.body \
73+
--testnet-magic "$TESTNET_MAGIC" \
74+
--signing-key-file $UTXO_SKEY \
75+
--out-file $WORK/create-datum-output.tx
76+
77+
# SUBMIT
78+
$CARDANO_CLI transaction submit --tx-file $WORK/create-datum-output.tx --testnet-magic "$TESTNET_MAGIC"
79+
echo "Pausing for 5 seconds..."
80+
sleep 5
81+
82+
# Step 2
83+
# After "locking" the tx output at the script address, we can now can attempt to spend
84+
# the "locked" tx output below.
85+
86+
$CARDANO_CLI query utxo --address $plutusscriptaddr --testnet-magic "$TESTNET_MAGIC" --out-file $WORK/plutusutxo.json
87+
88+
plutusutxotxin=$(jq -r 'keys[]' $WORK/plutusutxo.json)
89+
90+
$CARDANO_CLI query utxo --address $utxoaddr --cardano-mode --testnet-magic "$TESTNET_MAGIC" --out-file $WORK/utxo-2.json
91+
cat $WORK/utxo-2.json
92+
txinCollateral=$(jq -r 'keys[0]' $WORK/utxo-2.json)
93+
94+
95+
dummyaddress=addr_test1vpqgspvmh6m2m5pwangvdg499srfzre2dd96qq57nlnw6yctpasy4
96+
97+
lovelaceatplutusscriptaddr=$(jq -r ".[\"$plutusutxotxin\"].value.lovelace" $WORK/plutusutxo.json)
98+
99+
# Get read only reference input
100+
$CARDANO_CLI query utxo --address "$readonlyaddress" --cardano-mode \
101+
--testnet-magic "$TESTNET_MAGIC" --out-file $WORK/read-only-ref-input-utxo.json
102+
readonlyrefinput=$(jq -r 'keys[0]' $WORK/read-only-ref-input-utxo.json)
103+
104+
105+
106+
# We need to generate a dummy redeemer (the cli demands a redeemer) in order to create a txbody from which we can generate
107+
# a tx and then derive the correct redeemer.
108+
create-script-context --plutus-v2 --out-file "$WORK/script-context.redeemer"
109+
110+
correctredeemer="$WORK/script-context.redeemer"
111+
112+
echo "Constructing dummy tx..."
113+
114+
# Create dummy certificate
115+
certifyingscript="scripts/plutus/scripts/v2/stake-script.plutus"
116+
cardano-cli stake-address registration-certificate \
117+
--stake-script-file "$certifyingscript" \
118+
--out-file "$WORK/script.regcert"
119+
120+
requiredsignerhash=$(cardano-cli address key-hash --payment-verification-key-file ${UTXO_VKEY})
121+
122+
# DUMMY TX! We generate the actual redeemer from this!
123+
# --certificate-file "$WORK/script.regcert" \
124+
redeemerfilepath="$BASE/scripts/plutus/data/42.redeemer"
125+
$CARDANO_CLI transaction build \
126+
--babbage-era \
127+
--cardano-mode \
128+
--testnet-magic "$TESTNET_MAGIC" \
129+
--script-invalid \
130+
--change-address "$utxoaddr" \
131+
--invalid-before 1 \
132+
--invalid-hereafter 400 \
133+
--certificate-file "$WORK/script.regcert" \
134+
--read-only-tx-in-reference "$readonlyrefinput" \
135+
--tx-in "$plutusutxotxin" \
136+
--tx-in-collateral "$txinCollateral" \
137+
--tx-out "$dummyaddress+10000000" \
138+
--tx-in-script-file "$plutusscriptinuse" \
139+
--tx-in-datum-file "$datumfilepath" \
140+
--protocol-params-file "$WORK/pparams.json" \
141+
--tx-in-redeemer-file "$redeemerfilepath" \
142+
--required-signer-hash "$requiredsignerhash" \
143+
--out-file $WORK/test-alonzo.body
144+
145+
$CARDANO_CLI transaction sign \
146+
--tx-body-file $WORK/test-alonzo.body \
147+
--testnet-magic "$TESTNET_MAGIC" \
148+
--signing-key-file "${UTXO_SKEY}" \
149+
--out-file $WORK/test-alonzo.tx
150+
151+
# Generate the "real" redeeemer!
152+
153+
create-script-context \
154+
--generate-tx "$WORK/test-alonzo.tx" \
155+
--plutus-v2 \
156+
--out-file "$WORK/script-context.redeemer" \
157+
--cardano-mode \
158+
--testnet-magic 42 \
159+
160+
echo "Constructing real tx..."
161+
# REAL TX!
162+
163+
$CARDANO_CLI transaction build \
164+
--babbage-era \
165+
--cardano-mode \
166+
--testnet-magic "$TESTNET_MAGIC" \
167+
--script-valid \
168+
--invalid-before 1 \
169+
--invalid-hereafter 400 \
170+
--change-address "$utxoaddr" \
171+
--certificate-file "$WORK/script.regcert" \
172+
--read-only-tx-in-reference "$readonlyrefinput" \
173+
--tx-in "$plutusutxotxin" \
174+
--tx-in-collateral "$txinCollateral" \
175+
--tx-out "$dummyaddress+10000000" \
176+
--tx-in-script-file "$plutusscriptinuse" \
177+
--tx-in-datum-file "$datumfilepath" \
178+
--protocol-params-file "$WORK/pparams.json" \
179+
--tx-in-redeemer-file "$correctredeemer" \
180+
--required-signer-hash "$requiredsignerhash" \
181+
--out-file $WORK/test-alonzo-final.body
182+
183+
$CARDANO_CLI transaction sign \
184+
--tx-body-file $WORK/test-alonzo-final.body \
185+
--testnet-magic "$TESTNET_MAGIC" \
186+
--signing-key-file "${UTXO_SKEY}" \
187+
--out-file $WORK/alonzo.tx
188+
189+
# SUBMIT $WORK/alonzo.tx containing the correct redeemer
190+
191+
echo "Submit the tx with plutus script and wait 5 seconds..."
192+
$CARDANO_CLI transaction submit --tx-file $WORK/alonzo.tx --testnet-magic "$TESTNET_MAGIC"
193+
sleep 5
194+
echo ""
195+
echo "Querying UTxO at $dummyaddress. If there is ADA at the address the Plutus script successfully executed!"
196+
echo ""
197+
$CARDANO_CLI query utxo --address "$dummyaddress" --testnet-magic "$TESTNET_MAGIC" \
198+
| tee "$RESULT_FILE"

0 commit comments

Comments
 (0)