Closed sherlock-admin4 closed 3 months ago
package params_test
import (
"fmt"
"testing"
cosmosMath "cosmossdk.io/math"
"github.com/allora-network/allora-chain/app/params"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
)
func TestAlloIs1E18UAllo(t *testing.T) {
// Create amounts in both units
oneAllo := sdk.NewCoin(params.HumanCoinUnit, cosmosMath.NewInt(1))
oneUAllo := sdk.NewCoin(params.BaseCoinUnit, cosmosMath.NewInt(1))
// Convert 1 allo to uallo (should be 1e18 uallo)
oneAlloToUAllo := oneAllo.Amount.Mul(cosmosMath.NewIntWithDecimal(1, params.AlloraExponent))
// Check if the conversion is correct
fmt.Println("1 allo in uallo (expected):", cosmosMath.NewIntWithDecimal(1, params.AlloraExponent))
fmt.Println("1 allo in uallo (actual):", oneAlloToUAllo)
// Check if 1 uallo is considered equal to 1 allo (should be false)
fmt.Println("1 uallo == 1 allo:", oneUAllo.IsEqual(oneAllo))
require.False(t, oneUAllo.IsEqual(oneAllo))
}
Running tool: /usr/bin/go test -timeout 30s -run ^TestAlloIs1E18UAllo$ github.com/allora-network/allora-chain/app/params -v
=== RUN TestAlloIs1E18UAllo
1 allo in uallo (expected): 1000000000000000000
1 allo in uallo (actual): 1000000000000000000
1 uallo == 1 allo: false
--- PASS: TestAlloIs1E18UAllo (0.00s)
PASS
ok github.com/allora-network/allora-chain/app/params 0.025s
I can't reproduce this bug?
PoC is not working.
Minato7namikazi
Medium
Inconsistent Precision for
BaseCoinUnit
in ConfingVulnerability Detail
The issue lies within the
RegisterDenoms
function:In this line, the
math.LegacyNewDecWithPrec(1, AlloraExponent)
function is used to create aLegacyDec
object with a coefficient of 1 and a precision determined by the value ofAlloraExponent
. However, the way the precision is set might not match the intended behavior of theBaseCoinUnit
.Why It's a Problem
BaseCoinUnit
("uallo") is typically meant to represent the smallest, indivisible unit of the currency. Its precision should be set to0
to ensure that it represents whole numbers. Setting the precision toAlloraExponent
(which is likely 18) implies that theBaseCoinUnit
can have decimal places, which contradicts its fundamental purpose.BaseCoinUnit
has an incorrect precision, it can lead to errors in calculations, especially when converting between theBaseCoinUnit
and theHumanCoinUnit
("allo").Code Snippet
https://github.com/sherlock-audit/2024-06-allora/blob/main/allora-chain/app/params/config.go#L14
Tool used
Manual Review
Recommendation
Set Precision to 0 for
BaseCoinUnit
**To resolve this issue, you should change the precision argument in the
RegisterDenoms
function:By setting the precision to 0, you ensure that the
BaseCoinUnit
represents whole numbers, which is the correct behavior for a base unit of currency.PoC
Explanation
HumanCoinUnit
andBaseCoinUnit
with the incorrect precision forBaseCoinUnit
(set toAlloraExponent
, which is 18).oneAllo
: Represents 1 allo.oneUAllo
: Represents 1 uallo.oneAllo
touallo
by multiplying its amount by10^18
(which is the expected conversion factor).Expected Output
The output of this code will demonstrate the inconsistency:
As you can see, the conversion from allo to uallo is incorrect, and 1 uallo is mistakenly considered equal to 1 allo due to the incorrect precision setting.
After the Fix
If you change the
RegisterDenoms
function to set the precision ofBaseCoinUnit
to 0, as I suggested in the previous response, you'll get the correct output:This PoC clearly shows the bug caused by the incorrect precision and how the fix rectifies it.