-
Notifications
You must be signed in to change notification settings - Fork 775
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
VM: report dynamic gas values in fee
field of step
event
#1364
Conversation
Codecov Report
Flags with carried forward coverage won't be shown. Click here to find out more. |
13a04a4
to
cb9169e
Compare
ad113b2
to
ce50052
Compare
This should pass at least Istanbul, MuirGlacier, Berlin and London. Running all tests to see if I missed something on earlier forks. There are some TODOs left, but this is ready for early-review @ryanio @acolytec3 |
Just a note, this is not yet EIP3155-ready, this will happen in another PR. |
@alcuadrado could you maybe check if you see some inconsistencies in the fee output? There are some cases not yet covered; one of them is reporting the call gas limit for any CALL opcode, and the invalid opcode now should report 0 fee, this is not consistent with Geth's output. |
just getting started reviewing, looks really great jochem, feels very organized and logical to follow. added a commit with some small improvements as I dig in further. |
|
Rebased on master and force pushed |
If we want to merge this in without worrying about the breaking change on the fee field of the |
packages/vm/src/evm/interpreter.ts
Outdated
await dynamicGasHandler(this._runState, gas) | ||
} | ||
|
||
// TODO: figure out if we should try/catch this (in case step event throws) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you have an example of when or how the step event would throw? it seems innocent enough with some debug and emit
.
if it throws, in some extreme case, we would probably want the error to bubble up to the user, although we could wrap it more nicely.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we use a number
instead of BN
(so we convert the BN to a number) then it might throw if the BN does not fit in a number. What I actually meant with this TODO is to figure out what happens if any of the listeners throws. The previous behavior is that the tx then stops executing, so I guess we should keep that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I actually meant with this TODO is to figure out what happens if any of the listeners throws. The previous behavior is that the tx then stops executing, so I guess we should keep that.
ooh I see what you mean, if the consumer of the step event has a listener that throws? i thought you were maybe referring to it throwing within our own code, but it externally throwing also makes sense. in that case, could we consider wrapping the emit
in a try/catch? then we could return some kind of sensible error to the user with the original error context attached. (i'd have to double check with my own code that this is actually how it would work)
If we use a number instead of BN (so we convert the BN to a number) then it might throw if the BN does not fit in a number.
yeah, in what kind of situation would the fee be > 2^53 - 1
though? seems like a pretty extreme case. i only mention it because then we can merge this in without a breaking change and then update the number to BN in a future planned breaking changes release (which we can get started on as soon as london goes live on mainnet)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have ran into the number being too large, I am assuming this was some ethereum/tests test. It could theoretically also happen if we implement the trace specification; then if we normally would forward "all" gas, then Geth would report this amount of gas which would get forwarded, so if we have blocks with extremely large gas limits (i.e. in ethereum/tests) then it would report a number which is too large.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
circling back to this TODO, do you have any fresh thoughts on this?
// TODO: figure out if we should try/catch this (in case step event throws)
i figure it would be harmless to add a try/catch as an extra precaution, can even add a test for it where a listener on the step
event throws and expect that it doesn't stop vm execution.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if this has been addressed. Will nevertheless merge this in here, in doubt we can add separtely along a follow-up PR.
These are probably not ran by coverage, since these are hardfork specific rules, and IIRC we only run a specific fork in the coverage? |
step
eventfee
field of step
event
I will write some tests for the uncovered lines as reported by codecov. |
That's weird, we don't have API tests for EIP 1283. And for 2929 this is probably not triggered by codecov since that runs in Berlin, and codecov runs Istanbul (should we update the default fork to either Berlin or London?). |
I will add a few more tests to increase coverage of currently uncovered lines. |
I found some redundant code after some long research, here a quick explanation. Code in question is: (context is CALLCODE)
The second check is redundant. Note that |
Rebasing this one. |
6f13708
to
fb2d5c9
Compare
This was an extremely lengthy rebase process - lets hope tests pass. |
vm: clarify EIP-150 comment
vm: explicitly clone gasLimit for maxCallGas
07a0c8d
to
3264d5b
Compare
Rebased this on master, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks really great, again, really remarkable work! Totally love this now having all the gas logic together in one file and so transparently together, I would assume this will open up more use cases beyond the concrete (important) one addressed here.
Will merge to develop
.
* vm: codes: add dynamicGas property vm: codes: make codes more typesafe * vm: create no-op dynamic gas handlers * vm: first batch of dynamic gas up to 0x3f * vm: add other opcodes to gas map vm: change step event fee type from number to BN vm: deduct dynamic gas vm: fix stack peek vm: do not add gasLimit to gas cost for CREATE * vm: move errors to gas map * vm: fix memory dynamic gas bug * vm: fix gas bugs caused by not considering base fee * vm: fix message call gas related bugs, clone current gas left * add typedoc for peek use underscore for unused peek params (and fix eslint config) format some comment newlines for readability switch from require to import for exceptions * simplify the 2929 state manager castings in runTx * add changelog entry * vm: add EIP1283 tests * vm: split non-eip2929 and eip2929 gas costs * vm: fix gas costs * vm: add early-hardfork coverage * vm: clarify pre-Constantinople SSTORE gas vm: clarify EIP-150 comment * run coverage for all state and blockchain tests, remove redundant istanbul run * vm: fix CALLCODE gas vm: explicitly clone gasLimit for maxCallGas * vm: remove TODO in interpreter * update defaultCost to BN, cast 2929 statemanager to simplify use syntax * use underscore for unused variables, simplify types since they can be inferred * vm: fix browser tests + fix rebase Co-authored-by: Ryan Ghods <ryan@ryanio.com>
* vm: codes: add dynamicGas property vm: codes: make codes more typesafe * vm: create no-op dynamic gas handlers * vm: first batch of dynamic gas up to 0x3f * vm: add other opcodes to gas map vm: change step event fee type from number to BN vm: deduct dynamic gas vm: fix stack peek vm: do not add gasLimit to gas cost for CREATE * vm: move errors to gas map * vm: fix memory dynamic gas bug * vm: fix gas bugs caused by not considering base fee * vm: fix message call gas related bugs, clone current gas left * add typedoc for peek use underscore for unused peek params (and fix eslint config) format some comment newlines for readability switch from require to import for exceptions * simplify the 2929 state manager castings in runTx * add changelog entry * vm: add EIP1283 tests * vm: split non-eip2929 and eip2929 gas costs * vm: fix gas costs * vm: add early-hardfork coverage * vm: clarify pre-Constantinople SSTORE gas vm: clarify EIP-150 comment * run coverage for all state and blockchain tests, remove redundant istanbul run * vm: fix CALLCODE gas vm: explicitly clone gasLimit for maxCallGas * vm: remove TODO in interpreter * update defaultCost to BN, cast 2929 statemanager to simplify use syntax * use underscore for unused variables, simplify types since they can be inferred * vm: fix browser tests + fix rebase Co-authored-by: Ryan Ghods <ryan@ryanio.com>
* vm: codes: add dynamicGas property vm: codes: make codes more typesafe * vm: create no-op dynamic gas handlers * vm: first batch of dynamic gas up to 0x3f * vm: add other opcodes to gas map vm: change step event fee type from number to BN vm: deduct dynamic gas vm: fix stack peek vm: do not add gasLimit to gas cost for CREATE * vm: move errors to gas map * vm: fix memory dynamic gas bug * vm: fix gas bugs caused by not considering base fee * vm: fix message call gas related bugs, clone current gas left * add typedoc for peek use underscore for unused peek params (and fix eslint config) format some comment newlines for readability switch from require to import for exceptions * simplify the 2929 state manager castings in runTx * add changelog entry * vm: add EIP1283 tests * vm: split non-eip2929 and eip2929 gas costs * vm: fix gas costs * vm: add early-hardfork coverage * vm: clarify pre-Constantinople SSTORE gas vm: clarify EIP-150 comment * run coverage for all state and blockchain tests, remove redundant istanbul run * vm: fix CALLCODE gas vm: explicitly clone gasLimit for maxCallGas * vm: remove TODO in interpreter * update defaultCost to BN, cast 2929 statemanager to simplify use syntax * use underscore for unused variables, simplify types since they can be inferred * vm: fix browser tests + fix rebase Co-authored-by: Ryan Ghods <ryan@ryanio.com>
* vm: codes: add dynamicGas property vm: codes: make codes more typesafe * vm: create no-op dynamic gas handlers * vm: first batch of dynamic gas up to 0x3f * vm: add other opcodes to gas map vm: change step event fee type from number to BN vm: deduct dynamic gas vm: fix stack peek vm: do not add gasLimit to gas cost for CREATE * vm: move errors to gas map * vm: fix memory dynamic gas bug * vm: fix gas bugs caused by not considering base fee * vm: fix message call gas related bugs, clone current gas left * add typedoc for peek use underscore for unused peek params (and fix eslint config) format some comment newlines for readability switch from require to import for exceptions * simplify the 2929 state manager castings in runTx * add changelog entry * vm: add EIP1283 tests * vm: split non-eip2929 and eip2929 gas costs * vm: fix gas costs * vm: add early-hardfork coverage * vm: clarify pre-Constantinople SSTORE gas vm: clarify EIP-150 comment * run coverage for all state and blockchain tests, remove redundant istanbul run * vm: fix CALLCODE gas vm: explicitly clone gasLimit for maxCallGas * vm: remove TODO in interpreter * update defaultCost to BN, cast 2929 statemanager to simplify use syntax * use underscore for unused variables, simplify types since they can be inferred * vm: fix browser tests + fix rebase Co-authored-by: Ryan Ghods <ryan@ryanio.com>
* vm: codes: add dynamicGas property vm: codes: make codes more typesafe * vm: create no-op dynamic gas handlers * vm: first batch of dynamic gas up to 0x3f * vm: add other opcodes to gas map vm: change step event fee type from number to BN vm: deduct dynamic gas vm: fix stack peek vm: do not add gasLimit to gas cost for CREATE * vm: move errors to gas map * vm: fix memory dynamic gas bug * vm: fix gas bugs caused by not considering base fee * vm: fix message call gas related bugs, clone current gas left * add typedoc for peek use underscore for unused peek params (and fix eslint config) format some comment newlines for readability switch from require to import for exceptions * simplify the 2929 state manager castings in runTx * add changelog entry * vm: add EIP1283 tests * vm: split non-eip2929 and eip2929 gas costs * vm: fix gas costs * vm: add early-hardfork coverage * vm: clarify pre-Constantinople SSTORE gas vm: clarify EIP-150 comment * run coverage for all state and blockchain tests, remove redundant istanbul run * vm: fix CALLCODE gas vm: explicitly clone gasLimit for maxCallGas * vm: remove TODO in interpreter * update defaultCost to BN, cast 2929 statemanager to simplify use syntax * use underscore for unused variables, simplify types since they can be inferred * vm: fix browser tests + fix rebase Co-authored-by: Ryan Ghods <ryan@ryanio.com>
* VM: report dynamic gas values in `fee` field of `step` event (#1364) * vm: codes: add dynamicGas property vm: codes: make codes more typesafe * vm: create no-op dynamic gas handlers * vm: first batch of dynamic gas up to 0x3f * vm: add other opcodes to gas map vm: change step event fee type from number to BN vm: deduct dynamic gas vm: fix stack peek vm: do not add gasLimit to gas cost for CREATE * vm: move errors to gas map * vm: fix memory dynamic gas bug * vm: fix gas bugs caused by not considering base fee * vm: fix message call gas related bugs, clone current gas left * add typedoc for peek use underscore for unused peek params (and fix eslint config) format some comment newlines for readability switch from require to import for exceptions * simplify the 2929 state manager castings in runTx * add changelog entry * vm: add EIP1283 tests * vm: split non-eip2929 and eip2929 gas costs * vm: fix gas costs * vm: add early-hardfork coverage * vm: clarify pre-Constantinople SSTORE gas vm: clarify EIP-150 comment * run coverage for all state and blockchain tests, remove redundant istanbul run * vm: fix CALLCODE gas vm: explicitly clone gasLimit for maxCallGas * vm: remove TODO in interpreter * update defaultCost to BN, cast 2929 statemanager to simplify use syntax * use underscore for unused variables, simplify types since they can be inferred * vm: fix browser tests + fix rebase * VM: moved dynamic fee to dedicated dynamicFee field in step event and retained fee behavior and type for backwards compatibility * VM: aligned InterpreterStep and step event object property documentation, completed missing step event properties * VM: test fix * vm: fix hardhat e2e tests * vm: fix MSTORE opcodes * vm: added dynamicGas property to push0 (EIP-3855) opcode * hardhat e2e: add temporary workaround for skipping tests with inconsistent memory field * nit style: use underscore instead of comment out unused variable Co-authored-by: Jochem Brouwer <jochembrouwer96@gmail.com> Co-authored-by: Ryan Ghods <ryan@ryanio.com>
* VM: Adopted Backwards-compatible Dynamic Gas Cost Refactoring (#1553) * VM: report dynamic gas values in `fee` field of `step` event (#1364) * vm: codes: add dynamicGas property vm: codes: make codes more typesafe * vm: create no-op dynamic gas handlers * vm: first batch of dynamic gas up to 0x3f * vm: add other opcodes to gas map vm: change step event fee type from number to BN vm: deduct dynamic gas vm: fix stack peek vm: do not add gasLimit to gas cost for CREATE * vm: move errors to gas map * vm: fix memory dynamic gas bug * vm: fix gas bugs caused by not considering base fee * vm: fix message call gas related bugs, clone current gas left * add typedoc for peek use underscore for unused peek params (and fix eslint config) format some comment newlines for readability switch from require to import for exceptions * simplify the 2929 state manager castings in runTx * add changelog entry * vm: add EIP1283 tests * vm: split non-eip2929 and eip2929 gas costs * vm: fix gas costs * vm: add early-hardfork coverage * vm: clarify pre-Constantinople SSTORE gas vm: clarify EIP-150 comment * run coverage for all state and blockchain tests, remove redundant istanbul run * vm: fix CALLCODE gas vm: explicitly clone gasLimit for maxCallGas * vm: remove TODO in interpreter * update defaultCost to BN, cast 2929 statemanager to simplify use syntax * use underscore for unused variables, simplify types since they can be inferred * vm: fix browser tests + fix rebase * VM: moved dynamic fee to dedicated dynamicFee field in step event and retained fee behavior and type for backwards compatibility * VM: aligned InterpreterStep and step event object property documentation, completed missing step event properties * VM: test fix * vm: fix hardhat e2e tests * vm: fix MSTORE opcodes * vm: added dynamicGas property to push0 (EIP-3855) opcode * hardhat e2e: add temporary workaround for skipping tests with inconsistent memory field * nit style: use underscore instead of comment out unused variable Co-authored-by: Jochem Brouwer <jochembrouwer96@gmail.com> Co-authored-by: Ryan Ghods <ryan@ryanio.com> * ci: bump karma-typescript version (#1631) * bump karma-typescript to new release * bump libp2p-bootstrap * update package-lock * VM: Jump dest analysis enhancements (#1629) * Change valid jumps to single array * Switch array to Uint8Array * use correct opt code * Check for jumpsub * Address feedback * Address fixes * More efficiency * Adjust if/else logic for efficiency * Move comments Co-authored-by: Holger Drewes <Holger.Drewes@gmail.com> Co-authored-by: Ryan Ghods <ryan@ryanio.com> * rlp: add to root readme (#1642) * add rlp to root readme * add carryforward for rlp flag since it is not always tested on every build unless its paths are modified * Client: Add tests to verify shutdown (#1641) * Add tests to verify client shutdown * Add libp2p test * Address feedback * most libp2p into separate file * VM: Add comparison testing between branchs for State Test Runner (#1634) * Add diff tester and performance * update script * Simplify script * Readme updates * move start * Update stashing logic in script * tx: input value testing (#1620) * tx: helper methods to generate tx values * tx: helper methods to generate tx values * chore: scaffold test and add extra to values * chore: add missing spread operator * chore: single value for each tx key * tx: add generateCombinations method and legacy + eip 1559 test cases * chore: remove console log * chore: use london hardfork for eip1559 * tx: refactor generateCombinations interface and properly compare hash * tx: deterministically randomly sample 1000 elements from array for testing * chore: remove v * chore: remove todo * chore: clearer variable declarations and types * tx: fix and simplify random sampling * tx: display tx hash in testing output * tx: convert buffer to hex for output log * ci: fix node-versions run for node <16 (#1653) * re-add updating to npm v7 for node versions <16 * only upgrade npm for node v <16 * fix bin/rlp js: node 12 doesn't support ES11 which added support for nullish coalescing operator (??) so we'll use ternary here alternatively we could write this file in TS and compile to e.g. dist/bin/rlp (like we do in the client bin), but maybe if the file gets more complicated, in its current state i don't think it's so neccessary * use same errorMsg format for JSON.parse, remove unneeded extra Uint8Array.from (already is uint8array) * rlp: v3 updates from integration (#1648) * rlp updates * util: add arrToBufArr and bufArrToArr and tests * util updates, add deprecation notice for re-exports * rlp tsconfig: include rootDir for composite option * remove util exports deprecation notices * rlp: add readme note for buffer compatibility * undo capitalization * Devp2p, Client: Fix duplicated debug messages (#1643) * devp2p: update debug package * blockchain: update debug package * client: update debug package * vm: update debug package * devp2p/dpt fix for #1485 * devp2p/eth: Fix for 485 * devp2p: Fix for #1485: add base debug * devp2p/dpt #1485: change to debug.extend() * devp2p:dpt:server change to degub.extend() * devp2p:eth change to debug.extend() * devp2p/les: change to debug.extend() * devp2p:rlpx: change to debug.extend() * rlps: change to debug.extend() * Update debug to use IP as first tag * vm, client: removed ProofStateManager interface, added optional getProof, verifyProof methods to StateManager interface (#1660) * Monorepo (& Tx, VM): Examples scripts in CI (#1658) * chore(examples): examples added to ci * chore(examples-ci): remove script from VM (for now) & rename examples workflow file * chore(ci): new script formwatted with prettier & example workflow changes to run with non-test branches Co-authored-by: Holger Drewes <Holger.Drewes@gmail.com> Co-authored-by: Holger Drewes <Holger.Drewes@gmail.com> Co-authored-by: acolytec3 <17355484+acolytec3@users.noreply.github.com> Co-authored-by: Gabriel Rocheleau <contact@rockwaterweb.com> Co-authored-by: ScottyPoi <66335769+ScottyPoi@users.noreply.github.com> Co-authored-by: Cesar Brazon <cesarbrazon10@gmail.com>
This PR closes #1169, closes #593
Related: #1215
This PR internally refactors the EVM. Opcodes are now split into their behavior (stack edits, getting block hashes, etc.) and their gas cost. However, due to the complex behavior of gas cost they still edit some of the state of the VM, for instance, for memory expansions this is done into the gas calculation part.
The result is that the
fee
result of thestep
event now represents the actual cost of an operation, not just the base fee.This PR is a breaking change; it changes the
fee
field of thestep
event fromnumber
toBN
.The
_runStepHook()
function signature is also edited, but since this is an underscore, I assume this was meant to be private, so that should be non-breaking.