Skip to content

Commit

Permalink
Save and load reproducers for every test (#858)
Browse files Browse the repository at this point in the history
* Save and load reproducers for every test

* avoid saving [] as reproducer
  • Loading branch information
ggrieco-tob authored Dec 14, 2022
1 parent a83f726 commit 1f59ae1
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
4 changes: 3 additions & 1 deletion lib/Echidna.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ prepareContract :: (MonadCatch m, MonadReader x m, MonadIO m, MonadFail m, Has S
=> EConfig -> NE.NonEmpty FilePath -> Maybe ContractName -> Seed
-> m (VM, SourceCache, [SolcContract], World, [EchidnaTest], Maybe GenDict, [[Tx]])
prepareContract cfg fs c g = do
ctxs <- liftIO $ loadTxs cd
ctxs1 <- liftIO $ loadTxs (fmap (++ "/reproducers/") cd)
ctxs2 <- liftIO $ loadTxs (fmap (++ "/coverage/") cd)
let ctxs = ctxs1 ++ ctxs2

-- compile and load contracts
(cs, scs) <- Echidna.Solidity.contracts fs
Expand Down
11 changes: 5 additions & 6 deletions lib/Echidna/Output/Corpus.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,18 @@ import Echidna.Output.Utils

saveTxs :: Maybe FilePath -> [[Tx]] -> IO ()
saveTxs (Just d) txs = mapM_ saveTx txs where
saveTx v = do let fn = d ++ "/coverage/" ++ (show . hash . show) v ++ ".txt"
saveTx v = do let fn = d ++ (show . hash . show) v ++ ".txt"
b <- doesFileExist fn
unless b $ encodeFile fn (toJSON v)
saveTxs Nothing _ = pure ()

loadTxs :: Maybe FilePath -> IO [[Tx]]
loadTxs (Just d) = do
let d' = d ++ "/coverage"
createDirectoryIfMissing True d'
fs <- listDirectory d'
createDirectoryIfMissing True d
fs <- listDirectory d
css <- mapM readCall <$> mapM makeRelativeToCurrentDirectory fs
txs <- catMaybes <$> withCurrentDirectory d' css
putStrLn ("Loaded total of " ++ show (length txs) ++ " transactions from " ++ d')
txs <- catMaybes <$> withCurrentDirectory d css
putStrLn ("Loaded total of " ++ show (length txs) ++ " transactions from " ++ d)
return txs
where readCall f = decodeStrict <$> BS.readFile f

Expand Down
5 changes: 3 additions & 2 deletions src/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import Echidna.Config
import Echidna.Types.Config hiding (cfg)
import Echidna.Types.Solidity
import Echidna.Types.Campaign
import Echidna.Types.Test (TestMode)
import Echidna.Types.Test (TestMode, testReproducer)
import Echidna.Test (validateTestMode)
import Echidna.Campaign (isSuccess)
import Echidna.UI
Expand Down Expand Up @@ -133,7 +133,8 @@ main = do
return (sc, cs, r)

-- save corpus
saveTxs cd (snd <$> DS.toList (cpg ^. corpus))
saveTxs (fmap (++ "/reproducers/") cd) (filter (not . null) $ map (^. testReproducer) $ cpg ^. tests)
saveTxs (fmap (++ "/coverage/") cd) (snd <$> DS.toList (cpg ^. corpus))

-- get current time to save coverage
t <- getSystemTime
Expand Down

0 comments on commit 1f59ae1

Please sign in to comment.