Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug when there are 10 txs in last page #1362

Merged
merged 1 commit into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading