Skip to content

Commit d564592

Browse files
authored
Merge pull request #203 from arcz/witch
Replace num/fromIntegral with witch
2 parents a340a50 + 80b3d4c commit d564592

27 files changed

+323
-263
lines changed

hevm-cli/hevm-cli.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import Data.Word (Word64)
2525
import GHC.Conc (getNumProcessors)
2626
import Numeric.Natural (Natural)
2727
import Optics.Core ((&), (%), set)
28+
import Witch (unsafeInto)
2829
import Options.Generic as Options
2930
import Paths_hevm qualified as Paths
3031
import System.IO (stderr)
@@ -277,7 +278,7 @@ main = do
277278
Test {} -> do
278279
root <- getRoot cmd
279280
withCurrentDirectory root $ do
280-
cores <- num <$> getNumProcessors
281+
cores <- unsafeInto <$> getNumProcessors
281282
solver <- getSolver cmd
282283
withSolvers solver cores cmd.smttimeout $ \solvers -> do
283284
buildOut <- readBuildOutput root (getProjectType cmd)
@@ -381,7 +382,7 @@ assert cmd = do
381382
calldata <- buildCalldata cmd
382383
preState <- symvmFromCommand cmd calldata
383384
let errCodes = fromMaybe defaultPanicCodes cmd.assertions
384-
cores <- num <$> getNumProcessors
385+
cores <- unsafeInto <$> getNumProcessors
385386
let solverCount = fromMaybe cores cmd.numSolvers
386387
solver <- getSolver cmd
387388
withSolvers solver solverCount cmd.smttimeout $ \solvers -> do

hevm.cabal

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,8 @@ library
186186
spool >= 0.1 && < 0.2,
187187
stm >= 2.5.0 && < 2.6.0,
188188
spawn >= 0.3 && < 0.4,
189-
filepattern >= 0.1.2 && < 0.2
189+
filepattern >= 0.1.2 && < 0.2,
190+
witch >= 1.1 && < 1.3
190191
if !os(windows)
191192
build-depends:
192193
brick >= 1.4 && < 1.5,
@@ -242,7 +243,8 @@ executable hevm
242243
stm,
243244
spawn,
244245
optics-core,
245-
githash >= 0.1.6 && < 0.2
246+
githash >= 0.1.6 && < 0.2,
247+
witch
246248
if os(windows)
247249
buildable: False
248250

@@ -294,7 +296,8 @@ common test-base
294296
smt2-parser >= 0.1.0.1,
295297
operational,
296298
optics-core,
297-
optics-extra
299+
optics-extra,
300+
witch
298301

299302
library test-utils
300303
import:

src/EVM.hs

Lines changed: 97 additions & 99 deletions
Large diffs are not rendered by default.

src/EVM/ABI.hs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ import Test.QuickCheck hiding ((.&.), label)
8787
import Text.Megaparsec qualified as P
8888
import Text.Megaparsec.Char qualified as P
8989
import Text.ParserCombinators.ReadP
90+
import Witch (unsafeInto, into)
9091

9192
data AbiValue
9293
= AbiUInt Int Word256
@@ -216,7 +217,7 @@ getAbi t = label (Text.unpack (abiTypeSolidity t)) $
216217
AbiArrayDynamicType t' -> do
217218
AbiUInt _ n <- label "array length" (getAbi (AbiUIntType 256))
218219
AbiArrayDynamic t' <$>
219-
label "array body" (getAbiSeq (fromIntegral n) (repeat t'))
220+
label "array body" (getAbiSeq (unsafeInto n) (repeat t'))
220221

221222
AbiTupleType ts ->
222223
AbiTuple <$> getAbiSeq (Vector.length ts) (Vector.toList ts)
@@ -228,7 +229,7 @@ putAbi :: AbiValue -> Put
228229
putAbi = \case
229230
AbiUInt _ x ->
230231
forM_ (reverse [0 .. 7]) $ \i ->
231-
putWord32be (fromIntegral (shiftR x (i * 32) .&. 0xffffffff))
232+
putWord32be (unsafeInto (shiftR x (i * 32) .&. 0xffffffff))
232233

233234
AbiInt n x -> putAbi (AbiUInt n (fromIntegral x))
234235
AbiAddress x -> putAbi (AbiUInt 160 (fromIntegral x))
@@ -240,7 +241,7 @@ putAbi = \case
240241

241242
AbiBytesDynamic xs -> do
242243
let n = BS.length xs
243-
putAbi (AbiUInt 256 (fromIntegral n))
244+
putAbi (AbiUInt 256 (unsafeInto n))
244245
putAbi (AbiBytes n xs)
245246

246247
AbiString s ->
@@ -325,7 +326,7 @@ putAbiSeq xs =
325326
case abiKind (abiValueType x) of
326327
Static -> do putAbi x
327328
putHeads offset xs'
328-
Dynamic -> do putAbi (AbiUInt 256 (fromIntegral offset))
329+
Dynamic -> do putAbi (AbiUInt 256 (unsafeInto offset))
329330
putHeads (offset + abiTailSize x) xs'
330331

331332
encodeAbiValue :: AbiValue -> BS.ByteString
@@ -387,7 +388,7 @@ basicType v =
387388
pack32 :: Int -> [Word32] -> Word256
388389
pack32 n xs =
389390
sum [ shiftL x ((n - i) * 32)
390-
| (x, i) <- zip (map fromIntegral xs) [1..] ]
391+
| (x, i) <- zip (map into xs) [1..] ]
391392

392393
asUInt :: Integral i => Int -> (i -> a) -> Get a
393394
asUInt n f = y <$> getAbi (AbiUIntType n)
@@ -508,7 +509,7 @@ parseAbiValue :: AbiType -> ReadP AbiValue
508509
parseAbiValue (AbiUIntType n) = do W256 w <- readS_to_P reads
509510
pure $ AbiUInt n w
510511
parseAbiValue (AbiIntType n) = do W256 w <- readS_to_P reads
511-
pure $ AbiInt n (num w)
512+
pure $ AbiInt n (unsafeInto w)
512513
parseAbiValue AbiAddressType = AbiAddress <$> readS_to_P reads
513514
parseAbiValue AbiBoolType = (do W256 w <- readS_to_P reads
514515
pure $ AbiBool (w /= 0))
@@ -570,7 +571,7 @@ decodeBuf tps buf
570571

571572
decodeStaticArgs :: Int -> Int -> Expr Buf -> [Expr EWord]
572573
decodeStaticArgs offset numArgs b =
573-
[readWord (Lit . num $ i) b | i <- [offset,(offset+32) .. (offset + (numArgs-1)*32)]]
574+
[readWord (Lit . unsafeInto $ i) b | i <- [offset,(offset+32) .. (offset + (numArgs-1)*32)]]
574575

575576

576577
-- A modification of 'arbitrarySizedBoundedIntegral' quickcheck library

src/EVM/Concrete.hs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import Data.ByteString (ByteString, (!?))
88
import Data.ByteString qualified as BS
99
import Data.Maybe (fromMaybe)
1010
import Data.Word (Word8)
11+
import Witch (unsafeInto)
1112

1213
wordAt :: Int -> ByteString -> W256
1314
wordAt i bs =
@@ -28,23 +29,23 @@ byteStringSliceWithDefaultZeroes offset size bs =
2829
in bs' <> BS.replicate (size - BS.length bs') 0
2930

3031

31-
sliceMemory :: (Integral a, Integral b) => a -> b -> ByteString -> ByteString
32+
sliceMemory :: W256 -> W256 -> ByteString -> ByteString
3233
sliceMemory o s =
33-
byteStringSliceWithDefaultZeroes (num o) (num s)
34+
byteStringSliceWithDefaultZeroes (unsafeInto o) (unsafeInto s)
3435

3536
writeMemory :: ByteString -> W256 -> W256 -> W256 -> ByteString -> ByteString
3637
writeMemory bs1 n src dst bs0 =
3738
let
38-
(a, b) = BS.splitAt (num dst) bs0
39-
a' = BS.replicate (num dst - BS.length a) 0
39+
(a, b) = BS.splitAt (unsafeInto dst) bs0
40+
a' = BS.replicate (unsafeInto dst - BS.length a) 0
4041
-- sliceMemory should work for both cases, but we are using 256 bit
4142
-- words, whereas ByteString is only defined up to 64 bit. For large n,
4243
-- src, dst this will cause problems (often in GeneralStateTests).
4344
-- Later we could reimplement ByteString for 256 bit arguments.
44-
c = if src > num (BS.length bs1)
45-
then BS.replicate (num n) 0
45+
c = if src > unsafeInto (BS.length bs1)
46+
then BS.replicate (unsafeInto n) 0
4647
else sliceMemory src n bs1
47-
b' = BS.drop (num n) b
48+
b' = BS.drop (unsafeInto n) b
4849
in
4950
a <> a' <> c <> b'
5051

@@ -64,8 +65,8 @@ x0 ^ y0 | y0 < 0 = errorWithoutStackTrace "Negative exponent"
6465
| otherwise = g (x * x) ((y - 1) `shiftR` 1) (x * z)
6566

6667
createAddress :: Addr -> W256 -> Addr
67-
createAddress a n = num $ keccak' $ rlpList [rlpAddrFull a, rlpWord256 n]
68+
createAddress a n = unsafeInto $ keccak' $ rlpList [rlpAddrFull a, rlpWord256 n]
6869

6970
create2Address :: Addr -> W256 -> ByteString -> Addr
70-
create2Address a s b = num $ keccak' $ mconcat
71+
create2Address a s b = unsafeInto $ keccak' $ mconcat
7172
[BS.singleton 0xff, word160Bytes a, word256Bytes s, word256Bytes $ keccak' b]

src/EVM/Dapp.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import Data.Sequence qualified as Seq
1919
import Data.Text (Text, isPrefixOf, pack, unpack)
2020
import Data.Text.Encoding (encodeUtf8)
2121
import Data.Vector qualified as V
22+
import Witch (unsafeInto)
2223

2324
data DappInfo = DappInfo
2425
{ root :: FilePath
@@ -173,7 +174,7 @@ lookupCode (RuntimeCode (SymbolicRuntimeCode c)) a = let
173174
compareCode :: ByteString -> Code -> Bool
174175
compareCode raw (Code template locs) =
175176
let holes' = sort [(start, len) | (Reference start len) <- locs]
176-
insert at' len' bs = writeMemory (BS.replicate len' 0) (fromIntegral len') 0 (fromIntegral at') bs
177+
insert at' len' bs = writeMemory (BS.replicate len' 0) (unsafeInto len') 0 (unsafeInto at') bs
177178
refined = foldr (\(start, len) acc -> insert start len acc) raw holes'
178179
in BS.length raw == BS.length template && template == refined
179180

src/EVM/Debug.hs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module EVM.Debug where
33
import EVM (bytecode)
44
import EVM.Expr (bufLength)
55
import EVM.Solidity (SrcMap(..), SourceCache(..))
6-
import EVM.Types (Contract, Addr)
6+
import EVM.Types (Contract(..), Addr)
77

88
import Control.Arrow (second)
99
import Data.ByteString (ByteString)
@@ -12,6 +12,7 @@ import Data.Map (Map)
1212
import Data.Map qualified as Map
1313
import Optics.Core
1414
import Text.PrettyPrint.ANSI.Leijen
15+
import Witch (unsafeInto)
1516

1617
data Mode = Debug | Run | JsonTrace deriving (Eq, Show)
1718

@@ -27,9 +28,9 @@ prettyContract :: Contract -> Doc
2728
prettyContract c =
2829
object
2930
[ (text "codesize", text . show $ (bufLength (c ^. bytecode)))
30-
, (text "codehash", text (show (c ^. #codehash)))
31-
, (text "balance", int (fromIntegral (c ^. #balance)))
32-
, (text "nonce", int (fromIntegral (c ^. #nonce)))
31+
, (text "codehash", text (show c.codehash))
32+
, (text "balance", int (unsafeInto c.balance))
33+
, (text "nonce", int (unsafeInto c.nonce))
3334
]
3435

3536
prettyContracts :: Map Addr Contract -> Doc

src/EVM/Dev.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import EVM.SymExec
3030
import EVM.Types
3131
import EVM.UnitTest
3232
import GHC.Conc
33+
import Witch (unsafeInto)
3334

3435
checkEquiv :: (Typeable a) => Expr a -> Expr a -> IO ()
3536
checkEquiv a b = withSolvers Z3 1 Nothing $ \s -> do
@@ -40,7 +41,7 @@ checkEquiv a b = withSolvers Z3 1 Nothing $ \s -> do
4041
runDappTest :: FilePath -> IO ()
4142
runDappTest root =
4243
withCurrentDirectory root $ do
43-
cores <- num <$> getNumProcessors
44+
cores <- unsafeInto <$> getNumProcessors
4445
let testFile = root <> "/out/dapp.sol.json"
4546
Right (BuildOutput contracts _) <- readSolc DappTools root testFile
4647
withSolvers Z3 cores Nothing $ \solvers -> do

0 commit comments

Comments
 (0)