Skip to content

Commit

Permalink
add ssz_static containers modules
Browse files Browse the repository at this point in the history
  • Loading branch information
f3r10 committed Dec 5, 2023
1 parent dcfe248 commit 403085d
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
defmodule Helpers.SszStaticContainers.FixedTestStruct do
@moduledoc """
Struct definition for `FixedTestStruct`.
"""
@behaviour LambdaEthereumConsensus.Container

fields = [
:A,
:B,
:C
]

@type uint32 :: 0..unquote(2 ** 32 - 1)

@enforce_keys fields
defstruct fields

@type t :: %__MODULE__{
A: SszTypes.uint8(),
B: SszTypes.uint64(),
C: uint32()
}

@impl LambdaEthereumConsensus.Container
def schema do
[
{:A, {:int, 8}},
{:B, {:int, 64}},
{:C, {:int, 32}}
]
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
defmodule Helpers.SszStaticContainers.SingleFieldTestStruct do
@moduledoc """
Struct definition for `SingleFieldTestStruct`.
"""
@behaviour LambdaEthereumConsensus.Container

fields = [
:A
]

@enforce_keys fields
defstruct fields

@type t :: %__MODULE__{
A: SszTypes.uint8()
}

@impl LambdaEthereumConsensus.Container
def schema do
[
{:A, {:int, 8}}
]
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
defmodule Helpers.SszStaticContainers.SmallTestStruct do
@moduledoc """
Struct definition for `SmallTestStruct`.
"""
@behaviour LambdaEthereumConsensus.Container

fields = [
:A,
:B
]

@type uint16 :: 0..unquote(2 ** 16 - 1)

@enforce_keys fields
defstruct fields

@type t :: %__MODULE__{
A: uint16(),
B: uint16()
}

@impl LambdaEthereumConsensus.Container
def schema do
[
{:A, {:int, 16}},
{:B, {:int, 16}}
]
end
end
32 changes: 32 additions & 0 deletions lib/spec/runners/helpers/ssz_static_containers/var_test_struct.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
defmodule Helpers.SszStaticContainers.VarTestStruct do
@moduledoc """
Struct definition for `VarTestStruct`.
"""
@behaviour LambdaEthereumConsensus.Container

fields = [
:A,
:B,
:C
]

@type uint16 :: 0..unquote(2 ** 16 - 1)

@enforce_keys fields
defstruct fields

@type t :: %__MODULE__{
A: uint16(),
B: list(uint16()),
C: SszTypes.uint8()
}

@impl LambdaEthereumConsensus.Container
def schema do
[
{:A, {:int, 16}},
{:B, {:list, {:int, 16}, 1024}},
{:C, {:int, 8}}
]
end
end
63 changes: 49 additions & 14 deletions lib/spec/runners/ssz_generic.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,29 @@ defmodule SszGenericTestRunner do
@disabled [
"basic_vector",
"bitlist",
"bitvector",
"bitvector"
# "boolean",
"containers"
# "containers"
# "uints"
]

@disabled_containers [
# "SingleFieldTestStruct",
# "SmallTestStruct",
# "FixedTestStruct",
# "VarTestStruct",
"ComplexTestStruct",
"BitsStruct"
]

@impl TestRunner
def skip?(%SpecTestCase{fork: fork, handler: handler}) do
fork != "phase0" or Enum.member?(@disabled, handler)
def skip?(%SpecTestCase{fork: fork, handler: handler, case: cse}) do
skip_container? =
@disabled_containers
|> Enum.map(fn container -> String.contains?(cse, container) end)
|> Enum.any?()

fork != "phase0" or Enum.member?(@disabled, handler) or skip_container?
end

@impl TestRunner
Expand All @@ -32,18 +46,26 @@ defmodule SszGenericTestRunner do
handle_case(testcase.suite, schema, decompressed, testcase)
end

defp handle_case("valid", schema, real_deserialized, testcase) do
defp handle_case("valid", schema, real_serialized, testcase) do
case_dir = SpecTestCase.dir(testcase)

expected =
YamlElixir.read_from_file!(case_dir <> "/value.yaml")
|> SpecTestUtils.sanitize_yaml()

assert_ssz("valid", schema, real_deserialized, expected)
assert_ssz("valid", schema, real_serialized, expected)
end

defp handle_case("invalid", schema, real_deserialized, _testcase) do
assert_ssz("invalid", schema, real_deserialized)
defp handle_case("invalid", schema, real_serialized, _testcase) do
assert_ssz("invalid", schema, real_serialized)
end

defp assert_ssz("valid", {:container, module}, real_serialized, real_deserialized) do
real_struct = struct!(module, real_deserialized)
{:ok, deserialized} = SszEx.decode(real_serialized, module)
assert deserialized == real_struct
{:ok, serialized} = SszEx.encode(real_struct, module)
assert serialized == real_serialized
end

defp assert_ssz("valid", schema, real_serialized, real_deserialized) do
Expand All @@ -56,7 +78,7 @@ defmodule SszGenericTestRunner do
end

defp assert_ssz("invalid", schema, real_serialized) do
assert {:error, _error} = SszEx.encode(real_serialized, schema)
catch_error(SszEx.encode(real_serialized, schema))
end

defp parse_type(%SpecTestCase{handler: handler, case: cse}) do
Expand All @@ -69,13 +91,26 @@ defmodule SszGenericTestRunner do
"uint_" <> _rest ->
[_head, size] = Regex.run(~r/^.*?_(.*?)_.*$/, cse)
{:int, String.to_integer(size)}

unknown ->
:error
end

unknown ->
:error
"containers" ->
[name] = Regex.run(~r/^[^_]+(?=_)/, cse)
{:container, Module.concat(Helpers.SszStaticContainers, name)}
# "basic_vector" ->
# case cse do
# "vec_" <> rest ->
# case String.split(rest, "_") do
# ["bool", max_size | _] -> {:vector, :bool, String.to_integer(max_size)}
# ["uint" <> size, max_size | _] ->
# {:vector, {:int, String.to_integer(size)}, String.to_integer(max_size)}
# end
# end
# "bitlist" ->
# case cse do
# "bitlist_" <> rest ->
# [size | _] = String.split(rest, "_")
# {:bitlist, :bool, String.to_integer(size)}
# end
end
end
end
2 changes: 0 additions & 2 deletions lib/ssz_ex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,8 @@ defmodule LambdaEthereumConsensus.SszEx do
@offset_bits 32

defp encode_int(value, size) when is_integer(value), do: {:ok, <<value::size(size)-little>>}
defp encode_int(value, _size), do: {:error, "#{inspect(value)} is not a integer"}
defp encode_bool(true), do: {:ok, "\x01"}
defp encode_bool(false), do: {:ok, "\x00"}
defp encode_bool(not_valid), do: {:error, "#{inspect(not_valid)} not boolean"}

defp decode_uint(binary, size) do
<<element::integer-size(size)-little, _rest::bitstring>> = binary
Expand Down

0 comments on commit 403085d

Please sign in to comment.