From ab11f5a89a82fd6b3de92918178bed4a25d56537 Mon Sep 17 00:00:00 2001 From: nova <146671001+ovnanova@users.noreply.github.com> Date: Fri, 20 Sep 2024 01:33:35 -0700 Subject: [PATCH] the rent was too damn high so the mst files had to move rip mstnode --- lib/hexpds/mst.ex | 130 --------------------------------------- lib/hexpds/mstnode.ex | 17 ----- lib/hexpds/mststorage.ex | 9 --- lib/hexpds/mstutil.ex | 51 --------------- 4 files changed, 207 deletions(-) delete mode 100644 lib/hexpds/mst.ex delete mode 100644 lib/hexpds/mstnode.ex delete mode 100644 lib/hexpds/mststorage.ex delete mode 100644 lib/hexpds/mstutil.ex diff --git a/lib/hexpds/mst.ex b/lib/hexpds/mst.ex deleted file mode 100644 index d89de4d..0000000 --- a/lib/hexpds/mst.ex +++ /dev/null @@ -1,130 +0,0 @@ -defmodule Hexpds.MST do - alias Hexpds.MST.MSTNode, as: MSTNode - @hash_fun :sha256 - - def terminal_node?(%MSTNode{subtrees: subtrees}) do - Enum.empty?(subtrees) - end - - def build_tree(data_list) when is_list(data_list) do - leaves = Enum.map(data_list, &create_node(&1)) - build_nodes(leaves, nil) - end - - defp create_node(data) do - # hash = :crypto.hash(@hash_fun, data) - # %MSTNode{hash: hash, subtrees: []} - end - - defp build_nodes([single_node], _parent) do - single_node - end - - # defp build_nodes(nodes, _parent) do - # paired_nodes = Enum.chunk_every(nodes, 2, 2, :undefined) - - # parent_nodes = - # Enum.map(paired_nodes, fn [left = %MSTNode{hash: left_hash}, right] -> - # right_hash = - # case right do - # # %MSTNode{hash: hash} -> hash - # :undefined -> "" - # _ -> :crypto.hash(@hash_fun, "") - # end - # end) - - # build_nodes(parent_nodes, nil) - # end - - defp build_nodes([single_hash], _parent) do - # This is a single node tree (or a root for its subtree), no parent is needed. - single_hash - end - - defp build_nodes(hashes, parent) do - paired_hashes = Enum.chunk_every(hashes, 2, 2, :undefined) - - parent_hashes = - Enum.map(paired_hashes, fn [left, right] -> - {left_hash, right_hash} = {left || :undefined, right || :undefined} - combined = if right_hash == :undefined, do: left_hash, else: left_hash <> right_hash - hash = :crypto.hash(@hash_fun, combined) - # Insert each parent node into the ETS table. Each parent node contains - # its hash, the hashes of its left and right children, and its own parent. - :ets.insert(:merkle_tree, {hash, left_hash, right_hash, parent}) - hash - end) - - build_nodes(parent_hashes, nil) - end - - # defp leaf_hash(leaf) when is_nil(leaf), do: "" - # defp leaf_hash(%Hexpds.MST.MSTNode{hash: hash}), do: hash - - def verify(tree_hash, data) do - # Hmmm - end - - def depth(key), do: hash_depth(:crypto.hash(:sha256, key)) - def hash_depth(key, depth \\ 0) - - def hash_depth(<<0::2, rest::bitstring>>, depth), do: hash_depth(rest, depth + 1) - - def hash_depth(<<_::2, _rest::bitstring>>, depth), do: depth -end - -defmodule Hexpds.MSTServer do - use GenServer - alias Hexpds.MST.MSTNode, as: MSTNode - @hash_fun :sha256 - - # Starting the server with an empty root - @spec start_link(any()) :: :ignore | {:error, any()} | {:ok, pid()} - def start_link(%MSTNode{subtrees: [nil], keys: [], vals: []} = args) do - GenServer.start_link(__MODULE__, args, name: __MODULE__) - end - - # GenServer callback for initialization - def init(_) do - # Initialize the ETS table with the name :merkle_tree, making it a set - # where each object is unique and public so all processes can access it. - # The :keypos option indicates the position of the key in the tuple (2nd position here). - table = :ets.new(:merkle_tree, [:set, :public, {:keypos, 2}]) - {:ok, table} - end - - # Other callbacks, get, put, etc. - def handle_call({:get, key}, _from, state) do - # To-do - # Pattern match on the state - # Recursively search the tree for the key - # {:reply, value, state} - end - - def handle_call({:add_data, data}, from, state), do: {:reply, add_data(data, state)} - - def handle_cast({:put, key, value}, state) do - # To-do - # Insert the key-value pair into the tree - # Recursively adjust the tree structure - # {:noreply, new_state} - end - - def handle_cast({:delete, key}, state) do - # Implement logic to remove a key-value pair from the tree - # Adjust the tree structure accordingly - # {:noreply, new_state} - end - - def add_data(data, table) do - hash = :crypto.hash(@hash_fun, data) - # Insert a tuple into the ETS table with the structure {hash, data}. - :ets.insert(table, {hash, data}) - hash - end - - # def build_tree(data_list) when is_list(data_list) do - # hashed_data = Enum.map(data_list, &add_data()) - # build_nodes(hashed_data, nil) - # end -end diff --git a/lib/hexpds/mstnode.ex b/lib/hexpds/mstnode.ex deleted file mode 100644 index 1aa0ccb..0000000 --- a/lib/hexpds/mstnode.ex +++ /dev/null @@ -1,17 +0,0 @@ -defmodule Hexpds.MST.MSTNode do - @moduledoc """ - Represents a node in the merkle tree structure. It can function as both a - leaf (is_terminal_node) and an internal node. - - - 'keys' - collection of rkeys - - 'vals' - CID references to data - - 'subtrees' - optional CID references to child nodes - """ - @type t :: %__MODULE__{ - keys: [String.t()], - vals: [Hexpds.CID.t()], - subtrees: [Hexpds.CID.t() | nil] - } - - defstruct keys: [], vals: [], subtrees: [] -end diff --git a/lib/hexpds/mststorage.ex b/lib/hexpds/mststorage.ex deleted file mode 100644 index e957bf9..0000000 --- a/lib/hexpds/mststorage.ex +++ /dev/null @@ -1,9 +0,0 @@ -defmodule Hexpds.MST.MSTStorage do - @moduledoc """ - Simplified storage module for MST. - """ - def read_obj(_pointer) do - # TODO - %{} - end -end diff --git a/lib/hexpds/mstutil.ex b/lib/hexpds/mstutil.ex deleted file mode 100644 index e78954a..0000000 --- a/lib/hexpds/mstutil.ex +++ /dev/null @@ -1,51 +0,0 @@ -defmodule Hexpds.MST.MSTUtil do - @moduledoc """ - Utility functions for MST operations. - """ - - @spec ensure_valid_key(String.t()) :: :ok | no_return() - def ensure_valid_key(key) do - if String.length(key) > 0 do - :ok - else - raise "Invalid key: Key must be a non-empty string." - end - end - - @spec leading_zeros_on_hash(String.t()) :: integer() - def leading_zeros_on_hash(key) do - hash = :crypto.hash(:sha256, key) - hash - |> :binary.bin_to_list() - |> Enum.reduce_while(0, fn byte, acc -> - case byte do - 0 -> {:cont, acc + 8} - _ -> {:halt, acc + leading_zeros(byte)} - end - end) - end - - @spec leading_zeros(integer()) :: integer() - defp leading_zeros(byte) do - bits = :erlang.integer_to_binary(byte, 2) - String.length(String.trim_leading(bits, "0")) - end - - @spec cid_for_entries(list()) :: binary() - def cid_for_entries(entries) do - data = serialize_node_data(entries) - :crypto.hash(:sha256, data) - end - - @spec serialize_node_data(list()) :: binary() - def serialize_node_data(entries) do - # CBOR serialization - :erlang.term_to_binary(entries) - end - - @spec deserialize_node_data(module(), binary()) :: list() - def deserialize_node_data(_storage, data) do - # CBOR deserialization - :erlang.binary_to_term(data) - end -end