Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bank Refactor] Frojdi jonathan/remove setsupply #8491

Merged
32 changes: 12 additions & 20 deletions simapp/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"testing"
"time"

minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"

"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/log"
Expand Down Expand Up @@ -236,21 +238,11 @@ func createIncrementalAccounts(accNum int) []sdk.AccAddress {
func AddTestAddrsFromPubKeys(app *SimApp, ctx sdk.Context, pubKeys []cryptotypes.PubKey, accAmt sdk.Int) {
initCoins := sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), accAmt))

setTotalSupply(app, ctx, accAmt, len(pubKeys))

// fill all the addresses with some coins, set the loose pool tokens simultaneously
for _, pubKey := range pubKeys {
saveAccount(app, ctx, sdk.AccAddress(pubKey.Address()), initCoins)
for _, pk := range pubKeys {
initAccountWithCoins(app, ctx, sdk.AccAddress(pk.Address()), initCoins)
}
}

// setTotalSupply provides the total supply based on accAmt * totalAccounts.
func setTotalSupply(app *SimApp, ctx sdk.Context, accAmt sdk.Int, totalAccounts int) {
totalSupply := sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), accAmt.MulRaw(int64(totalAccounts))))
prevSupply := app.BankKeeper.GetSupply(ctx)
app.BankKeeper.SetSupply(ctx, banktypes.NewSupply(prevSupply.GetTotal().Add(totalSupply...)))
}

// AddTestAddrs constructs and returns accNum amount of accounts with an
// initial balance of accAmt in random order
func AddTestAddrs(app *SimApp, ctx sdk.Context, accNum int, accAmt sdk.Int) []sdk.AccAddress {
Expand All @@ -267,21 +259,21 @@ func addTestAddrs(app *SimApp, ctx sdk.Context, accNum int, accAmt sdk.Int, stra
testAddrs := strategy(accNum)

initCoins := sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), accAmt))
setTotalSupply(app, ctx, accAmt, accNum)

// fill all the addresses with some coins, set the loose pool tokens simultaneously
for _, addr := range testAddrs {
saveAccount(app, ctx, addr, initCoins)
initAccountWithCoins(app, ctx, addr, initCoins)
}

return testAddrs
}

// saveAccount saves the provided account into the simapp with balance based on initCoins.
func saveAccount(app *SimApp, ctx sdk.Context, addr sdk.AccAddress, initCoins sdk.Coins) {
acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr)
app.AccountKeeper.SetAccount(ctx, acc)
err := app.BankKeeper.AddCoins(ctx, addr, initCoins)
func initAccountWithCoins(app *SimApp, ctx sdk.Context, addr sdk.AccAddress, coins sdk.Coins) {
err := app.BankKeeper.MintCoins(ctx, minttypes.ModuleName, coins)
if err != nil {
panic(err)
}

err = app.BankKeeper.SendCoinsFromModuleToAccount(ctx, minttypes.ModuleName, addr, coins)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion x/bank/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (k BaseKeeper) InitGenesis(ctx sdk.Context, genState *types.GenesisState) {
genState.Supply = totalSupply
}

k.SetSupply(ctx, types.NewSupply(genState.Supply))
k.setSupply(ctx, types.NewSupply(genState.Supply))

for _, meta := range genState.DenomMetadata {
k.SetDenomMetaData(ctx, meta)
Expand Down
32 changes: 21 additions & 11 deletions x/bank/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,53 @@ package keeper_test
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/bank/types"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
)

func (suite *IntegrationTestSuite) TestExportGenesis() {
app, ctx := suite.app, suite.ctx

expectedMetadata := suite.getTestMetadata()
expectedBalances := suite.getTestBalances()
expectedBalances, totalSupply := suite.getTestBalancesAndSupply()
for i := range []int{1, 2} {
app.BankKeeper.SetDenomMetaData(ctx, expectedMetadata[i])
accAddr, err1 := sdk.AccAddressFromBech32(expectedBalances[i].Address)
if err1 != nil {
panic(err1)
}
err := app.BankKeeper.SetBalances(ctx, accAddr, expectedBalances[i].Coins)
suite.Require().NoError(err)
// set balances via mint and send
suite.
Require().
NoError(app.BankKeeper.MintCoins(ctx, minttypes.ModuleName, expectedBalances[i].Coins))
suite.
Require().
NoError(app.BankKeeper.SendCoinsFromModuleToAccount(ctx, minttypes.ModuleName, accAddr, expectedBalances[i].Coins))
}

totalSupply := types.NewSupply(sdk.NewCoins(sdk.NewInt64Coin("test", 400000000)))
app.BankKeeper.SetSupply(ctx, totalSupply)
// add mint module balance as nil
expectedBalances = append(expectedBalances, types.Balance{Address: "cosmos1m3h30wlvsf8llruxtpukdvsy0km2kum8g38c8q", Coins: nil})
app.BankKeeper.SetParams(ctx, types.DefaultParams())

exportGenesis := app.BankKeeper.ExportGenesis(ctx)

suite.Require().Len(exportGenesis.Params.SendEnabled, 0)
suite.Require().Equal(types.DefaultParams().DefaultSendEnabled, exportGenesis.Params.DefaultSendEnabled)
suite.Require().Equal(totalSupply.GetTotal(), exportGenesis.Supply)
suite.Require().Equal(totalSupply.Total, exportGenesis.Supply)
suite.Require().Equal(expectedBalances, exportGenesis.Balances)
suite.Require().Equal(expectedMetadata, exportGenesis.DenomMetadata)
}

func (suite *IntegrationTestSuite) getTestBalances() []types.Balance {
func (suite *IntegrationTestSuite) getTestBalancesAndSupply() ([]types.Balance, *types.Supply) {
addr2, _ := sdk.AccAddressFromBech32("cosmos1f9xjhxm0plzrh9cskf4qee4pc2xwp0n0556gh0")
addr1, _ := sdk.AccAddressFromBech32("cosmos1fl48vsnmsdzcv85q5d2q4z5ajdha8yu34mf0eh")
addr1Balance := sdk.Coins{sdk.NewInt64Coin("testcoin3", 10)}
addr2Balance := sdk.Coins{sdk.NewInt64Coin("testcoin1", 32), sdk.NewInt64Coin("testcoin2", 34)}

totalSupply := types.NewSupply(addr1Balance)
totalSupply.Inflate(addr2Balance)
return []types.Balance{
{Address: addr2.String(), Coins: sdk.Coins{sdk.NewInt64Coin("testcoin1", 32), sdk.NewInt64Coin("testcoin2", 34)}},
{Address: addr1.String(), Coins: sdk.Coins{sdk.NewInt64Coin("testcoin3", 10)}},
}
{Address: addr2.String(), Coins: addr2Balance},
{Address: addr1.String(), Coins: addr1Balance},
}, totalSupply

}

Expand Down
10 changes: 8 additions & 2 deletions x/bank/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
gocontext "context"
"fmt"

minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"

"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
Expand Down Expand Up @@ -87,7 +89,9 @@ func (suite *IntegrationTestSuite) TestQueryAllBalances() {
func (suite *IntegrationTestSuite) TestQueryTotalSupply() {
app, ctx, queryClient := suite.app, suite.ctx, suite.queryClient
expectedTotalSupply := types.NewSupply(sdk.NewCoins(sdk.NewInt64Coin("test", 400000000)))
app.BankKeeper.SetSupply(ctx, expectedTotalSupply)
suite.
Require().
NoError(app.BankKeeper.MintCoins(ctx, minttypes.ModuleName, expectedTotalSupply.Total))

res, err := queryClient.TotalSupply(gocontext.Background(), &types.QueryTotalSupplyRequest{})
suite.Require().NoError(err)
Expand All @@ -102,7 +106,9 @@ func (suite *IntegrationTestSuite) TestQueryTotalSupplyOf() {
test1Supply := sdk.NewInt64Coin("test1", 4000000)
test2Supply := sdk.NewInt64Coin("test2", 700000000)
expectedTotalSupply := types.NewSupply(sdk.NewCoins(test1Supply, test2Supply))
app.BankKeeper.SetSupply(ctx, expectedTotalSupply)
suite.
Require().
NoError(app.BankKeeper.MintCoins(ctx, minttypes.ModuleName, expectedTotalSupply.Total))

_, err := queryClient.SupplyOf(gocontext.Background(), &types.QuerySupplyOfRequest{})
suite.Require().Error(err)
Expand Down
9 changes: 4 additions & 5 deletions x/bank/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ type Keeper interface {
ExportGenesis(sdk.Context) *types.GenesisState

GetSupply(ctx sdk.Context) exported.SupplyI
SetSupply(ctx sdk.Context, supply exported.SupplyI)

GetDenomMetaData(ctx sdk.Context, denom string) (types.Metadata, bool)
SetDenomMetaData(ctx sdk.Context, denomMetaData types.Metadata)
Expand Down Expand Up @@ -168,8 +167,8 @@ func (k BaseKeeper) GetSupply(ctx sdk.Context) exported.SupplyI {
return supply
}

// SetSupply sets the Supply to store
func (k BaseKeeper) SetSupply(ctx sdk.Context, supply exported.SupplyI) {
// setSupply sets the Supply to store
func (k BaseKeeper) setSupply(ctx sdk.Context, supply exported.SupplyI) {
store := ctx.KVStore(k.storeKey)
bz, err := k.MarshalSupply(supply)
if err != nil {
Expand Down Expand Up @@ -341,7 +340,7 @@ func (k BaseKeeper) MintCoins(ctx sdk.Context, moduleName string, amt sdk.Coins)
supply := k.GetSupply(ctx)
supply.Inflate(amt)

k.SetSupply(ctx, supply)
k.setSupply(ctx, supply)

logger := k.Logger(ctx)
logger.Info("minted coins from module account", "amount", amt.String(), "from", moduleName)
Expand Down Expand Up @@ -369,7 +368,7 @@ func (k BaseKeeper) BurnCoins(ctx sdk.Context, moduleName string, amt sdk.Coins)
// update total supply
supply := k.GetSupply(ctx)
supply.Deflate(amt)
k.SetSupply(ctx, supply)
k.setSupply(ctx, supply)

logger := k.Logger(ctx)
logger.Info("burned tokens from module account", "amount", amt.String(), "from", moduleName)
Expand Down
61 changes: 39 additions & 22 deletions x/bank/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"testing"
"time"

minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"

"github.com/stretchr/testify/suite"
abci "github.com/tendermint/tendermint/abci/types"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
Expand Down Expand Up @@ -89,7 +91,7 @@ func (suite *IntegrationTestSuite) TestSupply() {
initTokens := sdk.TokensFromConsensusPower(initialPower)

totalSupply := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initTokens))
app.BankKeeper.SetSupply(ctx, types.NewSupply(totalSupply))
suite.NoError(app.BankKeeper.MintCoins(ctx, minttypes.ModuleName, totalSupply))

total := app.BankKeeper.GetSupply(ctx).GetTotal()
suite.Require().Equal(totalSupply, total)
Expand Down Expand Up @@ -118,23 +120,30 @@ func (suite *IntegrationTestSuite) TestSupply_SendCoins() {
)

baseAcc := authKeeper.NewAccountWithAddress(ctx, authtypes.NewModuleAddress("baseAcc"))
suite.Require().NoError(keeper.SetBalances(ctx, holderAcc.GetAddress(), initCoins))

keeper.SetSupply(ctx, types.NewSupply(initCoins))
// set initial balances
suite.
Require().
NoError(keeper.MintCoins(ctx, minttypes.ModuleName, initCoins))

suite.
Require().
NoError(keeper.SendCoinsFromModuleToAccount(ctx, minttypes.ModuleName, holderAcc.GetAddress(), initCoins))

authKeeper.SetModuleAccount(ctx, holderAcc)
authKeeper.SetModuleAccount(ctx, burnerAcc)
authKeeper.SetAccount(ctx, baseAcc)

suite.Require().Panics(func() {
keeper.SendCoinsFromModuleToModule(ctx, "", holderAcc.GetName(), initCoins) // nolint:errcheck
_ = keeper.SendCoinsFromModuleToModule(ctx, "", holderAcc.GetName(), initCoins) // nolint:errcheck
})

suite.Require().Panics(func() {
keeper.SendCoinsFromModuleToModule(ctx, authtypes.Burner, "", initCoins) // nolint:errcheck
_ = keeper.SendCoinsFromModuleToModule(ctx, authtypes.Burner, "", initCoins) // nolint:errcheck
})

suite.Require().Panics(func() {
keeper.SendCoinsFromModuleToAccount(ctx, "", baseAcc.GetAddress(), initCoins) // nolint:errcheck
_ = keeper.SendCoinsFromModuleToAccount(ctx, "", baseAcc.GetAddress(), initCoins) // nolint:errcheck
})

suite.Require().Error(
Expand Down Expand Up @@ -234,37 +243,45 @@ func (suite *IntegrationTestSuite) TestSupply_BurnCoins() {
app.GetSubspace(types.ModuleName), make(map[string]bool),
)

suite.Require().NoError(keeper.SetBalances(ctx, burnerAcc.GetAddress(), initCoins))
keeper.SetSupply(ctx, types.NewSupply(initCoins))
// set burnerAcc balance
authKeeper.SetModuleAccount(ctx, burnerAcc)

initialSupply := keeper.GetSupply(ctx)
initialSupply.Inflate(initCoins)
keeper.SetSupply(ctx, initialSupply)

suite.Require().Panics(func() { keeper.BurnCoins(ctx, "", initCoins) }, "no module account") // nolint:errcheck
suite.Require().Panics(func() { keeper.BurnCoins(ctx, authtypes.Minter, initCoins) }, "invalid permission") // nolint:errcheck
suite.Require().Panics(func() { keeper.BurnCoins(ctx, randomPerm, initialSupply.GetTotal()) }, "random permission") // nolint:errcheck
err := keeper.BurnCoins(ctx, authtypes.Burner, initialSupply.GetTotal())
suite.
Require().
NoError(keeper.MintCoins(ctx, authtypes.Minter, initCoins))
suite.
Require().
NoError(keeper.SendCoinsFromModuleToAccount(ctx, authtypes.Minter, burnerAcc.GetAddress(), initCoins))

// inflate supply
suite.
Require().
NoError(keeper.MintCoins(ctx, authtypes.Minter, initCoins))
supplyAfterInflation := keeper.GetSupply(ctx)

suite.Require().Panics(func() { keeper.BurnCoins(ctx, "", initCoins) }, "no module account") // nolint:errcheck
suite.Require().Panics(func() { keeper.BurnCoins(ctx, authtypes.Minter, initCoins) }, "invalid permission") // nolint:errcheck
suite.Require().Panics(func() { keeper.BurnCoins(ctx, randomPerm, supplyAfterInflation.GetTotal()) }, "random permission") // nolint:errcheck
err := keeper.BurnCoins(ctx, authtypes.Burner, supplyAfterInflation.GetTotal())
suite.Require().Error(err, "insufficient coins")

err = keeper.BurnCoins(ctx, authtypes.Burner, initCoins)
suite.Require().NoError(err)
suite.Require().Equal(sdk.Coins(nil), getCoinsByName(ctx, keeper, authKeeper, authtypes.Burner))
suite.Require().Equal(initialSupply.GetTotal().Sub(initCoins), keeper.GetSupply(ctx).GetTotal())
suite.Require().Equal(supplyAfterInflation.GetTotal().Sub(initCoins), keeper.GetSupply(ctx).GetTotal())

// test same functionality on module account with multiple permissions
initialSupply = keeper.GetSupply(ctx)
initialSupply.Inflate(initCoins)
keeper.SetSupply(ctx, initialSupply)
suite.
Require().
NoError(keeper.MintCoins(ctx, authtypes.Minter, initCoins))
supplyAfterInflation = keeper.GetSupply(ctx)

suite.Require().NoError(keeper.SetBalances(ctx, multiPermAcc.GetAddress(), initCoins))
authKeeper.SetModuleAccount(ctx, multiPermAcc)

err = keeper.BurnCoins(ctx, multiPermAcc.GetName(), initCoins)
suite.Require().NoError(err)
suite.Require().Equal(sdk.Coins(nil), getCoinsByName(ctx, keeper, authKeeper, multiPermAcc.GetName()))
suite.Require().Equal(initialSupply.GetTotal().Sub(initCoins), keeper.GetSupply(ctx).GetTotal())
suite.Require().Equal(supplyAfterInflation.GetTotal().Sub(initCoins), keeper.GetSupply(ctx).GetTotal())
}

func (suite *IntegrationTestSuite) TestSendCoinsNewAccount() {
Expand Down
10 changes: 8 additions & 2 deletions x/bank/keeper/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package keeper_test
import (
"fmt"

minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"

abci "github.com/tendermint/tendermint/abci/types"

"github.com/cosmos/cosmos-sdk/testutil/testdata"
Expand Down Expand Up @@ -89,7 +91,9 @@ func (suite *IntegrationTestSuite) TestQuerier_QueryTotalSupply() {
app, ctx := suite.app, suite.ctx
legacyAmino := app.LegacyAmino()
expectedTotalSupply := types.NewSupply(sdk.NewCoins(sdk.NewInt64Coin("test", 400000000)))
app.BankKeeper.SetSupply(ctx, expectedTotalSupply)
suite.
Require().
NoError(app.BankKeeper.MintCoins(ctx, minttypes.ModuleName, expectedTotalSupply.Total))

req := abci.RequestQuery{
Path: fmt.Sprintf("custom/%s/%s", types.ModuleName, types.QueryTotalSupply),
Expand Down Expand Up @@ -118,7 +122,9 @@ func (suite *IntegrationTestSuite) TestQuerier_QueryTotalSupplyOf() {
test1Supply := sdk.NewInt64Coin("test1", 4000000)
test2Supply := sdk.NewInt64Coin("test2", 700000000)
expectedTotalSupply := types.NewSupply(sdk.NewCoins(test1Supply, test2Supply))
app.BankKeeper.SetSupply(ctx, expectedTotalSupply)
suite.
Require().
NoError(app.BankKeeper.MintCoins(ctx, minttypes.ModuleName, expectedTotalSupply.Total))

req := abci.RequestQuery{
Path: fmt.Sprintf("custom/%s/%s", types.ModuleName, types.QuerySupplyOf),
Expand Down
9 changes: 2 additions & 7 deletions x/crisis/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@ import (
"testing"

"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/libs/log"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
dbm "github.com/tendermint/tm-db"

"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/crisis"
"github.com/cosmos/cosmos-sdk/x/crisis/types"
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
Expand All @@ -26,9 +23,8 @@ var (
)

func createTestApp() (*simapp.SimApp, sdk.Context, []sdk.AccAddress) {
db := dbm.NewMemDB()
app := simapp.NewSimApp(log.NewNopLogger(), db, nil, true, map[int64]bool{}, simapp.DefaultNodeHome, 1, simapp.MakeTestEncodingConfig(), simapp.EmptyAppOptions{})
ctx := app.NewContext(true, tmproto.Header{})
app := simapp.Setup(false)
ctx := app.NewContext(false, tmproto.Header{})

constantFee := sdk.NewInt64Coin(sdk.DefaultBondDenom, 10)
app.CrisisKeeper.SetConstantFee(ctx, constantFee)
Expand All @@ -40,7 +36,6 @@ func createTestApp() (*simapp.SimApp, sdk.Context, []sdk.AccAddress) {
feePool := distrtypes.InitialFeePool()
feePool.CommunityPool = sdk.NewDecCoinsFromCoins(sdk.NewCoins(constantFee)...)
app.DistrKeeper.SetFeePool(ctx, feePool)
app.BankKeeper.SetSupply(ctx, banktypes.NewSupply(sdk.Coins{}))

addrs := simapp.AddTestAddrs(app, ctx, 1, sdk.NewInt(10000))

Expand Down
Loading