Skip to content

Commit

Permalink
test: Adjust GenesisState.Validate() error checks
Browse files Browse the repository at this point in the history
  • Loading branch information
drklee3 committed May 10, 2024
1 parent 2e59c42 commit 9bdd6e5
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 86 deletions.
8 changes: 4 additions & 4 deletions x/precisebank/testutil/fractional_balances.go
Expand Up @@ -37,7 +37,7 @@ func GenerateEqualFractionalBalances(
// Random 0 < amt < CONVERSION_FACTOR
// POSITIVE and less than CONVERSION_FACTOR
// If it's 0, Validate() will error
amt := randRange(1, types.CONVERSION_FACTOR.Int64())
amt := randRange(1, types.ConversionFactor().Int64())
amtInt := sdkmath.NewInt(amt)

fb := types.NewFractionalBalance(addr, amtInt)
Expand All @@ -55,8 +55,8 @@ func GenerateEqualFractionalBalances(
// aka
// CONVERSION_FACTOR - (sum % CONVERSION_FACTOR) = lastAmt
addr := sdk.AccAddress{byte(count - 1)}.String()
amt := types.CONVERSION_FACTOR.
Sub(sum.Mod(types.CONVERSION_FACTOR))
amt := types.ConversionFactor().
Sub(sum.Mod(types.ConversionFactor()))

fb := types.NewFractionalBalance(addr, amt)
require.NoError(t, fb.Validate())
Expand All @@ -68,7 +68,7 @@ func GenerateEqualFractionalBalances(
for _, fb := range fbs {
verificationSum = verificationSum.Add(fb.Amount)
}
require.True(t, verificationSum.Mod(types.CONVERSION_FACTOR).IsZero())
require.True(t, verificationSum.Mod(types.ConversionFactor()).IsZero())

// Also make sure no duplicate addresses
require.NoError(t, fbs.Validate())
Expand Down
8 changes: 4 additions & 4 deletions x/precisebank/types/genesis.go
Expand Up @@ -20,11 +20,11 @@ func (gs *GenesisState) Validate() error {

// Validate remainder, 0 <= remainder <= maxFractionalAmount
if gs.Remainder.IsNegative() {
return fmt.Errorf("negative remainder amount: %s", gs.Remainder)
return fmt.Errorf("negative remainder amount %s", gs.Remainder)
}

if gs.Remainder.GT(MaxFractionalAmount()) {
return fmt.Errorf("remainder exceeds max of %v: %v", MaxFractionalAmount(), gs.Remainder)
return fmt.Errorf("remainder %v exceeds max of %v", gs.Remainder, MaxFractionalAmount())
}

// Determine if sum(fractionalBalances) + remainder = whole integer value
Expand All @@ -34,9 +34,9 @@ func (gs *GenesisState) Validate() error {

if !total.Mod(ConversionFactor()).IsZero() {
return fmt.Errorf(
"sum of fractional balances + remainder is not a whole integer value: %v + %v == %v, but expected to end in 12 zeros",
sum, gs.Remainder,
"sum of fractional balances %v + remainder %v is not a whole integer value %v",
total,
sum, gs.Remainder,
)
}

Expand Down
133 changes: 55 additions & 78 deletions x/precisebank/types/genesis_test.go
@@ -1,7 +1,6 @@
package types_test

import (
"math/rand"
"testing"

sdkmath "cosmossdk.io/math"
Expand All @@ -10,86 +9,43 @@ import (
"github.com/stretchr/testify/require"
)

func generateEqualFractionalBalances(
t *testing.T,
count int,
) types.FractionalBalances {
t.Helper()

fbs := make(types.FractionalBalances, count)
sum := sdkmath.ZeroInt()

// Random amounts for count - 1 FractionalBalances
for i := 0; i < count-1; i++ {
addr := sdk.AccAddress{byte(i)}.String()

// Random 0 <= amt < CONVERSION_FACTOR
amt := rand.Int63n(types.ConversionFactor().Int64())
amtInt := sdkmath.NewInt(amt)

fb := types.NewFractionalBalance(addr, amtInt)
require.NoError(t, fb.Validate())

fbs[i] = fb

sum = sum.Add(amtInt)
}

// Last FractionalBalance must make sum of all balances equal to have 0
// fractional remainder. Effectively the amount needed to round up to the
// nearest integer amount to make this true.
// (sum + lastAmt) % CONVERSION_FACTOR = 0
// aka
// CONVERSION_FACTOR - (sum % CONVERSION_FACTOR) = lastAmt
addr := sdk.AccAddress{byte(count - 1)}.String()
amt := types.ConversionFactor().
Sub(sum.Mod(types.ConversionFactor()))

fb := types.NewFractionalBalance(addr, amt)
require.NoError(t, fb.Validate())

fbs[count-1] = fb

// Lets double check this before returning
verificationSum := sdkmath.ZeroInt()
for _, fb := range fbs {
verificationSum = verificationSum.Add(fb.Amount)
}
require.True(t, verificationSum.Mod(types.ConversionFactor()).IsZero())

// Also make sure no duplicate addresses
require.NoError(t, fbs.Validate())

return fbs
}

func TestGenesisStateValidate(t *testing.T) {
func TestGenesisStateValidate_Basic(t *testing.T) {
testCases := []struct {
name string
genesisState *types.GenesisState
expErr bool
wantErr string
}{
{
"default genesisState",
"valid - default genesisState",
types.DefaultGenesisState(),
false,
"",
},
{
"valid - empty balances, zero remainder",
&types.GenesisState{
Remainder: sdkmath.ZeroInt(),
},
false,
"",
},
{
"valid - nil balances",
types.NewGenesisState(nil, sdkmath.ZeroInt()),
false,
"",
},
{
"valid - max remainder amount",
types.NewGenesisState(
types.FractionalBalances{
types.NewFractionalBalance(sdk.AccAddress{1}.String(), sdkmath.NewInt(1)),
},
types.MaxFractionalAmount(),
),
"",
},
{
"invalid - empty genesisState (nil remainder)",
&types.GenesisState{},
true,
"nil remainder amount",
},
{
"valid - balances add up",
Expand All @@ -100,7 +56,7 @@ func TestGenesisStateValidate(t *testing.T) {
},
sdkmath.ZeroInt(),
),
true,
"invalid balances: duplicate address cosmos1qyfkm2y3",
},
{
"invalid - calls (single) FractionalBalance.Validate()",
Expand All @@ -111,7 +67,7 @@ func TestGenesisStateValidate(t *testing.T) {
},
sdkmath.ZeroInt(),
),
true,
"invalid balances: invalid fractional balance for cosmos1qgcgaq4k: non-positive amount -1",
},
{
"invalid - calls (slice) FractionalBalances.Validate()",
Expand All @@ -122,7 +78,7 @@ func TestGenesisStateValidate(t *testing.T) {
},
sdkmath.ZeroInt(),
),
true,
"invalid balances: duplicate address cosmos1qyfkm2y3",
},
{
"invalid - negative remainder",
Expand All @@ -133,17 +89,7 @@ func TestGenesisStateValidate(t *testing.T) {
},
sdkmath.NewInt(-1),
),
true,
},
{
"valid - max remainder amount",
types.NewGenesisState(
types.FractionalBalances{
types.NewFractionalBalance(sdk.AccAddress{1}.String(), sdkmath.NewInt(1)),
},
types.MaxFractionalAmount(),
),
false,
"negative remainder amount -1",
},
{
"invalid - too large remainder",
Expand All @@ -154,7 +100,7 @@ func TestGenesisStateValidate(t *testing.T) {
},
types.MaxFractionalAmount().AddRaw(1),
),
true,
"remainder 1000000000000 exceeds max of 999999999999",
},
}

Expand All @@ -163,10 +109,41 @@ func TestGenesisStateValidate(t *testing.T) {
t.Run(tc.name, func(tt *testing.T) {
err := tc.genesisState.Validate()

if tc.expErr {
require.Error(tt, err)
if tc.wantErr == "" {
require.NoError(tt, err)
} else {
require.Error(tt, err)
require.EqualError(tt, err, tc.wantErr)
}
})
}
}

func TestGenesisStateValidate_Total(t *testing.T) {
testCases := []struct {
name string
genesisStateFn func() *types.GenesisState
wantErr string
}{
{
"valid - empty balances, zero remainder",
func() *types.GenesisState {
return types.NewGenesisState(nil, sdkmath.ZeroInt())
},
"",
},
}

for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(tt *testing.T) {
err := tc.genesisStateFn().Validate()

if tc.wantErr == "" {
require.NoError(tt, err)
} else {
require.Error(tt, err)
require.EqualError(tt, err, tc.wantErr)
}
})
}
Expand Down

0 comments on commit 9bdd6e5

Please sign in to comment.