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

test: fix random non-deterministic test failures #802

Merged
merged 1 commit into from
Jul 26, 2022
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
176 changes: 102 additions & 74 deletions test/ae_mdw/db/contract_call_mutation_test.exs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
defmodule AeMdw.Db.ContractCallMutationTest do
use ExUnit.Case, async: false

alias AeMdw.Database
alias AeMdw.Db.ContractCallMutation
alias AeMdw.Db.Model
alias AeMdw.Sync.AsyncTasks
alias AeMdw.Db.MemStore
alias AeMdw.Db.Mutation
alias AeMdw.Db.NullStore
alias AeMdw.Db.State
alias AeMdw.Db.Store
alias AeMdw.Sync.AsyncTasks.Consumer
alias AeMdw.Validate

import AeMdw.Node.ContractCallFixtures
Expand All @@ -22,28 +26,26 @@ defmodule AeMdw.Db.ContractCallMutationTest do
@burn_caller_pk <<234, 90, 164, 101, 3, 211, 169, 40, 246, 51, 6, 203, 132, 12, 34, 114, 203,
201, 104, 124, 76, 144, 134, 158, 55, 106, 213, 160, 170, 64, 59, 72>>

setup_all _context do
AsyncTasks.Supervisor.start_link([])

:ok
end

describe "aex9 presence" do
test "add aex9 presence after a mint" do
call_txi = 10_552_888
block_index = {kbi, mbi} = {246_949, 83}

state =
NullStore.new()
|> MemStore.new()
|> Store.put(
Model.Field,
Model.field(index: {:contract_create_tx, nil, @mint_ct_pk, 121})
)
|> State.new()

assert {account_pk, mutation} =
contract_call_mutation("mint", block_index, call_txi, @mint_ct_pk)

contract_pk = @mint_ct_pk
assert %ContractCallMutation{txi: ^call_txi, contract_pk: ^contract_pk} = mutation

Database.dirty_write(
Model.Field,
Model.field(index: {:contract_create_tx, nil, @mint_ct_pk, 121})
)

kb_hash = <<123_451::256>>
next_mb_hash = Validate.id!("mh_2JWXTaf6BzWrTpZMMcBZjxUXX9zNDva2GBT71jhFNiV1ic1gsL")

Expand All @@ -61,18 +63,28 @@ defmodule AeMdw.Db.ContractCallMutationTest do
end
]}
] do
:ets.insert(:aex9_sync_cache, {contract_pk, {kbi, mbi}, call_txi})
Database.commit([mutation])
process_async_task()
state =
state
|> State.commit_mem([mutation])
|> process_async_tasks()

assert Database.exists?(Model.Aex9AccountPresence, {account_pk, contract_pk})
assert State.exists?(state, Model.Aex9AccountPresence, {account_pk, contract_pk})
end
end

test "add aex9 presence after a transfer" do
call_txi = 10_587_359
block_index = {kbi, mbi} = {247_411, 5}

state =
NullStore.new()
|> MemStore.new()
|> Store.put(
Model.Field,
Model.field(index: {:contract_create_tx, nil, @transfer_ct_pk, 122})
)
|> State.new()

assert {account_pk, mutation} =
contract_call_mutation(
"transfer",
Expand All @@ -83,12 +95,6 @@ defmodule AeMdw.Db.ContractCallMutationTest do

contract_pk = @transfer_ct_pk
assert %ContractCallMutation{txi: ^call_txi, contract_pk: ^contract_pk} = mutation

Database.dirty_write(
Model.Field,
Model.field(index: {:contract_create_tx, nil, @transfer_ct_pk, 122})
)

kb_hash = <<123_452::256>>
next_mb_hash = Validate.id!("mh_2Kf3h4eYi77yvMg9HtLMLX9zJtThm6xBCbefCbKQpSi8Rxrcgy")

Expand All @@ -107,18 +113,28 @@ defmodule AeMdw.Db.ContractCallMutationTest do
end
]}
] do
:ets.insert(:aex9_sync_cache, {contract_pk, block_index, call_txi})
Database.commit([mutation])
process_async_task()
state =
state
|> State.commit_mem([mutation])
|> process_async_tasks()

assert Database.exists?(Model.Aex9AccountPresence, {account_pk, contract_pk})
assert State.exists?(state, Model.Aex9AccountPresence, {account_pk, contract_pk})
end
end

test "add aex9 presence after a transfer allowance" do
call_txi = 11_440_639
block_index = {kbi, mbi} = {258_867, 73}

state =
NullStore.new()
|> MemStore.new()
|> Store.put(
Model.Field,
Model.field(index: {:contract_create_tx, nil, @transfer_allow_ct_pk, 123})
)
|> State.new()

assert {account_pk, mutation} =
contract_call_mutation(
"transfer_allowance",
Expand All @@ -130,11 +146,6 @@ defmodule AeMdw.Db.ContractCallMutationTest do
contract_pk = @transfer_allow_ct_pk
assert %ContractCallMutation{txi: ^call_txi, contract_pk: ^contract_pk} = mutation

Database.dirty_write(
Model.Field,
Model.field(index: {:contract_create_tx, nil, @transfer_allow_ct_pk, 123})
)

kb_hash = <<123_453::256>>
next_mb_hash = Validate.id!("mh_2WR69dhBSJLc9gBt6gnHRJCYfCR7BjKNNQteDRshe5CNErghVR")

Expand All @@ -153,29 +164,33 @@ defmodule AeMdw.Db.ContractCallMutationTest do
end
]}
] do
:ets.insert(:aex9_sync_cache, {contract_pk, block_index, call_txi})
Database.commit([mutation])
process_async_task()
state =
state
|> State.commit_mem([mutation])
|> process_async_tasks()

assert Database.exists?(Model.Aex9AccountPresence, {account_pk, contract_pk})
assert State.exists?(state, Model.Aex9AccountPresence, {account_pk, contract_pk})
end
end

test "add aex9 presence after a burn (balance is 0)" do
call_txi = 11_213_118
block_index = {kbi, mbi} = {255_795, 74}

state =
NullStore.new()
|> MemStore.new()
|> Store.put(
Model.Field,
Model.field(index: {:contract_create_tx, nil, @burn_ct_pk, 124})
)
|> State.new()

assert {account_pk, mutation} =
contract_call_mutation("burn", block_index, call_txi, @burn_ct_pk)

contract_pk = @burn_ct_pk
assert %ContractCallMutation{txi: ^call_txi, contract_pk: ^contract_pk} = mutation

Database.dirty_write(
Model.Field,
Model.field(index: {:contract_create_tx, nil, @burn_ct_pk, 124})
)

kb_hash = <<123_454::256>>
next_mb_hash = Validate.id!("mh_9943pc2nXD7BaJjZMwaAYd5Jk4DbPY3THDoh8Sfgy7nTyrZ41")

Expand All @@ -194,23 +209,36 @@ defmodule AeMdw.Db.ContractCallMutationTest do
end
]}
] do
:ets.insert(:aex9_sync_cache, {contract_pk, block_index, call_txi})
Database.commit([mutation])
process_async_task()
state =
state
|> State.commit_mem([mutation])
|> process_async_tasks()

assert Database.exists?(Model.Aex9AccountPresence, {account_pk, contract_pk})
assert State.exists?(state, Model.Aex9AccountPresence, {account_pk, contract_pk})
end
end
end

describe "aex9 transfer" do
test "add aex9 transfers after a call with transfer logs" do
kb_hash = <<123_453::256>>
next_mb_hash = Validate.id!("mh_9943pc2nXD7BaJjZMwaAYd5Jk4DbPY3THDoh8Sfgy7nTyrZ41")

contract_pk =
<<45, 195, 148, 17, 5, 88, 182, 202, 65, 160, 150, 218, 33, 163, 136, 171, 149, 101, 165,
178, 212, 56, 89, 23, 172, 233, 200, 126, 138, 235, 158, 11>>

call_txi = 9_353_623

state =
NullStore.new()
|> MemStore.new()
|> Store.put(
Model.Field,
Model.field(index: {:contract_create_tx, nil, contract_pk, call_txi - 1})
)
|> State.new()

fun_arg_res = %{
arguments: [
%{
Expand Down Expand Up @@ -258,28 +286,34 @@ defmodule AeMdw.Db.ContractCallMutationTest do
call_rec
)

Database.dirty_write(
Model.Field,
Model.field(index: {:contract_create_tx, nil, contract_pk, call_txi - 1})
)

functions =
AeMdw.Node.aex9_signatures()
|> Enum.into(%{}, fn {hash, type} -> {hash, {nil, type, nil}} end)

type_info = {:fcode, functions, nil, nil}
AeMdw.EtsCache.put(AeMdw.Contract, contract_pk, {type_info, nil, nil})

Database.commit([mutation])
process_async_task()

[{^contract_pk, [_transfer_evt_hash | [from_pk, to_pk, <<amount::256>>]], _data}] =
:aect_call.log(call_rec)

assert Database.exists?(Model.Aex9Transfer, {from_pk, call_txi, to_pk, amount, 0})
assert Database.exists?(Model.RevAex9Transfer, {to_pk, call_txi, from_pk, amount, 0})
assert Database.exists?(Model.IdxAex9Transfer, {call_txi, 0, from_pk, to_pk, amount})
assert Database.exists?(Model.Aex9PairTransfer, {from_pk, to_pk, call_txi, amount, 0})
with_mocks [
{AeMdw.Node.Db, [],
[
get_key_block_hash: fn 231_735 -> kb_hash end,
get_next_hash: fn ^kb_hash, 54 -> next_mb_hash end,
aex9_balances: fn ^contract_pk, _next -> {%{}, nil} end
]}
] do
state =
state
|> State.commit_mem([mutation])
|> process_async_tasks()

[{^contract_pk, [_transfer_evt_hash | [from_pk, to_pk, <<amount::256>>]], _data}] =
:aect_call.log(call_rec)

assert State.exists?(state, Model.Aex9Transfer, {from_pk, call_txi, to_pk, amount, 0})
assert State.exists?(state, Model.RevAex9Transfer, {to_pk, call_txi, from_pk, amount, 0})
assert State.exists?(state, Model.IdxAex9Transfer, {call_txi, 0, from_pk, to_pk, amount})
assert State.exists?(state, Model.Aex9PairTransfer, {from_pk, to_pk, call_txi, amount, 0})
end
end
end

Expand Down Expand Up @@ -319,19 +353,13 @@ defmodule AeMdw.Db.ContractCallMutationTest do
{account_pk, mutation}
end

defp process_async_task do
AsyncTasks.Producer.commit_enqueued()
Process.sleep(200)

AsyncTasks.Supervisor
|> Supervisor.which_children()
|> Enum.filter(fn {id, _pid, _type, _mod} ->
is_binary(id) and String.starts_with?(id, "Elixir.AeMdw.Sync.AsyncTasks.Consumer")
defp process_async_tasks(state) do
state.jobs
|> Enum.flat_map(fn {{job_type, dedup_args}, extra_args} ->
Consumer.mutations(job_type, dedup_args ++ extra_args)
end)
|> Enum.each(fn {_id, consumer_pid, _type, _mod} ->
Process.send(consumer_pid, :demand, [:noconnect])
end)

Process.sleep(500)
|> List.flatten()
|> Enum.reject(&is_nil/1)
|> Enum.reduce(state, &Mutation.execute/2)
end
end
3 changes: 0 additions & 3 deletions test/ae_mdw/db/contract_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@ defmodule AeMdw.Db.ContractTest do
alias AeMdw.AexnContracts
alias AeMdw.Db.Contract
alias AeMdw.Db.State
alias AeMdw.Sync.AsyncTasks.Producer

import AeMdw.Node.ContractCallFixtures, only: [call_rec: 1]
import Mock

test "update AEX9 state on non AEX9 contract call with logs having AEX9 contracts" do
assert {:ok, _pid} = Producer.start_link([])

not_aex9_contract_pk =
<<46, 45, 66, 42, 171, 23, 186, 153, 167, 41, 204, 175, 3, 32, 136, 142, 172, 72, 29, 171,
231, 25, 168, 179, 135, 26, 13, 47, 67, 25, 57, 155>>
Expand Down