Skip to content
Closed
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
15 changes: 13 additions & 2 deletions contracts/tholos-v2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,11 @@ pub enum Error {
/// Rejected outright rather than treated as a no-op, so this call can
/// never be read as altering an already-decided result.
RoundAlreadyDecided = 36,
/// A checked arithmetic operation in `register` would have overflowed
/// `i128`. Not expected to be reachable given `initialize`'s
/// `max_total_weight`/`max_position` bounds, but checked rather than
/// relying on wrapping in release builds.
RegistrationArithmeticOverflow = 37,
}

const DAY_IN_LEDGERS: u32 = 17280;
Expand Down Expand Up @@ -1251,11 +1256,17 @@ impl TholosV2 {
}
};

let new_amount = previous_amount + amount;
let new_amount = previous_amount
.checked_add(amount)
.ok_or(Error::RegistrationArithmeticOverflow)?;
if new_amount > assertion.policy.max_position {
return Err(Error::PositionExceedsMax);
}
let new_total = resolution.eligible_total - previous_amount + new_amount;
let new_total = resolution
.eligible_total
.checked_sub(previous_amount)
.and_then(|total| total.checked_add(new_amount))
.ok_or(Error::RegistrationArithmeticOverflow)?;
if new_total > assertion.policy.max_total_weight {
return Err(Error::EligibleTotalExceedsMax);
}
Expand Down
52 changes: 52 additions & 0 deletions contracts/tholos-v2/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1334,6 +1334,58 @@ fn test_register_exceeds_max_total_weight_fails() {
assert_eq!(result, Err(Ok(Error::EligibleTotalExceedsMax)));
}

#[test]
fn test_register_position_amount_overflow_returns_error() {
let f = Fixture::new();
let asserter = f.funded_address();
let disputer = f.funded_address();
let voter = f.funded_address();

let id = f.asserted(&asserter);
f.client.dispute(&disputer, &id);

let c = commitment(&f.env, 1);
f.client.register(&voter, &id, &DEFAULT_BOND, &c);

let mut position = f.client.get_position(&id, &voter);
position.amount = i128::MAX;

f.env.as_contract(&f.client.address, || {
f.env
.storage()
.persistent()
.set(&DataKey::Position(id, voter.clone()), &position);
});

let result = f.client.try_register(&voter, &id, &100, &c);
assert_eq!(result, Err(Ok(Error::RegistrationArithmeticOverflow)));
}

#[test]
fn test_register_eligible_total_overflow_returns_error() {
let f = Fixture::new();
let asserter = f.funded_address();
let disputer = f.funded_address();
let voter = f.funded_address();

let id = f.asserted(&asserter);
f.client.dispute(&disputer, &id);

let mut resolution = f.client.get_resolution(&id);
resolution.eligible_total = i128::MAX;

f.env.as_contract(&f.client.address, || {
f.env
.storage()
.persistent()
.set(&DataKey::Resolution(id), &resolution);
});

let c = commitment(&f.env, 1);
let result = f.client.try_register(&voter, &id, &DEFAULT_BOND, &c);
assert_eq!(result, Err(Ok(Error::RegistrationArithmeticOverflow)));
}

#[test]
fn test_register_after_deadline_fails() {
let f = Fixture::new();
Expand Down
Loading