Skip to content

Commit

Permalink
PlutusV3: Datum/Redeemer in ScriptContext, Datum Optional, Unified Sc…
Browse files Browse the repository at this point in the history
…ript Type (#5934)
  • Loading branch information
zliu41 authored May 30, 2024
1 parent e675e65 commit 7b3fd3e
Show file tree
Hide file tree
Showing 17 changed files with 435 additions and 195 deletions.
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,12 @@ 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)
(Redeemer (PlutusTx.toBuiltinData (1 :: Integer)))
(SpendingScript (TxOutRef (TxId "") 0) Nothing)


mkTxInfo :: Int -> TxInfo
mkTxInfo i = TxInfo {
Expand Down Expand Up @@ -62,7 +67,7 @@ checkScriptContext1 d =
-- Bang pattern to ensure this is forced, probably not necesssary
-- since we do use it later
let !sc = PlutusTx.unsafeFromBuiltinData d
(ScriptContext txi _) = sc
ScriptContext txi _ _ = sc
in
if PlutusTx.length (txInfoOutputs txi) `PlutusTx.modInteger` 2 PlutusTx.== 0
then ()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
({cpu: 285912869
| mem: 1059503})
({cpu: 287479352
| mem: 1064729})
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
({cpu: 84686453
| mem: 324879})
({cpu: 86252936
| mem: 330105})
237 changes: 160 additions & 77 deletions plutus-benchmark/script-contexts/test/9.6/checkScriptContext1.pir.golden
Original file line number Diff line number Diff line change
Expand Up @@ -803,77 +803,6 @@
(unIData (headList {data} (tailList {data} args))))
(/\dead -> traceError {TxOutRef} "PT1")
{all dead. dead}
data ScriptPurpose | ScriptPurpose_match where
Certifying : integer -> TxCert -> ScriptPurpose
Minting : bytestring -> ScriptPurpose
Proposing : integer -> ProposalProcedure -> ScriptPurpose
Rewarding : Credential -> ScriptPurpose
Spending : TxOutRef -> ScriptPurpose
Voting : Voter -> ScriptPurpose
!`$fUnsafeFromDataScriptContext_$cunsafeFromBuiltinData` :
data -> ScriptPurpose
= \(d : data) ->
let
!tup : pair integer (list data) = unConstrData d
!index : integer = fstPair {integer} {list data} tup
!args : list data = sndPair {integer} {list data} tup
in
ifThenElse
{all dead. ScriptPurpose}
(equalsInteger 0 index)
(/\dead -> Minting (unBData (headList {data} args)))
(/\dead ->
ifThenElse
{all dead. ScriptPurpose}
(equalsInteger 1 index)
(/\dead ->
Spending
(`$fUnsafeFromDataTxOutRef_$cunsafeFromBuiltinData`
(headList {data} args)))
(/\dead ->
ifThenElse
{all dead. ScriptPurpose}
(equalsInteger 2 index)
(/\dead ->
Rewarding
(`$fUnsafeFromDataCredential_$cunsafeFromBuiltinData`
(headList {data} args)))
(/\dead ->
ifThenElse
{all dead. ScriptPurpose}
(equalsInteger 3 index)
(/\dead ->
Certifying
(unIData (headList {data} args))
(`$fUnsafeFromDataScriptContext_$cunsafeFromBuiltinData`
(headList {data} (tailList {data} args))))
(/\dead ->
ifThenElse
{all dead. ScriptPurpose}
(equalsInteger 4 index)
(/\dead ->
Voting
(`$fUnsafeFromDataScriptContext_$cunsafeFromBuiltinData`
(headList {data} args)))
(/\dead ->
ifThenElse
{all dead. ScriptPurpose}
(equalsInteger 5 index)
(/\dead ->
Proposing
(unIData (headList {data} args))
(`$fUnsafeFromDataProposalProcedure_$cunsafeFromBuiltinData`
(headList
{data}
(tailList {data} args))))
(/\dead ->
traceError {ScriptPurpose} "PT1")
{all dead. dead})
{all dead. dead})
{all dead. dead})
{all dead. dead})
{all dead. dead})
{all dead. dead}
~`$fUnsafeFromDataValue` :
data -> (\k v -> List (Tuple2 k v)) bytestring integer
= `$fUnsafeFromDataMap_$cunsafeFromBuiltinData`
Expand Down Expand Up @@ -1007,12 +936,26 @@
(headList {data} (tailList {data} args))))
(/\dead -> traceError {TxInInfo} "PT1")
{all dead. dead}
data ScriptInfo | ScriptInfo_match where
CertifyingScript : integer -> TxCert -> ScriptInfo
MintingScript : bytestring -> ScriptInfo
ProposingScript : integer -> ProposalProcedure -> ScriptInfo
RewardingScript : Credential -> ScriptInfo
SpendingScript : TxOutRef -> Maybe data -> ScriptInfo
VotingScript : Voter -> ScriptInfo
data (LowerBound :: * -> *) a | LowerBound_match where
LowerBound : Extended a -> Bool -> LowerBound a
data (UpperBound :: * -> *) a | UpperBound_match where
UpperBound : Extended a -> Bool -> UpperBound a
data (Interval :: * -> *) a | Interval_match where
Interval : LowerBound a -> UpperBound a -> Interval a
data ScriptPurpose | ScriptPurpose_match where
Certifying : integer -> TxCert -> ScriptPurpose
Minting : bytestring -> ScriptPurpose
Proposing : integer -> ProposalProcedure -> ScriptPurpose
Rewarding : Credential -> ScriptPurpose
Spending : TxOutRef -> ScriptPurpose
Voting : Voter -> ScriptPurpose
data Vote | Vote_match where
Abstain : Vote
VoteNo : Vote
Expand Down Expand Up @@ -1041,7 +984,7 @@
Maybe integer ->
TxInfo
data ScriptContext | ScriptContext_match where
ScriptContext : TxInfo -> ScriptPurpose -> ScriptContext
ScriptContext : TxInfo -> data -> ScriptInfo -> ScriptContext
in
\(d : data) ->
ScriptContext_match
Expand All @@ -1054,6 +997,9 @@
{all dead. ScriptContext}
(equalsInteger 0 index)
(/\dead ->
let
!l : list data = tailList {data} args
in
ScriptContext
(let
!tup : pair integer (list data)
Expand Down Expand Up @@ -1177,7 +1123,78 @@
(`$fUnsafeFromDataMap_$cunsafeFromBuiltinData`
{ScriptPurpose}
{data}
`$fUnsafeFromDataScriptContext_$cunsafeFromBuiltinData`
(\(d : data) ->
let
!tup : pair integer (list data) = unConstrData d
!index : integer = fstPair {integer} {list data} tup
!args : list data
= sndPair {integer} {list data} tup
in
ifThenElse
{all dead. ScriptPurpose}
(equalsInteger 0 index)
(/\dead -> Minting (unBData (headList {data} args)))
(/\dead ->
ifThenElse
{all dead. ScriptPurpose}
(equalsInteger 1 index)
(/\dead ->
Spending
(`$fUnsafeFromDataTxOutRef_$cunsafeFromBuiltinData`
(headList {data} args)))
(/\dead ->
ifThenElse
{all dead. ScriptPurpose}
(equalsInteger 2 index)
(/\dead ->
Rewarding
(`$fUnsafeFromDataCredential_$cunsafeFromBuiltinData`
(headList {data} args)))
(/\dead ->
ifThenElse
{all dead. ScriptPurpose}
(equalsInteger 3 index)
(/\dead ->
Certifying
(unIData (headList {data} args))
(`$fUnsafeFromDataScriptContext_$cunsafeFromBuiltinData`
(headList
{data}
(tailList {data} args))))
(/\dead ->
ifThenElse
{all dead. ScriptPurpose}
(equalsInteger 4 index)
(/\dead ->
Voting
(`$fUnsafeFromDataScriptContext_$cunsafeFromBuiltinData`
(headList {data} args)))
(/\dead ->
ifThenElse
{all dead. ScriptPurpose}
(equalsInteger 5 index)
(/\dead ->
Proposing
(unIData
(headList
{data}
args))
(`$fUnsafeFromDataProposalProcedure_$cunsafeFromBuiltinData`
(headList
{data}
(tailList
{data}
args))))
(/\dead ->
traceError
{ScriptPurpose}
"PT1")
{all dead. dead})
{all dead. dead})
{all dead. dead})
{all dead. dead})
{all dead. dead})
{all dead. dead})
`$fUnsafeFromDataBuiltinData_$cunsafeFromBuiltinData`
(headList {data} l))
(`$fUnsafeFromDataMap_$cunsafeFromBuiltinData`
Expand Down Expand Up @@ -1236,12 +1253,77 @@
(headList {data} (tailList {data} l))))
(/\dead -> traceError {TxInfo} "PT1")
{all dead. dead})
(`$fUnsafeFromDataScriptContext_$cunsafeFromBuiltinData`
(headList {data} (tailList {data} args))))
(headList {data} l)
(let
!tup : pair integer (list data)
= unConstrData (headList {data} (tailList {data} l))
!index : integer = fstPair {integer} {list data} tup
!args : list data = sndPair {integer} {list data} tup
in
ifThenElse
{all dead. ScriptInfo}
(equalsInteger 0 index)
(/\dead -> MintingScript (unBData (headList {data} args)))
(/\dead ->
ifThenElse
{all dead. ScriptInfo}
(equalsInteger 1 index)
(/\dead ->
SpendingScript
(`$fUnsafeFromDataTxOutRef_$cunsafeFromBuiltinData`
(headList {data} args))
(`$fUnsafeFromDataMaybe_$cunsafeFromBuiltinData`
{data}
`$fUnsafeFromDataBuiltinData_$cunsafeFromBuiltinData`
(headList {data} (tailList {data} args))))
(/\dead ->
ifThenElse
{all dead. ScriptInfo}
(equalsInteger 2 index)
(/\dead ->
RewardingScript
(`$fUnsafeFromDataCredential_$cunsafeFromBuiltinData`
(headList {data} args)))
(/\dead ->
ifThenElse
{all dead. ScriptInfo}
(equalsInteger 3 index)
(/\dead ->
CertifyingScript
(unIData (headList {data} args))
(`$fUnsafeFromDataScriptContext_$cunsafeFromBuiltinData`
(headList {data} (tailList {data} args))))
(/\dead ->
ifThenElse
{all dead. ScriptInfo}
(equalsInteger 4 index)
(/\dead ->
VotingScript
(`$fUnsafeFromDataScriptContext_$cunsafeFromBuiltinData`
(headList {data} args)))
(/\dead ->
ifThenElse
{all dead. ScriptInfo}
(equalsInteger 5 index)
(/\dead ->
ProposingScript
(unIData (headList {data} args))
(`$fUnsafeFromDataProposalProcedure_$cunsafeFromBuiltinData`
(headList
{data}
(tailList {data} args))))
(/\dead ->
traceError {ScriptInfo} "PT1")
{all dead. dead})
{all dead. dead})
{all dead. dead})
{all dead. dead})
{all dead. dead})
{all dead. dead}))
(/\dead -> traceError {ScriptContext} "PT1")
{all dead. dead})
{Unit}
(\(ipv : TxInfo) (ipv : ScriptPurpose) ->
(\(ipv : TxInfo) (ipv : data) (ipv : ScriptInfo) ->
ifThenElse
{all dead. Unit}
(equalsInteger
Expand Down Expand Up @@ -1305,4 +1387,5 @@
, List []
, Constr 1 []
, Constr 1 [] ]
, Constr 1 [Constr 0 [B #, I 0]] ])
, I 1
, Constr 1 [Constr 0 [B #, I 0], Constr 1 []] ])
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2785
2961
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
({cpu: 275563740
| mem: 1009660})
({cpu: 277114223
| mem: 1014786})
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
({cpu: 81588652
| mem: 310268})
({cpu: 83139135
| mem: 315394})
Loading

1 comment on commit 7b3fd3e

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'Plutus Benchmarks'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.05.

Benchmark suite Current: 7b3fd3e Previous: e675e65 Ratio
validation-decode-escrow-redeem_1-1 332.1 μs 307.7 μs 1.08
validation-decode-game-sm-success_1-2 171.2 μs 162.4 μs 1.05
validation-decode-stablecoin_1-6 168.7 μs 158.4 μs 1.07
validation-decode-stablecoin_2-4 172.6 μs 159.1 μs 1.08

This comment was automatically generated by workflow using github-action-benchmark.

CC: @input-output-hk/plutus-core

Please sign in to comment.