Skip to content

Commit

Permalink
[feature] Node Shared Secrets chain explorer
Browse files Browse the repository at this point in the history
  • Loading branch information
apoorv-2204 committed Sep 21, 2022
1 parent 60a4f71 commit 17df9b5
Show file tree
Hide file tree
Showing 25 changed files with 860 additions and 29 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,5 @@ log_*
archethic-node.iml
projectFilesBackup
tags
.vscode/settings.json
/src/c/nat/
4 changes: 2 additions & 2 deletions lib/archethic/bootstrap.ex
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,11 @@ defmodule Archethic.Bootstrap do

@spec get_genesis_addr(:node_shared_secrets | :oracle) :: binary() | nil
defp get_genesis_addr(:oracle) do
Archethic.OracleChain.get_gen_addr().current |> elem(0)
Archethic.OracleChain.genesis_address().current |> elem(0)
end

defp get_genesis_addr(:node_shared_secrets) do
Archethic.SharedSecrets.get_gen_addr(:node_shared_secrets)
Archethic.SharedSecrets.genesis_address(:node_shared_secrets)
end

defp first_initialization(
Expand Down
8 changes: 4 additions & 4 deletions lib/archethic/bootstrap/network_constraints.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ defmodule Archethic.Bootstrap.NetworkConstraints do

@spec persist(:oracle | :reward | :origin | :node_shared_secrets) :: :ok | :error
def persist(:reward) do
case Reward.get_gen_addr() do
case Reward.genesis_address() do
nil ->
Reward.persist_gen_addr()

Expand All @@ -44,7 +44,7 @@ defmodule Archethic.Bootstrap.NetworkConstraints do
end

def persist(:origin) do
case SharedSecrets.get_gen_addr(:origin) do
case SharedSecrets.genesis_address(:origin) do
nil ->
SharedSecrets.persist_gen_addr(:origin)

Expand All @@ -54,7 +54,7 @@ defmodule Archethic.Bootstrap.NetworkConstraints do
end

def persist(:node_shared_secrets) do
case SharedSecrets.get_gen_addr(:node_shared_secrets) do
case SharedSecrets.genesis_address(:node_shared_secrets) do
nil ->
SharedSecrets.persist_gen_addr(:node_shared_secrets)

Expand All @@ -68,7 +68,7 @@ defmodule Archethic.Bootstrap.NetworkConstraints do
OracleChain.update_summ_gen_addr()
Logger.info("Oracle Gen Addr Table: Loaded")

if gen_addr = OracleChain.get_gen_addr() do
if gen_addr = OracleChain.genesis_address() do
Logger.debug("New Oracle Gen Addr")
Logger.debug(gen_addr)
end
Expand Down
4 changes: 2 additions & 2 deletions lib/archethic/crypto.ex
Original file line number Diff line number Diff line change
Expand Up @@ -297,13 +297,13 @@ defmodule Archethic.Crypto do
@doc """
Return the the node shared secrets public key using the node shared secret transaction seed
"""
@spec node_shared_secrets_public_key(index :: number()) :: key()
@spec node_shared_secrets_public_key(index :: non_neg_integer()) :: key()
defdelegate node_shared_secrets_public_key(index), to: SharedSecretsKeystore

@doc """
Return the the network pool public key using the network pool transaction seed
"""
@spec network_pool_public_key(index :: number()) :: key()
@spec network_pool_public_key(index :: non_neg_integer()) :: key()
defdelegate network_pool_public_key(index), to: SharedSecretsKeystore

@doc """
Expand Down
5 changes: 4 additions & 1 deletion lib/archethic/db.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ defmodule Archethic.DB do
@callback write_transaction(Transaction.t()) :: :ok
@callback write_beacon_summary(Summary.t()) :: :ok
@callback write_transaction_chain(Enumerable.t()) :: :ok
@callback write_transaction(Transaction.t()) :: :ok
# @callback write_transaction(Transaction.t()) :: :ok
@callback list_transactions(fields :: list()) :: Enumerable.t()
@callback add_last_transaction_address(binary(), binary(), DateTime.t()) :: :ok
@callback list_last_transaction_addresses() :: Enumerable.t()
Expand All @@ -34,6 +34,9 @@ defmodule Archethic.DB do

@callback list_addresses_by_type(Transaction.transaction_type()) ::
Enumerable.t() | list(binary())
@callback list_chain_addresses(binary()) ::
Enumerable.t() | list({binary(), non_neg_integer()})

@callback get_last_chain_address(binary()) :: {binary(), DateTime.t()}
@callback get_last_chain_address(binary(), DateTime.t()) :: {binary(), DateTime.t()}
@callback get_first_chain_address(binary()) :: binary()
Expand Down
11 changes: 10 additions & 1 deletion lib/archethic/db/embedded_impl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ defmodule Archethic.DB.EmbeddedImpl do

@doc """
Write the transaction chain through the a chain writer which will
happens the transactions to the chain's file
append the transactions to the chain's file
If a transaction already exists it will be discarded
Expand Down Expand Up @@ -179,6 +179,15 @@ defmodule Archethic.DB.EmbeddedImpl do
ChainIndex.get_addresses_by_type(type, db_path())
end

@doc """
Stream all the addresses from the Genesis address(following it).
"""
@spec list_chain_addresses(binary()) ::
Enumerable.t() | list({binary(), non_neg_integer()})
def list_chain_addresses(address) when is_binary(address) do
ChainIndex.list_chain_addresses(address, db_path())
end

@doc """
Count the number of transactions for a given type
"""
Expand Down
35 changes: 35 additions & 0 deletions lib/archethic/db/embedded_impl/chain_index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,41 @@ defmodule Archethic.DB.EmbeddedImpl.ChainIndex do
)
end

@doc """
Stream all the transaction addresses from genesis_address-address.
"""
@spec list_chain_addresses(binary(), String.t()) ::
Enumerable.t() | list({binary(), non_neg_integer()})
def list_chain_addresses(address, db_path) when is_binary(address) do
filepath = chain_addresses_path(db_path, address)

Stream.resource(
fn -> File.open(filepath, [:binary, :read]) end,
fn
{:error, _} ->
{:halt, nil}

{:ok, fd} ->
with {:ok, <<timestamp::64>>} <- :file.read(fd, 8),
{:ok, <<curve_id::8, hash_id::8>>} <- :file.read(fd, 2),
hash_size <- Crypto.hash_size(hash_id),
{:ok, hash} <- :file.read(fd, hash_size) do
address = <<curve_id::8, hash_id::8, hash::binary>>
# return tuple of address and timestamp
{[{address, timestamp}], {:ok, fd}}
else
:eof ->
:file.close(fd)
{:halt, {:ok, fd}}
end
end,
fn
nil -> address
{:ok, fd} -> :file.close(fd)
end
)
end

@doc """
Return the number of transactions for a given type
"""
Expand Down
8 changes: 4 additions & 4 deletions lib/archethic/mining/pending_transaction_validation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ defmodule Archethic.Mining.PendingTransactionValidation do
when type in [:oracle, :oracle_summary] do
# mulitpe txn chain based on summary date

case OracleChain.get_gen_addr() do
case OracleChain.genesis_address() do
nil ->
false

Expand All @@ -127,7 +127,7 @@ defmodule Archethic.Mining.PendingTransactionValidation do
def validate_network_chain?(type, tx = %Transaction{})
when type in [:mint_rewards, :node_rewards] do
# singleton tx chain in network lifespan
case Reward.get_gen_addr() do
case Reward.genesis_address() do
nil ->
false

Expand All @@ -144,7 +144,7 @@ defmodule Archethic.Mining.PendingTransactionValidation do

def validate_network_chain?(:node_shared_secrets, tx = %Transaction{}) do
# singleton tx chain in network lifespan
case SharedSecrets.get_gen_addr(:node_shared_secrets) do
case SharedSecrets.genesis_address(:node_shared_secrets) do
nil ->
false

Expand All @@ -161,7 +161,7 @@ defmodule Archethic.Mining.PendingTransactionValidation do
def validate_network_chain?(:origin, tx = %Transaction{}) do
# singleton tx chain in network lifespan
# not parsing orgin pub key for origin family
case SharedSecrets.get_gen_addr(:origin) do
case SharedSecrets.genesis_address(:origin) do
nil ->
false

Expand Down
4 changes: 2 additions & 2 deletions lib/archethic/oracle_chain.ex
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,8 @@ defmodule Archethic.OracleChain do
@doc """
Returns current and previous summary_time genesis address of oracle chain
"""
@spec get_gen_addr() :: map() | nil
defdelegate get_gen_addr(),
@spec genesis_address() :: map() | nil
defdelegate genesis_address(),
to: MemTable,
as: :get_addr
end
8 changes: 8 additions & 0 deletions lib/archethic/pub_sub.ex
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,14 @@ defmodule Archethic.PubSub do
Registry.register(PubSubRegistry, :node_update, [])
end

@doc """
UnRegister a process to a node update publication
"""
@spec unregister_to_node_update :: :ok
def unregister_to_node_update() do
Registry.unregister(PubSubRegistry, :node_update)
end

@doc """
Register a process to a code deployment publication
"""
Expand Down
4 changes: 2 additions & 2 deletions lib/archethic/reward.ex
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,8 @@ defmodule Archethic.Reward do
end
end

@spec get_gen_addr() :: binary() | nil
def get_gen_addr() do
@spec genesis_address() :: binary() | nil
def genesis_address() do
:persistent_term.get(@key, nil)
end
end
8 changes: 4 additions & 4 deletions lib/archethic/shared_secrets.ex
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,13 @@ defmodule Archethic.SharedSecrets do
end
end

@spec get_gen_addr(:origin) :: binary() | nil
def get_gen_addr(:origin) do
@spec genesis_address(:origin) :: binary() | nil
def genesis_address(:origin) do
:persistent_term.get(@origin_gen_key, nil)
end

@spec get_gen_addr(:node_shared_secrets) :: binary() | nil
def get_gen_addr(:node_shared_secrets) do
@spec genesis_address(:node_shared_secrets) :: binary() | nil
def genesis_address(:node_shared_secrets) do
:persistent_term.get(@nss_gen_key, nil)
end
end
27 changes: 27 additions & 0 deletions lib/archethic/transaction_chain.ex
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ defmodule Archethic.TransactionChain do
@spec list_addresses_by_type(Transaction.transaction_type()) :: Enumerable.t() | list(binary())
defdelegate list_addresses_by_type(type), to: DB

@doc """
Stream all the addresses in chronological belonging to a genesis address
"""
@spec list_chain_addresses(binary()) :: Enumerable.t() | list({binary(), non_neg_integer()})
defdelegate list_chain_addresses(genesis_address), to: DB

@doc """
Get the last transaction address from a transaction chain with the latest time
"""
Expand Down Expand Up @@ -125,6 +131,27 @@ defmodule Archethic.TransactionChain do
@doc """
Retrieve an entire chain from the last transaction
The returned list is ordered chronologically.
## Example
tx0->tx1->tx2->tx3->tx4->tx5->tx6->tx7->tx8->tx9->tx10->tx11->tx12->tx13->tx14->tx15->tx16
Query: TransactionChain.get(tx5.address)
tx0->tx1->tx2->tx3->tx4->tx5
Query: TransactionChain.get(tx15.address)
tx0->tx1->tx2->tx3->tx4->tx5->tx6->tx7->tx8->tx9->tx10
more?: true
paging_address: tx10.address
Query: TransactionChain.get(tx15.address, paging_address: tx10.address)
tx11->tx12->tx13->tx14->tx15->tx16
more?: false
paging_address: nil
Query: TransactionChain.get(tx4.address, paging_address: tx4.address)
tx5->tx6->tx7->tx8->tx9->tx10->tx11->tx12->tx13->tx14
more?: true
paging_address: tx15.address
"""
@spec get(binary(), list()) ::
Enumerable.t() | {list(Transaction.t()), boolean(), binary()}
Expand Down
Loading

0 comments on commit 17df9b5

Please sign in to comment.