evm v0.1.4 EVM.Gas
Functions for interacting wth gas and costs of opscodes.
Link to this section Summary
Functions
Returns the cost to execute the given a cycle of the VM. This is defined
in Appenix H of the Yellow Paper, Eq.(220) and is denoted C
Returns the cost of a call to sstore
. This is defined
in Appenfix H.2. of the Yellow Paper under the
definition of SSTORE, referred to as C_SSTORE
Paid for every transaction
Paid by all contract-creating transactions after the Homestead transition
Returns the gas cost for G_txdata{zero, nonzero} as defined in Appendix G (Fee Schedule) of the Yellow Paper
Returns the instruction cost for every possible instruction. This is defined in Appendix H of the Yellow Paper
Link to this section Types
Link to this section Functions
cost(EVM.state, EVM.MachineState.t, EVM.ExecEnv.t) :: t | nil
Returns the cost to execute the given a cycle of the VM. This is defined
in Appenix H of the Yellow Paper, Eq.(220) and is denoted C
.
Examples
# TODO: Figure out how to hand in state
iex> EVM.Gas.cost(%{}, %EVM.MachineState{}, %EVM.ExecEnv{})
0
cost_sstore(EVM.state, EVM.MachineState.t) :: t
Returns the cost of a call to sstore
. This is defined
in Appenfix H.2. of the Yellow Paper under the
definition of SSTORE, referred to as C_SSTORE
.
Examples
iex> state = MerklePatriciaTree.Trie.new(MerklePatriciaTree.Test.random_ets_db(:evm_vm_test)) …> |> MerklePatriciaTree.Trie.update(<<0>>, 1) iex> EVM.Gas.cost_sstore(state, %EVM.MachineState{stack: [0, 0]}) 5000 iex> EVM.Gas.cost_sstore(state, %EVM.MachineState{stack: [0, 2]}) 20000
Paid for every transaction.
Paid by all contract-creating transactions after the Homestead transition.
Returns the gas cost for G_txdata{zero, nonzero} as defined in Appendix G (Fee Schedule) of the Yellow Paper.
This implements g_txdatazero
and g_txdatanonzero
Examples
iex> EVM.Gas.g_txdata(<<1, 2, 3, 0, 4, 5>>)
5 * 68 + 4
iex> EVM.Gas.g_txdata(<<0>>)
4
iex> EVM.Gas.g_txdata(<<0, 0>>)
8
iex> EVM.Gas.g_txdata(<<>>)
0
instr_cost(atom, EVM.state, EVM.MachineState.t, EVM.ExecEnv.t) :: t | nil
Returns the instruction cost for every possible instruction. This is defined in Appendix H of the Yellow Paper.
Examples
iex> EVM.Gas.instr_cost(:sstore, nil, %EVM.MachineState{stack: [0, 0]}, nil)
5000
iex> EVM.Gas.instr_cost(:exp, nil, %EVM.MachineState{stack: [0, 0]}, nil)
10
iex> EVM.Gas.instr_cost(:exp, nil, %EVM.MachineState{stack: [0, 10241]}, nil)
30
iex> EVM.Gas.instr_cost(:jumpdest, nil, nil, nil)
1
iex> EVM.Gas.instr_cost(:blockhash, nil, nil, nil)
20
iex> EVM.Gas.instr_cost(:stop, nil, nil, nil)
0
iex> EVM.Gas.instr_cost(:address, nil, nil, nil)
2
iex> EVM.Gas.instr_cost(:push0, nil, nil, nil)
3
iex> EVM.Gas.instr_cost(:mul, nil, nil, nil)
5
iex> EVM.Gas.instr_cost(:addmod, nil, nil, nil)
8
iex> EVM.Gas.instr_cost(:jumpi, nil, nil, nil)
10
iex> EVM.Gas.instr_cost(:extcodesize, nil, nil, nil)
700
iex> EVM.Gas.instr_cost(:mstore, nil, %EVM.MachineState{stack: [0, 0]}, nil)
6
iex> EVM.Gas.instr_cost(:mstore, nil, %EVM.MachineState{stack: [0, 0], memory: <<1::256>>, active_words: 0}, nil)
3
iex> EVM.Gas.instr_cost(:mstore, nil, %EVM.MachineState{stack: [0, round(:math.pow(2, 512))], memory: <<1::256>>, active_words: 0}, nil)
9