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

PlutusV3: Datum/Redeemer in ScriptContext, Datum Optional, Unified Script Type #5934

Merged
merged 4 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ module PlutusBenchmark.ScriptContexts where

import PlutusLedgerApi.V1.Address
import PlutusLedgerApi.V1.Value
import PlutusLedgerApi.V3 (OutputDatum (NoOutputDatum), PubKeyHash (..), ScriptContext (..),
ScriptPurpose (Spending), TxId (..), TxInfo (..), TxOut (..),
TxOutRef (..), always)
import PlutusLedgerApi.V3 (OutputDatum (NoOutputDatum), PubKeyHash (..), Redeemer (..),
ScriptContext (..), ScriptInfo (SpendingScript), TxId (..), TxInfo (..),
TxOut (..), TxOutRef (..), always)
import PlutusTx qualified
import PlutusTx.AssocMap qualified as Map
import PlutusTx.Builtins qualified as PlutusTx
Expand All @@ -19,7 +19,15 @@ import PlutusTx.Prelude qualified as PlutusTx
-- | A very crude deterministic generator for 'ScriptContext's with size
-- approximately proportional to the input integer.
mkScriptContext :: Int -> ScriptContext
mkScriptContext i = ScriptContext (mkTxInfo i) (Spending (TxOutRef (TxId "") 0))
mkScriptContext i =
ScriptContext
(mkTxInfo i)
( SpendingScript
(TxOutRef (TxId "") 0)
Nothing
(Redeemer (PlutusTx.toBuiltinData (1 :: Integer)))
)


mkTxInfo :: Int -> TxInfo
mkTxInfo i = TxInfo {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
({cpu: 363764173
| mem: 1101803})
({cpu: 365548587
| mem: 1106929})
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
({cpu: 109608941
| mem: 338379})
({cpu: 111393355
| mem: 343505})
252 changes: 175 additions & 77 deletions plutus-benchmark/script-contexts/test/9.6/checkScriptContext1.pir.golden

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
2785
3001
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
({cpu: 347299684
| mem: 1051360})
({cpu: 349084098
| mem: 1056486})
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
({cpu: 104544084
| mem: 323168})
({cpu: 106328498
| mem: 328294})
257 changes: 181 additions & 76 deletions plutus-benchmark/script-contexts/test/9.6/checkScriptContext2.pir.golden

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
2725
2941
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
({cpu: 65025439
| mem: 214602})
({cpu: 65414713
| mem: 215802})
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
({cpu: 49036100
| mem: 213300})
({cpu: 49312100
| mem: 214500})
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
({cpu: 304395100
| mem: 1215868})
({cpu: 306426953
| mem: 1219470})
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
### Changed

- The `ScriptContext` type for Plutus V3 is modified to contain redeemer and an optional
datum for spending scripts, in the `scriptContextScriptInfo` field. As a result, all
Plutus V3 scripts, regardless of the purpose, take a single argument: `ScriptContext`.
- Datum is now optional for Plutus V3 spending scripts.
15 changes: 9 additions & 6 deletions plutus-ledger-api/src/PlutusLedgerApi/V3.hs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ module PlutusLedgerApi.V3 (
-- * Context types
ScriptContext (..),
ScriptPurpose (..),
ScriptInfo (..),

-- ** Supporting types used in the context types

Expand Down Expand Up @@ -192,10 +193,11 @@ evaluateScriptCounting ::
EvaluationContext ->
-- | The script to evaluate
ScriptForEvaluation ->
-- | The arguments to the script
[PLC.Data] ->
-- | The @ScriptContext@ argument to the script
PLC.Data ->
(LogOutput, Either EvaluationError ExBudget)
evaluateScriptCounting = Common.evaluateScriptCounting thisLedgerLanguage
evaluateScriptCounting mpv verbose ec s arg =
Common.evaluateScriptCounting thisLedgerLanguage mpv verbose ec s [arg]

{- | Evaluates a script, with a cost model and a budget that restricts how many
resources it can use according to the cost model. Also returns the budget that
Expand All @@ -215,7 +217,8 @@ evaluateScriptRestricting ::
ExBudget ->
-- | The script to evaluate
ScriptForEvaluation ->
-- | The arguments to the script
[PLC.Data] ->
-- | The @ScriptContext@ argument to the script
PLC.Data ->
(LogOutput, Either EvaluationError ExBudget)
evaluateScriptRestricting = Common.evaluateScriptRestricting thisLedgerLanguage
evaluateScriptRestricting mpv verbose ec budget s arg =
Common.evaluateScriptRestricting thisLedgerLanguage mpv verbose ec budget s [arg]
59 changes: 54 additions & 5 deletions plutus-ledger-api/src/PlutusLedgerApi/V3/Contexts.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module PlutusLedgerApi.V3.Contexts
, GovernanceAction (..)
, ProposalProcedure (..)
, ScriptPurpose (..)
, ScriptInfo (..)
, TxInInfo (..)
, TxInfo (..)
, ScriptContext (..)
Expand Down Expand Up @@ -366,6 +367,7 @@ instance PlutusTx.Eq ProposalProcedure where
PlutusTx.&& b PlutusTx.== b'
PlutusTx.&& c PlutusTx.== c'

-- | A `ScriptPurpose` uniquely identifies a Plutus script within a transaction.
data ScriptPurpose
= Minting V2.CurrencySymbol
| Spending V3.TxOutRef
Expand Down Expand Up @@ -398,6 +400,42 @@ instance PlutusTx.Eq ScriptPurpose where
a PlutusTx.== a' PlutusTx.&& b PlutusTx.== b'
_ == _ = Haskell.False

-- | Like `ScriptPurpose` but also contains redeemers, and an optional datum
-- for spending scripts.
data ScriptInfo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a suggestion about an alternative name. Info isn't very informative IMHO. Feel free to ignore this suggestion if you like the ScriptInfo name better.

Suggested change
data ScriptInfo
data ScriptPurposeArgument

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There have been quite a few similar names mentioned 😄 I'll think about it

= MintingScript V2.CurrencySymbol V2.Redeemer
zliu41 marked this conversation as resolved.
Show resolved Hide resolved
| SpendingScript V3.TxOutRef (Haskell.Maybe V2.Datum) V2.Redeemer
| RewardingScript V2.Credential V2.Redeemer
| CertifyingScript
Haskell.Integer
-- ^ 0-based index of the given `TxCert` in `txInfoTxCerts`
TxCert
V2.Redeemer
| VotingScript Voter V2.Redeemer
| ProposingScript
Haskell.Integer
-- ^ 0-based index of the given `ProposalProcedure` in `txInfoProposalProcedures`
ProposalProcedure
V2.Redeemer
deriving stock (Generic, Haskell.Show, Haskell.Eq)
deriving (Pretty) via (PrettyShow ScriptInfo)

instance PlutusTx.Eq ScriptInfo where
{-# INLINEABLE (==) #-}
MintingScript a b == MintingScript a' b' =
a PlutusTx.== a' PlutusTx.&& b PlutusTx.== b'
SpendingScript a b c == SpendingScript a' b' c' =
a PlutusTx.== a' PlutusTx.&& b PlutusTx.== b' PlutusTx.&& c PlutusTx.== c'
RewardingScript a b == RewardingScript a' b' =
a PlutusTx.== a' PlutusTx.&& b PlutusTx.== b'
CertifyingScript a b c == CertifyingScript a' b' c' =
a PlutusTx.== a' PlutusTx.&& b PlutusTx.== b' PlutusTx.&& c PlutusTx.== c'
VotingScript a b == VotingScript a' b' =
a PlutusTx.== a' PlutusTx.&& b PlutusTx.== b'
ProposingScript a b c == ProposingScript a' b' c' =
a PlutusTx.== a' PlutusTx.&& b PlutusTx.== b' PlutusTx.&& c PlutusTx.== c'
_ == _ = Haskell.False

-- | An input of a pending transaction.
data TxInInfo = TxInInfo
{ txInInfoOutRef :: V3.TxOutRef
Expand Down Expand Up @@ -482,17 +520,17 @@ instance PlutusTx.Eq TxInfo where

-- | The context that the currently-executing script can access.
data ScriptContext = ScriptContext
{ scriptContextTxInfo :: TxInfo
{ scriptContextTxInfo :: TxInfo
-- ^ information about the transaction the currently-executing script is included in
, scriptContextPurpose :: ScriptPurpose
, scriptContextScriptInfo :: ScriptInfo
-- ^ the purpose of the currently-executing script
}
deriving stock (Generic, Haskell.Eq, Haskell.Show)

instance Pretty ScriptContext where
pretty ScriptContext{..} =
vsep
[ "Purpose:" <+> pretty scriptContextPurpose
[ "ScriptInfo:" <+> pretty scriptContextScriptInfo
, nest 2 (vsep ["TxInfo:", pretty scriptContextTxInfo])
]

Expand All @@ -508,7 +546,7 @@ findOwnInput :: ScriptContext -> Haskell.Maybe TxInInfo
findOwnInput
ScriptContext
{ scriptContextTxInfo = TxInfo{txInfoInputs}
, scriptContextPurpose = Spending txOutRef
, scriptContextScriptInfo = SpendingScript txOutRef _ _
} =
PlutusTx.find
(\TxInInfo{txInInfoOutRef} -> txInInfoOutRef PlutusTx.== txOutRef)
Expand Down Expand Up @@ -616,7 +654,7 @@ valueProduced = PlutusTx.foldMap V2.txOutValue PlutusTx.. txInfoOutputs

-- | The 'CurrencySymbol' of the current validator script.
ownCurrencySymbol :: ScriptContext -> V2.CurrencySymbol
ownCurrencySymbol ScriptContext{scriptContextPurpose = Minting cs} = cs
ownCurrencySymbol ScriptContext{scriptContextScriptInfo = MintingScript cs _} = cs
ownCurrencySymbol _ =
-- "Can't get currency symbol of the current validator script"
PlutusTx.traceError "Lh"
Expand Down Expand Up @@ -726,6 +764,17 @@ PlutusTx.makeIsDataIndexed
, ('Proposing, 5)
]

PlutusTx.makeLift ''ScriptInfo
PlutusTx.makeIsDataIndexed
''ScriptInfo
[ ('MintingScript, 0)
, ('SpendingScript, 1)
, ('RewardingScript, 2)
, ('CertifyingScript, 3)
, ('VotingScript, 4)
, ('ProposingScript, 5)
]

PlutusTx.makeLift ''TxInInfo
PlutusTx.makeIsDataIndexed ''TxInInfo [('TxInInfo, 0)]

Expand Down
2 changes: 1 addition & 1 deletion plutus-ledger-api/test/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ availableBuiltins = testCase "builtins are available after Alonzo" $
integerToByteStringExceedsBudget :: TestTree
integerToByteStringExceedsBudget = testCase "integerToByteString should exceed budget" $
let script = either (error . show) id $ V3.deserialiseScript conwayPV integerToByteStringFunction
(_, res) = V3.evaluateScriptCounting conwayPV V3.Quiet v3_evalCtxTooFewParams script []
(_, res) = V3.evaluateScriptCounting conwayPV V3.Quiet v3_evalCtxTooFewParams script (I 1)
in case res of
Left _ -> assertFailure "fails"
Right (ExBudget cpu _mem) -> assertBool "did not exceed budget" (cpu >= fromIntegral (maxBound :: Int64))
Expand Down
13 changes: 7 additions & 6 deletions plutus-ledger-api/testlib/PlutusLedgerApi/Test/Examples.hs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ integerToByteStringFunction :: SerialisedScript
integerToByteStringFunction = serialiseUPLC $ UPLC.Program () PLC.plcVersion110 body
where
body =
PLC.mkIterAppNoAnn
(UPLC.Builtin () PLC.IntegerToByteString)
[ PLC.mkConstant @Bool () False
, PLC.mkConstant @Integer () 5
, PLC.mkConstant @Integer () 1
]
UPLC.LamAbs () (UPLC.DeBruijn 0) $
PLC.mkIterAppNoAnn
(UPLC.Builtin () PLC.IntegerToByteString)
[ PLC.mkConstant @Bool () False
, PLC.mkConstant @Integer () 5
, PLC.mkConstant @Integer () 1
]