Skip to content

Commit

Permalink
feat: added test
Browse files Browse the repository at this point in the history
  • Loading branch information
Godspower-Eze committed Dec 12, 2023
1 parent 0aeffa3 commit 814ca95
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
16 changes: 12 additions & 4 deletions lib/spec/runners/ssz_generic.ex
Original file line number Diff line number Diff line change
Expand Up @@ -53,32 +53,40 @@ defmodule SszGenericTestRunner do
YamlElixir.read_from_file!(case_dir <> "/value.yaml")
|> SpecTestUtils.sanitize_yaml()

expected_root =
%{root: expected_root} =
YamlElixir.read_from_file!(case_dir <> "/meta.yaml")
|> SpecTestUtils.sanitize_yaml()

assert_ssz("valid", schema, real_serialized, expected_value)
assert_ssz("valid", schema, real_serialized, expected_value, expected_root)
end

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
defp assert_ssz(
"valid",
{:container, module},
real_serialized,
real_deserialized,
_hash_tree_root
) 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
defp assert_ssz("valid", schema, real_serialized, real_deserialized, hash_tree_root) do
{:ok, deserialized} = SszEx.decode(real_serialized, schema)
assert deserialized == real_deserialized

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

assert serialized == real_serialized

assert hash_tree_root == SszEx.hash_tree_root!(real_deserialized, schema)
end

defp assert_ssz("invalid", schema, real_serialized) do
Expand Down
20 changes: 8 additions & 12 deletions lib/ssz_ex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,11 @@ defmodule LambdaEthereumConsensus.SszEx do

def decode(binary, module) when is_atom(module), do: decode_container(binary, module)

@spec hash_tree_root!(boolean) :: SszTypes.root()
def hash_tree_root!(value) when is_boolean(value) do
pack(value) |> hash()
end
@spec hash_tree_root!(boolean, atom) :: SszTypes.root()
def hash_tree_root!(value, :bool), do: pack(value)

@spec hash_tree_root!(non_neg_integer) :: SszTypes.root()
def hash_tree_root!(value) when is_integer(value) and value >= 0 do
pack(value) |> hash()
end
@spec hash_tree_root!(non_neg_integer, {:int, non_neg_integer}) :: SszTypes.root()
def hash_tree_root!(value, {:int, size}), do: pack(value, size)

#################
### Private functions
Expand Down Expand Up @@ -395,14 +391,14 @@ defmodule LambdaEthereumConsensus.SszEx do
defp variable_size?({:int, _}), do: false
defp variable_size?({:bytes, _}), do: false

defp pack(value) when is_integer(value) and value >= 0 do
value |> :binary.encode_unsigned(:little) |> String.pad_trailing(32, <<0>>)
defp pack(value, size) when is_integer(value) and value >= 0 do
<<value::size(size)-little>> |> String.pad_trailing(32, <<0>>)
end

defp pack(value) when is_boolean(value) do
case value do
true -> <<1>> |> String.pad_trailing(32, <<0>>)
false -> <<0>> |> String.pad_trailing(32, <<0>>)
true -> <<1::256-little>>
false -> <<0::256>>
end
end
end

0 comments on commit 814ca95

Please sign in to comment.