Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
28 changes: 22 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
Plutus Scripts Evaluation
===
# Plutus Scripts Evaluation

Tools used to:

1. Accumulate Plutus script evaluation events by replaying blockchain folding over the Ledger state and extracting `PlutusScriptEvaluationEvent`s.
2. Record accumulated events:
1. On the file system as "dump" files.
2. In the PostgreSQL database.
1. On the file system as "dump" files.
2. In the PostgreSQL database.

## How to use

0. Initialise the PostgreSQL database and connection using files in the `database` folder:
* There is a [pgModeler](https://pgmodeler.io/) (Open-source tool) project for it,
* As well as the DDL statements.
- There is a [pgModeler](https://pgmodeler.io/) (Open-source tool) project for it,
- As well as the DDL statements.
1. Create `.envrc.local` with the following content (adjust the paths as needed):
```sh
export CARDANO_NODE_SOCKET_PATH="/home/projects/cardano/node/node-state/mainnet/node.sock"
Expand All @@ -21,3 +21,19 @@ Tools used to:
2. Enter the `nix` shell using either `nix develop` command or `direnv` hooked to your shell.
3. See available commands by entering `info` in the shell.
4. Run the script dump job using the `dump` command or script upload job with the `load` command.

## How to re-evaluate recorded Plutus Script evaluations locally

The database contains plutus script evaluation events from Mainnet which can be replayed locally to re-evaluate the scripts.

There is less value in re-evaluating scripts without any changes, as one would
simply re-obtain results that are already known. However, this can be useful
when the script evaluation logic has changed, and one wants to compare results
produced by the new logic with the old results.

The repository contains a program that can be used to re-evaluate the scripts
locally. You can use this program as a basis for your own re-evaluation, where
you can modify various parameters to suit your needs:

- The [Main module](plutus-script-evaluation/evaluate-scripts/Main.hs) of the `evaluate-scripts` executable.
- The main workhorse, function `evaluateScripts` in the [Evaluation module](plutus-script-evaluation/evaluate-scripts/Evaluation.hs) does the boring parts (aggregating relevant script evaluation inputs, streaming the data from DB to a local computer, decoding CBOR) so that you can do the interesting part: fold over the script evaluations from the Mainnet accessing all of the original evaluation inputs, to re-interpret them accordingly to your task, maintaining local state (accumulator) if needed.
12 changes: 8 additions & 4 deletions plutus-script-evaluation/lib/Database/Query.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Database.Query where

import Cardano.Slotting.Slot (SlotNo)
import Control.Monad.IO.Unlift (MonadUnliftIO, withRunInIO)
import Data.Profunctor.Product.Default (Default)
import Database.Orphans ()
import Database.PostgreSQL.Simple (Connection)
Expand Down Expand Up @@ -92,13 +93,16 @@ selectSerialisedScriptsBatch conn count =
pure serialised

withScriptEvaluationEvents
:: Connection
:: (MonadUnliftIO m)
=> Connection
-> a
-> (a -> ScriptEvaluationRecord -> IO a)
-> IO a
-> (a -> ScriptEvaluationRecord -> m a)
-> m a
withScriptEvaluationEvents conn a f = do
let select = selectTable scriptEvaluations
runSelectFold conn select a f
withRunInIO \runInIO ->
runSelectFold conn select a \accum record ->
runInIO (f accum record)

insertScriptEvaluationEvents
:: (Default ToFields EvaluationEventRecord EvaluationEventRecordFields)
Expand Down
23 changes: 11 additions & 12 deletions plutus-script-evaluation/lib/Evaluate.hs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{-# LANGUAGE PartialTypeSignatures #-}

module Evaluate where

import Codec.Serialise (deserialise)
import Control.Monad.IO.Unlift (MonadUnliftIO)
import Control.Monad.Trans.Except (runExceptT)
import Control.Monad.Trans.Writer (WriterT (runWriterT))
import Data.ByteString qualified as BSL
Expand Down Expand Up @@ -35,20 +34,23 @@ data ScriptEvaluationInput = MkScriptEvaluationInput
}

evaluateScripts
:: Postgres.Connection
:: (MonadFail m, MonadUnliftIO m)
=> Postgres.Connection
-- ^ Database connection
-> a
-- ^ Initial accumulator
-> (ScriptEvaluationInput -> a -> IO a)
-> (ScriptEvaluationInput -> a -> m a)
-- ^ Accumulation function
-> IO a
-> m a
evaluateScripts conn initialAccum accumulate =
Db.withScriptEvaluationEvents conn initialAccum \accum record -> do
scriptInput <- inputFromRecord record
accumulate scriptInput accum

inputFromRecord
:: (MonadFail m) => Db.ScriptEvaluationRecord -> m ScriptEvaluationInput
:: (MonadFail m)
=> Db.ScriptEvaluationRecord
-> m ScriptEvaluationInput
inputFromRecord MkScriptEvaluationRecord'{..} = do
let mkEvalCtx f =
runExceptT (runWriterT f) >>= \case
Expand Down Expand Up @@ -97,13 +99,10 @@ onScriptEvaluationInput MkScriptEvaluationInput{..} budget = do
Left err -> do
putStrLn $ "Script evaluation was not successful: " <> show err
Right (ExBudget cpu mem) -> do
putStrLn $
"Script evaluation was successful.\nConsumed: "
<> show cpu
<> ", "
<> show mem
putStrLn $
putStrLn "Script evaluation was successful."
putStrLn
let ExBudget cpu' mem' = seiExBudget
in "Expected: " <> show cpu' <> ", " <> show mem'
putStrLn $ "Consumed: " <> show cpu <> ", " <> show mem

pure budget
4 changes: 3 additions & 1 deletion plutus-script-evaluation/plutus-script-evaluation.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ library
, string-interpolate ^>=0.3
, text
, transformers
, unliftio-core
, vector

executable dump-script-events
Expand Down Expand Up @@ -157,5 +158,6 @@ executable evaluate-scripts
ghc-options: -threaded -rtsopts
other-modules: Options
build-depends:
, cardano-api ^>=10.4
, cardano-api ^>=10.4
, text
, unliftio-core