Skip to content
This repository has been archived by the owner on Dec 4, 2024. It is now read-only.

Commit

Permalink
Adding check inside EVM loop for errors to avoid executing contract w…
Browse files Browse the repository at this point in the history
…hen it should be stopped.
  • Loading branch information
cokicm committed May 20, 2024
1 parent cac76d3 commit 1d3bd1a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
7 changes: 7 additions & 0 deletions state/runtime/evm/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,13 @@ func (c *state) Run() ([]byte, error) {
if tracer != nil {
c.captureExecution(op.String(), ipCopy, gasCopy, gasCopy-c.gas)
}

// In case there was an error set in current instruction, let the execution trace
// to allow easier debugging and stop the execution.
if err := c.err; err != nil {
break
}

// check if stack size exceeds the max size
if c.stack.sp > stackSize {
c.exit(&runtime.StackOverflowError{StackLen: c.stack.sp, Limit: stackSize})
Expand Down
21 changes: 21 additions & 0 deletions state/runtime/evm/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ func (c *codeHelper) push1() {
c.buf = append(c.buf, 0x1)
}

func (c *codeHelper) opDup() {
c.buf = append(c.buf, DUP16)
}

func (c *codeHelper) pop() {
c.buf = append(c.buf, POP)
}
Expand Down Expand Up @@ -126,3 +130,20 @@ func TestOpcodeNotFound(t *testing.T) {
_, err := s.Run()
assert.Equal(t, errOpCodeNotFound, err)
}

func TestErrorHandlingStopsContractExecution(t *testing.T) {
code := codeHelper{}
code.opDup()
code.opDup()

s, closeFn := getState(&chain.ForksInTime{})
defer closeFn()

s.code = code.buf
s.gas = 10000
s.host = &mockHost{}

_, err := s.Run()
assert.Error(t, err, "The EVM did not handle an error")
assert.Equal(t, s.ip, 0, "The EVM did not executingon first error.")
}

0 comments on commit 1d3bd1a

Please sign in to comment.