Skip to content

Commit

Permalink
chore: add migration for entrypoint (#1433)
Browse files Browse the repository at this point in the history
  • Loading branch information
jyeshe committed Jun 30, 2023
1 parent 1da6406 commit 565dfa3
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions priv/migrations/20230629130000_contract_call_entrypoint.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
defmodule AeMdw.Migrations.ContractCallEntrypoint do
# credo:disable-for-this-file
@moduledoc """
Index contract calls by entrypoint virtual field.
"""

alias AeMdw.Collection
alias AeMdw.Fields
alias AeMdw.Db.Model
alias AeMdw.Db.Origin
alias AeMdw.Db.WriteMutation
alias AeMdw.Db.State
alias AeMdw.Node.Db, as: NodeDb

require Model

@spec run(State.t(), boolean()) :: {:ok, non_neg_integer()}
def run(state, _from_start?) do
tx_count =
case State.get(state, Model.TypeCount, :contract_call_tx) do
{:ok, Model.type_count(count: count)} -> count
:not_found -> 0
end

if tx_count > 0 do
num_tasks = System.schedulers_online() * 4
amount_per_task = trunc(:math.ceil(tx_count / num_tasks))
IO.puts("num_tasks=#{num_tasks} amount_per_task=#{amount_per_task}")

tasks =
Enum.map(0..(num_tasks - 1), fn i ->
cursor = {:contract_call_tx, i * amount_per_task}
boundary = {cursor, {:contract_call_tx, (i + 1) * amount_per_task}}

Task.async(fn ->
state
|> Collection.stream(Model.Type, :forward, boundary, cursor)
|> Stream.map(fn {:contract_call_tx, txi} ->
field_pos = Fields.mdw_field_pos("entrypoint")
fname = get_function_name(state, txi)
m_entrypoint_field = Model.field(index: {:contract_call_tx, field_pos, fname, txi})
WriteMutation.new(Model.Field, m_entrypoint_field)
end)
|> Enum.to_list()
end)
end)

write_mutations =
tasks
|> Task.await_many(60_000 * 30)
|> IO.inspect()
|> List.flatten()

_state = State.commit(state, write_mutations)
{:ok, length(write_mutations)}
else
{:ok, 0}
end
end

def get_function_name(state, txi) do
Model.tx(id: tx_hash) = State.fetch!(state, Model.Tx, txi)
{_block_hash, :contract_call_tx, _singed_tx, tx_rec} = NodeDb.get_tx_data(tx_hash)

contract_pk =
tx_rec
|> :aect_call_tx.contract_id()
|> NodeDb.id_pubkey()

create_txi = Origin.tx_index!(state, {:contract, contract_pk})

Model.contract_call(fun: fname) = State.fetch!(state, Model.ContractCall, {create_txi, txi})
fname
end
end

0 comments on commit 565dfa3

Please sign in to comment.