diff --git a/app/app_test.go b/app/app_test.go index e4e90245..9390c1f3 100644 --- a/app/app_test.go +++ b/app/app_test.go @@ -6,10 +6,11 @@ import ( "cosmossdk.io/log" wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper" - "github.com/archway-network/archway/app/appconst" tmdb "github.com/cosmos/cosmos-db" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/require" + + "github.com/archway-network/archway/app/appconst" ) // import ( diff --git a/e2e/ibchooks/ibchooks_test.go b/e2e/ibchooks/ibchooks_test.go index a28cb296..e01bfb04 100644 --- a/e2e/ibchooks/ibchooks_test.go +++ b/e2e/ibchooks/ibchooks_test.go @@ -7,12 +7,13 @@ import ( "testing" wasmKeeper "github.com/CosmWasm/wasmd/x/wasm/keeper" - "github.com/archway-network/archway/app" ibc_hooks "github.com/cosmos/ibc-apps/modules/ibc-hooks/v8" ibchookskeeper "github.com/cosmos/ibc-apps/modules/ibc-hooks/v8/keeper" "github.com/cosmos/ibc-apps/modules/ibc-hooks/v8/tests/unit/mocks" "github.com/stretchr/testify/suite" + "github.com/archway-network/archway/app" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth/types" diff --git a/e2e/ibchooks/setup.go b/e2e/ibchooks/setup.go index 60d92325..550faaea 100644 --- a/e2e/ibchooks/setup.go +++ b/e2e/ibchooks/setup.go @@ -9,8 +9,6 @@ import ( "cosmossdk.io/log" sdkmath "cosmossdk.io/math" wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper" - "github.com/archway-network/archway/app" - "github.com/archway-network/archway/app/appconst" abci "github.com/cometbft/cometbft/abci/types" cmttypes "github.com/cometbft/cometbft/types" dbm "github.com/cosmos/cosmos-db" @@ -29,6 +27,9 @@ import ( stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" "github.com/cosmos/ibc-go/v8/testing/mock" "github.com/stretchr/testify/require" + + "github.com/archway-network/archway/app" + "github.com/archway-network/archway/app/appconst" ) type App = app.ArchwayApp @@ -174,7 +175,6 @@ func GenesisStateWithValSet( } validators = append(validators, validator) delegations = append(delegations, stakingtypes.NewDelegation(genAccs[0].GetAddress().String(), sdk.ValAddress(val.Address).String(), sdkmath.LegacyOneDec())) - } // set validators and delegations diff --git a/e2e/testing/client.go b/e2e/testing/client.go index b32dc85a..788f7552 100644 --- a/e2e/testing/client.go +++ b/e2e/testing/client.go @@ -40,7 +40,7 @@ func (c grpcClient) Invoke(ctx context.Context, method string, args, reply inter } if resp.Code != abci.CodeTypeOK { - return fmt.Errorf(resp.Log) + return fmt.Errorf("%s", resp.Log) } c.app.AppCodec().MustUnmarshal(resp.Value, reply.(proto.Message)) diff --git a/types/math/dec.go b/types/math/dec.go index 9f5a36fd..6961947e 100644 --- a/types/math/dec.go +++ b/types/math/dec.go @@ -6,6 +6,7 @@ import ( "strings" "cosmossdk.io/math" + "github.com/archway-network/archway/types/errors" ) diff --git a/types/math/dec_test.go b/types/math/dec_test.go index 7ad8ec19..3a676beb 100644 --- a/types/math/dec_test.go +++ b/types/math/dec_test.go @@ -7,8 +7,9 @@ import ( "cosmossdk.io/math" - "github.com/archway-network/archway/types/errors" "github.com/stretchr/testify/assert" + + "github.com/archway-network/archway/types/errors" ) func TestSqrtBigInt(t *testing.T) { diff --git a/x/oracle/abci_test.go b/x/oracle/abci_test.go index 27a58114..0fe43380 100644 --- a/x/oracle/abci_test.go +++ b/x/oracle/abci_test.go @@ -6,6 +6,9 @@ import ( "cosmossdk.io/math" "github.com/stretchr/testify/require" + cmTypes "github.com/cometbft/cometbft/types" + sdk "github.com/cosmos/cosmos-sdk/types" + e2eTesting "github.com/archway-network/archway/e2e/testing" "github.com/archway-network/archway/x/oracle" "github.com/archway-network/archway/x/oracle/asset" @@ -13,8 +16,6 @@ import ( "github.com/archway-network/archway/x/oracle/keeper" "github.com/archway-network/archway/x/oracle/types" oracletypes "github.com/archway-network/archway/x/oracle/types" - cmTypes "github.com/cometbft/cometbft/types" - sdk "github.com/cosmos/cosmos-sdk/types" ) // TODO (spekalsg3): duplicated from `package keeper_test` @@ -55,7 +56,7 @@ func TestOracleTallyTiming(t *testing.T) { params, err := keepers.OracleKeeper.Params.Get(ctx) require.NoError(t, err) params.VotePeriod = 1 - keepers.OracleKeeper.Params.Set(ctx, params) + require.NoError(t, keepers.OracleKeeper.Params.Set(ctx, params)) // all the Addrs vote for the block ... not last period block yet, so tally fails for _, val := range chain.GetCurrentValSet().Validators { @@ -66,15 +67,15 @@ func TestOracleTallyTiming(t *testing.T) { params.VotePeriod = 10 // set vote period to 10 for now, for convenience params.ExpirationBlocks = 100 - keepers.OracleKeeper.Params.Set(ctx, params) + require.NoError(t, keepers.OracleKeeper.Params.Set(ctx, params)) require.Equal(t, 1, int(ctx.BlockHeight())) - oracle.EndBlocker(ctx, keepers.OracleKeeper) + require.NoError(t, oracle.EndBlocker(ctx, keepers.OracleKeeper)) _, err = keepers.OracleKeeper.ExchangeRates.Get(ctx, asset.Registry.Pair(denoms.BTC, denoms.USD)) require.Error(t, err) ctx = ctx.WithBlockHeight(int64(params.VotePeriod)) - oracle.EndBlocker(ctx, keepers.OracleKeeper) + require.NoError(t, oracle.EndBlocker(ctx, keepers.OracleKeeper)) _, err = keepers.OracleKeeper.ExchangeRates.Get(ctx, asset.Registry.Pair(denoms.BTC, denoms.USD)) require.NoError(t, err) @@ -95,7 +96,7 @@ func TestOraclePriceExpiration(t *testing.T) { params, err := keepers.OracleKeeper.Params.Get(ctx) require.NoError(t, err) params.VotePeriod = 1 - keepers.OracleKeeper.Params.Set(ctx, params) + require.NoError(t, keepers.OracleKeeper.Params.Set(ctx, params)) // Set prices for both pairs for _, val := range chain.GetCurrentValSet().Validators { @@ -107,11 +108,11 @@ func TestOraclePriceExpiration(t *testing.T) { params.VotePeriod = 10 params.ExpirationBlocks = 10 - keepers.OracleKeeper.Params.Set(ctx, params) + require.NoError(t, keepers.OracleKeeper.Params.Set(ctx, params)) // Wait for prices to set ctx = ctx.WithBlockHeight(int64(params.VotePeriod)) - oracle.EndBlocker(ctx, keepers.OracleKeeper) + require.NoError(t, oracle.EndBlocker(ctx, keepers.OracleKeeper)) // Check if both prices are set _, err = keepers.OracleKeeper.ExchangeRates.Get(ctx, pair1) @@ -129,12 +130,12 @@ func TestOraclePriceExpiration(t *testing.T) { // Set price ctx = ctx.WithBlockHeight(voteHeight) - oracle.EndBlocker(ctx, keepers.OracleKeeper) + require.NoError(t, oracle.EndBlocker(ctx, keepers.OracleKeeper)) // Set the block height to the expiration height // End blocker should delete the price of pair2 ctx = ctx.WithBlockHeight(int64(params.ExpirationBlocks + params.VotePeriod)) - oracle.EndBlocker(ctx, keepers.OracleKeeper) + require.NoError(t, oracle.EndBlocker(ctx, keepers.OracleKeeper)) _, err = keepers.OracleKeeper.ExchangeRates.Get(ctx, pair1) require.NoError(t, err) diff --git a/x/oracle/asset/pair.go b/x/oracle/asset/pair.go index 9c385251..1c5e167f 100644 --- a/x/oracle/asset/pair.go +++ b/x/oracle/asset/pair.go @@ -177,7 +177,6 @@ func (pke pairKeyEncoder) EncodeNonTerminal(buffer []byte, key Pair) (int, error } func (pke pairKeyEncoder) DecodeNonTerminal(buffer []byte) (int, Pair, error) { return pke.Decode(buffer) - } func (pairKeyEncoder) SizeNonTerminal(key Pair) int { return key.Size() diff --git a/x/oracle/client/cli/gen_pricefeeder_delegation_test.go b/x/oracle/client/cli/gen_pricefeeder_delegation_test.go index b3068c7e..b3bf3975 100644 --- a/x/oracle/client/cli/gen_pricefeeder_delegation_test.go +++ b/x/oracle/client/cli/gen_pricefeeder_delegation_test.go @@ -6,9 +6,10 @@ import ( "github.com/archway-network/archway/x/oracle/client/cli" - e2eTesting "github.com/archway-network/archway/e2e/testing" "github.com/cosmos/cosmos-sdk/client/flags" "github.com/stretchr/testify/require" + + e2eTesting "github.com/archway-network/archway/e2e/testing" ) func TestAddGenesisPricefeederDelegation(t *testing.T) { diff --git a/x/oracle/genesis.go b/x/oracle/genesis.go index 5242844c..16ba1283 100644 --- a/x/oracle/genesis.go +++ b/x/oracle/genesis.go @@ -24,7 +24,10 @@ func InitGenesis(ctx sdk.Context, keeper keeper.Keeper, data *types.GenesisState panic(err) } - keeper.FeederDelegations.Set(ctx, voter, feeder) + err = keeper.FeederDelegations.Set(ctx, voter, feeder) + if err != nil { + panic(err) + } } for _, ex := range data.ExchangeRates { @@ -37,7 +40,10 @@ func InitGenesis(ctx sdk.Context, keeper keeper.Keeper, data *types.GenesisState panic(err) } - keeper.MissCounters.Set(ctx, operator, missCounter.MissCounter) + err = keeper.MissCounters.Set(ctx, operator, missCounter.MissCounter) + if err != nil { + panic(err) + } } for _, aggregatePrevote := range data.AggregateExchangeRatePrevotes { @@ -46,7 +52,10 @@ func InitGenesis(ctx sdk.Context, keeper keeper.Keeper, data *types.GenesisState panic(err) } - keeper.Prevotes.Set(ctx, valAddr, aggregatePrevote) + err = keeper.Prevotes.Set(ctx, valAddr, aggregatePrevote) + if err != nil { + panic(err) + } } for _, aggregateVote := range data.AggregateExchangeRateVotes { @@ -55,28 +64,46 @@ func InitGenesis(ctx sdk.Context, keeper keeper.Keeper, data *types.GenesisState panic(err) } - keeper.Votes.Set(ctx, valAddr, aggregateVote) + err = keeper.Votes.Set(ctx, valAddr, aggregateVote) + if err != nil { + panic(err) + } } if len(data.Pairs) > 0 { for _, tt := range data.Pairs { - keeper.WhitelistedPairs.Set(ctx, tt) + err := keeper.WhitelistedPairs.Set(ctx, tt) + if err != nil { + panic(err) + } } } else { for _, item := range data.Params.Whitelist { - keeper.WhitelistedPairs.Set(ctx, item) + err := keeper.WhitelistedPairs.Set(ctx, item) + if err != nil { + panic(err) + } } } for _, pr := range data.Rewards { - keeper.Rewards.Set(ctx, pr.Id, pr) + err := keeper.Rewards.Set(ctx, pr.Id, pr) + if err != nil { + panic(err) + } } // set last ID based on the last pair reward if len(data.Rewards) != 0 { - keeper.RewardsID.Set(ctx, data.Rewards[len(data.Rewards)-1].Id) + err := keeper.RewardsID.Set(ctx, data.Rewards[len(data.Rewards)-1].Id) + if err != nil { + panic(err) + } + } + err := keeper.Params.Set(ctx, data.Params) + if err != nil { + panic(err) } - keeper.Params.Set(ctx, data.Params) // check if the module account exists moduleAcc := keeper.AccountKeeper.GetModuleAccount(ctx, types.ModuleName) @@ -95,31 +122,40 @@ func ExportGenesis(ctx sdk.Context, keeper keeper.Keeper) *types.GenesisState { } feederDelegations := []types.FeederDelegation{} - keeper.FeederDelegations.Walk(ctx, nil, func(valBytes []byte, accBytes []byte) (bool, error) { + err = keeper.FeederDelegations.Walk(ctx, nil, func(valBytes []byte, accBytes []byte) (bool, error) { feederDelegations = append(feederDelegations, types.FeederDelegation{ FeederAddress: sdk.AccAddress(accBytes).String(), ValidatorAddress: sdk.ValAddress(valBytes).String(), }) return false, nil }) + if err != nil { + panic(err) + } exchangeRates := []types.ExchangeRateTuple{} - keeper.ExchangeRates.Walk(ctx, nil, func(pair asset.Pair, price types.DatedPrice) (bool, error) { + err = keeper.ExchangeRates.Walk(ctx, nil, func(pair asset.Pair, price types.DatedPrice) (bool, error) { exchangeRates = append(exchangeRates, types.ExchangeRateTuple{ Pair: pair, ExchangeRate: price.ExchangeRate, }) return false, nil }) + if err != nil { + panic(err) + } missCounters := []types.MissCounter{} - keeper.MissCounters.Walk(ctx, nil, func(valAddrBytes []byte, counter uint64) (bool, error) { + err = keeper.MissCounters.Walk(ctx, nil, func(valAddrBytes []byte, counter uint64) (bool, error) { missCounters = append(missCounters, types.MissCounter{ ValidatorAddress: sdk.ValAddress(valAddrBytes).String(), MissCounter: counter, }) return false, nil }) + if err != nil { + panic(err) + } var pairs []asset.Pair iter, err := keeper.WhitelistedPairs.Iterate(ctx, nil) diff --git a/x/oracle/genesis_test.go b/x/oracle/genesis_test.go index cb5618e6..68d8e173 100644 --- a/x/oracle/genesis_test.go +++ b/x/oracle/genesis_test.go @@ -25,19 +25,19 @@ func TestExportInitGenesis(t *testing.T) { ValAddrs[i] = sdk.ValAddress(vals[i].Address) } - keepers.OracleKeeper.Params.Set(ctx, types.DefaultParams()) - keepers.OracleKeeper.FeederDelegations.Set(ctx, ValAddrs[0], AccAddrs[1]) - keepers.OracleKeeper.ExchangeRates.Set(ctx, "pair1:pair2", types.DatedPrice{ExchangeRate: math.LegacyNewDec(123), CreatedBlock: 0}) - keepers.OracleKeeper.Prevotes.Set(ctx, ValAddrs[0], types.NewAggregateExchangeRatePrevote(types.AggregateVoteHash{123}, ValAddrs[0], uint64(2))) - keepers.OracleKeeper.Votes.Set(ctx, ValAddrs[0], types.NewAggregateExchangeRateVote(types.ExchangeRateTuples{{Pair: "foo", ExchangeRate: math.LegacyNewDec(123)}}, ValAddrs[0])) - keepers.OracleKeeper.WhitelistedPairs.Set(ctx, "pair1:pair1") - keepers.OracleKeeper.WhitelistedPairs.Set(ctx, "pair2:pair2") - keepers.OracleKeeper.MissCounters.Set(ctx, ValAddrs[0], 10) - keepers.OracleKeeper.Rewards.Set(ctx, 0, types.Rewards{ + require.NoError(t, keepers.OracleKeeper.Params.Set(ctx, types.DefaultParams())) + require.NoError(t, keepers.OracleKeeper.FeederDelegations.Set(ctx, ValAddrs[0], AccAddrs[1])) + require.NoError(t, keepers.OracleKeeper.ExchangeRates.Set(ctx, "pair1:pair2", types.DatedPrice{ExchangeRate: math.LegacyNewDec(123), CreatedBlock: 0})) + require.NoError(t, keepers.OracleKeeper.Prevotes.Set(ctx, ValAddrs[0], types.NewAggregateExchangeRatePrevote(types.AggregateVoteHash{123}, ValAddrs[0], uint64(2)))) + require.NoError(t, keepers.OracleKeeper.Votes.Set(ctx, ValAddrs[0], types.NewAggregateExchangeRateVote(types.ExchangeRateTuples{{Pair: "foo", ExchangeRate: math.LegacyNewDec(123)}}, ValAddrs[0]))) + require.NoError(t, keepers.OracleKeeper.WhitelistedPairs.Set(ctx, "pair1:pair1")) + require.NoError(t, keepers.OracleKeeper.WhitelistedPairs.Set(ctx, "pair2:pair2")) + require.NoError(t, keepers.OracleKeeper.MissCounters.Set(ctx, ValAddrs[0], 10)) + require.NoError(t, keepers.OracleKeeper.Rewards.Set(ctx, 0, types.Rewards{ Id: 0, VotePeriods: 100, Coins: sdk.NewCoins(sdk.NewInt64Coin("test", 1000)), - }) + })) genesis := oracle.ExportGenesis(ctx, keepers.OracleKeeper) chain = e2eTesting.NewTestChain(t, 2) diff --git a/x/oracle/keeper/ballot.go b/x/oracle/keeper/ballot.go index 97befd87..ace8485e 100644 --- a/x/oracle/keeper/ballot.go +++ b/x/oracle/keeper/ballot.go @@ -1,7 +1,7 @@ package keeper import ( - "fmt" + "fmt" sdk "github.com/cosmos/cosmos-sdk/types" @@ -63,7 +63,7 @@ func (k Keeper) GroupVotesByPair( // ClearVotesAndPrevotes clears all tallied prevotes and votes from the store func (k Keeper) ClearVotesAndPrevotes(ctx sdk.Context, votePeriod uint64) { // Clear all aggregate prevotes - k.Prevotes.Walk(ctx, nil, func(valAddrBytes []byte, aggregatePrevote types.AggregateExchangeRatePrevote) (bool, error) { + _ = k.Prevotes.Walk(ctx, nil, func(valAddrBytes []byte, aggregatePrevote types.AggregateExchangeRatePrevote) (bool, error) { valAddr := sdk.ValAddress(valAddrBytes) if ctx.BlockHeight() >= int64(aggregatePrevote.SubmitBlock+votePeriod) { err := k.Prevotes.Remove(ctx, valAddr) diff --git a/x/oracle/keeper/ballot_test.go b/x/oracle/keeper/ballot_test.go index 8536a5ce..004c6745 100644 --- a/x/oracle/keeper/ballot_test.go +++ b/x/oracle/keeper/ballot_test.go @@ -34,7 +34,8 @@ func TestGroupVotesByPair(t *testing.T) { ValAddrs[i] = sdk.ValAddress(vals[i].Address) } - keepers.StakingKeeper.EndBlocker(ctx) + _, err := keepers.StakingKeeper.EndBlocker(ctx) + require.NoError(t, err) pairBtc := asset.Registry.Pair(denoms.BTC, denoms.NUSD) pairEth := asset.Registry.Pair(denoms.ETH, denoms.NUSD) @@ -50,7 +51,7 @@ func TestGroupVotesByPair(t *testing.T) { } for i, v := range btcVotes { - keepers.OracleKeeper.Votes.Set( + require.NoError(t, keepers.OracleKeeper.Votes.Set( ctx, ValAddrs[i], types.NewAggregateExchangeRateVote( @@ -60,7 +61,7 @@ func TestGroupVotesByPair(t *testing.T) { }, ValAddrs[i], ), - ) + )) } // organize votes by pair @@ -105,7 +106,8 @@ func TestClearVotesAndPrevotes(t *testing.T) { ValAddrs[i] = sdk.ValAddress(vals[i].Address) } - keepers.StakingKeeper.EndBlocker(ctx) + _, err := keepers.StakingKeeper.EndBlocker(ctx) + require.NoError(t, err) btcVotes := types.ExchangeRateVotes{ types.NewExchangeRateVote(math.LegacyNewDec(17), asset.Registry.Pair(denoms.BTC, denoms.NUSD), ValAddrs[0], power), @@ -119,17 +121,17 @@ func TestClearVotesAndPrevotes(t *testing.T) { } for i := range btcVotes { - keepers.OracleKeeper.Prevotes.Set(ctx, ValAddrs[i], types.AggregateExchangeRatePrevote{ + require.NoError(t, keepers.OracleKeeper.Prevotes.Set(ctx, ValAddrs[i], types.AggregateExchangeRatePrevote{ Hash: "", Voter: ValAddrs[i].String(), SubmitBlock: uint64(ctx.BlockHeight()), - }) + })) - keepers.OracleKeeper.Votes.Set(ctx, ValAddrs[i], + require.NoError(t, keepers.OracleKeeper.Votes.Set(ctx, ValAddrs[i], types.NewAggregateExchangeRateVote(types.ExchangeRateTuples{ {Pair: btcVotes[i].Pair, ExchangeRate: btcVotes[i].ExchangeRate}, {Pair: ethVotes[i].Pair, ExchangeRate: ethVotes[i].ExchangeRate}, - }, ValAddrs[i])) + }, ValAddrs[i]))) } keepers.OracleKeeper.ClearVotesAndPrevotes(ctx, 10) diff --git a/x/oracle/keeper/keeper.go b/x/oracle/keeper/keeper.go index 25981a5b..25858841 100644 --- a/x/oracle/keeper/keeper.go +++ b/x/oracle/keeper/keeper.go @@ -158,7 +158,14 @@ func (k Keeper) GetExchangeRateTwap(ctx sdk.Context, pair asset.Pair) (price mat EndInclusive( ctx.BlockTime()), ) + if err != nil { + return math.LegacyOneDec().Neg(), err + } + snapshots, err := snapshotsIter.Values() + if err != nil { + return math.LegacyOneDec().Neg(), err + } if len(snapshots) == 0 { // if there are no snapshots, return -1 for the price @@ -206,15 +213,22 @@ func (k Keeper) GetExchangeRate(ctx sdk.Context, pair asset.Pair) (price math.Le // SetPrice sets the price for a pair as well as the price snapshot. func (k Keeper) SetPrice(ctx sdk.Context, pair asset.Pair, price math.LegacyDec) { - k.ExchangeRates.Set(ctx, pair, types.DatedPrice{ExchangeRate: price, CreatedBlock: uint64(ctx.BlockHeight())}) + if err := k.ExchangeRates.Set(ctx, pair, types.DatedPrice{ + ExchangeRate: price, + CreatedBlock: uint64(ctx.BlockHeight()), + }); err != nil { + ctx.Logger().Error("failed to set DatedPrice", "pair", pair, "error", err) + } key := collections.Join(pair, ctx.BlockTime()) timestampMs := ctx.BlockTime().UnixMilli() - k.PriceSnapshots.Set(ctx, key, types.PriceSnapshot{ + if err := k.PriceSnapshots.Set(ctx, key, types.PriceSnapshot{ Pair: pair, Price: price, TimestampMs: timestampMs, - }) + }); err != nil { + ctx.Logger().Error("failed to set PriceSnapshot", "pair", pair, "error", err) + } if err := ctx.EventManager().EmitTypedEvent(&types.EventPriceUpdate{ Pair: pair.String(), Price: price, diff --git a/x/oracle/keeper/keeper_test.go b/x/oracle/keeper/keeper_test.go index 1aa83c06..fc814b37 100644 --- a/x/oracle/keeper/keeper_test.go +++ b/x/oracle/keeper/keeper_test.go @@ -3,10 +3,11 @@ package keeper_test import ( "testing" - e2eTesting "github.com/archway-network/archway/e2e/testing" sdk "github.com/cosmos/cosmos-sdk/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" "github.com/stretchr/testify/require" + + e2eTesting "github.com/archway-network/archway/e2e/testing" ) func TestValidateFeeder(t *testing.T) { @@ -34,7 +35,8 @@ func TestValidateFeeder(t *testing.T) { ValAddrs[i] = sdk.ValAddress(val.Address) } - keepers.StakingKeeper.EndBlocker(ctx) + _, err := keepers.StakingKeeper.EndBlocker(ctx) + require.NoError(t, err) stakingparams, err := keepers.StakingKeeper.GetParams(ctx) require.NoError(t, err) @@ -60,12 +62,12 @@ func TestValidateFeeder(t *testing.T) { require.NoError(t, keepers.OracleKeeper.ValidateFeeder(ctx, sdk.AccAddress(ValAddrs[1]), ValAddrs[1])) // delegate works - keepers.OracleKeeper.FeederDelegations.Set(ctx, ValAddrs[0], AccAddrs[1]) + require.NoError(t, keepers.OracleKeeper.FeederDelegations.Set(ctx, ValAddrs[0], AccAddrs[1])) require.NoError(t, keepers.OracleKeeper.ValidateFeeder(ctx, AccAddrs[1], ValAddrs[0])) require.Error(t, keepers.OracleKeeper.ValidateFeeder(ctx, AccAddrs[2], ValAddrs[0])) // only active validators can do oracle votes validator.Status = stakingtypes.Unbonded - keepers.StakingKeeper.SetValidator(ctx, validator) + require.NoError(t, keepers.StakingKeeper.SetValidator(ctx, validator)) require.Error(t, keepers.OracleKeeper.ValidateFeeder(ctx, AccAddrs[1], ValAddrs[0])) } diff --git a/x/oracle/keeper/msg_server.go b/x/oracle/keeper/msg_server.go index abf20b8f..880f87f5 100644 --- a/x/oracle/keeper/msg_server.go +++ b/x/oracle/keeper/msg_server.go @@ -47,7 +47,9 @@ func (ms msgServer) AggregateExchangeRatePrevote( return nil, sdkerrors.Wrap(types.ErrInvalidHash, err.Error()) } - ms.Keeper.Prevotes.Set(ctx, valAddr, types.NewAggregateExchangeRatePrevote(voteHash, valAddr, uint64(ctx.BlockHeight()))) + if err := ms.Keeper.Prevotes.Set(ctx, valAddr, types.NewAggregateExchangeRatePrevote(voteHash, valAddr, uint64(ctx.BlockHeight()))); err != nil { + return nil, err + } err = ctx.EventManager().EmitTypedEvent(&types.EventAggregatePrevote{ Validator: msg.Validator, @@ -121,10 +123,17 @@ func (ms msgServer) AggregateExchangeRateVote( } // Move aggregate prevote to aggregate vote with given exchange rates - ms.Keeper.Votes.Set( + err = ms.Keeper.Votes.Set( ctx, valAddr, types.NewAggregateExchangeRateVote(exchangeRateTuples, valAddr), ) - _ = ms.Keeper.Prevotes.Remove(ctx, valAddr) + if err != nil { + return nil, err + } + + err = ms.Keeper.Prevotes.Remove(ctx, valAddr) + if err != nil { + return nil, err + } priceTuples, err := types.NewExchangeRateTuplesFromString(msg.ExchangeRates) if err != nil { @@ -161,7 +170,10 @@ func (ms msgServer) DelegateFeedConsent( } // Set the delegation - ms.Keeper.FeederDelegations.Set(ctx, operatorAddr, delegateAddr) + err = ms.Keeper.FeederDelegations.Set(ctx, operatorAddr, delegateAddr) + if err != nil { + return nil, err + } err = ctx.EventManager().EmitTypedEvent(&types.EventDelegateFeederConsent{ Feeder: msg.Delegate, diff --git a/x/oracle/keeper/msg_server_test.go b/x/oracle/keeper/msg_server_test.go index 78db6add..f86aaca4 100644 --- a/x/oracle/keeper/msg_server_test.go +++ b/x/oracle/keeper/msg_server_test.go @@ -34,7 +34,7 @@ func TestFeederDelegation(t *testing.T) { params, err := keepers.OracleKeeper.Params.Get(ctx) require.NoError(t, err) params.VotePeriod = 1 - keepers.OracleKeeper.Params.Set(ctx, params) + require.NoError(t, keepers.OracleKeeper.Params.Set(ctx, params)) exchangeRates := types.ExchangeRateTuples{ { @@ -116,7 +116,7 @@ func TestAggregatePrevoteVote(t *testing.T) { params, err := keepers.OracleKeeper.Params.Get(ctx) require.NoError(t, err) params.VotePeriod = 1 - keepers.OracleKeeper.Params.Set(ctx, params) + require.NoError(t, keepers.OracleKeeper.Params.Set(ctx, params)) salt := "1" exchangeRates := types.ExchangeRateTuples{ diff --git a/x/oracle/keeper/params.go b/x/oracle/keeper/params.go index a0867bb3..ff3b7b50 100644 --- a/x/oracle/keeper/params.go +++ b/x/oracle/keeper/params.go @@ -11,7 +11,7 @@ import ( // UpdateParams updates the oracle parameters func (k Keeper) UpdateParams(ctx sdk.Context, params types.Params) { - k.Params.Set(ctx, params) + _ = k.Params.Set(ctx, params) } // VotePeriod returns the number of blocks during which voting takes place. diff --git a/x/oracle/keeper/params_test.go b/x/oracle/keeper/params_test.go index 0cd1198c..483a3984 100644 --- a/x/oracle/keeper/params_test.go +++ b/x/oracle/keeper/params_test.go @@ -18,7 +18,7 @@ func TestParams(t *testing.T) { ctx := chain.GetContext() // Test default params setting - keepers.OracleKeeper.Params.Set(ctx, types.DefaultParams()) + require.NoError(t, keepers.OracleKeeper.Params.Set(ctx, types.DefaultParams())) params, err := keepers.OracleKeeper.Params.Get(ctx) require.NoError(t, err) require.NotNil(t, params) @@ -49,7 +49,7 @@ func TestParams(t *testing.T) { MinValidPerWindow: minValidPerWindow, ValidatorFeeRatio: minFeeRatio, } - keepers.OracleKeeper.Params.Set(ctx, newParams) + require.NoError(t, keepers.OracleKeeper.Params.Set(ctx, newParams)) storedParams, err := keepers.OracleKeeper.Params.Get(ctx) require.NoError(t, err) diff --git a/x/oracle/keeper/querier.go b/x/oracle/keeper/querier.go index a3eba6e1..31997f42 100644 --- a/x/oracle/keeper/querier.go +++ b/x/oracle/keeper/querier.go @@ -79,9 +79,8 @@ func (q querier) ExchangeRateTwap(c context.Context, req *types.QueryExchangeRat // ExchangeRates queries exchange rates of all pairs func (q querier) ExchangeRates(ctx context.Context, _ *types.QueryExchangeRatesRequest) (*types.QueryExchangeRatesResponse, error) { - var exchangeRates types.ExchangeRateTuples - q.Keeper.ExchangeRates.Walk(ctx, nil, func(key asset.Pair, value types.DatedPrice) (bool, error) { + _ = q.Keeper.ExchangeRates.Walk(ctx, nil, func(key asset.Pair, value types.DatedPrice) (bool, error) { exchangeRates = append(exchangeRates, types.ExchangeRateTuple{ Pair: key, ExchangeRate: value.ExchangeRate, diff --git a/x/oracle/keeper/querier_test.go b/x/oracle/keeper/querier_test.go index 1ec833f0..2029baf0 100644 --- a/x/oracle/keeper/querier_test.go +++ b/x/oracle/keeper/querier_test.go @@ -38,7 +38,10 @@ func TestQueryExchangeRate(t *testing.T) { querier := keeper.NewQuerier(keepers.OracleKeeper) rate := math.LegacyNewDec(1700) - keepers.OracleKeeper.ExchangeRates.Set(ctx, asset.Registry.Pair(denoms.ETH, denoms.NUSD), types.DatedPrice{ExchangeRate: rate, CreatedBlock: uint64(ctx.BlockHeight())}) + require.NoError(t, keepers.OracleKeeper.ExchangeRates.Set(ctx, asset.Registry.Pair(denoms.ETH, denoms.NUSD), types.DatedPrice{ + ExchangeRate: rate, + CreatedBlock: uint64(ctx.BlockHeight()), + })) // empty request _, err := querier.ExchangeRate(ctx, nil) @@ -65,7 +68,7 @@ func TestQueryMissCounter(t *testing.T) { } missCounter := uint64(1) - keepers.OracleKeeper.MissCounters.Set(ctx, ValAddrs[0], missCounter) + require.NoError(t, keepers.OracleKeeper.MissCounters.Set(ctx, ValAddrs[0], missCounter)) // empty request _, err := querier.MissCounter(ctx, nil) @@ -86,8 +89,14 @@ func TestQueryExchangeRates(t *testing.T) { querier := keeper.NewQuerier(keepers.OracleKeeper) rate := math.LegacyNewDec(1700) - keepers.OracleKeeper.ExchangeRates.Set(ctx, asset.Registry.Pair(denoms.BTC, denoms.NUSD), types.DatedPrice{ExchangeRate: rate, CreatedBlock: uint64(ctx.BlockHeight())}) - keepers.OracleKeeper.ExchangeRates.Set(ctx, asset.Registry.Pair(denoms.ETH, denoms.NUSD), types.DatedPrice{ExchangeRate: rate, CreatedBlock: uint64(ctx.BlockHeight())}) + require.NoError(t, keepers.OracleKeeper.ExchangeRates.Set(ctx, asset.Registry.Pair(denoms.BTC, denoms.NUSD), types.DatedPrice{ + ExchangeRate: rate, + CreatedBlock: uint64(ctx.BlockHeight()), + })) + require.NoError(t, keepers.OracleKeeper.ExchangeRates.Set(ctx, asset.Registry.Pair(denoms.ETH, denoms.NUSD), types.DatedPrice{ + ExchangeRate: rate, + CreatedBlock: uint64(ctx.BlockHeight()), + })) res, err := querier.ExchangeRates(ctx, &types.QueryExchangeRatesRequest{}) require.NoError(t, err) @@ -194,7 +203,7 @@ func TestCalcTwap(t *testing.T) { ValidatorFeeRatio: types.DefaultValidatorFeeRatio, } - keepers.OracleKeeper.Params.Set(ctx, newParams) + require.NoError(t, keepers.OracleKeeper.Params.Set(ctx, newParams)) ctx = ctx.WithBlockTime(time.UnixMilli(0)) for _, reserve := range tc.priceSnapshots { ctx = ctx.WithBlockTime(time.UnixMilli(reserve.TimestampMs)) @@ -203,7 +212,7 @@ func TestCalcTwap(t *testing.T) { ctx = ctx.WithBlockTime(tc.currentBlockTime).WithBlockHeight(tc.currentBlockHeight) - price, err := querier.ExchangeRateTwap(sdk.WrapSDKContext(ctx), &types.QueryExchangeRateRequest{Pair: asset.Registry.Pair(denoms.BTC, denoms.NUSD)}) + price, err := querier.ExchangeRateTwap(ctx, &types.QueryExchangeRateRequest{Pair: asset.Registry.Pair(denoms.BTC, denoms.NUSD)}) require.NoError(t, err) require.EqualValuesf(t, tc.expectedPrice, price.ExchangeRate, @@ -219,9 +228,18 @@ func TestQueryActives(t *testing.T) { queryClient := keeper.NewQuerier(keepers.OracleKeeper) rate := math.LegacyNewDec(1700) - keepers.OracleKeeper.ExchangeRates.Set(ctx, asset.Registry.Pair(denoms.BTC, denoms.NUSD), types.DatedPrice{ExchangeRate: rate, CreatedBlock: uint64(ctx.BlockHeight())}) - keepers.OracleKeeper.ExchangeRates.Set(ctx, asset.Registry.Pair(denoms.NIBI, denoms.NUSD), types.DatedPrice{ExchangeRate: rate, CreatedBlock: uint64(ctx.BlockHeight())}) - keepers.OracleKeeper.ExchangeRates.Set(ctx, asset.Registry.Pair(denoms.ETH, denoms.NUSD), types.DatedPrice{ExchangeRate: rate, CreatedBlock: uint64(ctx.BlockHeight())}) + require.NoError(t, keepers.OracleKeeper.ExchangeRates.Set(ctx, asset.Registry.Pair(denoms.BTC, denoms.NUSD), types.DatedPrice{ + ExchangeRate: rate, + CreatedBlock: uint64(ctx.BlockHeight()), + })) + require.NoError(t, keepers.OracleKeeper.ExchangeRates.Set(ctx, asset.Registry.Pair(denoms.NIBI, denoms.NUSD), types.DatedPrice{ + ExchangeRate: rate, + CreatedBlock: uint64(ctx.BlockHeight()), + })) + require.NoError(t, keepers.OracleKeeper.ExchangeRates.Set(ctx, asset.Registry.Pair(denoms.ETH, denoms.NUSD), types.DatedPrice{ + ExchangeRate: rate, + CreatedBlock: uint64(ctx.BlockHeight()), + })) res, err := queryClient.Actives(ctx, &types.QueryActivesRequest{}) require.NoError(t, err) @@ -249,7 +267,7 @@ func TestQueryFeederDelegation(t *testing.T) { ValAddrs[i] = sdk.ValAddress(vals[i].Address) } - keepers.OracleKeeper.FeederDelegations.Set(ctx, ValAddrs[0], AccAddrs[1]) + require.NoError(t, keepers.OracleKeeper.FeederDelegations.Set(ctx, ValAddrs[0], AccAddrs[1])) // empty request _, err := querier.FeederDelegation(ctx, nil) @@ -276,9 +294,9 @@ func TestQueryAggregatePrevote(t *testing.T) { } prevote1 := types.NewAggregateExchangeRatePrevote(types.AggregateVoteHash{}, ValAddrs[0], 0) - keepers.OracleKeeper.Prevotes.Set(ctx, ValAddrs[0], prevote1) + require.NoError(t, keepers.OracleKeeper.Prevotes.Set(ctx, ValAddrs[0], prevote1)) prevote2 := types.NewAggregateExchangeRatePrevote(types.AggregateVoteHash{}, ValAddrs[1], 0) - keepers.OracleKeeper.Prevotes.Set(ctx, ValAddrs[1], prevote2) + require.NoError(t, keepers.OracleKeeper.Prevotes.Set(ctx, ValAddrs[1], prevote2)) // validator 0 address params res, err := querier.AggregatePrevote(ctx, &types.QueryAggregatePrevoteRequest{ @@ -312,11 +330,11 @@ func TestQueryAggregatePrevotes(t *testing.T) { } prevote1 := types.NewAggregateExchangeRatePrevote(types.AggregateVoteHash{}, ValAddrs[0], 0) - keepers.OracleKeeper.Prevotes.Set(ctx, ValAddrs[0], prevote1) + require.NoError(t, keepers.OracleKeeper.Prevotes.Set(ctx, ValAddrs[0], prevote1)) prevote2 := types.NewAggregateExchangeRatePrevote(types.AggregateVoteHash{}, ValAddrs[1], 0) - keepers.OracleKeeper.Prevotes.Set(ctx, ValAddrs[1], prevote2) + require.NoError(t, keepers.OracleKeeper.Prevotes.Set(ctx, ValAddrs[1], prevote2)) prevote3 := types.NewAggregateExchangeRatePrevote(types.AggregateVoteHash{}, ValAddrs[2], 0) - keepers.OracleKeeper.Prevotes.Set(ctx, ValAddrs[2], prevote3) + require.NoError(t, keepers.OracleKeeper.Prevotes.Set(ctx, ValAddrs[2], prevote3)) expectedPrevotes := []types.AggregateExchangeRatePrevote{prevote1, prevote2, prevote3} // sort.SliceStable(expectedPrevotes, func(i, j int) bool { @@ -341,9 +359,9 @@ func TestQueryAggregateVote(t *testing.T) { } vote1 := types.NewAggregateExchangeRateVote(types.ExchangeRateTuples{{Pair: "", ExchangeRate: math.LegacyOneDec()}}, ValAddrs[0]) - keepers.OracleKeeper.Votes.Set(ctx, ValAddrs[0], vote1) + require.NoError(t, keepers.OracleKeeper.Votes.Set(ctx, ValAddrs[0], vote1)) vote2 := types.NewAggregateExchangeRateVote(types.ExchangeRateTuples{{Pair: "", ExchangeRate: math.LegacyOneDec()}}, ValAddrs[1]) - keepers.OracleKeeper.Votes.Set(ctx, ValAddrs[1], vote2) + require.NoError(t, keepers.OracleKeeper.Votes.Set(ctx, ValAddrs[1], vote2)) // empty request _, err := querier.AggregateVote(ctx, nil) @@ -377,11 +395,11 @@ func TestQueryAggregateVotes(t *testing.T) { } vote1 := types.NewAggregateExchangeRateVote(types.ExchangeRateTuples{{Pair: "", ExchangeRate: math.LegacyOneDec()}}, ValAddrs[0]) - keepers.OracleKeeper.Votes.Set(ctx, ValAddrs[0], vote1) + require.NoError(t, keepers.OracleKeeper.Votes.Set(ctx, ValAddrs[0], vote1)) vote2 := types.NewAggregateExchangeRateVote(types.ExchangeRateTuples{{Pair: "", ExchangeRate: math.LegacyOneDec()}}, ValAddrs[1]) - keepers.OracleKeeper.Votes.Set(ctx, ValAddrs[1], vote2) + require.NoError(t, keepers.OracleKeeper.Votes.Set(ctx, ValAddrs[1], vote2)) vote3 := types.NewAggregateExchangeRateVote(types.ExchangeRateTuples{{Pair: "", ExchangeRate: math.LegacyOneDec()}}, ValAddrs[2]) - keepers.OracleKeeper.Votes.Set(ctx, ValAddrs[2], vote3) + require.NoError(t, keepers.OracleKeeper.Votes.Set(ctx, ValAddrs[2], vote3)) expectedVotes := []types.AggregateExchangeRateVote{vote1, vote2, vote3} // sort.SliceStable(expectedVotes, func(i, j int) bool { @@ -404,7 +422,7 @@ func TestQueryVoteTargets(t *testing.T) { voteTargets := []asset.Pair{"denom1:denom2", "denom3:denom4", "denom5:denom6"} for _, target := range voteTargets { - keepers.OracleKeeper.WhitelistedPairs.Set(ctx, target) + require.NoError(t, keepers.OracleKeeper.WhitelistedPairs.Set(ctx, target)) } res, err := querier.VoteTargets(ctx, &types.QueryVoteTargetsRequest{}) diff --git a/x/oracle/keeper/reward.go b/x/oracle/keeper/reward.go index a81e5cbb..bca8afad 100644 --- a/x/oracle/keeper/reward.go +++ b/x/oracle/keeper/reward.go @@ -18,11 +18,14 @@ func (k Keeper) AllocateRewards(ctx sdk.Context, funderModule string, totalCoins if err != nil { return err } - k.Rewards.Set(ctx, id, types.Rewards{ + err = k.Rewards.Set(ctx, id, types.Rewards{ Id: id, VotePeriods: votePeriods, Coins: votePeriodCoins, }) + if err != nil { + return err + } return k.bankKeeper.SendCoinsFromModuleToModule(ctx, funderModule, types.ModuleName, totalCoins) } @@ -50,14 +53,18 @@ func (k Keeper) rewardWinners( } rewardPortion, _ := totalRewards.MulDec(math.LegacyNewDec(validatorPerformance.RewardWeight).QuoInt64(totalRewardWeight)).TruncateDecimal() - k.distrKeeper.AllocateTokensToValidator(ctx, validator, sdk.NewDecCoinsFromCoins(rewardPortion...)) + err = k.distrKeeper.AllocateTokensToValidator(ctx, validator, sdk.NewDecCoinsFromCoins(rewardPortion...)) + if err != nil { + continue + } + distributedRewards = distributedRewards.Add(rewardPortion...) } // Move distributed reward to distribution module err := k.bankKeeper.SendCoinsFromModuleToModule(ctx, types.ModuleName, k.distrModuleName, distributedRewards) if err != nil { - k.Logger(ctx).Error("Failed to send coins to distribution module", "err", err) + k.Logger(ctx).Error("failed to send coins to distribution module", "err", err) } } @@ -76,7 +83,7 @@ func (k Keeper) GatherRewardsForVotePeriod(ctx sdk.Context) sdk.Coins { for _, rewardId := range keys { pairReward, err := k.Rewards.Get(ctx, rewardId) if err != nil { - k.Logger(ctx).Error("Failed to get reward", "err", err) + k.Logger(ctx).Error("failed to get reward", "err", err) continue } coins = coins.Add(pairReward.Coins...) @@ -87,10 +94,13 @@ func (k Keeper) GatherRewardsForVotePeriod(ctx sdk.Context) sdk.Coins { // If the distribution period count drops to 0: the reward instance is removed. err := k.Rewards.Remove(ctx, rewardId) if err != nil { - k.Logger(ctx).Error("Failed to delete pair reward", "err", err) + k.Logger(ctx).Error("failed to delete pair reward", "err", err) } } else { - k.Rewards.Set(ctx, rewardId, pairReward) + err := k.Rewards.Set(ctx, rewardId, pairReward) + if err != nil { + k.Logger(ctx).Error("failed to set pair reward", "err", err) + } } } diff --git a/x/oracle/keeper/reward_test.go b/x/oracle/keeper/reward_test.go index 7c8e3590..c6774657 100644 --- a/x/oracle/keeper/reward_test.go +++ b/x/oracle/keeper/reward_test.go @@ -8,12 +8,13 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/require" + minttypes "github.com/cosmos/cosmos-sdk/x/mint/types" + e2eTesting "github.com/archway-network/archway/e2e/testing" "github.com/archway-network/archway/x/oracle/asset" "github.com/archway-network/archway/x/oracle/denoms" "github.com/archway-network/archway/x/oracle/keeper" "github.com/archway-network/archway/x/oracle/types" - minttypes "github.com/cosmos/cosmos-sdk/x/mint/types" ) func TestKeeperRewardsDistributionMultiVotePeriods(t *testing.T) { @@ -32,7 +33,7 @@ func TestKeeperRewardsDistributionMultiVotePeriods(t *testing.T) { params, err := keepers.OracleKeeper.Params.Get(ctx) require.NoError(t, err) params.VotePeriod = 1 - keepers.OracleKeeper.Params.Set(ctx, params) + require.NoError(t, keepers.OracleKeeper.Params.Set(ctx, params)) msgServer := keeper.NewMsgServerImpl(keepers.OracleKeeper) diff --git a/x/oracle/keeper/slash.go b/x/oracle/keeper/slash.go index 0e461fe7..639e2794 100644 --- a/x/oracle/keeper/slash.go +++ b/x/oracle/keeper/slash.go @@ -20,7 +20,7 @@ func (k Keeper) SlashAndResetMissCounters(ctx sdk.Context) { slashFraction := k.SlashFraction(ctx) powerReduction := k.StakingKeeper.PowerReduction(ctx) - k.MissCounters.Walk(ctx, nil, func(operatorBytes []byte, missCounter uint64) (bool, error) { + _ = k.MissCounters.Walk(ctx, nil, func(operatorBytes []byte, missCounter uint64) (bool, error) { operator := sdk.ValAddress(operatorBytes) // Calculate valid vote rate; (SlashWindow - MissCounter)/SlashWindow @@ -32,26 +32,34 @@ func (k Keeper) SlashAndResetMissCounters(ctx sdk.Context) { if validVoteRate.LT(minValidPerWindow) { validator, err := k.StakingKeeper.Validator(ctx, operator) if err != nil { - k.Logger(ctx).Error("fail to get validator", "operator", operator) + k.Logger(ctx).Error("failed to get validator", "operator", operator) } if validator.IsBonded() && !validator.IsJailed() { consAddr, err := validator.GetConsAddr() if err != nil { - k.Logger(ctx).Error("fail to get consensus address", "validator", validator.GetOperator()) + k.Logger(ctx).Error("failed to get consensus address", "validator", validator.GetOperator()) return false, nil } - k.slashingKeeper.Slash( + err = k.slashingKeeper.Slash( ctx, consAddr, slashFraction, validator.GetConsensusPower(powerReduction), distributionHeight, ) + if err != nil { + k.Logger(ctx).Error("failed to slash validator", "validator", validator.GetOperator()) + return false, nil + } k.Logger(ctx).Info("oracle slash", "validator", string(consAddr), "fraction", slashFraction.String()) - k.slashingKeeper.Jail(ctx, consAddr) + err = k.slashingKeeper.Jail(ctx, consAddr) + if err != nil { + k.Logger(ctx).Error("failed to jail validator", "validator", validator.GetOperator()) + return false, nil + } } } err := k.MissCounters.Remove(ctx, operator) if err != nil { - k.Logger(ctx).Error("fail to delete miss counter", "operator", operator.String(), "error", err) + k.Logger(ctx).Error("failed to delete miss counter", "operator", operator.String(), "error", err) } return false, nil }) diff --git a/x/oracle/keeper/slash_test.go b/x/oracle/keeper/slash_test.go index 4efca103..3a28e78c 100644 --- a/x/oracle/keeper/slash_test.go +++ b/x/oracle/keeper/slash_test.go @@ -43,9 +43,10 @@ func TestSlashAndResetMissCounters(t *testing.T) { params, err := keepers.OracleKeeper.Params.Get(ctx) require.NoError(t, err) params.VotePeriod = 1 - keepers.OracleKeeper.Params.Set(ctx, params) + require.NoError(t, keepers.OracleKeeper.Params.Set(ctx, params)) - keepers.StakingKeeper.EndBlocker(ctx) + _, err = keepers.StakingKeeper.EndBlocker(ctx) + require.NoError(t, err) stakingparams, err := keepers.StakingKeeper.GetParams(ctx) require.NoError(t, err) @@ -70,16 +71,17 @@ func TestSlashAndResetMissCounters(t *testing.T) { slashFraction := keepers.OracleKeeper.SlashFraction(ctx) minValidVotes := keepers.OracleKeeper.MinValidPerWindow(ctx).MulInt64(votePeriodsPerWindow).Ceil().TruncateInt64() // Case 1, no slash - keepers.OracleKeeper.MissCounters.Set(ctx, ValAddrs[0], uint64(votePeriodsPerWindow-minValidVotes)) + require.NoError(t, keepers.OracleKeeper.MissCounters.Set(ctx, ValAddrs[0], uint64(votePeriodsPerWindow-minValidVotes))) keepers.OracleKeeper.SlashAndResetMissCounters(ctx) - keepers.StakingKeeper.EndBlocker(ctx) + _, err = keepers.StakingKeeper.EndBlocker(ctx) + require.NoError(t, err) validator, _ = keepers.StakingKeeper.GetValidator(ctx, ValAddrs[0]) require.NoError(t, err) require.Equal(t, amt, validator.GetBondedTokens()) // Case 2, slash - keepers.OracleKeeper.MissCounters.Set(ctx, ValAddrs[0], uint64(votePeriodsPerWindow-minValidVotes+1)) + require.NoError(t, keepers.OracleKeeper.MissCounters.Set(ctx, ValAddrs[0], uint64(votePeriodsPerWindow-minValidVotes+1))) keepers.OracleKeeper.SlashAndResetMissCounters(ctx) validator, _ = keepers.StakingKeeper.GetValidator(ctx, ValAddrs[0]) require.Equal(t, amt.Sub(slashFraction.MulInt(amt).TruncateInt()), validator.GetBondedTokens()) @@ -90,9 +92,9 @@ func TestSlashAndResetMissCounters(t *testing.T) { validator.Status = stakingtypes.Unbonded validator.Jailed = false validator.Tokens = amt - keepers.StakingKeeper.SetValidator(ctx, validator) + require.NoError(t, keepers.StakingKeeper.SetValidator(ctx, validator)) - keepers.OracleKeeper.MissCounters.Set(ctx, ValAddrs[0], uint64(votePeriodsPerWindow-minValidVotes+1)) + require.NoError(t, keepers.OracleKeeper.MissCounters.Set(ctx, ValAddrs[0], uint64(votePeriodsPerWindow-minValidVotes+1))) keepers.OracleKeeper.SlashAndResetMissCounters(ctx) validator, _ = keepers.StakingKeeper.GetValidator(ctx, ValAddrs[0]) require.Equal(t, amt, validator.Tokens) @@ -103,9 +105,9 @@ func TestSlashAndResetMissCounters(t *testing.T) { validator.Status = stakingtypes.Bonded validator.Jailed = true validator.Tokens = amt - keepers.StakingKeeper.SetValidator(ctx, validator) + require.NoError(t, keepers.StakingKeeper.SetValidator(ctx, validator)) - keepers.OracleKeeper.MissCounters.Set(ctx, ValAddrs[0], uint64(votePeriodsPerWindow-minValidVotes+1)) + require.NoError(t, keepers.OracleKeeper.MissCounters.Set(ctx, ValAddrs[0], uint64(votePeriodsPerWindow-minValidVotes+1))) keepers.OracleKeeper.SlashAndResetMissCounters(ctx) validator, _ = keepers.StakingKeeper.GetValidator(ctx, ValAddrs[0]) require.Equal(t, amt, validator.Tokens) @@ -130,8 +132,8 @@ func TestInvalidVotesSlashing(t *testing.T) { require.NoError(t, err) params.VotePeriod = 1 params.Whitelist = []asset.Pair{asset.Registry.Pair(denoms.ATOM, denoms.USD)} - keepers.OracleKeeper.Params.Set(ctx, params) - keepers.OracleKeeper.WhitelistedPairs.Set(ctx, asset.Registry.Pair(denoms.ATOM, denoms.USD)) + require.NoError(t, keepers.OracleKeeper.Params.Set(ctx, params)) + require.NoError(t, keepers.OracleKeeper.WhitelistedPairs.Set(ctx, asset.Registry.Pair(denoms.ATOM, denoms.USD))) votePeriodsPerWindow := math.LegacyNewDec(int64(keepers.OracleKeeper.SlashWindow(ctx))).QuoInt64(int64(keepers.OracleKeeper.VotePeriod(ctx))).TruncateInt64() slashFraction := keepers.OracleKeeper.SlashFraction(ctx) @@ -172,6 +174,7 @@ func TestInvalidVotesSlashing(t *testing.T) { } validator, err := keepers.StakingKeeper.GetValidator(ctx, ValAddrs[1]) + require.NoError(t, err) require.Equal(t, testStakingAmt, validator.GetBondedTokens()) // one more miss vote will inccur ValAddrs[1] slashing @@ -225,7 +228,7 @@ func TestWhitelistSlashing(t *testing.T) { params, err := keepers.OracleKeeper.Params.Get(ctx) require.NoError(t, err) params.VotePeriod = 1 - keepers.OracleKeeper.Params.Set(ctx, params) + require.NoError(t, keepers.OracleKeeper.Params.Set(ctx, params)) votePeriodsPerSlashWindow := math.LegacyNewDec(int64(keepers.OracleKeeper.SlashWindow(ctx))).QuoInt64(int64(keepers.OracleKeeper.VotePeriod(ctx))).TruncateInt64() minValidVotePeriodsPerWindow := keepers.OracleKeeper.MinValidPerWindow(ctx) @@ -283,7 +286,7 @@ func TestNotPassedBallotSlashing(t *testing.T) { require.NoError(t, err) params.VotePeriod = 1 params.Whitelist = []asset.Pair{asset.Registry.Pair(denoms.ATOM, denoms.USD)} - keepers.OracleKeeper.Params.Set(ctx, params) + require.NoError(t, keepers.OracleKeeper.Params.Set(ctx, params)) // clear tobin tax to reset vote targets err = keepers.OracleKeeper.WhitelistedPairs.Clear(ctx, nil) @@ -336,7 +339,7 @@ func TestAbstainSlashing(t *testing.T) { params.VotePeriod = 1 // reset whitelisted pairs params.Whitelist = []asset.Pair{asset.Registry.Pair(denoms.ATOM, denoms.USD)} - keepers.OracleKeeper.Params.Set(ctx, params) + require.NoError(t, keepers.OracleKeeper.Params.Set(ctx, params)) err = keepers.OracleKeeper.WhitelistedPairs.Clear(ctx, nil) require.NoError(t, err) diff --git a/x/oracle/keeper/update_exchange_rates.go b/x/oracle/keeper/update_exchange_rates.go index 6196dcc4..edd846b5 100644 --- a/x/oracle/keeper/update_exchange_rates.go +++ b/x/oracle/keeper/update_exchange_rates.go @@ -58,12 +58,16 @@ func (k Keeper) incrementMissCounters( if err != nil { counter = 0 } - k.MissCounters.Set( + counter = counter + uint64(validatorPerformance.MissCount) + err = k.MissCounters.Set( ctx, validatorPerformance.ValAddress, - counter+uint64(validatorPerformance.MissCount), + counter, ) - - k.Logger(ctx).Info("vote miss", "validator", validatorPerformance.ValAddress.String()) + if err == nil { + k.Logger(ctx).Info("vote miss", "validator", validatorPerformance.ValAddress.String()) + } else { + k.Logger(ctx).Error("failed to set MissCounter", "validator", validatorPerformance.ValAddress.String(), "counter", counter, "error", err) + } } } } @@ -113,7 +117,7 @@ func (k Keeper) getPairVotes( func (k Keeper) ClearExchangeRates(ctx sdk.Context, pairVotes map[asset.Pair]types.ExchangeRateVotes) { params, _ := k.Params.Get(ctx) - k.ExchangeRates.Walk(ctx, nil, func(key asset.Pair, _ types.DatedPrice) (bool, error) { + _ = k.ExchangeRates.Walk(ctx, nil, func(key asset.Pair, _ types.DatedPrice) (bool, error) { _, isValid := pairVotes[key] previousExchangeRate, _ := k.ExchangeRates.Get(ctx, key) isExpired := previousExchangeRate.CreatedBlock+params.ExpirationBlocks <= uint64(ctx.BlockHeight()) diff --git a/x/oracle/keeper/update_exchange_rates_test.go b/x/oracle/keeper/update_exchange_rates_test.go index b1f2dfa8..d10d7bac 100644 --- a/x/oracle/keeper/update_exchange_rates_test.go +++ b/x/oracle/keeper/update_exchange_rates_test.go @@ -52,7 +52,7 @@ func TestOracleThreshold(t *testing.T) { require.NoError(t, err) params.VotePeriod = 1 params.ExpirationBlocks = 0 - keepers.OracleKeeper.Params.Set(ctx, params) + require.NoError(t, keepers.OracleKeeper.Params.Set(ctx, params)) // Case 1. // Less than the threshold signs, exchange rate consensus fails @@ -141,7 +141,7 @@ func TestResetExchangeRates(t *testing.T) { params, err := keepers.OracleKeeper.Params.Get(ctx) require.NoError(t, err) params.ExpirationBlocks = 10 - keepers.OracleKeeper.Params.Set(ctx, params) + require.NoError(t, keepers.OracleKeeper.Params.Set(ctx, params)) // Post a price at block 1 keepers.OracleKeeper.SetPrice(ctx.WithBlockHeight(1), pair, testExchangeRate) @@ -181,7 +181,7 @@ func TestOracleTally(t *testing.T) { params, err := keepers.OracleKeeper.Params.Get(ctx) require.NoError(t, err) params.VotePeriod = 1 - keepers.OracleKeeper.Params.Set(ctx, params) + require.NoError(t, keepers.OracleKeeper.Params.Set(ctx, params)) for i, rate := range rates { decExchangeRate := sdkmath.LegacyNewDecWithPrec(int64(rate*math.Pow10(OracleDecPrecision)), int64(OracleDecPrecision)) @@ -283,7 +283,7 @@ func TestOracleRewardBand(t *testing.T) { require.NoError(t, err) params.VotePeriod = 1 params.Whitelist = []asset.Pair{asset.Registry.Pair(denoms.ATOM, denoms.USD)} - keepers.OracleKeeper.Params.Set(ctx, params) + require.NoError(t, keepers.OracleKeeper.Params.Set(ctx, params)) // clear pairs to reset vote targets err = keepers.OracleKeeper.WhitelistedPairs.Clear(ctx, nil) @@ -433,7 +433,7 @@ func TestOracleExchangeRate(t *testing.T) { params, err := keepers.OracleKeeper.Params.Get(ctx) require.NoError(t, err) params.VotePeriod = 1 - keepers.OracleKeeper.Params.Set(ctx, params) + require.NoError(t, keepers.OracleKeeper.Params.Set(ctx, params)) atomUsdExchangeRate := sdkmath.LegacyNewDec(1000000) ethUsdExchangeRate := sdkmath.LegacyNewDec(1000000) @@ -507,7 +507,7 @@ func TestOracleRandomPrices(t *testing.T) { params, err := keepers.OracleKeeper.Params.Get(ctx) require.NoError(t, err) params.VotePeriod = 1 - keepers.OracleKeeper.Params.Set(ctx, params) + require.NoError(t, keepers.OracleKeeper.Params.Set(ctx, params)) for i := 0; i < 100; i++ { for _, val := range vals { @@ -538,7 +538,7 @@ func TestWhitelistedPairs(t *testing.T) { params, err := keepers.OracleKeeper.Params.Get(ctx) require.NoError(t, err) params.VotePeriod = 1 - keepers.OracleKeeper.Params.Set(ctx, params) + require.NoError(t, keepers.OracleKeeper.Params.Set(ctx, params)) t.Log("whitelist ONLY atom:usd") err = keepers.OracleKeeper.WhitelistedPairs.Clear(ctx, nil) @@ -558,7 +558,7 @@ func TestWhitelistedPairs(t *testing.T) { t.Log("whitelist btc:usd for next vote period") params.Whitelist = []asset.Pair{asset.Registry.Pair(denoms.ATOM, denoms.USD), asset.Registry.Pair(denoms.BTC, denoms.USD)} - keepers.OracleKeeper.Params.Set(ctx, params) + require.NoError(t, keepers.OracleKeeper.Params.Set(ctx, params)) keepers.OracleKeeper.UpdateExchangeRates(ctx) t.Log("assert: no miss counts for all vals") @@ -602,7 +602,7 @@ func TestWhitelistedPairs(t *testing.T) { t.Log("delete btc:usd for next vote period") params.Whitelist = []asset.Pair{asset.Registry.Pair(denoms.ATOM, denoms.USD)} - keepers.OracleKeeper.Params.Set(ctx, params) + require.NoError(t, keepers.OracleKeeper.Params.Set(ctx, params)) perfs := keepers.OracleKeeper.UpdateExchangeRates(ctx) t.Log("validators 0-3 all voted -> expect win") diff --git a/x/oracle/keeper/whitelist.go b/x/oracle/keeper/whitelist.go index 49da1338..15154acd 100644 --- a/x/oracle/keeper/whitelist.go +++ b/x/oracle/keeper/whitelist.go @@ -48,7 +48,10 @@ func (k Keeper) RefreshWhitelist(ctx sdk.Context, nextWhitelist []asset.Pair, cu panic(err) } for _, pair := range nextWhitelist { - k.WhitelistedPairs.Set(ctx, pair) + err = k.WhitelistedPairs.Set(ctx, pair) + if err != nil { + panic(err) + } } } } diff --git a/x/oracle/keeper/whitelist_test.go b/x/oracle/keeper/whitelist_test.go index a14ac043..1bdac388 100644 --- a/x/oracle/keeper/whitelist_test.go +++ b/x/oracle/keeper/whitelist_test.go @@ -41,7 +41,7 @@ func TestKeeper_GetVoteTargets(t *testing.T) { expectedTargets := tc.in for _, target := range expectedTargets { - keepers.OracleKeeper.WhitelistedPairs.Set(ctx, target) + require.NoError(t, keepers.OracleKeeper.WhitelistedPairs.Set(ctx, target)) } var panicAssertFn func(t assert.TestingT, f assert.PanicTestFunc, msgAndArgs ...interface{}) bool @@ -68,7 +68,7 @@ func TestKeeper_GetVoteTargets(t *testing.T) { expectedTargets := []asset.Pair{"foo:bar", "whoo:whoo"} for _, target := range expectedTargets { - keepers.OracleKeeper.WhitelistedPairs.Set(ctx, target) + require.NoError(t, keepers.OracleKeeper.WhitelistedPairs.Set(ctx, target)) } targets, err := keepers.OracleKeeper.GetWhitelistedPairs(ctx) @@ -86,7 +86,7 @@ func TestIsWhitelistedPair(t *testing.T) { validPairs := []asset.Pair{"foo:bar", "xxx:yyy", "whoo:whoo"} for _, target := range validPairs { - keepers.OracleKeeper.WhitelistedPairs.Set(ctx, target) + require.NoError(t, keepers.OracleKeeper.WhitelistedPairs.Set(ctx, target)) flag, err := keepers.OracleKeeper.IsWhitelistedPair(ctx, target) require.NoError(t, err) require.True(t, flag) @@ -104,7 +104,7 @@ func TestUpdateWhitelist(t *testing.T) { currentWhitelist := set.New(asset.NewPair(denoms.NIBI, denoms.USD), asset.NewPair(denoms.BTC, denoms.USD)) for p := range currentWhitelist { - keepers.OracleKeeper.WhitelistedPairs.Set(ctx, p) + require.NoError(t, keepers.OracleKeeper.WhitelistedPairs.Set(ctx, p)) } nextWhitelist := set.New(asset.NewPair(denoms.NIBI, denoms.USD), asset.NewPair(denoms.BTC, denoms.USD)) diff --git a/x/oracle/module.go b/x/oracle/module.go index 68438d42..7e94fd1d 100644 --- a/x/oracle/module.go +++ b/x/oracle/module.go @@ -161,7 +161,10 @@ func (AppModule) BeginBlock(_ sdk.Context) {} // EndBlock returns the end blocker for the oracle module. func (am AppModule) EndBlock(ctx context.Context) ([]abci.ValidatorUpdate, error) { - EndBlocker(sdk.UnwrapSDKContext(ctx), am.keeper) + err := EndBlocker(sdk.UnwrapSDKContext(ctx), am.keeper) + if err != nil { + return nil, err + } return []abci.ValidatorUpdate{}, nil } diff --git a/x/oracle/types/codec.go b/x/oracle/types/codec.go index efe53d14..898a07b6 100644 --- a/x/oracle/types/codec.go +++ b/x/oracle/types/codec.go @@ -36,7 +36,7 @@ var ( // // The actual codec used for serialization should be provided to x/staking and // defined at the application level. - ModuleCdc = codec.NewAminoCodec(amino) + ModuleCdc = amino ) func init() { diff --git a/x/rewards/ante/fee_deduction.go b/x/rewards/ante/fee_deduction.go index 1512db27..963df3b9 100644 --- a/x/rewards/ante/fee_deduction.go +++ b/x/rewards/ante/fee_deduction.go @@ -173,14 +173,14 @@ func (dfd DeductFeeDecorator) deductFees(ctx sdk.Context, tx sdk.Tx, acc sdk.Acc rebateRatio := dfd.rewardsKeeper.TxFeeRebateRatio(ctx) if rebateRatio.IsZero() || !hasWasmMsgs { if err := dfd.bankKeeper.SendCoinsFromAccountToModule(ctx, acc.GetAddress(), authTypes.FeeCollectorName, fees); err != nil { - return errorsmod.Wrapf(sdkErrors.ErrInsufficientFunds, err.Error()) + return errorsmod.Wrapf(sdkErrors.ErrInsufficientFunds, "%s", err.Error()) } return nil } if !flatFees.Empty() { if err := dfd.bankKeeper.SendCoinsFromAccountToModule(ctx, acc.GetAddress(), rewardsTypes.ContractRewardCollector, flatFees); err != nil { - return errorsmod.Wrapf(sdkErrors.ErrInsufficientFunds, err.Error()) + return errorsmod.Wrapf(sdkErrors.ErrInsufficientFunds, "%s", err.Error()) } fees = fees.Sub(flatFees...) // reduce flatfees from the sent fees amount } @@ -190,17 +190,17 @@ func (dfd DeductFeeDecorator) deductFees(ctx sdk.Context, tx sdk.Tx, acc sdk.Acc if !authFees.Empty() { if err := dfd.bankKeeper.SendCoinsFromAccountToModule(ctx, acc.GetAddress(), authTypes.FeeCollectorName, authFees); err != nil { - return errorsmod.Wrapf(sdkErrors.ErrInsufficientFunds, err.Error()) + return errorsmod.Wrapf(sdkErrors.ErrInsufficientFunds, "%s", err.Error()) } // burn the auth fees. if err := dfd.bankKeeper.BurnCoins(ctx, authTypes.FeeCollectorName, authFees); err != nil { - return errorsmod.Wrapf(sdkErrors.ErrInsufficientFunds, err.Error()) + return errorsmod.Wrapf(sdkErrors.ErrInsufficientFunds, "%s", err.Error()) } } if !rewardsFees.Empty() { if err := dfd.bankKeeper.SendCoinsFromAccountToModule(ctx, acc.GetAddress(), rewardsTypes.ContractRewardCollector, rewardsFees); err != nil { - return errorsmod.Wrapf(sdkErrors.ErrInsufficientFunds, err.Error()) + return errorsmod.Wrapf(sdkErrors.ErrInsufficientFunds, "%s", err.Error()) } }