Skip to content

Commit 7e1e5cc

Browse files
authored
Merge pull request #248 from anoma/fix-vote-inserting
fix: duplicate votes for same voter
2 parents 73723a9 + 32fb47f commit 7e1e5cc

File tree

8 files changed

+59
-35
lines changed

8 files changed

+59
-35
lines changed

chain/src/services/namada.rs

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -605,30 +605,57 @@ 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()
641+
.filter(|&vote| {
642+
seen_voters.insert((vote.address.clone(), vote.proposal_id))
625643
})
626-
.map(futures::future::ready)
627-
.buffer_unordered(32)
628-
.collect::<Vec<_>>()
629-
.await;
630-
631-
anyhow::Ok(votes.iter().flatten().cloned().collect())
644+
.cloned()
645+
.map(|mut vote| {
646+
if let Some(count) =
647+
voter_count.get(&(vote.address.clone(), vote.proposal_id))
648+
{
649+
if *count > 1_u64 {
650+
vote.vote = ProposalVoteKind::Unknown;
651+
}
652+
vote
653+
} else {
654+
vote
655+
}
656+
})
657+
.collect(),
658+
)
632659
}
633660

634661
pub async fn get_validator_set_at_epoch(
Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,2 @@
11
-- This file should undo anything in `up.sql`
2-
3-
-- Step 1: Rename the existing enum type
4-
ALTER TYPE VALIDATOR_STATE RENAME TO VALIDATOR_STATE_OLD;
5-
6-
-- Step 2: Create the new enum type without the added values
7-
CREATE TYPE VALIDATOR_STATE AS ENUM ('consensus', 'inactive', 'jailed', 'below_capacity', 'below_threshold', 'unknown');
8-
9-
-- Step 3: Update all columns to use the new enum type
10-
ALTER TABLE validators ALTER COLUMN state TYPE VALIDATOR_STATE
11-
USING state::text::VALIDATOR_STATE;
12-
13-
-- Step 4: Drop the old enum type
14-
DROP TYPE VALIDATOR_STATE_OLD;
2+
SELECT 1;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- This file should undo anything in `up.sql`
2+
SELECT 1;
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 {

swagger.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -965,7 +965,7 @@ components:
965965
type: string
966966
vote:
967967
type: string
968-
enum: [yay, nay, abstain]
968+
enum: [yay, nay, abstain, unknown]
969969
voterAddress:
970970
type: string
971971
Reward:

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)