From 706b7853f93304f7a3c43987d8f61495c8f5e50d Mon Sep 17 00:00:00 2001 From: Sebastian Borrazas Date: Tue, 3 Jan 2023 07:05:04 -0300 Subject: [PATCH] chore: remove tx hashes handling on int contract calls (#1099) Removing the tx_hash handling of events, since it's always going to be the tx hash of the ContractCall transaction. --- lib/ae_mdw/db/format.ex | 1 + lib/ae_mdw/db/mutations/int_calls_mutation.ex | 2 +- lib/ae_mdw/db/sync/contract.ex | 39 +++++++++---------- test/ae_mdw/db/sync/contract_test.exs | 26 ++++++------- .../controllers/contract_controller_test.exs | 6 +-- 5 files changed, 36 insertions(+), 38 deletions(-) diff --git a/lib/ae_mdw/db/format.ex b/lib/ae_mdw/db/format.ex index 6891edc24..ec785d74e 100644 --- a/lib/ae_mdw/db/format.ex +++ b/lib/ae_mdw/db/format.ex @@ -216,6 +216,7 @@ defmodule AeMdw.Db.Format do def to_map(state, {call_txi, local_idx}, Model.IntContractCall) do m_call = State.fetch!(state, Model.IntContractCall, {call_txi, local_idx}) + create_txi = Model.int_contract_call(m_call, :create_txi) fname = Model.int_contract_call(m_call, :fname) diff --git a/lib/ae_mdw/db/mutations/int_calls_mutation.ex b/lib/ae_mdw/db/mutations/int_calls_mutation.ex index fc60e2be8..97b4448e0 100644 --- a/lib/ae_mdw/db/mutations/int_calls_mutation.ex +++ b/lib/ae_mdw/db/mutations/int_calls_mutation.ex @@ -42,7 +42,7 @@ defmodule AeMdw.Db.IntCallsMutation do create_txi = Origin.tx_index!(state, {:contract, contract_pk}) int_calls - |> Enum.reduce(state, fn {local_idx, fname, tx_type, aetx, tx, _tx_hash}, state -> + |> Enum.reduce(state, fn {local_idx, fname, tx_type, aetx, tx}, state -> m_call = Model.int_contract_call( index: {call_txi, local_idx}, diff --git a/lib/ae_mdw/db/sync/contract.ex b/lib/ae_mdw/db/sync/contract.ex index 4c99a3ba0..414958018 100644 --- a/lib/ae_mdw/db/sync/contract.ex +++ b/lib/ae_mdw/db/sync/contract.ex @@ -94,21 +94,20 @@ defmodule AeMdw.Db.Sync.Contract do # Oracle.query events don't have the right nonce (it's hard-coded to 0), which generates an invalid query_id int_calls = non_chain_events - |> Enum.map(fn {_prev_event, {{:internal_call_tx, fname}, %{info: tx, tx_hash: tx_hash}}} -> - {fname, tx, tx_hash} - end) + |> Enum.map(fn {_prev_event, {{:internal_call_tx, fname}, %{info: tx}}} -> {fname, tx} end) |> fix_oracle_queries(block_hash) |> Enum.with_index() - |> Enum.map(fn {{fname, aetx, tx_hash}, local_idx} -> + |> Enum.map(fn {{fname, aetx}, local_idx} -> {tx_type, tx} = :aetx.specialize_type(aetx) - {local_idx, fname, tx_type, aetx, tx, tx_hash} + {local_idx, fname, tx_type, aetx, tx} end) int_calls_mutation = IntCallsMutation.new(contract_pk, call_txi, int_calls) chain_mutations ++ - oracle_and_name_mutations(int_calls, block_index, call_txi) ++ [int_calls_mutation] + oracle_and_name_mutations(int_calls, block_index, call_txi, call_tx_hash) ++ + [int_calls_mutation] end @spec aexn_create_contract_mutation(Db.pubkey(), Blocks.block_index(), Txs.txi()) :: @@ -142,35 +141,35 @@ defmodule AeMdw.Db.Sync.Contract do end end - defp oracle_and_name_mutations(int_calls, {height, _mbi} = block_index, call_txi) do + defp oracle_and_name_mutations(int_calls, {height, _mbi} = block_index, call_txi, tx_hash) do Enum.map(int_calls, fn - {local_idx, "Oracle.extend", :oracle_extend_tx, _aetx, tx, _tx_hash} -> + {local_idx, "Oracle.extend", :oracle_extend_tx, _aetx, tx} -> Oracle.extend_mutation(tx, block_index, {call_txi, local_idx}) - {local_idx, "Oracle.register", :oracle_register_tx, _aetx, tx, tx_hash} -> + {local_idx, "Oracle.register", :oracle_register_tx, _aetx, tx} -> Oracle.register_mutations(tx, tx_hash, block_index, {call_txi, local_idx}) - {_local_idx, "Oracle.respond", :oracle_respond_tx, _aetx, tx, _tx_hash} -> + {_local_idx, "Oracle.respond", :oracle_respond_tx, _aetx, tx} -> Oracle.response_mutation(tx, block_index, call_txi) - {_local_idx, "Oracle.query", :oracle_query_tx, _aetx, tx, _tx_hash} -> + {_local_idx, "Oracle.query", :oracle_query_tx, _aetx, tx} -> Oracle.query_mutation(tx, height) - {local_idx, "AENS.claim", :name_claim_tx, _aetx, tx, tx_hash} -> + {local_idx, "AENS.claim", :name_claim_tx, _aetx, tx} -> Name.name_claim_mutations(tx, tx_hash, block_index, {call_txi, local_idx}) - {local_idx, "AENS.update", :name_update_tx, _aetx, tx, _tx_hash} -> + {local_idx, "AENS.update", :name_update_tx, _aetx, tx} -> Name.update_mutations(tx, {call_txi, local_idx}, block_index, true) - {local_idx, "AENS.transfer", :name_transfer_tx, _aetx, tx, _tx_hash} -> + {local_idx, "AENS.transfer", :name_transfer_tx, _aetx, tx} -> NameTransferMutation.new(tx, {call_txi, local_idx}, block_index) - {local_idx, "AENS.revoke", :name_revoke_tx, _aetx, tx, _tx_hash} -> + {local_idx, "AENS.revoke", :name_revoke_tx, _aetx, tx} -> tx |> :aens_revoke_tx.name_hash() |> NameRevokeMutation.new({call_txi, local_idx}, block_index) - {_local_idx, _fname, _tx_type, _aetx, _tx, _tx_hash} -> + {_local_idx, _fname, _tx_type, _aetx, _tx} -> [] end) end @@ -179,7 +178,7 @@ defmodule AeMdw.Db.Sync.Contract do defp fix_oracle_queries(events, block_hash) do {fixed_events, _acc} = Enum.map_reduce(events, %{}, fn - {"Oracle.query", aetx, tx_hash}, accounts_nonces -> + {"Oracle.query", aetx}, accounts_nonces -> {:oracle_query_tx, tx} = :aetx.specialize_type(aetx) sender_pk = :aeo_query_tx.sender_pubkey(tx) @@ -193,10 +192,10 @@ defmodule AeMdw.Db.Sync.Contract do fixed_tx = put_elem(tx, 2, nonce) fixed_aetx = :aetx.update_tx(aetx, fixed_tx) - {{"Oracle.query", fixed_aetx, tx_hash}, accounts_nonces} + {{"Oracle.query", fixed_aetx}, accounts_nonces} - {fname, aetx, tx_hash}, accounts_nonces -> - {{fname, aetx, tx_hash}, accounts_nonces} + {fname, aetx}, accounts_nonces -> + {{fname, aetx}, accounts_nonces} end) fixed_events diff --git a/test/ae_mdw/db/sync/contract_test.exs b/test/ae_mdw/db/sync/contract_test.exs index 31be3c933..77bd88450 100644 --- a/test/ae_mdw/db/sync/contract_test.exs +++ b/test/ae_mdw/db/sync/contract_test.exs @@ -7,7 +7,6 @@ defmodule AeMdw.Db.Sync.ContractTest do alias AeMdw.Db.AexnCreateContractMutation alias AeMdw.Db.IntCallsMutation alias AeMdw.Db.Sync.Contract, as: SyncContract - alias AeMdw.Db.IntCallsMutation alias AeMdw.Db.NameTransferMutation alias AeMdw.Db.NameUpdateMutation alias AeMdw.Db.OracleExtendMutation @@ -37,13 +36,13 @@ defmodule AeMdw.Db.Sync.ContractTest do tx_2 = {:tx2, account_id} events = [ - {{:internal_call_tx, "Call.amount"}, %{info: tx_1, tx_hash: "tx1-hash"}}, - {{:internal_call_tx, "Call.amount"}, %{info: tx_2, tx_hash: "tx2-hash"}} + {{:internal_call_tx, "Call.amount"}, %{info: tx_1}}, + {{:internal_call_tx, "Call.amount"}, %{info: tx_2}} ] int_calls = [ - {0, "Call.amount", :spend_tx, tx_1, tx_1, "tx1-hash"}, - {1, "Call.amount", :spend_tx, tx_2, tx_1, "tx2-hash"} + {0, "Call.amount", :spend_tx, tx_1, tx_1}, + {1, "Call.amount", :spend_tx, tx_2, tx_1} ] mutation = IntCallsMutation.new(contract_pk, call_txi, int_calls) @@ -70,15 +69,15 @@ defmodule AeMdw.Db.Sync.ContractTest do tx_2 = {:tx2, contract_pk} events = [ - {{:internal_call_tx, "Call.amount"}, %{info: tx_1, tx_hash: "tx1-hash"}}, + {{:internal_call_tx, "Call.amount"}, %{info: tx_1}}, {{:internal_call_tx, "Chain.create"}, %{info: :error}}, - {{:internal_call_tx, "Call.amount"}, %{info: tx_2, tx_hash: "tx2-hash"}}, + {{:internal_call_tx, "Call.amount"}, %{info: tx_2}}, {{:internal_call_tx, "Chain.clone"}, %{info: :error}} ] int_calls = [ - {0, "Call.amount", :spend_tx, tx_1, tx_1, "tx1-hash"}, - {1, "Call.amount", :spend_tx, tx_2, tx_1, "tx2-hash"} + {0, "Call.amount", :spend_tx, tx_1, tx_1}, + {1, "Call.amount", :spend_tx, tx_2, tx_1} ] mutation = IntCallsMutation.new(contract_pk, call_txi, int_calls) @@ -221,12 +220,11 @@ defmodule AeMdw.Db.Sync.ContractTest do call_txi = sync_height * 1_000 block_hash = Validate.id!("mh_FmanZfXX2WWwv6BXMNSDdTgX1TGAknp3Td4aV99SbgRVmEyZo") - tx_hash = Validate.id!("th_2tBJBwA7Z866e5DKA3vckc7iojEXHvTNCenZWo4iaKagfRP4g9") call_tx_hash = <<1::256>> contract_pk = <<2::256>> mutations = - [{{:internal_call_tx, "Oracle.register"}, %{info: aetx, tx_hash: tx_hash}}] + [{{:internal_call_tx, "Oracle.register"}, %{info: aetx}}] |> SyncContract.events_mutations( {sync_height, 0}, block_hash, @@ -258,7 +256,7 @@ defmodule AeMdw.Db.Sync.ContractTest do %IntCallsMutation{ call_txi: ^call_txi, contract_pk: ^contract_pk, - int_calls: [{0, "Oracle.register", ^tx_type, ^aetx, ^tx, ^tx_hash}] + int_calls: [{0, "Oracle.register", ^tx_type, ^aetx, ^tx}] } ] = mutations end @@ -286,7 +284,7 @@ defmodule AeMdw.Db.Sync.ContractTest do contract_pk = <<3::256>> mutations = - [{{:internal_call_tx, "Oracle.extend"}, %{info: aetx, tx_hash: "tx-hash"}}] + [{{:internal_call_tx, "Oracle.extend"}, %{info: aetx}}] |> SyncContract.events_mutations( block_index, block_hash, @@ -309,7 +307,7 @@ defmodule AeMdw.Db.Sync.ContractTest do %IntCallsMutation{ call_txi: ^call_txi, contract_pk: ^contract_pk, - int_calls: [{0, "Oracle.extend", :oracle_extend_tx, ^aetx, ^tx, "tx-hash"}] + int_calls: [{0, "Oracle.extend", :oracle_extend_tx, ^aetx, ^tx}] } ] = mutations ) diff --git a/test/ae_mdw_web/controllers/contract_controller_test.exs b/test/ae_mdw_web/controllers/contract_controller_test.exs index a5490beee..b08675e7f 100644 --- a/test/ae_mdw_web/controllers/contract_controller_test.exs +++ b/test/ae_mdw_web/controllers/contract_controller_test.exs @@ -1102,7 +1102,7 @@ defmodule AeMdwWeb.Controllers.ContractControllerTest do {tx_type, raw_tx} = :aetx.specialize_type(tx) - {i, @call_function, tx_type, tx, raw_tx, "tx-hash"} + {i, @call_function, tx_type, tx, raw_tx} end) contract_pk = :crypto.strong_rand_bytes(32) @@ -1144,7 +1144,7 @@ defmodule AeMdwWeb.Controllers.ContractControllerTest do {tx_type, raw_tx} = :aetx.specialize_type(tx) - {i, @call_function, tx_type, tx, raw_tx, "tx-hash"} + {i, @call_function, tx_type, tx, raw_tx} end) store = @@ -1168,7 +1168,7 @@ defmodule AeMdwWeb.Controllers.ContractControllerTest do {tx_type, raw_tx} = :aetx.specialize_type(tx) - {i, "Call.amount", tx_type, tx, raw_tx, "tx-hash"} + {i, "Call.amount", tx_type, tx, raw_tx} end) extra_ct_pk = :crypto.strong_rand_bytes(32)