Skip to content

Commit

Permalink
fix: broken unit test + make x/sudo safer by making blank genesis inv…
Browse files Browse the repository at this point in the history
…alid
  • Loading branch information
Unique-Divine committed Dec 7, 2023
1 parent 8adab45 commit 1ecad2d
Show file tree
Hide file tree
Showing 14 changed files with 114 additions and 81 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* [#1690](https://github.com/NibiruChain/nibiru/pull/1690) - docs(CHANGELOG.md): Correct the change log, providing clarity on what's released.
* [#1679](https://github.com/NibiruChain/nibiru/pull/1679) - test(perp): add more tests for perp module msg server
* [#1695](https://github.com/NibiruChain/nibiru/pull/1695) - feat(inflation): add events for inflation distribution
* [#1695](https://github.com/NibiruChain/nibiru/pull/1695) - fix(sudo): Make blank sudoers root invalid at genesis time.

### Dependencies

Expand Down
32 changes: 16 additions & 16 deletions proto/nibiru/inflation/v1/event.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ import "cosmos/base/v1beta1/coin.proto";

option go_package = "github.com/NibiruChain/nibiru/x/inflation/types";

// Emitted when inflation is distributed
message InflationDistributionEvent {
cosmos.base.v1beta1.Coin staking_rewards = 1 [
(gogoproto.moretags) = "yaml:\"staking_rewards\"",
(gogoproto.nullable)= false
];
// EventInflationDistribution: Emitted when NIBI tokens are minted on the
// network based on Nibiru's inflation schedule.
message EventInflationDistribution {
cosmos.base.v1beta1.Coin staking_rewards = 1 [
(gogoproto.moretags) = "yaml:\"staking_rewards\"",
(gogoproto.nullable) = false
];

cosmos.base.v1beta1.Coin strategic_reserve = 2 [
(gogoproto.moretags) = "yaml:\"strategic_reserve\"",
(gogoproto.nullable)= false
];
cosmos.base.v1beta1.Coin strategic_reserve = 2 [
(gogoproto.moretags) = "yaml:\"strategic_reserve\"",
(gogoproto.nullable) = false
];

cosmos.base.v1beta1.Coin community_pool = 3 [
(gogoproto.moretags) = "yaml:\"community_pool\"",
(gogoproto.nullable)= false
];

}
cosmos.base.v1beta1.Coin community_pool = 3 [
(gogoproto.moretags) = "yaml:\"community_pool\"",
(gogoproto.nullable) = false
];
}
9 changes: 9 additions & 0 deletions x/common/testutil/testapp/testapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,18 @@ func NewNibiruTestAppAndContext() (*app.NibiruApp, sdk.Context) {
encoding := app.MakeEncodingConfig()
var appGenesis app.GenesisState = app.NewDefaultGenesisState(encoding.Marshaler)
genModEpochs := epochstypes.DefaultGenesisFromTime(time.Now().UTC())

// Set happy genesis: epochs
appGenesis[epochstypes.ModuleName] = encoding.Marshaler.MustMarshalJSON(
genModEpochs,
)

// Set happy genesis: sudo
sudoGenesis := new(sudotypes.GenesisState)
sudoGenesis.Sudoers = DefaultSudoers()
appGenesis[sudotypes.ModuleName] =
encoding.Marshaler.MustMarshalJSON(sudoGenesis)

app := NewNibiruTestApp(appGenesis)
ctx := NewContext(app)

Expand Down
24 changes: 12 additions & 12 deletions x/inflation/keeper/inflation.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package keeper

import (
"fmt"

sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"

Expand Down Expand Up @@ -76,30 +78,28 @@ func (k Keeper) AllocatePolynomialInflation(
return sdk.Coin{}, sdk.Coin{}, sdk.Coin{}, err
}

// Remaining balance is strategic reserve allocation to the root account of the x/sudo module
// Remaining balance is strategic reserve allocation to the root account
// of the x/sudo module
strategic = k.bankKeeper.GetBalance(ctx, inflationModuleAddr, denoms.NIBI)
strategicAccountAddr, err := k.sudoKeeper.GetRoot(ctx)
strategicAccountAddr, err := k.sudoKeeper.GetRootAddr(ctx)
if err != nil {
k.Logger(ctx).Error("get root account error", "error", err)
err := fmt.Errorf("inflation error: failed to get sudo root account: %w", err)
k.Logger(ctx).Error(err.Error())
return staking, strategic, community, err
}

if err = k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, strategicAccountAddr, sdk.NewCoins(strategic)); err != nil {
k.Logger(ctx).Error("send coins to root account error", "error", err)
err := fmt.Errorf("inflation error: failed to send coins to sudo root account: %w", err)
k.Logger(ctx).Error(err.Error())
return sdk.Coin{}, sdk.Coin{}, sdk.Coin{}, err
}

if err := ctx.EventManager().EmitTypedEvents(
&types.InflationDistributionEvent{
return staking, strategic, community, ctx.EventManager().EmitTypedEvents(
&types.EventInflationDistribution{
StakingRewards: staking,
StrategicReserve: strategic,
CommunityPool: community,
},
); err != nil {
return sdk.Coin{}, sdk.Coin{}, sdk.Coin{}, err
}

return staking, strategic, community, nil
})
}

// GetAllocationProportion calculates the proportion of coins that is to be
Expand Down
21 changes: 16 additions & 5 deletions x/inflation/keeper/inflation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,20 @@ func TestMintAndAllocateInflation(t *testing.T) {
})

staking, strategic, community, err := nibiruApp.InflationKeeper.MintAndAllocateInflation(ctx, tc.coinsToMint, types.DefaultParams())
require.NoError(t, err)
if tc.rootAccount != "" {
require.NoError(t, err)
} else {
require.Error(t, err)
return
}
assert.Equal(t, tc.expectedStakingAmt, staking)
assert.Equal(t, tc.expectedStrategicAmt, strategic)
assert.Equal(t, tc.expectedCommunityAmt, community)

// Get balances
var balanceStrategicReserve sdk.Coin
if tc.rootAccount != "" {
strategicAccount, err := nibiruApp.SudoKeeper.GetRoot(ctx)
strategicAccount, err := nibiruApp.SudoKeeper.GetRootAddr(ctx)
require.NoError(t, err)
balanceStrategicReserve = nibiruApp.BankKeeper.GetBalance(
ctx,
Expand All @@ -108,9 +113,15 @@ func TestMintAndAllocateInflation(t *testing.T) {
balanceCommunityPool := nibiruApp.DistrKeeper.GetFeePoolCommunityCoins(ctx)

require.NoError(t, err, tc.name)
assert.Equal(t, tc.expectedStakingRewardsBalance, balanceStakingRewards)
assert.Equal(t, tc.expectedStrategicReservesBalance, balanceStrategicReserve)
assert.Equal(t, tc.expectedCommunityPoolBalance, balanceCommunityPool)
assert.Equal(t,
tc.expectedStakingRewardsBalance.String(),
balanceStakingRewards.String())
assert.Equal(t,
tc.expectedStrategicReservesBalance.String(),
balanceStrategicReserve.String())
assert.Equal(t,
tc.expectedCommunityPoolBalance.String(),
balanceCommunityPool.String())
})
}
}
Expand Down
59 changes: 30 additions & 29 deletions x/inflation/types/event.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion x/inflation/types/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ type StakingKeeper interface {
}

type SudoKeeper interface {
GetRoot(ctx sdk.Context) (sdk.AccAddress, error)
GetRootAddr(ctx sdk.Context) (sdk.AccAddress, error)
}
2 changes: 2 additions & 0 deletions x/sudo/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState {
}
}

// DefaultGenesis: A blank genesis state. The DefaultGenesis is invalid because
// it does not specify a "Sudoers.Root".
func DefaultGenesis() *types.GenesisState {
return &types.GenesisState{
Sudoers: types.Sudoers{
Expand Down
2 changes: 1 addition & 1 deletion x/sudo/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func NewKeeper(
}

// Returns the root address of the sudo module.
func (k Keeper) GetRoot(ctx sdk.Context) (sdk.AccAddress, error) {
func (k Keeper) GetRootAddr(ctx sdk.Context) (sdk.AccAddress, error) {
sudoers, err := k.Sudoers.Get(ctx)
if err != nil {
return nil, err
Expand Down
12 changes: 2 additions & 10 deletions x/sudo/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/NibiruChain/nibiru/x/sudo/types"

tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -25,14 +24,7 @@ func init() {
}

func setup() (*app.NibiruApp, sdk.Context) {
genState := app.NewDefaultGenesisState(app.MakeEncodingConfig().Marshaler)
nibiru := testapp.NewNibiruTestApp(genState)
ctx := nibiru.NewContext(false, tmproto.Header{
Height: 1,
ChainID: "nibiru-sudonet-1",
Time: time.Now().UTC(),
})
return nibiru, ctx
return testapp.NewNibiruTestAppAndContextAtTime(time.Now().UTC())
}

func TestGenesis(t *testing.T) {
Expand All @@ -45,7 +37,7 @@ func TestGenesis(t *testing.T) {
{
name: "default genesis (empty)",
genState: sudo.DefaultGenesis(),
empty: true,
panic: true,
},
{
name: "happy genesis with contracts",
Expand Down
19 changes: 17 additions & 2 deletions x/sudo/types/errors.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
package types

import sdkerrors "cosmossdk.io/errors"
import (
"fmt"

var ErrUnauthorized = sdkerrors.Register(ModuleName, 2, "unauthorized: missing sudo permissions")
sdkerrors "cosmossdk.io/errors"
)

var (
ErrUnauthorized = sdkerrors.Register(ModuleName, 2, "unauthorized: missing sudo permissions")
errGenesis = sdkerrors.Register(ModuleName, 3, "sudo genesis error")
errSudoers = sdkerrors.Register(ModuleName, 4, "sudoers error")
)

func ErrGenesis(errMsg string) error {
return fmt.Errorf("%s: %s", errGenesis, errMsg)
}
func ErrSudoers(errMsg string) error {
return fmt.Errorf("%s: %s", errSudoers, errMsg)
}
Loading

0 comments on commit 1ecad2d

Please sign in to comment.