Skip to content

Commit

Permalink
Add utils test
Browse files Browse the repository at this point in the history
  • Loading branch information
skosito committed Mar 29, 2024
1 parent e99b43b commit 7721694
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 4 deletions.
5 changes: 5 additions & 0 deletions testutil/keeper/mocks/observer/staking.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions x/observer/keeper/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (k Keeper) IsValidator(ctx sdk.Context, creator string) error {
return types.ErrNotValidator
}

if validator.Jailed == true || validator.IsBonded() == false {
if validator.Jailed || !validator.IsBonded() {
return types.ErrValidatorStatus
}
return nil
Expand Down Expand Up @@ -135,7 +135,6 @@ func (k Keeper) CheckObserverSelfDelegation(ctx sdk.Context, accAddress string)
return err
}
tokens := validator.TokensFromShares(delegation.Shares)

if tokens.LT(minDelegation) {
k.RemoveObserverFromSet(ctx, accAddress)
}
Expand Down
201 changes: 199 additions & 2 deletions x/observer/keeper/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/stretchr/testify/require"
"github.com/zeta-chain/zetacore/pkg/chains"
keepertest "github.com/zeta-chain/zetacore/testutil/keeper"
Expand Down Expand Up @@ -67,8 +68,8 @@ func TestKeeper_IsAuthorized(t *testing.T) {
ObserverList: []string{accAddressOfValidator.String()},
})
require.True(t, k.IsNonTombstonedObserver(ctx, accAddressOfValidator.String()))

})

t.Run("not authorized for tombstoned observer", func(t *testing.T) {
k, ctx, _, _ := keepertest.ObserverKeeper(t)

Expand All @@ -88,13 +89,15 @@ func TestKeeper_IsAuthorized(t *testing.T) {
})

accAddressOfValidator, err := types.GetAccAddressFromOperatorAddress(validator.OperatorAddress)
require.NoError(t, err)

k.SetObserverSet(ctx, types.ObserverSet{
ObserverList: []string{accAddressOfValidator.String()},
})

require.False(t, k.IsNonTombstonedObserver(ctx, accAddressOfValidator.String()))

})

t.Run("not authorized for non-validator observer", func(t *testing.T) {
k, ctx, _, _ := keepertest.ObserverKeeper(t)

Expand All @@ -114,11 +117,205 @@ func TestKeeper_IsAuthorized(t *testing.T) {
})

accAddressOfValidator, err := types.GetAccAddressFromOperatorAddress(validator.OperatorAddress)
require.NoError(t, err)

k.SetObserverSet(ctx, types.ObserverSet{
ObserverList: []string{accAddressOfValidator.String()},
})

require.False(t, k.IsNonTombstonedObserver(ctx, accAddressOfValidator.String()))
})
}

func TestKeeper_CheckObserverSelfDelegation(t *testing.T) {
t.Run("should error if accAddress invalid", func(t *testing.T) {
k, ctx, _, _ := keepertest.ObserverKeeper(t)

err := k.CheckObserverSelfDelegation(ctx, "invalid")
require.Error(t, err)
})

t.Run("should error if validator not found", func(t *testing.T) {
k, ctx, _, _ := keepertest.ObserverKeeper(t)

accAddress := sample.AccAddress()
err := k.CheckObserverSelfDelegation(ctx, accAddress)
require.Error(t, err)
})

t.Run("should error if delegation not found", func(t *testing.T) {
k, ctx, _, _ := keepertest.ObserverKeeper(t)

r := rand.New(rand.NewSource(9))
validator := sample.Validator(t, r)
k.GetStakingKeeper().SetValidator(ctx, validator)
accAddressOfValidator, err := types.GetAccAddressFromOperatorAddress(validator.OperatorAddress)
require.NoError(t, err)

err = k.CheckObserverSelfDelegation(ctx, accAddressOfValidator.String())
require.Error(t, err)
})

t.Run("should remove from observer list if tokens less than min del", func(t *testing.T) {
k, ctx, _, _ := keepertest.ObserverKeeper(t)

r := rand.New(rand.NewSource(9))
validator := sample.Validator(t, r)
validator.DelegatorShares = sdk.NewDec(100)
k.GetStakingKeeper().SetValidator(ctx, validator)
accAddressOfValidator, err := types.GetAccAddressFromOperatorAddress(validator.OperatorAddress)
require.NoError(t, err)

k.GetStakingKeeper().SetDelegation(ctx, stakingtypes.Delegation{
DelegatorAddress: accAddressOfValidator.String(),
ValidatorAddress: validator.GetOperator().String(),
Shares: sdk.NewDec(10),
})

k.SetObserverSet(ctx, types.ObserverSet{
ObserverList: []string{accAddressOfValidator.String()},
})
err = k.CheckObserverSelfDelegation(ctx, accAddressOfValidator.String())
require.NoError(t, err)

os, found := k.GetObserverSet(ctx)
require.True(t, found)
require.Empty(t, os.ObserverList)
})

t.Run("should not remove from observer list if tokens gte than min del", func(t *testing.T) {
k, ctx, _, _ := keepertest.ObserverKeeper(t)

r := rand.New(rand.NewSource(9))
validator := sample.Validator(t, r)

validator.DelegatorShares = sdk.NewDec(1)
validator.Tokens = sdk.NewInt(1)
k.GetStakingKeeper().SetValidator(ctx, validator)
accAddressOfValidator, err := types.GetAccAddressFromOperatorAddress(validator.OperatorAddress)
require.NoError(t, err)

minDelegation, err := types.GetMinObserverDelegationDec()
require.NoError(t, err)
k.GetStakingKeeper().SetDelegation(ctx, stakingtypes.Delegation{
DelegatorAddress: accAddressOfValidator.String(),
ValidatorAddress: validator.GetOperator().String(),
Shares: minDelegation,
})

k.SetObserverSet(ctx, types.ObserverSet{
ObserverList: []string{accAddressOfValidator.String()},
})
err = k.CheckObserverSelfDelegation(ctx, accAddressOfValidator.String())
require.NoError(t, err)

os, found := k.GetObserverSet(ctx)
require.True(t, found)
require.Equal(t, 1, len(os.ObserverList))
require.Equal(t, accAddressOfValidator.String(), os.ObserverList[0])
})
}

func TestKeeper_IsOpeartorTombstoned(t *testing.T) {
t.Run("should err if invalid addr", func(t *testing.T) {
k, ctx, _, _ := keepertest.ObserverKeeper(t)

res, err := k.IsOperatorTombstoned(ctx, "invalid")
require.Error(t, err)
require.False(t, res)
})

t.Run("should error if validator not found", func(t *testing.T) {
k, ctx, _, _ := keepertest.ObserverKeeper(t)

accAddress := sample.AccAddress()
res, err := k.IsOperatorTombstoned(ctx, accAddress)
require.Error(t, err)
require.False(t, res)
})

t.Run("should not error if validator found", func(t *testing.T) {
k, ctx, _, _ := keepertest.ObserverKeeper(t)

r := rand.New(rand.NewSource(9))
validator := sample.Validator(t, r)
k.GetStakingKeeper().SetValidator(ctx, validator)
accAddressOfValidator, err := types.GetAccAddressFromOperatorAddress(validator.OperatorAddress)
require.NoError(t, err)

res, err := k.IsOperatorTombstoned(ctx, accAddressOfValidator.String())
require.NoError(t, err)
require.False(t, res)
})
}

func TestKeeper_IsValidator(t *testing.T) {
t.Run("should err if invalid addr", func(t *testing.T) {
k, ctx, _, _ := keepertest.ObserverKeeper(t)

err := k.IsValidator(ctx, "invalid")
require.Error(t, err)
})

t.Run("should error if validator not found", func(t *testing.T) {
k, ctx, _, _ := keepertest.ObserverKeeper(t)

accAddress := sample.AccAddress()
err := k.IsValidator(ctx, accAddress)
require.Error(t, err)
})

t.Run("should err if validator not bonded", func(t *testing.T) {
k, ctx, _, _ := keepertest.ObserverKeeper(t)

r := rand.New(rand.NewSource(9))
validator := sample.Validator(t, r)
k.GetStakingKeeper().SetValidator(ctx, validator)
accAddressOfValidator, err := types.GetAccAddressFromOperatorAddress(validator.OperatorAddress)
require.NoError(t, err)

err = k.IsValidator(ctx, accAddressOfValidator.String())
require.Error(t, err)
})

t.Run("should err if validator jailed", func(t *testing.T) {
k, ctx, _, _ := keepertest.ObserverKeeper(t)

r := rand.New(rand.NewSource(9))
validator := sample.Validator(t, r)
validator.Status = stakingtypes.Bonded
validator.Jailed = true
k.GetStakingKeeper().SetValidator(ctx, validator)
accAddressOfValidator, err := types.GetAccAddressFromOperatorAddress(validator.OperatorAddress)
require.NoError(t, err)

err = k.IsValidator(ctx, accAddressOfValidator.String())
require.Error(t, err)
})

t.Run("should not err if validator not jailed and bonded", func(t *testing.T) {
k, ctx, _, _ := keepertest.ObserverKeeper(t)

r := rand.New(rand.NewSource(9))
validator := sample.Validator(t, r)
validator.Status = stakingtypes.Bonded
validator.Jailed = false
k.GetStakingKeeper().SetValidator(ctx, validator)
accAddressOfValidator, err := types.GetAccAddressFromOperatorAddress(validator.OperatorAddress)
require.NoError(t, err)

err = k.IsValidator(ctx, accAddressOfValidator.String())
require.NoError(t, err)
})
}

func TestKeeper_FindBallot(t *testing.T) {
t.Run("should err if chain params not found", func(t *testing.T) {
k, ctx, _, _ := keepertest.ObserverKeeper(t)

_, _, err := k.FindBallot(ctx, "index", &chains.Chain{
ChainId: 987,
}, types.ObservationType_InBoundTx)
require.Error(t, err)
})
}
2 changes: 2 additions & 0 deletions x/observer/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package types
import (
sdk "github.com/cosmos/cosmos-sdk/types"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
"github.com/cosmos/cosmos-sdk/x/staking/types"

Check failure on line 6 in x/observer/types/expected_keepers.go

View workflow job for this annotation

GitHub Actions / lint

ST1019: package "github.com/cosmos/cosmos-sdk/x/staking/types" is being imported more than once (stylecheck)
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

Check failure on line 7 in x/observer/types/expected_keepers.go

View workflow job for this annotation

GitHub Actions / lint

ST1019(related information): other import of "github.com/cosmos/cosmos-sdk/x/staking/types" (stylecheck)
authoritytypes "github.com/zeta-chain/zetacore/x/authority/types"
)
Expand All @@ -11,6 +12,7 @@ type StakingKeeper interface {
GetValidator(ctx sdk.Context, addr sdk.ValAddress) (validator stakingtypes.Validator, found bool)
GetDelegation(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) (delegation stakingtypes.Delegation, found bool)
SetValidator(ctx sdk.Context, validator stakingtypes.Validator)
SetDelegation(ctx sdk.Context, delegation types.Delegation)
}

type SlashingKeeper interface {
Expand Down

0 comments on commit 7721694

Please sign in to comment.