Skip to content

Commit

Permalink
Merge pull request #5889 from IntersectMBO/bench-master
Browse files Browse the repository at this point in the history
bench: implement CIP-69 and CIP-117 for benchmarking
  • Loading branch information
disassembler authored Jun 27, 2024
2 parents 06943b6 + 3a164a3 commit 8874c19
Show file tree
Hide file tree
Showing 10 changed files with 701 additions and 669 deletions.
3 changes: 1 addition & 2 deletions bench/plutus-scripts-bench/plutus-scripts-bench.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 3.0
name: plutus-scripts-bench
version: 1.0.3.0
version: 1.0.4.0
synopsis: Plutus scripts used for benchmarking
description: Plutus scripts used for benchmarking.
category: Cardano,
Expand Down Expand Up @@ -91,7 +91,6 @@ library
build-depends:
, filepath
, bytestring
-- , serialise
, template-haskell
, text

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ import qualified PlutusLedgerApi.V3 as PlutusV3
import Prelude as Haskell (String, (.), (<$>))

import qualified Data.ByteString.Short as SBS
import GHC.ByteOrder (ByteOrder(LittleEndian))
import GHC.ByteOrder (ByteOrder (LittleEndian))

import Language.Haskell.TH
import Language.Haskell.TH.Syntax
import qualified PlutusTx
import qualified PlutusTx.Builtins.Internal as BI (unitval)
import qualified PlutusTx.Builtins.Internal as BI (BuiltinList, head, snd, tail, unitval,
unsafeDataAsConstr)
import PlutusTx.Prelude as Tx hiding (Semigroup (..), (.), (<$>))


Expand All @@ -37,15 +38,26 @@ script :: PlutusBenchScript
script = mkPlutusBenchScript scriptName (toScriptInAnyLang (PlutusScript PlutusScriptV3 scriptSerialized))

{-# INLINABLE mkValidator #-}
mkValidator :: BuiltinData -> BuiltinData -> BuiltinData -> BuiltinUnit
mkValidator _datum red _txContext =
case PlutusV3.fromBuiltinData red of
Nothing -> Tx.traceError "invalid redeemer"
Just (n, l) ->
if n < (1000000 :: Integer) -- large number ensures same bitsize for all counter values
then traceError "redeemer is < 1000000"
else loop n l
mkValidator :: BuiltinData -> BuiltinUnit
mkValidator arg =
if red_n < 1000000 -- large number ensures same bitsize for all counter values
then traceError "redeemer is < 1000000"
else loop red_n red_l
where
-- lazily decode script context up to redeemer, which is less expensive and results in much smaller tx size
constrArgs :: BuiltinData -> BI.BuiltinList BuiltinData
constrArgs = BI.snd . BI.unsafeDataAsConstr

redeemerFollowedByScriptInfo :: BI.BuiltinList BuiltinData
redeemerFollowedByScriptInfo = BI.tail (constrArgs arg)

redeemer :: BuiltinData
redeemer = BI.head redeemerFollowedByScriptInfo

red_n :: Integer
red_l :: [BuiltinByteString]
(red_n, red_l) = PlutusV3.unsafeFromBuiltinData redeemer

hashAndAddG2 :: [BuiltinByteString] -> Integer -> BuiltinBLS12_381_G2_Element
hashAndAddG2 l i =
go l (Tx.bls12_381_G2_uncompress Tx.bls12_381_G2_compressed_zero)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ import qualified Data.ByteString.Short as SBS
import qualified PlutusLedgerApi.V3 as PlutusV3
import PlutusTx
import PlutusTx.Builtins (unsafeDataAsI)
import qualified PlutusTx.Builtins.Internal as BI (unitval)
import PlutusTx.Prelude hiding (Semigroup (..), unless, (.), (<$>))
import qualified PlutusTx.Builtins.Internal as BI (BuiltinList, head, snd, tail, unitval,
unsafeDataAsConstr)
import PlutusTx.Prelude as Plutus hiding (Semigroup (..), unless, (.), (<$>))


scriptName :: String
Expand All @@ -31,14 +32,25 @@ script = mkPlutusBenchScript scriptName (toScriptInAnyLang (PlutusScript PlutusS


{-# INLINABLE mkValidator #-}
mkValidator :: BuiltinData -> BuiltinData -> BuiltinData -> BuiltinUnit
mkValidator _datum redeemer _txContext
= if n < 1000000
then traceError "redeemer is < 1000000"
else loop n
mkValidator :: BuiltinData -> BuiltinUnit
mkValidator arg =
if red_n < 1000000
then traceError "redeemer is < 1000000" -- large number ensures same bitsize for all counter values
else loop red_n
where
n = unsafeDataAsI redeemer
loop i = if i == 1000000 then BI.unitval else loop $ pred i
-- lazily decode script context up to redeemer, which is less expensive and results in much smaller tx size
constrArgs :: BuiltinData -> BI.BuiltinList BuiltinData
constrArgs = BI.snd . BI.unsafeDataAsConstr

redeemerFollowedByScriptInfo :: BI.BuiltinList BuiltinData
redeemerFollowedByScriptInfo = BI.tail (constrArgs arg)

redeemer :: BuiltinData
redeemer = BI.head redeemerFollowedByScriptInfo

red_n = unsafeDataAsI redeemer

loop i = if i == 1000000 then BI.unitval else loop (pred i)

loopScriptShortBs :: SBS.ShortByteString
loopScriptShortBs = PlutusV3.serialiseCompiledCode $$(PlutusTx.compile [|| mkValidator ||])
Expand Down
Loading

0 comments on commit 8874c19

Please sign in to comment.