Skip to content

Commit

Permalink
lint(x/oracle): handle errors (#597)
Browse files Browse the repository at this point in the history
  • Loading branch information
pyncz authored Nov 19, 2024
1 parent afbe7c9 commit 866c106
Show file tree
Hide file tree
Showing 32 changed files with 265 additions and 145 deletions.
3 changes: 2 additions & 1 deletion app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
3 changes: 2 additions & 1 deletion e2e/ibchooks/ibchooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
6 changes: 3 additions & 3 deletions e2e/ibchooks/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion e2e/testing/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
1 change: 1 addition & 0 deletions types/math/dec.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"cosmossdk.io/math"

"github.com/archway-network/archway/types/errors"
)

Expand Down
3 changes: 2 additions & 1 deletion types/math/dec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
23 changes: 12 additions & 11 deletions x/oracle/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ 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"
"github.com/archway-network/archway/x/oracle/denoms"
"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`
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -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)
Expand Down
1 change: 0 additions & 1 deletion x/oracle/asset/pair.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion x/oracle/client/cli/gen_pricefeeder_delegation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
60 changes: 48 additions & 12 deletions x/oracle/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -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)
Expand Down
20 changes: 10 additions & 10 deletions x/oracle/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions x/oracle/keeper/ballot.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package keeper

import (
"fmt"
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"

Expand Down Expand Up @@ -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)
Expand Down
Loading

0 comments on commit 866c106

Please sign in to comment.