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

refactor: save only one txi for aex9 presence #777

Merged
merged 1 commit into from
Jul 7, 2022

Conversation

jyeshe
Copy link
Member

@jyeshe jyeshe commented Jul 5, 2022

What

  • Unifies :derive_aex9_presence with :update_aex9_state
  • Moves aex9 presence txi from key to value

Why

  1. Task under same :update_aex9_state type for a contract call is de-duplicated if enqueued right afterwards an async task created for a contract create.
  2. Update aex9 txi without deleting other record (only the last value is used on aex9 endpoints).
  3. Resolves keep only one aex9 account presence per account and contract pair #459 and fixes Endpoint /aex9/balances/account returning duplicates #735

Validation steps

Run the migration bellow and access:
http://localhost:4000/aex9/balances/account/ak_CNcf2oywqbgmVg3FfKdbHQJfB959wrVwqfzSpdWVKZnep7nj4

It shall return deduped entries (around 30).

defmodule AeMdw.Migrations.AddAex9PresenceTxi do
  alias AeMdw.Database
  alias AeMdw.Db.Model
  alias AeMdw.Db.WriteMutation
  alias AeMdw.Db.State
  alias AeMdw.Log

  require Model

  @spec run(boolean()) :: {:ok, {non_neg_integer(), non_neg_integer()}}
  def run(_from_start?) do
    begin = DateTime.utc_now()

    aex9_mutations =
      Model.Aex9AccountPresence
      |> Database.all_keys()
      |> Enum.into(%{}, fn {account_pk, txi, contract_pk} ->
        {{account_pk, contract_pk}, txi}
      end)
      |> Enum.map(fn {key, txi} ->
        m_presence = Model.aex9_account_presence(index: key, txi: txi)
        WriteMutation.new(Model.Aex9AccountPresence, m_presence)
      end)

    State.commit(State.new(), aex9_mutations)

    indexed_count = length(aex9_mutations)
    duration = DateTime.diff(DateTime.utc_now(), begin)
    Log.info("Indexed #{indexed_count} records in #{duration}s")

    {:ok, {indexed_count, duration}}
  end
end

@jyeshe jyeshe self-assigned this Jul 5, 2022
@jyeshe jyeshe changed the title refact: save only the used txi on aex9 presence refactor: save only the used txi on aex9 presence Jul 6, 2022
@jyeshe jyeshe marked this pull request as ready for review July 6, 2022 13:20
@jyeshe jyeshe requested review from sborrazas and thepiwo July 6, 2022 13:21
@jyeshe jyeshe changed the title refactor: save only the used txi on aex9 presence refactor: save only one txi on aex9 presence Jul 6, 2022
@jyeshe jyeshe changed the title refactor: save only one txi on aex9 presence refactor: save only one txi for aex9 presence Jul 6, 2022
@jyeshe jyeshe merged commit 1c678b5 into master Jul 7, 2022
@jyeshe jyeshe deleted the refactor-aex9-presence branch July 7, 2022 20:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants