ruvector_dag/qudag/
consensus.rs1#[derive(Debug, Clone)]
4pub struct ConsensusResult {
5 pub round: u64,
6 pub proposal_id: String,
7 pub accepted: bool,
8 pub stake_weight: f64,
9 pub validator_count: usize,
10}
11
12#[derive(Debug, Clone)]
13pub struct Vote {
14 pub voter_id: String,
15 pub proposal_id: String,
16 pub approve: bool,
17 pub stake_weight: f64,
18 pub signature: Vec<u8>, }
20
21impl Vote {
22 pub fn new(voter_id: String, proposal_id: String, approve: bool, stake_weight: f64) -> Self {
23 Self {
24 voter_id,
25 proposal_id,
26 approve,
27 stake_weight,
28 signature: Vec::new(),
29 }
30 }
31
32 pub fn sign(&mut self, _private_key: &[u8]) {
33 self.signature = vec![0u8; 64];
35 }
36
37 pub fn verify(&self, _public_key: &[u8]) -> bool {
38 !self.signature.is_empty()
40 }
41}
42
43#[allow(dead_code)]
44pub struct ConsensusTracker {
45 proposals: std::collections::HashMap<String, Vec<Vote>>,
46 threshold: f64, }
48
49#[allow(dead_code)]
50impl ConsensusTracker {
51 pub fn new(threshold: f64) -> Self {
52 Self {
53 proposals: std::collections::HashMap::new(),
54 threshold,
55 }
56 }
57
58 pub fn add_vote(&mut self, vote: Vote) {
59 self.proposals
60 .entry(vote.proposal_id.clone())
61 .or_default()
62 .push(vote);
63 }
64
65 pub fn check_consensus(&self, proposal_id: &str) -> Option<ConsensusResult> {
66 let votes = self.proposals.get(proposal_id)?;
67
68 let total_stake: f64 = votes.iter().map(|v| v.stake_weight).sum();
69 let approve_stake: f64 = votes
70 .iter()
71 .filter(|v| v.approve)
72 .map(|v| v.stake_weight)
73 .sum();
74
75 let accepted = approve_stake / total_stake > self.threshold;
76
77 Some(ConsensusResult {
78 round: 0,
79 proposal_id: proposal_id.to_string(),
80 accepted,
81 stake_weight: total_stake,
82 validator_count: votes.len(),
83 })
84 }
85}