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

Sanity checks on initialization and prevention of claiming money if reward vec not fully initialized #10

Merged
merged 22 commits into from
May 28, 2021

Conversation

girazoki
Copy link
Contributor

@girazoki girazoki commented May 20, 2021

This PR adds:

  • some more sanity checks for the initialization
  • not let users claim money if reward vec is not fully initialized.
  • Correct arithmetic by comparing how much should an account have vested vs how much it already vested, and pay the difference.
  • Add more tests for the floating point arithmetic problem
  • Removal of free claim payment.

Closes #9

…iming if reward vec is not initialized and make some associated types constants
@girazoki girazoki changed the title Sanity checks on initialization (we dont go over funds), dont let cla… Sanity checks on initialization and prevention of claiming money if reward vec not fully initialized May 20, 2021
@girazoki girazoki marked this pull request as ready for review May 21, 2021 10:32
src/lib.rs Outdated
&PALLET_ID.into_account(),
&payee,
payable_amount,
KeepAlive,
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe KeepAlive will not allow the transfer to go through if it would take the account below the existential deposit. In this case, the ideal scenario is that every single reward token will be claimed and paid out. So if we require KeepAlive the last claim will not succeed because it would have killed the pallet's pot account.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see, I missread it then. Will change it

src/lib.rs Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated

let pay_period_as_balance: BalanceOf<T> = payable_period
.saturated_into::<u128>()
.try_into()
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure I understand the need for this conversation.
To know the should_have_claimed, we need:
payable_period (number of blocks) / vesting_period (number of blocks) * (info.total_reward (balance) - first_paid (balance))

Copy link
Contributor Author

@girazoki girazoki May 25, 2021

Choose a reason for hiding this comment

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

The main reason is that I need all these parameters to have the same type to do the arithmetic computation. And I cannot divide, then make the conversion, then multiply (the division would already be truncated). I might be able to do this in a more elegant way, let me take a closer look, but this was the most straight-forward way I found

Copy link
Contributor

@notlesh notlesh May 25, 2021

Choose a reason for hiding this comment

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

I agree that converting a number of blocks to a balance is a bit odd. My inclination would be to convert everything to an integer (e.g. u128) when they need a common type, but that might be my C++ background talking :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried minimizing conversions but I can do that too definitely

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I ended up using relay_chain::BlockNumber from cumulus_primitives_core. I think this is what makes most sense as we are relying on this type when we count vested blocks. This type is a u32 and convertible automatically to Balance thanks to the atLeast32BitsUnsigned trait. Let me know if this looks better


// If the period is bigger than whats missing to pay, then return whats missing to pay
let payable_amount = if should_have_claimed >= info.total_reward {
info.total_reward.saturating_sub(info.claimed_reward)
Copy link
Contributor

Choose a reason for hiding this comment

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

This case is supposed to be when the entire amount is vested but we haven't claimed it all, right? Is this going to be slightly off because we subtract first_paid above?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, I need to substract first_paid from the comparison. Good catch! I will also add a test for that ( I have one already but I am gonna make it more precise)

let payable_amount = if should_have_claimed >= info.total_reward {
info.total_reward.saturating_sub(info.claimed_reward)
} else {
should_have_claimed + first_paid - info.claimed_reward
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we add back in first_paid here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because claimed_reward also contains first_paid.

Copy link
Contributor

@JoshOrndorff JoshOrndorff left a comment

Choose a reason for hiding this comment

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

I think the function signatures are fine.

src/lib.rs Outdated
Comment on lines 119 to 120
/// The total vesting period. Ideally this should be less than the lease period to ensure
/// there is no overlap between contributors from two different auctions
Copy link
Contributor

Choose a reason for hiding this comment

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

Outdated comment. There is no notion of different auctions

src/lib.rs Outdated
ensure!(
info.free_claim_done == false,
Error::<T>::FirstClaimAlreadyDone
initialized == true,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
initialized == true,
initialized,

@girazoki girazoki merged commit 9edea5b into main May 28, 2021
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.

payable_per_block suffers from integer division truncation
4 participants