|
| 1 | +package v023_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "cosmossdk.io/core/appmodule" |
| 9 | + "cosmossdk.io/core/header" |
| 10 | + "cosmossdk.io/math" |
| 11 | + "cosmossdk.io/x/upgrade" |
| 12 | + upgradetypes "cosmossdk.io/x/upgrade/types" |
| 13 | + apptesting "github.com/bitsongofficial/go-bitsong/app/testing" |
| 14 | + v023 "github.com/bitsongofficial/go-bitsong/app/upgrades/v023" |
| 15 | + addresscodec "github.com/cosmos/cosmos-sdk/codec/address" |
| 16 | + "github.com/cosmos/cosmos-sdk/types" |
| 17 | + |
| 18 | + minttypes "github.com/cosmos/cosmos-sdk/x/mint/types" |
| 19 | + protocolpooltypes "github.com/cosmos/cosmos-sdk/x/protocolpool/types" |
| 20 | + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" |
| 21 | + |
| 22 | + distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types" |
| 23 | + |
| 24 | + "github.com/stretchr/testify/suite" |
| 25 | +) |
| 26 | + |
| 27 | +const dummyUpgradeHeight = 5 |
| 28 | + |
| 29 | +var testDescription = stakingtypes.NewDescription("test_moniker", "test_identity", "test_website", "test_security_contact", "test_details") |
| 30 | + |
| 31 | +type UpgradeTestSuite struct { |
| 32 | + apptesting.KeeperTestHelper |
| 33 | + preModule appmodule.HasPreBlocker |
| 34 | +} |
| 35 | + |
| 36 | +func (s *UpgradeTestSuite) SetupTest() { |
| 37 | + s.Setup() |
| 38 | + s.preModule = upgrade.NewAppModule(s.App.UpgradeKeeper, addresscodec.NewBech32Codec("bitsong")) |
| 39 | +} |
| 40 | + |
| 41 | +func TestUpgradeTestSuite(t *testing.T) { |
| 42 | + suite.Run(t, new(UpgradeTestSuite)) |
| 43 | +} |
| 44 | + |
| 45 | +func (s *UpgradeTestSuite) TestUpgrade() { |
| 46 | + coin := types.NewCoin("ft12345", math.NewInt(12345)) |
| 47 | + communityPoolFunds := types.NewCoins(types.NewCoin("ubtsg", math.NewInt(69420)), types.NewCoin("ftfantoken", math.NewInt(1234567890))) |
| 48 | + protocolpoolEscrow := protocolpooltypes.ProtocolPoolEscrowAccount |
| 49 | + protocolpool := protocolpooltypes.ModuleName |
| 50 | + distributionModule := distrtypes.ModuleName |
| 51 | + |
| 52 | + upgradeSetup := func() { |
| 53 | + s.Ctx = s.Ctx.WithBlockHeight(dummyUpgradeHeight - 2) |
| 54 | + s.FundAcc(s.TestAccs[0], types.NewCoins(coin)) |
| 55 | + |
| 56 | + // fund protocolpool_escrow (cannot use protocolpool fund-community-pool since we have disbabled external community pool, so we just seed with balance to module account) |
| 57 | + s.App.BankKeeper.MintCoins(s.Ctx, minttypes.ModuleName, communityPoolFunds) |
| 58 | + s.App.BankKeeper.SendCoinsFromModuleToModule(s.Ctx, minttypes.ModuleName, protocolpoolEscrow, communityPoolFunds) |
| 59 | + |
| 60 | + } |
| 61 | + |
| 62 | + postUpgrade := func() { |
| 63 | + s.Ctx = s.Ctx.WithBlockHeight(dummyUpgradeHeight + 1) |
| 64 | + |
| 65 | + // assert protocolpool & protocolpool_escrow are empty |
| 66 | + protocolPoolBalance := s.App.BankKeeper.GetAllBalances(s.Ctx, s.App.AccountKeeper.GetModuleAddress(protocolpool)) |
| 67 | + protocolPoolEscrowBalance := s.App.BankKeeper.GetAllBalances(s.Ctx, s.App.AccountKeeper.GetModuleAddress(protocolpoolEscrow)) |
| 68 | + distrModuleBalance := s.App.BankKeeper.GetAllBalances(s.Ctx, s.App.AccountKeeper.GetModuleAddress(distributionModule)) |
| 69 | + |
| 70 | + fmt.Printf("distrModuleBalance: %v\n", distrModuleBalance) |
| 71 | + fmt.Printf("protocolPoolBalance: %v\n", protocolPoolBalance) |
| 72 | + fmt.Printf("protocolPoolEscrowBalance: %v\n", protocolPoolEscrowBalance) |
| 73 | + |
| 74 | + s.Require().Equal(protocolPoolBalance.Empty(), true) |
| 75 | + s.Require().Equal(protocolPoolEscrowBalance.Empty(), true) |
| 76 | + |
| 77 | + // assert fund community pool does not error |
| 78 | + err := s.App.DistrKeeper.FundCommunityPool(s.Ctx, types.NewCoins(coin), s.TestAccs[0]) |
| 79 | + s.Require().NoError(err) |
| 80 | + |
| 81 | + // check for funded token in distribution module balance |
| 82 | + distrModuleBalance = s.App.BankKeeper.GetAllBalances(s.Ctx, s.App.AccountKeeper.GetModuleAddress(distributionModule)) |
| 83 | + amountOf := distrModuleBalance.AmountOf(coin.Denom) |
| 84 | + s.Require().Equal(amountOf, coin.Amount) |
| 85 | + |
| 86 | + // assert we are unblocking fund communityu pool requests |
| 87 | + s.Require().Equal(s.App.DistrKeeper.HasExternalCommunityPool(), false) |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | + testCases := []struct { |
| 92 | + name string |
| 93 | + pre_upgrade func() |
| 94 | + upgrade func() |
| 95 | + post_upgrade func() |
| 96 | + }{ |
| 97 | + { |
| 98 | + "test: protocol-pool patch", |
| 99 | + func() { |
| 100 | + upgradeSetup() |
| 101 | + }, |
| 102 | + func() { |
| 103 | + dummyUpgrade(s) |
| 104 | + s.Require().NotPanics(func() { |
| 105 | + _, err := s.preModule.PreBlock(s.Ctx) |
| 106 | + s.Require().NoError(err) |
| 107 | + }) |
| 108 | + |
| 109 | + }, |
| 110 | + func() { |
| 111 | + s.Ctx = s.Ctx.WithBlockHeight(dummyUpgradeHeight + 1) |
| 112 | + postUpgrade() |
| 113 | + }, |
| 114 | + }, |
| 115 | + } |
| 116 | + |
| 117 | + for _, tc := range testCases { |
| 118 | + s.Run(fmt.Sprintf("Case %s", tc.name), func() { |
| 119 | + s.SetupTest() // reset |
| 120 | + tc.pre_upgrade() |
| 121 | + tc.upgrade() |
| 122 | + tc.post_upgrade() |
| 123 | + }) |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +func dummyUpgrade(s *UpgradeTestSuite) { |
| 128 | + s.Ctx = s.Ctx.WithBlockHeight(dummyUpgradeHeight - 1) |
| 129 | + plan := upgradetypes.Plan{Name: v023.UpgradeName, Height: dummyUpgradeHeight} |
| 130 | + err := s.App.UpgradeKeeper.ScheduleUpgrade(s.Ctx, plan) |
| 131 | + s.Require().NoError(err) |
| 132 | + _, err = s.App.UpgradeKeeper.GetUpgradePlan(s.Ctx) |
| 133 | + s.Require().NoError(err) |
| 134 | + |
| 135 | + s.Ctx = s.Ctx.WithHeaderInfo(header.Info{Height: dummyUpgradeHeight, Time: s.Ctx.BlockTime().Add(time.Second)}).WithBlockHeight(dummyUpgradeHeight) |
| 136 | +} |
0 commit comments