Skip to content

fix(tholos-v2): use checked arithmetic in register() - #153

Merged
collinsezedike merged 7 commits into
drydocs:mainfrom
ZacLou:fix/checked-arithmetic-register-131
Sep 4, 2026
Merged

fix(tholos-v2): use checked arithmetic in register()#153
collinsezedike merged 7 commits into
drydocs:mainfrom
ZacLou:fix/checked-arithmetic-register-131

Conversation

@ZacLou

@ZacLou ZacLou commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Closes #131

Replaces the unchecked previous_amount + amount and �ligible_total - previous_amount + new_amount calculations in
egister() with checked_add/checked_sub, returning the existing Error::SettlementArithmeticOverflow on overflow.

Also adds two unit tests that seed storage near i128::MAX to confirm both error paths are reachable.

  • [x]
    egister() arithmetic is now checked
  • Overflow tests added
  • No behavioral change for all in-bounds inputs

Replaces unchecked previous_amount + amount and �ligible_total - previous_amount + new_amount with checked_add/checked_sub, returning Error::SettlementArithmeticOverflow on overflow.

Closes drydocs#131
Comment thread contracts/tholos-v2/src/test.rs Outdated
assert_eq!(result, Err(Ok(Error::CommitmentMismatch)));
}


Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a double blank line above this function. cargo fmt --check will fail CI's formatting gate on this. Please run cargo fmt before pushing.

Comment thread contracts/tholos-v2/src/test.rs Outdated
.get(&DataKey::Resolution(id))
.unwrap();
resolution.eligible_total = i128::MAX;
env.storage().persistent().set(&DataKey::Resolution(id), &resolution);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is unwrapped and exceeds rustfmt's line width, another cargo fmt --check failure. Run cargo fmt to reformat it.

DEFAULT_MAX_POSITION,
DEFAULT_MAX_TOTAL_WEIGHT,
);
assert_eq!(result, Ok(Ok(())));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing trailing newline at end of file, also flagged by cargo fmt --check.

ZacLou and others added 3 commits September 3, 2026 08:58
initialize() rejects max_position/max_total_weight above
MAX_SETTLEMENT_TOTAL_WEIGHT with InvalidMaxTotalWeight, so the two
register() overflow tests passed i128::MAX as caps and died at init
instead of exercising the checked arithmetic.

Use MAX_SETTLEMENT_TOTAL_WEIGHT as the caps and fund the voter at the
min-resolution-bond level so register() passes the bond gate and the
overflow paths in checked_add/checked_sub are actually reached.
Also drop the extra blank line flagged by cargo fmt --check.
- Run cargo fmt (double blank line, line width, trailing newline)
- Fix overflow test setup: use MAX_SETTLEMENT_TOTAL_WEIGHT for init params
  (i128::MAX is rejected by initialize), mint enough for voter bond,
  and register with DEFAULT_BOND so the overflow path is actually hit
@ZacLou

ZacLou commented Sep 3, 2026

Copy link
Copy Markdown
Contributor Author

Pushed fixes for the review feedback:

  • cargo fmt --all formatting issues resolved
  • Overflow tests now use MAX_SETTLEMENT_TOTAL_WEIGHT for initialize params instead of i128::MAX (which initialize rejects)
  • Eligible-total test mints enough for the voter bond and registers with DEFAULT_BOND so the arithmetic overflow path is actually exercised

cargo test --package tholos-v2 passes (109 tests). cargo fmt --all -- --check and cargo clippy --package tholos-v2 --lib -- -D warnings are clean.

@ZacLou

ZacLou commented Sep 3, 2026

Copy link
Copy Markdown
Contributor Author

Hi @collinsezedike, thanks for the review. I ran cargo fmt on the branch and cargo fmt --check now passes locally. The trailing newline, line width, and blank-line issues should be resolved in the latest commits. Please let me know if you see anything else.

@collinsezedike collinsezedike left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ZacLou thank you for the contribution. This looks good, there is nothing to flag.

@collinsezedike
collinsezedike merged commit 6a08ead into drydocs:main Sep 4, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Chore] register's position/weight arithmetic is unchecked in tholos-v2

3 participants