Skip to content

Commit

Permalink
Fix benchmarking to work with HashableScriptData
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert 'Probie' Offner committed Oct 31, 2022
1 parent b2ca75d commit c598dea
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 20 deletions.
6 changes: 3 additions & 3 deletions bench/tx-generator/src/Cardano/Benchmarking/Compiler.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type DstWallet = WalletName
compileOptions :: NixServiceOptions -> Either CompileError [Action]
compileOptions opts = runCompiler opts compileToScript

runCompiler ::NixServiceOptions -> Compiler () -> Either CompileError [Action]
runCompiler :: NixServiceOptions -> Compiler () -> Either CompileError [Action]
runCompiler o c = case runExcept $ runRWST c o 0 of
Left err -> Left err
Right ((), _ , l) -> Right $ DL.toList l
Expand Down Expand Up @@ -139,8 +139,8 @@ splittingPhase srcWallet = do
executionUnits <- ExecutionUnits <$> askNixOption _nix_executionMemory <*> askNixOption _nix_executionSteps
debugMode <- askNixOption _nix_debugMode
budget <- (if debugMode then CheckScriptBudget else StaticScriptBudget)
<$> (ScriptDataNumber <$> askNixOption _nix_plutusData)
<*> (ScriptDataNumber <$> askNixOption _nix_plutusRedeemer)
<$> (unsafeScriptDataToHashable . ScriptDataNumber <$> askNixOption _nix_plutusData)
<*> (unsafeScriptDataToHashable . ScriptDataNumber <$> askNixOption _nix_plutusRedeemer)
<*> pure executionUnits
ScriptSpec <$> askNixOption _nix_plutusScript <*> pure budget
return $ PayToScript scriptSpec dst
Expand Down
4 changes: 2 additions & 2 deletions bench/tx-generator/src/Cardano/Benchmarking/PlutusExample.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ toScriptHash str =
preExecuteScript ::
ProtocolParameters
-> Script PlutusScriptV1
-> ScriptData
-> ScriptData
-> HashableScriptData
-> HashableScriptData
-> Either String ExecutionUnits
preExecuteScript protocolParameters (PlutusScript _ (PlutusScriptSerialised script)) datum redeemer = do
costModel <- case Map.lookup (AnyPlutusScriptVersion PlutusScriptV1) (protocolParamCostModels protocolParameters) of
Expand Down
11 changes: 8 additions & 3 deletions bench/tx-generator/src/Cardano/Benchmarking/Script/Aeson.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import Data.Aeson.Encode.Pretty
import qualified Data.Attoparsec.ByteString as Atto

import qualified Ouroboros.Network.Magic as Ouroboros (NetworkMagic(..))
import Cardano.Api (ScriptData, ScriptDataJsonSchema(..), NetworkId(..)
, scriptDataFromJson, scriptDataToJson)
import Cardano.Api (ScriptData, ScriptDataJsonSchema(..), NetworkId(..), SerialiseAsCBOR
, scriptDataFromJson, scriptDataToJson, WithCBOR, withoutCBOR, withCBORViaRoundtrip)
import Cardano.Api.Shelley (ProtocolParameters)
import Cardano.CLI.Types (SigningKeyFile(..))

Expand Down Expand Up @@ -54,11 +54,16 @@ instance FromJSON ProtocolParametersSource where
parseJSON = genericParseJSON jsonOptionsUnTaggedSum

-- Orphan instance used in the tx-generator
instance ToJSON a => ToJSON (WithCBOR a) where
toJSON = toJSON . withoutCBOR
instance (SerialiseAsCBOR a, FromJSON a) => FromJSON (WithCBOR a) where
parseJSON = fmap withCBORViaRoundtrip . parseJSON

instance ToJSON ScriptData where
toJSON = scriptDataToJson ScriptDataJsonNoSchema
instance FromJSON ScriptData where
parseJSON v = case scriptDataFromJson ScriptDataJsonNoSchema v of
Right r -> return r
Right r -> return $ withoutCBOR r
Left err -> fail $ show err

instance ToJSON Generator where
Expand Down
14 changes: 7 additions & 7 deletions bench/tx-generator/src/Cardano/Benchmarking/Script/Core.hs
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ spendAutoScript relies on a particular calling convention of the loop script.
spendAutoScript ::
ProtocolParameters
-> Script PlutusScriptV1
-> ActionM (ScriptData, ScriptRedeemer)
-> ActionM (HashableScriptData, ScriptRedeemer)
spendAutoScript protocolParameters script = do
perTxBudget <- case protocolParamMaxTxExUnits protocolParameters of
Nothing -> throwE $ ApiError "Cannot determine protocolParamMaxTxExUnits"
Expand All @@ -407,18 +407,18 @@ spendAutoScript protocolParameters script = do

let
isInLimits :: Integer -> Either String Bool
isInLimits n = case preExecuteScript protocolParameters script (ScriptDataNumber 0) (toLoopArgument n) of
isInLimits n = case preExecuteScript protocolParameters script (unsafeScriptDataToHashable $ ScriptDataNumber 0) (toLoopArgument n) of
Left err -> Left err
Right use -> Right $ (executionSteps use <= executionSteps budget) && (executionMemory use <= executionMemory budget)
searchUpperBound = 100000 -- The highest loop count that is tried. (This is about 50 times the current mainnet limit.)
redeemer <- case startSearch isInLimits 0 searchUpperBound of
Left err -> throwE $ ApiError $ "cannot find fitting redeemer :" ++ err
Right n -> return $ toLoopArgument n
return (ScriptDataNumber 0, redeemer)
return (unsafeScriptDataToHashable (ScriptDataNumber 0), redeemer)
where
-- This is the hardcoded calling convention of the loop.plutus script.
-- To loop n times one has to pass n + 1_000_000 as redeemer.
toLoopArgument n = ScriptDataNumber $ n + 1000000
toLoopArgument n = unsafeScriptDataToHashable $ ScriptDataNumber $ n + 1000000
startSearch f a b = do
l <- f a
h <- f b
Expand All @@ -433,7 +433,7 @@ spendAutoScript protocolParameters script = do

makePlutusContext :: forall era. IsShelleyBasedEra era
=> ScriptSpec
-> ActionM (Witness WitCtxTxIn era, Script PlutusScriptV1, ScriptData, Lovelace)
-> ActionM (Witness WitCtxTxIn era, Script PlutusScriptV1, HashableScriptData, Lovelace)
makePlutusContext scriptSpec = do
protocolParameters <- getProtocolParameters
script <- liftIO $ PlutusExample.readScript $ scriptSpecFile scriptSpec
Expand Down Expand Up @@ -498,8 +498,8 @@ makePlutusContext scriptSpec = do
preExecuteScriptAction ::
ProtocolParameters
-> Script PlutusScriptV1
-> ScriptData
-> ScriptData
-> HashableScriptData
-> HashableScriptData
-> ActionM ExecutionUnits
preExecuteScriptAction protocolParameters script scriptData redeemer
= case preExecuteScript protocolParameters script scriptData redeemer of
Expand Down
8 changes: 4 additions & 4 deletions bench/tx-generator/src/Cardano/Benchmarking/Script/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import Prelude
import Data.List.NonEmpty
import Data.Text (Text)

import Cardano.Api (AnyCardanoEra, ExecutionUnits, Lovelace, ScriptData, ScriptRedeemer,
TextEnvelope, TxIn)
import Cardano.Api (AnyCardanoEra, ExecutionUnits, HashableScriptData, Lovelace,
ScriptRedeemer, TextEnvelope, TxIn)
import Cardano.Benchmarking.OuroborosImports (SigningKeyFile)
import Cardano.Node.Configuration.NodeAddress (NodeIPv4Address)

Expand Down Expand Up @@ -85,8 +85,8 @@ data PayMode where
deriving instance Generic PayMode

data ScriptBudget where
StaticScriptBudget :: !ScriptData -> !ScriptRedeemer -> !ExecutionUnits -> ScriptBudget
CheckScriptBudget :: !ScriptData -> !ScriptRedeemer -> !ExecutionUnits -> ScriptBudget
StaticScriptBudget :: !HashableScriptData -> !ScriptRedeemer -> !ExecutionUnits -> ScriptBudget
CheckScriptBudget :: !HashableScriptData -> !ScriptRedeemer -> !ExecutionUnits -> ScriptBudget
AutoScript :: ScriptBudget --todo: add fraction of total available budget to use (==2 with 2 inputs !)
deriving (Show, Eq)
deriving instance Generic ScriptBudget
Expand Down
2 changes: 1 addition & 1 deletion bench/tx-generator/src/Cardano/TxGenerator/UTxO.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ mkUTxOVariant networkId key value
mkUTxOScript :: forall era.
IsShelleyBasedEra era
=> NetworkId
-> (Script PlutusScriptV1, ScriptData)
-> (Script PlutusScriptV1, HashableScriptData)
-> Witness WitCtxTxIn era
-> ToUTxO era
mkUTxOScript networkId (script, txOutDatum) witness value
Expand Down

0 comments on commit c598dea

Please sign in to comment.