Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
explorer: stake control and implement public tally
this could re-use more code from the ledger, but it's not that easy to ignore validations and also I think this is simple enough for now. The implementation is somewhat isolated, so it's not that hard to change things after some more thought.
- Loading branch information
1 parent
8a65598
commit 3e14b75
Showing
5 changed files
with
156 additions
and
17 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
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,44 @@ | ||
use chain_impl_mockchain::stake::StakeControl; | ||
|
||
use super::indexing::ExplorerVoteProposal; | ||
use crate::db::indexing::{ExplorerVote, ExplorerVoteTally}; | ||
|
||
pub fn compute_private_tally(proposal: &ExplorerVoteProposal) -> ExplorerVoteTally { | ||
tracing::warn!("private tally triggered but it is not implemented"); | ||
|
||
ExplorerVoteTally::Private { | ||
results: None, | ||
options: proposal.options.clone(), | ||
} | ||
} | ||
|
||
pub fn compute_public_tally( | ||
proposal: &ExplorerVoteProposal, | ||
stake: &StakeControl, | ||
) -> ExplorerVoteTally { | ||
let mut results = vec![0u64; proposal.options.choice_range().end as usize]; | ||
|
||
for (address, vote) in proposal.votes.iter() { | ||
if let Some(account_id) = address.to_single_account() { | ||
if let Some(stake) = stake.by(&account_id) { | ||
match vote.as_ref() { | ||
ExplorerVote::Public(choice) => { | ||
let index = choice.as_byte() as usize; | ||
results[index] = results[index].saturating_add(stake.into()); | ||
} | ||
ExplorerVote::Private { | ||
proof: _, | ||
encrypted_vote: _, | ||
} => { | ||
unreachable!("internal error: found private vote when computing tally for public proposal") | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
ExplorerVoteTally::Public { | ||
results: results.drain(..).map(u64::into).collect(), | ||
options: proposal.options.clone(), | ||
} | ||
} |