Skip to content

Commit

Permalink
add gauge limit test
Browse files Browse the repository at this point in the history
  • Loading branch information
hard-nett committed Aug 11, 2024
1 parent 5e972e1 commit f66c819
Show file tree
Hide file tree
Showing 10 changed files with 206 additions and 26 deletions.
21 changes: 21 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ members = [
"contracts/test/*",
"contracts/voting/*",
"packages/*",
"scripts",
"ci/*",
]
resolver = "2"
Expand Down
10 changes: 6 additions & 4 deletions contracts/gauges/gauge/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,8 +427,8 @@ mod execute {
last: None,
reset_each: r,
next: env.block.time.plus_seconds(r).seconds(),
total: total_epochs,
}),
total_epoch: total_epochs,
};
let last_id: GaugeId = fetch_last_id(deps.storage)?;
GAUGES.save(deps.storage, last_id, &gauge)?;
Expand Down Expand Up @@ -817,15 +817,17 @@ mod execute {

msgs.extend(execute_messages.execute);


// increments epoch count
gauge.count = gauge.increment_gauge_count()?;

if gauge.will_reach_epoch_limit() {
msgs.push(CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: env.contract.address.to_string(),
msg: to_json_binary(&ExecuteMsg::StopGauge { gauge: gauge_id })?,
funds: vec![],
}))
}
// increments epoch count
gauge.count = gauge.increment_gauge_count()?;

let config = CONFIG.load(deps.storage)?;
let execute_msg = WasmMsg::Execute {
Expand Down Expand Up @@ -903,6 +905,7 @@ mod query {
is_stopped: gauge.is_stopped,
next_epoch: gauge.next_epoch,
reset: gauge.reset,
total_epochs: gauge.total_epoch,
}
}

Expand Down Expand Up @@ -1058,7 +1061,6 @@ pub fn migrate(deps: DepsMut, env: Env, msg: MigrateMsg) -> Result<Response, Con
last: gauge.reset.map(|r| r.last).unwrap_or_default(),
reset_each: reset_config.reset_epoch,
next: reset_config.next_reset,
total: None,
});
}
Ok(gauge)
Expand Down
2 changes: 2 additions & 0 deletions contracts/gauges/gauge/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ pub struct GaugeResponse {
pub adapter: String,
/// Frequency (in seconds) the gauge executes messages, typically something like 7*86400
pub epoch_size: u64,
/// Total epoch duration
pub total_epochs: Option<u64>,
/// Minimum percentage of votes needed by a given option to be in the selected set.
/// If unset, there is no minimum percentage, just the `max_options_selected` limit.
pub min_percent_selected: Option<Decimal>,
Expand Down
20 changes: 18 additions & 2 deletions contracts/gauges/gauge/src/multitest/gauge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ fn create_gauge() {
is_stopped: false,
next_epoch: suite.current_time() + 7 * 86400,
reset: None,
total_epochs: None
}
);
}
Expand Down Expand Up @@ -84,6 +85,7 @@ fn gauge_can_upgrade_from_self() {
is_stopped: false,
next_epoch: suite.current_time() + 7 * 86400,
reset: None,
total_epochs: None
}
);
}
Expand Down Expand Up @@ -123,6 +125,7 @@ fn gauge_migrate_with_next_epochs() {
is_stopped: false,
next_epoch: suite.current_time() + 7 * 86400,
reset: None,
total_epochs: None
}
);

Expand Down Expand Up @@ -156,6 +159,7 @@ fn gauge_migrate_with_next_epochs() {
is_stopped: false,
next_epoch: suite.current_time() + 14 * 86400,
reset: None,
total_epochs: None
}
);

Expand Down Expand Up @@ -502,7 +506,13 @@ fn execute_stopped_gauge() {

suite.next_block();
let gauge_config = suite
.instantiate_adapter_and_return_config(&[voter1, voter2], reward_to_distribute, None, None,None)
.instantiate_adapter_and_return_config(
&[voter1, voter2],
reward_to_distribute,
None,
None,
None,
)
.unwrap();
suite
.propose_update_proposal_module(voter1.to_string(), vec![gauge_config])
Expand Down Expand Up @@ -618,6 +628,7 @@ fn update_gauge() {
is_stopped: false,
next_epoch: suite.current_time() + 7 * 86400,
reset: None,
total_epochs: None
},
GaugeResponse {
id: 1,
Expand All @@ -630,6 +641,7 @@ fn update_gauge() {
is_stopped: false,
next_epoch: suite.current_time() + 7 * 86400,
reset: None,
total_epochs: None
}
]
);
Expand All @@ -646,8 +658,8 @@ fn update_gauge() {
&owner,
gauge_contract.clone(),
0,
epoch_limit,
new_epoch,
epoch_limit,
new_min_percent,
new_max_options,
new_max_available_percentage,
Expand All @@ -669,6 +681,7 @@ fn update_gauge() {
is_stopped: false,
next_epoch: suite.current_time() + 7 * 86400,
reset: None,
total_epochs: None
},
GaugeResponse {
id: 1,
Expand All @@ -681,6 +694,7 @@ fn update_gauge() {
is_stopped: false,
next_epoch: suite.current_time() + 7 * 86400,
reset: None,
total_epochs: None
}
]
);
Expand Down Expand Up @@ -714,6 +728,7 @@ fn update_gauge() {
is_stopped: false,
next_epoch: suite.current_time() + 7 * 86400,
reset: None,
total_epochs: None
},
GaugeResponse {
id: 1,
Expand All @@ -726,6 +741,7 @@ fn update_gauge() {
is_stopped: false,
next_epoch: suite.current_time() + 7 * 86400,
reset: None,
total_epochs: None
}
]
);
Expand Down
4 changes: 2 additions & 2 deletions contracts/gauges/gauge/src/multitest/reset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ fn basic_gauge_reset() {
let gauge_contract = proposal_modules[1].clone();
let gauge_id = 0;


// vote for one of the options in gauge
suite
.place_vote(
Expand Down Expand Up @@ -199,6 +198,7 @@ fn gauge_migrate_with_reset() {
is_stopped: false,
next_epoch: suite.current_time() + 7 * 86400,
reset: None,
total_epochs: None,
}
);

Expand Down Expand Up @@ -263,8 +263,8 @@ fn gauge_migrate_with_reset() {
last: None,
reset_each: RESET_EPOCH,
next: suite.current_time() + 100,
total: None,
}),
total_epochs: None,
}
);
}
Expand Down
2 changes: 1 addition & 1 deletion contracts/gauges/gauge/src/multitest/suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ impl Suite {
to_distribute: (u128, &str),
max_available_percentage: impl Into<Option<Decimal>>,
reset_epoch: impl Into<Option<u64>>,
epoch_limit: impl Into<Option<u64>>
epoch_limit: impl Into<Option<u64>>,
) -> AnyResult<Addr> {
let option = self.instantiate_adapter_and_return_config(
options,
Expand Down
18 changes: 9 additions & 9 deletions contracts/gauges/gauge/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ pub struct Gauge {
pub epoch: u64,
/// Epoch count.
pub count: Option<u64>,
/// total possible count for a gauge to run. will automatially disable itself when reaching this epoch count.
pub total_epoch: Option<u64>,
/// Minimum percentage of votes needed by a given option to be in the selected set
pub min_percent_selected: Option<Decimal>,
/// Maximum number of Options to make the selected set. Needed even with
Expand All @@ -78,8 +80,6 @@ pub struct Reset {
pub reset_each: u64,
/// next time we can reset
pub next: u64,
// total
pub total: Option<u64>,
}

impl Gauge {
Expand All @@ -91,13 +91,11 @@ impl Gauge {
.unwrap_or_default()
}
pub fn will_reach_epoch_limit(&self) -> bool {
self.reset.as_ref().map_or(false, |r| {
if let Some(total) = r.total {
total == self.count.unwrap_or_default() + 1
} else {
false
}
})
if let Some(total) = self.total_epoch {
total == self.count.unwrap_or_default()
} else {
false
}
}
pub fn increment_gauge_count(&self) -> StdResult<Option<u64>> {
Ok(self.count.map_or(Some(0), |o| Some(o + 1)))
Expand Down Expand Up @@ -471,6 +469,7 @@ mod tests {
last_executed_set: None,
reset: None,
count: Some(0),
total_epoch: None,
},
)
.unwrap();
Expand Down Expand Up @@ -599,6 +598,7 @@ mod tests {
last_executed_set: None,
reset: None,
count: Some(0),
total_epoch: None,
},
)
.unwrap();
Expand Down
Loading

0 comments on commit f66c819

Please sign in to comment.