From e5df7b565155cb2a2c2a95cc1034979b765bc3e8 Mon Sep 17 00:00:00 2001 From: Sebastian Borrazas Date: Fri, 23 Dec 2022 09:53:10 -0300 Subject: [PATCH] feat: introduce {bi, {txi, local_idx}} for precise internal txs refs (#1088) New transactions created by contract calls were only referenced by the txi. With this new implementation, we can now point exactly to the internal contract call that created the transaction, without having to search for it. NOTE: requies a full re-sync to index all of the existing contract call indexes for claims/updates/transfers/etc --- lib/ae_mdw/auction_bids.ex | 6 +- lib/ae_mdw/blocks.ex | 1 + lib/ae_mdw/db/format.ex | 18 ++- lib/ae_mdw/db/model.ex | 24 +-- lib/ae_mdw/db/mutations/int_calls_mutation.ex | 10 +- .../db/mutations/name_claim_mutation.ex | 45 +++--- .../db/mutations/name_revoke_mutation.ex | 17 +- .../db/mutations/name_transfer_mutation.ex | 14 +- .../db/mutations/name_update_mutation.ex | 14 +- .../db/mutations/oracle_extend_mutation.ex | 14 +- .../db/mutations/oracle_register_mutation.ex | 14 +- lib/ae_mdw/db/name.ex | 70 +------- lib/ae_mdw/db/sync/contract.ex | 79 ++++----- lib/ae_mdw/db/sync/name.ex | 46 +++--- lib/ae_mdw/db/sync/oracle.ex | 13 +- lib/ae_mdw/db/sync/transaction.ex | 12 +- lib/ae_mdw/db/util.ex | 29 ++++ lib/ae_mdw/names.ex | 106 ++++++------ lib/ae_mdw/oracles.ex | 12 +- lib/ae_mdw/txs.ex | 2 + test/ae_mdw/db/name_claim_mutation_test.exs | 12 +- test/ae_mdw/db/name_revoke_mutation_test.exs | 8 +- test/ae_mdw/db/name_test.exs | 4 +- test/ae_mdw/db/name_update_mutation_test.exs | 12 +- test/ae_mdw/db/names_expire_mutation_test.exs | 8 +- .../ae_mdw/db/oracle_expire_mutation_test.exs | 6 +- test/ae_mdw/db/sync/contract_test.exs | 28 ++-- test/ae_mdw/db/sync/name_test.exs | 9 +- test/ae_mdw/db/sync/oracle_test.exs | 5 +- test/ae_mdw/db/sync/transaction_test.exs | 2 +- .../controllers/contract_controller_test.exs | 6 +- .../controllers/name_controller_test.exs | 153 ++++++++++++------ .../controllers/oracle_controller_test.exs | 6 +- test/support/test_samples.ex | 14 +- 34 files changed, 418 insertions(+), 401 deletions(-) diff --git a/lib/ae_mdw/auction_bids.ex b/lib/ae_mdw/auction_bids.ex index 11283f897..22b24b6b5 100644 --- a/lib/ae_mdw/auction_bids.ex +++ b/lib/ae_mdw/auction_bids.ex @@ -86,7 +86,7 @@ defmodule AeMdw.AuctionBids do ), _opts ) do - last_bid = Txs.fetch!(state, bi_txi_txi(last_bid)) + last_bid = Txs.fetch!(state, bi_txi_idx_txi(last_bid)) name_ttl = Names.expire_after(expire_height) %{ @@ -96,13 +96,13 @@ defmodule AeMdw.AuctionBids do info: %{ auction_end: expire_height, last_bid: put_in(last_bid, ["tx", "ttl"], name_ttl), - bids: Enum.map(bids, &bi_txi_txi/1) + bids: Enum.map(bids, &bi_txi_idx_txi/1) }, previous: Names.fetch_previous_list(state, plain_name) } end - defp bi_txi_txi({{_height, _mbi}, txi}), do: txi + defp bi_txi_idx_txi({{_height, _mbi}, {txi, _idx}}), do: txi defp serialize_exp_cursor(nil), do: nil diff --git a/lib/ae_mdw/blocks.ex b/lib/ae_mdw/blocks.ex index a7e9eb9d3..0d25145db 100644 --- a/lib/ae_mdw/blocks.ex +++ b/lib/ae_mdw/blocks.ex @@ -25,6 +25,7 @@ defmodule AeMdw.Blocks do @type block_index_txi_pos() :: {height(), txi_pos()} @type block_index_txi() :: {block_index(), Txs.txi()} @type bi_txi() :: block_index_txi() + @type bi_txi_idx() :: {block_index(), Txs.txi_idx()} @type key_header() :: term() @type block_hash() :: <<_::256>> diff --git a/lib/ae_mdw/db/format.ex b/lib/ae_mdw/db/format.ex index 4b157382a..6891edc24 100644 --- a/lib/ae_mdw/db/format.ex +++ b/lib/ae_mdw/db/format.ex @@ -21,7 +21,7 @@ defmodule AeMdw.Db.Format do ########## - def bi_txi_txi({{_height, _mbi}, txi}), do: txi + def bi_txi_idx_txi({{_height, _mbi}, {txi, _idx}}), do: txi def to_raw_map(_state, {{height, mbi}, txi}), do: %{block_height: height, micro_index: mbi, tx_index: txi} @@ -450,10 +450,10 @@ defmodule AeMdw.Db.Format do %{ active_from: active_h, expire_height: expire_h, - claims: Enum.map(cs, &bi_txi_txi/1), - updates: Enum.map(us, &bi_txi_txi/1), - transfers: Enum.map(ts, &bi_txi_txi/1), - revoke: (revoke && bi_txi_txi(revoke)) || nil, + claims: Enum.map(cs, &bi_txi_idx_txi/1), + updates: Enum.map(us, &bi_txi_idx_txi/1), + transfers: Enum.map(ts, &bi_txi_idx_txi/1), + revoke: (revoke && bi_txi_idx_txi(revoke)) || nil, auction_timeout: auction_tm, pointers: Name.pointers(state, name), ownership: Name.ownership(state, name) @@ -462,7 +462,11 @@ defmodule AeMdw.Db.Format do defp auction_bid( state, - Model.auction_bid(index: plain, expire_height: auction_end, bids: [{_, txi} | _] = bids), + Model.auction_bid( + index: plain, + expire_height: auction_end, + bids: [{_bi, {txi, _idx}} | _rest_bids] = bids + ), key, tx_fmt, info_fmt @@ -479,7 +483,7 @@ defmodule AeMdw.Db.Format do key.(:info) => %{ key.(:auction_end) => auction_end, key.(:last_bid) => last_bid, - key.(:bids) => Enum.map(bids, &bi_txi_txi/1) + key.(:bids) => Enum.map(bids, &bi_txi_idx_txi/1) }, key.(:previous) => case Name.locate(state, plain) do diff --git a/lib/ae_mdw/db/model.ex b/lib/ae_mdw/db/model.ex index fba8a7350..1e8082e6f 100644 --- a/lib/ae_mdw/db/model.ex +++ b/lib/ae_mdw/db/model.ex @@ -34,9 +34,11 @@ defmodule AeMdw.Db.Model do @typep pubkey :: Db.pubkey() @typep tx_type() :: Node.tx_type() @typep txi() :: Txs.txi() + # @typep txi_idx() :: Txs.txi_idx() @typep log_idx() :: AeMdw.Contracts.log_idx() @typep tx_hash() :: Txs.tx_hash() - @typep bi_txi() :: Blocks.block_index_txi() + @typep bi_txi() :: Blocks.bi_txi() + @typep bi_txi_idx() :: Blocks.bi_txi_idx() @typep query_id() :: Oracles.query_id() @typep amount() :: non_neg_integer() @typep fname() :: Contract.fname() @@ -180,7 +182,7 @@ defmodule AeMdw.Db.Model do # index = {plain_name, {block_index, txi}, expire_height = height, owner = pk, prev_bids = []} @auction_bid_defaults [ index: nil, - block_index_txi: nil, + block_index_txi_idx: nil, expire_height: nil, owner: nil, bids: [] @@ -191,10 +193,10 @@ defmodule AeMdw.Db.Model do @type auction_bid :: record(:auction_bid, index: auction_bid_index(), - block_index_txi: bi_txi(), + block_index_txi_idx: bi_txi_idx(), expire_height: Blocks.height(), owner: pubkey(), - bids: [bi_txi()] + bids: [bi_txi_idx()] ) # activation: @@ -251,10 +253,10 @@ defmodule AeMdw.Db.Model do index: name_index(), active: Blocks.height(), expire: Blocks.height(), - claims: [bi_txi()], - updates: [bi_txi()], - transfers: [bi_txi()], - revoke: bi_txi() | nil, + claims: [bi_txi_idx()], + updates: [bi_txi_idx()], + transfers: [bi_txi_idx()], + revoke: bi_txi_idx() | nil, auction_timeout: non_neg_integer(), owner: pubkey(), previous: record(:name) | nil @@ -281,7 +283,7 @@ defmodule AeMdw.Db.Model do @pointee_defaults [index: {nil, {{nil, nil}, nil}, nil}, unused: nil] defrecord :pointee, @pointee_defaults - @type pointee_index() :: {pubkey(), bi_txi(), pubkey()} + @type pointee_index() :: {pubkey(), bi_txi_idx(), pubkey()} @type pointee() :: record(:pointee, index: pointee_index()) # in 2 tables: active_oracle, inactive_oracle @@ -311,8 +313,8 @@ defmodule AeMdw.Db.Model do index: oracle_index(), active: Blocks.height(), expire: Blocks.height(), - register: bi_txi(), - extends: [bi_txi()], + register: bi_txi_idx(), + extends: [bi_txi_idx()], previous: oracle() | nil ) diff --git a/lib/ae_mdw/db/mutations/int_calls_mutation.ex b/lib/ae_mdw/db/mutations/int_calls_mutation.ex index e95fa5bbf..fc60e2be8 100644 --- a/lib/ae_mdw/db/mutations/int_calls_mutation.ex +++ b/lib/ae_mdw/db/mutations/int_calls_mutation.ex @@ -17,7 +17,8 @@ defmodule AeMdw.Db.IntCallsMutation do @derive AeMdw.Db.Mutation defstruct [:contract_pk, :call_txi, :int_calls] - @typep int_call() :: {Contract.fname(), Node.tx_type(), Node.tx(), Node.aetx()} + @typep int_call() :: + {Contract.local_idx(), Contract.fname(), Node.tx_type(), Node.tx(), Node.aetx()} @opaque t() :: %__MODULE__{ contract_pk: Db.pubkey(), call_txi: Txs.txi(), @@ -41,14 +42,13 @@ defmodule AeMdw.Db.IntCallsMutation do create_txi = Origin.tx_index!(state, {:contract, contract_pk}) int_calls - |> Enum.with_index() - |> Enum.reduce(state, fn {{fname, tx_type, tx, raw_tx}, local_idx}, state -> + |> Enum.reduce(state, fn {local_idx, fname, tx_type, aetx, tx, _tx_hash}, state -> m_call = Model.int_contract_call( index: {call_txi, local_idx}, create_txi: create_txi, fname: fname, - tx: tx + tx: aetx ) m_grp_call = Model.grp_int_contract_call(index: {create_txi, call_txi, local_idx}) @@ -67,7 +67,7 @@ defmodule AeMdw.Db.IntCallsMutation do tx_type |> Node.tx_ids() |> Enum.reduce(state2, fn {_field, pos}, state -> - pk = Validate.id!(elem(raw_tx, pos)) + pk = Validate.id!(elem(tx, pos)) m_id_call = Model.id_int_contract_call(index: {pk, pos, call_txi, local_idx}) m_grp_id_call = diff --git a/lib/ae_mdw/db/mutations/name_claim_mutation.ex b/lib/ae_mdw/db/mutations/name_claim_mutation.ex index eb53b05e5..129b74099 100644 --- a/lib/ae_mdw/db/mutations/name_claim_mutation.ex +++ b/lib/ae_mdw/db/mutations/name_claim_mutation.ex @@ -4,7 +4,6 @@ defmodule AeMdw.Db.NameClaimMutation do """ alias AeMdw.Blocks - alias AeMdw.Db.Format alias AeMdw.Db.IntTransfer alias AeMdw.Db.Model alias AeMdw.Db.State @@ -25,7 +24,7 @@ defmodule AeMdw.Db.NameClaimMutation do :owner_pk, :name_fee, :lima_or_higher?, - :txi, + :txi_idx, :block_index, :timeout ] @@ -36,7 +35,7 @@ defmodule AeMdw.Db.NameClaimMutation do owner_pk: Db.pubkey(), name_fee: Names.name_fee(), lima_or_higher?: boolean(), - txi: Txs.txi(), + txi_idx: Txs.txi_idx(), block_index: Blocks.block_index(), timeout: Names.auction_timeout() } @@ -47,18 +46,27 @@ defmodule AeMdw.Db.NameClaimMutation do Db.pubkey(), Names.name_fee(), boolean(), - Txs.txi(), + Txs.txi_idx(), Blocks.block_index(), Names.auction_timeout() ) :: t() - def new(plain_name, name_hash, owner_pk, name_fee, lima_or_higher?, txi, block_index, timeout) do + def new( + plain_name, + name_hash, + owner_pk, + name_fee, + lima_or_higher?, + txi_idx, + block_index, + timeout + ) do %__MODULE__{ plain_name: plain_name, name_hash: name_hash, owner_pk: owner_pk, name_fee: name_fee, lima_or_higher?: lima_or_higher?, - txi: txi, + txi_idx: txi_idx, block_index: block_index, timeout: timeout } @@ -72,7 +80,7 @@ defmodule AeMdw.Db.NameClaimMutation do owner_pk: owner_pk, name_fee: name_fee, lima_or_higher?: lima_or_higher?, - txi: txi, + txi_idx: {txi, _idx} = txi_idx, block_index: {height, _mbi} = block_index, timeout: timeout }, @@ -93,7 +101,7 @@ defmodule AeMdw.Db.NameClaimMutation do index: plain_name, active: height, expire: expire, - claims: [{block_index, txi}], + claims: [{block_index, txi_idx}], owner: owner_pk, previous: previous, auction_timeout: 0 @@ -124,7 +132,7 @@ defmodule AeMdw.Db.NameClaimMutation do make_m_bid = &Model.auction_bid( index: plain_name, - block_index_txi: {block_index, txi}, + block_index_txi_idx: {block_index, txi_idx}, expire_height: auction_end, owner: owner_pk, bids: &1 @@ -136,16 +144,17 @@ defmodule AeMdw.Db.NameClaimMutation do case Name.cache_through_read(state, Model.AuctionBid, plain_name) do nil -> state4 = State.inc_stat(state3, :auctions_started) - {make_m_bid.([{block_index, txi}]), state4} + {make_m_bid.([{block_index, txi_idx}]), state4} {:ok, Model.auction_bid( - block_index_txi: {_bi, prev_txi}, + block_index_txi_idx: {_bi, {prev_txi, _prev_idx} = prev_txi_idx}, expire_height: prev_auction_end, owner: prev_owner, bids: prev_bids )} -> - %{tx: %{name_fee: prev_name_fee}} = read_cached_raw_tx!(state, prev_txi) + prev_name_claim_tx = DbUtil.read_node_tx(state, prev_txi_idx) + prev_name_fee = :aens_claim_tx.name_fee(prev_name_claim_tx) state4 = state3 @@ -164,7 +173,7 @@ defmodule AeMdw.Db.NameClaimMutation do ) |> State.inc_stat(:locked_in_auctions, name_fee - prev_name_fee) - {make_m_bid.([{block_index, txi} | prev_bids]), state4} + {make_m_bid.([{block_index, txi_idx} | prev_bids]), state4} end state4 @@ -173,14 +182,4 @@ defmodule AeMdw.Db.NameClaimMutation do |> Name.cache_through_write(Model.AuctionExpiration, m_auction_exp) end end - - defp read_raw_tx!(state, txi), - do: Format.to_raw_map(state, DbUtil.read_tx!(state, txi)) - - defp read_cached_raw_tx!(state, txi) do - case :ets.lookup(:tx_sync_cache, txi) do - [{^txi, m_tx}] -> Format.to_raw_map(state, m_tx) - [] -> read_raw_tx!(state, txi) - end - end end diff --git a/lib/ae_mdw/db/mutations/name_revoke_mutation.ex b/lib/ae_mdw/db/mutations/name_revoke_mutation.ex index 29c5beb1e..88e84f892 100644 --- a/lib/ae_mdw/db/mutations/name_revoke_mutation.ex +++ b/lib/ae_mdw/db/mutations/name_revoke_mutation.ex @@ -10,25 +10,28 @@ defmodule AeMdw.Db.NameRevokeMutation do alias AeMdw.Txs @derive AeMdw.Db.Mutation - defstruct [:name_hash, :txi, :block_index] + defstruct [:name_hash, :txi_idx, :block_index] @opaque t() :: %__MODULE__{ name_hash: Names.name_hash(), - txi: Txs.txi(), + txi_idx: Txs.txi_idx(), block_index: Blocks.block_index() } - @spec new(Names.name_hash(), Txs.txi(), Blocks.block_index()) :: t() - def new(name_hash, txi, block_index) do + @spec new(Names.name_hash(), Txs.txi_idx(), Blocks.block_index()) :: t() + def new(name_hash, txi_idx, block_index) do %__MODULE__{ name_hash: name_hash, - txi: txi, + txi_idx: txi_idx, block_index: block_index } end @spec execute(t(), State.t()) :: State.t() - def execute(%__MODULE__{name_hash: name_hash, txi: txi, block_index: block_index}, state) do - Name.revoke(state, name_hash, txi, block_index) + def execute( + %__MODULE__{name_hash: name_hash, txi_idx: txi_idx, block_index: block_index}, + state + ) do + Name.revoke(state, name_hash, txi_idx, block_index) end end diff --git a/lib/ae_mdw/db/mutations/name_transfer_mutation.ex b/lib/ae_mdw/db/mutations/name_transfer_mutation.ex index f724a17c8..71d08d367 100644 --- a/lib/ae_mdw/db/mutations/name_transfer_mutation.ex +++ b/lib/ae_mdw/db/mutations/name_transfer_mutation.ex @@ -12,24 +12,24 @@ defmodule AeMdw.Db.NameTransferMutation do alias AeMdw.Txs @derive AeMdw.Db.Mutation - defstruct [:name_hash, :new_owner, :txi, :block_index] + defstruct [:name_hash, :new_owner, :txi_idx, :block_index] @opaque t() :: %__MODULE__{ name_hash: Names.name_hash(), new_owner: Db.pubkey(), - txi: Txs.txi(), + txi_idx: Txs.txi_idx(), block_index: Blocks.block_index() } - @spec new(Node.tx(), Txs.txi(), Blocks.block_index()) :: t() - def new(tx, txi, block_index) do + @spec new(Node.tx(), Txs.txi_idx(), Blocks.block_index()) :: t() + def new(tx, txi_idx, block_index) do name_hash = :aens_transfer_tx.name_hash(tx) new_owner = :aens_transfer_tx.recipient_pubkey(tx) %__MODULE__{ name_hash: name_hash, new_owner: new_owner, - txi: txi, + txi_idx: txi_idx, block_index: block_index } end @@ -39,11 +39,11 @@ defmodule AeMdw.Db.NameTransferMutation do %__MODULE__{ name_hash: name_hash, new_owner: new_owner, - txi: txi, + txi_idx: txi_idx, block_index: block_index }, state ) do - Name.transfer(state, name_hash, new_owner, txi, block_index) + Name.transfer(state, name_hash, new_owner, txi_idx, block_index) end end diff --git a/lib/ae_mdw/db/mutations/name_update_mutation.ex b/lib/ae_mdw/db/mutations/name_update_mutation.ex index a29bc2793..75a46cf24 100644 --- a/lib/ae_mdw/db/mutations/name_update_mutation.ex +++ b/lib/ae_mdw/db/mutations/name_update_mutation.ex @@ -10,13 +10,13 @@ defmodule AeMdw.Db.NameUpdateMutation do alias AeMdw.Txs @derive AeMdw.Db.Mutation - defstruct [:name_hash, :update_type, :pointers, :txi, :block_index, :internal?] + defstruct [:name_hash, :update_type, :pointers, :txi_idx, :block_index, :internal?] @opaque t() :: %__MODULE__{ name_hash: Names.name_hash(), update_type: Name.update_type(), pointers: Names.pointers(), - txi: Txs.txi(), + txi_idx: Txs.txi_idx(), block_index: Blocks.block_index() } @@ -24,15 +24,15 @@ defmodule AeMdw.Db.NameUpdateMutation do Names.name_hash(), Name.update_type(), Names.pointers(), - Txs.txi(), + Txs.txi_idx(), Blocks.block_index() ) :: t() - def new(name_hash, update_type, pointers, txi, block_index) do + def new(name_hash, update_type, pointers, txi_idx, block_index) do %__MODULE__{ name_hash: name_hash, update_type: update_type, pointers: pointers, - txi: txi, + txi_idx: txi_idx, block_index: block_index } end @@ -43,11 +43,11 @@ defmodule AeMdw.Db.NameUpdateMutation do name_hash: name_hash, update_type: update_type, pointers: pointers, - txi: txi, + txi_idx: txi_idx, block_index: block_index }, state ) do - Name.update(state, name_hash, update_type, pointers, txi, block_index) + Name.update(state, name_hash, update_type, pointers, txi_idx, block_index) end end diff --git a/lib/ae_mdw/db/mutations/oracle_extend_mutation.ex b/lib/ae_mdw/db/mutations/oracle_extend_mutation.ex index e349fe1a3..4ffd906c8 100644 --- a/lib/ae_mdw/db/mutations/oracle_extend_mutation.ex +++ b/lib/ae_mdw/db/mutations/oracle_extend_mutation.ex @@ -16,20 +16,20 @@ defmodule AeMdw.Db.OracleExtendMutation do require Model @derive AeMdw.Db.Mutation - defstruct [:block_index, :txi, :oracle_pk, :delta_ttl] + defstruct [:block_index, :txi_idx, :oracle_pk, :delta_ttl] @opaque t() :: %__MODULE__{ block_index: Blocks.block_index(), - txi: Txs.txi(), + txi_idx: Txs.txi_idx(), oracle_pk: Db.pubkey(), delta_ttl: Blocks.height() } - @spec new(Blocks.block_index(), Txs.txi(), Db.pubkey(), Blocks.height()) :: t() - def new(block_index, txi, oracle_pk, delta_ttl) do + @spec new(Blocks.block_index(), Txs.txi_idx(), Db.pubkey(), Blocks.height()) :: t() + def new(block_index, txi_idx, oracle_pk, delta_ttl) do %__MODULE__{ block_index: block_index, - txi: txi, + txi_idx: txi_idx, oracle_pk: oracle_pk, delta_ttl: delta_ttl } @@ -39,7 +39,7 @@ defmodule AeMdw.Db.OracleExtendMutation do def execute( %__MODULE__{ block_index: {height, _mbi} = block_index, - txi: txi, + txi_idx: txi_idx, oracle_pk: oracle_pk, delta_ttl: delta_ttl }, @@ -48,7 +48,7 @@ defmodule AeMdw.Db.OracleExtendMutation do case Oracle.cache_through_read(state, Model.ActiveOracle, oracle_pk) do {:ok, Model.oracle(expire: old_expire, extends: extends) = m_oracle} -> new_expire = old_expire + delta_ttl - extends = [{block_index, txi} | extends] + extends = [{block_index, txi_idx} | extends] m_exp = Model.expiration(index: {new_expire, oracle_pk}) m_oracle = Model.oracle(m_oracle, expire: new_expire, extends: extends) diff --git a/lib/ae_mdw/db/mutations/oracle_register_mutation.ex b/lib/ae_mdw/db/mutations/oracle_register_mutation.ex index f42333688..40fada9db 100644 --- a/lib/ae_mdw/db/mutations/oracle_register_mutation.ex +++ b/lib/ae_mdw/db/mutations/oracle_register_mutation.ex @@ -14,7 +14,7 @@ defmodule AeMdw.Db.OracleRegisterMutation do require Model @derive AeMdw.Db.Mutation - defstruct [:oracle_pk, :block_index, :expire, :txi] + defstruct [:oracle_pk, :block_index, :expire, :txi_idx] @typep expiration() :: Blocks.height() @@ -22,16 +22,16 @@ defmodule AeMdw.Db.OracleRegisterMutation do oracle_pk: Db.pubkey(), block_index: Blocks.block_index(), expire: expiration(), - txi: Txs.txi() + txi_idx: Txs.txi_idx() } - @spec new(Db.pubkey(), Blocks.block_index(), expiration(), Txs.txi()) :: t() - def new(oracle_pk, block_index, expire, txi) do + @spec new(Db.pubkey(), Blocks.block_index(), expiration(), Txs.txi_idx()) :: t() + def new(oracle_pk, block_index, expire, txi_idx) do %__MODULE__{ oracle_pk: oracle_pk, block_index: block_index, expire: expire, - txi: txi + txi_idx: txi_idx } end @@ -41,7 +41,7 @@ defmodule AeMdw.Db.OracleRegisterMutation do oracle_pk: oracle_pk, block_index: {height, _mbi} = block_index, expire: expire, - txi: txi + txi_idx: txi_idx }, state ) do @@ -72,7 +72,7 @@ defmodule AeMdw.Db.OracleRegisterMutation do index: oracle_pk, active: height, expire: expire, - register: {block_index, txi}, + register: {block_index, txi_idx}, previous: previous ) diff --git a/lib/ae_mdw/db/name.ex b/lib/ae_mdw/db/name.ex index 05cfdb416..7be126fdf 100644 --- a/lib/ae_mdw/db/name.ex +++ b/lib/ae_mdw/db/name.ex @@ -12,13 +12,10 @@ defmodule AeMdw.Db.Name do """ alias AeMdw.Blocks alias AeMdw.Collection - alias AeMdw.Contracts - alias AeMdw.Node, as: AE alias AeMdw.Node.Db alias AeMdw.Db.Model alias AeMdw.Db.Format alias AeMdw.Db.State - alias AeMdw.Db.Sync.InnerTx alias AeMdw.Db.Util, as: DbUtil alias AeMdw.Names alias AeMdw.Validate @@ -118,33 +115,10 @@ defmodule AeMdw.Db.Name do @spec pointers(state(), Model.name()) :: map() def pointers(_state, Model.name(updates: [])), do: %{} - def pointers( - state, - Model.name(index: plain_name, updates: [{_block_index, txi} | _rest_updates]) - ) do - Model.tx(id: tx_hash) = DbUtil.read_tx!(state, txi) - {:ok, name_hash} = :aens.get_name_hash(plain_name) - - pointers = - case AE.Db.get_tx_data(tx_hash) do - {_block_hash, :name_update_tx, _signed_tx, tx_rec} -> - :aens_update_tx.pointers(tx_rec) - - {_block_hash, :contract_call_tx, _signed_tx, _tx_rec} -> - state - |> Contracts.fetch_int_contract_calls(txi, "AENS.update") - |> Stream.map(fn Model.int_contract_call(tx: aetx) -> - {:name_update_tx, tx} = :aetx.specialize_type(aetx) - - tx - end) - |> Enum.find(fn tx -> - name_hash == :aens_update_tx.name_hash(tx) - end) - |> :aens_update_tx.pointers() - end - - pointers + def pointers(state, Model.name(updates: [{_block_index, txi_idx} | _rest_updates])) do + state + |> DbUtil.read_node_tx(txi_idx) + |> :aens_update_tx.pointers() |> Enum.into(%{}, &pointer_kv_raw/1) |> Format.encode_pointers() end @@ -162,42 +136,14 @@ defmodule AeMdw.Db.Name do def ownership( state, Model.name( - index: plain_name, - claims: [{_block_index, last_claim_txi} | _rest_claims], + claims: [{_block_index, last_claim_txi_idx} | _rest_claims], owner: owner ) ) do - Model.tx(id: tx_hash) = DbUtil.read_tx!(state, last_claim_txi) - {:ok, name_hash} = :aens.get_name_hash(plain_name) - orig_owner = - case AE.Db.get_tx_data(tx_hash) do - {_block_hash, :name_claim_tx, _signed_tx, tx_rec} -> - :aens_claim_tx.account_id(tx_rec) - - {_block_hash, :contract_call_tx, _signed_tx, _tx_rec} -> - state - |> Contracts.fetch_int_contract_calls(last_claim_txi, "AENS.claim") - |> Stream.map(fn Model.int_contract_call(tx: aetx) -> - {:name_claim_tx, tx} = :aetx.specialize_type(aetx) - - tx - end) - |> Enum.find(fn tx -> - name_hash == :aens_transfer_tx.name_hash(tx) - end) - |> :aens_transfer_tx.account_id() - - {_block_hash, tx_type, _signed_tx, tx_rec} - when tx_type in [:ga_meta_tx, :paying_for_tx] -> - {:name_claim_tx, tx_rec} = - tx_type - |> InnerTx.signed_tx(tx_rec) - |> :aetx_sign.tx() - |> :aetx.specialize_type() - - :aens_claim_tx.account_id(tx_rec) - end + state + |> DbUtil.read_node_tx(last_claim_txi_idx) + |> :aens_claim_tx.account_id() %{original: orig_owner, current: :aeser_id.create(:account, owner)} end diff --git a/lib/ae_mdw/db/sync/contract.ex b/lib/ae_mdw/db/sync/contract.ex index 55b64bd16..4c99a3ba0 100644 --- a/lib/ae_mdw/db/sync/contract.ex +++ b/lib/ae_mdw/db/sync/contract.ex @@ -30,7 +30,7 @@ defmodule AeMdw.Db.Sync.Contract do Txs.txi(), Txs.tx_hash() ) :: [Mutation.t()] - def child_contract_mutations({:error, _any}, _block_index, _txi, _tx_hash), do: [] + def child_contract_mutations({:error, _any}, _block_index, _txi_idx, _tx_hash), do: [] def child_contract_mutations(%{result: fun_result}, block_index, txi, tx_hash) do with %{type: :contract, value: contract_id} <- fun_result, @@ -94,18 +94,21 @@ 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}}} -> {fname, tx} end) + |> Enum.map(fn {_prev_event, {{:internal_call_tx, fname}, %{info: tx, tx_hash: tx_hash}}} -> + {fname, tx, tx_hash} + end) |> fix_oracle_queries(block_hash) - |> Enum.map(fn {fname, tx} -> - {tx_type, raw_tx} = :aetx.specialize_type(tx) + |> Enum.with_index() + |> Enum.map(fn {{fname, aetx, tx_hash}, local_idx} -> + {tx_type, tx} = :aetx.specialize_type(aetx) - {fname, tx_type, tx, raw_tx} + {local_idx, fname, tx_type, aetx, tx, tx_hash} end) int_calls_mutation = IntCallsMutation.new(contract_pk, call_txi, int_calls) chain_mutations ++ - oracle_and_name_mutations(events, block_index, call_txi) ++ [int_calls_mutation] + oracle_and_name_mutations(int_calls, block_index, call_txi) ++ [int_calls_mutation] end @spec aexn_create_contract_mutation(Db.pubkey(), Blocks.block_index(), Txs.txi()) :: @@ -139,54 +142,36 @@ defmodule AeMdw.Db.Sync.Contract do end end - defp oracle_and_name_mutations(events, {height, _mbi} = block_index, call_txi) do - events - |> Enum.filter(fn - {{:internal_call_tx, "Oracle.extend"}, _info} -> true - {{:internal_call_tx, "Oracle.register"}, _info} -> true - {{:internal_call_tx, "Oracle.respond"}, _info} -> true - {{:internal_call_tx, "Oracle.query"}, _info} -> true - {{:internal_call_tx, "AENS.claim"}, _info} -> true - {{:internal_call_tx, "AENS.update"}, _info} -> true - {{:internal_call_tx, "AENS.transfer"}, _info} -> true - {{:internal_call_tx, "AENS.revoke"}, _info} -> true - _int_call -> false - end) - |> Enum.map(fn - {{:internal_call_tx, "Oracle.extend"}, %{info: aetx}} -> - {:oracle_extend_tx, tx} = :aetx.specialize_type(aetx) - Oracle.extend_mutation(tx, block_index, call_txi) + defp oracle_and_name_mutations(int_calls, {height, _mbi} = block_index, call_txi) do + Enum.map(int_calls, fn + {local_idx, "Oracle.extend", :oracle_extend_tx, _aetx, tx, _tx_hash} -> + Oracle.extend_mutation(tx, block_index, {call_txi, local_idx}) - {{:internal_call_tx, "Oracle.register"}, %{info: aetx, tx_hash: tx_hash}} -> - {:oracle_register_tx, tx} = :aetx.specialize_type(aetx) - Oracle.register_mutations(tx, tx_hash, block_index, call_txi) + {local_idx, "Oracle.register", :oracle_register_tx, _aetx, tx, tx_hash} -> + Oracle.register_mutations(tx, tx_hash, block_index, {call_txi, local_idx}) - {{:internal_call_tx, "Oracle.respond"}, %{info: aetx}} -> - {:oracle_response_tx, tx} = :aetx.specialize_type(aetx) + {_local_idx, "Oracle.respond", :oracle_respond_tx, _aetx, tx, _tx_hash} -> Oracle.response_mutation(tx, block_index, call_txi) - {{:internal_call_tx, "Oracle.query"}, %{info: aetx}} -> - {:oracle_query_tx, tx} = :aetx.specialize_type(aetx) + {_local_idx, "Oracle.query", :oracle_query_tx, _aetx, tx, _tx_hash} -> Oracle.query_mutation(tx, height) - {{:internal_call_tx, "AENS.claim"}, %{info: aetx, tx_hash: tx_hash}} -> - {:name_claim_tx, tx} = :aetx.specialize_type(aetx) - Name.name_claim_mutations(tx, tx_hash, block_index, call_txi) + {local_idx, "AENS.claim", :name_claim_tx, _aetx, tx, tx_hash} -> + Name.name_claim_mutations(tx, tx_hash, block_index, {call_txi, local_idx}) - {{:internal_call_tx, "AENS.update"}, %{info: aetx}} -> - {:name_update_tx, tx} = :aetx.specialize_type(aetx) - Name.update_mutations(tx, call_txi, block_index, true) + {local_idx, "AENS.update", :name_update_tx, _aetx, tx, _tx_hash} -> + Name.update_mutations(tx, {call_txi, local_idx}, block_index, true) - {{:internal_call_tx, "AENS.transfer"}, %{info: aetx}} -> - {:name_transfer_tx, tx} = :aetx.specialize_type(aetx) - NameTransferMutation.new(tx, call_txi, block_index) - - {{:internal_call_tx, "AENS.revoke"}, %{info: aetx}} -> - {:name_revoke_tx, tx} = :aetx.specialize_type(aetx) + {local_idx, "AENS.transfer", :name_transfer_tx, _aetx, tx, _tx_hash} -> + NameTransferMutation.new(tx, {call_txi, local_idx}, block_index) + {local_idx, "AENS.revoke", :name_revoke_tx, _aetx, tx, _tx_hash} -> tx |> :aens_revoke_tx.name_hash() - |> NameRevokeMutation.new(call_txi, block_index) + |> NameRevokeMutation.new({call_txi, local_idx}, block_index) + + {_local_idx, _fname, _tx_type, _aetx, _tx, _tx_hash} -> + [] end) end @@ -194,7 +179,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}, accounts_nonces -> + {"Oracle.query", aetx, tx_hash}, accounts_nonces -> {:oracle_query_tx, tx} = :aetx.specialize_type(aetx) sender_pk = :aeo_query_tx.sender_pubkey(tx) @@ -208,10 +193,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}, accounts_nonces} + {{"Oracle.query", fixed_aetx, tx_hash}, accounts_nonces} - {fname, aetx}, accounts_nonces -> - {{fname, aetx}, accounts_nonces} + {fname, aetx, tx_hash}, accounts_nonces -> + {{fname, aetx, tx_hash}, accounts_nonces} end) fixed_events diff --git a/lib/ae_mdw/db/sync/name.ex b/lib/ae_mdw/db/sync/name.ex index 0ee3f1f07..40ef79e0a 100644 --- a/lib/ae_mdw/db/sync/name.ex +++ b/lib/ae_mdw/db/sync/name.ex @@ -5,7 +5,6 @@ defmodule AeMdw.Db.Sync.Name do alias AeMdw.Db.Model alias AeMdw.Db.Mutation alias AeMdw.Names - alias AeMdw.Db.Format alias AeMdw.Db.IntTransfer alias AeMdw.Db.NameClaimMutation alias AeMdw.Db.NameUpdateMutation @@ -56,10 +55,10 @@ defmodule AeMdw.Db.Sync.Name do | {String.t(), <<>>, <<>>, <<>>, <<>>} | {pubkey(), Blocks.height(), binary()} - @spec name_claim_mutations(Node.tx(), Txs.tx_hash(), Blocks.block_index(), Txs.txi()) :: [ + @spec name_claim_mutations(Node.tx(), Txs.tx_hash(), Blocks.block_index(), Txs.txi_idx()) :: [ Mutation.t() ] - def name_claim_mutations(tx, tx_hash, {height, _mbi} = block_index, txi) do + def name_claim_mutations(tx, tx_hash, {height, _mbi} = block_index, {txi, _idx} = txi_idx) do plain_name = String.downcase(:aens_claim_tx.name(tx)) {:ok, name_hash} = :aens.get_name_hash(plain_name) owner_pk = Validate.id!(:aens_claim_tx.account_id(tx)) @@ -75,7 +74,7 @@ defmodule AeMdw.Db.Sync.Name do owner_pk, name_fee, lima_or_higher?, - txi, + txi_idx, block_index, timeout ) @@ -83,8 +82,10 @@ defmodule AeMdw.Db.Sync.Name do ] end - @spec update_mutations(Node.tx(), Txs.txi(), Blocks.block_index(), boolean()) :: [Mutation.t()] - def update_mutations(tx, txi, {height, _mbi} = block_index, internal? \\ false) do + @spec update_mutations(Node.tx(), Txs.txi_idx(), Blocks.block_index(), boolean()) :: [ + Mutation.t() + ] + def update_mutations(tx, txi_idx, {height, _mbi} = block_index, internal? \\ false) do name_hash = :aens_update_tx.name_hash(tx) pointers = :aens_update_tx.pointers(tx) name_ttl = :aens_update_tx.name_ttl(tx) @@ -107,7 +108,7 @@ defmodule AeMdw.Db.Sync.Name do end [ - NameUpdateMutation.new(name_hash, update_type, pointers, txi, block_index) + NameUpdateMutation.new(name_hash, update_type, pointers, txi_idx, block_index) ] end @@ -116,20 +117,20 @@ defmodule AeMdw.Db.Sync.Name do Names.name_hash(), update_type(), Names.pointers(), - Txs.txi(), + Txs.txi_idx(), Blocks.block_index() ) :: State.t() - def update(state, name_hash, update_type, pointers, txi, {height, _mbi} = bi) do + def update(state, name_hash, update_type, pointers, txi_idx, {height, _mbi} = bi) do plain_name = plain_name!(state, name_hash) Model.name(expire: old_expire, owner: owner_pk, updates: updates) = m_name = cache_through_read!(state, Model.ActiveName, plain_name) - updates = [{bi, txi} | updates] + updates = [{bi, txi_idx} | updates] state2 = Enum.reduce(pointers, state, fn ptr, state -> - m_pointee = Model.pointee(index: pointee_key(ptr, {bi, txi})) + m_pointee = Model.pointee(index: pointee_key(ptr, {bi, txi_idx})) cache_through_write(state, Model.Pointee, m_pointee) end) @@ -170,15 +171,15 @@ defmodule AeMdw.Db.Sync.Name do end end - @spec transfer(State.t(), Names.name_hash(), Db.pubkey(), Txs.txi(), Blocks.block_index()) :: + @spec transfer(State.t(), Names.name_hash(), Db.pubkey(), Txs.txi_idx(), Blocks.block_index()) :: State.t() - def transfer(state, name_hash, new_owner, txi, {height, _mbi} = bi) do + def transfer(state, name_hash, new_owner, txi_idx, {height, _mbi} = bi) do plain_name = plain_name!(state, name_hash) m_name = cache_through_read!(state, Model.ActiveName, plain_name) Model.name(owner: old_owner, expire: expire) = m_name - transfers = [{bi, txi} | Model.name(m_name, :transfers)] + transfers = [{bi, txi_idx} | Model.name(m_name, :transfers)] m_name = Model.name(m_name, transfers: transfers, owner: new_owner) m_owner = Model.owner(index: {new_owner, plain_name}) m_name_owner_deactivation = Model.owner_deactivation(index: {new_owner, expire, plain_name}) @@ -193,14 +194,14 @@ defmodule AeMdw.Db.Sync.Name do |> cache_through_write(Model.ActiveNameOwnerDeactivation, m_name_owner_deactivation) end - @spec revoke(State.t(), Names.name_hash(), Txs.txi(), Blocks.block_index()) :: State.t() - def revoke(state, name_hash, txi, {height, _mbi} = bi) do + @spec revoke(State.t(), Names.name_hash(), Txs.txi_idx(), Blocks.block_index()) :: State.t() + def revoke(state, name_hash, txi_idx, {height, _mbi} = bi) do plain_name = plain_name!(state, name_hash) Model.name(expire: expiration) = m_name = cache_through_read!(state, Model.ActiveName, plain_name) - m_name = Model.name(m_name, revoke: {bi, txi}) + m_name = Model.name(m_name, revoke: {bi, txi_idx}) log_name_change(height, plain_name, "revoke") @@ -223,7 +224,7 @@ defmodule AeMdw.Db.Sync.Name do def expire_auction(state, height, plain_name) do {:ok, Model.auction_bid( - block_index_txi: {{last_bid_height, _mbi} = _block_index, txi}, + block_index_txi_idx: {{last_bid_height, _mbi} = _block_index, {txi, _idx} = txi_idx}, owner: owner, bids: bids )} = cache_through_read(state, Model.AuctionBid, plain_name) @@ -251,7 +252,11 @@ defmodule AeMdw.Db.Sync.Name do m_name_exp = Model.expiration(index: {expire, plain_name}) m_owner = Model.owner(index: {owner, plain_name}) m_name_owner_deactivation = Model.owner_deactivation(index: {owner, expire, plain_name}) - %{tx: %{name_fee: name_fee}} = read_raw_tx!(state, txi) + + name_fee = + state + |> DbUtil.read_node_tx(txi_idx) + |> :aens_claim_tx.name_fee() state |> cache_through_write(Model.ActiveName, m_name) @@ -372,7 +377,4 @@ defmodule AeMdw.Db.Sync.Name do defp pointer_kv(ptr) do {:aens_pointer.key(ptr), Validate.id!(:aens_pointer.id(ptr))} end - - defp read_raw_tx!(state, txi), - do: Format.to_raw_map(state, DbUtil.read_tx!(state, txi)) end diff --git a/lib/ae_mdw/db/sync/oracle.ex b/lib/ae_mdw/db/sync/oracle.ex index 1829ae7b0..acc64357a 100644 --- a/lib/ae_mdw/db/sync/oracle.ex +++ b/lib/ae_mdw/db/sync/oracle.ex @@ -25,17 +25,17 @@ defmodule AeMdw.Db.Sync.Oracle do @typep cache_key :: pubkey() | {pos_integer(), pubkey()} @typep state :: State.t() - @spec register_mutations(Node.tx(), Txs.tx_hash(), Blocks.block_index(), Txs.txi()) :: [ + @spec register_mutations(Node.tx(), Txs.tx_hash(), Blocks.block_index(), Txs.txi_idx()) :: [ Mutation.t() ] - def register_mutations(tx, tx_hash, {height, _mbi} = block_index, txi) do + def register_mutations(tx, tx_hash, {height, _mbi} = block_index, {txi, _idx} = txi_idx) do oracle_pk = :aeo_register_tx.account_pubkey(tx) delta_ttl = :aeo_utils.ttl_delta(height, :aeo_register_tx.oracle_ttl(tx)) expire = height + delta_ttl [ Origin.origin_mutations(:oracle_register_tx, nil, oracle_pk, txi, tx_hash), - OracleRegisterMutation.new(oracle_pk, block_index, expire, txi) + OracleRegisterMutation.new(oracle_pk, block_index, expire, txi_idx) ] end @@ -65,12 +65,13 @@ defmodule AeMdw.Db.Sync.Oracle do OracleResponseMutation.new(block_index, txi, oracle_pk, query_id) end - @spec extend_mutation(Node.tx(), Blocks.block_index(), Txs.txi()) :: OracleExtendMutation.t() - def extend_mutation(tx, block_index, txi) do + @spec extend_mutation(Node.tx(), Blocks.block_index(), Txs.txi_idx()) :: + OracleExtendMutation.t() + def extend_mutation(tx, block_index, txi_idx) do oracle_pk = :aeo_extend_tx.oracle_pubkey(tx) {:delta, delta_ttl} = :aeo_extend_tx.oracle_ttl(tx) - OracleExtendMutation.new(block_index, txi, oracle_pk, delta_ttl) + OracleExtendMutation.new(block_index, txi_idx, oracle_pk, delta_ttl) end @spec cache_through_write(state(), atom(), tuple()) :: state() diff --git a/lib/ae_mdw/db/sync/transaction.ex b/lib/ae_mdw/db/sync/transaction.ex index 6ac7cfdbe..37853b4cb 100644 --- a/lib/ae_mdw/db/sync/transaction.ex +++ b/lib/ae_mdw/db/sync/transaction.ex @@ -327,7 +327,7 @@ defmodule AeMdw.Db.Sync.Transaction do tx_hash: tx_hash, block_index: block_index }) do - Oracle.register_mutations(tx, tx_hash, block_index, txi) + Oracle.register_mutations(tx, tx_hash, block_index, {txi, -1}) end defp tx_mutations(%TxContext{ @@ -337,7 +337,7 @@ defmodule AeMdw.Db.Sync.Transaction do tx_hash: tx_hash, block_index: block_index }) do - SyncName.name_claim_mutations(tx, tx_hash, block_index, txi) + SyncName.name_claim_mutations(tx, tx_hash, block_index, {txi, -1}) end defp tx_mutations(%TxContext{ @@ -347,7 +347,7 @@ defmodule AeMdw.Db.Sync.Transaction do block_index: block_index }) do [ - Oracle.extend_mutation(tx, block_index, txi) + Oracle.extend_mutation(tx, block_index, {txi, -1}) ] end @@ -378,7 +378,7 @@ defmodule AeMdw.Db.Sync.Transaction do txi: txi, block_index: block_index }) do - SyncName.update_mutations(tx, txi, block_index) + SyncName.update_mutations(tx, {txi, -1}, block_index) end defp tx_mutations(%TxContext{ @@ -388,7 +388,7 @@ defmodule AeMdw.Db.Sync.Transaction do block_index: block_index }) do [ - NameTransferMutation.new(tx, txi, block_index) + NameTransferMutation.new(tx, {txi, -1}, block_index) ] end @@ -401,7 +401,7 @@ defmodule AeMdw.Db.Sync.Transaction do name_hash = :aens_revoke_tx.name_hash(tx) [ - NameRevokeMutation.new(name_hash, txi, block_index) + NameRevokeMutation.new(name_hash, {txi, -1}, block_index) ] end diff --git a/lib/ae_mdw/db/util.ex b/lib/ae_mdw/db/util.ex index f82aae9e1..f90a9bd37 100644 --- a/lib/ae_mdw/db/util.ex +++ b/lib/ae_mdw/db/util.ex @@ -5,8 +5,10 @@ defmodule AeMdw.Db.Util do alias AeMdw.Collection alias AeMdw.Db.Model alias AeMdw.Db.State + alias AeMdw.Db.Sync.InnerTx alias AeMdw.Error alias AeMdw.Error.Input, as: ErrInput + alias AeMdw.Node alias AeMdw.Node.Db alias AeMdw.Txs alias AeMdw.Util @@ -154,6 +156,33 @@ defmodule AeMdw.Db.Util do end end + @spec read_node_tx(state(), Txs.txi_idx()) :: Node.tx() + def read_node_tx(state, {txi, -1}) do + Model.tx(id: tx_hash) = State.fetch!(state, Model.Tx, txi) + + case Db.get_tx_data(tx_hash) do + {_block_hash, tx_type, _signed_tx, tx_rec} when tx_type in [:ga_meta_tx, :paying_for_tx] -> + {_type, tx} = + tx_type + |> InnerTx.signed_tx(tx_rec) + |> :aetx_sign.tx() + |> :aetx.specialize_type() + + tx + + {_block_hash, _tx_type, _signed_tx, tx_rec} -> + tx_rec + end + end + + def read_node_tx(state, {txi, local_idx}) do + Model.int_contract_call(tx: aetx) = + State.fetch!(state, Model.IntContractCall, {txi, local_idx}) + + {_type, tx} = :aetx.specialize_type(aetx) + tx + end + defp extract_height_hash(state, type, hash) do with {:ok, encoded_hash} <- Validate.id(hash), {:ok, block} <- :aec_chain.get_block(encoded_hash), diff --git a/lib/ae_mdw/names.ex b/lib/ae_mdw/names.ex index 1c2bf1767..d9eb0332b 100644 --- a/lib/ae_mdw/names.ex +++ b/lib/ae_mdw/names.ex @@ -10,11 +10,11 @@ defmodule AeMdw.Names do alias AeMdw.AuctionBids alias AeMdw.Blocks alias AeMdw.Collection - alias AeMdw.Contracts alias AeMdw.Db.Format alias AeMdw.Db.Model alias AeMdw.Db.Name alias AeMdw.Db.State + alias AeMdw.Db.Util, as: DbUtil alias AeMdw.Error alias AeMdw.Error.Input, as: ErrInput alias AeMdw.Node.Db @@ -121,16 +121,16 @@ defmodule AeMdw.Names do {:ok, {nested_cursor() | nil, [claim()], nested_cursor() | nil}} | {:error, Error.t()} def fetch_name_claims(state, plain_name_or_hash, pagination, scope, cursor) do with {:ok, name_or_auction} <- locate_name_or_auction(state, plain_name_or_hash) do - {claims, plain_name} = + claims = case name_or_auction do - Model.name(index: plain_name, claims: claims) -> {claims, plain_name} - Model.auction_bid(index: plain_name, bids: bids) -> {bids, plain_name} + Model.name(claims: claims) -> claims + Model.auction_bid(bids: bids) -> bids end {prev_cursor, claims, next_cursor} = paginate_nested_resource(claims, scope, cursor, pagination) - {:ok, {prev_cursor, Enum.map(claims, &render_claim(state, plain_name, &1)), next_cursor}} + {:ok, {prev_cursor, Enum.map(claims, &render_claim(state, &1)), next_cursor}} end end @@ -138,16 +138,16 @@ defmodule AeMdw.Names do {:ok, {nested_cursor() | nil, [update()], nested_cursor() | nil}} | {:error, Error.t()} def fetch_name_updates(state, plain_name_or_hash, pagination, scope, cursor) do with {:ok, name_or_auction} <- locate_name_or_auction(state, plain_name_or_hash) do - {updates, plain_name} = + updates = case name_or_auction do - Model.name(index: plain_name, updates: updates) -> {updates, plain_name} - Model.auction_bid(index: plain_name) -> {[], plain_name} + Model.name(updates: updates) -> updates + Model.auction_bid() -> [] end {prev_cursor, updates, next_cursor} = paginate_nested_resource(updates, scope, cursor, pagination) - {:ok, {prev_cursor, Enum.map(updates, &render_update(state, plain_name, &1)), next_cursor}} + {:ok, {prev_cursor, Enum.map(updates, &render_update(state, &1)), next_cursor}} end end @@ -155,17 +155,16 @@ defmodule AeMdw.Names do {:ok, {nested_cursor() | nil, [update()], nested_cursor() | nil}} | {:error, Error.t()} def fetch_name_transfers(state, plain_name_or_hash, pagination, scope, cursor) do with {:ok, name_or_auction} <- locate_name_or_auction(state, plain_name_or_hash) do - {transfers, plain_name} = + transfers = case name_or_auction do - Model.name(index: plain_name, transfers: transfers) -> {transfers, plain_name} - Model.auction_bid(index: plain_name) -> {[], plain_name} + Model.name(transfers: transfers) -> transfers + Model.auction_bid() -> [] end {prev_cursor, transfers, next_cursor} = paginate_nested_resource(transfers, scope, cursor, pagination) - {:ok, - {prev_cursor, Enum.map(transfers, &render_transfer(state, plain_name, &1)), next_cursor}} + {:ok, {prev_cursor, Enum.map(transfers, &render_transfer(state, &1)), next_cursor}} end end @@ -486,10 +485,10 @@ defmodule AeMdw.Names do %{ active_from: active, expire_height: expire, - claims: Enum.map(claims, &expand_txi(state, &1, opts)), - updates: Enum.map(updates, &expand_txi(state, &1, opts)), - transfers: Enum.map(transfers, &expand_txi(state, &1, opts)), - revoke: (revoke && expand_txi(state, revoke, opts)) || nil, + claims: Enum.map(claims, &expand_txi_idx(state, &1, opts)), + updates: Enum.map(updates, &expand_txi_idx(state, &1, opts)), + transfers: Enum.map(transfers, &expand_txi_idx(state, &1, opts)), + revoke: (revoke && expand_txi_idx(state, revoke, opts)) || nil, auction_timeout: auction_timeout, pointers: Name.pointers(state, name), ownership: render_ownership(state, name) @@ -538,9 +537,7 @@ defmodule AeMdw.Names do end end - defp expand_txi(state, bi_txi, opts) do - txi = Format.bi_txi_txi(bi_txi) - + defp expand_txi_idx(state, {_bi, {txi, _idx}}, opts) do cond do Keyword.get(opts, :expand?, false) -> Txs.fetch!(state, txi) @@ -553,41 +550,41 @@ defmodule AeMdw.Names do end end - defp paginate_nested_resource(bi_txis, scope, cursor, pagination) do + defp paginate_nested_resource(bi_txi_idxs, scope, cursor, pagination) do cursor = deserialize_nested_cursor(cursor) - bi_txis = + bi_txi_idxs = case scope do {:gen, first_gen..last_gen} -> - bi_txis - |> Enum.drop_while(fn {{height, _mbi}, _txi} -> height >= last_gen end) - |> Enum.take_while(fn {{height, _mbi}, _txi} -> height <= first_gen end) + bi_txi_idxs + |> Enum.drop_while(fn {{height, _mbi}, _txi_idx} -> height >= last_gen end) + |> Enum.take_while(fn {{height, _mbi}, _txi_idx} -> height <= first_gen end) nil -> - bi_txis + bi_txi_idxs end streamer = fn :forward -> - bi_txis = Enum.reverse(bi_txis) + bi_txi_idxs = Enum.reverse(bi_txi_idxs) if cursor do - Enum.drop_while(bi_txis, &(&1 < cursor)) + Enum.drop_while(bi_txi_idxs, &(&1 < cursor)) else - bi_txis + bi_txi_idxs end :backward -> if cursor do - Enum.drop_while(bi_txis, &(&1 > cursor)) + Enum.drop_while(bi_txi_idxs, &(&1 > cursor)) else - bi_txis + bi_txi_idxs end end - {prev_cursor, bi_txis, next_cursor} = Collection.paginate(streamer, pagination) + {prev_cursor, bi_txi_idxs, next_cursor} = Collection.paginate(streamer, pagination) - {serialize_nested_cursor(prev_cursor), bi_txis, serialize_nested_cursor(next_cursor)} + {serialize_nested_cursor(prev_cursor), bi_txi_idxs, serialize_nested_cursor(next_cursor)} end defp render_previous(state, name, opts) do @@ -599,48 +596,36 @@ defmodule AeMdw.Names do |> Enum.map(&render_name_info(state, &1, opts)) end - defp render_claim(state, plain_name, {{height, _mbi} = block_index, txi}) do + defp render_claim(state, {{height, _mbi} = block_index, txi_idx}) do block_hash = Blocks.block_index_to_hash(state, block_index) - - claim_aetx = - Contracts.get_aetx(state, txi, :name_claim_tx, "AENS.claim", fn tx -> - :aens_claim_tx.name(tx) == plain_name - end) + claim_aetx = DbUtil.read_node_tx(state, txi_idx) %{ height: height, block_hash: Enc.encode(:micro_block_hash, block_hash), - tx: :aetx.serialize_for_client(claim_aetx) + tx: :aens_claim_tx.for_client(claim_aetx) } end - defp render_update(state, plain_name, {{height, _mbi} = block_index, txi}) do + defp render_update(state, {{height, _mbi} = block_index, txi_idx}) do block_hash = Blocks.block_index_to_hash(state, block_index) - - update_aetx = - Contracts.get_aetx(state, txi, :name_update_tx, "AENS.update", fn tx -> - :aens_update_tx.name(tx) == plain_name - end) + update_aetx = DbUtil.read_node_tx(state, txi_idx) %{ height: height, block_hash: Enc.encode(:micro_block_hash, block_hash), - tx: :aetx.serialize_for_client(update_aetx) + tx: :aens_update_tx.for_client(update_aetx) } end - defp render_transfer(state, plain_name, {{height, _mbi} = block_index, txi}) do + defp render_transfer(state, {{height, _mbi} = block_index, txi_idx}) do block_hash = Blocks.block_index_to_hash(state, block_index) - - transfer_aetx = - Contracts.get_aetx(state, txi, :name_transfer_tx, "AENS.transfer", fn tx -> - :aens_transfer_tx.name(tx) == plain_name - end) + transfer_aetx = DbUtil.read_node_tx(state, txi_idx) %{ height: height, block_hash: Enc.encode(:micro_block_hash, block_hash), - tx: :aetx.serialize_for_client(transfer_aetx) + tx: :aens_transfer_tx.for_client(transfer_aetx) } end @@ -691,15 +676,16 @@ defmodule AeMdw.Names do defp serialize_nested_cursor(nil), do: nil - defp serialize_nested_cursor({{{height, mbi}, txi}, is_reverse?}), - do: {"#{height}-#{mbi}-#{txi}", is_reverse?} + defp serialize_nested_cursor({{{height, mbi}, {txi, idx}}, is_reverse?}), + do: {"#{height}-#{mbi}-#{txi}-#{idx + 1}", is_reverse?} defp deserialize_nested_cursor(nil), do: nil defp deserialize_nested_cursor(cursor_bin) do - case Regex.run(~r/\A(\d+)-(\d+)-(\d+)\z/, cursor_bin, capture: :all_but_first) do - [height, mbi, txi] -> - {{String.to_integer(height), String.to_integer(mbi)}, String.to_integer(txi)} + case Regex.run(~r/\A(\d+)-(\d+)-(\d+)-(\d+)\z/, cursor_bin, capture: :all_but_first) do + [height, mbi, txi, idx] -> + {{String.to_integer(height), String.to_integer(mbi)}, + {String.to_integer(txi), String.to_integer(idx) - 1}} _error_or_invalid -> nil diff --git a/lib/ae_mdw/oracles.ex b/lib/ae_mdw/oracles.ex index 6faa87f35..6a14eecc4 100644 --- a/lib/ae_mdw/oracles.ex +++ b/lib/ae_mdw/oracles.ex @@ -7,7 +7,6 @@ defmodule AeMdw.Oracles do alias AeMdw.Blocks alias AeMdw.Collection - alias AeMdw.Db.Format alias AeMdw.Db.Model alias AeMdw.Db.Oracle alias AeMdw.Db.State @@ -159,7 +158,8 @@ defmodule AeMdw.Oracles do Model.oracle( index: pk, expire: expire_height, - register: {{register_height, _mbi}, register_txi} = register_bi_txi, + register: + {{register_height, _mbi}, {register_txi, _register_idx}} = register_bi_txi_idx, extends: extends, previous: _previous ), @@ -181,9 +181,9 @@ defmodule AeMdw.Oracles do active: is_active?, active_from: register_height, expire_height: expire_height, - register: expand_txi(state, register_bi_txi, opts), + register: expand_bi_txi_idx(state, register_bi_txi_idx, opts), register_tx_hash: :aeser_api_encoder.encode(:tx_hash, Txs.txi_to_hash(state, register_txi)), - extends: Enum.map(extends, &expand_txi(state, &1, opts)), + extends: Enum.map(extends, &expand_bi_txi_idx(state, &1, opts)), query_fee: query_fee, format: %{ query: query_format, @@ -211,9 +211,7 @@ defmodule AeMdw.Oracles do end end - defp expand_txi(state, bi_txi, opts) do - txi = Format.bi_txi_txi(bi_txi) - + defp expand_bi_txi_idx(state, {_bi, {txi, _idx}}, opts) do cond do Keyword.get(opts, :expand?, false) -> Txs.fetch!(state, txi) diff --git a/lib/ae_mdw/txs.ex b/lib/ae_mdw/txs.ex index 56e3b46c1..d46736a40 100644 --- a/lib/ae_mdw/txs.ex +++ b/lib/ae_mdw/txs.ex @@ -5,6 +5,7 @@ defmodule AeMdw.Txs do alias AeMdw.Blocks alias AeMdw.Collection + alias AeMdw.Contract alias AeMdw.Db.Format alias AeMdw.Db.Model alias AeMdw.Db.Model.Field @@ -25,6 +26,7 @@ defmodule AeMdw.Txs do @type tx :: map() @type txi :: non_neg_integer() + @type txi_idx() :: {txi(), Contract.local_idx() | -1} @type tx_hash() :: <<_::256>> @type cursor :: binary() @type query :: diff --git a/test/ae_mdw/db/name_claim_mutation_test.exs b/test/ae_mdw/db/name_claim_mutation_test.exs index 0496839e4..aafa335d2 100644 --- a/test/ae_mdw/db/name_claim_mutation_test.exs +++ b/test/ae_mdw/db/name_claim_mutation_test.exs @@ -32,6 +32,7 @@ defmodule AeMdw.Db.NameClaimMutationTest do owner_pk = <<8435::256>> block_index = {claim_height, 0} txi = 223 + txi_idx = {txi, -1} inactive_name = Model.name( @@ -39,7 +40,7 @@ defmodule AeMdw.Db.NameClaimMutationTest do active: old_claim_height, expire: 199, claims: old_claims, - updates: [{{150, 0}, 173}], + updates: [{{150, 0}, {173, -1}}], owner: owner_pk, previous: nil ) @@ -61,7 +62,7 @@ defmodule AeMdw.Db.NameClaimMutationTest do with_mocks [ {AeMdw.Node.Db, [], [proto_vsn: fn _height -> 3 end]} ] do - mutations = Sync.Name.name_claim_mutations(tx, tx_hash, block_index, txi) + mutations = Sync.Name.name_claim_mutations(tx, tx_hash, block_index, txi_idx) State.commit(state, mutations) end @@ -70,7 +71,7 @@ defmodule AeMdw.Db.NameClaimMutationTest do Model.name( index: ^plain_name, active: ^claim_height, - claims: [{^block_index, ^txi} | _old_claims], + claims: [{^block_index, ^txi_idx} | _old_claims], expire: expire, owner: ^new_owner_pk, updates: [] @@ -96,6 +97,7 @@ defmodule AeMdw.Db.NameClaimMutationTest do block_index = {claim_height, 0} timeout = 54 txi = 1234 + txi_idx = {txi, -1} state = State.new() State.commit(state, [ @@ -105,7 +107,7 @@ defmodule AeMdw.Db.NameClaimMutationTest do owner_pk, name_fee, false, - txi, + txi_idx, block_index, timeout ) @@ -125,7 +127,7 @@ defmodule AeMdw.Db.NameClaimMutationTest do Model.auction_bid( index: ^plain_name, owner: ^owner_pk, - bids: [{^block_index, ^txi}] + bids: [{^block_index, ^txi_idx}] )} = State.get(state, Model.AuctionBid, plain_name) assert State.exists?(state, Model.AuctionOwner, {owner_pk, plain_name}) diff --git a/test/ae_mdw/db/name_revoke_mutation_test.exs b/test/ae_mdw/db/name_revoke_mutation_test.exs index 46ea69127..a37df6cf0 100644 --- a/test/ae_mdw/db/name_revoke_mutation_test.exs +++ b/test/ae_mdw/db/name_revoke_mutation_test.exs @@ -19,7 +19,7 @@ defmodule AeMdw.Db.NameRevokeMutationTest do revoke_height = 3 revoke_block_index = {revoke_height, 0} - revoke_txi = 124 + revoke_txi_idx = {124, -1} active_from = 11 expire = 100 @@ -30,7 +30,7 @@ defmodule AeMdw.Db.NameRevokeMutationTest do index: plain_name, active: active_from, expire: expire, - claims: [{{active_from, 0}, 123}], + claims: [{{active_from, 0}, {123, -1}}], updates: [], transfers: [], revoke: nil, @@ -55,7 +55,7 @@ defmodule AeMdw.Db.NameRevokeMutationTest do state2 = State.commit_mem(State.new(), [ - NameRevokeMutation.new(name_hash, revoke_txi, revoke_block_index) + NameRevokeMutation.new(name_hash, revoke_txi_idx, revoke_block_index) ]) refute State.exists?(state2, Model.ActiveName, plain_name) @@ -74,7 +74,7 @@ defmodule AeMdw.Db.NameRevokeMutationTest do index: ^plain_name, expire: ^expire, owner: ^owner_pk, - revoke: {^revoke_block_index, ^revoke_txi} + revoke: {^revoke_block_index, ^revoke_txi_idx} )} = State.get(state2, Model.InactiveName, plain_name) assert State.exists?(state2, Model.InactiveNameExpiration, {revoke_height, plain_name}) diff --git a/test/ae_mdw/db/name_test.exs b/test/ae_mdw/db/name_test.exs index 1f401d309..7eeac1c99 100644 --- a/test/ae_mdw/db/name_test.exs +++ b/test/ae_mdw/db/name_test.exs @@ -70,7 +70,7 @@ defmodule AeMdw.Db.NameTest do State.new(store), Model.name( index: name, - updates: [{{1, 1}, 2}] + updates: [{{1, 1}, {2, -1}}] ) ) end @@ -128,7 +128,7 @@ defmodule AeMdw.Db.NameTest do State.new(store), Model.name( index: name, - updates: [{{1, 1}, 2}] + updates: [{{1, 1}, {2, -1}}] ) ) end diff --git a/test/ae_mdw/db/name_update_mutation_test.exs b/test/ae_mdw/db/name_update_mutation_test.exs index 7608b09e3..13f34e5c3 100644 --- a/test/ae_mdw/db/name_update_mutation_test.exs +++ b/test/ae_mdw/db/name_update_mutation_test.exs @@ -27,7 +27,7 @@ defmodule AeMdw.Db.NameUpdateMutationTest do index: plain_name, active: active_from, expire: expire, - claims: [{{active_from, 0}, 123}], + claims: [{{active_from, 0}, {123, -1}}], updates: [], transfers: [], revoke: nil, @@ -59,7 +59,7 @@ defmodule AeMdw.Db.NameUpdateMutationTest do state2 = State.commit_mem(State.new(), [ - NameUpdateMutation.new(name_hash, :expire, pointers, txi, block_index) + NameUpdateMutation.new(name_hash, :expire, pointers, {txi, -1}, block_index) ]) assert {:ok, @@ -67,7 +67,7 @@ defmodule AeMdw.Db.NameUpdateMutationTest do index: ^plain_name, expire: ^update_height, owner: ^owner_pk, - updates: [{^block_index, ^txi}], + updates: [{^block_index, {^txi, -1}}], revoke: nil )} = State.get(state2, Model.InactiveName, plain_name) @@ -96,7 +96,7 @@ defmodule AeMdw.Db.NameUpdateMutationTest do index: plain_name, active: active_from, expire: expire, - claims: [{{active_from, 0}, 1234}], + claims: [{{active_from, 0}, {1234, -1}}], updates: [], transfers: [], revoke: nil, @@ -133,7 +133,7 @@ defmodule AeMdw.Db.NameUpdateMutationTest do name_hash, {:update_expiration, new_expire}, pointers, - txi, + {txi, -1}, block_index ) ]) @@ -143,7 +143,7 @@ defmodule AeMdw.Db.NameUpdateMutationTest do index: ^plain_name, expire: ^new_expire, owner: ^owner_pk, - updates: [{^block_index, ^txi}], + updates: [{^block_index, {^txi, -1}}], revoke: nil )} = State.get(state2, Model.ActiveName, plain_name) diff --git a/test/ae_mdw/db/names_expire_mutation_test.exs b/test/ae_mdw/db/names_expire_mutation_test.exs index e8ed15b87..d0a8a8214 100644 --- a/test/ae_mdw/db/names_expire_mutation_test.exs +++ b/test/ae_mdw/db/names_expire_mutation_test.exs @@ -17,7 +17,7 @@ defmodule AeMdw.Db.NamesExpirationMutationTest do index: plain_name, active: sync_height - 10_000, expire: sync_height - 1_000, - claims: {{sync_height - 10_000, 0}, 1_000_000}, + claims: [{{sync_height - 10_000, 0}, {1_000_000, -1}}], owner: <<1::256>>, previous: nil ) @@ -29,7 +29,7 @@ defmodule AeMdw.Db.NamesExpirationMutationTest do index: plain_name, active: active_height, expire: sync_height, - claims: {{active_height, 0}, 1_500_000}, + claims: [{{active_height, 0}, {1_500_000, -1}}], owner: <<2::256>>, previous: m_previous ) @@ -64,7 +64,7 @@ defmodule AeMdw.Db.NamesExpirationMutationTest do index: plain_name, active: active_height, expire: sync_height + 1, - claims: {{active_height, 0}, 1_500_000}, + claims: [{{active_height, 0}, {1_500_000, -1}}], owner: <<2::256>>, previous: nil ) @@ -96,7 +96,7 @@ defmodule AeMdw.Db.NamesExpirationMutationTest do index: plain_name, active: active_height, expire: sync_height - 1, - claims: {{active_height, 0}, 1_500_000}, + claims: [{{active_height, 0}, {1_500_000, -1}}], owner: <<2::256>>, previous: nil ) diff --git a/test/ae_mdw/db/oracle_expire_mutation_test.exs b/test/ae_mdw/db/oracle_expire_mutation_test.exs index c85d25666..c6dd18da5 100644 --- a/test/ae_mdw/db/oracle_expire_mutation_test.exs +++ b/test/ae_mdw/db/oracle_expire_mutation_test.exs @@ -17,7 +17,7 @@ defmodule AeMdw.Db.OraclesExpirationMutationTest do Model.oracle( index: pubkey, expire: sync_height, - register: {{sync_height - 5_000, 0}, 1_234}, + register: {{sync_height - 5_000, 0}, {1_234, -1}}, extends: nil, previous: nil ) @@ -48,7 +48,7 @@ defmodule AeMdw.Db.OraclesExpirationMutationTest do Model.oracle( index: pubkey, expire: expire, - register: {{expire - 5_000, 0}, 1_234}, + register: {{expire - 5_000, 0}, {1_234, -1}}, extends: nil, previous: nil ) @@ -79,7 +79,7 @@ defmodule AeMdw.Db.OraclesExpirationMutationTest do Model.oracle( index: pubkey, expire: expire, - register: {{expire - 5_000, 0}, 1_234}, + register: {{expire - 5_000, 0}, {1_234, -1}}, extends: nil, previous: nil ) diff --git a/test/ae_mdw/db/sync/contract_test.exs b/test/ae_mdw/db/sync/contract_test.exs index 8358cee9a..31be3c933 100644 --- a/test/ae_mdw/db/sync/contract_test.exs +++ b/test/ae_mdw/db/sync/contract_test.exs @@ -37,13 +37,13 @@ defmodule AeMdw.Db.Sync.ContractTest do tx_2 = {:tx2, account_id} events = [ - {{:internal_call_tx, "some_funname_1"}, %{info: tx_1}}, - {{:internal_call_tx, "some_funname_2"}, %{info: tx_2}} + {{:internal_call_tx, "Call.amount"}, %{info: tx_1, tx_hash: "tx1-hash"}}, + {{:internal_call_tx, "Call.amount"}, %{info: tx_2, tx_hash: "tx2-hash"}} ] int_calls = [ - {"some_funname_1", :spend_tx, tx_1, tx_1}, - {"some_funname_2", :spend_tx, tx_2, tx_1} + {0, "Call.amount", :spend_tx, tx_1, tx_1, "tx1-hash"}, + {1, "Call.amount", :spend_tx, tx_2, tx_1, "tx2-hash"} ] mutation = IntCallsMutation.new(contract_pk, call_txi, int_calls) @@ -70,15 +70,15 @@ defmodule AeMdw.Db.Sync.ContractTest do tx_2 = {:tx2, contract_pk} events = [ - {{:internal_call_tx, "Call.amount"}, %{info: tx_1}}, + {{:internal_call_tx, "Call.amount"}, %{info: tx_1, tx_hash: "tx1-hash"}}, {{:internal_call_tx, "Chain.create"}, %{info: :error}}, - {{:internal_call_tx, "Call.amount"}, %{info: tx_2}}, + {{:internal_call_tx, "Call.amount"}, %{info: tx_2, tx_hash: "tx2-hash"}}, {{:internal_call_tx, "Chain.clone"}, %{info: :error}} ] int_calls = [ - {"Call.amount", :spend_tx, tx_1, tx_1}, - {"Call.amount", :spend_tx, tx_2, tx_1} + {0, "Call.amount", :spend_tx, tx_1, tx_1, "tx1-hash"}, + {1, "Call.amount", :spend_tx, tx_2, tx_1, "tx2-hash"} ] mutation = IntCallsMutation.new(contract_pk, call_txi, int_calls) @@ -165,7 +165,7 @@ defmodule AeMdw.Db.Sync.ContractTest do assert Enum.any?(event_mutations, fn %NameTransferMutation{ - txi: ^call_txi, + txi_idx: {^call_txi, 0}, block_index: ^block_index } -> true @@ -187,7 +187,7 @@ defmodule AeMdw.Db.Sync.ContractTest do assert Enum.any?(event_mutations, fn %NameUpdateMutation{ - txi: ^call_txi, + txi_idx: {^call_txi, 0}, block_index: ^block_index } -> true @@ -258,7 +258,7 @@ defmodule AeMdw.Db.Sync.ContractTest do %IntCallsMutation{ call_txi: ^call_txi, contract_pk: ^contract_pk, - int_calls: [{"Oracle.register", ^tx_type, ^aetx, ^tx}] + int_calls: [{0, "Oracle.register", ^tx_type, ^aetx, ^tx, ^tx_hash}] } ] = mutations end @@ -286,7 +286,7 @@ defmodule AeMdw.Db.Sync.ContractTest do contract_pk = <<3::256>> mutations = - [{{:internal_call_tx, "Oracle.extend"}, %{info: aetx}}] + [{{:internal_call_tx, "Oracle.extend"}, %{info: aetx, tx_hash: "tx-hash"}}] |> SyncContract.events_mutations( block_index, block_hash, @@ -303,13 +303,13 @@ defmodule AeMdw.Db.Sync.ContractTest do %OracleExtendMutation{ oracle_pk: ^pubkey, block_index: ^block_index, - txi: ^call_txi, + txi_idx: {^call_txi, 0}, delta_ttl: ^delta_ttl }, %IntCallsMutation{ call_txi: ^call_txi, contract_pk: ^contract_pk, - int_calls: [{"Oracle.extend", :oracle_extend_tx, ^aetx, ^tx}] + int_calls: [{0, "Oracle.extend", :oracle_extend_tx, ^aetx, ^tx, "tx-hash"}] } ] = mutations ) diff --git a/test/ae_mdw/db/sync/name_test.exs b/test/ae_mdw/db/sync/name_test.exs index 9f21181d3..e47e51814 100644 --- a/test/ae_mdw/db/sync/name_test.exs +++ b/test/ae_mdw/db/sync/name_test.exs @@ -28,6 +28,7 @@ defmodule AeMdw.Db.Sync.NameTest do height = AeMdw.Node.lima_height() + Enum.random(100..999) block_index = {height, 1} txi = height * 1_000 + txi_idx = {txi, -1} tx_hash = <> {:ok, aetx} = @@ -54,14 +55,14 @@ defmodule AeMdw.Db.Sync.NameTest do owner_pk, name_fee, true, - txi, + txi_idx, block_index, timeout ) | Origin.origin_mutations(:name_claim_tx, nil, name_hash, txi, tx_hash) ] - assert ^mutations = Name.name_claim_mutations(tx_rec, tx_hash, block_index, txi) + assert ^mutations = Name.name_claim_mutations(tx_rec, tx_hash, block_index, txi_idx) end end @@ -99,14 +100,14 @@ defmodule AeMdw.Db.Sync.NameTest do owner_pk, name_fee, true, - txi, + {txi, -1}, block_index, 0 ) | Origin.origin_mutations(:name_claim_tx, nil, name_hash, txi, tx_hash) ] - assert ^mutations = Name.name_claim_mutations(tx_rec, tx_hash, block_index, txi) + assert ^mutations = Name.name_claim_mutations(tx_rec, tx_hash, block_index, {txi, -1}) end end end diff --git a/test/ae_mdw/db/sync/oracle_test.exs b/test/ae_mdw/db/sync/oracle_test.exs index 827fdf13b..9dc5e869c 100644 --- a/test/ae_mdw/db/sync/oracle_test.exs +++ b/test/ae_mdw/db/sync/oracle_test.exs @@ -18,6 +18,7 @@ defmodule AeMdw.Db.Sync.OracleTest do expire = height + ttl txi = height * 1000 tx_hash = <<123_456::256>> + txi_idx = {txi, -1} {:ok, aetx} = :aeo_register_tx.new(%{ @@ -35,10 +36,10 @@ defmodule AeMdw.Db.Sync.OracleTest do mutations = [ Origin.origin_mutations(:oracle_register_tx, nil, pubkey, txi, tx_hash), - OracleRegisterMutation.new(pubkey, block_index, expire, txi) + OracleRegisterMutation.new(pubkey, block_index, expire, txi_idx) ] - assert ^mutations = Oracle.register_mutations(tx_rec, tx_hash, block_index, txi) + assert ^mutations = Oracle.register_mutations(tx_rec, tx_hash, block_index, txi_idx) end end diff --git a/test/ae_mdw/db/sync/transaction_test.exs b/test/ae_mdw/db/sync/transaction_test.exs index 0669bcc78..fb81a576b 100644 --- a/test/ae_mdw/db/sync/transaction_test.exs +++ b/test/ae_mdw/db/sync/transaction_test.exs @@ -147,7 +147,7 @@ defmodule AeMdw.Db.Sync.TransactionTest do } -> true - %{} -> + _mutation -> false end) end diff --git a/test/ae_mdw_web/controllers/contract_controller_test.exs b/test/ae_mdw_web/controllers/contract_controller_test.exs index f05cf2fca..a5490beee 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) - {@call_function, tx_type, tx, raw_tx} + {i, @call_function, tx_type, tx, raw_tx, "tx-hash"} 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) - {@call_function, tx_type, tx, raw_tx} + {i, @call_function, tx_type, tx, raw_tx, "tx-hash"} end) store = @@ -1168,7 +1168,7 @@ defmodule AeMdwWeb.Controllers.ContractControllerTest do {tx_type, raw_tx} = :aetx.specialize_type(tx) - {"Call.amount", tx_type, tx, raw_tx} + {i, "Call.amount", tx_type, tx, raw_tx, "tx-hash"} end) extra_ct_pk = :crypto.strong_rand_bytes(32) diff --git a/test/ae_mdw_web/controllers/name_controller_test.exs b/test/ae_mdw_web/controllers/name_controller_test.exs index a346aab71..6388571cc 100644 --- a/test/ae_mdw_web/controllers/name_controller_test.exs +++ b/test/ae_mdw_web/controllers/name_controller_test.exs @@ -69,9 +69,9 @@ defmodule AeMdwWeb.NameControllerTest do index: alice_name, owner: alice_pk, active: active_from, - claims: [{{10, 0}, 1}], - transfers: [{{10, 0}, 12}], - updates: [{{10, 0}, 2}], + claims: [{{10, 0}, {1, -1}}], + transfers: [{{10, 0}, {12, -1}}], + updates: [{{10, 0}, {2, -1}}], expire: expire1 ) ) @@ -81,9 +81,9 @@ defmodule AeMdwWeb.NameControllerTest do index: bob_name, owner: bob_pk, active: active_from, - claims: [{{10, 1}, 3}], - transfers: [{{10, 0}, 14}], - updates: [{{10, 2}, 4}], + claims: [{{10, 1}, {3, -1}}], + transfers: [{{10, 0}, {14, -1}}], + updates: [{{10, 2}, {4, -1}}], expire: expire2 ) ) @@ -195,7 +195,7 @@ defmodule AeMdwWeb.NameControllerTest do index: plain_name, active: expire - 10, expire: expire, - claims: [{{expire - 10, 0}, 0}], + claims: [{{expire - 10, 0}, {0, -1}}], updates: [], transfers: [], revoke: nil, @@ -260,10 +260,10 @@ defmodule AeMdwWeb.NameControllerTest do Model.name( active: true, expire: 1, - claims: [{{0, 0}, 0}], + claims: [{{0, 0}, {0, -1}}], updates: [], transfers: [], - revoke: {{0, 0}, 0}, + revoke: {{0, 0}, {0, -1}}, auction_timeout: 1 )} @@ -327,10 +327,10 @@ defmodule AeMdwWeb.NameControllerTest do Model.name( active: true, expire: 1, - claims: [{{0, 0}, 0}], - updates: [{{1, 2}, 3}], + claims: [{{0, 0}, {0, -1}}], + updates: [{{1, 2}, {3, -1}}], transfers: [], - revoke: {{0, 0}, 0}, + revoke: {{0, 0}, {0, -1}}, auction_timeout: 1 )} @@ -399,8 +399,8 @@ defmodule AeMdwWeb.NameControllerTest do index: alice_name, owner: alice_pk, active: active_from, - claims: [{{10, 0}, 1}], - revoke: {{10, 0}, 2}, + claims: [{{10, 0}, {1, -1}}], + revoke: {{10, 0}, {2, -1}}, transfers: [], updates: [], expire: expire1 @@ -412,10 +412,10 @@ defmodule AeMdwWeb.NameControllerTest do index: bob_name, owner: bob_pk, active: active_from, - claims: [{{10, 1}, 3}], - revoke: {{10, 2}, 5}, + claims: [{{10, 1}, {3, -1}}], + revoke: {{10, 2}, {5, -1}}, transfers: [], - updates: [{{10, 2}, 4}], + updates: [{{10, 2}, {4, -1}}], expire: expire2 ) ) @@ -517,10 +517,10 @@ defmodule AeMdwWeb.NameControllerTest do Model.name( active: true, expire: 1, - claims: [{{0, 0}, 0}], + claims: [{{0, 0}, {0, -1}}], updates: [], transfers: [], - revoke: {{0, 0}, 0}, + revoke: {{0, 0}, {0, -1}}, auction_timeout: 1 )} @@ -569,10 +569,10 @@ defmodule AeMdwWeb.NameControllerTest do Model.name( active: true, expire: 1, - claims: [{{0, 0}, 0}], + claims: [{{0, 0}, {0, -1}}], updates: [], transfers: [], - revoke: {{0, 0}, 0}, + revoke: {{0, 0}, {0, -1}}, auction_timeout: 1 )} @@ -624,10 +624,10 @@ defmodule AeMdwWeb.NameControllerTest do Model.name( active: true, expire: 1, - claims: [{{0, 0}, 0}], + claims: [{{0, 0}, {0, -1}}], updates: [], transfers: [], - revoke: {{0, 0}, 0}, + revoke: {{0, 0}, {0, -1}}, auction_timeout: 1 )} @@ -696,9 +696,9 @@ defmodule AeMdwWeb.NameControllerTest do {:ok, Model.auction_bid( index: plain_name, - block_index_txi: {{0, 1}, 0}, + block_index_txi_idx: {{0, 1}, {0, -1}}, expire_height: 0, - bids: [{{2, 3}, 4}] + bids: [{{2, 3}, {4, -1}}] )} end, next_key: fn @@ -749,9 +749,9 @@ defmodule AeMdwWeb.NameControllerTest do {:ok, Model.auction_bid( index: plain_name, - block_index_txi: {{0, 1}, 3}, + block_index_txi_idx: {{0, 1}, {3, -1}}, expire_height: 0, - bids: [{{2, 3}, 4}] + bids: [{{2, 3}, {4, -1}}] )} end, last_key: fn AuctionExpiration -> {:ok, expiration_key} end, @@ -807,9 +807,9 @@ defmodule AeMdwWeb.NameControllerTest do {:ok, Model.auction_bid( index: plain_name, - block_index_txi: {{0, 1}, 0}, + block_index_txi_idx: {{0, 1}, {0, -1}}, expire_height: 30, - bids: [{{2, 3}, 4}] + bids: [{{2, 3}, {4, -1}}] )} end, first_key: fn AuctionExpiration -> {:ok, expiration_key} end, @@ -873,10 +873,10 @@ defmodule AeMdwWeb.NameControllerTest do Model.name( active: true, expire: 1, - claims: [{{0, 0}, 0}], + claims: [{{0, 0}, {0, -1}}], updates: [], transfers: [], - revoke: {{0, 0}, 0}, + revoke: {{0, 0}, {0, -1}}, auction_timeout: 1 )} @@ -926,10 +926,10 @@ defmodule AeMdwWeb.NameControllerTest do Model.name( active: true, expire: 1, - claims: [{{0, 0}, 0}], + claims: [{{0, 0}, {0, -1}}], updates: [], transfers: [], - revoke: {{0, 0}, 0}, + revoke: {{0, 0}, {0, -1}}, auction_timeout: 1 )} end, @@ -972,7 +972,7 @@ defmodule AeMdwWeb.NameControllerTest do index: first_name, active: false, expire: 3, - claims: [{{2, 0}, 0}], + claims: [{{2, 0}, {0, -1}}], updates: [], transfers: [], revoke: nil, @@ -1063,10 +1063,10 @@ defmodule AeMdwWeb.NameControllerTest do Model.name( active: Enum.random(1000..9999), expire: 1, - claims: [{{0, 0}, 0}], + claims: [{{0, 0}, {0, -1}}], updates: [], transfers: [], - revoke: {{0, 0}, 0}, + revoke: {{0, 0}, {0, -1}}, auction_timeout: 1 )} @@ -1120,7 +1120,7 @@ defmodule AeMdwWeb.NameControllerTest do Model.name( active: active_from, expire: active_from + 10, - claims: [{{active_from, 0}, 0}] + claims: [{{active_from, 0}, {0, -1}}] )} AuctionBid, _key -> @@ -1162,7 +1162,7 @@ defmodule AeMdwWeb.NameControllerTest do index: first_name, active: false, expire: 3, - claims: [{{2, 0}, 0}], + claims: [{{2, 0}, {0, -1}}], updates: [], transfers: [], revoke: nil, @@ -1228,7 +1228,7 @@ defmodule AeMdwWeb.NameControllerTest do index: first_name, active: false, expire: 3, - claims: [{{2, 0}, 0}], + claims: [{{2, 0}, {0, -1}}], updates: [], transfers: [], revoke: nil, @@ -1322,8 +1322,8 @@ defmodule AeMdwWeb.NameControllerTest do index: name, owner: alice_pk, active: active_from, - claims: [{{1, 1}, 1}], - updates: [{{1, 1}, 2}], + claims: [{{1, 1}, {1, -1}}], + updates: [{{1, 1}, {2, -1}}], expire: expire ) ) @@ -1394,8 +1394,8 @@ defmodule AeMdwWeb.NameControllerTest do index: plain_name, owner: owner_pk, active: 10, - claims: [{{1, 1}, 1}], - transfers: [{{1, 1}, 2}], + claims: [{{1, 1}, {1, -1}}], + transfers: [{{1, 1}, {2, -1}}], expire: 10_000 ) ) @@ -1455,8 +1455,8 @@ defmodule AeMdwWeb.NameControllerTest do index: plain_name, owner: owner_pk, active: 10, - claims: [{{1, 1}, 1}], - transfers: [{{1, 1}, 2}], + claims: [{{1, 1}, {1, -1}}], + transfers: [{{1, 1}, {2, -1}}], expire: 10_000 ) ) @@ -1512,13 +1512,17 @@ defmodule AeMdwWeb.NameControllerTest do index: name, owner: alice_pk, active: active_from, - claims: [{{10, 1}, claim_txi}], + claims: [{{10, 1}, {claim_txi, -1}}], expire: expire ) ) |> Store.put( Model.AuctionBid, - Model.auction_bid(index: name, expire_height: bid_expire, bids: [{{1, 1}, bid_txi}]) + Model.auction_bid( + index: name, + expire_height: bid_expire, + bids: [{{1, 1}, {bid_txi, -1}}] + ) ) |> Store.put(Model.AuctionOwner, Model.owner(index: {alice_pk, name})) |> Store.put(Model.AuctionExpiration, Model.expiration(index: {bid_expire, name})) @@ -1604,7 +1608,48 @@ defmodule AeMdwWeb.NameControllerTest do Model.tx(index: 2, block_index: {0, 0}, id: :aetx_sign.hash(tx2)) ) |> Store.put(Model.PlainName, Model.plain_name(index: name_hash, value: name)) - |> Store.put(Model.ActiveName, Model.name(index: name, updates: [{{0, 0}, 2}])) + |> Store.put(Model.ActiveName, Model.name(index: name, updates: [{{0, 0}, {2, -1}}])) + + {:id, :account, alice_pk} = accounts[:alice] + alice_id = encode(:account_pubkey, alice_pk) + oracle_id = encode(:oracle_pubkey, alice_pk) + + assert %{ + "account_pubkey" => ^alice_id, + "oracle_pubkey" => ^oracle_id + } = + conn + |> with_store(store) + |> get("/v2/names/#{name}/pointers") + |> json_response(200) + end + end + + test "when last update tx is internal, it gets pointers for valid given name", %{ + conn: conn, + store: store + } do + name = "wwwbeaconoidcom.chain" + {:ok, name_hash} = :aens.get_name_hash(name) + + with_blockchain %{alice: 1_000}, + mb: [ + tx1: name_tx(:name_claim_tx, :alice, name), + tx2: name_tx(:name_update_tx, :alice, name) + ] do + tx2 = transactions[:tx2] + aetx2 = :aetx_sign.tx(tx2) + + store = + store + |> Store.put(Model.Block, Model.block(index: {0, 0}, tx_index: 2)) + |> Store.put( + Model.Tx, + Model.tx(index: 2, block_index: {0, 0}, id: :aetx_sign.hash(tx2)) + ) + |> Store.put(Model.PlainName, Model.plain_name(index: name_hash, value: name)) + |> Store.put(Model.ActiveName, Model.name(index: name, updates: [{{0, 0}, {2, 0}}])) + |> Store.put(Model.IntContractCall, Model.int_contract_call(index: {2, 0}, tx: aetx2)) {:id, :account, alice_pk} = accounts[:alice] alice_id = encode(:account_pubkey, alice_pk) @@ -1906,6 +1951,10 @@ defmodule AeMdwWeb.NameControllerTest do |> name_claims_store(plain_name) |> Store.put(Model.IntContractCall, int_contract_call) + {:ok, name} = Store.get(store, Model.ActiveName, plain_name) + name = Model.name(name, claims: [{{123, 0}, {call_txi, 1}}]) + store = Store.put(store, Model.ActiveName, name) + with_mocks [ {Db, [], [ @@ -1923,7 +1972,7 @@ defmodule AeMdwWeb.NameControllerTest do assert %{ "height" => 123, - "tx" => %{"type" => "NameClaimTx", "fee" => 111_111, "name" => ^plain_name} + "tx" => %{"fee" => 111_111, "name" => ^plain_name} } = claim1 end end @@ -2121,9 +2170,9 @@ defmodule AeMdwWeb.NameControllerTest do end defp name_claims_store(store, plain_name) do - claim_bi_txi_1 = {{123, 0}, 567} - claim_bi_txi_2 = {{123, 0}, 678} - claim_bi_txi_3 = {{124, 1}, 788} + claim_bi_txi_1 = {{123, 0}, {567, -1}} + claim_bi_txi_2 = {{123, 0}, {678, -1}} + claim_bi_txi_3 = {{124, 1}, {788, -1}} name = Model.name( diff --git a/test/ae_mdw_web/controllers/oracle_controller_test.exs b/test/ae_mdw_web/controllers/oracle_controller_test.exs index db763f432..6779af40d 100644 --- a/test/ae_mdw_web/controllers/oracle_controller_test.exs +++ b/test/ae_mdw_web/controllers/oracle_controller_test.exs @@ -105,15 +105,15 @@ defmodule AeMdwWeb.OracleControllerTest do oracle1 = Model.oracle(TS.oracle(), index: oracle_pk1, - register: {{0, -1}, register_txi1}, + register: {{0, -1}, {register_txi1, -1}}, expire: exp_height1, - extends: [{{0, -1}, extends_txi}] + extends: [{{0, -1}, {extends_txi, -1}}] ) oracle2 = Model.oracle(TS.oracle(), index: oracle_pk2, - register: {{0, -1}, register_txi2}, + register: {{0, -1}, {register_txi2, -1}}, expire: exp_height2, extends: [] ) diff --git a/test/support/test_samples.ex b/test/support/test_samples.ex index 047192bcb..327e5a313 100644 --- a/test/support/test_samples.ex +++ b/test/support/test_samples.ex @@ -49,10 +49,16 @@ defmodule AeMdw.TestSamples do @spec oracle() :: Model.oracle() def oracle do - {:oracle, - <<191, 26, 12, 186, 4, 218, 249, 80, 53, 193, 143, 183, 72, 192, 245, 70, 194, 128, 17, 36, - 177, 223, 218, 73, 7, 139, 204, 31, 194, 87, 254, 212>>, 4608, 5851, {{4608, 0}, 8916}, - [{{4609, 0}, 8989}], nil} + Model.oracle( + index: + <<191, 26, 12, 186, 4, 218, 249, 80, 53, 193, 143, 183, 72, 192, 245, 70, 194, 128, 17, + 36, 177, 223, 218, 73, 7, 139, 204, 31, 194, 87, 254, 212>>, + active: 4608, + expire: 5851, + register: {{4608, 0}, {8916, -1}}, + extends: [{{4609, 0}, {8989, -1}}], + previous: nil + ) end @spec core_oracle() :: :aeo_oracles.oracle()