Skip to content

Commit

Permalink
feat(x/protocolpool)!: allow any coins in continuous funds (#21916)
Browse files Browse the repository at this point in the history
(cherry picked from commit 13c234f)

# Conflicts:
#	api/cosmos/protocolpool/v1/genesis.pulsar.go
#	api/cosmos/protocolpool/v1/tx.pulsar.go
#	api/cosmos/protocolpool/v1/tx_grpc.pb.go
#	api/cosmos/protocolpool/v1/types.pulsar.go
#	x/protocolpool/keeper/genesis.go
#	x/protocolpool/testutil/expected_keepers_mocks.go
  • Loading branch information
facundomedica authored and mergify[bot] committed Nov 11, 2024
1 parent 2b4f5fb commit bb57927
Show file tree
Hide file tree
Showing 25 changed files with 15,319 additions and 351 deletions.
1,747 changes: 1,747 additions & 0 deletions api/cosmos/protocolpool/v1/genesis.pulsar.go

Large diffs are not rendered by default.

9,065 changes: 9,065 additions & 0 deletions api/cosmos/protocolpool/v1/tx.pulsar.go

Large diffs are not rendered by default.

417 changes: 417 additions & 0 deletions api/cosmos/protocolpool/v1/tx_grpc.pb.go

Large diffs are not rendered by default.

2,763 changes: 2,763 additions & 0 deletions api/cosmos/protocolpool/v1/types.pulsar.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ func NewSimApp(

app.MintKeeper = mintkeeper.NewKeeper(appCodec, runtime.NewEnvironment(runtime.NewKVStoreService(keys[minttypes.StoreKey]), logger.With(log.ModuleKey, "x/mint")), app.StakingKeeper, app.AuthKeeper, app.BankKeeper, authtypes.FeeCollectorName, govModuleAddr)

app.PoolKeeper = poolkeeper.NewKeeper(appCodec, runtime.NewEnvironment(runtime.NewKVStoreService(keys[pooltypes.StoreKey]), logger.With(log.ModuleKey, "x/protocolpool")), app.AuthKeeper, app.BankKeeper, app.StakingKeeper, govModuleAddr)
app.PoolKeeper = poolkeeper.NewKeeper(appCodec, runtime.NewEnvironment(runtime.NewKVStoreService(keys[pooltypes.StoreKey]), logger.With(log.ModuleKey, "x/protocolpool")), app.AuthKeeper, app.BankKeeper, govModuleAddr)

app.DistrKeeper = distrkeeper.NewKeeper(appCodec, runtime.NewEnvironment(runtime.NewKVStoreService(keys[distrtypes.StoreKey]), logger.With(log.ModuleKey, "x/distribution")), app.AuthKeeper, app.BankKeeper, app.StakingKeeper, cometService, authtypes.FeeCollectorName, govModuleAddr)

Expand Down
2 changes: 1 addition & 1 deletion tests/integration/distribution/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func initFixture(t *testing.T) *fixture {
stakingKeeper := stakingkeeper.NewKeeper(cdc, runtime.NewEnvironment(runtime.NewKVStoreService(keys[stakingtypes.StoreKey]), log.NewNopLogger(), runtime.EnvWithQueryRouterService(grpcRouter), runtime.EnvWithMsgRouterService(msgRouter)), accountKeeper, bankKeeper, consensusParamsKeeper, authority.String(), addresscodec.NewBech32Codec(sdk.Bech32PrefixValAddr), addresscodec.NewBech32Codec(sdk.Bech32PrefixConsAddr), cometService)
require.NoError(t, stakingKeeper.Params.Set(newCtx, stakingtypes.DefaultParams()))

poolKeeper := poolkeeper.NewKeeper(cdc, runtime.NewEnvironment(runtime.NewKVStoreService(keys[pooltypes.StoreKey]), log.NewNopLogger()), accountKeeper, bankKeeper, stakingKeeper, authority.String())
poolKeeper := poolkeeper.NewKeeper(cdc, runtime.NewEnvironment(runtime.NewKVStoreService(keys[pooltypes.StoreKey]), log.NewNopLogger()), accountKeeper, bankKeeper, authority.String())

distrKeeper := distrkeeper.NewKeeper(
cdc, runtime.NewEnvironment(runtime.NewKVStoreService(keys[distrtypes.StoreKey]), logger), accountKeeper, bankKeeper, stakingKeeper, cometService, distrtypes.ModuleName, authority.String(),
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/gov/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func initFixture(tb testing.TB) *fixture {

stakingKeeper := stakingkeeper.NewKeeper(cdc, runtime.NewEnvironment(runtime.NewKVStoreService(keys[stakingtypes.StoreKey]), log.NewNopLogger()), accountKeeper, bankKeeper, consensusParamsKeeper, authority.String(), addresscodec.NewBech32Codec(sdk.Bech32PrefixValAddr), addresscodec.NewBech32Codec(sdk.Bech32PrefixConsAddr), runtime.NewContextAwareCometInfoService())

poolKeeper := poolkeeper.NewKeeper(cdc, runtime.NewEnvironment(runtime.NewKVStoreService(keys[pooltypes.StoreKey]), log.NewNopLogger()), accountKeeper, bankKeeper, stakingKeeper, authority.String())
poolKeeper := poolkeeper.NewKeeper(cdc, runtime.NewEnvironment(runtime.NewKVStoreService(keys[pooltypes.StoreKey]), log.NewNopLogger()), accountKeeper, bankKeeper, authority.String())

// set default staking params
err := stakingKeeper.Params.Set(newCtx, stakingtypes.DefaultParams())
Expand Down
8 changes: 8 additions & 0 deletions x/protocolpool/autocli.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions {
},
GovProposal: true,
},
{
RpcMethod: "UpdateParams",
Use: "update-params-proposal <params>",
Short: "Submit a proposal to update protocolpool module params. Note: the entire params must be provided.",
Example: fmt.Sprintf(`%s tx protocolpool update-params-proposal '{ "enabled_distribution_denoms": ["stake", "foo"] }'`, version.AppName),
PositionalArgs: []*autocliv1.PositionalArgDescriptor{{ProtoField: "params"}},
GovProposal: true,
},
},
},
}
Expand Down
3 changes: 1 addition & 2 deletions x/protocolpool/depinject.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ type ModuleInputs struct {

AccountKeeper types.AccountKeeper
BankKeeper types.BankKeeper
StakingKeeper types.StakingKeeper
}

type ModuleOutputs struct {
Expand All @@ -59,7 +58,7 @@ func ProvideModule(in ModuleInputs) ModuleOutputs {
panic(err)
}

k := keeper.NewKeeper(in.Codec, in.Environment, in.AccountKeeper, in.BankKeeper, in.StakingKeeper, authorityAddr)
k := keeper.NewKeeper(in.Codec, in.Environment, in.AccountKeeper, in.BankKeeper, authorityAddr)
m := NewAppModule(in.Codec, k, in.AccountKeeper, in.BankKeeper)

return ModuleOutputs{
Expand Down
30 changes: 25 additions & 5 deletions x/protocolpool/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@ import (
"fmt"
"time"

"cosmossdk.io/math"
"cosmossdk.io/x/protocolpool/types"

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

func (k Keeper) InitGenesis(ctx context.Context, data *types.GenesisState) error {
currentTime := k.HeaderService.HeaderInfo(ctx).Time

err := k.Params.Set(ctx, *data.Params)
if err != nil {
return fmt.Errorf("failed to set params: %w", err)
}

for _, cf := range data.ContinuousFund {
// ignore expired ContinuousFunds
if cf.Expiry != nil && cf.Expiry.Before(currentTime) {
Expand Down Expand Up @@ -55,19 +60,25 @@ func (k Keeper) InitGenesis(ctx context.Context, data *types.GenesisState) error
return fmt.Errorf("failed to set last balance: %w", err)
}

totalToBeDistributed := math.ZeroInt()
totalToBeDistributed := sdk.NewCoins()
for _, distribution := range data.Distributions {
totalToBeDistributed = totalToBeDistributed.Add(distribution.Amount)
totalToBeDistributed = totalToBeDistributed.Add(distribution.Amount.Amount...)
if err := k.Distributions.Set(ctx, *distribution.Time, distribution.Amount); err != nil {
return fmt.Errorf("failed to set distribution: %w", err)
}
}

// sanity check to avoid trying to distribute more than what is available
<<<<<<< HEAD
if data.LastBalance.LT(totalToBeDistributed) {
return fmt.Errorf("total to be distributed is greater than the last balance")
}
=======
>>>>>>> 13c234f42 (feat(x/protocolpool)!: allow any coins in continuous funds (#21916))

if totalToBeDistributed.IsAnyGT(data.LastBalance.Amount) || !totalToBeDistributed.DenomsSubsetOf(data.LastBalance.Amount) {
return fmt.Errorf("total to be distributed is greater than the last balance: %s > %s", totalToBeDistributed, data.LastBalance.Amount)
}

Check warning

Code scanning / CodeQL

Unreachable statement Warning

This statement is unreachable.
return nil
}

Expand Down Expand Up @@ -111,12 +122,14 @@ func (k Keeper) ExportGenesis(ctx context.Context) (*types.GenesisState, error)

genState := types.NewGenesisState(cf, budget)

genState.LastBalance, err = k.LastBalance.Get(ctx)
lastBalance, err := k.LastBalance.Get(ctx)
if err != nil {
return nil, err
}

err = k.Distributions.Walk(ctx, nil, func(key time.Time, value math.Int) (stop bool, err error) {
genState.LastBalance = lastBalance

err = k.Distributions.Walk(ctx, nil, func(key time.Time, value types.DistributionAmount) (stop bool, err error) {
genState.Distributions = append(genState.Distributions, &types.Distribution{
Time: &key,
Amount: value,
Expand All @@ -128,5 +141,12 @@ func (k Keeper) ExportGenesis(ctx context.Context) (*types.GenesisState, error)
return nil, err
}

params, err := k.Params.Get(ctx)
if err != nil {
return nil, err
}

genState.Params = &params

return genState, nil
}
6 changes: 3 additions & 3 deletions x/protocolpool/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ func (suite *KeeperTestSuite) TestInitGenesis() {
)

gs.Distributions = append(gs.Distributions, &types.Distribution{
Amount: math.OneInt(),
Amount: types.DistributionAmount{Amount: sdk.NewCoins(sdk.NewCoin("stake", math.NewInt(100)))},
Time: &time.Time{},
})

err := suite.poolKeeper.InitGenesis(suite.ctx, gs)
suite.Require().ErrorContains(err, "total to be distributed is greater than the last balance")

// Set last balance
gs.LastBalance = math.NewInt(1)
gs.LastBalance = types.DistributionAmount{Amount: sdk.NewCoins(sdk.NewCoin("stake", math.NewInt(101)))}
err = suite.poolKeeper.InitGenesis(suite.ctx, gs)
suite.Require().NoError(err)

Expand All @@ -49,5 +49,5 @@ func (suite *KeeperTestSuite) TestInitGenesis() {
suite.Require().NoError(err)
suite.Require().Equal(gs.ContinuousFund, exportedGenState.ContinuousFund)
suite.Require().Equal(gs.Budget, exportedGenState.Budget)
suite.Require().Equal(math.OneInt(), exportedGenState.LastBalance)
suite.Require().Equal(math.NewInt(101), exportedGenState.LastBalance.Amount.AmountOf("stake"))
}
Loading

0 comments on commit bb57927

Please sign in to comment.