Skip to content

Commit

Permalink
fix: Upgrade handler for v0.4.0 (#459)
Browse files Browse the repository at this point in the history
* fix: Register ibcclient gov route

* ibc param keytable

* set ibc client in upgrade handler

* nolint space

* register param set in initParamsKeeper

* initgensis in upgrade handler

* set params in upgrade handler with logs

* set ibc client params in app.go

* move client setting to end of NewApp

* update consensus params in upgrade handler

* fix updating consensus params

* try rc12 handler

* remove registerUpgrade0_4_0Rc12

* ibc client update in handler

* put ibc client param setting back in app.go

* move client setting

* move client setting again

* move preblocker setting

* init restructure

* ibc param after initchain

* ibc param and preblock at end

* mm.PreBlock in oracle PreBlocker

* update preblocker

* move setpreblocker

* move preblocker in app package

* ibc client params in upgrade keeper

* update ibc params in both upgrade handler amd app.go

* initparamskeeper

* fix initparamskeeper

* vote period handler

* fix unit tests with vote period as 1

* comment
  • Loading branch information
rbajollari authored Jul 5, 2024
1 parent ea3355b commit 2195260
Show file tree
Hide file tree
Showing 13 changed files with 144 additions and 201 deletions.
22 changes: 9 additions & 13 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ import (
ibctransferkeeper "github.com/cosmos/ibc-go/v8/modules/apps/transfer/keeper"
ibctransfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
ibc "github.com/cosmos/ibc-go/v8/modules/core"
ibcclienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
ibcconnectiontypes "github.com/cosmos/ibc-go/v8/modules/core/03-connection/types"
ibcporttypes "github.com/cosmos/ibc-go/v8/modules/core/05-port/types"
ibcexported "github.com/cosmos/ibc-go/v8/modules/core/exported"
ibckeeper "github.com/cosmos/ibc-go/v8/modules/core/keeper"
Expand Down Expand Up @@ -736,12 +738,6 @@ func New(
app.SetPrepareProposal(proposalHandler.PrepareProposalHandler())
app.SetProcessProposal(proposalHandler.ProcessProposalHandler())

preBlockHandler := oracleabci.NewPreBlockHandler(
app.Logger(),
app.OracleKeeper,
)
app.SetPreBlocker(preBlockHandler.PreBlocker())

// initialize empty price feeder object to pass reference into vote extension handler
app.PriceFeeder = &pricefeeder.PriceFeeder{}
voteExtensionsHandler := oracleabci.NewVoteExtensionHandler(
Expand All @@ -759,6 +755,7 @@ func New(

// initialize BaseApp
app.SetInitChainer(app.InitChainer)
app.SetPreBlocker(app.PreBlocker)
app.SetBeginBlocker(app.BeginBlocker)
app.SetEndBlocker(app.EndBlocker)
app.setAnteHandler(txConfig)
Expand All @@ -781,11 +778,6 @@ func (app *App) Name() string { return app.BaseApp.Name() }
// GetBaseApp returns the base app of the application
func (app App) GetBaseApp() *baseapp.BaseApp { return app.BaseApp }

// PreBlocker application updates every pre block
func (app *App) PreBlocker(ctx sdk.Context, _ *abci.RequestFinalizeBlock) (*sdk.ResponsePreBlock, error) {
return app.mm.PreBlock(ctx)
}

// BeginBlocker application updates every begin block
func (app *App) BeginBlocker(ctx sdk.Context) (sdk.BeginBlock, error) {
return app.mm.BeginBlock(ctx)
Expand Down Expand Up @@ -1004,10 +996,14 @@ func initParamsKeeper(
paramsKeeper.Subspace(slashingtypes.ModuleName)
paramsKeeper.Subspace(govtypes.ModuleName)
paramsKeeper.Subspace(crisistypes.ModuleName)
paramsKeeper.Subspace(ibctransfertypes.ModuleName)
paramsKeeper.Subspace(ibcexported.ModuleName)
paramsKeeper.Subspace(oracletypes.ModuleName)

// register the key tables for legacy param subspaces
keyTable := ibcclienttypes.ParamKeyTable()
keyTable.RegisterParamSet(&ibcconnectiontypes.Params{})
paramsKeeper.Subspace(ibcexported.ModuleName).WithKeyTable(keyTable)
paramsKeeper.Subspace(ibctransfertypes.ModuleName).WithKeyTable(ibctransfertypes.ParamKeyTable())

return paramsKeeper
}

Expand Down
72 changes: 72 additions & 0 deletions app/preblocker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package app

import (
"encoding/json"
"fmt"

"cosmossdk.io/core/appmodule"
cometabci "github.com/cometbft/cometbft/abci/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ojo-network/ojo/x/oracle/abci"
)

// PreBlocker is run before finalize block to update the aggregrate exchange rate votes on the oracle module
// that were verified by the vote etension handler so that the exchange rate votes are available during the
// entire block execution (from BeginBlock). It will execute the preblockers of the other modules set in
// SetOrderPreBlockers as well.
func (app *App) PreBlocker(ctx sdk.Context, req *cometabci.RequestFinalizeBlock) (*sdk.ResponsePreBlock, error) {
if req == nil {
err := fmt.Errorf("preblocker received a nil request")
app.Logger().Error(err.Error())
return nil, err
}

// execute preblockers of modules in OrderPreBlockers first.
ctx = ctx.WithEventManager(sdk.NewEventManager())
paramsChanged := false
for _, moduleName := range app.mm.OrderPreBlockers {
if module, ok := app.mm.Modules[moduleName].(appmodule.HasPreBlocker); ok {
rsp, err := module.PreBlock(ctx)
if err != nil {
return nil, err
}
if rsp.IsConsensusParamsChanged() {
paramsChanged = true
}
}
}

res := &sdk.ResponsePreBlock{
ConsensusParamsChanged: paramsChanged,
}

if len(req.Txs) == 0 {
return res, nil
}
voteExtensionsEnabled := abci.VoteExtensionsEnabled(ctx)
if voteExtensionsEnabled {
var injectedVoteExtTx abci.AggregateExchangeRateVotes
if err := json.Unmarshal(req.Txs[0], &injectedVoteExtTx); err != nil {
app.Logger().Error("failed to decode injected vote extension tx", "err", err)
return nil, err
}

// set oracle exchange rate votes using the passed in context, which will make
// these votes available in the current block.
for _, exchangeRateVote := range injectedVoteExtTx.ExchangeRateVotes {
valAddr, err := sdk.ValAddressFromBech32(exchangeRateVote.Voter)
if err != nil {
app.Logger().Error("failed to get voter address", "err", err)
continue
}
app.OracleKeeper.SetAggregateExchangeRateVote(ctx, valAddr, exchangeRateVote)
}
}

app.Logger().Info(
"oracle preblocker executed",
"vote_extensions_enabled", voteExtensionsEnabled,
)

return res, nil
}
36 changes: 31 additions & 5 deletions app/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package app

import (
"context"
"fmt"

storetypes "cosmossdk.io/store/types"
circuittypes "cosmossdk.io/x/circuit/types"
upgradetypes "cosmossdk.io/x/upgrade/types"
tenderminttypes "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
Expand All @@ -20,7 +22,6 @@ import (
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/cosmos/ibc-go/v8/modules/core/exported"
gmptypes "github.com/ojo-network/ojo/x/gmp/types"

oraclekeeper "github.com/ojo-network/ojo/x/oracle/keeper"
Expand Down Expand Up @@ -209,10 +210,35 @@ func (app *App) registerUpgrade0_4_0(upgradeInfo upgradetypes.Plan) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
sdkCtx.Logger().Info("Upgrade handler execution", "name", planName)

// explicitly update the IBC 02-client params, adding the localhost client type
params := app.IBCKeeper.ClientKeeper.GetParams(sdkCtx)
params.AllowedClients = append(params.AllowedClients, exported.Localhost)
app.IBCKeeper.ClientKeeper.SetParams(sdkCtx, params)
// enable vote extensions after upgrade
consensusParamsKeeper := app.ConsensusParamsKeeper
currentParams, err := consensusParamsKeeper.Params(ctx, &consensustypes.QueryParamsRequest{})
if err != nil || currentParams == nil || currentParams.Params == nil {
panic(fmt.Sprintf("failed to retrieve existing consensus params in upgrade handler: %s", err))
}
currentParams.Params.Abci = &tenderminttypes.ABCIParams{
VoteExtensionsEnableHeight: sdkCtx.BlockHeight() + int64(4), // enable vote extensions 4 blocks after upgrade
}
_, err = consensusParamsKeeper.UpdateParams(ctx, &consensustypes.MsgUpdateParams{
Authority: consensusParamsKeeper.GetAuthority(),
Block: currentParams.Params.Block,
Evidence: currentParams.Params.Evidence,
Validator: currentParams.Params.Validator,
Abci: currentParams.Params.Abci,
})
if err != nil {
panic(fmt.Sprintf("failed to update consensus params : %s", err))
}
sdkCtx.Logger().Info(
"Successfully set VoteExtensionsEnableHeight",
"consensus_params",
currentParams.Params.String(),
)

// update vote period to 1 block
oracleKeeper := app.OracleKeeper
oracleKeeper.SetVotePeriod(sdkCtx, 1)

return app.mm.RunMigrations(ctx, app.configurator, fromVM)
},
)
Expand Down
18 changes: 9 additions & 9 deletions x/oracle/abci/endblocker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,9 @@ func (s *IntegrationTestSuite) TestEndBlockerValidatorRewards() {
s.Require().NoError(err)
currRewards3, err := app.DistrKeeper.GetValidatorCurrentRewards(ctx, valAddr3)
s.Require().NoError(err)
s.Require().Equal(sdk.NewInt64DecCoin("uojo", 142), currRewards1.Rewards[0])
s.Require().Equal(sdk.NewInt64DecCoin("uojo", 142), currRewards2.Rewards[0])
s.Require().Equal(sdk.NewInt64DecCoin("uojo", 142), currRewards3.Rewards[0])
s.Require().Equal(sdk.NewInt64DecCoin("uojo", 141), currRewards1.Rewards[0])
s.Require().Equal(sdk.NewInt64DecCoin("uojo", 141), currRewards2.Rewards[0])
s.Require().Equal(sdk.NewInt64DecCoin("uojo", 141), currRewards3.Rewards[0])

// update prevotes' block
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + preVoteBlockDiff)
Expand Down Expand Up @@ -282,9 +282,9 @@ func (s *IntegrationTestSuite) TestEndBlockerValidatorRewards() {
s.Require().NoError(err)
currRewards3, err = app.DistrKeeper.GetValidatorCurrentRewards(ctx, valAddr3)
s.Require().NoError(err)
s.Require().Equal(sdk.NewInt64DecCoin("uojo", 284), currRewards1.Rewards[0])
s.Require().Equal(sdk.NewInt64DecCoin("uojo", 275), currRewards2.Rewards[0])
s.Require().Equal(sdk.NewInt64DecCoin("uojo", 284), currRewards3.Rewards[0])
s.Require().Equal(sdk.NewInt64DecCoin("uojo", 235), currRewards1.Rewards[0])
s.Require().Equal(sdk.NewInt64DecCoin("uojo", 232), currRewards2.Rewards[0])
s.Require().Equal(sdk.NewInt64DecCoin("uojo", 235), currRewards3.Rewards[0])

// update prevotes' block
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + preVoteBlockDiff)
Expand Down Expand Up @@ -314,9 +314,9 @@ func (s *IntegrationTestSuite) TestEndBlockerValidatorRewards() {
s.Require().NoError(err)
currRewards3, err = app.DistrKeeper.GetValidatorCurrentRewards(ctx, valAddr3)
s.Require().NoError(err)
s.Require().Equal(sdk.NewInt64DecCoin("uojo", 426), currRewards1.Rewards[0])
s.Require().Equal(sdk.NewInt64DecCoin("uojo", 408), currRewards2.Rewards[0])
s.Require().Equal(sdk.NewInt64DecCoin("uojo", 426), currRewards3.Rewards[0])
s.Require().Equal(sdk.NewInt64DecCoin("uojo", 329), currRewards1.Rewards[0])
s.Require().Equal(sdk.NewInt64DecCoin("uojo", 320), currRewards2.Rewards[0])
s.Require().Equal(sdk.NewInt64DecCoin("uojo", 329), currRewards3.Rewards[0])
}

var exchangeRates = map[string][]math.LegacyDec{
Expand Down
69 changes: 0 additions & 69 deletions x/oracle/abci/preblocker.go

This file was deleted.

93 changes: 0 additions & 93 deletions x/oracle/abci/preblocker_test.go

This file was deleted.

Loading

0 comments on commit 2195260

Please sign in to comment.