Skip to content

Commit c29e553

Browse files
colll78Unisay
authored andcommitted
feat: native builtin data vs th builtin data benchmark
1 parent ea541ef commit c29e553

5 files changed

Lines changed: 471 additions & 7 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module Main (main) where
2+
3+
import Data.Text qualified as Text
4+
import LinearVesting.TestOptimized (validatorOptimizedCodeFullyApplied)
5+
import PlutusTx.Test (displayEvalResult, evaluateCompiledCode)
6+
7+
main :: IO ()
8+
main = do
9+
putStrLn ""
10+
putStrLn $
11+
Text.unpack $
12+
displayEvalResult $
13+
evaluateCompiledCode validatorOptimizedCodeFullyApplied
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{-# LANGUAGE NoImplicitPrelude #-}
2+
3+
module LinearVesting.TestOptimized where
4+
5+
import LinearVesting.Test (testScriptContext)
6+
import LinearVesting.ValidatorOptimized (validatorOptimizedCode)
7+
import PlutusTx
8+
import PlutusTx.Prelude
9+
10+
validatorOptimizedCodeFullyApplied :: CompiledCode BuiltinUnit
11+
validatorOptimizedCodeFullyApplied =
12+
validatorOptimizedCode `unsafeApplyCode` liftCodeDef (toBuiltinData testScriptContext)

plutus-benchmark/linear-vesting/src/LinearVesting/Validator.hs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
module LinearVesting.Validator where
3030

3131
import PlutusTx
32+
import PlutusTx.Builtins.Internal qualified as BI
3233
import PlutusTx.Prelude
3334
import Prelude qualified as Haskell
3435

@@ -38,6 +39,8 @@ import PlutusLedgerApi.V3.Data.Contexts (txSignedBy)
3839
import PlutusTx.Data.List (List)
3940
import PlutusTx.Data.List qualified as List
4041

42+
{-# ANN module ("onchain-contract" :: Haskell.String) #-}
43+
4144
data VestingDatum = VestingDatum
4245
{ beneficiary :: Address
4346
, vestingAsset :: AssetClass
@@ -139,20 +142,37 @@ validateVestingFullUnlock ctx =
139142
vestingDatum :: VestingDatum = unsafeFromBuiltinData datum
140143
PubKeyCredential beneficiaryKey = addressCredential (beneficiary vestingDatum)
141144
in
142-
if
143-
| not (txSignedBy txInfo beneficiaryKey) ->
144-
traceError "Missing beneficiary signature"
145-
| vestingPeriodEnd vestingDatum >= currentTimeApproximation ->
146-
traceError "Unlock not permitted until vestingPeriodEnd time"
147-
| otherwise ->
148-
True
145+
BI.ifThenElse
146+
(not (txSignedBy txInfo beneficiaryKey))
147+
(\_ -> traceError "Missing beneficiary signature")
148+
( \_ ->
149+
BI.ifThenElse
150+
(vestingPeriodEnd vestingDatum >= currentTimeApproximation)
151+
(\_ -> traceError "Unlock not permitted until vestingPeriodEnd time")
152+
(\_ -> True)
153+
BI.unitval
154+
)
155+
BI.unitval
149156

150157
getLowerInclusiveTimeRange :: POSIXTimeRange -> POSIXTime
151158
getLowerInclusiveTimeRange = \case
152159
Interval (LowerBound (Finite posixTime) inclusive) _upperBound ->
153160
if inclusive then posixTime else posixTime + 1
154161
_ -> traceError "Time range not Finite"
155162

163+
-- Evaluation was SUCCESSFUL, result is:
164+
-- ()
165+
166+
-- Execution budget spent:
167+
-- CPU 30,837,131
168+
-- MEM 131,619
169+
170+
-- Evaluation traces:
171+
-- 1. Parsing ScriptContext...
172+
-- 2. Parsed ScriptContext
173+
-- 3. Parsed Redeemer
174+
-- 4. Full unlock requested
175+
-- 5. Validation completed
156176
{-# INLINEABLE typedValidator #-}
157177
typedValidator :: ScriptContext -> Bool
158178
typedValidator context =

0 commit comments

Comments
 (0)