This repository has been archived by the owner on Feb 24, 2025. It is now read-only.
forked from blockscout/blockscout
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Obasilakis/save voter active votes to db (#581)
* Add celo_voter_votes table * Add voters_activated_votes_in_last_epoch function * Add last_non_zero_voter_votes function and format * Add voter votes to celo_pending_epoch_operations * Add celo_voter_votes runner and fetcher * Buffer blocks to fetch epoch rewards and voter votes for * Add CeloVoterVotes fetcher to supervision tree * Fix query in voters_activated_votes_in_last_epoch * Pass array of blocks instead of single blocks * Add hash and number to account/group pairs in CeloVoterVotes fetcher pipeline * Lower max batch size * Remove IO.inspects * Make sure pending operations are falsified after votes are fetched * Remove duplicates before fetching votes from archive node * Reduce max_batch_size to 1 for voter votes fetcher Also dialyze * Make sure epoch blocks are ordered when buffered into voter votes fetcher * Format, credo, dialyze and remove deprecated test * Fix block fetcher tests * Set max concurrency to 1 * Change poll_interval to 60 min for voter votes fetcher * Stop buffering epoch blocks in the block fetcher The block runner takes care of that * Add PR to CHANGELOG.md * Keep large integers consistent * Set max concurrency and max batch size back to default The new way of getting account/group pairs doesn't require sequential processing * Retry unique blocks * Get all account/group pairs with activated votes instead of just last epoch * Remove leftover tag from test * Avoid homonymous multis * Set default celo_pending_epoch_operations to false * Format and dialyze * Fix test * Lower max_batch_size * Fetch and save vote only for accounts that have activated votes * Credo * Use event block_number since block_hash doesn't exist anymore * Format
- Loading branch information
1 parent
f609297
commit 3393ebf
Showing
28 changed files
with
836 additions
and
44 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
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 |
---|---|---|
|
@@ -90,6 +90,6 @@ defmodule Explorer.Celo.Util do | |
end | ||
|
||
def epoch_by_block_number(bn) do | ||
div(bn, 17280) | ||
div(bn, 17_280) | ||
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
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,76 @@ | ||
defmodule Explorer.Chain.CeloVoterVotes do | ||
@moduledoc """ | ||
Tracks individual active votes for every voter on each epoch. | ||
""" | ||
|
||
use Explorer.Schema | ||
|
||
alias Explorer.Chain.{Block, Hash, Wei} | ||
alias Explorer.Repo | ||
|
||
@required_attrs ~w(account_hash block_hash block_number active_votes group_hash)a | ||
|
||
@typedoc """ | ||
* `account_hash` - the hash of the voter's account. | ||
* `active_votes` - the number of the voter's active votes for a specific group. | ||
* `block_hash` - the hash of the block. | ||
* `block_number` - the number of the block. | ||
* `group_hash` - the hash of the group. | ||
""" | ||
@type t :: %__MODULE__{ | ||
account_hash: Hash.Address.t(), | ||
active_votes: Wei.t(), | ||
block_hash: Hash.Full.t(), | ||
block_number: integer, | ||
group_hash: Hash.Address.t() | ||
} | ||
|
||
@primary_key false | ||
schema "celo_voter_votes" do | ||
field(:active_votes, Wei) | ||
field(:block_number, :integer) | ||
field(:group_hash, Hash.Address) | ||
|
||
timestamps() | ||
|
||
belongs_to(:block, Block, foreign_key: :block_hash, primary_key: true, references: :hash, type: Hash.Full) | ||
|
||
belongs_to(:addresses, Explorer.Chain.Address, | ||
foreign_key: :account_hash, | ||
references: :hash, | ||
type: Hash.Address | ||
) | ||
end | ||
|
||
def changeset(%__MODULE__{} = celo_voter_votes, attrs) do | ||
celo_voter_votes | ||
|> cast(attrs, @required_attrs) | ||
|> validate_required(@required_attrs) | ||
|> foreign_key_constraint(:block_hash) | ||
|> foreign_key_constraint(:group_hash) | ||
|> foreign_key_constraint(:account_hash) | ||
|> unique_constraint( | ||
[:account_hash, :block_hash, :group_hash], | ||
name: :celo_voter_votes_account_hash_block_hash_group_hash_index | ||
) | ||
end | ||
|
||
def previous_epoch_non_zero_voter_votes(epoch_block_number) do | ||
zero_votes = %Explorer.Chain.Wei{value: Decimal.new(0)} | ||
previous_epoch_block_number = epoch_block_number - 17_280 | ||
|
||
query = | ||
from( | ||
votes in __MODULE__, | ||
where: votes.block_number == ^previous_epoch_block_number, | ||
where: votes.active_votes != ^zero_votes, | ||
select: %{ | ||
account_hash: votes.account_hash, | ||
group_hash: votes.group_hash | ||
} | ||
) | ||
|
||
query | ||
|> Repo.all() | ||
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
Oops, something went wrong.