refactor(evidence): migrate to env var (#19482)
This commit is contained in:
parent
f5a3bfb058
commit
8acade49db
@ -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
|
||||
|
||||
@ -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)
|
||||
@ -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)
|
||||
@ -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))
|
||||
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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"
|
||||
@ -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
|
||||
@ -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}
|
||||
|
||||
@ -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
|
||||
@ -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
|
||||
|
||||
@ -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)
|
||||
@ -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(
|
||||
|
||||
@ -8,14 +8,14 @@ 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
|
||||
@ -23,7 +23,7 @@ import (
|
||||
// module.
|
||||
type Keeper struct {
|
||||
cdc codec.BinaryCodec
|
||||
storeService store.KVStoreService
|
||||
environment appmodule.Environment
|
||||
router types.Router
|
||||
stakingKeeper types.StakingKeeper
|
||||
slashingKeeper types.SlashingKeeper
|
||||
@ -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,
|
||||
@ -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,
|
||||
@ -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)
|
||||
}
|
||||
|
||||
@ -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"
|
||||
@ -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
|
||||
@ -99,7 +100,7 @@ func (suite *KeeperTestSuite) SetupTest() {
|
||||
|
||||
evidenceKeeper := keeper.NewKeeper(
|
||||
encCfg.Codec,
|
||||
storeService,
|
||||
env,
|
||||
stakingKeeper,
|
||||
slashingKeeper,
|
||||
address.NewBech32Codec("cosmos"),
|
||||
@ -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)
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user