Skip to content

Commit

Permalink
optimize transaction_exists? by checking local DB first
Browse files Browse the repository at this point in the history
  • Loading branch information
bchamagne authored and Neylix committed Jan 3, 2023
1 parent e7e4d36 commit 47ddf4d
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions lib/archethic.ex
Original file line number Diff line number Diff line change
Expand Up @@ -415,35 +415,41 @@ defmodule Archethic do

@doc """
Check if a transaction exists at address
Check locally first and fallback to a quorum read
"""
@spec transaction_exists?(binary()) :: boolean()
def transaction_exists?(address) do
storage_nodes = Election.chain_storage_nodes(address, P2P.authorized_and_available_nodes())
if TransactionChain.transaction_exists?(address) do
# if it exists locally, no need to query the network
true
else
storage_nodes = Election.chain_storage_nodes(address, P2P.authorized_and_available_nodes())

conflict_resolver = fn results ->
# Prioritize transactions results over not found
case Enum.filter(results, &match?(%TransactionSummary{}, &1)) do
[] ->
%NotFound{}
conflict_resolver = fn results ->
# Prioritize transactions results over not found
case Enum.filter(results, &match?(%TransactionSummary{}, &1)) do
[] ->
%NotFound{}

[first | _] ->
first
[first | _] ->
first
end
end
end

case P2P.quorum_read(
storage_nodes,
%GetTransactionSummary{address: address},
conflict_resolver
) do
{:ok, %TransactionSummary{address: ^address}} ->
true
case P2P.quorum_read(
storage_nodes,
%GetTransactionSummary{address: address},
conflict_resolver
) do
{:ok, %TransactionSummary{address: ^address}} ->
true

{:ok, %NotFound{}} ->
false
{:ok, %NotFound{}} ->
false

{:error, e} ->
raise e
{:error, e} ->
raise e
end
end
end

Expand Down

0 comments on commit 47ddf4d

Please sign in to comment.