Skip to content

Commit

Permalink
refactor(oracle): add oracle slashing events
Browse files Browse the repository at this point in the history
  • Loading branch information
k-yang committed May 1, 2024
1 parent 45bb20c commit 339287c
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ func (app *NibiruApp) InitKeepers(
)

app.OracleKeeper = oraclekeeper.NewKeeper(appCodec, keys[oracletypes.StoreKey],
app.AccountKeeper, app.BankKeeper, app.DistrKeeper, app.stakingKeeper,
app.AccountKeeper, app.BankKeeper, app.DistrKeeper, app.stakingKeeper, app.slashingKeeper,
app.SudoKeeper,
distrtypes.ModuleName,
)
Expand Down
13 changes: 8 additions & 5 deletions x/oracle/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ type Keeper struct {
cdc codec.BinaryCodec
storeKey storetypes.StoreKey

AccountKeeper types.AccountKeeper
bankKeeper types.BankKeeper
distrKeeper types.DistributionKeeper
StakingKeeper types.StakingKeeper
SudoKeeper types.SudoKeeper
AccountKeeper types.AccountKeeper
bankKeeper types.BankKeeper
distrKeeper types.DistributionKeeper
StakingKeeper types.StakingKeeper
slashingKeeper types.SlashingKeeper
SudoKeeper types.SudoKeeper

distrModuleName string

Expand Down Expand Up @@ -57,6 +58,7 @@ func NewKeeper(
bankKeeper types.BankKeeper,
distrKeeper types.DistributionKeeper,
stakingKeeper types.StakingKeeper,
slashingKeeper types.SlashingKeeper,
sudoKeeper types.SudoKeeper,

distrName string,
Expand All @@ -73,6 +75,7 @@ func NewKeeper(
bankKeeper: bankKeeper,
distrKeeper: distrKeeper,
StakingKeeper: stakingKeeper,
slashingKeeper: slashingKeeper,
SudoKeeper: sudoKeeper,
distrModuleName: distrName,
Params: collections.NewItem(storeKey, 11, collections.ProtoValueEncoder[types.Params](cdc)),
Expand Down
9 changes: 4 additions & 5 deletions x/oracle/keeper/slash.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,11 @@ func (k Keeper) SlashAndResetMissCounters(ctx sdk.Context) {
continue
}

k.StakingKeeper.Slash(
ctx, consAddr,
distributionHeight, validator.GetConsensusPower(powerReduction), slashFraction,
k.slashingKeeper.Slash(
ctx, consAddr, slashFraction, validator.GetConsensusPower(powerReduction), distributionHeight,
)
k.Logger(ctx).Info("slash", "validator", consAddr.String(), "fraction", slashFraction.String())
k.StakingKeeper.Jail(ctx, consAddr)
k.Logger(ctx).Info("oracle slash", "validator", consAddr.String(), "fraction", slashFraction.String())
k.slashingKeeper.Jail(ctx, consAddr)
}
}

Expand Down
9 changes: 8 additions & 1 deletion x/oracle/keeper/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ import (
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
"github.com/cosmos/cosmos-sdk/x/params"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
"github.com/cosmos/cosmos-sdk/x/staking"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
Expand Down Expand Up @@ -139,9 +141,12 @@ func CreateTestFixture(t *testing.T) TestFixture {
tKeyParams := sdk.NewTransientStoreKey(paramstypes.TStoreKey)
keyOracle := sdk.NewKVStoreKey(types.StoreKey)
keyStaking := sdk.NewKVStoreKey(stakingtypes.StoreKey)
keySlashing := sdk.NewKVStoreKey(slashingtypes.StoreKey)
keyDistr := sdk.NewKVStoreKey(distrtypes.StoreKey)
keySudo := sdk.NewKVStoreKey(sudotypes.StoreKey)

govModuleAddr := authtypes.NewModuleAddress(govtypes.ModuleName).String()

db := dbm.NewMemDB()
ms := store.NewCommitMultiStore(db)
ctx := sdk.NewContext(ms, tmproto.Header{Time: time.Now().UTC(), Height: 1}, false, log.NewNopLogger())
Expand Down Expand Up @@ -201,11 +206,12 @@ func CreateTestFixture(t *testing.T) TestFixture {
bankKeeper,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)

stakingParams := stakingtypes.DefaultParams()
stakingParams.BondDenom = denoms.NIBI
stakingKeeper.SetParams(ctx, stakingParams)

slashingKeeper := slashingkeeper.NewKeeper(appCodec, legacyAmino, keySlashing, stakingKeeper, govModuleAddr)

distrKeeper := distrkeeper.NewKeeper(
appCodec,
keyDistr,
Expand Down Expand Up @@ -253,6 +259,7 @@ func CreateTestFixture(t *testing.T) TestFixture {
bankKeeper,
distrKeeper,
stakingKeeper,
slashingKeeper,
sudoKeeper,
distrtypes.ModuleName,
)
Expand Down
5 changes: 5 additions & 0 deletions x/oracle/types/expected_keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ type StakingKeeper interface {
PowerReduction(ctx sdk.Context) (res sdkmath.Int)
}

type SlashingKeeper interface {
Slash(ctx sdk.Context, consAddr sdk.ConsAddress, fraction sdk.Dec, power int64, height int64)
Jail(sdk.Context, sdk.ConsAddress)
}

// DistributionKeeper is expected keeper for distribution module
type DistributionKeeper interface {
AllocateTokensToValidator(ctx sdk.Context, val stakingtypes.ValidatorI, tokens sdk.DecCoins)
Expand Down

0 comments on commit 339287c

Please sign in to comment.