Skip to content

Potential bug in PrivateEpochInfoTrait::calculate_next_epoch_starting_block #16

@hudem1

Description

@hudem1

Hi !

I was going through the staking & attestation contracts, and I believe I might have found a potential bug.

In the function calculate_next_epoch_starting_block here in the statement:

let blocks_passed = current_block - *self.starting_block;

I believe if the update method for EpochInfo is called twice in the same epoch, starting_block will be equal to the starting block of the next epoch (after the 1st call) and will therefore be greater than current_block (in the 2nd call). Therefore, I believe this statement would panic.

If I'm correct, then a fix could be something like this:

let mut blocks_passed = current_block - *self.last_starting_block_before_update;
if *self.starting_block < current_block {
   blocks_passed = current_block - *self.starting_block;
}

And this would necessitate setting last_starting_block_before_update to starting_block in the new method. Actually, the if statement might not even be needed because the modulo operation % on length is used after anyway, so I believe always setting blocks_passed to last_starting_block_before_update would be fine!

(If i may, I would almost also add a check in new method to make sure that method parameter starting_block <= get_block_number() to be an additional safety)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions