Skip to content

Commit

Permalink
feat: expose names locked/burned fees on stats (#822)
Browse files Browse the repository at this point in the history
  • Loading branch information
sborrazas committed Aug 5, 2022
1 parent c5e7f40 commit d75d45f
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 17 deletions.
16 changes: 12 additions & 4 deletions lib/ae_mdw/db/model.ex
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,9 @@ defmodule AeMdw.Db.Model do
oracles_expired: 0,
contracts_created: 0,
block_reward: 0,
dev_reward: 0
dev_reward: 0,
locked_in_auctions: 0,
burned_in_auctions: 0
]
defrecord :delta_stat, @delta_stat_defaults

Expand All @@ -553,7 +555,9 @@ defmodule AeMdw.Db.Model do
oracles_expired: integer(),
contracts_created: integer(),
block_reward: integer(),
dev_reward: integer()
dev_reward: integer(),
locked_in_auctions: integer(),
burned_in_auctions: integer()
)

# summarized statistics
Expand All @@ -568,7 +572,9 @@ defmodule AeMdw.Db.Model do
inactive_names: 0,
active_oracles: 0,
inactive_oracles: 0,
contracts: 0
contracts: 0,
locked_in_auctions: 0,
burned_in_auctions: 0
]
defrecord :total_stat, @total_stat_defaults

Expand All @@ -583,7 +589,9 @@ defmodule AeMdw.Db.Model do
inactive_names: integer(),
active_oracles: integer(),
inactive_oracles: integer(),
contracts: integer()
contracts: integer(),
locked_in_auctions: non_neg_integer(),
burned_in_auctions: non_neg_integer()
)

################################################################################
Expand Down
6 changes: 4 additions & 2 deletions lib/ae_mdw/db/mutations/name_claim_mutation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ defmodule AeMdw.Db.NameClaimMutation do
|> cache_through_delete_inactive(previous)
|> IntTransfer.fee({height, txi}, :lock_name, owner_pk, txi, lock_amount)
|> State.inc_stat(:names_activated)
|> State.inc_stat(:burned_in_auctions, lock_amount)

timeout ->
auction_end = height + timeout
Expand Down Expand Up @@ -146,7 +147,7 @@ defmodule AeMdw.Db.NameClaimMutation do
owner: prev_owner,
bids: prev_bids
)} ->
%{tx: prev_tx} = read_cached_raw_tx!(state, prev_txi)
%{tx: %{name_fee: prev_name_fee}} = read_cached_raw_tx!(state, prev_txi)

state4 =
state3
Expand All @@ -158,8 +159,9 @@ defmodule AeMdw.Db.NameClaimMutation do
:refund_name,
prev_owner,
prev_txi,
prev_tx.name_fee
prev_name_fee
)
|> State.inc_stat(:locked_in_auctions, name_fee - prev_name_fee)

{make_m_bid.([{block_index, txi} | prev_bids]), state4}
end
Expand Down
42 changes: 37 additions & 5 deletions lib/ae_mdw/db/mutations/stats_mutation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule AeMdw.Db.StatsMutation do
Inserts statistics about this generation into Model.DeltaStat table.
"""

alias AeMdw.Collection
alias AeMdw.Db.Model
alias AeMdw.Blocks
alias AeMdw.Db.IntTransfer
Expand All @@ -11,6 +12,7 @@ defmodule AeMdw.Db.StatsMutation do
alias AeMdw.Db.Oracle
alias AeMdw.Db.Origin
alias AeMdw.Db.State
alias AeMdw.Util

require Model

Expand Down Expand Up @@ -56,7 +58,9 @@ defmodule AeMdw.Db.StatsMutation do
oracles_expired: get(state, :oracles_expired, 0),
contracts_created: get(state, :contracts_created, 0),
block_reward: get(state, :block_reward, 0),
dev_reward: get(state, :dev_reward, 0)
dev_reward: get(state, :dev_reward, 0),
locked_in_auctions: get(state, :locked_in_auctions, 0),
burned_in_auctions: get(state, :burned_in_auctions, 0)
)
end

Expand Down Expand Up @@ -96,6 +100,11 @@ defmodule AeMdw.Db.StatsMutation do
current_block_reward = IntTransfer.read_block_reward(state, height)
current_dev_reward = IntTransfer.read_dev_reward(state, height)

burned_in_auctions = height_int_amount(state, height, :lock_name)
spent_in_auctions = height_int_amount(state, height, :spend_name)
refund_in_auctions = height_int_amount(state, height, :refund_name)
locked_in_auctions = spent_in_auctions - refund_in_auctions

Model.delta_stat(
index: height,
auctions_started: max(0, current_active_auctions - prev_active_auctions),
Expand All @@ -106,7 +115,9 @@ defmodule AeMdw.Db.StatsMutation do
oracles_expired: oracles_expired_count,
contracts_created: max(0, all_contracts_count - prev_contracts),
block_reward: current_block_reward,
dev_reward: current_dev_reward
dev_reward: current_dev_reward,
locked_in_auctions: locked_in_auctions,
burned_in_auctions: burned_in_auctions
)
end

Expand All @@ -123,7 +134,9 @@ defmodule AeMdw.Db.StatsMutation do
oracles_expired: oracles_expired,
contracts_created: contracts_created,
block_reward: inc_block_reward,
dev_reward: inc_dev_reward
dev_reward: inc_dev_reward,
locked_in_auctions: locked_in_auctions,
burned_in_auctions: burned_in_auctions
)
) do
Model.total_stat(
Expand All @@ -135,7 +148,9 @@ defmodule AeMdw.Db.StatsMutation do
inactive_names: prev_inactive_names,
active_oracles: prev_active_oracles,
inactive_oracles: prev_inactive_oracles,
contracts: prev_contracts
contracts: prev_contracts,
locked_in_auctions: prev_locked_in_auctions,
burned_in_auctions: prev_burned_in_acutions
) = fetch_total_stat(state, height - 1)

token_supply_delta = AeMdw.Node.token_supply_delta(height - 1)
Expand All @@ -151,7 +166,9 @@ defmodule AeMdw.Db.StatsMutation do
inactive_names: prev_inactive_names + names_expired + names_revoked,
active_oracles: max(0, prev_active_oracles + oracles_registered - oracles_expired),
inactive_oracles: prev_inactive_oracles + oracles_expired,
contracts: prev_contracts + contracts_created
contracts: prev_contracts + contracts_created,
locked_in_auctions: prev_locked_in_auctions + locked_in_auctions,
burned_in_auctions: prev_burned_in_acutions + burned_in_auctions
)
end

Expand All @@ -164,4 +181,19 @@ defmodule AeMdw.Db.StatsMutation do
defp fetch_total_stat(state, height) do
State.fetch!(state, Model.TotalStat, height)
end

defp height_int_amount(state, height, kind) do
kind_str = "fee_#{kind}"

state
|> Collection.stream(Model.KindIntTransferTx, {kind_str, {height, -1}, Util.min_bin(), nil})
|> Stream.take_while(&match?({^kind_str, {^height, _mbi}, _address, _ref_txi}, &1))
|> Stream.map(fn {kind_str, block_index, address, ref_txi} ->
{block_index, kind_str, address, ref_txi}
end)
|> Stream.map(&State.fetch!(state, Model.IntTransferTx, &1))
|> Enum.reduce(0, fn Model.int_transfer_tx(amount: amount), amount_acc ->
amount_acc + amount
end)
end
end
6 changes: 4 additions & 2 deletions lib/ae_mdw/db/name.ex
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ defmodule AeMdw.Db.Name do
m_name_activation = Model.activation(index: {height, plain_name})
m_name_exp = Model.expiration(index: {expire, plain_name})
m_owner = Model.owner(index: {owner, plain_name})
%{tx: winning_tx} = read_raw_tx!(state, txi)
%{tx: %{name_fee: name_fee}} = read_raw_tx!(state, txi)

state
|> cache_through_write(Model.ActiveName, m_name)
Expand All @@ -147,9 +147,11 @@ defmodule AeMdw.Db.Name do
|> cache_through_delete(Model.AuctionOwner, {owner, plain_name})
|> cache_through_delete(Model.AuctionBid, plain_name)
|> cache_through_delete_inactive(previous)
|> IntTransfer.fee({height, -1}, :lock_name, owner, txi, winning_tx.name_fee)
|> IntTransfer.fee({height, -1}, :lock_name, owner, txi, name_fee)
|> State.inc_stat(:names_activated)
|> State.inc_stat(:auctions_expired)
|> State.inc_stat(:burned_in_auctions, name_fee)
|> State.inc_stat(:locked_in_auctions, -name_fee)
end

@doc """
Expand Down
16 changes: 12 additions & 4 deletions lib/ae_mdw/stats.ex
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ defmodule AeMdw.Stats do
oracles_expired: oracles_expired,
contracts_created: contracts_created,
block_reward: block_reward,
dev_reward: dev_reward
dev_reward: dev_reward,
locked_in_auctions: locked_in_auctions,
burned_in_auctions: burned_in_auctions
)
) do
%{
Expand All @@ -157,7 +159,9 @@ defmodule AeMdw.Stats do
oracles_expired: oracles_expired,
contracts_created: contracts_created,
block_reward: block_reward,
dev_reward: dev_reward
dev_reward: dev_reward,
locked_in_auctions: locked_in_auctions,
burned_in_auctions: burned_in_auctions
}
end

Expand All @@ -172,7 +176,9 @@ defmodule AeMdw.Stats do
inactive_oracles: inactive_oracles,
block_reward: block_reward,
dev_reward: dev_reward,
total_supply: total_supply
total_supply: total_supply,
locked_in_auctions: locked_in_auctions,
burned_in_auctions: burned_in_auctions
)
) do
%{
Expand All @@ -185,7 +191,9 @@ defmodule AeMdw.Stats do
inactive_oracles: inactive_oracles,
sum_block_reward: block_reward,
sum_dev_reward: dev_reward,
total_token_supply: total_supply
total_token_supply: total_supply,
locked_in_auctions: locked_in_auctions,
burned_in_auctions: burned_in_auctions
}
end

Expand Down
116 changes: 116 additions & 0 deletions priv/migrations/20220803120000_add_name_fees_to_stats.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
defmodule AeMdw.Migrations.AddNameFeesToStats do
@moduledoc """
Add auctions_burned and auctions_locked attribute to delta and total stats.
"""

alias AeMdw.Collection
alias AeMdw.Db.Model
alias AeMdw.Db.State
alias AeMdw.Db.WriteMutation
alias AeMdw.Util

require Model

@spec run(boolean()) :: {:ok, {non_neg_integer(), non_neg_integer()}}
def run(_from_start?) do
begin = DateTime.utc_now()
state = State.new()

{mutations, _last_burned_locked} =
state
|> Collection.stream(Model.DeltaStat, nil)
|> Stream.map(fn height ->
{State.fetch!(state, Model.DeltaStat, height),
State.fetch!(state, Model.TotalStat, height + 1)}
end)
|> Enum.flat_map_reduce({0, 0}, fn {old_delta_stat, old_total_stat},
{prev_burned, prev_locked} ->
delta_stat = Model.delta_stat(index: height) = transform_delta_stat(old_delta_stat)
total_stat = transform_total_stat(old_total_stat)

delta_burned = height_int_amount(state, height, :lock_name)
delta_spend = height_int_amount(state, height, :spend_name)
delta_refund = height_int_amount(state, height, :refund_name)
delta_locked = delta_spend - delta_refund
new_total_burned = prev_burned + delta_burned
new_total_locked = prev_locked + delta_locked

new_delta_stat =
Model.delta_stat(delta_stat,
burned_in_auctions: delta_burned,
locked_in_auctions: delta_locked
)

new_total_stat =
Model.total_stat(total_stat,
burned_in_auctions: new_total_burned,
locked_in_auctions: new_total_locked
)

{
[
WriteMutation.new(Model.DeltaStat, new_delta_stat),
WriteMutation.new(Model.TotalStat, new_total_stat)
],
{new_total_burned, new_total_locked}
}
end)

State.commit(state, mutations)

duration = DateTime.diff(DateTime.utc_now(), begin)

{:ok, {div(length(mutations), 2), duration}}
end

defp transform_delta_stat(
{:delta_stat, height, auctions_started, names_activated, names_expired, names_revoked,
oracles_registered, oracles_expired, contracts_created, block_reward, dev_reward}
) do
Model.delta_stat(
index: height,
auctions_started: auctions_started,
names_activated: names_activated,
names_expired: names_expired,
names_revoked: names_revoked,
oracles_registered: oracles_registered,
oracles_expired: oracles_expired,
contracts_created: contracts_created,
block_reward: block_reward,
dev_reward: dev_reward
)
end

defp transform_total_stat(
{:total_stat, height, block_reward, dev_reward, total_supply, active_auctions,
active_names, inactive_names, active_oracles, inactive_oracles, contracts}
) do
Model.total_stat(
index: height,
block_reward: block_reward,
dev_reward: dev_reward,
total_supply: total_supply,
active_auctions: active_auctions,
active_names: active_names,
inactive_names: inactive_names,
active_oracles: active_oracles,
inactive_oracles: inactive_oracles,
contracts: contracts
)
end

defp height_int_amount(state, height, kind) do
kind_str = "fee_#{kind}"

state
|> Collection.stream(Model.KindIntTransferTx, {kind_str, {height, -1}, Util.min_bin(), nil})
|> Stream.take_while(&match?({^kind_str, {^height, _mbi}, _address, _ref_txi}, &1))
|> Stream.map(fn {kind_str, block_index, address, ref_txi} ->
{block_index, kind_str, address, ref_txi}
end)
|> Stream.map(&State.fetch!(state, Model.IntTransferTx, &1))
|> Enum.reduce(0, fn Model.int_transfer_tx(amount: amount), amount_acc ->
amount_acc + amount
end)
end
end
1 change: 1 addition & 0 deletions test/ae_mdw/db/mutations/stats_mutation_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ defmodule AeMdw.Db.StatsMutationTest do
stream: fn
_state, Model.Origin, _start_key -> [1, 2, 3]
_state, Model.IntTransferTx, _start_key -> []
_state, Model.KindIntTransferTx, _start_key -> []
end
]}
] do
Expand Down

0 comments on commit d75d45f

Please sign in to comment.