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

article: Efficient gas calculation algorithm for EVM #123

Merged
merged 3 commits into from
Nov 13, 2019
Merged

Conversation

chfast
Copy link
Member

@chfast chfast commented Aug 13, 2019

TODO:

  • Add link to the article in README

@codecov-io
Copy link

codecov-io commented Aug 13, 2019

Codecov Report

❗ No coverage uploaded for pull request base (master@3d90b52). Click here to learn what that means.
The diff coverage is n/a.

@@            Coverage Diff            @@
##             master     #123   +/-   ##
=========================================
  Coverage          ?   99.88%           
=========================================
  Files             ?        5           
  Lines             ?      839           
  Branches          ?      109           
=========================================
  Hits              ?      838           
  Misses            ?        1           
  Partials          ?        0

A _basic block_ is a sequence of instructions that are executed "straight-line"
without being interrupted by jumps. I.e. they are nodes in the _control flow graph_.

The name "basic block" has been taken from LLVM. Other names are: just "block"
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't this just the generic term in compiler theory?

Copy link
Member Author

Choose a reason for hiding this comment

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

I never had proper education in this field. If the term is well known, I should simplify this paragraph.

Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure about the well-known-ness, but llvm certainly did not introduce it :)

Copy link

Choose a reason for hiding this comment

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

I'd prefer if you called it 'basic segment', and talk about segments. Because we're talking about blockchains here, and in extension maybe doing paralell gas-calculation of future blocks, and things become a bit messy if we have two definitions for 'block'.

Copy link
Member Author

Choose a reason for hiding this comment

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

This is pretty concrete term: https://en.wikipedia.org/wiki/Basic_block.

Maybe "basic instruction block" then?

- `RETURN`,
- `REVERT`,
- `SELFDESTRUCT`,
- an instruction directly preceding `JUMPDEST`.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
- an instruction directly preceding `JUMPDEST`.
- the last instruction in the program
- an invalid instruction

Copy link
Member Author

Choose a reason for hiding this comment

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

We actually ignore invalid instructions by assuming they cost 0.

- an instruction directly preceding `JUMPDEST`.

2. The following instructions _start_ a basic block:
- `JUMPDEST`,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
- `JUMPDEST`,

(this should be redundant together with the two rules below)

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't believe this is true. You can have a JUMPDEST anywhere.

Copy link
Contributor

Choose a reason for hiding this comment

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

sure, but you say: a basic block starts right before ... an instruction directly after an intruction that has ended the previous basic block. And above: a basic block ends after ... an instruction directly preceding "jumpdest". Maybe just nitpicking :)

Copy link

Choose a reason for hiding this comment

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

I'm wondering about the attackability of this -- in hte case where a contract consists of 24k JUMPDESTs. I guess that's up to the implementation.

Copy link
Member Author

Choose a reason for hiding this comment

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

I have a test/benchmark for this one. Other variations about it are definitely needed.

evmone's top speed is ~1Ggas/s. In this case where we do gas/stack checking for every instruction the execution is ~0.5Ggas/s.

The bigger problem is the analysis of this code takes ~0.1 ms what means it should cost ~1000 gas. This is the biggest concern so far.

chfast and others added 3 commits November 13, 2019 11:38
Co-Authored-By: chriseth <chris@ethereum.org>
Co-Authored-By: Andrei Maiboroda <andrei@ethereum.org>
@chfast chfast merged commit 8f1c340 into master Nov 13, 2019
@chfast chfast deleted the docs_gas_algo branch November 13, 2019 10:47
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.

5 participants