Skip to content

Commit

Permalink
Merge pull request #40 from ethereum/memory-metering
Browse files Browse the repository at this point in the history
Memory metering
  • Loading branch information
axic committed Aug 14, 2016
2 parents 3d5b82c + 15430ea commit e68c522
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
8 changes: 8 additions & 0 deletions feeSchedule.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,11 @@ All fees for opcodes are currently 1 gas. This needs to be updated before finali
|Opcode |Price |
|-----------|------|
|unreachable|1 |

# Expanding memory

Memory can be expanded in pages, where a page corresponds to 65536 bytes of space.

The EVM1 formula for extending memory is `words * 3 + words ^ 2 / 512` where `word` corresponds to 32 bytes.

From this we can calculate that a 65536 byte page should cost 14336 gas.
23 changes: 22 additions & 1 deletion metering.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,31 @@ Metering is done by counting the cost of running a continuous subtree of the AST

Currently each opcode is measured as 1 unit of gas. Functions, Parameters to functions and Result values are also counted as 1 unit of gas. See the [fee schedule](./feeSchedule.md) for more information.

## Special metering: memory

Metering memory makes use of the separate memory system of WebAssembly:
- the module parameter `memory`
- the two instructions:
- `grow_memory`
- `current_memory`

Memory size is expressed in pages, where a page is 65536 bytes.

The module parameter specifies the initial page count and an optional maximal page count the module cannot exceed. The currently available page count can be queried via `current_memory` and new pages can be allocated via `grow_memory`. Read more about memory in the [the WebAssembly design](https://github.com/WebAssembly/design/blob/master/Modules.md#linear-memory-section).

Gas needs to be charged for the initial allocated pages as well as any increase of pages via `grow_memory`.

### Initial memory allocation

The cost of pre-allocated memory must be included in the very first metering call.

### Increasing memory

Any calls to `grow_memory` needs to be prepended with a call for metering.

## TODO

* Specify a cost table for Ethereum System calls
* Specify cost for memory

## Examples

Expand Down

0 comments on commit e68c522

Please sign in to comment.