Skip to content

Commit

Permalink
fix: add fee field to txs table instead of fetching it from the node
Browse files Browse the repository at this point in the history
  • Loading branch information
yaboiishere committed Jan 30, 2025
1 parent 19cfbd0 commit 5497682
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
7 changes: 4 additions & 3 deletions lib/ae_mdw/db/model.ex
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ defmodule AeMdw.Db.Model do
)

# txs table :
# index = tx_index (0..), id = tx_id, block_index = {kbi, mbi}
@tx_defaults [index: nil, id: nil, block_index: nil, time: nil]
# index = tx_index (0..), id = tx_id, block_index = {kbi, mbi}, time = block_time, fee = fee
@tx_defaults [index: nil, id: nil, block_index: nil, time: nil, fee: nil]
defrecord :tx, @tx_defaults

@type tx_index() :: txi()
Expand All @@ -152,7 +152,8 @@ defmodule AeMdw.Db.Model do
index: tx_index(),
id: Txs.tx_hash(),
block_index: block_index(),
time: Blocks.time()
time: Blocks.time(),
fee: non_neg_integer()
)

# txs time index :
Expand Down
10 changes: 10 additions & 0 deletions lib/ae_mdw/node/db.ex
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,16 @@ defmodule AeMdw.Node.Db do
end
end

@spec get_tx_fee!(binary()) :: non_neg_integer()
def get_tx_fee!(<<_pk::256>> = tx_hash) do
case :aec_chain.find_tx_with_location(tx_hash) do
{_block_hash, signed_tx} ->
signed_tx
|> :aetx_sign.tx()
|> :aetx.fee()
end
end

@spec top_height_hash(boolean()) :: height_hash()
def top_height_hash(false = _the_very_top?) do
block = :aec_chain.top_key_block() |> ok!
Expand Down
12 changes: 11 additions & 1 deletion lib/ae_mdw/stats.ex
Original file line number Diff line number Diff line change
Expand Up @@ -687,13 +687,23 @@ defmodule AeMdw.Stats do
end

defp average_tx_fees(state, start_txi, end_txi) do
IO.inspect(start_txi, label: "start_txi")
IO.inspect(end_txi, label: "end_txi")
txs_count = end_txi - start_txi + 1

if txs_count != 0 do
start_txi..end_txi
|> Enum.reduce(0, fn tx_index, acc ->
Model.tx(id: tx_hash) = State.fetch!(state, Model.Tx, tx_index)
fee = NodeDb.get_tx_fee(tx_hash)
fee = NodeDb.get_tx_fee!(tx_hash)

if fee == nil do
NodeDb.get_tx_fee!(tx_hash)
|> IO.inspect(label: "fee")
IO.inspect(tx_hash, label: "tx_hash")
IO.inspect(tx_hash, label: "#{tx_index} tx_hash")
IO.inspect(tx_index, label: "tx_index")
end

acc + fee
end)
Expand Down
38 changes: 38 additions & 0 deletions priv/migrations/20250130092944_populate_txs_fee_field.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
defmodule AeMdw.Migrations.PopulateTxsFeeField do
require Record
alias AeMdw.Db.WriteMutation
alias AeMdw.Db.RocksDbCF
alias AeMdw.Db.State
alias AeMdw.Db.Model
alias AeMdw.Node.Db, as: NodeDb

require Model

import Record, only: [defrecord: 2]

defrecord :tx,
index: nil,
id: nil,
block_index: nil,
time: nil

@spec run(State.t(), boolean()) :: {:ok, non_neg_integer()}
def run(_state, _from_start?) do
RocksDbCF.stream(Model.Tx)
|> Stream.map(fn tx(index: index, id: tx_hash, block_index: bi, time: time) ->
fee = NodeDb.get_tx_fee!(tx_hash)
new_tx = Model.tx(index: index, id: tx_hash, block_index: bi, time: time, fee: fee)

IO.inspect(fee, label: "fee")

WriteMutation.new(Model.Tx, new_tx)
end)
|> Stream.chunk_every(1000)
|> Stream.map(fn mutations ->
_state = State.commit_db(state, mutations)
length(mutations)
end)
|> Enum.sum()
|> then(&{:ok, &1})
end
end

0 comments on commit 5497682

Please sign in to comment.