Skip to content

Commit

Permalink
Fix bug when there are 10 txs in last page
Browse files Browse the repository at this point in the history
  • Loading branch information
bchamagne authored and Neylix committed Jan 5, 2024
1 parent 3503bee commit f9cb2ca
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/archethic/db/embedded_impl/chain_reader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ defmodule Archethic.DB.EmbeddedImpl.ChainReader do
all_addresses_asc
|> Enum.find_index(&(&1 == paging_state))

if paging_state_idx < @page_size do
if paging_state_idx <= @page_size do
{paging_state_idx, nil, false, nil}
else
idx = paging_state_idx - 1 - @page_size
Expand Down
43 changes: 41 additions & 2 deletions test/archethic/db/embedded_impl_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ defmodule Archethic.DB.EmbeddedTest do
end
end

describe "get_transaction_chain/2" do
describe "get_transaction_chain/1-3 order: :asc" do
test "should return an empty list when the transaction chain is not found" do
assert {[], false, nil} = EmbeddedImpl.get_transaction_chain(:crypto.strong_rand_bytes(32))
end
Expand Down Expand Up @@ -349,7 +349,7 @@ defmodule Archethic.DB.EmbeddedTest do
end
end

describe "get_transaction_chain/4 order: :desc" do
describe "get_transaction_chain/1-3 order: :desc" do
test "should return empty when there is no transactions" do
{pub_key, _} = Crypto.generate_deterministic_keypair("SEED")
address = Crypto.derive_address(pub_key)
Expand Down Expand Up @@ -416,6 +416,45 @@ defmodule Archethic.DB.EmbeddedTest do
assert length(page3) == 8
assert page1 ++ page2 ++ page3 == Enum.reverse(transactions)
end

test "should be able to load the last page if there are 10 transactions (for a page_size=10)" do
transactions =
Enum.map(1..30, fn i ->
tx =
TransactionFactory.create_valid_transaction([],
index: i,
timestamp: DateTime.utc_now() |> DateTime.add(i * 60)
)

EmbeddedImpl.write_transaction(tx)

tx
end)

{page1, true, paging_state1} =
EmbeddedImpl.get_transaction_chain(List.last(transactions).address, [], order: :desc)

assert length(page1) == 10
assert paging_state1 == List.last(page1).address

{page2, true, paging_state2} =
EmbeddedImpl.get_transaction_chain(List.last(transactions).address, [],
paging_state: paging_state1,
order: :desc
)

assert length(page2) == 10
assert paging_state2 == List.last(page2).address

{page3, false, nil} =
EmbeddedImpl.get_transaction_chain(List.last(transactions).address, [],
paging_state: paging_state2,
order: :desc
)

assert length(page3) == 10
assert page1 ++ page2 ++ page3 == Enum.reverse(transactions)
end
end

describe "chain_size/1" do
Expand Down

0 comments on commit f9cb2ca

Please sign in to comment.