Skip to content

Commit

Permalink
2315: changes in gascosts (ethereum#2676)
Browse files Browse the repository at this point in the history
  • Loading branch information
holiman authored and Arachnid committed Mar 6, 2021
1 parent dbc913a commit 17630d7
Showing 1 changed file with 39 additions and 27 deletions.
66 changes: 39 additions & 27 deletions EIPS/eip-2315.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,59 +76,62 @@ This should jump into a subroutine, back out and stop.

Bytecode: `0x6004b300b2b7`


| Pc | Op | Cost | Stack | RStack |
|-------|-------------|------|-----------|-----------|
| 0 | PUSH1 | 3 | [] | [] |
| 2 | JUMPSUB | 8 | [4] | [] |
| 5 | RETURNSUB | 2 | [] | [ 2] |
| 2 | JUMPSUB | 10 | [4] | [] |
| 5 | RETURNSUB | 5 | [] | [ 2] |
| 3 | STOP | 0 | [] | [] |

Output: 0x
Consumed gas: `18`

### Two levels of subroutines

This should execute fine, going into one two depths of subroutines

Bytecode: `0x6800000000000000000cb300b26011b3b7b2b7`

| Pc | Op | Cost | Stack | RStack |
|-------|-------------|------|-----------|-----------|
| 0 | PUSH9 | 3 | [] | [] |
| 10 | JUMPSUB | 8 | [12] | [] |
| 10 | JUMPSUB | 10 | [12] | [] |
| 13 | PUSH1 | 3 | [] | [10] |
| 15 | JUMPSUB | 8 | [17] | [10] |
| 18 | RETURNSUB | 2 | [] | [10,15] |
| 16 | RETURNSUB | 2 | [] | [10] |
| 15 | JUMPSUB | 10 | [17] | [10] |
| 18 | RETURNSUB | 5 | [] | [10,15] |
| 16 | RETURNSUB | 5 | [] | [10] |
| 11 | STOP | 0 | [] | [] |

Consumed gas: `36`

### Failure 1: invalid jump

This should fail, since the given `location` is outside of the code-range. The code is the same as previous example,
except that the pushed `location` is `0x01000000000000000c` instead of `0x0c`.
This should fail, since the given location is outside of the code-range. The code is the same as previous example,
except that the pushed location is `0x01000000000000000c` instead of `0x0c`.

Bytecode: `0x6801000000000000000cb300b26011b3b7b2b7 `
Bytecode: `0x6801000000000000000cb300b26011b3b7b2b7`

| Pc | Op | Cost | Stack | RStack |
|-------|-------------|------|-----------|-----------|
| 0 | PUSH9 | 3 | [] | [] |
| 10 | JUMPSUB | 8 |[18446744073709551628] | [] |
| 10 | JUMPSUB | 10 |[18446744073709551628] | [] |

```
Error: at pc=10, op=JUMPSUB: evm: invalid jump destination
Error: at pc=10, op=JUMPSUB: invalid jump destination
```

### Failure 2: shallow `return stack`

This should fail at first opcode, due to shallow `return stack`
This should fail at first opcode, due to shallow `return_stack`

Bytecode: `0xb75858` (`RETURNSUB`, `PC`, `PC`)


| Pc | Op | Cost | Stack | RStack |
|-------|-------------|------|-----------|-----------|
| 0 | RETURNSUB | 2 | [] | [] |
| 0 | RETURNSUB | 5 | [] | [] |

```
Error: at pc=0, op=RETURNSUB: evm: invalid retsub
Error: at pc=0, op=RETURNSUB: invalid retsub
```

### Subroutine at end of code
Expand All @@ -143,49 +146,58 @@ Bytecode: `0x600556b2b75b6003b3`
| 2 | JUMP | 8 | [5] | [] |
| 5 | JUMPDEST | 1 | [] | [] |
| 6 | PUSH1 | 3 | [] | [] |
| 8 | JUMPSUB | 8 | [3] | [] |
| 4 | RETURNSUB | 2 | [] | [ 8] |
| 8 | JUMPSUB | 10 | [3] | [] |
| 4 | RETURNSUB | 5 | [] | [ 8] |
| 9 | STOP | 0 | [] | [] |

Consumed gas: `25`
Consumed gas: `30`

### Error on "walk-into-subroutine"

In this example, the code 'walks' into a subroutine, which is not allowed, and causes an error

Bytecode: `0xb2b700`


| Pc | Op | Cost | Stack | RStack |
|-------|-------------|------|-----------|-----------|
| 0 | BEGINSUB | 1 | [] | [] |

| 0 | BEGINSUB | 2 | [] | [] |

```
Error: at pc=0, op=BEGINSUB: invalid subroutine entry
```

**Note 5**: The content of the error message, (`invalid subroutine entry`) is implementation-specific.

## Implementations

Three clients have implemented the previous version of proposal:
Three clients have implemented this (or an earlier version of) this proposal:

- [geth](https://github.com/ethereum/go-ethereum/pull/20619) .
- [besu](https://github.com/hyperledger/besu/pull/717), and
- [openethereum](https://github.com/openethereum/openethereum/pull/11629).

The changes for the current version are trivial.

### Costs and Codes

We suggest that the cost of `BEGINSUB` be _base_, `JUMPSUB` be _mid_, and `RETURNSUB` be _verylow_.
Measurement will tell. We suggest the following opcodes:
We suggest that the cost of

- `BEGINSUB` be _base_ (`2`)
- Although formally specified, the cost of `BEGINSUB` does not matter in practice, since `BEGINSUB` never executes without error.
- `JUMPSUB` be _high_ (`10`)
- This is the same as `JUMPI`, and `2` more than `JUMP`.
- `RETURNSUB` be _low_ (`5`).

Benchmarking might be needed to tell if the costs are well-balanced.

We suggest the following opcodes:

```
0xb2 BEGINSUB
0xb3 JUMPSUB
0xb7 RETURNSUB
```

**Note 6**: Although specified at _base_, the cost of `BEGINSUB` does not matter in practice, since `BEGINSUB` never executes without error.

## Security Considerations

These changes do introduce new flow control instructions, so any software which does static/dynamic analysis of evm-code
Expand Down

0 comments on commit 17630d7

Please sign in to comment.