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

Rewards sanity check #641

Merged
merged 3 commits into from
Dec 19, 2023
Merged

Rewards sanity check #641

merged 3 commits into from
Dec 19, 2023

Conversation

cyberphysic4l
Copy link
Contributor

Adds a sanity check that token supply bits count + poolCoefficientExponent is less that 64 to fit in the int64 range during rewards calculations.

Copy link
Contributor

@PhilippGackstatter PhilippGackstatter left a comment

Choose a reason for hiding this comment

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

Small improvement to use integers for the comparison rather than floats and print the actual values. We do this in the congestion control sanity check and it was already quite helpful when I set custom parameters..

Also would you mind adding another sanity check to ensure ValidationBlocksPerSlot <= 32? We currently have that assumption but it's not checked anywhere afaik, so in the protocol params would be good.

Edit: I think it's <= 32 because ValidationBlocksPerSlot equals the number of subslots and a uint32 can track 32 bits (=subslots) so it's fine to be equal to 32.

Comment on lines 354 to 356
if math.Log2(float64(protocolParams.TokenSupply()))+float64(protocolParams.RewardsParameters().PoolCoefficientExponent) > 63 {
panic("Token supply bits count + poolCoefficientExponent must be less than or equal to 63")
}
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
if math.Log2(float64(protocolParams.TokenSupply()))+float64(protocolParams.RewardsParameters().PoolCoefficientExponent) > 63 {
panic("Token supply bits count + poolCoefficientExponent must be less than or equal to 63")
}
tokenSupplyBitsCount := uint8(math.Log2(float64(protocolParams.TokenSupply())))
if tokenSupplyBitsCount + protocolParams.RewardsParameters().PoolCoefficientExponent > 63 {
message := fmt.Sprintf("Token supply bits count (%d) + PoolCoefficientExponent (%d) must be less than or equal to 63\n", tokenSupplyBitsCount, protocolParams.RewardsParameters().PoolCoefficientExponent)
panic(message)
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe @oliviasaa can comment on whether it should be > 63 or >= 63. I'm not sure right now.

Also I guess if we truncate the float result to integer it would have to be uint8(log2(tokenSupply)) + 1 in case there is a decimal place?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you use uint8 in this way, then you also need to check for overflows incase someone were to set PoolCoefficentExponent = 255 for example, then the sanity check would pass but this would obviously not be a sane value.

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 am actually also sceptical about one of the other assumptions in the comments/TIP, which is that the target reward is at most 42 bits. Where does this assumption come from and does it also need to be sanity checked?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Regarding the >63 or >=63 point, I actually think the check can be >64 if, as you suggested, we use the actual bit count used by the token supply which is given by uint8(log2(tokenSupply))+1. So the pool stake takes up tokenSupplyBits, and that gets shifted left by poolCoefficientExponent, so that can be up to 64 bits without issue. Still happy to wait for @oliviasaa to check though.

Copy link
Contributor

Choose a reason for hiding this comment

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

<64 :)

Copy link
Contributor

Choose a reason for hiding this comment

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

and I agree with Andrew that using floats for the tests is simpler, because of a potential overflowing

Copy link
Contributor

@oliviasaa oliviasaa Dec 14, 2023

Choose a reason for hiding this comment

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

that the target reward is at most 42 bits

this was in the initial calculations, which used the iota supply, but you can just check if Computed Initial Rewards uses this amount of bits

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this was in the initial calculations, which used the iota supply, but you can just check if Computed Initial Rewards uses this amount of bits

@oliviasaa are you saying that the reference to 42 bits is outdated and should be updated to something else? Do we need another check that Computed Initial Rewards uses some amount of bits? The tip should be updated and so should the comment in the code to make this understandable either way.

Copy link
Contributor

@PhilippGackstatter PhilippGackstatter left a comment

Choose a reason for hiding this comment

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

Looks good to me, thanks for the Validation Blocks check!

@PhilippGackstatter
Copy link
Contributor

Let's get this merged to fix iotaledger/iota-core#614 and address potential other checks in future PRs.

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.

Panic on rewards calculation overflow
4 participants