evm v0.1.14 EVM.Helpers

Various helper functions with no other home.

Link to this section Summary

Functions

Defined as Eq.(224) in the Yellow Paper, this is “all but one 64th”, written as L(x)

Encodes signed ints using twos compliment

Helper function to print an instruction message

Inverts a map so each key becomes a value, and vice versa

Left pad binary with bytes

Reads a length of data from a binary, filling in all unknown values as zero

Right pad binary with bytes

Take the last n bytes of a binary

Wrap ints greater than the maximum allowed address size

Wrap ints greater than the max int around back to 0

Link to this section Functions

Link to this function all_but_one_64th(n)
all_but_one_64th(integer) :: integer

Defined as Eq.(224) in the Yellow Paper, this is “all but one 64th”, written as L(x).

Examples

iex> EVM.Helpers.all_but_one_64th(5)
5

iex> EVM.Helpers.all_but_one_64th(1000)
985
Link to this function bit_position(byte_position)
Link to this function decode_signed(n)
decode_signed(integer | nil) :: EVM.val
Link to this function encode_signed(n)
encode_signed(integer) :: EVM.val

Encodes signed ints using twos compliment

Examples

iex> EVM.Helpers.encode_signed(1)
1

iex> EVM.Helpers.encode_signed(-1)
EVM.max_int() - 1
Link to this function encode_val(n)
encode_val(integer | binary | [integer]) :: [EVM.val]
Link to this function inspect(msg, prefix)

Helper function to print an instruction message.

Link to this function invert(m)
invert(map) :: map

Inverts a map so each key becomes a value, and vice versa.

Examples

iex> EVM.Helpers.invert(%{a: 5, b: 10})
%{5 => :a, 10 => :b}

iex> EVM.Helpers.invert(%{dog: "cat"})
%{"cat" => :dog}

iex> EVM.Helpers.invert(%{cow: :moo})
%{moo: :cow}

iex> EVM.Helpers.invert(%{"name" => "bob"})
%{"bob" => "name"}

iex> EVM.Helpers.invert(%{})
%{}
Link to this function left_pad_bytes(n, size \\ EVM.word_size())
left_pad_bytes(binary | integer, integer) :: integer

Left pad binary with bytes

Examples

iex> EVM.Helpers.left_pad_bytes(1, 3)
<<0, 0, 1>>
iex> EVM.Helpers.left_pad_bytes(<<1>>, 3)
<<0, 0, 1>>
iex> EVM.Helpers.left_pad_bytes(<<1, 2, 3>>, 2)
<<1, 2, 3>>
Link to this function read_zero_padded(data, start_pos, read_length)
read_zero_padded(binary, integer, integer) :: binary

Reads a length of data from a binary, filling in all unknown values as zero.

Examples

iex> EVM.Helpers.read_zero_padded(<<5, 6, 7>>, 1, 3)
<<6, 7, 0>>

iex> EVM.Helpers.read_zero_padded(<<5, 6, 7>>, 0, 2)
<<5, 6>>

iex> EVM.Helpers.read_zero_padded(<<5, 6, 7>>, 0, 3)
<<5, 6, 7>>

iex> EVM.Helpers.read_zero_padded(<<5, 6, 7>>, 4, 3)
<<0, 0, 0>>
Link to this function right_pad_bytes(n, size \\ EVM.word_size())
right_pad_bytes(binary | integer, integer) :: integer

Right pad binary with bytes

Examples

iex> EVM.Helpers.right_pad_bytes(1, 3)
<<1, 0, 0>>
iex> EVM.Helpers.right_pad_bytes(<<1>>, 3)
<<1, 0, 0>>
iex> EVM.Helpers.right_pad_bytes(<<1, 2, 3>>, 2)
<<1, 2, 3>>
Link to this function take_n_last_bytes(data, n)
take_n_last_bytes(binary, integer) :: integer

Take the last n bytes of a binary

Link to this function wrap_address(n)

Wrap ints greater than the maximum allowed address size.

Examples

iex> EVM.Helpers.wrap_address(1)
1

iex> EVM.Helpers.wrap_address(<<1>>)
<<1>>

iex> EVM.Helpers.wrap_address(EVM.Address.max() + 1)
1
Link to this function wrap_int(n)
wrap_int(integer) :: EVM.val

Wrap ints greater than the max int around back to 0

Examples

iex> EVM.Helpers.wrap_int(1)
1

iex> EVM.Helpers.wrap_int(EVM.max_int() + 1)
1