Skip to content

Commit 6040929

Browse files
authored
fix: allow retrieving the latest txs by hash (#1167)
1 parent d80f7b5 commit 6040929

File tree

3 files changed

+61
-9
lines changed

3 files changed

+61
-9
lines changed

lib/ae_mdw/blocks.ex

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -161,15 +161,22 @@ defmodule AeMdw.Blocks do
161161

162162
@spec fetch_txis_from_gen(State.t(), height()) :: Enumerable.t()
163163
def fetch_txis_from_gen(state, height) do
164-
with {:ok, Model.block(tx_index: tx_index_start)}
165-
when is_integer(tx_index_start) and tx_index_start >= 0 <-
166-
State.get(state, @table, {height, -1}),
167-
{:ok, Model.block(tx_index: tx_index_end)}
168-
when is_integer(tx_index_end) and tx_index_end >= 0 <-
169-
State.get(state, @table, {height + 1, -1}) do
170-
tx_index_start..tx_index_end
171-
else
172-
_full_block_not_found -> []
164+
case State.get(state, @table, {height, -1}) do
165+
{:ok, Model.block(tx_index: tx_index_start)} ->
166+
tx_index_end =
167+
case State.get(state, @table, {height + 1, -1}) do
168+
{:ok, Model.block(tx_index: tx_index_end)} -> tx_index_end - 1
169+
:not_found -> DbUtil.last_txi!(state)
170+
end
171+
172+
if tx_index_end >= tx_index_start do
173+
tx_index_start..tx_index_end
174+
else
175+
[]
176+
end
177+
178+
:not_found ->
179+
[]
173180
end
174181
end
175182

test/ae_mdw/blocks_test.exs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
defmodule AeMdw.BlocksTest do
2+
use ExUnit.Case
3+
4+
alias AeMdw.Db.State
5+
alias AeMdw.Db.MemStore
6+
alias AeMdw.Db.Model
7+
alias AeMdw.Db.NullStore
8+
alias AeMdw.Db.Store
9+
alias AeMdw.Blocks
10+
11+
require Model
12+
13+
describe "fetch_txis_from_gen/2" do
14+
test "returns the range of txis from any two blocks" do
15+
state =
16+
NullStore.new()
17+
|> MemStore.new()
18+
|> Store.put(Model.Block, Model.block(index: {0, -1}, tx_index: 0))
19+
|> Store.put(Model.Block, Model.block(index: {0, 0}, tx_index: 0))
20+
|> Store.put(Model.Block, Model.block(index: {1, -1}, tx_index: 0))
21+
|> Store.put(Model.Block, Model.block(index: {1, 0}, tx_index: 10))
22+
|> Store.put(Model.Block, Model.block(index: {1, 1}, tx_index: 20))
23+
|> Store.put(Model.Block, Model.block(index: {2, -1}, tx_index: 30))
24+
|> Store.put(Model.Block, Model.block(index: {2, 0}, tx_index: 30))
25+
|> Store.put(Model.Tx, Model.tx(index: 34))
26+
|> State.new()
27+
28+
assert [] = Enum.to_list(Blocks.fetch_txis_from_gen(state, 0))
29+
assert Enum.to_list(0..29) == Enum.to_list(Blocks.fetch_txis_from_gen(state, 1))
30+
assert Enum.to_list(30..34) == Enum.to_list(Blocks.fetch_txis_from_gen(state, 2))
31+
end
32+
end
33+
end

test/integration/ae_mdw_web/controllers/tx_controller_test.exs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ defmodule Integration.AeMdwWeb.TxControllerTest do
4040
assert %{"hash" => ^tx_hash} = body = json_response(conn, 200)
4141
assert %{body: ^body} = MainnetClient.get!(path)
4242
end
43+
44+
test "when getting the last tx by hash, it returns 200", %{conn: conn} do
45+
assert %{"data" => [%{"hash" => tx_hash} = tx]} =
46+
conn
47+
|> get("/v2/txs", limit: 1)
48+
|> json_response(200)
49+
50+
assert ^tx =
51+
conn
52+
|> get("/v2/txs/#{tx_hash}")
53+
|> json_response(200)
54+
end
4355
end
4456

4557
describe "txi" do

0 commit comments

Comments
 (0)