Skip to content

Commit

Permalink
fix user votes query (#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
dusan-maksimovic authored Nov 20, 2024
1 parent babefa9 commit d0cc219
Show file tree
Hide file tree
Showing 6 changed files with 387 additions and 17 deletions.
2 changes: 2 additions & 0 deletions .changelog/unreleased/bug-fixes/174-query-user-votes-fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Fix user votes query to take into account only active round validators and their power ratios.
([\#174](https://github.com/informalsystems/hydro/pull/174))
4 changes: 2 additions & 2 deletions artifacts/checksums.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
79d6844dcd3b36fe6a8c2fd28ea3351fa12be1321f002eb32656b5aba4c16802 hydro.wasm
6317aa70358b9eeca68e9ee2a9a502e18312ee47e9ad7126c6cf2bc1858458c5 tribute.wasm
6f1f80739d9deaba86aba39961841a73b6fb1ae2b52046eff6f9b638777d9b21 hydro.wasm
eeb3108ad732ca312cf4c5faad508f0c41977a2174bd93c8d06d783012287459 tribute.wasm
Binary file modified artifacts/hydro.wasm
Binary file not shown.
Binary file modified artifacts/tribute.wasm
Binary file not shown.
34 changes: 24 additions & 10 deletions contracts/hydro/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1729,7 +1729,8 @@ pub fn query_user_voting_power(
// It goes through all user votes per lock_id and groups them by the
// proposal ID. The returned result will contain one VoteWithPower per
// each proposal ID, with the total power summed up from all lock IDs
// used to vote for that proposal.
// used to vote for that proposal. The votes that are referring to the
// validators that later dropped out from the top N will be filtered out.
pub fn query_user_votes(
deps: Deps<NeutronQuery>,
round_id: u64,
Expand All @@ -1739,22 +1740,35 @@ pub fn query_user_votes(
let user_address = deps.api.addr_validate(&user_address)?;
let mut voted_proposals_power_sum: HashMap<u64, Decimal> = HashMap::new();

VOTE_MAP
let votes = VOTE_MAP
.prefix(((round_id, tranche_id), user_address.clone()))
.range(deps.storage, None, None, Order::Ascending)
.filter_map(|vote| match vote {
Err(_) => None,
Ok(vote) => Some(vote.1),
})
.for_each(|vote| {
let current_power = match voted_proposals_power_sum.get(&vote.prop_id) {
None => Decimal::zero(),
Some(current_power) => *current_power,
};
.collect::<Vec<Vote>>();

for vote in votes {
let vote_validator = vote.time_weighted_shares.0;
// If the validator was active in the given round, we will get its power ratio.
// If it wasn't we will get 0, which means we should filter out this vote.
let val_power_ratio =
get_validator_power_ratio_for_round(deps.storage, round_id, vote_validator)?;

let vote_power = vote.time_weighted_shares.1.checked_mul(val_power_ratio)?;
if vote_power == Decimal::zero() {
continue;
}

let current_power = match voted_proposals_power_sum.get(&vote.prop_id) {
None => Decimal::zero(),
Some(current_power) => *current_power,
};

let new_power = current_power + vote.time_weighted_shares.1;
voted_proposals_power_sum.insert(vote.prop_id, new_power);
});
let new_power = current_power.checked_add(vote_power)?;
voted_proposals_power_sum.insert(vote.prop_id, new_power);
}

if voted_proposals_power_sum.is_empty() {
return Err(StdError::generic_err(
Expand Down
Loading

0 comments on commit d0cc219

Please sign in to comment.