Skip to content

Commit

Permalink
refactor(oracle): add oracle slashing events (#1859)
Browse files Browse the repository at this point in the history
* refactor(oracle): add oracle slashing events

* Update CHANGELOG.md
  • Loading branch information
k-yang committed May 1, 2024
1 parent 208012c commit 3e26c65
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 18 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,21 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

## [v1.0.3-post.1](https://github.com/NibiruChain/nibiru/releases/tag/v1.0.3-post.1) - 2024-05-01

* [#1859](https://github.com/NibiruChain/nibiru/pull/1859) - refactor(oracle): add oracle slashing events

## [v1.0.3](https://github.com/NibiruChain/nibiru/releases/tag/v1.0.3) - 2024-03-18

### Fix

- [#1816](https://github.com/NibiruChain/nibiru/pull/1816) - fix(ibc): fix ibc transaction from wasm contract
* [#1816](https://github.com/NibiruChain/nibiru/pull/1816) - fix(ibc): fix ibc transaction from wasm contract

### CLI

* [#1731](https://github.com/NibiruChain/nibiru/pull/1731) - feat(cli): add cli command to decode stargate base64 messages
* [#1754](https://github.com/NibiruChain/nibiru/pull/1754) - refactor(decode-base64): clean code improvements and fn docs


## [v1.0.1](https://github.com/NibiruChain/nibiru/releases/tag/v1.0.1) - 2024-02-09

### Dependencies
Expand Down Expand Up @@ -130,6 +133,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* [#1501](https://github.com/NibiruChain/nibiru/pull/1501) - feat(proto): add Python buf generation logic for py-sdk
* [#1503](https://github.com/NibiruChain/nibiru/pull/1503) - feat(wasm): add Oracle Exchange Rate query for wasm
* [#1543](https://github.com/NibiruChain/nibiru/pull/1543) - epic(devgas): devgas module for incentivizing smart contract

*

### Improvements
Expand Down
2 changes: 1 addition & 1 deletion app/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ func (app *NibiruApp) InitKeepers(

// ---------------------------------- Nibiru Chain x/ keepers
app.OracleKeeper = oraclekeeper.NewKeeper(appCodec, keys[oracletypes.StoreKey],
app.AccountKeeper, app.BankKeeper, app.DistrKeeper, app.stakingKeeper, distrtypes.ModuleName,
app.AccountKeeper, app.BankKeeper, app.DistrKeeper, app.stakingKeeper, app.slashingKeeper, distrtypes.ModuleName,
)

app.EpochsKeeper = epochskeeper.NewKeeper(
Expand Down
22 changes: 15 additions & 7 deletions x/oracle/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ type Keeper struct {
cdc codec.BinaryCodec
storeKey storetypes.StoreKey

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

distrModuleName string

Expand All @@ -45,10 +46,16 @@ type Keeper struct {
}

// NewKeeper constructs a new keeper for oracle
func NewKeeper(cdc codec.BinaryCodec, storeKey storetypes.StoreKey,
func NewKeeper(
cdc codec.BinaryCodec,
storeKey storetypes.StoreKey,
accountKeeper types.AccountKeeper,
bankKeeper types.BankKeeper, distrKeeper types.DistributionKeeper,
stakingKeeper types.StakingKeeper, distrName string,
bankKeeper types.BankKeeper,
distrKeeper types.DistributionKeeper,
stakingKeeper types.StakingKeeper,
slashingKeeper types.SlashingKeeper,

distrName string,
) Keeper {
// ensure oracle module account is set
if addr := accountKeeper.GetModuleAddress(types.ModuleName); addr == nil {
Expand All @@ -62,6 +69,7 @@ func NewKeeper(cdc codec.BinaryCodec, storeKey storetypes.StoreKey,
bankKeeper: bankKeeper,
distrKeeper: distrKeeper,
StakingKeeper: stakingKeeper,
slashingKeeper: slashingKeeper,
distrModuleName: distrName,
Params: collections.NewItem(storeKey, 11, collections.ProtoValueEncoder[types.Params](cdc)),
ExchangeRates: collections.NewMap(storeKey, 1, asset.PairKeyEncoder, collections.ProtoValueEncoder[types.DatedPrice](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
14 changes: 11 additions & 3 deletions x/oracle/keeper/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
package keeper

import (
sudokeeper "github.com/NibiruChain/nibiru/x/sudo/keeper"
sudotypes "github.com/NibiruChain/nibiru/x/sudo/types"
"testing"
"time"

sudokeeper "github.com/NibiruChain/nibiru/x/sudo/keeper"
sudotypes "github.com/NibiruChain/nibiru/x/sudo/types"

"github.com/cosmos/cosmos-sdk/store"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
"github.com/cosmos/cosmos-sdk/testutil/sims"
Expand Down Expand Up @@ -38,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 @@ -137,9 +140,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 @@ -200,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 +260,7 @@ func CreateTestFixture(t *testing.T) TestFixture {
bankKeeper,
distrKeeper,
stakingKeeper,
slashingKeeper,
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 3e26c65

Please sign in to comment.