Skip to content

Commit

Permalink
chore: backport #1741
Browse files Browse the repository at this point in the history
  • Loading branch information
chatton authored and crodriguezvega committed Jul 28, 2022
1 parent ff6c5e2 commit a4a329b
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
### Improvements

* (core/02-client) [\#1570](https://github.com/cosmos/ibc-go/pull/1570) Emitting an event when handling an upgrade client proposal.
* (modules/core/02-client) [\#1741](https://github.com/cosmos/ibc-go/pull/1741) Emitting a new `upgrade_chain` event upon setting upgrade consensus state.

### Features

Expand Down
1 change: 1 addition & 0 deletions modules/core/02-client/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func BeginBlocker(ctx sdk.Context, k keeper.Keeper) {
bz := k.MustMarshalConsensusState(upgradedConsState)

k.SetUpgradedConsensusState(ctx, plan.Height, bz)
keeper.EmitUpgradeChainEvent(ctx, plan.Height)
}
}

Expand Down
56 changes: 55 additions & 1 deletion modules/core/02-client/abci_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package client_test

import (
"strings"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
"github.com/stretchr/testify/suite"
abci "github.com/tendermint/tendermint/abci/types"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
client "github.com/cosmos/ibc-go/v2/modules/core/02-client"
"github.com/cosmos/ibc-go/v2/modules/core/02-client/types"
"github.com/cosmos/ibc-go/v2/modules/core/exported"
Expand Down Expand Up @@ -92,3 +94,55 @@ func (suite *ClientTestSuite) TestBeginBlockerConsensusState() {
suite.Require().NoError(err)
suite.Require().Equal(bz, consState)
}

func (suite *ClientTestSuite) TestBeginBlockerUpgradeEvents() {
plan := &upgradetypes.Plan{
Name: "test",
Height: suite.chainA.GetContext().BlockHeight() + 1,
}
// set upgrade plan in the upgrade store
store := suite.chainA.GetContext().KVStore(suite.chainA.GetSimApp().GetKey(upgradetypes.StoreKey))
bz := suite.chainA.App.AppCodec().MustMarshal(plan)
store.Set(upgradetypes.PlanKey(), bz)

nextValsHash := []byte("nextValsHash")
newCtx := suite.chainA.GetContext().WithBlockHeader(tmproto.Header{
Height: suite.chainA.GetContext().BlockHeight(),
NextValidatorsHash: nextValsHash,
})

err := suite.chainA.GetSimApp().UpgradeKeeper.SetUpgradedClient(newCtx, plan.Height, []byte("client state"))
suite.Require().NoError(err)

cacheCtx, writeCache := suite.chainA.GetContext().CacheContext()

client.BeginBlocker(cacheCtx, suite.chainA.App.GetIBCKeeper().ClientKeeper)
writeCache()

suite.requireContainsEvent(cacheCtx.EventManager().Events(), types.EventTypeUpgradeChain, true)
}

func (suite *ClientTestSuite) TestBeginBlockerUpgradeEventsAbsence() {
cacheCtx, writeCache := suite.chainA.GetContext().CacheContext()
client.BeginBlocker(suite.chainA.GetContext(), suite.chainA.App.GetIBCKeeper().ClientKeeper)
writeCache()
suite.requireContainsEvent(cacheCtx.EventManager().Events(), types.EventTypeUpgradeChain, false)
}

// requireContainsEvent verifies if an event of a specific type was emitted.
func (suite *ClientTestSuite) requireContainsEvent(events sdk.Events, eventType string, shouldContain bool) {
found := false
var eventTypes []string
for _, e := range events {
eventTypes = append(eventTypes, e.Type)
if e.Type == eventType {
found = true
break
}
}
if shouldContain {
suite.Require().True(found, "event type %s was not found in %s", eventType, strings.Join(eventTypes, ","))
} else {
suite.Require().False(found, "event type %s was found in %s", eventType, strings.Join(eventTypes, ","))
}
}
15 changes: 15 additions & 0 deletions modules/core/02-client/keeper/events.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package keeper

import (
"encoding/hex"
"fmt"
"strconv"
"strings"

sdk "github.com/cosmos/cosmos-sdk/types"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"

"github.com/cosmos/ibc-go/v2/modules/core/02-client/types"
)
Expand All @@ -18,3 +22,14 @@ func EmitUpgradeClientProposalEvent(ctx sdk.Context, title string, height int64)
),
)
}

// EmitUpgradeChainEvent emits an upgrade chain event.
func EmitUpgradeChainEvent(ctx sdk.Context, height int64) {
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeUpgradeChain,
sdk.NewAttribute(types.AttributeKeyUpgradePlanHeight, strconv.FormatInt(height, 10)),
sdk.NewAttribute(types.AttributeKeyUpgradeStore, upgradetypes.StoreKey), // which store to query proof of consensus state from
),
})
}
3 changes: 3 additions & 0 deletions modules/core/02-client/types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const (
AttributeKeyHeader = "header"
AttributeKeyUpgradePlanTitle = "title"
AttributeKeyUpgradePlanHeight = "height"
AttributeKeyUpgradeStore = "upgrade_store"
AttributeKeyUpgradePlanHeight = "upgrade_plan_height"
)

// IBC client events vars
Expand All @@ -25,6 +27,7 @@ var (
EventTypeSubmitMisbehaviour = "client_misbehaviour"
EventTypeUpdateClientProposal = "update_client_proposal"
EventTypeUpgradeClientProposal = "upgrade_client_proposal"
EventTypeUpgradeChain = "upgrade_chain"

AttributeValueCategory = fmt.Sprintf("%s_%s", host.ModuleName, SubModuleName)
)

0 comments on commit a4a329b

Please sign in to comment.