Skip to content

Commit 284fddf

Browse files
committed
fix governance vote inserting
1 parent 7d1ad97 commit 284fddf

File tree

7 files changed

+55
-22
lines changed

7 files changed

+55
-22
lines changed

chain/src/repository/gov.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::HashSet;
1+
use std::collections::{HashSet};
22

33
use anyhow::Context;
44
use diesel::upsert::excluded;

chain/src/services/namada.rs

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -605,30 +605,55 @@ pub async fn query_all_votes(
605605
client: &HttpClient,
606606
proposals_ids: Vec<u64>,
607607
) -> anyhow::Result<HashSet<GovernanceVote>> {
608-
let votes: Vec<HashSet<GovernanceVote>> =
609-
futures::stream::iter(proposals_ids)
610-
.filter_map(|proposal_id| async move {
611-
let votes = rpc::query_proposal_votes(client, proposal_id)
612-
.await
613-
.ok()?;
608+
let votes = futures::stream::iter(proposals_ids)
609+
.filter_map(|proposal_id| async move {
610+
let votes =
611+
rpc::query_proposal_votes(client, proposal_id).await.ok()?;
612+
613+
let votes = votes
614+
.into_iter()
615+
.map(|vote| GovernanceVote {
616+
proposal_id,
617+
vote: ProposalVoteKind::from(vote.data),
618+
address: Id::from(vote.delegator),
619+
})
620+
.collect::<HashSet<_>>();
621+
622+
Some(votes)
623+
})
624+
.map(futures::future::ready)
625+
.buffer_unordered(32)
626+
.collect::<Vec<_>>()
627+
.await;
614628

615-
let votes = votes
616-
.into_iter()
617-
.map(|vote| GovernanceVote {
618-
proposal_id,
619-
vote: ProposalVoteKind::from(vote.data),
620-
address: Id::from(vote.delegator),
621-
})
622-
.collect::<HashSet<_>>();
629+
let mut voter_count: HashMap<(Id, u64), u64> = HashMap::new();
630+
for vote in votes.iter().flatten() {
631+
*voter_count
632+
.entry((vote.address.clone(), vote.proposal_id))
633+
.or_insert(0) += 1;
634+
}
623635

624-
Some(votes)
636+
let mut seen_voters = HashSet::new();
637+
anyhow::Ok(
638+
votes
639+
.iter()
640+
.flatten().filter(|&vote| {
641+
seen_voters.insert((vote.address.clone(), vote.proposal_id))
642+
}).cloned()
643+
.map(|mut vote| {
644+
if let Some(count) =
645+
voter_count.get(&(vote.address.clone(), vote.proposal_id))
646+
{
647+
if *count > 1_u64 {
648+
vote.vote = ProposalVoteKind::Unknown;
649+
}
650+
vote
651+
} else {
652+
vote
653+
}
625654
})
626-
.map(futures::future::ready)
627-
.buffer_unordered(32)
628-
.collect::<Vec<_>>()
629-
.await;
630-
631-
anyhow::Ok(votes.iter().flatten().cloned().collect())
655+
.collect(),
656+
)
632657
}
633658

634659
pub async fn get_validator_set_at_epoch(
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-- This file should undo anything in `up.sql`
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- Your SQL goes here
2+
ALTER TYPE VOTE_KIND ADD VALUE 'unknown';

orm/src/governance_votes.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub enum GovernanceVoteKindDb {
1010
Nay,
1111
Yay,
1212
Abstain,
13+
Unknown,
1314
}
1415

1516
impl From<ProposalVoteKind> for GovernanceVoteKindDb {
@@ -18,6 +19,7 @@ impl From<ProposalVoteKind> for GovernanceVoteKindDb {
1819
ProposalVoteKind::Nay => Self::Nay,
1920
ProposalVoteKind::Yay => Self::Yay,
2021
ProposalVoteKind::Abstain => Self::Abstain,
22+
ProposalVoteKind::Unknown => Self::Unknown,
2123
}
2224
}
2325
}

shared/src/vote.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub enum ProposalVoteKind {
99
Nay,
1010
Yay,
1111
Abstain,
12+
Unknown,
1213
}
1314

1415
impl From<ProposalVote> for ProposalVoteKind {

webserver/src/response/governance.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ pub enum VoteType {
5858
Yay,
5959
Nay,
6060
Abstain,
61+
Unknown,
6162
}
6263

6364
#[derive(Clone, Debug, Serialize, Deserialize)]
@@ -219,6 +220,7 @@ impl From<GovernanceProposalVoteDb> for ProposalVote {
219220
GovernanceVoteKindDb::Nay => VoteType::Nay,
220221
GovernanceVoteKindDb::Yay => VoteType::Yay,
221222
GovernanceVoteKindDb::Abstain => VoteType::Abstain,
223+
GovernanceVoteKindDb::Unknown => VoteType::Unknown,
222224
},
223225
voter_address: value.voter_address,
224226
}

0 commit comments

Comments
 (0)