refactor(x/protocolpool)!: Reducing complexity and removing some bugs (#20786)

This commit is contained in:
Facundo Medica
2024-06-28 15:53:26 +00:00
committed by GitHub
parent 488e74cc5d
commit d426a5db67
14 changed files with 570 additions and 196 deletions
@@ -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(),
)
@@ -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())
}