Rewrite logic to avoid unsigned int overflow

- When calculating the offset the calculation here is overflowing; since
  they are `uint64_t`s where overflow is well-defined that doesn't
  actually break anything (the overflowed value overflows in the other
  direction when added, and works as the intended subtraction), but it
  feels a bit icky.  Rearranging the logic avoids it.

- If given a small height (e.g. 3) the next payout height would come out
  as a large positive number (close to uint64_t max).  The change above
  avoids this as well.
This commit is contained in:
Jason Rhinelander 2022-06-13 16:52:36 -03:00
parent 946b182384
commit 598c174c9c
No known key found for this signature in database
GPG Key ID: C4992CE7A88D4262
1 changed files with 5 additions and 4 deletions

View File

@ -150,10 +150,11 @@ uint64_t account_public_address::modulus(uint64_t interval) const
uint64_t account_public_address::next_payout_height(uint64_t current_height, uint64_t interval) const
{
uint64_t next_payout_height = current_height + (modulus(interval) - current_height % interval);
if (next_payout_height <= current_height)
next_payout_height += interval;
return next_payout_height;
auto pay_offset = modulus(interval);
auto curr_offset = current_height % interval;
if (pay_offset <= curr_offset)
pay_offset += interval;
return current_height + pay_offset - curr_offset;
}
}