Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
skosito committed Mar 29, 2024
1 parent de88a7d commit e99b43b
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 0 deletions.
72 changes: 72 additions & 0 deletions x/observer/keeper/grpc_query_prove_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package keeper_test

import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
"github.com/zeta-chain/zetacore/pkg/proofs"
keepertest "github.com/zeta-chain/zetacore/testutil/keeper"
"github.com/zeta-chain/zetacore/testutil/sample"
"github.com/zeta-chain/zetacore/x/observer/types"
)

func TestKeeper_Prove(t *testing.T) {
t.Run("should error if req is nil", func(t *testing.T) {
k, ctx, _, _ := keepertest.ObserverKeeper(t)
wctx := sdk.WrapSDKContext(ctx)

res, err := k.Prove(wctx, nil)
require.Nil(t, res)
require.Error(t, err)
})

t.Run("should error if invalid hash", func(t *testing.T) {
k, ctx, _, _ := keepertest.ObserverKeeper(t)
wctx := sdk.WrapSDKContext(ctx)

res, err := k.Prove(wctx, &types.QueryProveRequest{
ChainId: 987,
BlockHash: sample.Hash().String(),
})
require.Nil(t, res)
require.Error(t, err)
})

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

res, err := k.Prove(wctx, &types.QueryProveRequest{
ChainId: 5,
BlockHash: sample.Hash().String(),
})
require.Nil(t, res)
require.Error(t, err)
})

t.Run("should error if proof not valid", func(t *testing.T) {
k, ctx, _, _ := keepertest.ObserverKeeper(t)
wctx := sdk.WrapSDKContext(ctx)

hash := sample.Hash()
bh := proofs.BlockHeader{
Height: 1,
Hash: hash.Bytes(),
ParentHash: sample.Hash().Bytes(),
ChainId: 1,
Header: proofs.HeaderData{},
}
k.SetBlockHeader(ctx, bh)

res, err := k.Prove(wctx, &types.QueryProveRequest{
ChainId: 5,
BlockHash: hash.String(),
Proof: &proofs.Proof{},
})
require.Nil(t, res)
require.Error(t, err)
})

// TODO: // https://github.com/zeta-chain/node/issues/1875 add more tests
}
51 changes: 51 additions & 0 deletions x/observer/keeper/vote_inbound_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,57 @@ func TestKeeper_VoteOnInboundBallot(t *testing.T) {
require.True(t, isNew)
})

t.Run("fail if can not add vote", func(t *testing.T) {
k, ctx, _, _ := keepertest.ObserverKeeperWithMocks(t, keepertest.ObserverMocksAll)

observer := sample.AccAddress()
stakingMock := keepertest.GetObserverStakingMock(t, k)
slashingMock := keepertest.GetObserverSlashingMock(t, k)

k.SetCrosschainFlags(ctx, types.CrosschainFlags{
IsInboundEnabled: true,
})
k.SetChainParamsList(ctx, types.ChainParamsList{
ChainParams: []*types.ChainParams{
{
ChainId: getValidEthChainIDWithIndex(t, 0),
IsSupported: true,
},
{
ChainId: getValidEthChainIDWithIndex(t, 1),
IsSupported: true,
},
},
})
k.SetObserverSet(ctx, types.ObserverSet{
ObserverList: []string{observer},
})
stakingMock.MockGetValidator(sample.Validator(t, sample.Rand()))
slashingMock.MockIsTombstoned(false)
ballot := types.Ballot{
Index: "index",
BallotIdentifier: "index",
VoterList: []string{observer},
// already voted
Votes: []types.VoteType{types.VoteType_SuccessObservation},
BallotStatus: types.BallotStatus_BallotInProgress,
BallotThreshold: sdk.NewDec(2),
}
k.SetBallot(ctx, &ballot)
isFinalized, isNew, err := k.VoteOnInboundBallot(
ctx,
getValidEthChainIDWithIndex(t, 0),
getValidEthChainIDWithIndex(t, 1),
coin.CoinType_ERC20,
observer,
"index",
"inTxHash",
)
require.Error(t, err)
require.False(t, isFinalized)
require.False(t, isNew)
})

t.Run("can add vote and create ballot without finalizing ballot", func(t *testing.T) {
k, ctx, _, _ := keepertest.ObserverKeeperWithMocks(t, keepertest.ObserverMocksAll)

Expand Down
42 changes: 42 additions & 0 deletions x/observer/keeper/vote_outbound_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,48 @@ func TestKeeper_VoteOnOutboundBallot(t *testing.T) {
require.Equal(t, expectedBallot, ballot)
})

t.Run("fail if can not add vote", func(t *testing.T) {
k, ctx, _, _ := keepertest.ObserverKeeperWithMocks(t, keepertest.ObserverMocksAll)

observer := sample.AccAddress()
stakingMock := keepertest.GetObserverStakingMock(t, k)
slashingMock := keepertest.GetObserverSlashingMock(t, k)

k.SetChainParamsList(ctx, types.ChainParamsList{
ChainParams: []*types.ChainParams{
{
ChainId: getValidEthChainIDWithIndex(t, 0),
IsSupported: true,
},
},
})
k.SetObserverSet(ctx, types.ObserverSet{
ObserverList: []string{observer},
})
stakingMock.MockGetValidator(sample.Validator(t, sample.Rand()))
slashingMock.MockIsTombstoned(false)
ballot := types.Ballot{
Index: "index",
BallotIdentifier: "index",
VoterList: []string{observer},
// already voted
Votes: []types.VoteType{types.VoteType_SuccessObservation},
BallotStatus: types.BallotStatus_BallotInProgress,
BallotThreshold: sdk.NewDec(2),
}
k.SetBallot(ctx, &ballot)
isFinalized, isNew, ballot, _, err := k.VoteOnOutboundBallot(
ctx,
"index",
getValidEthChainIDWithIndex(t, 0),
chains.ReceiveStatus_Success,
observer,
)
require.Error(t, err)
require.False(t, isFinalized)
require.False(t, isNew)
})

t.Run("can add vote and create ballot without finalizing ballot", func(t *testing.T) {
k, ctx, _, _ := keepertest.ObserverKeeperWithMocks(t, keepertest.ObserverMocksAll)

Expand Down

0 comments on commit e99b43b

Please sign in to comment.