Skip to content

Commit

Permalink
inbound vote test
Browse files Browse the repository at this point in the history
  • Loading branch information
lumtis committed Feb 20, 2024
1 parent 926e12e commit f474277
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 15 deletions.
5 changes: 5 additions & 0 deletions testutil/sample/sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ func newRandFromStringSeed(t *testing.T, s string) *rand.Rand {
return newRandFromSeed(int64(h.Sum64()))
}

// Rand returns a new random number generator
func Rand() *rand.Rand {
return newRandFromSeed(42)
}

// PubKey returns a sample account PubKey
func PubKey(r *rand.Rand) cryptotypes.PubKey {
seed := []byte(strconv.Itoa(r.Int()))
Expand Down
16 changes: 8 additions & 8 deletions x/observer/keeper/tss_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

func TestTSSGet(t *testing.T) {
k, ctx := keepertest.ObserverKeeper(t)
k, ctx, _ := keepertest.ObserverKeeper(t)
tss := sample.Tss()
k.SetTSS(ctx, tss)
tssQueried, found := k.GetTSS(ctx)
Expand All @@ -26,7 +26,7 @@ func TestTSSGet(t *testing.T) {

}
func TestTSSRemove(t *testing.T) {
k, ctx := keepertest.ObserverKeeper(t)
k, ctx, _ := keepertest.ObserverKeeper(t)
tss := sample.Tss()
k.SetTSS(ctx, tss)
k.RemoveTSS(ctx)
Expand All @@ -35,7 +35,7 @@ func TestTSSRemove(t *testing.T) {
}

func TestTSSQuerySingle(t *testing.T) {
k, ctx := keepertest.ObserverKeeper(t)
k, ctx, _ := keepertest.ObserverKeeper(t)
wctx := sdk.WrapSDKContext(ctx)
//msgs := createTSS(keeper, ctx, 1)
tss := sample.Tss()
Expand Down Expand Up @@ -69,7 +69,7 @@ func TestTSSQuerySingle(t *testing.T) {
}

func TestTSSQueryHistory(t *testing.T) {
keeper, ctx := keepertest.ObserverKeeper(t)
keeper, ctx, _ := keepertest.ObserverKeeper(t)
wctx := sdk.WrapSDKContext(ctx)
for _, tc := range []struct {
desc string
Expand Down Expand Up @@ -115,7 +115,7 @@ func TestTSSQueryHistory(t *testing.T) {

func TestKeeper_TssHistory(t *testing.T) {
t.Run("Get tss history paginated by limit", func(t *testing.T) {
k, ctx := keepertest.ObserverKeeper(t)
k, ctx, _ := keepertest.ObserverKeeper(t)
tssList := sample.TssList(10)
for _, tss := range tssList {
k.SetTSSHistory(ctx, tss)
Expand All @@ -132,7 +132,7 @@ func TestKeeper_TssHistory(t *testing.T) {
require.Equal(t, len(tssList), int(pageRes.Total))
})
t.Run("Get tss history paginated by offset", func(t *testing.T) {
k, ctx := keepertest.ObserverKeeper(t)
k, ctx, _ := keepertest.ObserverKeeper(t)
tssList := sample.TssList(100)
offset := 20
for _, tss := range tssList {
Expand All @@ -151,7 +151,7 @@ func TestKeeper_TssHistory(t *testing.T) {
require.Equal(t, len(tssList), int(pageRes.Total))
})
t.Run("Get all TSS without pagination", func(t *testing.T) {
k, ctx := keepertest.ObserverKeeper(t)
k, ctx, _ := keepertest.ObserverKeeper(t)
tssList := sample.TssList(100)
for _, tss := range tssList {
k.SetTSSHistory(ctx, tss)
Expand All @@ -166,7 +166,7 @@ func TestKeeper_TssHistory(t *testing.T) {
require.Equal(t, tssList, rst)
})
t.Run("Get historical TSS", func(t *testing.T) {
k, ctx := keepertest.ObserverKeeper(t)
k, ctx, _ := keepertest.ObserverKeeper(t)
tssList := sample.TssList(100)
for _, tss := range tssList {
k.SetTSSHistory(ctx, tss)
Expand Down
2 changes: 1 addition & 1 deletion x/observer/keeper/vote_inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (k Keeper) VoteOnInboundBallot(
if receiverChain == nil {
return false, sdkerrors.Wrap(types.ErrSupportedChains, fmt.Sprintf(
"ChainID %d, Observation %s",
receiverChain.ChainId,
receiverChainID,
types.ObservationType_InBoundTx.String()),
)
}
Expand Down
125 changes: 119 additions & 6 deletions x/observer/keeper/vote_inbound_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper_test

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
"github.com/zeta-chain/zetacore/common"
"github.com/zeta-chain/zetacore/testutil/sample"
Expand Down Expand Up @@ -106,9 +107,11 @@ func TestKeeper_VoteOnInboundBallot(t *testing.T) {
})

t.Run("fail if receiver chain not supported", func(t *testing.T) {
k, ctx, _ := keepertest.ObserverKeeper(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,
Expand All @@ -124,6 +127,8 @@ func TestKeeper_VoteOnInboundBallot(t *testing.T) {
k.SetObserverSet(ctx, types.ObserverSet{
ObserverList: []string{observer},
})
stakingMock.MockGetValidator(sample.Validator(t, sample.Rand()))
slashingMock.MockIsTombstoned(false)

_, err := k.VoteOnInboundBallot(
ctx,
Expand All @@ -150,6 +155,8 @@ func TestKeeper_VoteOnInboundBallot(t *testing.T) {
},
},
})
stakingMock.MockGetValidator(sample.Validator(t, sample.Rand()))
slashingMock.MockIsTombstoned(false)

_, err = k.VoteOnInboundBallot(
ctx,
Expand All @@ -165,9 +172,11 @@ func TestKeeper_VoteOnInboundBallot(t *testing.T) {
})

t.Run("fail if inbound contain ZETA but receiver chain doesn't support ZETA", func(t *testing.T) {
k, ctx, _ := keepertest.ObserverKeeper(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,
Expand All @@ -179,7 +188,7 @@ func TestKeeper_VoteOnInboundBallot(t *testing.T) {
IsSupported: true,
},
{
ChainId: common.ZetaPrivnetChain().ChainId,
ChainId: getValidEthChainIDWithIndex(t, 1),
IsSupported: true,
ZetaTokenContractAddress: "",
},
Expand All @@ -188,21 +197,125 @@ func TestKeeper_VoteOnInboundBallot(t *testing.T) {
k.SetObserverSet(ctx, types.ObserverSet{
ObserverList: []string{observer},
})
stakingMock.MockGetValidator(sample.Validator(t, sample.Rand()))
slashingMock.MockIsTombstoned(false)

_, err := k.VoteOnInboundBallot(
ctx,
getValidEthChainIDWithIndex(t, 0),
common.ZetaPrivnetChain().ChainId,
getValidEthChainIDWithIndex(t, 1),
common.CoinType_Zeta,
sample.AccAddress(),
observer,
"index",
"inTxHash",
)
require.Error(t, err)
require.ErrorIs(t, err, types.ErrInvalidZetaCoinTypes)
})

t.Run("can add vote to inbound ballot", func(t *testing.T) {
t.Run("can add vote and create ballot", 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)

isFinalized, err := k.VoteOnInboundBallot(
ctx,
getValidEthChainIDWithIndex(t, 0),
getValidEthChainIDWithIndex(t, 1),
common.CoinType_ERC20,
observer,
"index",
"inTxHash",
)
require.NoError(t, err)

// ballot should be finalized since there is only one observer
require.True(t, isFinalized)
})

t.Run("can add vote to an existing ballot", 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)

// set a ballot
threshold, err := sdk.NewDecFromStr("0.7")
require.NoError(t, err)
ballot := types.Ballot{
Index: "index",
BallotIdentifier: "index",
VoterList: []string{
sample.AccAddress(),
sample.AccAddress(),
observer,
sample.AccAddress(),
sample.AccAddress(),
},
Votes: types.CreateVotes(5),
ObservationType: types.ObservationType_InBoundTx,
BallotThreshold: threshold,
BallotStatus: types.BallotStatus_BallotInProgress,
}
k.SetBallot(ctx, &ballot)

isFinalized, err := k.VoteOnInboundBallot(
ctx,
getValidEthChainIDWithIndex(t, 0),
getValidEthChainIDWithIndex(t, 1),
common.CoinType_ERC20,
observer,
"index",
"inTxHash",
)
require.NoError(t, err)

// ballot should not be finalized as the threshold is not reached
require.False(t, isFinalized)
})
}

0 comments on commit f474277

Please sign in to comment.