Skip to content

Commit

Permalink
feat: introduce {bi, {txi, local_idx}} for precise internal txs refs (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
sborrazas committed Dec 23, 2022
1 parent bcbfa5b commit e5df7b5
Show file tree
Hide file tree
Showing 34 changed files with 418 additions and 401 deletions.
6 changes: 3 additions & 3 deletions lib/ae_mdw/auction_bids.ex
Expand Up @@ -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)

%{
Expand All @@ -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

Expand Down
1 change: 1 addition & 0 deletions lib/ae_mdw/blocks.ex
Expand Up @@ -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>>

Expand Down
18 changes: 11 additions & 7 deletions lib/ae_mdw/db/format.ex
Expand Up @@ -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}
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand Down
24 changes: 13 additions & 11 deletions lib/ae_mdw/db/model.ex
Expand Up @@ -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()
Expand Down Expand Up @@ -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: []
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
)

Expand Down
10 changes: 5 additions & 5 deletions lib/ae_mdw/db/mutations/int_calls_mutation.ex
Expand Up @@ -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(),
Expand All @@ -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})
Expand All @@ -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 =
Expand Down
45 changes: 22 additions & 23 deletions lib/ae_mdw/db/mutations/name_claim_mutation.ex
Expand Up @@ -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
Expand All @@ -25,7 +24,7 @@ defmodule AeMdw.Db.NameClaimMutation do
:owner_pk,
:name_fee,
:lima_or_higher?,
:txi,
:txi_idx,
:block_index,
:timeout
]
Expand All @@ -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()
}
Expand All @@ -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
}
Expand All @@ -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
},
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
17 changes: 10 additions & 7 deletions lib/ae_mdw/db/mutations/name_revoke_mutation.ex
Expand Up @@ -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
14 changes: 7 additions & 7 deletions lib/ae_mdw/db/mutations/name_transfer_mutation.ex
Expand Up @@ -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
Expand All @@ -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

0 comments on commit e5df7b5

Please sign in to comment.