Skip to content

Commit

Permalink
refactor(evidence): migrate to env var (#19482)
Browse files Browse the repository at this point in the history
  • Loading branch information
tac0turtle authored Feb 20, 2024
1 parent f5a3bfb commit 8acade4
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 36 deletions.
2 changes: 1 addition & 1 deletion simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ func NewSimApp(

// create evidence keeper with router
evidenceKeeper := evidencekeeper.NewKeeper(
appCodec, runtime.NewKVStoreService(keys[evidencetypes.StoreKey]), app.StakingKeeper, app.SlashingKeeper, app.AuthKeeper.AddressCodec(),
appCodec, runtime.NewEnvironment(runtime.NewKVStoreService(keys[evidencetypes.StoreKey]), logger), app.StakingKeeper, app.SlashingKeeper, app.AuthKeeper.AddressCodec(),
)
// If evidence needs to be handled for the app, set routes in router here and seal
app.EvidenceKeeper = *evidenceKeeper
Expand Down
7 changes: 3 additions & 4 deletions tests/integration/evidence/keeper/infraction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func initFixture(tb testing.TB) *fixture {

stakingKeeper.SetHooks(stakingtypes.NewMultiStakingHooks(slashingKeeper.Hooks()))

evidenceKeeper := keeper.NewKeeper(cdc, runtime.NewKVStoreService(keys[evidencetypes.StoreKey]), stakingKeeper, slashingKeeper, addresscodec.NewBech32Codec(sdk.Bech32PrefixAccAddr))
evidenceKeeper := keeper.NewKeeper(cdc, runtime.NewEnvironment(runtime.NewKVStoreService(keys[evidencetypes.StoreKey]), log.NewNopLogger()), stakingKeeper, slashingKeeper, addresscodec.NewBech32Codec(sdk.Bech32PrefixAccAddr))
router := evidencetypes.NewRouter()
router = router.AddRoute(evidencetypes.RouteEquivocation, testEquivocationHandler(evidenceKeeper))
evidenceKeeper.SetRouter(router)
Expand Down Expand Up @@ -271,7 +271,7 @@ func TestHandleDoubleSign_TooOld(t *testing.T) {
t.Parallel()
f := initFixture(t)

ctx := f.sdkCtx.WithIsCheckTx(false).WithBlockHeight(1).WithHeaderInfo(header.Info{Time: time.Now()})
ctx := f.sdkCtx.WithIsCheckTx(false).WithHeaderInfo(header.Info{Height: 1, Time: time.Now()})
populateValidators(t, f)

power := int64(100)
Expand Down Expand Up @@ -306,8 +306,7 @@ func TestHandleDoubleSign_TooOld(t *testing.T) {

ctx = ctx.WithCometInfo(nci)
ctx = ctx.WithConsensusParams(cp)
ctx = ctx.WithHeaderInfo(header.Info{Time: ctx.HeaderInfo().Time.Add(cp.Evidence.MaxAgeDuration + 1)})
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + cp.Evidence.MaxAgeNumBlocks + 1)
ctx = ctx.WithHeaderInfo(header.Info{Height: ctx.BlockHeight() + cp.Evidence.MaxAgeNumBlocks + 1, Time: ctx.HeaderInfo().Time.Add(cp.Evidence.MaxAgeDuration + 1)})

assert.NilError(t, f.evidenceKeeper.BeginBlocker(ctx))

Expand Down
6 changes: 5 additions & 1 deletion x/evidence/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@ Ref: https://keepachangelog.com/en/1.0.0/

## [Unreleased]

### Api Breaking Changes

* [#19482](https://github.com/cosmos/cosmos-sdk/pull/19482) `appmodule.Environment` is passed to `NewKeeper` instead of individual services

## [v0.1.0](https://github.com/cosmos/cosmos-sdk/releases/tag/x/evidence/v0.1.0) - 2023-11-07

### Features

* (x/evidence) [14724](https://github.com/cosmos/cosmos-sdk/pull/14724) The `x/evidence` module is extracted to have a separate go.mod file which allows it be a standalone module.
* [14724](https://github.com/cosmos/cosmos-sdk/pull/14724) The `x/evidence` module is extracted to have a separate go.mod file which allows it be a standalone module.
* (keeper) [#15420](https://github.com/cosmos/cosmos-sdk/pull/15420) Move `BeginBlocker` to the keeper folder & make HandleEquivocation private

### API Breaking Changes
Expand Down
7 changes: 3 additions & 4 deletions x/evidence/depinject.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
modulev1 "cosmossdk.io/api/cosmos/evidence/module/v1"
"cosmossdk.io/core/address"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/core/store"
"cosmossdk.io/depinject"
"cosmossdk.io/depinject/appconfig"
"cosmossdk.io/x/evidence/keeper"
Expand All @@ -27,8 +26,8 @@ func init() {
type ModuleInputs struct {
depinject.In

StoreService store.KVStoreService
Cdc codec.Codec
Environment appmodule.Environment
Cdc codec.Codec

StakingKeeper types.StakingKeeper
SlashingKeeper types.SlashingKeeper
Expand All @@ -43,7 +42,7 @@ type ModuleOutputs struct {
}

func ProvideModule(in ModuleInputs) ModuleOutputs {
k := keeper.NewKeeper(in.Cdc, in.StoreService, in.StakingKeeper, in.SlashingKeeper, in.AddressCodec)
k := keeper.NewKeeper(in.Cdc, in.Environment, in.StakingKeeper, in.SlashingKeeper, in.AddressCodec)
m := NewAppModule(*k)

return ModuleOutputs{EvidenceKeeper: *k, Module: m}
Expand Down
3 changes: 1 addition & 2 deletions x/evidence/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ func (k Keeper) BeginBlocker(ctx context.Context) error {
bi := sdk.UnwrapSDKContext(ctx).CometInfo()

evidences := bi.Evidence
sdkCtx := sdk.UnwrapSDKContext(ctx)
for _, evidence := range evidences {
switch evidence.Type {
// It's still ongoing discussion how should we treat and slash attacks with
Expand All @@ -32,7 +31,7 @@ func (k Keeper) BeginBlocker(ctx context.Context) error {
return err
}
default:
k.Logger(sdkCtx).Error(fmt.Sprintf("ignored unknown evidence type: %x", evidence.Type))
k.Logger().Error(fmt.Sprintf("ignored unknown evidence type: %x", evidence.Type))
}
}
return nil
Expand Down
11 changes: 6 additions & 5 deletions x/evidence/keeper/infraction.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ import (
// TODO: Some of the invalid constraints listed above may need to be reconsidered
// in the case of a lunatic attack.
func (k Keeper) handleEquivocationEvidence(ctx context.Context, evidence *types.Equivocation) error {
sdkCtx := sdk.UnwrapSDKContext(ctx)
logger := k.Logger(ctx)
logger := k.Logger()
consAddr := evidence.GetConsensusAddress(k.stakingKeeper.ConsensusAddressCodec())

validator, err := k.stakingKeeper.ValidatorByConsAddr(ctx, consAddr)
Expand Down Expand Up @@ -64,16 +63,18 @@ func (k Keeper) handleEquivocationEvidence(ctx context.Context, evidence *types.
}
}

headerInfo := k.environment.HeaderService.GetHeaderInfo(ctx)
// calculate the age of the evidence
infractionHeight := evidence.GetHeight()
infractionTime := evidence.GetTime()
ageDuration := sdkCtx.HeaderInfo().Time.Sub(infractionTime)
ageBlocks := sdkCtx.BlockHeader().Height - infractionHeight
ageDuration := headerInfo.Time.Sub(infractionTime)
ageBlocks := headerInfo.Height - infractionHeight

// Reject evidence if the double-sign is too old. Evidence is considered stale
// if the difference in time and number of blocks is greater than the allowed
// parameters defined.
cp := sdkCtx.ConsensusParams()
sdkCtx := sdk.UnwrapSDKContext(ctx)
cp := sdkCtx.ConsensusParams() // TODO: remove in favor of querying consensus module
if cp.Evidence != nil {
if ageDuration > cp.Evidence.MaxAgeDuration && ageBlocks > cp.Evidence.MaxAgeNumBlocks {
logger.Info(
Expand Down
30 changes: 14 additions & 16 deletions x/evidence/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@ import (

"cosmossdk.io/collections"
"cosmossdk.io/core/address"
"cosmossdk.io/core/store"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/core/event"
"cosmossdk.io/errors"
"cosmossdk.io/log"
"cosmossdk.io/x/evidence/exported"
"cosmossdk.io/x/evidence/types"

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

// Keeper defines the evidence module's keeper. The keeper is responsible for
// managing persistence, state transitions and query handling for the evidence
// module.
type Keeper struct {
cdc codec.BinaryCodec
storeService store.KVStoreService
environment appmodule.Environment
router types.Router
stakingKeeper types.StakingKeeper
slashingKeeper types.SlashingKeeper
Expand All @@ -36,13 +36,13 @@ type Keeper struct {

// NewKeeper creates a new Keeper object.
func NewKeeper(
cdc codec.BinaryCodec, storeService store.KVStoreService, stakingKeeper types.StakingKeeper,
cdc codec.BinaryCodec, env appmodule.Environment, stakingKeeper types.StakingKeeper,
slashingKeeper types.SlashingKeeper, ac address.Codec,
) *Keeper {
sb := collections.NewSchemaBuilder(storeService)
sb := collections.NewSchemaBuilder(env.KVStoreService)
k := &Keeper{
cdc: cdc,
storeService: storeService,
environment: env,
stakingKeeper: stakingKeeper,
slashingKeeper: slashingKeeper,
addressCodec: ac,
Expand All @@ -57,9 +57,8 @@ func NewKeeper(
}

// Logger returns a module-specific logger.
func (k Keeper) Logger(ctx context.Context) log.Logger {
sdkCtx := sdk.UnwrapSDKContext(ctx)
return sdkCtx.Logger().With("module", "x/"+types.ModuleName)
func (k Keeper) Logger() log.Logger {
return k.environment.Logger.With("module", "x/"+types.ModuleName)
}

// SetRouter sets the Evidence Handler router for the x/evidence module. Note,
Expand Down Expand Up @@ -107,13 +106,12 @@ func (k Keeper) SubmitEvidence(ctx context.Context, evidence exported.Evidence)
return errors.Wrap(types.ErrInvalidEvidence, err.Error())
}

sdkCtx := sdk.UnwrapSDKContext(ctx)
sdkCtx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeSubmitEvidence,
sdk.NewAttribute(types.AttributeKeyEvidenceHash, strings.ToUpper(hex.EncodeToString(evidence.Hash()))),
),
)
if err := k.environment.EventService.EventManager(ctx).EmitKV(
types.EventTypeSubmitEvidence,
event.NewAttribute(types.AttributeKeyEvidenceHash, strings.ToUpper(hex.EncodeToString(evidence.Hash()))),
); err != nil {
return err
}

return k.Evidences.Set(ctx, evidence.Hash(), evidence)
}
7 changes: 4 additions & 3 deletions x/evidence/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"cosmossdk.io/collections"
"cosmossdk.io/core/header"
"cosmossdk.io/log"
storetypes "cosmossdk.io/store/types"
"cosmossdk.io/x/evidence"
"cosmossdk.io/x/evidence/exported"
Expand Down Expand Up @@ -85,7 +86,7 @@ type KeeperTestSuite struct {
func (suite *KeeperTestSuite) SetupTest() {
encCfg := moduletestutil.MakeTestEncodingConfig(evidence.AppModuleBasic{})
key := storetypes.NewKVStoreKey(types.StoreKey)
storeService := runtime.NewKVStoreService(key)
env := runtime.NewEnvironment(runtime.NewKVStoreService(key), log.NewNopLogger())
tkey := storetypes.NewTransientStoreKey("evidence_transient_store")
testCtx := testutil.DefaultContextWithDB(suite.T(), key, tkey)
suite.ctx = testCtx.Ctx
Expand All @@ -99,7 +100,7 @@ func (suite *KeeperTestSuite) SetupTest() {

evidenceKeeper := keeper.NewKeeper(
encCfg.Codec,
storeService,
env,
stakingKeeper,
slashingKeeper,
address.NewBech32Codec("cosmos"),
Expand All @@ -124,7 +125,7 @@ func (suite *KeeperTestSuite) SetupTest() {
suite.evidenceKeeper = *evidenceKeeper

suite.Require().Equal(testCtx.Ctx.Logger().With("module", "x/"+types.ModuleName),
suite.evidenceKeeper.Logger(testCtx.Ctx))
suite.evidenceKeeper.Logger())

suite.msgServer = keeper.NewMsgServerImpl(suite.evidenceKeeper)
}
Expand Down

0 comments on commit 8acade4

Please sign in to comment.