|
| 1 | +use crate::common::RepsVoterAssignerSource; |
| 2 | +use chain_addr::Discrimination; |
| 3 | +use fraction::Fraction; |
| 4 | +use jormungandr_lib::crypto::account::Identifier; |
| 5 | +use jormungandr_lib::interfaces::InitialUTxO; |
| 6 | +use jormungandr_lib::interfaces::Value; |
| 7 | +use snapshot_lib::registration::VotingRegistration; |
| 8 | +use snapshot_lib::voting_group::VotingGroupAssigner; |
| 9 | +use snapshot_lib::{RawSnapshot, Snapshot, VoterHIR}; |
| 10 | +use snapshot_trigger_service::client::SnapshotResult; |
| 11 | +use std::collections::HashSet; |
| 12 | + |
| 13 | +pub trait SnapshotFilterSource { |
| 14 | + fn filter( |
| 15 | + &self, |
| 16 | + voting_threshold: Value, |
| 17 | + cap: Fraction, |
| 18 | + voting_group_assigner: &impl VotingGroupAssigner, |
| 19 | + ) -> SnapshotFilter; |
| 20 | + fn filter_default(&self, reps: &HashSet<Identifier>) -> SnapshotFilter; |
| 21 | +} |
| 22 | + |
| 23 | +impl SnapshotFilterSource for SnapshotResult { |
| 24 | + fn filter( |
| 25 | + &self, |
| 26 | + voting_threshold: Value, |
| 27 | + cap: Fraction, |
| 28 | + voting_group_assigner: &impl VotingGroupAssigner, |
| 29 | + ) -> SnapshotFilter { |
| 30 | + SnapshotFilter::from_snapshot_result(self, voting_threshold, cap, voting_group_assigner) |
| 31 | + } |
| 32 | + |
| 33 | + fn filter_default(&self, reps: &HashSet<Identifier>) -> SnapshotFilter { |
| 34 | + SnapshotFilter::from_snapshot_result_default(self, reps) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +pub struct SnapshotFilter { |
| 39 | + snapshot: Snapshot, |
| 40 | +} |
| 41 | + |
| 42 | +impl SnapshotFilter { |
| 43 | + pub(crate) fn from_snapshot_result_default( |
| 44 | + result: &SnapshotResult, |
| 45 | + reps: &HashSet<Identifier>, |
| 46 | + ) -> Self { |
| 47 | + Self::from_snapshot_result( |
| 48 | + result, |
| 49 | + 450u64.into(), |
| 50 | + Fraction::new(1u64, 3u64), |
| 51 | + &reps.clone().into_reps_voter_assigner(), |
| 52 | + ) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +impl SnapshotFilter { |
| 57 | + pub fn from_snapshot_result( |
| 58 | + snapshot_result: &SnapshotResult, |
| 59 | + voting_threshold: Value, |
| 60 | + cap: Fraction, |
| 61 | + voting_group_assigner: &impl VotingGroupAssigner, |
| 62 | + ) -> SnapshotFilter { |
| 63 | + Self::from_voting_registrations( |
| 64 | + snapshot_result.registrations().to_vec(), |
| 65 | + voting_threshold, |
| 66 | + cap, |
| 67 | + voting_group_assigner, |
| 68 | + ) |
| 69 | + } |
| 70 | + |
| 71 | + pub fn from_voting_registrations( |
| 72 | + voting_registrations: Vec<VotingRegistration>, |
| 73 | + voting_threshold: Value, |
| 74 | + cap: Fraction, |
| 75 | + voting_group_assigner: &impl VotingGroupAssigner, |
| 76 | + ) -> SnapshotFilter { |
| 77 | + Self { |
| 78 | + snapshot: Snapshot::from_raw_snapshot( |
| 79 | + RawSnapshot::from(voting_registrations), |
| 80 | + voting_threshold, |
| 81 | + cap, |
| 82 | + voting_group_assigner, |
| 83 | + ) |
| 84 | + .unwrap(), |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + pub fn to_voters_hirs(&self) -> Vec<VoterHIR> { |
| 89 | + self.snapshot |
| 90 | + .voting_keys() |
| 91 | + .map(|vk| VoterHIR { |
| 92 | + voting_key: vk.clone(), |
| 93 | + voting_power: self |
| 94 | + .snapshot |
| 95 | + .contributions_for_voting_key(vk) |
| 96 | + .iter() |
| 97 | + .map(|c| c.value) |
| 98 | + .sum::<u64>() |
| 99 | + .into(), |
| 100 | + voting_group: "direct".to_string(), |
| 101 | + }) |
| 102 | + .collect() |
| 103 | + } |
| 104 | + |
| 105 | + pub fn to_block0_initials(&self) -> Vec<InitialUTxO> { |
| 106 | + self.snapshot.to_block0_initials(Discrimination::Production) |
| 107 | + } |
| 108 | + |
| 109 | + pub fn snapshot(&self) -> Snapshot { |
| 110 | + self.snapshot.clone() |
| 111 | + } |
| 112 | +} |
0 commit comments