diff --git a/x/observer/keeper/params_test.go b/x/observer/keeper/params_test.go index c487ea0f73..149de0e583 100644 --- a/x/observer/keeper/params_test.go +++ b/x/observer/keeper/params_test.go @@ -1,4 +1,4 @@ -package keeper +package keeper_test import ( "fmt" @@ -9,11 +9,13 @@ import ( "github.com/tendermint/tendermint/crypto" "github.com/stretchr/testify/require" + testutilkeeper "github.com/zeta-chain/zetacore/testutil/keeper" + "github.com/zeta-chain/zetacore/x/observer/keeper" "github.com/zeta-chain/zetacore/x/observer/types" ) func TestGetParams(t *testing.T) { - k, ctx := SetupKeeper(t) + k, ctx := keeper.SetupKeeper(t) params := types.DefaultParams() k.SetParams(ctx, params) @@ -22,7 +24,7 @@ func TestGetParams(t *testing.T) { } func TestGenerateAddress(t *testing.T) { - types.SetConfig(false) + testutilkeeper.SetConfig(false) addr := sdk.AccAddress(crypto.AddressHash([]byte("Output1" + strconv.Itoa(1)))) addrString := addr.String() fmt.Println(addrString) diff --git a/x/observer/types/ballot.go b/x/observer/types/ballot.go index 10f21119d1..b9619d16ff 100644 --- a/x/observer/types/ballot.go +++ b/x/observer/types/ballot.go @@ -14,12 +14,18 @@ func (m Ballot) AddVote(address string, vote VoteType) (Ballot, error) { // `index` is the index of the `address` in the `VoterList` // `index` is used to set the vote in the `Votes` array index := m.GetVoterIndex(address) + if index == -1 { + return m, errors.Wrap(ErrUnableToAddVote, fmt.Sprintf("Voter %s not in voter list", address)) + } m.Votes[index] = vote return m, nil } func (m Ballot) HasVoted(address string) bool { index := m.GetVoterIndex(address) + if index == -1 { + return false + } return m.Votes[index] != VoteType_NotYetVoted } diff --git a/x/observer/types/ballot_test.go b/x/observer/types/ballot_test.go index 2bd6441c48..322affd728 100644 --- a/x/observer/types/ballot_test.go +++ b/x/observer/types/ballot_test.go @@ -21,6 +21,7 @@ func TestBallot_AddVote(t *testing.T) { finalVotes []VoteType finalStatus BallotStatus isFinalized bool + wantErr bool }{ { name: "All success", @@ -188,6 +189,18 @@ func TestBallot_AddVote(t *testing.T) { finalStatus: BallotStatus_BallotInProgress, isFinalized: false, }, + { + name: "Voter not in voter list", + threshold: sdk.MustNewDecFromStr("0.66"), + voterList: []string{}, + votes: []votes{ + {"Observer5", VoteType_SuccessObservation}, + }, + wantErr: true, + finalVotes: []VoteType{}, + finalStatus: BallotStatus_BallotInProgress, + isFinalized: false, + }, } for _, test := range tt { test := test @@ -202,7 +215,11 @@ func TestBallot_AddVote(t *testing.T) { BallotStatus: BallotStatus_BallotInProgress, } for _, vote := range test.votes { - ballot, _ = ballot.AddVote(vote.address, vote.vote) + b, err := ballot.AddVote(vote.address, vote.vote) + if test.wantErr { + require.Error(t, err) + } + ballot = b } finalBallot, isFinalized := ballot.IsFinalizingVote() diff --git a/x/observer/types/test_data.go b/x/observer/types/test_data.go deleted file mode 100644 index 3a43ca4306..0000000000 --- a/x/observer/types/test_data.go +++ /dev/null @@ -1,27 +0,0 @@ -package types - -import ( - sdk "github.com/cosmos/cosmos-sdk/types" -) - -const ( - AccountAddressPrefix = "zeta" -) - -var ( - AccountPubKeyPrefix = AccountAddressPrefix + "pub" - ValidatorAddressPrefix = AccountAddressPrefix + "valoper" - ValidatorPubKeyPrefix = AccountAddressPrefix + "valoperpub" - ConsNodeAddressPrefix = AccountAddressPrefix + "valcons" - ConsNodePubKeyPrefix = AccountAddressPrefix + "valconspub" -) - -func SetConfig(seal bool) { - config := sdk.GetConfig() - config.SetBech32PrefixForAccount(AccountAddressPrefix, AccountPubKeyPrefix) - config.SetBech32PrefixForValidator(ValidatorAddressPrefix, ValidatorPubKeyPrefix) - config.SetBech32PrefixForConsensusNode(ConsNodeAddressPrefix, ConsNodePubKeyPrefix) - if seal { - config.Seal() - } -}