Skip to content

Commit

Permalink
underflow test fix
Browse files Browse the repository at this point in the history
  • Loading branch information
goran-ethernal committed Feb 19, 2024
1 parent 24c4fe6 commit b1d9070
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 17 deletions.
2 changes: 1 addition & 1 deletion state/runtime/evm/dispatch_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func init() {
register(SLT, handler{inst: opSlt, stack: 2, gas: 3})
register(SGT, handler{inst: opSgt, stack: 2, gas: 3})

register(SIGNEXTEND, handler{inst: opSignExtension, stack: 1, gas: 5})
register(SIGNEXTEND, handler{inst: opSignExtension, stack: 2, gas: 5})

register(SHL, handler{inst: opShl, stack: 2, gas: 3})
register(SHR, handler{inst: opShr, stack: 2, gas: 3})
Expand Down
20 changes: 6 additions & 14 deletions state/runtime/evm/instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -1418,9 +1418,6 @@ func (c *state) buildCreateContract(op OpCode) (*runtime.Contract, error) {

// Calculate and consume gas cost

// var overflow bool
var gasCost uint64

// Both CREATE and CREATE2 use memory
var input []byte

Expand All @@ -1431,17 +1428,6 @@ func (c *state) buildCreateContract(op OpCode) (*runtime.Contract, error) {
return nil, nil
}

// Consume memory resize gas (TODO, change with get2) (to be fixed in EVM-528) //nolint:godox
if !c.consumeGas(gasCost) {
return nil, nil
}

if hasTransfer {
if c.host.GetBalance(c.msg.Address).Cmp(value) < 0 {
return nil, types.ErrInsufficientFunds
}
}

if op == CREATE2 {
// Consume sha3 gas cost
size := length.Uint64()
Expand All @@ -1450,6 +1436,12 @@ func (c *state) buildCreateContract(op OpCode) (*runtime.Contract, error) {
}
}

if hasTransfer {
if c.host.GetBalance(c.msg.Address).Cmp(value) < 0 {
return nil, types.ErrInsufficientFunds
}
}

// Calculate and consume gas for the call
gas := c.gas

Expand Down
4 changes: 2 additions & 2 deletions state/transition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestSubGasLimitPrice(t *testing.T) {
expectedErr: nil,
},
{
name: "should fail by ErrNotEnoughFunds",
name: "should fail by ErrInsufficientFunds",
preState: map[types.Address]*PreState{
addr1: {
Nonce: 0,
Expand All @@ -59,7 +59,7 @@ func TestSubGasLimitPrice(t *testing.T) {
from: addr1,
gas: 10,
gasPrice: 10,
// should return ErrNotEnoughFundsForGas when state.SubBalance returns ErrNotEnoughFunds
// should return ErrInsufficientFunds when state.SubBalance returns ErrNotEnoughFunds
expectedErr: ErrInsufficientFunds,
},
}
Expand Down

0 comments on commit b1d9070

Please sign in to comment.