Skip to content

Commit

Permalink
feat: handle case where skip epoch not aligned with epoch passed (#1796)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasmatt authored Feb 14, 2024
1 parent df58262 commit f721571
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [#1786](https://github.com/NibiruChain/nibiru/pull/1786) - fix(inflation): fix inflation off-by 2 error
- [#1792](https://github.com/NibiruChain/nibiru/pull/1792) - fix(inflation): uncomment legacy amino register on app module basic
- [#1795](https://github.com/NibiruChain/nibiru/pull/1795) - feat(inflation): add inflation tx cmds
- [#1796](https://github.com/NibiruChain/nibiru/pull/1796) - fix(inflation): fix num skipped epoch when inflation is added to an existing chain

#### Dapp modules: perp, spot, etc

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

app.InflationKeeper = inflationkeeper.NewKeeper(
appCodec, keys[inflationtypes.StoreKey], app.GetSubspace(inflationtypes.ModuleName),
app.AccountKeeper, app.BankKeeper, app.DistrKeeper, app.stakingKeeper, app.SudoKeeper, authtypes.FeeCollectorName,
app.AccountKeeper, app.BankKeeper, app.DistrKeeper, app.stakingKeeper, app.SudoKeeper, authtypes.FeeCollectorName, app.EpochsKeeper,
)

app.EpochsKeeper.SetHooks(
Expand Down
19 changes: 8 additions & 11 deletions contrib/scripts/localnet.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ echo_success() {

# Flag parsing: --flag-name (BASH_VAR_NAME)
#
# --no-build ($FLAG_NO_BUILD): toggles whether to build from source. The default
# behavior of the script is to run make install.
FLAG_NO_BUILD=false
# --no-build ($FLAG_NO_BUILD): toggles whether to build from source. The default
# behavior of the script is to run make install.
FLAG_NO_BUILD=false

build_from_source() {
echo_info "Building from source..."
Expand All @@ -53,20 +53,17 @@ build_from_source() {
echo_info "Parsing flags for the script..."

# Iterate over all arguments to the script
for arg in "$@"
do
if [ "$arg" == "--no-build" ] ; then
for arg in "$@"; do
if [ "$arg" == "--no-build" ]; then
FLAG_NO_BUILD=true
fi
done


# Check if FLAG_NO_BUILD was set to true
if ! $FLAG_NO_BUILD ; then
if ! $FLAG_NO_BUILD; then
build_from_source
fi


# Set localnet settings
BINARY="nibid"
CHAIN_ID="nibiru-localnet-0"
Expand All @@ -78,7 +75,6 @@ CHAIN_DIR="$HOME/.nibid"
echo "CHAIN_DIR: $CHAIN_DIR"
echo "CHAIN_ID: $CHAIN_ID"


SEDOPTION=""
if [[ "$OSTYPE" == "darwin"* ]]; then
SEDOPTION="''"
Expand Down Expand Up @@ -132,7 +128,6 @@ sed -i $SEDOPTION 's/swagger = false/swagger = true/' $CHAIN_DIR/config/app.toml
echo_info "config/app.toml: Enabling CORS"
sed -i $SEDOPTION 's/enabled-unsafe-cors = false/enabled-unsafe-cors = true/' $CHAIN_DIR/config/app.toml


echo_info "Adding genesis accounts..."

val_key_name="validator"
Expand Down Expand Up @@ -237,6 +232,8 @@ add_genesis_param '.app_state.oracle.exchange_rates[0].exchange_rate = "'"$price
add_genesis_param '.app_state.oracle.exchange_rates[1].pair = "ueth:uuusd"'
add_genesis_param '.app_state.oracle.exchange_rates[1].exchange_rate = "'"$price_eth"'"'

add_genesis_param '.app_state.inflation.params.inflation_enabled = false'

# ------------------------------------------------------------------------
# Gentx
# ------------------------------------------------------------------------
Expand Down
33 changes: 30 additions & 3 deletions x/inflation/keeper/hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,11 @@ func GetBalanceStaking(ctx sdk.Context, nibiruApp *app.NibiruApp) sdkmath.Int {
}

func TestManual(t *testing.T) {
// This test is a manual test to check if the inflation is working as expected
// We turn off inflation, then we turn it on and check if the balance is increasing
// We turn it off again and check if the balance is not increasing
// We turn it on again and check if the balance is increasing again with the correct amount

nibiruApp, ctx := testapp.NewNibiruTestAppAndContext()

params := nibiruApp.InflationKeeper.GetParams(ctx)
Expand All @@ -242,8 +247,18 @@ func TestManual(t *testing.T) {
}
require.Equal(t, sdk.ZeroInt(), GetBalanceStaking(ctx, nibiruApp))

params.InflationEnabled = true
nibiruApp.InflationKeeper.Params.Set(ctx, params)
nibiruApp.EpochsKeeper.Epochs.Insert(ctx, epochstypes.DayEpochID, epochstypes.EpochInfo{
Identifier: epochstypes.DayEpochID,
StartTime: time.Now(),
Duration: 0,
CurrentEpoch: 42069,
CurrentEpochStartTime: time.Now(),
EpochCountingStarted: false,
CurrentEpochStartHeight: 0,
},
)
err := nibiruApp.InflationKeeper.Sudo().ToggleInflation(ctx, true, testapp.DefaultSudoRoot())
require.NoError(t, err)

for i := 0; i < 30; i++ {
nibiruApp.InflationKeeper.AfterEpochEnd(ctx, epochstypes.DayEpochID, epochNumber)
Expand All @@ -252,12 +267,24 @@ func TestManual(t *testing.T) {
}
require.Equal(t, sdk.NewInt(3_000_000), GetBalanceStaking(ctx, nibiruApp))

err = nibiruApp.InflationKeeper.Sudo().ToggleInflation(ctx, false, testapp.DefaultSudoRoot())
require.NoError(t, err)

for i := 0; i < 42069; i++ {
nibiruApp.InflationKeeper.AfterEpochEnd(ctx, epochstypes.DayEpochID, epochNumber)
epochNumber++
}
require.Equal(t, sdk.NewInt(3_000_000), GetBalanceStaking(ctx, nibiruApp))

err = nibiruApp.InflationKeeper.Sudo().ToggleInflation(ctx, true, testapp.DefaultSudoRoot())
require.NoError(t, err)

// Period 1 - we do 200_000 per periods now
for i := 0; i < 30; i++ {
nibiruApp.InflationKeeper.AfterEpochEnd(ctx, epochstypes.DayEpochID, epochNumber)
require.Equal(t, sdk.NewInt(3_000_000).Add(sdk.NewInt(200_000).Mul(sdk.NewInt(int64(i+1)))), GetBalanceStaking(ctx, nibiruApp))
epochNumber++
}
require.EqualValues(t, epochNumber, uint64(42069+60))
require.EqualValues(t, epochNumber, uint64(2*42069+60))
require.Equal(t, sdk.NewInt(9_000_000), GetBalanceStaking(ctx, nibiruApp))
}
3 changes: 3 additions & 0 deletions x/inflation/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Keeper struct {
distrKeeper types.DistrKeeper
stakingKeeper types.StakingKeeper
sudoKeeper types.SudoKeeper
epochsKeeper types.EpochsKeeper
feeCollectorName string

CurrentPeriod collections.Sequence
Expand All @@ -41,6 +42,7 @@ func NewKeeper(
stakingKeeper types.StakingKeeper,
sudoKeeper types.SudoKeeper,
feeCollectorName string,
epochsKeeper types.EpochsKeeper,
) Keeper {
// ensure mint module account is set
if addr := accountKeeper.GetModuleAddress(types.ModuleName); addr == nil {
Expand All @@ -56,6 +58,7 @@ func NewKeeper(
distrKeeper: distributionKeeper,
stakingKeeper: stakingKeeper,
sudoKeeper: sudoKeeper,
epochsKeeper: epochsKeeper,
feeCollectorName: feeCollectorName,
CurrentPeriod: collections.NewSequence(storeKey, 0),
NumSkippedEpochs: collections.NewSequence(storeKey, 1),
Expand Down
13 changes: 13 additions & 0 deletions x/inflation/keeper/sudo.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

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

epochstypes "github.com/NibiruChain/nibiru/x/epochs/types"
inflationtypes "github.com/NibiruChain/nibiru/x/inflation/types"
)

Expand Down Expand Up @@ -57,6 +58,18 @@ func (k sudoExtension) ToggleInflation(
}

params.InflationEnabled = enabled
if k.NumSkippedEpochs.Peek(ctx) == 0 {
// update skipped epochs since this means that inflation could have
// added to the chain after launch

epochInfo, err := k.epochsKeeper.GetEpochInfo(ctx, epochstypes.DayEpochID)
if err != nil {
return err
}

k.NumSkippedEpochs.Set(ctx, epochInfo.CurrentEpoch)
}

k.Params.Set(ctx, params)
return
}
Expand Down
6 changes: 6 additions & 0 deletions x/inflation/types/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/types"

epochsTypes "github.com/NibiruChain/nibiru/x/epochs/types"
)

// AccountKeeper defines the contract required for account APIs.
Expand Down Expand Up @@ -44,3 +46,7 @@ type SudoKeeper interface {
GetRootAddr(ctx sdk.Context) (sdk.AccAddress, error)
CheckPermissions(contract sdk.AccAddress, ctx sdk.Context) error
}

type EpochsKeeper interface {
GetEpochInfo(ctx sdk.Context, epochIdentifier string) (epochsTypes.EpochInfo, error)
}

0 comments on commit f721571

Please sign in to comment.