Skip to content

Commit

Permalink
hevm update
Browse files Browse the repository at this point in the history
  • Loading branch information
zoep committed Sep 17, 2024
1 parent 84be0ad commit 20a65e7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/Act/HEVM.hs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ translateConstructor bytecode (Constructor _ iface preconds _ _ upds) = do
calldata = makeCtrCalldata iface
initcontract = EVM.C { EVM.code = EVM.RuntimeCode (EVM.ConcreteRuntimeCode bytecode)
, EVM.storage = EVM.ConcreteStore mempty
, EVM.tStorage = EVM.ConcreteStore mempty
, EVM.balance = EVM.Lit 0
, EVM.nonce = Just 1
}
Expand Down Expand Up @@ -212,12 +213,12 @@ applyUpdate readMap writeMap (Update typ (Item _ _ ref) e) = do
where

updateStorage :: (EVM.Expr EVM.Storage -> EVM.Expr EVM.Storage) -> EVM.Expr EVM.EContract -> EVM.Expr EVM.EContract
updateStorage updfun (EVM.C code storage bal nonce) = EVM.C code (updfun storage) bal nonce
updateStorage updfun (EVM.C code storage tstorage bal nonce) = EVM.C code (updfun storage) tstorage bal nonce
updateStorage _ (EVM.GVar _) = error "Internal error: contract cannot be a global variable"

updateNonce :: EVM.Expr EVM.EContract -> EVM.Expr EVM.EContract
updateNonce (EVM.C code storage bal (Just n)) = EVM.C code storage bal (Just (n + 1))
updateNonce c@(EVM.C _ _ _ Nothing) = c
updateNonce (EVM.C code storage tstorage bal (Just n)) = EVM.C code storage tstorage bal (Just (n + 1))
updateNonce c@(EVM.C _ _ _ _ Nothing) = c
updateNonce (EVM.GVar _) = error "Internal error: contract cannot be a global variable"

createContract :: ContractMap -> ContractMap -> EVM.Expr EVM.EAddr -> Exp AContract -> ActM ContractMap
Expand All @@ -227,6 +228,7 @@ createContract readMap writeMap freshAddr (Create _ cid args) = do
Just (Contract (Constructor _ iface _ _ _ upds) _, _, bytecode) -> do
let contract = EVM.C { EVM.code = EVM.RuntimeCode (EVM.ConcreteRuntimeCode bytecode)
, EVM.storage = EVM.ConcreteStore mempty
, EVM.tStorage = EVM.ConcreteStore mempty
, EVM.balance = EVM.Lit 0
, EVM.nonce = Just 1
}
Expand Down Expand Up @@ -369,7 +371,7 @@ refAddr :: ContractMap -> StorageRef -> ActM (EVM.Expr EVM.EAddr)
refAddr cmap (SVar _ c x) = do
caddr <- getCaddr
case M.lookup caddr cmap of
Just (EVM.C _ storage _ _) -> do
Just (EVM.C _ storage _ _ _) -> do
layout <- getLayout
let slot = EVM.Lit $ fromIntegral $ getSlot layout c x
case simplify (EVM.SLoad slot storage) of
Expand All @@ -381,7 +383,7 @@ refAddr cmap (SField _ ref c x) = do
layout <- getLayout
caddr' <- refAddr cmap ref
case M.lookup caddr' cmap of
Just (EVM.C _ storage _ _) -> do
Just (EVM.C _ storage _ _ _) -> do
let slot = EVM.Lit $ fromIntegral $ getSlot layout c x
case simplify (EVM.SLoad slot storage) of
EVM.WAddr symaddr -> pure symaddr
Expand Down Expand Up @@ -528,7 +530,7 @@ toExpr cmap = liftM stripMods . go
caddr' <- baseAddr cmap ref
let contract = fromMaybe (error "Internal error: contract not found") $ M.lookup caddr' cmap
let storage = case contract of
EVM.C _ s _ _ -> s
EVM.C _ s _ _ _ -> s
EVM.GVar _ -> error "Internal error: contract cannot be a global variable"
pure $ EVM.SLoad slot storage

Expand Down Expand Up @@ -638,13 +640,14 @@ createStorage cmap =
traverseStorage _ _ = error "Internal error: unexpected storage shape"

makeContract :: EVM.Expr EVM.EAddr -> EVM.Expr EVM.EContract -> EVM.Expr EVM.EContract
makeContract addr (EVM.C code storage _ _) = EVM.C code (traverseStorage addr storage) (EVM.Balance addr) (Just 0)
makeContract addr (EVM.C code storage tstorage _ _) = EVM.C code (traverseStorage addr storage) tstorage (EVM.Balance addr) (Just 0)
makeContract _ (EVM.GVar _) = error "Internal error: contract cannot be gvar"

toContract :: EVM.Expr EVM.EContract -> EVM.Contract
toContract (EVM.C code storage balance nonce) = EVM.Contract
toContract (EVM.C code storage tstorage balance nonce) = EVM.Contract
{ EVM.code = code
, EVM.storage = storage
, EVM.tStorage = tstorage
, EVM.origStorage = storage
, EVM.balance = balance
, EVM.nonce = nonce
Expand Down
2 changes: 2 additions & 0 deletions src/Act/HEVM_utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,6 @@ loadSymVM (entryaddr, entrycontract) othercontracts callvalue cd create =
, create = create
, txAccessList = mempty
, allowFFI = False
, freshAddresses = 0
, beaconRoot = 0
})

0 comments on commit 20a65e7

Please sign in to comment.