Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrote gas usage tester #86

Merged
merged 4 commits into from Jul 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 39 additions & 0 deletions gastest/Main.hs
@@ -0,0 +1,39 @@
{-# LANGUAGE LambdaCase #-}

module Main where

import Prelude hiding (Word)

import Control.Lens (view)
import Control.Monad (forM_, replicateM)
import Control.Monad.IO.Class (liftIO)
import Control.Monad.Reader (runReaderT)
import Control.Monad.State (execState)
import Data.Foldable (maximumBy, minimumBy)
import Data.Ord (comparing)
import Data.Text (unpack)
import EVM (gas, state)
import Hedgehog.Gen (sample, prune)
import System.Environment (getArgs)

import Echidna.ABI
import Echidna.Config
import Echidna.Exec
import Echidna.Solidity


main :: IO ()
main = let conf m = runReaderT m defaultConfig in getArgs >>= \case
[] -> putStrLn "need to specify solidity file"
prog:_ -> do
(v,a,_) <- conf $ loadSolidity prog Nothing
forM_ a $ \f -> do
inputs <- liftIO $ replicateM 1000 . sample . prune . conf $ genAbiCall f
let results = map (\i -> (i, g v - g (execState (execCall i) v))) inputs; g = view $ state . gas
mapM_ putStrLn [ unpack $ fst f
, pp maximumBy "Max" results, pp minimumBy "Min" results
, " Avg: " ++ show ( fromIntegral (sum $ snd <$> results)
/ fromIntegral (length results) :: Double)
] where pp f s r = let (c, n) = f (comparing snd) r in
" " ++ s ++ ": " ++ show (toInteger n)
++ " (" ++ displayAbiCall c ++ ")"
21 changes: 12 additions & 9 deletions lib/Echidna/ABI.hs
Expand Up @@ -12,6 +12,7 @@ module Echidna.ABI (
, genAbiBool
, genAbiBytes
, genAbiBytesDynamic
, genAbiCall
, genAbiInt
, genInteractions
, genAbiString
Expand All @@ -32,7 +33,7 @@ import Data.DoubleWord (Word128(..), Word160(..))
import Data.Monoid ((<>))
import Data.ByteString (ByteString)
import Data.Text (Text, unpack)
import Data.Vector (Vector)
import Data.Vector (Vector, generateM)
import Hedgehog.Internal.Gen (MonadGen)
import GHC.Exts (IsList(..), Item)
import Hedgehog.Range (exponential, exponentialFrom, constant, singleton, Range)
Expand Down Expand Up @@ -115,14 +116,16 @@ genAbiType = Gen.choice [ pure AbiBytesDynamicType
]

genVecOfType :: (MonadReader Config m, MonadGen m) => AbiType -> Range Int -> m (Vector AbiValue)
genVecOfType t r = fmap fromList . Gen.list r $ case t of
AbiUIntType n -> genAbiUInt n
AbiIntType n -> genAbiInt n
AbiAddressType -> genAbiAddress
AbiBoolType -> genAbiBool
AbiBytesType n -> genAbiBytes n
AbiArrayType n t' -> genAbiArray n t'
_ -> error "Arrays must only contain statically sized types"
genVecOfType t r = do
s <- Gen.integral r
generateM s $ \_ -> case t of
AbiUIntType n -> genAbiUInt n
AbiIntType n -> genAbiInt n
AbiAddressType -> genAbiAddress
AbiBoolType -> genAbiBool
AbiBytesType n -> genAbiBytes n
AbiArrayType n t' -> genAbiArray n t'
_ -> error "Arrays must only contain statically sized types"

genAbiArrayDynamic :: (MonadReader Config m, MonadGen m) => AbiType -> m AbiValue
genAbiArrayDynamic t = AbiArrayDynamic t <$> genVecOfType t (constant 0 256)
Expand Down
4 changes: 4 additions & 0 deletions package.yaml
Expand Up @@ -60,3 +60,7 @@ executables:
main: Main.hs
source-dirs: perprop
dependencies: echidna
gastest-exe:
main: Main.hs
source-dirs: gastest
dependencies: echidna