Skip to content

Commit

Permalink
Resolve bug and Test
Browse files Browse the repository at this point in the history
  • Loading branch information
apoorv-2204 committed Jul 6, 2022
1 parent 5ba428d commit e3f35dd
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 20 deletions.
52 changes: 32 additions & 20 deletions lib/archethic/db/embedded_impl/chain_reader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,34 +64,46 @@ defmodule Archethic.DB.EmbeddedImpl.ChainReader do
{[], false, ""}

{:ok, %{genesis_address: genesis_address}} ->
filepath = ChainWriter.chain_path(db_path, genesis_address)
fd = File.open!(filepath, [:binary, :read])
{transactions, more?, paging_state} =
process_get_chain({address, fields, opts, db_path}, genesis_address)

# Set the file cursor position to the paging state
case Keyword.get(opts, :paging_state) do
nil ->
:file.position(fd, 0)
0
:telemetry.execute([:archethic, :db], %{duration: System.monotonic_time() - start}, %{
query: "get_transaction_chain"
})

paging_address ->
{:ok, %{offset: offset, size: size}} =
ChainIndex.get_tx_entry(paging_address, db_path)
{transactions, more?, paging_state}
end
end

defp process_get_chain({address, fields, opts, db_path}, genesis_address) do
filepath = ChainWriter.chain_path(db_path, genesis_address)
fd = File.open!(filepath, [:binary, :read])
# Set the file cursor position to the paging state
case Keyword.get(opts, :paging_state) do
nil ->
:file.position(fd, 0)
process_get_chain({address, fields}, {fd})

paging_address ->
case ChainIndex.get_tx_entry(paging_address, db_path) do
{:ok, %{offset: offset, size: size}} ->
:file.position(fd, offset + size)
end
process_get_chain({address, fields}, {fd})

column_names = fields_to_column_names(fields)
{:error, :not_exists} ->
{[], false, ""}
end
end
end

# Read the transactions until the nb of transactions to fullfil the page (ie. 10 transactions)
{transactions, more?, paging_state} = scan_chain(fd, column_names, address)
:file.close(fd)
defp process_get_chain({address, fields}, {fd}) do
column_names = fields_to_column_names(fields)

:telemetry.execute([:archethic, :db], %{duration: System.monotonic_time() - start}, %{
query: "get_transaction_chain"
})
# Read the transactions until the nb of transactions to fullfil the page (ie. 10 transactions)
{transactions, more?, paging_state} = scan_chain(fd, column_names, address)
:file.close(fd)

{transactions, more?, paging_state}
end
{transactions, more?, paging_state}
end

defp read_transaction(fd, fields, limit, position, acc \\ %{})
Expand Down
24 changes: 24 additions & 0 deletions test/archethic/db/embedded_impl_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,30 @@ defmodule Archethic.DB.EmbeddedTest do
assert length(page2) == 10
assert page2 == Enum.slice(transactions, 10, 10)
end

test "should return an empty list when the Paging Address is not found" do
transactions =
Enum.map(1..15, fn i ->
TransactionFactory.create_valid_transaction([],
index: i,
timestamp: DateTime.utc_now() |> DateTime.add(i * 60)
)
end)

EmbeddedImpl.write_transaction_chain(transactions)

{page, true, paging_state} =
EmbeddedImpl.get_transaction_chain(List.last(transactions).address)

assert length(page) == 10
assert page == Enum.take(transactions, 10)
assert paging_state == List.last(page).address

assert {[], false, ""} =
EmbeddedImpl.get_transaction_chain(List.last(transactions).address, [],
paging_state: :crypto.strong_rand_bytes(32)
)
end
end

describe "chain_size/1" do
Expand Down

0 comments on commit e3f35dd

Please sign in to comment.