Skip to content

Commit

Permalink
Move state to state.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
p-offtermatt committed Sep 4, 2024
1 parent 773ce4c commit e177445
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
1 change: 1 addition & 0 deletions contracts/hydro/src/lsm_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ pub fn get_round_validators(storage: &dyn Storage, round_id: u64) -> Vec<String>
VALIDATORS_INFO
.prefix(round_id)
.range(storage, None, None, Order::Ascending)
.filter(|val_res| val_res.is_ok())
.map(|val_res| {
let val = val_res.unwrap();
val.0
Expand Down
17 changes: 4 additions & 13 deletions contracts/hydro/src/score_keeper.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
use cosmwasm_std::{Decimal, StdError, StdResult, Storage};
use cw_storage_plus::Map;

use crate::lsm_integration::get_validator_power_ratio_for_round;

// The score keeper is a module that keeps track of amounts of individual validator shares, and power ratios (i.e. how many
// tokens a share of a validator represents). It stores the shares and power ratios for each validator in separate maps,
// and keeps those up-to-date with the total power (computed by multiplying the individual shares with the power ratio).
// The total is updated when either the shares or the power ratio of a validator is updated.

// SCALED_PROPOSAL_SHARES_MAP: key(proposal_id, validator_address) -> number_of_shares
const SCALED_PROPOSAL_SHARES_MAP: Map<(u64, String), Decimal> =
Map::new("scaled_proposal_power_shares");

// PROPOSAL_TOTAL_MAP: key(proposal_id) -> total_power
const PROPOSAL_TOTAL_MAP: Map<u64, Decimal> = Map::new("proposal_power_total");
use crate::{
lsm_integration::get_validator_power_ratio_for_round,
state::{PROPOSAL_TOTAL_MAP, SCALED_PROPOSAL_SHARES_MAP},
};

pub fn get_total_power_for_proposal(storage: &dyn Storage, prop_id: u64) -> StdResult<Decimal> {
Ok(PROPOSAL_TOTAL_MAP
Expand Down
13 changes: 13 additions & 0 deletions contracts/hydro/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,19 @@ pub const VALIDATORS_INFO: Map<(u64, String), ValidatorInfo> = Map::new("validat
pub const SCALED_ROUND_POWER_SHARES_MAP: Map<(u64, String), Decimal> =
Map::new("scaled_round_power_shares");

// The following two store fields are supposed to be kept in sync,
// i.e. whenever the shares of a proposal (or the power ratio of a validator)
// get updated, the total power of the proposal should be updated as well.
// For each proposal and validator, it stores the time-scaled number of shares of that validator
// that voted for the proposal.
// SCALED_PROPOSAL_SHARES_MAP: key(proposal_id, validator_address) -> number_of_shares
pub const SCALED_PROPOSAL_SHARES_MAP: Map<(u64, String), Decimal> =
Map::new("scaled_proposal_power_shares");

// Stores the total power for each proposal.
// PROPOSAL_TOTAL_MAP: key(proposal_id) -> total_power
pub const PROPOSAL_TOTAL_MAP: Map<u64, Decimal> = Map::new("proposal_power_total");

#[cw_serde]
#[derive(Default)]
pub struct ValidatorInfo {
Expand Down

0 comments on commit e177445

Please sign in to comment.