diff --git a/x/observer/keeper/grpc_query_prove_test.go b/x/observer/keeper/grpc_query_prove_test.go new file mode 100644 index 0000000000..7eb11b00f5 --- /dev/null +++ b/x/observer/keeper/grpc_query_prove_test.go @@ -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 +} diff --git a/x/observer/keeper/vote_inbound_test.go b/x/observer/keeper/vote_inbound_test.go index 41a3999b75..5b1c09e81b 100644 --- a/x/observer/keeper/vote_inbound_test.go +++ b/x/observer/keeper/vote_inbound_test.go @@ -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) diff --git a/x/observer/keeper/vote_outbound_test.go b/x/observer/keeper/vote_outbound_test.go index fa34a11ca6..c92fec5927 100644 --- a/x/observer/keeper/vote_outbound_test.go +++ b/x/observer/keeper/vote_outbound_test.go @@ -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)