@@ -7,6 +7,9 @@ import Control.Monad.Trans.Except (runExceptT)
7
7
import Control.Monad.Trans.Writer (WriterT (runWriterT ))
8
8
import Data.ByteString qualified as BSL
9
9
import Data.ByteString.Short qualified as BSS
10
+ import Data.Text qualified as Text
11
+ import Data.Text.IO qualified as TIO
12
+ import Data.Time.Clock (getCurrentTime )
10
13
import Database qualified as Db
11
14
import Database.PostgreSQL.Simple qualified as Postgres
12
15
import Database.Schema (ScriptEvaluationRecord' (.. ))
@@ -27,7 +30,6 @@ import PlutusLedgerApi.Common (
27
30
import PlutusLedgerApi.V1 qualified as V1
28
31
import PlutusLedgerApi.V2 qualified as V2
29
32
import PlutusLedgerApi.V3 qualified as V3
30
- import System.Exit (exitFailure )
31
33
import Text.PrettyBy qualified as Pretty
32
34
33
35
data ScriptEvaluationInput = MkScriptEvaluationInput
@@ -41,6 +43,26 @@ data ScriptEvaluationInput = MkScriptEvaluationInput
41
43
, seiBlock :: BlockNo
42
44
}
43
45
46
+ renderScriptEvaluationInput :: ScriptEvaluationInput -> String
47
+ renderScriptEvaluationInput MkScriptEvaluationInput {.. } =
48
+ " \n\n seiPlutusLedgerLanguage = "
49
+ ++ show seiPlutusLedgerLanguage
50
+ ++ " \n\n seiMajorProtocolVersion = "
51
+ ++ show seiMajorProtocolVersion
52
+ ++ " \n\n seiEvaluationContext = <evaluation context>"
53
+ ++ " \n\n seiExBudget = "
54
+ ++ show seiExBudget
55
+ ++ " \n\n seiEvaluationSuccess = "
56
+ ++ show seiEvaluationSuccess
57
+ ++ " \n\n seiBlock = "
58
+ ++ show seiBlock
59
+ ++ " \n\n seiData = "
60
+ ++ Pretty. display seiData
61
+ ++ " \n\n seiScript = "
62
+ ++ ( let ScriptNamedDeBruijn uplc = deserialisedScript seiScript
63
+ in Pretty. display uplc
64
+ )
65
+
44
66
evaluateScripts
45
67
:: (MonadFail m , MonadUnliftIO m )
46
68
=> Postgres. Connection
@@ -91,56 +113,63 @@ inputFromRecord MkScriptEvaluationRecord'{..} = do
91
113
, seiBlock = seBlockNo
92
114
}
93
115
94
- onScriptEvaluationInput :: ScriptEvaluationInput -> ExBudget -> IO ExBudget
95
- onScriptEvaluationInput MkScriptEvaluationInput {.. } budget = do
116
+ onScriptEvaluationInput :: ScriptEvaluationInput -> () -> IO ()
117
+ onScriptEvaluationInput input @ MkScriptEvaluationInput {.. } _accum = do
96
118
let
97
119
(_logOutput, evaluationResult) =
98
120
evaluateScriptRestricting
99
121
seiPlutusLedgerLanguage
100
122
seiMajorProtocolVersion
101
123
Quiet
102
124
seiEvaluationContext
103
- (ExBudget maxBound maxBound )
125
+ (ExBudget maxBound maxBound ) -- will check the budget separately
104
126
seiScript
105
127
seiData
106
128
129
+ let budgetExceeded (ExBudget cpu mem) =
130
+ let ExBudget cpuPaidFor memPaidFor = seiExBudget
131
+ in cpu > cpuPaidFor || mem > memPaidFor
132
+
107
133
let evaluationSuccess =
108
- either (const False ) (const True ) evaluationResult
134
+ either (const False ) (not . budgetExceeded ) evaluationResult
109
135
110
136
print seiBlock
111
137
112
138
when (evaluationSuccess /= seiEvaluationSuccess) do
113
- putStrLn $
114
- " Script evaluation result ("
115
- ++ show evaluationSuccess
116
- ++ " ) does not match the expected result ("
117
- ++ show seiEvaluationSuccess
118
- ++ " ): "
119
- let ScriptNamedDeBruijn uplc = deserialisedScript seiScript
120
- in putStrLn $ Pretty. display uplc
139
+ let msg =
140
+ " Script evaluation result ("
141
+ ++ show evaluationSuccess
142
+ ++ " ) does not match the recorded result ("
143
+ ++ show seiEvaluationSuccess
144
+ ++ " )"
145
+
146
+ nonce <- getCurrentTime
147
+ let logFile = show seiBlock ++ " _" ++ show nonce ++ " .log"
148
+
149
+ putStrLn msg
150
+ putStrLn $ " Writing log to " ++ logFile
151
+
152
+ TIO. writeFile logFile $
153
+ Text. pack msg
154
+ <> " \n\n Evaluation result:\n "
155
+ <> Text. pack (show evaluationResult)
156
+ <> " \n\n Script evaluation inputs:\n "
157
+ <> Text. pack (renderScriptEvaluationInput input)
121
158
122
159
case evaluationResult of
123
160
Left err ->
124
161
putStrLn $ " Script evaluation was not successful: " <> show err
125
- Right (ExBudget cpu mem) -> do
126
- let ExBudget cpu' mem' = seiExBudget
127
- if cpu > cpu' || mem > mem'
162
+ Right spentExBudget -> do
163
+ if budgetExceeded spentExBudget
128
164
then do
129
165
putStrLn " Budget exceeded!"
130
- putStrLn $ " Paid for: " <> show cpu' <> " , " <> show mem'
131
- putStrLn $ " Consumed: " <> show cpu <> " , " <> show mem
132
- exitFailure
166
+ putStrLn $ " Paid for: " <> show seiExBudget
167
+ putStrLn $ " Consumed: " <> show spentExBudget
133
168
else
134
- if cpu == cpu' && mem == mem'
169
+ if seiExBudget == spentExBudget
135
170
then do
136
- putStrLn $
137
- " Budget matches exactly: "
138
- <> show cpu
139
- <> " , "
140
- <> show mem
171
+ putStrLn $ " Budget matches exactly: " <> show spentExBudget
141
172
else do
142
173
putStrLn " Budget is sufficient:"
143
- putStrLn $ " Paid for: " <> show cpu' <> " , " <> show mem'
144
- putStrLn $ " Consumed: " <> show cpu <> " , " <> show mem
145
-
146
- pure budget
174
+ putStrLn $ " Paid for: " <> show seiExBudget
175
+ putStrLn $ " Consumed: " <> show spentExBudget
0 commit comments