-
Notifications
You must be signed in to change notification settings - Fork 368
/
Widgets.hs
395 lines (353 loc) · 12.7 KB
/
Widgets.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
{-# LANGUAGE CPP #-}
module Echidna.UI.Widgets where
#ifdef INTERACTIVE_UI
import Brick hiding (style)
import Brick.AttrMap qualified as A
import Brick.Widgets.Border
import Brick.Widgets.Center
import Brick.Widgets.Dialog qualified as B
import Control.Monad.Reader (MonadReader, asks, ask)
import Control.Monad.ST (RealWorld)
import Data.List (nub, intersperse, sortBy)
import Data.Map (Map)
import Data.Map qualified as Map
import Data.Maybe (fromMaybe, isJust, fromJust)
import Data.Sequence (Seq)
import Data.Sequence qualified as Seq
import Data.String.AnsiEscapeCodes.Strip.Text (stripAnsiEscapeCodes)
import Data.Text (Text)
import Data.Text qualified as T
import Data.Time (LocalTime, NominalDiffTime, formatTime, defaultTimeLocale, diffLocalTime)
import Data.Version (showVersion)
import Graphics.Vty qualified as V
import Paths_echidna qualified (version)
import Text.Printf (printf)
import Text.Wrap
import Echidna.ABI
import Echidna.Types.Campaign
import Echidna.Types.Config
import Echidna.Types.Test
import Echidna.Types.Tx (Tx(..), TxResult(..))
import Echidna.UI.Report
import Echidna.Utility (timePrefix)
import EVM.Format (showTraceTree)
import EVM.Types (Addr, Contract, W256, VM(..), VMType(Concrete))
data UIState = UIState
{ status :: UIStateStatus
, campaigns :: [WorkerState]
, timeStarted :: LocalTime
, timeStopped :: Maybe LocalTime
, now :: LocalTime
, fetchedContracts :: Map Addr (Maybe Contract)
, fetchedSlots :: Map Addr (Map W256 (Maybe W256))
, fetchedDialog :: B.Dialog () Name
, displayFetchedDialog :: Bool
, displayLogPane :: Bool
, displayTestsPane :: Bool
, events :: Seq (LocalTime, CampaignEvent)
, workersAlive :: Int
, corpusSize :: Int
, coverage :: Int
, numCodehashes :: Int
, lastNewCov :: LocalTime
-- ^ last timestamp of 'NewCoverage' event
, tests :: [EchidnaTest]
}
data UIStateStatus = Uninitialized | Running
attrs :: A.AttrMap
attrs = A.attrMap (V.white `on` V.black)
[ (attrName "failure", fg V.brightRed)
, (attrName "maximum", fg V.brightBlue)
, (attrName "bold", fg V.white `V.withStyle` V.bold)
, (attrName "tx", fg V.brightWhite)
, (attrName "working", fg V.brightBlue)
, (attrName "success", fg V.brightGreen)
, (attrName "title", fg V.brightYellow `V.withStyle` V.bold)
, (attrName "subtitle", fg V.brightCyan `V.withStyle` V.bold)
, (attrName "time", fg (V.rgbColor (0x70 :: Int) 0x70 0x70))
]
bold :: Widget n -> Widget n
bold = withAttr (attrName "bold")
failure :: Widget n -> Widget n
failure = withAttr (attrName "failure")
success :: Widget n -> Widget n
success = withAttr (attrName "success")
data Name
= LogViewPort
| TestsViewPort
| SBClick ClickableScrollbarElement Name
deriving (Ord, Show, Eq)
-- | Render 'Campaign' progress as a 'Widget'.
campaignStatus :: MonadReader Env m => UIState -> m (Widget Name)
campaignStatus uiState = do
tests <- testsWidget uiState.tests
if uiState.workersAlive == 0 then
mainbox tests (finalStatus "Campaign complete, C-c or esc to exit")
else
case uiState.status of
Uninitialized ->
mainbox (padLeft (Pad 1) $ str "Starting up, please wait...") emptyWidget
Running ->
mainbox tests emptyWidget
where
mainbox inner underneath = do
env <- ask
pure $ hCenter . hLimit 160 $
joinBorders $ borderWithLabel echidnaTitle $
summaryWidget env uiState
<=>
(if uiState.displayTestsPane then
hBorderWithLabel (withAttr (attrName "subtitle") $ str $
(" Tests (" <> show (length uiState.tests)) <> ") ")
<=>
inner
else emptyWidget)
<=>
(if uiState.displayLogPane then
hBorderWithLabel (withAttr (attrName "subtitle") $ str $
" Log (" <> show (length uiState.events) <> ") ")
<=>
logPane uiState
else emptyWidget)
<=>
underneath
echidnaTitle =
str "[ " <+>
withAttr (attrName "title")
(str $ "Echidna " <> showVersion Paths_echidna.version) <+>
str " ]"
finalStatus s = hBorder <=> hCenter (bold $ str s)
logPane :: UIState -> Widget Name
logPane uiState =
vLimitPercent 33 .
padLeft (Pad 1) $
withClickableVScrollBars SBClick .
withVScrollBars OnRight .
withVScrollBarHandles .
viewport LogViewPort Vertical $
foldl (<=>) emptyWidget (showLogLine <$> Seq.reverse uiState.events)
showLogLine :: (LocalTime, CampaignEvent) -> Widget Name
showLogLine (time, event@(WorkerEvent workerId workerType _)) =
(withAttr (attrName "time") $ str $ (timePrefix time) <> "[Worker " <> show workerId <> symSuffix <> "] ")
<+> strBreak (ppCampaignEvent event)
where
symSuffix = case workerType of
SymbolicWorker -> ", symbolic"
_ -> ""
showLogLine (time, event) =
(withAttr (attrName "time") $ str $ (timePrefix time) <> " ") <+> strBreak (ppCampaignEvent event)
summaryWidget :: Env -> UIState -> Widget Name
summaryWidget env uiState =
vLimit 5 $ -- limit to 5 rows
hLimitPercent 33 leftSide <+> vBorder <+>
hLimitPercent 50 middle <+> vBorder <+>
rightSide
where
leftSide =
padLeft (Pad 1) $
(str ("Time elapsed: " <> timeElapsed uiState uiState.timeStarted) <+> fill ' ')
<=>
(str "Workers: " <+> outOf uiState.workersAlive (length uiState.campaigns))
<=>
str ("Seed: " ++ ppSeed uiState.campaigns)
<=>
perfWidget uiState
<=>
str ("Total calls: " <> progress (sum $ (.ncalls) <$> uiState.campaigns)
env.cfg.campaignConf.testLimit)
middle =
padLeft (Pad 1) $
str ("Unique instructions: " <> show uiState.coverage)
<=>
str ("Unique codehashes: " <> show uiState.numCodehashes)
<=>
str ("Corpus size: " <> show uiState.corpusSize <> " seqs")
<=>
str ("New coverage: " <> timeElapsed uiState uiState.lastNewCov <> " ago") <+> fill ' '
rightSide =
padLeft (Pad 1) $
(rpcInfoWidget uiState.fetchedContracts uiState.fetchedSlots env.chainId)
timeElapsed :: UIState -> LocalTime -> String
timeElapsed uiState since =
formatNominalDiffTime $
diffLocalTime (fromMaybe uiState.now uiState.timeStopped) since
formatNominalDiffTime :: NominalDiffTime -> String
formatNominalDiffTime diff =
let fmt = if | diff < 60 -> "%Ss"
| diff < 60*60 -> "%Mm %Ss"
| diff < 24*60*60 -> "%Hh %Mm %Ss"
| otherwise -> "%dd %Hh %Mm %Ss"
in formatTime defaultTimeLocale fmt diff
rpcInfoWidget
:: Map Addr (Maybe Contract)
-> Map Addr (Map W256 (Maybe W256))
-> Maybe W256
-> Widget Name
rpcInfoWidget contracts slots chainId =
(str "Chain ID: " <+> str (maybe "-" show chainId))
<=>
(str "Fetched contracts: " <+> countWidget (Map.elems contracts))
<=>
(str "Fetched slots: " <+> countWidget (concat $ Map.elems (Map.elems <$> slots)))
where
countWidget fetches =
let successful = filter isJust fetches
in outOf (length successful) (length fetches)
outOf :: Int -> Int -> Widget n
outOf n m =
let style = if n == m then success else failure
in style . str $ progress n m
perfWidget :: UIState -> Widget n
perfWidget uiState =
str $ "Calls/s: " <>
if totalTime > 0
then show $ totalCalls `div` totalTime
else "-"
where
totalCalls = sum $ (.ncalls) <$> uiState.campaigns
totalTime = round $
diffLocalTime (fromMaybe uiState.now uiState.timeStopped)
uiState.timeStarted
ppSeed :: [WorkerState] -> String
ppSeed campaigns = show (head campaigns).genDict.defSeed
fetchedDialogWidget :: UIState -> Widget Name
fetchedDialogWidget uiState =
B.renderDialog uiState.fetchedDialog $ padLeftRight 1 $
foldl (<=>) emptyWidget (Map.mapWithKey renderContract uiState.fetchedContracts)
where
renderContract addr (Just _code) =
bold (str (show addr))
<=>
renderSlots addr
renderContract addr Nothing =
bold $ failure (str (show addr))
renderSlots addr =
foldl (<=>) emptyWidget $
Map.mapWithKey renderSlot (fromMaybe mempty $ Map.lookup addr uiState.fetchedSlots)
renderSlot slot (Just value) =
padLeft (Pad 1) $ strBreak (show slot <> " => " <> show value)
renderSlot slot Nothing =
padLeft (Pad 1) $ failure $ str (show slot)
failedFirst :: EchidnaTest -> EchidnaTest -> Ordering
failedFirst t1 _ | didFail t1 = LT
| otherwise = GT
testsWidget :: MonadReader Env m => [EchidnaTest] -> m (Widget Name)
testsWidget tests' =
withClickableVScrollBars SBClick .
withVScrollBars OnRight .
withVScrollBarHandles .
viewport TestsViewPort Vertical .
foldl (<=>) emptyWidget . intersperse hBorder <$>
traverse testWidget (sortBy failedFirst tests')
testWidget :: MonadReader Env m => EchidnaTest -> m (Widget Name)
testWidget test =
case test.testType of
Exploration -> widget tsWidget "exploration" ""
PropertyTest n _ -> widget tsWidget n ""
OptimizationTest n _ -> widget optWidget n "optimizing "
AssertionTest _ s _ -> widget tsWidget (encodeSig s) "assertion in "
CallTest n _ -> widget tsWidget n ""
where
widget f n infront = do
(status, details) <- f test.state test
pure $ padLeft (Pad 1) $
str infront <+> name n <+> str ": " <+> status
<=> padTop (Pad 1) details
name n = bold $ str (T.unpack n)
tsWidget
:: MonadReader Env m
=> TestState
-> EchidnaTest
-> m (Widget Name, Widget Name)
tsWidget (Failed e) _ = pure (str "could not evaluate", str $ show e)
tsWidget Solved t = failWidget Nothing t.reproducer (fromJust t.vm) t.value t.result
tsWidget Passed _ = pure (success $ str "PASSED!", emptyWidget)
tsWidget Open _ = pure (success $ str "passing", emptyWidget)
tsWidget (Large n) t = do
m <- asks (.cfg.campaignConf.shrinkLimit)
failWidget (if n < m then Just (n,m) else Nothing) t.reproducer (fromJust t.vm) t.value t.result
titleWidget :: Widget n
titleWidget = str "Call sequence" <+> str ":"
tracesWidget :: MonadReader Env m => VM Concrete RealWorld -> m (Widget n)
tracesWidget vm = do
dappInfo <- asks (.dapp)
-- TODO: showTraceTree does coloring with ANSI escape codes, we need to strip
-- those because they break the Brick TUI. Fix in hevm so we can display
-- colors here as well.
let traces = stripAnsiEscapeCodes $ showTraceTree dappInfo vm
pure $
if T.null traces then str ""
else str "Traces" <+> str ":" <=> (txtBreak traces)
failWidget
:: MonadReader Env m
=> Maybe (Int, Int)
-> [Tx]
-> VM Concrete RealWorld
-> TestValue
-> TxResult
-> m (Widget Name, Widget Name)
failWidget _ [] _ _ _= pure (failureBadge, str "*no transactions made*")
failWidget b xs vm _ r = do
s <- seqWidget vm xs
traces <- tracesWidget vm
pure
( failureBadge <+> str (" with " ++ show r)
, status <=> titleWidget <=> s <=> str " " <=> traces
)
where
status = case b of
Nothing -> emptyWidget
Just (n,m) ->
str "Current action: " <+>
withAttr (attrName "working") (str ("shrinking " ++ progress n m))
optWidget
:: MonadReader Env m
=> TestState
-> EchidnaTest
-> m (Widget Name, Widget Name)
optWidget (Failed e) _ = pure (str "could not evaluate", str $ show e)
optWidget Solved _ = error "optimization tests cannot be solved"
optWidget Passed t = pure (str $ "max value found: " ++ show t.value, emptyWidget)
optWidget Open t =
pure (withAttr (attrName "working") $ str $
"optimizing, max value: " ++ show t.value, emptyWidget)
optWidget (Large n) t = do
m <- asks (.cfg.campaignConf.shrinkLimit)
maxWidget (if n < m then Just (n,m) else Nothing) t.reproducer (fromJust t.vm) t.value
maxWidget
:: MonadReader Env m
=> Maybe (Int, Int)
-> [Tx]
-> VM Concrete RealWorld
-> TestValue
-> m (Widget Name, Widget Name)
maxWidget _ [] _ _ = pure (failureBadge, str "*no transactions made*")
maxWidget b xs vm v = do
s <- seqWidget vm xs
traces <- tracesWidget vm
pure
( maximumBadge <+> str (" max value: " ++ show v)
, status <=> titleWidget <=> s <=> str " " <=> traces
)
where
status = case b of
Nothing -> emptyWidget
Just (n,m) ->
str "Current action: " <+>
withAttr (attrName "working") (str ("shrinking " ++ progress n m))
seqWidget :: MonadReader Env m => VM Concrete RealWorld -> [Tx] -> m (Widget Name)
seqWidget vm xs = do
ppTxs <- mapM (ppTx vm $ length (nub $ (.src) <$> xs) /= 1) xs
let ordinals = str . printf "%d. " <$> [1 :: Int ..]
pure $
foldl (<=>) emptyWidget $
zipWith (<+>) ordinals (withAttr (attrName "tx") . strBreak <$> ppTxs)
failureBadge :: Widget Name
failureBadge = failure $ str "FAILED!"
maximumBadge :: Widget Name
maximumBadge = withAttr (attrName "maximum") $ str "OPTIMIZED!"
strBreak :: String -> Widget n
strBreak = strWrapWith $ defaultWrapSettings { breakLongWords = True }
txtBreak :: Text -> Widget n
txtBreak = txtWrapWith $ defaultWrapSettings { breakLongWords = True }
#endif