From e532bc4f9c939e0d5157c90f1bf5fcf3e23b4058 Mon Sep 17 00:00:00 2001 From: Nicolas Lara Date: Mon, 15 Aug 2022 20:15:58 +0200 Subject: [PATCH 1/2] added new constructor for genesis supply that matches upstream --- simapp/test_helpers.go | 4 ++-- x/bank/keeper/genesis.go | 2 +- x/bank/keeper/genesis_test.go | 6 +++--- x/bank/types/genesis.go | 14 +++++++++++++- 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/simapp/test_helpers.go b/simapp/test_helpers.go index 6cf6fdc8d37b..860accb45f7d 100644 --- a/simapp/test_helpers.go +++ b/simapp/test_helpers.go @@ -141,7 +141,7 @@ func SetupWithGenesisValSet(t *testing.T, valSet *tmtypes.ValidatorSet, genAccs }) // update total supply - bankGenesis := banktypes.NewGenesisState(banktypes.DefaultGenesisState().Params, balances, totalSupply, []banktypes.Metadata{}, []banktypes.GenesisSupplyOffset{}) + bankGenesis := banktypes.NewGenesisState(banktypes.DefaultGenesisState().Params, balances, totalSupply, []banktypes.Metadata{}) genesisState[banktypes.ModuleName] = app.AppCodec().MustMarshalJSON(bankGenesis) stateBytes, err := json.MarshalIndent(genesisState, "", " ") @@ -180,7 +180,7 @@ func SetupWithGenesisAccounts(genAccs []authtypes.GenesisAccount, balances ...ba totalSupply = totalSupply.Add(b.Coins...) } - bankGenesis := banktypes.NewGenesisState(banktypes.DefaultGenesisState().Params, balances, totalSupply, []banktypes.Metadata{}, []banktypes.GenesisSupplyOffset{}) + bankGenesis := banktypes.NewGenesisState(banktypes.DefaultGenesisState().Params, balances, totalSupply, []banktypes.Metadata{}) genesisState[banktypes.ModuleName] = app.AppCodec().MustMarshalJSON(bankGenesis) stateBytes, err := json.MarshalIndent(genesisState, "", " ") diff --git a/x/bank/keeper/genesis.go b/x/bank/keeper/genesis.go index d4293a241d9d..69e4afced1e7 100644 --- a/x/bank/keeper/genesis.go +++ b/x/bank/keeper/genesis.go @@ -52,7 +52,7 @@ func (k BaseKeeper) ExportGenesis(ctx sdk.Context) *types.GenesisState { panic(fmt.Errorf("unable to fetch total supply %v", err)) } - return types.NewGenesisState( + return types.NewGenesisStateWithSupplyOffsets( k.GetParams(ctx), k.GetAccountsBalances(ctx), totalSupply, diff --git a/x/bank/keeper/genesis_test.go b/x/bank/keeper/genesis_test.go index 58495bd6bc28..dc16d5d813d6 100644 --- a/x/bank/keeper/genesis_test.go +++ b/x/bank/keeper/genesis_test.go @@ -83,17 +83,17 @@ func (suite *IntegrationTestSuite) TestTotalSupply() { }{ { "calculation NOT matching genesis Supply field", - types.NewGenesisState(defaultGenesis.Params, balances, sdk.NewCoins(sdk.NewCoin("wrongcoin", sdk.NewInt(1))), defaultGenesis.DenomMetadata, defaultGenesis.SupplyOffsets), + types.NewGenesisStateWithSupplyOffsets(defaultGenesis.Params, balances, sdk.NewCoins(sdk.NewCoin("wrongcoin", sdk.NewInt(1))), defaultGenesis.DenomMetadata, defaultGenesis.SupplyOffsets), nil, true, "genesis supply is incorrect, expected 1wrongcoin, got 21barcoin,11foocoin", }, { "calculation matches genesis Supply field", - types.NewGenesisState(defaultGenesis.Params, balances, totalSupply, defaultGenesis.DenomMetadata, defaultGenesis.SupplyOffsets), + types.NewGenesisStateWithSupplyOffsets(defaultGenesis.Params, balances, totalSupply, defaultGenesis.DenomMetadata, defaultGenesis.SupplyOffsets), totalSupply, false, "", }, { "calculation is correct, empty genesis Supply field", - types.NewGenesisState(defaultGenesis.Params, balances, nil, defaultGenesis.DenomMetadata, defaultGenesis.SupplyOffsets), + types.NewGenesisStateWithSupplyOffsets(defaultGenesis.Params, balances, nil, defaultGenesis.DenomMetadata, defaultGenesis.SupplyOffsets), totalSupply, false, "", }, } diff --git a/x/bank/types/genesis.go b/x/bank/types/genesis.go index deeb4ebe689c..acc38dc3f18a 100644 --- a/x/bank/types/genesis.go +++ b/x/bank/types/genesis.go @@ -63,6 +63,18 @@ func (gs GenesisState) Validate() error { // NewGenesisState creates a new genesis state. func NewGenesisState(params Params, balances []Balance, supply sdk.Coins, + denomMetaData []Metadata) *GenesisState { + return &GenesisState{ + Params: params, + Balances: balances, + Supply: supply, + DenomMetadata: denomMetaData, + SupplyOffsets: []GenesisSupplyOffset{}, + } +} + +// NewGenesisState creates a new genesis state with supply offsets +func NewGenesisStateWithSupplyOffsets(params Params, balances []Balance, supply sdk.Coins, denomMetaData []Metadata, supplyOffsets []GenesisSupplyOffset) *GenesisState { return &GenesisState{ Params: params, @@ -75,7 +87,7 @@ func NewGenesisState(params Params, balances []Balance, supply sdk.Coins, // DefaultGenesisState returns a default bank module genesis state. func DefaultGenesisState() *GenesisState { - return NewGenesisState(DefaultParams(), []Balance{}, sdk.Coins{}, []Metadata{}, []GenesisSupplyOffset{}) + return NewGenesisState(DefaultParams(), []Balance{}, sdk.Coins{}, []Metadata{}) } // GetGenesisStateFromAppState returns x/bank GenesisState given raw application From 93c7962ac23b0fed2bfc4f9b14340be0f4dbede4 Mon Sep 17 00:00:00 2001 From: Nicolas Lara Date: Tue, 16 Aug 2022 09:21:11 +0200 Subject: [PATCH 2/2] moved to decorator pattern --- x/bank/keeper/genesis.go | 5 ++--- x/bank/keeper/genesis_test.go | 6 +++--- x/bank/types/genesis.go | 14 ++++---------- 3 files changed, 9 insertions(+), 16 deletions(-) diff --git a/x/bank/keeper/genesis.go b/x/bank/keeper/genesis.go index 69e4afced1e7..b254a8678591 100644 --- a/x/bank/keeper/genesis.go +++ b/x/bank/keeper/genesis.go @@ -52,11 +52,10 @@ func (k BaseKeeper) ExportGenesis(ctx sdk.Context) *types.GenesisState { panic(fmt.Errorf("unable to fetch total supply %v", err)) } - return types.NewGenesisStateWithSupplyOffsets( + return types.NewGenesisState( k.GetParams(ctx), k.GetAccountsBalances(ctx), totalSupply, k.GetAllDenomMetaData(ctx), - k.getGenesisSupplyOffsets(ctx), - ) + ).WithSupplyOffsets(k.getGenesisSupplyOffsets(ctx)) } diff --git a/x/bank/keeper/genesis_test.go b/x/bank/keeper/genesis_test.go index dc16d5d813d6..e6dde363f750 100644 --- a/x/bank/keeper/genesis_test.go +++ b/x/bank/keeper/genesis_test.go @@ -83,17 +83,17 @@ func (suite *IntegrationTestSuite) TestTotalSupply() { }{ { "calculation NOT matching genesis Supply field", - types.NewGenesisStateWithSupplyOffsets(defaultGenesis.Params, balances, sdk.NewCoins(sdk.NewCoin("wrongcoin", sdk.NewInt(1))), defaultGenesis.DenomMetadata, defaultGenesis.SupplyOffsets), + types.NewGenesisState(defaultGenesis.Params, balances, sdk.NewCoins(sdk.NewCoin("wrongcoin", sdk.NewInt(1))), defaultGenesis.DenomMetadata).WithSupplyOffsets(defaultGenesis.SupplyOffsets), nil, true, "genesis supply is incorrect, expected 1wrongcoin, got 21barcoin,11foocoin", }, { "calculation matches genesis Supply field", - types.NewGenesisStateWithSupplyOffsets(defaultGenesis.Params, balances, totalSupply, defaultGenesis.DenomMetadata, defaultGenesis.SupplyOffsets), + types.NewGenesisState(defaultGenesis.Params, balances, totalSupply, defaultGenesis.DenomMetadata).WithSupplyOffsets(defaultGenesis.SupplyOffsets), totalSupply, false, "", }, { "calculation is correct, empty genesis Supply field", - types.NewGenesisStateWithSupplyOffsets(defaultGenesis.Params, balances, nil, defaultGenesis.DenomMetadata, defaultGenesis.SupplyOffsets), + types.NewGenesisState(defaultGenesis.Params, balances, nil, defaultGenesis.DenomMetadata).WithSupplyOffsets(defaultGenesis.SupplyOffsets), totalSupply, false, "", }, } diff --git a/x/bank/types/genesis.go b/x/bank/types/genesis.go index acc38dc3f18a..d114954329da 100644 --- a/x/bank/types/genesis.go +++ b/x/bank/types/genesis.go @@ -73,16 +73,10 @@ func NewGenesisState(params Params, balances []Balance, supply sdk.Coins, } } -// NewGenesisState creates a new genesis state with supply offsets -func NewGenesisStateWithSupplyOffsets(params Params, balances []Balance, supply sdk.Coins, - denomMetaData []Metadata, supplyOffsets []GenesisSupplyOffset) *GenesisState { - return &GenesisState{ - Params: params, - Balances: balances, - Supply: supply, - DenomMetadata: denomMetaData, - SupplyOffsets: supplyOffsets, - } +// WithSupplyOffsets adds supply offsets to a genesis state +func (gs *GenesisState) WithSupplyOffsets(supplyOffsets []GenesisSupplyOffset) *GenesisState { + gs.SupplyOffsets = supplyOffsets + return gs } // DefaultGenesisState returns a default bank module genesis state.