Skip to content

Commit 7b81cea

Browse files
authored
Merge pull request #4923 from IntersectMBO/zliu41/data-api
Update V3 scripts in plutus-preprocessor to use the new `Data` api
2 parents 9734171 + 95a9ca1 commit 7b81cea

File tree

9 files changed

+1598
-2375
lines changed

9 files changed

+1598
-2375
lines changed

cabal.project

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ source-repository-package
2222
-- see CONTRIBUTING.md#to-update-the-referenced-agda-ledger-spec
2323
index-state:
2424
, hackage.haskell.org 2025-01-14T00:25:08Z
25-
, cardano-haskell-packages 2025-01-08T16:35:32Z
25+
, cardano-haskell-packages 2025-04-03T08:46:45Z
2626

2727
packages:
2828
-- == Byron era ==

eras/alonzo/impl/testlib/Test/Cardano/Ledger/Alonzo/Imp/UtxowSpec/Invalid.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ spec = describe "Invalid transactions" $ do
271271
, mkDelegStakeTxCert cred poolId -- 1: Needs a redeemer
272272
, mkDelegStakeTxCert cred poolId -- 2: Duplicate, ignored, no redeemer needed
273273
]
274-
redeemer = (Data (P.I 32), ExUnits 5000 1_000_000)
274+
redeemer = (Data (P.I 32), ExUnits 15_000 5_000_000)
275275
redeemers = Map.fromList [(mkCertifyingPurpose (AsIx i), redeemer) | i <- [1 .. 2]]
276276
tx =
277277
mkBasicTx mkBasicTxBody

eras/conway/impl/testlib/Test/Cardano/Ledger/Conway/Imp/LedgerSpec.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ import Test.Cardano.Ledger.Conway.ImpTest
3333
import Test.Cardano.Ledger.Core.Rational (IsRatio (..))
3434
import Test.Cardano.Ledger.Imp.Common
3535
import Test.Cardano.Ledger.Plutus.Examples (
36-
alwaysFailsWithDatum,
3736
alwaysSucceedsNoDatum,
37+
purposeIsWellformedNoDatum,
3838
)
3939

4040
spec ::
@@ -48,7 +48,7 @@ spec = do
4848
it "TxRefScriptsSizeTooBig" $ do
4949
-- we use here the largest script we currently have as many times as necessary to
5050
-- trigger the predicate failure
51-
Just plutusScript <- pure $ mkPlutusScript @era $ alwaysFailsWithDatum SPlutusV3
51+
Just plutusScript <- pure $ mkPlutusScript @era $ purposeIsWellformedNoDatum SPlutusV3
5252
let script :: Script era
5353
script = fromPlutusScript plutusScript
5454
size = originalBytesSize script

flake.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

libs/cardano-ledger-core/testlib/Test/Cardano/Ledger/Plutus/Examples.hs

Lines changed: 1522 additions & 2315 deletions
Large diffs are not rendered by default.

libs/plutus-preprocessor/plutus-preprocessor.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ library
4242
bytestring,
4343
cardano-ledger-binary:testlib,
4444
cardano-ledger-core,
45-
plutus-ledger-api ^>=1.37,
45+
plutus-ledger-api >=1.44,
4646
plutus-tx,
4747
plutus-tx-plugin,
4848
template-haskell,

libs/plutus-preprocessor/src/Cardano/Ledger/Plutus/Preprocessor/Source/V1.hs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import qualified PlutusLedgerApi.V1 as PV1
77
import PlutusTx (fromBuiltinData, unsafeFromBuiltinData)
88
import qualified PlutusTx.AssocMap as PAM
99
import qualified PlutusTx.Builtins as P
10+
import qualified PlutusTx.List as PL
1011
import qualified PlutusTx.Prelude as P
1112

1213
alwaysSucceedsNoDatumQ :: Q [Dec]
@@ -153,11 +154,11 @@ purposeIsWellformedNoDatumQ =
153154
-- Spending PlutusV1 scripts must have a Datum
154155
PV1.Spending _ -> P.error ()
155156
PV1.Rewarding stakingCredential ->
156-
if null $ P.filter ((stakingCredential P.==) . fst) $ PV1.txInfoWdrl txInfo
157+
if null $ PL.filter ((stakingCredential P.==) . fst) $ PV1.txInfoWdrl txInfo
157158
then P.error ()
158159
else ()
159160
PV1.Certifying dCert ->
160-
if null $ P.filter (dCert P.==) $ PV1.txInfoDCert txInfo
161+
if null $ PL.filter (dCert P.==) $ PV1.txInfoDCert txInfo
161162
then P.error ()
162163
else ()
163164
|]
@@ -174,7 +175,7 @@ purposeIsWellformedWithDatumQ =
174175
case unsafeFromBuiltinData context of
175176
-- Only spending scripts can have a Datum
176177
PV1.ScriptContext txInfo (PV1.Spending txOutRef) ->
177-
if null $ P.filter ((txOutRef P.==) . PV1.txInInfoOutRef) $ PV1.txInfoInputs txInfo
178+
if null $ PL.filter ((txOutRef P.==) . PV1.txInInfoOutRef) $ PV1.txInfoInputs txInfo
178179
then P.error ()
179180
else ()
180181
_ -> P.error ()
@@ -192,7 +193,7 @@ datumIsWellformedQ =
192193
case unsafeFromBuiltinData context of
193194
-- Only spending scripts can have a Datum
194195
PV1.ScriptContext txInfo (PV1.Spending _txOutRef) ->
195-
if null $ P.filter ((datum' P.==) . snd) $ PV1.txInfoData txInfo
196+
if null $ PL.filter ((datum' P.==) . snd) $ PV1.txInfoData txInfo
196197
then P.error ()
197198
else ()
198199
_ -> P.error ()

libs/plutus-preprocessor/src/Cardano/Ledger/Plutus/Preprocessor/Source/V2.hs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import qualified PlutusLedgerApi.V2 as PV2
77
import PlutusTx (fromBuiltinData, unsafeFromBuiltinData)
88
import qualified PlutusTx.AssocMap as PAM
99
import qualified PlutusTx.Builtins as P
10+
import qualified PlutusTx.List as PL
1011
import qualified PlutusTx.Prelude as P
1112

1213
alwaysSucceedsNoDatumQ :: Q [Dec]
@@ -157,7 +158,7 @@ purposeIsWellformedNoDatumQ =
157158
then ()
158159
else P.error ()
159160
PV2.Certifying dCert ->
160-
if null $ P.filter (dCert P.==) $ PV2.txInfoDCert txInfo
161+
if null $ PL.filter (dCert P.==) $ PV2.txInfoDCert txInfo
161162
then P.error ()
162163
else ()
163164
|]
@@ -174,7 +175,7 @@ purposeIsWellformedWithDatumQ =
174175
case unsafeFromBuiltinData context of
175176
-- Only spending scripts can have a Datum
176177
PV2.ScriptContext txInfo (PV2.Spending txOutRef) ->
177-
if null $ P.filter ((txOutRef P.==) . PV2.txInInfoOutRef) $ PV2.txInfoInputs txInfo
178+
if null $ PL.filter ((txOutRef P.==) . PV2.txInInfoOutRef) $ PV2.txInfoInputs txInfo
178179
then P.error ()
179180
else ()
180181
_ -> P.error ()
@@ -192,7 +193,7 @@ datumIsWellformedQ =
192193
case unsafeFromBuiltinData context of
193194
-- Only spending scripts can have a Datum
194195
PV2.ScriptContext txInfo (PV2.Spending _txOutRef) ->
195-
if null $ P.filter (datum' P.==) $ PAM.elems $ PV2.txInfoData txInfo
196+
if null $ PL.filter (datum' P.==) $ PAM.elems $ PV2.txInfoData txInfo
196197
then P.error ()
197198
else ()
198199
_ -> P.error ()

libs/plutus-preprocessor/src/Cardano/Ledger/Plutus/Preprocessor/Source/V3.hs

Lines changed: 59 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,24 @@
33
module Cardano.Ledger.Plutus.Preprocessor.Source.V3 where
44

55
import Language.Haskell.TH
6-
import qualified PlutusLedgerApi.V3 as PV3
6+
import qualified PlutusLedgerApi.Data.V3 as PV3D
77
import PlutusTx (fromBuiltinData, unsafeFromBuiltinData)
8-
import qualified PlutusTx.AssocMap as PAM
98
import qualified PlutusTx.Builtins as P
9+
import qualified PlutusTx.Data.AssocMap as PAMD
10+
import qualified PlutusTx.Data.List as PLD
1011
import qualified PlutusTx.Prelude as P
1112

1213
alwaysSucceedsNoDatumQ :: Q [Dec]
1314
alwaysSucceedsNoDatumQ =
1415
[d|
1516
alwaysSucceedsNoDatum :: P.BuiltinData -> P.BuiltinUnit
1617
alwaysSucceedsNoDatum arg =
17-
P.check $
18-
case unsafeFromBuiltinData arg of
19-
PV3.ScriptContext _txInfo (PV3.Redeemer _redeemer) scriptInfo ->
18+
let PV3D.ScriptContext _txInfo (PV3D.Redeemer _redeemer) scriptInfo =
19+
P.unsafeFromBuiltinData arg
20+
in P.check $
2021
case scriptInfo of
2122
-- We fail if this is a spending script with a Datum
22-
PV3.SpendingScript _ (Just _) -> False
23+
PV3D.SpendingScript _ (Just _) -> False
2324
_ -> True
2425
|]
2526

@@ -28,11 +29,13 @@ alwaysSucceedsWithDatumQ =
2829
[d|
2930
alwaysSucceedsWithDatum :: P.BuiltinData -> P.BuiltinUnit
3031
alwaysSucceedsWithDatum arg =
31-
P.check $
32-
case unsafeFromBuiltinData arg of
33-
-- Expecting a spending script with a Datum, thus failing when it is not
34-
PV3.ScriptContext _txInfo (PV3.Redeemer _redeemer) (PV3.SpendingScript _ (Just _)) -> True
35-
_ -> False
32+
let PV3D.ScriptContext _txInfo (PV3D.Redeemer _redeemer) scriptPurpose =
33+
P.unsafeFromBuiltinData arg
34+
in P.check $
35+
case scriptPurpose of
36+
PV3D.SpendingScript _ (Just _) -> True
37+
-- Expecting a spending script with a Datum, thus failing when it is not
38+
_ -> False
3639
|]
3740

3841
alwaysFailsNoDatumQ :: Q [Dec]
@@ -42,10 +45,10 @@ alwaysFailsNoDatumQ =
4245
alwaysFailsNoDatum arg =
4346
P.check $
4447
case fromBuiltinData arg of
45-
Just (PV3.ScriptContext _txInfo (PV3.Redeemer _redeemer) scriptInfo) ->
48+
Just (PV3D.ScriptContext _txInfo (PV3D.Redeemer _redeemer) scriptInfo) ->
4649
case scriptInfo of
4750
-- We fail only if this is not a spending script with a Datum
48-
PV3.SpendingScript _ (Just _) -> True
51+
PV3D.SpendingScript _ (Just _) -> True
4952
_ -> False
5053
Nothing -> True
5154
|]
@@ -57,10 +60,10 @@ alwaysFailsWithDatumQ =
5760
alwaysFailsWithDatum arg =
5861
P.check $
5962
case fromBuiltinData arg of
60-
Just (PV3.ScriptContext _txInfo (PV3.Redeemer _redeemer) scriptInfo) ->
63+
Just (PV3D.ScriptContext _txInfo (PV3D.Redeemer _redeemer) scriptInfo) ->
6164
case scriptInfo of
6265
-- We fail only if this is a spending script with a Datum
63-
PV3.SpendingScript _ (Just _) -> False
66+
PV3D.SpendingScript _ (Just _) -> False
6467
_ -> True
6568
Nothing -> True
6669
|]
@@ -72,9 +75,12 @@ redeemerSameAsDatumQ =
7275
redeemerSameAsDatum arg =
7376
P.check $
7477
case unsafeFromBuiltinData arg of
75-
PV3.ScriptContext _txInfo (PV3.Redeemer redeemer) (PV3.SpendingScript _ (Just (PV3.Datum datum))) ->
76-
-- Expecting a spending script with a Datum, thus failing when it is not
77-
datum P.== redeemer
78+
PV3D.ScriptContext
79+
_txInfo
80+
(PV3D.Redeemer redeemer)
81+
(PV3D.SpendingScript _ (Just (PV3D.Datum datum))) ->
82+
-- Expecting a spending script with a Datum, thus failing when it is not
83+
datum P.== redeemer
7884
_ -> False
7985
|]
8086

@@ -85,7 +91,7 @@ evenDatumQ =
8591
evenDatum arg =
8692
P.check $
8793
case unsafeFromBuiltinData arg of
88-
PV3.ScriptContext _txInfo _redeemer (PV3.SpendingScript _ (Just (PV3.Datum datum))) ->
94+
PV3D.ScriptContext _txInfo _redeemer (PV3D.SpendingScript _ (Just (PV3D.Datum datum))) ->
8995
-- Expecting a spending script with a Datum, thus failing when it is not
9096
P.modulo (P.unsafeDataAsI datum) 2 P.== 0
9197
|]
@@ -97,10 +103,10 @@ evenRedeemerNoDatumQ =
97103
evenRedeemerNoDatum arg =
98104
P.check $
99105
case unsafeFromBuiltinData arg of
100-
PV3.ScriptContext _txInfo (PV3.Redeemer redeemer) scriptInfo ->
106+
PV3D.ScriptContext _txInfo (PV3D.Redeemer redeemer) scriptInfo ->
101107
case scriptInfo of
102108
-- Expecting No Datum, therefore should fail when it is supplied
103-
PV3.SpendingScript _ (Just _) -> False
109+
PV3D.SpendingScript _ (Just _) -> False
104110
_ -> P.modulo (P.unsafeDataAsI redeemer) 2 P.== 0
105111
|]
106112

@@ -111,7 +117,7 @@ evenRedeemerWithDatumQ =
111117
evenRedeemerWithDatum arg =
112118
P.check $
113119
case unsafeFromBuiltinData arg of
114-
PV3.ScriptContext _txInfo (PV3.Redeemer redeemer) (PV3.SpendingScript _ (Just _)) ->
120+
PV3D.ScriptContext _txInfo (PV3D.Redeemer redeemer) (PV3D.SpendingScript _ (Just _)) ->
115121
-- Expecting a spending script with a Datum, thus failing when it is not
116122
P.modulo (P.unsafeDataAsI redeemer) 2 P.== 0
117123
_ -> False
@@ -124,23 +130,31 @@ purposeIsWellformedNoDatumQ =
124130
purposeIsWellformedNoDatum arg =
125131
P.check $
126132
case unsafeFromBuiltinData arg of
127-
PV3.ScriptContext txInfo _redeemer scriptInfo ->
128-
case scriptInfo of
129-
PV3.MintingScript cs ->
130-
PAM.member cs $ PV3.getValue . PV3.mintValueMinted $ PV3.txInfoMint txInfo
133+
PV3D.ScriptContext
134+
PV3D.TxInfo
135+
{ PV3D.txInfoMint = infoMint
136+
, PV3D.txInfoInputs = infoInputs
137+
, PV3D.txInfoWdrl = infoWdrl
138+
, PV3D.txInfoTxCerts = infoTxCerts
139+
, PV3D.txInfoVotes = infoVotes
140+
}
141+
_redeemer
142+
scriptInfo -> case scriptInfo of
143+
PV3D.MintingScript cs ->
144+
PAMD.member cs $ PV3D.getValue $ PV3D.mintValueMinted infoMint
131145
-- Expecting No Datum, therefore should fail when it is supplied
132-
PV3.SpendingScript txOutRef mDatum ->
146+
PV3D.SpendingScript txOutRef mDatum ->
133147
case mDatum of
134148
Just _ -> False
135149
Nothing ->
136-
null $ P.filter ((txOutRef P.==) . PV3.txInInfoOutRef) $ PV3.txInfoInputs txInfo
137-
PV3.RewardingScript stakingCredential ->
138-
PAM.member stakingCredential $ PV3.txInfoWdrl txInfo
139-
PV3.CertifyingScript _idx txCert ->
140-
null $ P.filter (txCert P.==) $ PV3.txInfoTxCerts txInfo
141-
PV3.VotingScript voter ->
142-
PAM.member voter $ PV3.txInfoVotes txInfo
143-
PV3.ProposingScript _idx _propProc -> True
150+
PLD.null $ PLD.filter ((txOutRef P.==) . PV3D.txInInfoOutRef) infoInputs
151+
PV3D.RewardingScript cred ->
152+
PAMD.member cred infoWdrl
153+
PV3D.CertifyingScript _idx txCert ->
154+
PLD.null $ PLD.filter (txCert P.==) infoTxCerts
155+
PV3D.VotingScript voter ->
156+
PAMD.member voter infoVotes
157+
PV3D.ProposingScript _idx _propProc -> True
144158
-- Eq instance for proposals have been removed. I am not sure if this is a
145159
-- good idea, but it only affects ledger test script, so we'll deal with
146160
-- this later:
@@ -155,8 +169,8 @@ purposeIsWellformedWithDatumQ =
155169
purposeIsWellformedWithDatum arg =
156170
P.check $
157171
case unsafeFromBuiltinData arg of
158-
PV3.ScriptContext txInfo _redeemer (PV3.SpendingScript txOutRef (Just _)) ->
159-
not $ null $ P.filter ((txOutRef P.==) . PV3.txInInfoOutRef) $ PV3.txInfoInputs txInfo
172+
PV3D.ScriptContext txInfo _redeemer (PV3D.SpendingScript txOutRef (Just _)) ->
173+
not $ PLD.null $ PLD.filter ((txOutRef P.==) . PV3D.txInInfoOutRef) $ PV3D.txInfoInputs txInfo
160174
_ -> False
161175
|]
162176

@@ -167,8 +181,8 @@ datumIsWellformedQ =
167181
datumIsWellformed arg =
168182
P.check $
169183
case unsafeFromBuiltinData arg of
170-
PV3.ScriptContext txInfo _redeemer (PV3.SpendingScript _txOutRef (Just datum)) ->
171-
not $ null $ P.filter (datum P.==) $ PAM.elems $ PV3.txInfoData txInfo
184+
PV3D.ScriptContext txInfo _redeemer (PV3D.SpendingScript _txOutRef (Just datum)) ->
185+
not $ PLD.null $ PLD.filter (datum P.==) $ PAMD.elems $ PV3D.txInfoData txInfo
172186
_ -> False
173187
|]
174188

@@ -180,9 +194,9 @@ inputsOutputsAreNotEmptyNoDatumQ =
180194
P.check $
181195
case unsafeFromBuiltinData arg of
182196
-- When there is a datum supplied, we need to fail.
183-
PV3.ScriptContext _txInfo _redeemer (PV3.SpendingScript _txOutRef (Just _)) -> False
184-
PV3.ScriptContext txInfo _redeemer _scriptPurpose ->
185-
not $ null (PV3.txInfoInputs txInfo) || null (PV3.txInfoOutputs txInfo)
197+
PV3D.ScriptContext _txInfo _redeemer (PV3D.SpendingScript _txOutRef (Just _)) -> False
198+
PV3D.ScriptContext txInfo _redeemer _scriptPurpose ->
199+
not $ PLD.null (PV3D.txInfoInputs txInfo) || PLD.null (PV3D.txInfoOutputs txInfo)
186200
|]
187201

188202
inputsOutputsAreNotEmptyWithDatumQ :: Q [Dec]
@@ -192,7 +206,7 @@ inputsOutputsAreNotEmptyWithDatumQ =
192206
inputsOutputsAreNotEmptyWithDatum arg =
193207
P.check $
194208
case unsafeFromBuiltinData arg of
195-
PV3.ScriptContext _txInfo _redeemer (PV3.SpendingScript _txOutRef Nothing) -> False
196-
PV3.ScriptContext txInfo _redeemer _scriptPurpose ->
197-
not $ null (PV3.txInfoInputs txInfo) || null (PV3.txInfoOutputs txInfo)
209+
PV3D.ScriptContext _txInfo _redeemer (PV3D.SpendingScript _txOutRef Nothing) -> False
210+
PV3D.ScriptContext txInfo _redeemer _scriptPurpose ->
211+
not $ PLD.null (PV3D.txInfoInputs txInfo) || PLD.null (PV3D.txInfoOutputs txInfo)
198212
|]

0 commit comments

Comments
 (0)