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

Abstract gas v2 #403

Closed
wants to merge 14 commits into from
Closed

Abstract gas v2 #403

wants to merge 14 commits into from

Conversation

d-xo
Copy link
Collaborator

@d-xo d-xo commented Oct 3, 2023

Description

This is a second attempt at implementing a partially abstract gas model, based on top of some experiments from @arcz. Instead of modelling gas as a Maybe Word64 (as in #352), here we add a gas parameter to the VM type and constrain it to be an instance of the Gas typeclass.

Two instances of this class are provided:

  1. Word64: this exactly matches the previous concrete gas semantics
  2. SymGas: this is a single constructor type and simply acts as a marker that we should ignore all gas accounting

This (combined with a bunch of specilialise and inline pragmas) should keep the overhead for concrete execution relatively low. There are still four failing ethereum tests that need to be fixed before I can confirm this hypothesis for sure, but initial tests by @arcz on an earlier commit seemed promising.

Compared to #352 this is maybe not quite as clean (our type parameters continue to proliferate), and it does allow more invalid vm states (e.g. as implemented here the combination of concrete gas with symbolic VM state will output garbage numbers for gas), but it's probably worth it if it minimizes the impact to concrete perf.

As mentioned, there are still a few failing tests, and I want to make another pass to cleanup / better organise the code here before it's ready to merge. I may also try deleting gas entirely just to get a feel for exactly how much nicer the code would be without it...

Checklist

  • tested locally
  • added automated tests
  • updated the docs
  • updated the changelog

@d-xo d-xo requested review from arcz and msooseth October 3, 2023 17:15
@d-xo
Copy link
Collaborator Author

d-xo commented Oct 6, 2023

All tests pass now, but perf unfortunately seems to be very similar to the V1 attempt, running cabal run bench -- -p "blockchain-tests" gives:

main: 33.712 s ± 199 ms
here: 46.897 s ± 462 ms

@d-xo d-xo mentioned this pull request Oct 6, 2023
4 tasks
@d-xo d-xo marked this pull request as ready for review October 6, 2023 13:57
src/EVM/Expr.hs Outdated
@@ -374,13 +374,13 @@ writeByte offset byte src = WriteByte offset byte src

writeWord :: Expr EWord -> Expr EWord -> Expr Buf -> Expr Buf
writeWord o@(Lit offset) (WAddr (LitAddr val)) b@(ConcreteBuf _)
| offset + 32 < maxBytes
| offset < maxBytes
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this an accidental change?

@@ -648,8 +648,8 @@ data FrameContext
| CallContext
{ target :: Expr EAddr
, context :: Expr EAddr
, offset :: W256
, size :: W256
, offset :: Expr EWord
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not strictly necessary, right? Or is it? I am just curious.

@@ -681,7 +681,7 @@ data FrameState s = FrameState
, calldata :: Expr Buf
, callvalue :: Expr EWord
, caller :: Expr EAddr
, gas :: {-# UNPACK #-} !Word64
, gas :: !gas
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change above had

{-# UNPACK #-} !gas

Wouldn't that be faster?

Copy link
Collaborator

@msooseth msooseth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, except for 2 things that I am a bit curious about :)

@arcz arcz mentioned this pull request Nov 17, 2023
4 tasks
@d-xo
Copy link
Collaborator Author

d-xo commented Nov 24, 2023

added the magic compiler options that @arcz uncovered in #427 and now this benchmarks exactly the same as main!

main: 37.372 s ± 1.63 s
here: 36.147 s ± 1.55 s

@msooseth
Copy link
Collaborator

msooseth commented Nov 24, 2023 via email

@msooseth
Copy link
Collaborator

Yay, Abstract Gasv3 has been merged!! See: #427 So I am closing this :)

@msooseth msooseth closed this Jan 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants