Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: increase x/oracle unit test coverage #798

Merged
merged 9 commits into from
Apr 12, 2022
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
- [781](https://github.com/umee-network/umee/pull/781) Oracle module unit test cleanup.
- [782](https://github.com/umee-network/umee/pull/782) Add unit test to `x/oracle/types/denom.go` and `x/oracle/types/keys.go`.
- [786](https://github.com/umee-network/umee/pull/786) Add unit test to `x/oracle/...`.
- [798](https://github.com/umee-network/umee/pull/798) Increase `x/oracle` unit test coverage.

## [v2.0.0](https://github.com/umee-network/umee/releases/tag/v2.0.0) - 2022-04-06

Expand Down
1 change: 1 addition & 0 deletions x/oracle/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ func (s *IntegrationTestSuite) TestMsgServer_AggregateExchangeRateVote() {
_, err = s.msgServer.AggregateExchangeRateVote(sdk.WrapSDKContext(ctx), voteMsg)
s.Require().NoError(err)
vote, err := s.app.OracleKeeper.GetAggregateExchangeRateVote(ctx, valAddr)
s.Require().Nil(err)
for _, v := range vote.ExchangeRateTuples {
s.Require().Contains(acceptListFlat, strings.ToLower(v.Denom))
}
Expand Down
21 changes: 21 additions & 0 deletions x/oracle/keeper/params_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package keeper_test

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/umee-network/umee/v2/x/oracle/types"
)

func (s *IntegrationTestSuite) TestVoteThreshold() {
app, ctx := s.app, s.ctx

voteDec := app.OracleKeeper.VoteThreshold(ctx)
s.Require().Equal(sdk.MustNewDecFromStr("0.5"), voteDec)

newVoteTreshold := sdk.MustNewDecFromStr("0.6")
defaultParams := types.DefaultParams()
defaultParams.VoteThreshold = newVoteTreshold
app.OracleKeeper.SetParams(ctx, defaultParams)

voteThresholdDec := app.OracleKeeper.VoteThreshold(ctx)
s.Require().Equal(newVoteTreshold, voteThresholdDec)
}
8 changes: 6 additions & 2 deletions x/oracle/keeper/reward_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@ func (s *IntegrationTestSuite) TestRewardBallotWinners() {
s.Require().NoError(err)

var voteTargets []string
var voteTargetDenoms []string
params := s.app.OracleKeeper.GetParams(s.ctx)
for _, v := range params.AcceptList {
voteTargets = append(voteTargets, v.SymbolDenom)
voteTargetDenoms = append(voteTargetDenoms, v.BaseDenom)
}

votePeriodsPerWindow := sdk.NewDec((int64)(s.app.OracleKeeper.RewardDistributionWindow(s.ctx))).
Expand All @@ -38,3 +36,9 @@ func (s *IntegrationTestSuite) TestRewardBallotWinners() {
s.Require().Equal(sdk.NewDecFromInt(givingAmt.AmountOf(types.UmeeDenom)).QuoInt64(votePeriodsPerWindow).QuoInt64(3).TruncateInt(),
outstandingRewards.AmountOf(types.UmeeDenom))
}

func (s *IntegrationTestSuite) TestRewardBallotWinnersPowerSum0() {
RafilxTenfen marked this conversation as resolved.
Show resolved Hide resolved
s.app.OracleKeeper.RewardBallotWinners(s.ctx, 0, 0, []string{}, map[string]types.Claim{valAddr.String(): {}})
outstandingRewardsDec := s.app.DistrKeeper.GetValidatorOutstandingRewardsCoins(s.ctx, valAddr)
s.Require().Equal("", outstandingRewardsDec.String())
}
82 changes: 82 additions & 0 deletions x/oracle/types/ballot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,85 @@ func TestPBStandardDeviation_Overflow(t *testing.T) {
expectedDevation := sdk.MustNewDecFromStr("871.862661203013097586")
require.Equal(t, expectedDevation, deviation)
}

func TestBallotMapToSlice(t *testing.T) {
valAddress := GenerateRandomValAddr(1)

pb := ExchangeRateBallot{
NewVoteForTally(
sdk.NewDec(1234),
UmeeSymbol,
valAddress[0],
2,
),
NewVoteForTally(
sdk.NewDec(12345),
UmeeSymbol,
valAddress[0],
1,
),
}

ballotSlice := BallotMapToSlice(map[string]ExchangeRateBallot{
UmeeDenom: pb,
IbcDenomAtom: pb,
})
require.Equal(t, []BallotDenom{{Ballot: pb, Denom: IbcDenomAtom}, {Ballot: pb, Denom: UmeeDenom}}, ballotSlice)
}

func TestExchangeRateBallotSwap(t *testing.T) {
valAddress := GenerateRandomValAddr(2)

voteTallyVal0 := NewVoteForTally(
sdk.NewDec(1234),
UmeeSymbol,
valAddress[0],
2,
)
voteTallyVal1 := NewVoteForTally(
sdk.NewDec(12345),
UmeeSymbol,
valAddress[1],
1,
)

pb := ExchangeRateBallot{voteTallyVal0, voteTallyVal1}
RafilxTenfen marked this conversation as resolved.
Show resolved Hide resolved

require.Equal(t, pb[0], voteTallyVal0)
require.Equal(t, pb[1], voteTallyVal1)
pb.Swap(1, 0)
require.Equal(t, pb[1], voteTallyVal0)
require.Equal(t, pb[0], voteTallyVal1)
}

func TestStandardDeviation_WeightedMedian(t *testing.T) {
RafilxTenfen marked this conversation as resolved.
Show resolved Hide resolved
valAddress := GenerateRandomValAddr(1)
pb := ExchangeRateBallot{
NewVoteForTally(
sdk.NewDec(1234),
UmeeSymbol,
valAddress[0],
2,
),
NewVoteForTally(
sdk.NewDec(12),
UmeeSymbol,
valAddress[0],
1,
),
}

deviation, err := pb.StandardDeviation()
require.ErrorIs(t, err, ErrBallotNotSorted)
require.Equal(t, "0.000000000000000000", deviation.String())
}

func TestClaimMapToSlice(t *testing.T) {
valAddress := GenerateRandomValAddr(1)
claim := NewClaim(10, 1, 4, valAddress[0])
claimSlice := ClaimMapToSlice(map[string]Claim{
"testClaim": claim,
"anotherClaim": claim,
})
require.Equal(t, []Claim{claim, claim}, claimSlice)
}
14 changes: 14 additions & 0 deletions x/oracle/types/codec_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package types

import (
"testing"

"github.com/cosmos/cosmos-sdk/codec/types"
"github.com/stretchr/testify/require"
)

func TestRegisterInterfaces(t *testing.T) {
registry := types.NewInterfaceRegistry()
RegisterInterfaces(registry)
require.Equal(t, registry.ListAllInterfaces(), []string([]string{}))
}
7 changes: 6 additions & 1 deletion x/oracle/types/genesis_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types

import (
"encoding/json"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -9,7 +10,7 @@ import (

func TestGenesisValidation(t *testing.T) {
// Valid state
genState := DefaultGenesisState()
genState := NewGenesisState(DefaultParams(), []ExchangeRateTuple{}, []FeederDelegation{}, []MissCounter{}, []AggregateExchangeRatePrevote{}, []AggregateExchangeRateVote{})
require.NoError(t, ValidateGenesis(genState))

// Invalid Vote Period
Expand Down Expand Up @@ -57,3 +58,7 @@ func TestGenesisValidation(t *testing.T) {
genState.Params.AcceptList = DenomList{Denom{}}
require.Error(t, ValidateGenesis(genState))
}

func TestGetGenesisStateFromAppState(t *testing.T) {
require.NotNil(t, GetGenesisStateFromAppState(ModuleCdc, map[string]json.RawMessage{}))
}
88 changes: 71 additions & 17 deletions x/oracle/types/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,20 @@ func TestMsgAggregateExchangeRatePrevote(t *testing.T) {
tests := []struct {
hash AggregateVoteHash
exchangeRates sdk.DecCoins
voter sdk.AccAddress
feeder sdk.AccAddress
validator sdk.AccAddress
expectPass bool
}{
{bz, exchangeRates, addrs[0], true},
{bz[1:], exchangeRates, addrs[0], false},
{bz, exchangeRates, sdk.AccAddress{}, false},
{AggregateVoteHash{}, exchangeRates, addrs[0], false},
{bz, exchangeRates, addrs[0], addrs[0], true},
{[]byte("0\x01"), exchangeRates, addrs[0], addrs[0], false},
{bz[1:], exchangeRates, addrs[0], addrs[0], false},
{bz, exchangeRates, sdk.AccAddress{}, addrs[0], false},
{AggregateVoteHash{}, exchangeRates, addrs[0], addrs[0], false},
{bz, exchangeRates, addrs[0], sdk.AccAddress{}, false},
}

for i, tc := range tests {
msg := NewMsgAggregateExchangeRatePrevote(tc.hash, tc.voter, sdk.ValAddress(tc.voter))
msg := NewMsgAggregateExchangeRatePrevote(tc.hash, tc.feeder, sdk.ValAddress(tc.validator))
if tc.expectPass {
require.NoError(t, msg.ValidateBasic(), "test: %v", i)
} else {
Expand All @@ -73,31 +76,82 @@ func TestMsgAggregateExchangeRateVote(t *testing.T) {
exchangeRates := "foo:1.0,bar:1232.123"
zeroExchangeRates := "foo:0.0,bar:1232.132"
negativeExchangeRates := "foo:-1234.5,bar:1232.132"
overFlowExchangeRates := "foo:1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0,bar:1232.132"
overFlowMsgExchangeRates := StringWithCharset(4097, "56432")
overFlowExchangeRates := "foo:100000000000000000000000000000000000000000000000000000000000000000000000000000.01,bar:1232.132"
validSalt := "0cf33fb528b388660c3a42c3f3250e983395290b75fef255050fb5bc48a6025f"
saltWithColon := "0cf33fb528b388660c3a42c3f3250e983395290b75fef255050fb5bc48a6025:"
tests := []struct {
voter sdk.AccAddress
feeder sdk.AccAddress
validator sdk.AccAddress
salt string
exchangeRates string
expectPass bool
}{
{addrs[0], validSalt, exchangeRates, true},
{addrs[0], validSalt, invalidExchangeRates, false},
{addrs[0], validSalt, zeroExchangeRates, false},
{addrs[0], validSalt, negativeExchangeRates, false},
{addrs[0], validSalt, overFlowExchangeRates, false},
{sdk.AccAddress{}, validSalt, exchangeRates, false},
{addrs[0], "", exchangeRates, false},
{addrs[0], saltWithColon, exchangeRates, false},
{addrs[0], addrs[0], validSalt, exchangeRates, true},
RafilxTenfen marked this conversation as resolved.
Show resolved Hide resolved
{addrs[0], addrs[0], validSalt, invalidExchangeRates, false},
{addrs[0], addrs[0], validSalt, zeroExchangeRates, false},
{addrs[0], addrs[0], validSalt, negativeExchangeRates, false},
{addrs[0], addrs[0], validSalt, overFlowMsgExchangeRates, false},
{addrs[0], addrs[0], validSalt, overFlowExchangeRates, false},
{sdk.AccAddress{}, sdk.AccAddress{}, validSalt, exchangeRates, false},
{addrs[0], sdk.AccAddress{}, validSalt, exchangeRates, false},
{addrs[0], addrs[0], "", exchangeRates, false},
{addrs[0], addrs[0], validSalt, "", false},
{addrs[0], addrs[0], saltWithColon, exchangeRates, false},
}

for i, tc := range tests {
msg := NewMsgAggregateExchangeRateVote(tc.salt, tc.exchangeRates, tc.voter, sdk.ValAddress(tc.voter))
msg := NewMsgAggregateExchangeRateVote(tc.salt, tc.exchangeRates, tc.feeder, sdk.ValAddress(tc.validator))
if tc.expectPass {
require.Nil(t, msg.ValidateBasic(), "test: %v", i)
} else {
require.NotNil(t, msg.ValidateBasic(), "test: %v", i)
}
}
}

func TestNewMsgAggregateExchangeRatePrevote(t *testing.T) {
vals := GenerateRandomValAddr(2)
feederAddr := sdk.AccAddress(vals[1])

exchangeRates := sdk.DecCoins{sdk.NewDecCoinFromDec(UmeeDenom, sdk.OneDec()), sdk.NewDecCoinFromDec(UmeeDenom, sdk.NewDecWithPrec(32121, 1))}
bz := GetAggregateVoteHash("1", exchangeRates.String(), sdk.ValAddress(vals[0]))

aggregateExchangeRatePreVote := NewMsgAggregateExchangeRatePrevote(
bz,
feederAddr,
vals[0],
)

require.Equal(t, aggregateExchangeRatePreVote.Route(), RouterKey)
require.Equal(t, aggregateExchangeRatePreVote.Type(), TypeMsgAggregateExchangeRatePrevote)
require.NotNil(t, aggregateExchangeRatePreVote.GetSignBytes())
require.Equal(t, aggregateExchangeRatePreVote.GetSigners(), []sdk.AccAddress{feederAddr})
}

func TestNewMsgAggregateExchangeRateVote(t *testing.T) {
vals := GenerateRandomValAddr(2)
feederAddr := sdk.AccAddress(vals[1])

aggregateExchangeRateVote := NewMsgAggregateExchangeRateVote(
"salt",
"0.1",
feederAddr,
vals[0],
)

require.Equal(t, aggregateExchangeRateVote.Route(), RouterKey)
require.Equal(t, aggregateExchangeRateVote.Type(), TypeMsgAggregateExchangeRateVote)
require.NotNil(t, aggregateExchangeRateVote.GetSignBytes())
require.Equal(t, aggregateExchangeRateVote.GetSigners(), []sdk.AccAddress{feederAddr})
}

func TestMsgDelegateFeedConsent(t *testing.T) {
vals := GenerateRandomValAddr(2)
msgFeedConsent := NewMsgDelegateFeedConsent(vals[0], sdk.AccAddress(vals[1]))

require.Equal(t, msgFeedConsent.Route(), RouterKey)
require.Equal(t, msgFeedConsent.Type(), TypeMsgDelegateFeedConsent)
require.NotNil(t, msgFeedConsent.GetSignBytes())
require.Equal(t, msgFeedConsent.GetSigners(), []sdk.AccAddress{sdk.AccAddress(vals[0])})
}
31 changes: 31 additions & 0 deletions x/oracle/types/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,37 @@ var (
}
)

func StringWithCharset(length int, charset string) string {
RafilxTenfen marked this conversation as resolved.
Show resolved Hide resolved
b := make([]byte, length)

if len(charset) == 0 {
charset = "abcd"
}

for i := 0; i < length; i++ {
for j := 0; j < len(charset); j++ {
b[i] = charset[j]
i++
if len(b) == length {
return string(b)
}
}
}

return string(b)
}

// GenerateRandomValAddr returns N validators
RafilxTenfen marked this conversation as resolved.
Show resolved Hide resolved
func GenerateRandomValAddr(quantity int) (validatorAddrs []sdk.ValAddress) {
for i := 0; i < quantity; i++ {
pubKey := secp256k1.GenPrivKey().PubKey()
valAddr := sdk.ValAddress(pubKey.Address())
validatorAddrs = append(validatorAddrs, valAddr)
}

return validatorAddrs
}

// GenerateRandomTestCase
func GenerateRandomTestCase() (valValAddrs []sdk.ValAddress, stakingKeeper MockStakingKeeper) {
valValAddrs = []sdk.ValAddress{}
Expand Down