-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4a3ebc1
commit 4f5d992
Showing
9 changed files
with
438 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
defmodule AeMdw.Db.TopMinerStatsMutation do | ||
@moduledoc """ | ||
Increments the top miners stats. | ||
""" | ||
|
||
alias AeMdw.Collection | ||
alias AeMdw.Db.IntTransfer | ||
alias AeMdw.Db.State | ||
alias AeMdw.Db.Model | ||
alias AeMdw.Db.Sync.Stats | ||
|
||
require Model | ||
|
||
@derive AeMdw.Db.Mutation | ||
defstruct [:rewards, :time] | ||
|
||
@opaque t() :: %__MODULE__{rewards: IntTransfer.rewards(), time: non_neg_integer()} | ||
|
||
@spec new(IntTransfer.rewards(), non_neg_integer()) :: t() | ||
def new(rewards, time), do: %__MODULE__{rewards: rewards, time: time} | ||
|
||
@spec execute(t(), State.t()) :: State.t() | ||
def execute(%__MODULE__{rewards: rewards, time: time}, state) do | ||
Enum.reduce(rewards, state, fn {beneficiary_pk, _reward}, state -> | ||
increment_top_miners(state, time, beneficiary_pk) | ||
end) | ||
end | ||
|
||
defp increment_top_miners(state, time, beneficiary_pk) do | ||
time | ||
|> Stats.time_intervals() | ||
|> Enum.reduce(state, fn {interval_by, interval_start}, state -> | ||
kb = | ||
Collection.generate_key_boundary( | ||
{interval_by, interval_start, Collection.integer(), Collection.binary()} | ||
) | ||
|
||
state | ||
|> Collection.stream(Model.TopMinerStats, :backward, kb, nil) | ||
|> Stream.filter(fn {_interval_by, _interval_start, _count, bpk} -> | ||
bpk == beneficiary_pk | ||
end) | ||
|> tap(&IO.inspect(Enum.count(&1))) | ||
|> Enum.at(0, :none) | ||
|> case do | ||
{^interval_by, ^interval_start, count, ^beneficiary_pk} -> | ||
IO.inspect("updating") | ||
|
||
state | ||
|> State.delete( | ||
Model.TopMinerStats, | ||
{interval_by, interval_start, count, beneficiary_pk} | ||
) | ||
|> State.put( | ||
Model.TopMinerStats, | ||
Model.top_miner_stats(index: {interval_by, interval_start, count + 1, beneficiary_pk}) | ||
) | ||
|
||
:none -> | ||
IO.inspect("inserting missing") | ||
|
||
State.put( | ||
state, | ||
Model.TopMinerStats, | ||
Model.top_miner_stats(index: {interval_by, interval_start, 1, beneficiary_pk}) | ||
) | ||
end | ||
end) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
defmodule AeMdw.Migrations.GenerateTopMiners do | ||
alias AeMdw.Db.TopMinerStatsMutation | ||
alias AeMdw.Db.Model | ||
alias AeMdw.Db.RocksDbCF | ||
alias AeMdw.Db.State | ||
|
||
require Model | ||
|
||
@spec run(State.t(), boolean()) :: {:ok, non_neg_integer()} | ||
def run(state, _from_start?) do | ||
# dev_benefs = | ||
# for {protocol, _height} <- :aec_hard_forks.protocols(), | ||
# {pk, _share} <- :aec_dev_reward.beneficiaries(protocol) do | ||
# pk | ||
# end | ||
|
||
# delay = :aec_governance.beneficiary_reward_delay() | ||
|
||
{_state, count} = | ||
Model.Block | ||
|> RocksDbCF.stream() | ||
|> Stream.filter(fn Model.block(index: {_key_index, micro_index}) -> micro_index == -1 end) | ||
|> Stream.map(fn Model.block() = block -> | ||
Model.block(hash: hash) = block | ||
{:ok, key_block} = :aec_chain.get_block(hash) | ||
time = :aec_blocks.time_in_msecs(key_block) | ||
|
||
miner = | ||
key_block | ||
|> :aec_blocks.to_header() | ||
|> :aec_headers.miner() | ||
|
||
TopMinerStatsMutation.new([{miner, 0}], time) | ||
end) | ||
|> Stream.chunk_every(1000) | ||
|> Enum.reduce({state, 0}, fn mutations, {state, count} -> | ||
len = length(mutations) | ||
{State.commit_db(state, mutations), count + len} | ||
end) | ||
|
||
{:ok, count} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.