Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions lib/examples/e_local_domain.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ defmodule Examples.ELocalDomain do
I provide the examples for Anoma.LocalDomain
"""

use ExExample
use Anoma.LocalDomain

require ExUnit.Assertions
import ExUnit.Assertions
use Anoma.LocalDomain

@spec sigil_key_segment_pattern_match() :: String.t()
def sigil_key_segment_pattern_match() do

example sigil_key_segment_pattern_match do
c = "segment c"

assert ~k"/a/b/!c" == ["a", "b", "segment c"]
Expand All @@ -17,15 +18,17 @@ defmodule Examples.ELocalDomain do

assert c == "matched c"

c
c
end

@spec sigil_rest_segment_pattern_match() :: [String.t()]
def sigil_rest_segment_pattern_match() do
example sigil_rest_segment_pattern_match do
:rest

~k"/a/&rest" = ["a", "b", "c"]

assert rest == ["b", "c"]
end

def rerun?(_), do: false

end
8 changes: 8 additions & 0 deletions lib/examples/e_merkle_tree.ex
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ defmodule Examples.EMerkleTree do
{frontiers, root}
end

def generate_a_proof_wrongly() do
tree = expand_merkle_tree()

assert nil == MerkleTree.generate_proof(tree, :crypto.hash(:sha256, "d"))

:ok
end

def verify_a_proof() do
{frontiers, root} = generate_a_proof()
MerkleTree.verify_proof(:crypto.hash(:sha256, "b"), frontiers, root)
Expand Down
4 changes: 4 additions & 0 deletions lib/examples/e_node.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
defmodule Examples.ENode do
@moduledoc """
I provide the examples for LocalDomain nodes
"""

require ExUnit.Assertions
import ExUnit.Assertions

Expand Down
51 changes: 0 additions & 51 deletions lib/examples/e_poller.ex

This file was deleted.

9 changes: 7 additions & 2 deletions lib/examples/e_storage.ex
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
defmodule Examples.EStorage do
@moduledoc """
I contain examples that test Anoma.LocalDomain.Storage
"""

use Anoma.LocalDomain
use ExExample

alias Examples.ENode
alias Anoma.LocalDomain.Storage

import ExUnit.Assertions

@spec read_and_write_to_node() :: String.t()
def read_and_write_to_node() do
example read_and_write_to_node() do
{:ok, node_id, pid} = ENode.start_node()
Storage.write_local(node_id, ~k"/k1/k2", "val")

Expand Down
34 changes: 21 additions & 13 deletions lib/local_domain/structures/merkle_tree.ex
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,34 @@ defmodule Anoma.LocalDomain.MerkleTree do
{frontiers, root} =
for i <- 0..(depth(tree) - 1), reduce: {[], leaf} do
{acc, leaf} ->
if (leaf == nil) do
{acc, leaf}
else
leaves = Map.get(tree.nodes, i)

leaf_index =
leaves
|> Enum.find_index(&(&1 == leaf))
leaf_index =
leaves
|> Enum.find_index(&(&1 == leaf))

if (leaf_index != nil) do

is_left = (leaf_index &&& 1) == 0
is_left = (leaf_index &&& 1) == 0

if is_left do
neighbour = Enum.at(leaves, leaf_index + 1)
if is_left do
neighbour = Enum.at(leaves, leaf_index + 1)

{acc ++ [{neighbour, true}], hash(leaf <> neighbour)}
else
neighbour = Enum.at(leaves, leaf_index - 1)
{acc ++ [{neighbour, true}], hash(leaf <> neighbour)}
else
neighbour = Enum.at(leaves, leaf_index - 1)

{acc ++ [{neighbour, false}], hash(neighbour <> leaf)}
{acc ++ [{neighbour, false}], hash(neighbour <> leaf)}
end
else
{acc, nil}
end
end

if root == root(tree) do
end
end
if root == root(tree) && root != nil do
{frontiers, root}
else
nil
Expand Down
Loading