From 2beeb2a2afd550e032fffbef1cb849017b769e58 Mon Sep 17 00:00:00 2001 From: Valentin Atanasov Date: Tue, 29 Oct 2024 08:21:23 +0200 Subject: [PATCH 1/2] fix: aex9 balance at height requirement --- lib/ae_mdw/aex9.ex | 47 +++++++++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/lib/ae_mdw/aex9.ex b/lib/ae_mdw/aex9.ex index b3b4213d3..55e4de291 100644 --- a/lib/ae_mdw/aex9.ex +++ b/lib/ae_mdw/aex9.ex @@ -284,19 +284,33 @@ defmodule AeMdw.Aex9 do nil -> {0, DbUtil.last_gen!(state)} end - streamer = fn - :forward when first_gen <= cursor and cursor <= last_gen -> cursor..last_gen - :backward when cursor == nil -> last_gen..first_gen - :backward when first_gen <= cursor and cursor <= last_gen -> cursor..first_gen - _dir -> first_gen..last_gen - end + streamer = + fn direction -> + case direction do + :forward when first_gen <= cursor and cursor <= last_gen -> cursor..last_gen + :backward when cursor == nil -> last_gen..first_gen + :backward when first_gen <= cursor and cursor <= last_gen -> cursor..first_gen + _dir -> first_gen..last_gen + end + |> Stream.flat_map(fn gen -> + type_height_hash = {:key, gen, DbUtil.height_hash(gen)} + + case Db.aex9_balance(contract_pk, account_pk, type_height_hash) do + {:ok, {amount_or_nil, _height_hash}} -> + [{contract_pk, account_pk, gen, amount_or_nil}] + + _invalid -> + [] + end + end) + end paginated_history = Collection.paginate( streamer, pagination, - &render_balance_history_item(contract_pk, account_pk, &1), - & &1 + &render_balance_history_item/1, + &serialize_history_cursor/1 ) {:ok, paginated_history} @@ -335,15 +349,10 @@ defmodule AeMdw.Aex9 do } end - defp render_balance_history_item(contract_pk, account_pk, gen) do - type_height_hash = {:key, gen, DbUtil.height_hash(gen)} - - {:ok, {amount_or_nil, _height_hash}} = - Db.aex9_balance(contract_pk, account_pk, type_height_hash) - - balance = render_balance(contract_pk, {:address, account_pk}, amount_or_nil) - - Map.put(balance, :height, gen) + defp render_balance_history_item({contract_pk, account_pk, gen, amount_or_nil}) do + contract_pk + |> render_balance({:address, account_pk}, amount_or_nil) + |> Map.put(:height, gen) end defp render_account_balance(state, type_height_hash, {account_pk, contract_pk}) do @@ -385,6 +394,10 @@ defmodule AeMdw.Aex9 do end end + defp serialize_history_cursor({_contract_pk, _account_pk, gen, _amount_or_nil}) do + gen + end + defp serialize_event_balances_cursor({_contract_pk, account_pk}), do: encode_account(account_pk) defp deserialize_event_balances_cursor(nil), do: {:ok, nil} From 1ee69422cd70d9ed55faeb60cd2532901a1ea5e9 Mon Sep 17 00:00:00 2001 From: Valentin Atanasov Date: Tue, 29 Oct 2024 08:33:08 +0200 Subject: [PATCH 2/2] fix: lint issues --- lib/ae_mdw/aex9.ex | 53 +++++++++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 19 deletions(-) diff --git a/lib/ae_mdw/aex9.ex b/lib/ae_mdw/aex9.ex index 55e4de291..d65da55bd 100644 --- a/lib/ae_mdw/aex9.ex +++ b/lib/ae_mdw/aex9.ex @@ -285,25 +285,12 @@ defmodule AeMdw.Aex9 do end streamer = - fn direction -> - case direction do - :forward when first_gen <= cursor and cursor <= last_gen -> cursor..last_gen - :backward when cursor == nil -> last_gen..first_gen - :backward when first_gen <= cursor and cursor <= last_gen -> cursor..first_gen - _dir -> first_gen..last_gen - end - |> Stream.flat_map(fn gen -> - type_height_hash = {:key, gen, DbUtil.height_hash(gen)} - - case Db.aex9_balance(contract_pk, account_pk, type_height_hash) do - {:ok, {amount_or_nil, _height_hash}} -> - [{contract_pk, account_pk, gen, amount_or_nil}] - - _invalid -> - [] - end - end) - end + balance_history_streamer(%{ + account_pk: account_pk, + contract_pk: contract_pk, + range: {first_gen, last_gen}, + cursor: cursor + }) paginated_history = Collection.paginate( @@ -317,6 +304,34 @@ defmodule AeMdw.Aex9 do end end + defp balance_history_streamer(%{ + account_pk: account_pk, + contract_pk: contract_pk, + range: {first_gen, last_gen}, + cursor: cursor + }) do + fn direction -> + direction + |> case do + :forward when first_gen <= cursor and cursor <= last_gen -> cursor..last_gen + :backward when cursor == nil -> last_gen..first_gen + :backward when first_gen <= cursor and cursor <= last_gen -> cursor..first_gen + _dir -> first_gen..last_gen + end + |> Stream.flat_map(fn gen -> + type_height_hash = {:key, gen, DbUtil.height_hash(gen)} + + case Db.aex9_balance(contract_pk, account_pk, type_height_hash) do + {:ok, {amount_or_nil, _height_hash}} -> + [{contract_pk, account_pk, gen, amount_or_nil}] + + _invalid -> + [] + end + end) + end + end + defp serialize_account_balance_cursor(cursor), do: cursor |> :erlang.term_to_binary() |> Base.encode64(padding: false)