-
Notifications
You must be signed in to change notification settings - Fork 20.1k
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
core/vm: Simplify error handling in interpreter loop #23952
Conversation
evmone benchmark suite with go 1.16 (obsolete).
|
core/vm/instructions.go
Outdated
return res, nil | ||
} | ||
interpreter.returnData = nil |
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.
In general I'm not happy how returnData
is handled after these changes, but wanted to see big impact all changes have. Also please note that returning []byte
from instructions is not needed any more.
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.
In CREATE
and CREATE2
the res
is either returned bytecode or revert return data, depending on the error. So we need handle ErrExecutionReverted
separately. But I think it would be better to reset returnData = nil
before evm.Create()
and only assign it in the end in case of ErrExecutionReverted
.
I can also move the returnData
change to separate PR.
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 left it with some better comments.
core/vm/errors.go
Outdated
@@ -36,6 +36,7 @@ var ( | |||
ErrGasUintOverflow = errors.New("gas uint64 overflow") | |||
ErrInvalidCode = errors.New("invalid code: must not begin with 0xef") | |||
ErrNonceUintOverflow = errors.New("nonce uint64 overflow") | |||
errStopToken = errors.New("stop token") |
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.
Why is this lowercase?
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.
Lowercase names are not visible outside of a go package
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.
Is this name fine?
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 think it's ok. Perhaps it should be changed like this
errStopToken = errors.New("stop token") | |
// errStopToken is an internal stop token which is never returned to outside callers. | |
errStopToken = errors.New("stop token") |
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.
Added.
So I built this branch and synced goerli from block 5,416,168 to the current head (5900905) without seeing an issue |
0bc56c9
to
782e923
Compare
} | ||
return nil, nil | ||
|
||
if err == errStopToken { |
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.
more go-idiomatic
if err == errStopToken { | |
if errors.Is(err, errStopToken) { |
But maybe your variant is more performant?
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.
The errors.Is()
is not inlined (https://godbolt.org/z/n81qdKTzn), but this does not matter because this check is outside of the loop.
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.
My slight preference is to leave ==
because of other usages in EVM, e.g. suberr == ErrCodeStoreOutOfGas
.
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.
There is no need to use errors.Is
here. The error is purely internal, and this check is performance-critical. errors.Is
is appropriate when dealing with unknown and potentially nested errors.
Gave this a test on the existing benchmarks. I had expected it to have a bit of impact on
|
evmone benchmark suite on go 1.17
|
|
Yes, more like it! |
return res, nil | ||
} | ||
interpreter.returnData = nil // clear dirty return data buffer |
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.
This is not covered by tests. Reported in ethereum/tests#993.
core/vm/errors.go
Outdated
@@ -36,6 +36,7 @@ var ( | |||
ErrGasUintOverflow = errors.New("gas uint64 overflow") | |||
ErrInvalidCode = errors.New("invalid code: must not begin with 0xef") | |||
ErrNonceUintOverflow = errors.New("nonce uint64 overflow") | |||
errStopToken = errors.New("stop token") |
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.
Added.
It would be nice to have some other hardware to confirm the numbers. |
|
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.
LGTM
|
* core/vm: break loop on any error * core/vm: move ErrExecutionReverted to opRevert() * core/vm: use "stop token" to stop the loop * core/vm: unconditionally pc++ in the loop * core/vm: set return data in instruction impls
Cherry-pick ethereum/go-ethereum#23952 Co-authored-by: Paweł Bylica <chfast@gmail.com>
* core/vm: break loop on any error * core/vm: move ErrExecutionReverted to opRevert() * core/vm: use "stop token" to stop the loop * core/vm: unconditionally pc++ in the loop * core/vm: set return data in instruction impls
This simplifies interpreter loop exit condition to just
err != nil
. This catches:REVERT
),errStopToken
).Also
operation.jumps
check is removed by settingpc - 1
inside the jump instruction and doingpc++
in the loop unconditionally.Finally, instructions manipulating return data are handling this themselves. This eliminates another
operation.returns
check.