Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement ssz_generic spec tests #490

Merged
81 changes: 81 additions & 0 deletions lib/spec/runners/ssz_generic.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
defmodule SszGenericTestRunner do
@moduledoc """
Runner for SSZ general test cases. `run_test_case/1` is the main entrypoint.
"""
alias LambdaEthereumConsensus.SszEx
use ExUnit.CaseTemplate
use TestRunner

@disabled [
"basic_vector",
"bitlist",
"bitvector",
# "boolean",
"containers"
# "uints"
]

@impl TestRunner
def skip?(%SpecTestCase{fork: fork, handler: handler}) do
fork != "phase0" or Enum.member?(@disabled, handler)
end

@impl TestRunner
def run_test_case(%SpecTestCase{} = testcase) do
case_dir = SpecTestCase.dir(testcase)

schema = parse_type(testcase)

compressed = File.read!(case_dir <> "/serialized.ssz_snappy")
assert {:ok, decompressed} = :snappyer.decompress(compressed)

handle_case(testcase.suite, schema, decompressed, testcase)
end

defp handle_case("valid", schema, real_deserialized, 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)
end

defp handle_case("invalid", schema, real_deserialized, _testcase) do
assert_ssz("invalid", schema, real_deserialized)
end
MegaRedHand marked this conversation as resolved.
Show resolved Hide resolved

defp assert_ssz("valid", schema, real_serialized, real_deserialized) do
{:ok, deserialized} = SszEx.decode(real_serialized, schema)
assert deserialized == real_deserialized

{:ok, serialized} = SszEx.encode(real_deserialized, schema)

assert serialized == real_serialized
end

defp assert_ssz("invalid", schema, real_serialized) do
assert {:error, _error} = SszEx.encode(real_serialized, schema)
MegaRedHand marked this conversation as resolved.
Show resolved Hide resolved
end

defp parse_type(%SpecTestCase{handler: handler, case: cse}) do
case handler do
"boolean" ->
:bool

"uints" ->
case cse do
"uint_" <> _rest ->
[_head, size] = Regex.run(~r/^.*?_(.*?)_.*$/, cse)
{:int, String.to_integer(size)}

unknown ->

Check warning on line 73 in lib/spec/runners/ssz_generic.ex

View workflow job for this annotation

GitHub Actions / Build project

variable "unknown" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 73 in lib/spec/runners/ssz_generic.ex

View workflow job for this annotation

GitHub Actions / Test

variable "unknown" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 73 in lib/spec/runners/ssz_generic.ex

View workflow job for this annotation

GitHub Actions / Run spec-tests (general)

variable "unknown" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 73 in lib/spec/runners/ssz_generic.ex

View workflow job for this annotation

GitHub Actions / Run spec-tests (general)

variable "unknown" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 73 in lib/spec/runners/ssz_generic.ex

View workflow job for this annotation

GitHub Actions / Run spec-tests (mainnet)

variable "unknown" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 73 in lib/spec/runners/ssz_generic.ex

View workflow job for this annotation

GitHub Actions / Run spec-tests (mainnet)

variable "unknown" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 73 in lib/spec/runners/ssz_generic.ex

View workflow job for this annotation

GitHub Actions / Run spec-tests (minimal)

variable "unknown" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 73 in lib/spec/runners/ssz_generic.ex

View workflow job for this annotation

GitHub Actions / Run spec-tests (minimal)

variable "unknown" is unused (if the variable is not meant to be used, prefix it with an underscore)
MegaRedHand marked this conversation as resolved.
Show resolved Hide resolved
:error
end

unknown ->

Check warning on line 77 in lib/spec/runners/ssz_generic.ex

View workflow job for this annotation

GitHub Actions / Build project

variable "unknown" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 77 in lib/spec/runners/ssz_generic.ex

View workflow job for this annotation

GitHub Actions / Test

variable "unknown" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 77 in lib/spec/runners/ssz_generic.ex

View workflow job for this annotation

GitHub Actions / Run spec-tests (general)

variable "unknown" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 77 in lib/spec/runners/ssz_generic.ex

View workflow job for this annotation

GitHub Actions / Run spec-tests (general)

variable "unknown" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 77 in lib/spec/runners/ssz_generic.ex

View workflow job for this annotation

GitHub Actions / Run spec-tests (mainnet)

variable "unknown" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 77 in lib/spec/runners/ssz_generic.ex

View workflow job for this annotation

GitHub Actions / Run spec-tests (mainnet)

variable "unknown" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 77 in lib/spec/runners/ssz_generic.ex

View workflow job for this annotation

GitHub Actions / Run spec-tests (minimal)

variable "unknown" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 77 in lib/spec/runners/ssz_generic.ex

View workflow job for this annotation

GitHub Actions / Run spec-tests (minimal)

variable "unknown" is unused (if the variable is not meant to be used, prefix it with an underscore)
MegaRedHand marked this conversation as resolved.
Show resolved Hide resolved
:error
end
end
end
2 changes: 2 additions & 0 deletions lib/ssz_ex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ 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"}
MegaRedHand marked this conversation as resolved.
Show resolved Hide resolved
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
Loading