Skip to content

Commit

Permalink
refactor(x/protocolpool)!: Reducing complexity and removing some bugs (
Browse files Browse the repository at this point in the history
  • Loading branch information
facundomedica authored Jun 28, 2024
1 parent 488e74c commit d426a5d
Show file tree
Hide file tree
Showing 14 changed files with 565 additions and 191 deletions.
29 changes: 29 additions & 0 deletions tests/integration/protocolpool/app_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package protocolpool

import (
_ "cosmossdk.io/x/accounts" // import as blank for app wiring
_ "cosmossdk.io/x/auth" // import as blank for app wiring
_ "cosmossdk.io/x/auth/tx/config" // import as blank for app wiring
_ "cosmossdk.io/x/bank" // import as blank for app wiring
_ "cosmossdk.io/x/consensus" // import as blank for app wiring
_ "cosmossdk.io/x/distribution" // import as blank for app wiring
_ "cosmossdk.io/x/mint" // import as blank for app wiring
_ "cosmossdk.io/x/protocolpool" // import as blank for app wiring
_ "cosmossdk.io/x/staking" // import as blank for app wiring

"github.com/cosmos/cosmos-sdk/testutil/configurator"
_ "github.com/cosmos/cosmos-sdk/x/genutil" // import as blank for app wiring
)

var AppConfig = configurator.NewAppConfig(
configurator.AccountsModule(),
configurator.AuthModule(),
configurator.BankModule(),
configurator.StakingModule(),
configurator.TxModule(),
configurator.ConsensusModule(),
configurator.GenutilModule(),
configurator.MintModule(),
configurator.DistributionModule(),
configurator.ProtocolPoolModule(),
)
138 changes: 138 additions & 0 deletions tests/integration/protocolpool/module_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package protocolpool

import (
"math/rand"
"testing"
"time"

"github.com/stretchr/testify/require"

"cosmossdk.io/core/header"
"cosmossdk.io/depinject"
"cosmossdk.io/log"
"cosmossdk.io/math"
authkeeper "cosmossdk.io/x/auth/keeper"
authtypes "cosmossdk.io/x/auth/types"
bankkeeper "cosmossdk.io/x/bank/keeper"
"cosmossdk.io/x/mint/types"
protocolpoolkeeper "cosmossdk.io/x/protocolpool/keeper"
protocolpooltypes "cosmossdk.io/x/protocolpool/types"
stakingkeeper "cosmossdk.io/x/staking/keeper"

simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// TestWithdrawAnytime tests if withdrawing funds many times vs withdrawing funds once
// yield the same end balance.
func TestWithdrawAnytime(t *testing.T) {
var accountKeeper authkeeper.AccountKeeper
var protocolpoolKeeper protocolpoolkeeper.Keeper
var bankKeeper bankkeeper.Keeper
var stakingKeeper *stakingkeeper.Keeper

app, err := simtestutil.SetupAtGenesis(
depinject.Configs(
AppConfig,
depinject.Supply(log.NewNopLogger()),
), &accountKeeper, &protocolpoolKeeper, &bankKeeper, &stakingKeeper)
require.NoError(t, err)

ctx := app.BaseApp.NewContext(false).WithBlockHeight(1).WithHeaderInfo(header.Info{Height: 1})
acc := accountKeeper.GetAccount(ctx, authtypes.NewModuleAddress(types.ModuleName))
require.NotNil(t, acc)

testAddrs := simtestutil.AddTestAddrs(bankKeeper, stakingKeeper, ctx, 5, math.NewInt(1))
testAddr0Str, err := accountKeeper.AddressCodec().BytesToString(testAddrs[0])
require.NoError(t, err)

msgServer := protocolpoolkeeper.NewMsgServerImpl(protocolpoolKeeper)
_, err = msgServer.CreateContinuousFund(
ctx,
&protocolpooltypes.MsgCreateContinuousFund{
Authority: protocolpoolKeeper.GetAuthority(),
Recipient: testAddr0Str,
Percentage: math.LegacyMustNewDecFromStr("0.5"),
},
)
require.NoError(t, err)

// increase the community pool by a bunch
for i := 0; i < 30; i++ {
ctx, err = simtestutil.NextBlock(app, ctx, time.Minute)
require.NoError(t, err)

// withdraw funds randomly, but it must always land on the same end balance
if rand.Intn(100) > 50 {
_, err = msgServer.WithdrawContinuousFund(ctx, &protocolpooltypes.MsgWithdrawContinuousFund{
RecipientAddress: testAddr0Str,
})
require.NoError(t, err)
}
}

pool, err := protocolpoolKeeper.GetCommunityPool(ctx)
require.NoError(t, err)
require.True(t, pool.IsAllGT(sdk.NewCoins(sdk.NewInt64Coin("stake", 100000))))

_, err = msgServer.WithdrawContinuousFund(ctx, &protocolpooltypes.MsgWithdrawContinuousFund{
RecipientAddress: testAddr0Str,
})
require.NoError(t, err)

endBalance := bankKeeper.GetBalance(ctx, testAddrs[0], sdk.DefaultBondDenom)
require.Equal(t, "11883031stake", endBalance.String())
}

// TestExpireInTheMiddle tests if a continuous fund that expires without anyone
// calling the withdraw function, the funds are still distributed correctly.
func TestExpireInTheMiddle(t *testing.T) {
t.Skip("This is a bug @facu found, will fix in another PR")
var accountKeeper authkeeper.AccountKeeper
var protocolpoolKeeper protocolpoolkeeper.Keeper
var bankKeeper bankkeeper.Keeper
var stakingKeeper *stakingkeeper.Keeper

app, err := simtestutil.SetupAtGenesis(
depinject.Configs(
AppConfig,
depinject.Supply(log.NewNopLogger()),
), &accountKeeper, &protocolpoolKeeper, &bankKeeper, &stakingKeeper)
require.NoError(t, err)

ctx := app.BaseApp.NewContext(false).WithBlockHeight(1).WithHeaderInfo(header.Info{Height: 1})
acc := accountKeeper.GetAccount(ctx, authtypes.NewModuleAddress(types.ModuleName))
require.NotNil(t, acc)

testAddrs := simtestutil.AddTestAddrs(bankKeeper, stakingKeeper, ctx, 5, math.NewInt(1))
testAddr0Str, err := accountKeeper.AddressCodec().BytesToString(testAddrs[0])
require.NoError(t, err)

msgServer := protocolpoolkeeper.NewMsgServerImpl(protocolpoolKeeper)

expirationTime := ctx.BlockTime().Add(time.Minute * 2)
_, err = msgServer.CreateContinuousFund(
ctx,
&protocolpooltypes.MsgCreateContinuousFund{
Authority: protocolpoolKeeper.GetAuthority(),
Recipient: testAddr0Str,
Percentage: math.LegacyMustNewDecFromStr("0.1"),
Expiry: &expirationTime,
},
)
require.NoError(t, err)

// increase the community pool by a bunch
for i := 0; i < 30; i++ {
ctx, err = simtestutil.NextBlock(app, ctx, time.Minute)
require.NoError(t, err)
}

_, err = msgServer.WithdrawContinuousFund(ctx, &protocolpooltypes.MsgWithdrawContinuousFund{
RecipientAddress: testAddr0Str,
})
require.Error(t, err)

endBalance := bankKeeper.GetBalance(ctx, testAddrs[0], sdk.DefaultBondDenom)
require.Equal(t, "158441stake", endBalance.String())
}
42 changes: 42 additions & 0 deletions x/protocolpool/keeper/genesis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package keeper_test

import (
"time"

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

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

func (suite *KeeperTestSuite) TestInitGenesis() {
hour := time.Hour
gs := types.NewGenesisState(
[]*types.ContinuousFund{
{
Recipient: "cosmos1qy3529yj3v4xw2z3vz3vz3vz3vz3vz3v3k0vyf",
Percentage: math.LegacyMustNewDecFromStr("0.1"),
Expiry: nil,
},
},
[]*types.Budget{
{
RecipientAddress: "cosmos1qy3529yj3v4xw2z3vz3vz3vz3vz3vz3v3k0vyf",
ClaimedAmount: &sdk.Coin{},
LastClaimedAt: &time.Time{},
TranchesLeft: 10,
BudgetPerTranche: &sdk.Coin{Denom: "stake", Amount: math.NewInt(100)},
Period: &hour,
},
},
)

err := suite.poolKeeper.InitGenesis(suite.ctx, gs)
suite.Require().NoError(err)

// Export
exportedGenState, err := suite.poolKeeper.ExportGenesis(suite.ctx)
suite.Require().NoError(err)
suite.Require().Equal(gs.ContinuousFund, exportedGenState.ContinuousFund)
suite.Require().Equal(gs.Budget, exportedGenState.Budget)
}
Loading

0 comments on commit d426a5d

Please sign in to comment.