Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: account for steps overflow #117

Merged
merged 1 commit into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion contract/src/score/account_score.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl AccountScore {
fn update_today(&mut self, chain: Chain) -> Vec<Score> {
for (score, day) in chain {
let day_index: usize = day.0.try_into().unwrap();
self.scores[day_index] += score;
self.scores[day_index] = self.scores[day_index].checked_add(score).unwrap_or(u16::MAX);
self.scores_history[day_index] = self.scores_history[day_index].checked_add(score).unwrap_or(u16::MAX);
}
vec![]
Expand Down
21 changes: 21 additions & 0 deletions contract/src/score/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,3 +413,24 @@ fn test_steps_history() {

check_score_interest(&ctx, 0);
}

#[test]
fn record_max_score() {
const ALICE_JAR: JarId = 0;

set_test_log_events(false);

let mut ctx = TestBuilder::new()
.product(SCORE_PRODUCT, [APY(0), TermDays(10), ScoreCap(20_000)])
.jar(ALICE_JAR, JarField::Timezone(Timezone::hour_shift(4)))
.build();

ctx.record_score(UTC(0), 25000, alice());
ctx.record_score(UTC(0), 25000, alice());
ctx.record_score(UTC(0), 25000, alice());
ctx.record_score(UTC(0), 25000, alice());

ctx.set_block_timestamp_in_days(1);

assert_eq!(ctx.contract().get_score_interest(alice()).unwrap().0, 65535);
}
Binary file modified res/sweat_jar.wasm
Binary file not shown.