Skip to content

Commit 8c25cb6

Browse files
committed
EVM: Introduce deafult VMOpts
This value represents decent choice for the VMOpts fields. It can be easily customized using record update syntax. This should make it easy if we need to introduce a new field there.
1 parent 17e0724 commit 8c25cb6

File tree

9 files changed

+53
-179
lines changed

9 files changed

+53
-179
lines changed

bench/bench-perf.hs

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import EVM.Types
2020
import EVM.UnitTest
2121
import Test.Tasty.Bench
2222
import Control.Monad.ST
23-
import EVM.FeeSchedule (feeSchedule)
2423

2524
-- benchmark hevm using tasty-bench
2625

@@ -38,36 +37,10 @@ vm0 :: Contract -> ST RealWorld (VM Concrete)
3837
vm0 c = makeVm $ vm0Opts c
3938

4039
vm0Opts :: Contract -> VMOpts Concrete
41-
vm0Opts c =
42-
VMOpts
43-
{ contract = c,
44-
calldata = (ConcreteBuf "", []),
45-
value = Lit 0xfffffffffffff, -- balance
46-
baseState = EmptyBase,
47-
address = LitAddr 0xacab,
48-
caller = LitAddr 0,
49-
origin = LitAddr 0,
50-
gas = 0xffffffffffffffff,
51-
baseFee = 0,
52-
priorityFee = 0,
53-
gaslimit = 0xffffffffffffffff,
54-
coinbase = LitAddr 0,
55-
number = Lit 0,
56-
timestamp = Lit 0,
57-
blockGaslimit = 0xffffffffffffffff,
58-
gasprice = 0,
59-
maxCodeSize = 0xffffffff,
60-
prevRandao = 0,
61-
schedule = feeSchedule,
62-
chainId = 1,
63-
create = False,
64-
txAccessList = mempty, -- TODO: support me soon
65-
allowFFI = False,
66-
otherContracts = [],
67-
freshAddresses = 0,
68-
beaconRoot = 0,
69-
parentHash = 0
70-
}
40+
vm0Opts c = defaultVMOpts
41+
{ contract = c,
42+
value = Lit 0xfffffffffffff -- balance
43+
}
7144

7245
vmOptsToTestVMParams :: VMOpts Concrete -> TestVMParams
7346
vmOptsToTestVMParams v =

cli/cli.hs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,11 @@ import Data.List.Split (splitOn)
3939
import Text.Read (readMaybe)
4040
import JSONL (jsonlBuilder, jsonLine)
4141

42-
import EVM (initialContract, abstractContract, makeVm)
42+
import EVM (initialContract, abstractContract, makeVm, defaultVMOpts)
4343
import EVM.ABI (Sig(..))
4444
import EVM.Dapp (dappInfo, DappInfo, emptyDapp)
4545
import EVM.Expr qualified as Expr
4646
import EVM.Concrete qualified as Concrete
47-
import EVM.FeeSchedule (feeSchedule)
4847
import EVM.Fetch qualified as Fetch
4948
import EVM.Format (hexByteString, strip0x, formatExpr, indent)
5049
import EVM.Solidity
@@ -708,9 +707,8 @@ vmFromCommand cOpts cExecOpts cFileOpts execOpts sess = do
708707
then addr (.address) (Concrete.createAddress (fromJust $ maybeLitAddrSimp origin) (W64 $ word64 (.nonce) 0))
709708
else addr (.address) (LitAddr 0xacab)
710709

711-
vm0 baseFee miner ts blockNum prevRan c = makeVm $ VMOpts
710+
vm0 baseFee miner ts blockNum prevRan c = makeVm $ (defaultVMOpts @Concrete)
712711
{ contract = c
713-
, otherContracts = []
714712
, calldata = (calldata, [])
715713
, value = Lit val
716714
, address = address
@@ -727,15 +725,8 @@ vmFromCommand cOpts cExecOpts cFileOpts execOpts sess = do
727725
, gasprice = word (.gasprice) 0
728726
, maxCodeSize = word (.maxcodesize) 0xffffffff
729727
, prevRandao = word (.prevRandao) prevRan
730-
, schedule = feeSchedule
731728
, chainId = word (.chainid) 1
732729
, create = (.create) execOpts
733-
, baseState = EmptyBase
734-
, txAccessList = mempty -- TODO: support me soon
735-
, allowFFI = False
736-
, freshAddresses = 0
737-
, beaconRoot = 0
738-
, parentHash = 0
739730
}
740731
word f def = fromMaybe def (f cExecOpts)
741732
word64 f def = fromMaybe def (f cExecOpts)
@@ -815,15 +806,14 @@ symvmFromCommand cExecOpts sOpts cFileOpts sess calldata = do
815806
address = eaddr (.address) (SymAddr "entrypoint")
816807
originAddr = eaddr (.origin) (SymAddr "origin")
817808
originContr = abstractContract (RuntimeCode (SymbolicRuntimeCode mempty)) originAddr
818-
vm0 baseFee miner ts blockNum prevRan cd callvalue caller c baseState = makeVm $ VMOpts
809+
vm0 baseFee miner ts blockNum prevRan cd callvalue caller c baseState = makeVm $ defaultVMOpts
819810
{ contract = c
820811
, otherContracts = [(originAddr, originContr)]
821812
, calldata = cd
822813
, value = callvalue
823814
, address = address
824815
, caller = caller
825816
, origin = origin
826-
, gas = ()
827817
, gaslimit = word64 (.gaslimit) 0xffffffffffffffff
828818
, baseFee = baseFee
829819
, priorityFee = word (.priorityFee) 0
@@ -834,15 +824,9 @@ symvmFromCommand cExecOpts sOpts cFileOpts sess calldata = do
834824
, gasprice = word (.gasprice) 0
835825
, maxCodeSize = word (.maxcodesize) 0xffffffff
836826
, prevRandao = word (.prevRandao) prevRan
837-
, schedule = feeSchedule
838827
, chainId = word (.chainid) 1
839828
, create = (.create) sOpts
840829
, baseState = baseState
841-
, txAccessList = mempty
842-
, allowFFI = False
843-
, freshAddresses = 0
844-
, beaconRoot = 0
845-
, parentHash = 0
846830
}
847831
word f def = fromMaybe def (f cExecOpts)
848832
word64 f def = fromMaybe def (f cExecOpts)

src/EVM.hs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import EVM.Expr (readStorage, concStoreContains, writeStorage, readByte, readWor
1515
writeByte, bufLength, indexWord, readBytes, copySlice, wordToAddr, maybeLitByteSimp, maybeLitWordSimp, maybeLitAddrSimp)
1616
import EVM.Expr qualified as Expr
1717
import EVM.FeeSchedule (FeeSchedule (..))
18+
import EVM.FeeSchedule qualified as Fees (feeSchedule)
1819
import EVM.Op
1920
import EVM.Precompiled qualified
2021
import EVM.Solidity
@@ -66,6 +67,37 @@ import Crypto.Hash (Digest, SHA256, RIPEMD160)
6667
import Crypto.Hash qualified as Crypto
6768
import Crypto.Number.ModArithmetic (expFast)
6869

70+
defaultVMOpts :: VMOps t => VMOpts t
71+
defaultVMOpts = VMOpts
72+
{ contract = emptyContract
73+
, otherContracts = []
74+
, calldata = mempty
75+
, value = Lit 0
76+
, address = LitAddr 0xacab
77+
, caller = LitAddr 0
78+
, origin = LitAddr 0
79+
, gas = toGas maxBound
80+
, gaslimit = maxBound
81+
, baseFee = 0
82+
, priorityFee = 0
83+
, coinbase = LitAddr 0
84+
, number = Lit 0
85+
, timestamp = Lit 0
86+
, blockGaslimit = maxBound
87+
, gasprice = 0
88+
, maxCodeSize = 0xffffffff
89+
, prevRandao = 0
90+
, schedule = Fees.feeSchedule
91+
, chainId = 1
92+
, create = False
93+
, baseState = EmptyBase
94+
, txAccessList = mempty
95+
, allowFFI = False
96+
, freshAddresses = 0
97+
, beaconRoot = 0
98+
, parentHash = 0
99+
}
100+
69101
blankState :: VMOps t => ST RealWorld (FrameState t)
70102
blankState = do
71103
memory <- ConcreteMemory <$> VS.Mutable.new 0

src/EVM/Exec.hs

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ module EVM.Exec where
22

33
import EVM hiding (createAddress)
44
import EVM.Concrete (createAddress)
5-
import EVM.FeeSchedule (feeSchedule)
65
import EVM.Types
76

87
import Control.Monad.Trans.State.Strict (get, State)
@@ -18,34 +17,13 @@ ethrunAddress = Addr 0x00a329c0648769a73afac7f9381e08fb43dbea72
1817

1918
vmForEthrunCreation :: VMOps t => ByteString -> ST RealWorld (VM t)
2019
vmForEthrunCreation creationCode =
21-
(makeVm $ VMOpts
20+
(makeVm $ defaultVMOpts
2221
{ contract = initialContract (InitCode creationCode mempty)
23-
, otherContracts = []
24-
, calldata = mempty
25-
, value = Lit 0
26-
, baseState = EmptyBase
2722
, address = createAddress ethrunAddress 1
2823
, caller = LitAddr ethrunAddress
2924
, origin = LitAddr ethrunAddress
30-
, coinbase = LitAddr 0
31-
, number = Lit 0
32-
, timestamp = Lit 0
3325
, blockGaslimit = 0
34-
, gasprice = 0
3526
, prevRandao = 42069
36-
, gas = toGas 0xffffffffffffffff
37-
, gaslimit = 0xffffffffffffffff
38-
, baseFee = 0
39-
, priorityFee = 0
40-
, maxCodeSize = 0xffffffff
41-
, schedule = feeSchedule
42-
, chainId = 1
43-
, create = False
44-
, txAccessList = mempty
45-
, allowFFI = False
46-
, freshAddresses = 0
47-
, beaconRoot = 0
48-
, parentHash = 0
4927
}) <&> set (#env % #contracts % at (LitAddr ethrunAddress))
5028
(Just (initialContract (RuntimeCode (ConcreteRuntimeCode ""))))
5129

src/EVM/SymExec.hs

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,12 @@ import Options.Generic (ParseField, ParseFields, ParseRecord)
4848
import Text.Printf (printf)
4949
import Witch (into, unsafeInto)
5050

51-
import EVM (makeVm, abstractContract, initialContract, getCodeLocation, isValidJumpDest)
51+
import EVM (makeVm, abstractContract, initialContract, getCodeLocation, isValidJumpDest, defaultVMOpts)
5252
import EVM.Exec (exec)
5353
import EVM.Fetch qualified as Fetch
5454
import EVM.ABI
5555
import EVM.Effects
5656
import EVM.Expr qualified as Expr
57-
import EVM.FeeSchedule (feeSchedule)
5857
import EVM.Format (formatExpr, formatPartial, formatPartialDetailed, showVal, indent, formatBinary, formatProp, formatState, formatError)
5958
import EVM.SMT qualified as SMT
6059
import EVM.Solvers (SolverGroup, checkSatWithProps)
@@ -234,34 +233,16 @@ loadEmptySymVM
234233
-> (Expr Buf, [Prop])
235234
-> ST RealWorld (VM Symbolic)
236235
loadEmptySymVM x callvalue cd =
237-
(makeVm $ VMOpts
236+
(makeVm $ defaultVMOpts
238237
{ contract = initialContract x
239-
, otherContracts = []
240238
, calldata = cd
241239
, value = callvalue
242-
, baseState = EmptyBase
243240
, address = SymAddr "entrypoint"
244241
, caller = SymAddr "caller"
245242
, origin = SymAddr "origin"
246243
, coinbase = SymAddr "coinbase"
247-
, number = Lit 0
248-
, timestamp = Lit 0
249244
, blockGaslimit = 0
250-
, gasprice = 0
251245
, prevRandao = 42069
252-
, gas = ()
253-
, gaslimit = 0xffffffffffffffff
254-
, baseFee = 0
255-
, priorityFee = 0
256-
, maxCodeSize = 0xffffffff
257-
, schedule = feeSchedule
258-
, chainId = 1
259-
, create = False
260-
, txAccessList = mempty
261-
, allowFFI = False
262-
, freshAddresses = 0
263-
, beaconRoot = 0
264-
, parentHash = 0
265246
})
266247

267248
-- Creates a symbolic VM that has symbolic storage, unlike loadEmptySymVM
@@ -272,34 +253,18 @@ loadSymVM
272253
-> Bool
273254
-> ST RealWorld (VM Symbolic)
274255
loadSymVM x callvalue cd create =
275-
(makeVm $ VMOpts
256+
(makeVm $ defaultVMOpts
276257
{ contract = if create then initialContract x else abstractContract x (SymAddr "entrypoint")
277-
, otherContracts = []
278258
, calldata = cd
279259
, value = callvalue
280260
, baseState = AbstractBase
281261
, address = SymAddr "entrypoint"
282262
, caller = SymAddr "caller"
283263
, origin = SymAddr "origin"
284264
, coinbase = SymAddr "coinbase"
285-
, number = Lit 0
286-
, timestamp = Lit 0
287265
, blockGaslimit = 0
288-
, gasprice = 0
289266
, prevRandao = 42069
290-
, gas = ()
291-
, gaslimit = 0xffffffffffffffff
292-
, baseFee = 0
293-
, priorityFee = 0
294-
, maxCodeSize = 0xffffffff
295-
, schedule = feeSchedule
296-
, chainId = 1
297267
, create = create
298-
, txAccessList = mempty
299-
, allowFFI = False
300-
, freshAddresses = 0
301-
, beaconRoot = 0
302-
, parentHash = 0
303268
})
304269

305270
-- freezes any mutable refs, making it safe to share between threads

src/EVM/UnitTest.hs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import EVM.Dapp
1111
import EVM.Effects
1212
import EVM.Exec
1313
import EVM.Expr qualified as Expr
14-
import EVM.FeeSchedule (feeSchedule)
1514
import EVM.Fetch qualified as Fetch
1615
import EVM.Format
1716
import EVM.Solidity
@@ -524,13 +523,10 @@ makeTxCall params (cd, cdProps) = do
524523
vm <- get
525524
put $ initTx vm
526525

527-
initialUnitTestVm :: VMOps t => UnitTestOptions -> SolcContract -> ST RealWorld (VM t)
526+
initialUnitTestVm :: forall t. VMOps t => UnitTestOptions -> SolcContract -> ST RealWorld (VM t)
528527
initialUnitTestVm (UnitTestOptions {..}) theContract = do
529-
vm <- makeVm $ VMOpts
528+
vm <- makeVm $ (defaultVMOpts @t)
530529
{ contract = initialContract (InitCode theContract.creationCode mempty)
531-
, otherContracts = []
532-
, calldata = mempty
533-
, value = Lit 0
534530
, address = testParams.address
535531
, caller = testParams.caller
536532
, origin = testParams.origin
@@ -545,15 +541,9 @@ initialUnitTestVm (UnitTestOptions {..}) theContract = do
545541
, priorityFee = testParams.priorityFee
546542
, maxCodeSize = testParams.maxCodeSize
547543
, prevRandao = testParams.prevrandao
548-
, schedule = feeSchedule
549544
, chainId = testParams.chainId
550545
, create = True
551-
, baseState = EmptyBase
552-
, txAccessList = mempty -- TODO: support unit test access lists???
553546
, allowFFI = ffiAllowed
554-
, freshAddresses = 0
555-
, beaconRoot = 0
556-
, parentHash = 0
557547
}
558548
let creator =
559549
initialContract (RuntimeCode (ConcreteRuntimeCode ""))

test/EVM/ConcreteExecution/ConcreteExecutionTests.hs

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,13 @@ import Data.Vector qualified as V
1414

1515
import EVM.ABI
1616
import EVM.Effects qualified as Effects
17-
import EVM.FeeSchedule
1817
import EVM.Fetch qualified as Fetch
1918
import EVM.Solidity (solcRuntime)
2019
import EVM.Solvers (defMemLimit)
2120
import EVM.Stepper qualified
2221
import EVM.Transaction (initTx)
2322
import EVM.Types
24-
import EVM (initialContract, makeVm)
23+
import EVM (initialContract, makeVm, defaultVMOpts)
2524
import Test.Tasty
2625
import Test.Tasty.HUnit
2726

@@ -42,34 +41,9 @@ executeSingleCall sourceCode contractName functionName abiArgs = do
4241
let functionSignature = functionName <> "(" <> T.intercalate "," (map (abiTypeSolidity . abiValueType) abiArgs) <> ")"
4342
let callData = abiMethod functionSignature (AbiTuple $ V.fromList abiArgs)
4443
let contractWithCode = initialContract (RuntimeCode $ ConcreteRuntimeCode runtimeCode)
45-
initialVM <- stToIO $ makeVm $ VMOpts
46-
{ contract = contractWithCode
47-
, otherContracts = []
48-
, calldata = (ConcreteBuf callData, [])
49-
, value = Lit 0
50-
, address = (LitAddr 0xacab)
51-
, caller = (LitAddr 0)
52-
, origin = (LitAddr 0)
53-
, gas = maxBound
54-
, baseFee = 0
55-
, priorityFee = 0
56-
, gaslimit = maxBound
57-
, coinbase = (LitAddr 0)
58-
, number = Lit 0
59-
, timestamp = Lit 0
60-
, blockGaslimit = maxBound
61-
, gasprice = 0
62-
, maxCodeSize = maxBound
63-
, prevRandao = 0
64-
, schedule = feeSchedule
65-
, chainId = 1
66-
, create = False
67-
, baseState = EmptyBase
68-
, txAccessList = mempty
69-
, allowFFI = False
70-
, freshAddresses = 0
71-
, beaconRoot = 0
72-
, parentHash = 0
44+
initialVM <- stToIO $ makeVm $ defaultVMOpts
45+
{ contract = contractWithCode
46+
, calldata = (ConcreteBuf callData, [])
7347
}
7448
let withInitializedTransactionVM = EVM.Transaction.initTx initialVM
7549
let fetcher = Fetch.zero 0 Nothing defMemLimit

0 commit comments

Comments
 (0)