Current section
Files
Jump to
Current section
Files
lib/rns/packet_hash_store.ex
defmodule RNS.PacketHashStore do
@moduledoc """
Stores the hashes of all sent/received packets.
This is to be sure no packets are received twice, and also limits attacks on announces which could be used to block some paths.
"""
@spec child_spec(any()) :: map()
def child_spec(_args) do
%{
id: __MODULE__,
start: {__MODULE__, :start_link, []}
}
end
@spec start_link() :: {:ok, pid()}
def start_link() do
pid = spawn_link(__MODULE__, :init, [])
{:ok, pid}
end
# Client
@doc "Adds a packet hash."
@spec add(RNS.Packet.hash()) :: :ok
def add(packet_hash) do
true = :ets.insert(:packet_hashes, {packet_hash})
:ok
end
@doc "Checks if the packet hash is in the table."
@spec exists?(RNS.Packet.hash()) :: boolean()
def exists?(packet_hash) do
:ets.member(:packet_hashes, packet_hash)
end
# Callbacks
@spec init() :: :ok
def init() do
:ets.new(:packet_hashes, [
{:read_concurrency, true},
{:write_concurrency, true},
:public,
:named_table
])
Process.sleep(:infinity)
end
end