Missing highestVotingPower Update in argmaxBlockByStake Resulting in Incorrect Block Selection
The argmaxBlockByStake function fails to update highestVotingPower when a new block with higher voting power is found, so it may return a block that does not have the highest cumulative voting power. This could lead to incorrect block selection and downstream inconsistencies.
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2024-06-allora-judging/issues/44
Found by
Yuriisereda
Summary
The argmaxBlockByStake function is used to identify the block height with the highest cumulative voting power based on the stakes of voting reputers. However, the calculation for highestVotingPower is flawed. The function updates blockOfMaxPower when finding a new block with higher voting power but fails to update highestVotingPower. This results in incorrect block selection.
Vulnerability Detail
The following code snippet from argmaxBlockByStake illustrates the issue. Lines 13 and 14 properly calculate the voting power blockVotingPower for each block. However, when a new highest voting power is found, only blockOfMaxPower is updated while highestVotingPower is not, leading to erroneous results.
func (ap *AppChain) argmaxBlockByStake(
blockToReputer *map[int64][]string,
stakesPerReputer map[string]cosmossdk_io_math.Int,
) int64 {
// Find the current block height with the highest voting power
firstIter := true
highestVotingPower := cosmossdk_io_math.ZeroInt()
blockOfMaxPower := int64(-1)
for block, reputersWhoVotedForBlock := range *blockToReputer {
// Calc voting power of this candidate block by total voting reputer stake
blockVotingPower := cosmossdk_io_math.ZeroInt()
for _, reputerAddr := range reputersWhoVotedForBlock {
blockVotingPower = blockVotingPower.Add(stakesPerReputer[reputerAddr])
}
// Decide if voting power exceeds that of current front-runner
if firstIter || blockVotingPower.GT(highestVotingPower) {
@> blockOfMaxPower = block // Correctly updates the block
@> // Missing highestVotingPower = blockVotingPower
}
firstIter = false
}
return blockOfMaxPower
}
Impact
The function will incorrectly identify the block with the greatest cumulative voting power. This could have downstream effects, such as incorrect block selection, mishandling of stakes, and potential inconsistencies in subsequent processing or decision-making based on these results.
Code Snippet
Tool used
Manual Review
Recommendation
To resolve this issue, update highestVotingPower whenever blockOfMaxPower is updated:
func (ap *AppChain) argmaxBlockByStake(
blockToReputer *map[int64][]string,
stakesPerReputer map[string]cosmossdk_io_math.Int,
) int64 {
firstIter := true
highestVotingPower := cosmossdk_io_math.ZeroInt()
blockOfMaxPower := int64(-1)
for block, reputersWhoVotedForBlock := range *blockToReputer {
blockVotingPower := cosmossdk_io_math.ZeroInt()
for _, reputerAddr := range reputersWhoVotedForBlock {
if stake, exists := stakesPerReputer[reputerAddr]; exists { // Ensure reputer has a valid stake
blockVotingPower = blockVotingPower.Add(stake)
} else {
ap.Logger.Warn().Str("reputerAddr", reputerAddr).Msg("Reputer address not found in stake map")
}
}
if firstIter || blockVotingPower.GT(highestVotingPower) {
+ highestVotingPower = blockVotingPower // Update the highest voting power
blockOfMaxPower = block // Correctly updates the block
}
firstIter = false
}
// Log warning if no block was found with positive voting power
if highestVotingPower.IsZero() {
ap.Logger.Warn().Msg("No block found with positive voting power.")
}
return blockOfMaxPower
}
Discussion
sherlock-admin2
1 comment(s) were left on this issue during the judging contest.
0xmystery commented:
Missing
highestVotingPowerupdate
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/allora-network/allora-inference-base/pull/141
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The variable highestVotingPower is not updated when a new block with greater voting power is found, so subsequent comparisons use the initial zero value.
Impact
The function will incorrectly identify the block with the greatest cumulative voting power, potentially causing incorrect block selection and inconsistencies in later processing.
What the attacker needed
None
The fix
The issue was fixed by updating highestVotingPower whenever blockOfMaxPower is updated.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
This code is supposed to pick the block that has the most voting power behind it. But it forgets to remember the new highest voting power after finding a better block, so it can pick the wrong block.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.