Skip to content

Commit

Permalink
feat(x/precisebank): Implement ExportGenesis (#1915)
Browse files Browse the repository at this point in the history
  • Loading branch information
drklee3 committed May 20, 2024
1 parent 7990021 commit dbc3ad7
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 43 deletions.
22 changes: 19 additions & 3 deletions x/precisebank/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,27 @@ func InitGenesis(
))
}

// TODO: After keeper methods are implemented
// - Set account FractionalBalances
// Set FractionalBalances in state
for _, bal := range gs.Balances {
addr := sdk.MustAccAddressFromBech32(bal.Address)

keeper.SetFractionalBalance(ctx, addr, bal.Amount)
}

// Set remainder amount in state
keeper.SetRemainderAmount(ctx, gs.Remainder)
}

// ExportGenesis returns a GenesisState for a given context and keeper.
func ExportGenesis(ctx sdk.Context, keeper keeper.Keeper) *types.GenesisState {
return types.NewGenesisState(types.FractionalBalances{}, sdkmath.ZeroInt())
balances := types.FractionalBalances{}
keeper.IterateFractionalBalances(ctx, func(addr sdk.AccAddress, amount sdkmath.Int) bool {
balances = append(balances, types.NewFractionalBalance(addr.String(), amount))

return false
})

remainder := keeper.GetRemainderAmount(ctx)

return types.NewGenesisState(balances, remainder)
}
106 changes: 66 additions & 40 deletions x/precisebank/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/kava-labs/kava/x/precisebank"
"github.com/kava-labs/kava/x/precisebank/keeper"
"github.com/kava-labs/kava/x/precisebank/testutil"
"github.com/kava-labs/kava/x/precisebank/types"
"github.com/stretchr/testify/suite"
Expand Down Expand Up @@ -163,80 +164,105 @@ func (suite *GenesisTestSuite) TestInitGenesis() {
"module account should be created & stored in account store",
)

// TODO: Check module state once implemented
// Verify balances are set in state, get full list of balances in
// state to ensure they are set AND no extra balances are set
var bals []types.FractionalBalance
suite.Keeper.IterateFractionalBalances(suite.Ctx, func(addr sdk.AccAddress, bal sdkmath.Int) bool {
bals = append(bals, types.NewFractionalBalance(addr.String(), bal))

// Verify balances
// IterateBalances() or something
return false
})

suite.Require().ElementsMatch(tc.genesisState.Balances, bals, "balances should be set in state")

// Ensure reserve balance matches sum of all fractional balances
// sum up IterateBalances()
remainder := suite.Keeper.GetRemainderAmount(suite.Ctx)
suite.Require().Equal(tc.genesisState.Remainder, remainder, "remainder should be set in state")

// - etc
// Additional verification of state via invariants
invariantFn := keeper.AllInvariants(suite.Keeper)
msg, broken := invariantFn(suite.Ctx)
suite.Require().False(broken, "invariants should not be broken after InitGenesis")
suite.Require().Empty(msg, "invariants should not return a message after InitGenesis")
})
}
}

func (suite *GenesisTestSuite) TestExportGenesis_Valid() {
// ExportGenesis(moduleState) should return a valid genesis state
func (suite *GenesisTestSuite) TestExportGenesis() {
// ExportGenesis(InitGenesis(genesisState)) == genesisState
// Must also be valid.

tests := []struct {
name string
maleate func()
name string
initGenesisState func() *types.GenesisState
}{
{
"InitGenesis(DefaultGenesisState)",
func() {
precisebank.InitGenesis(
func() *types.GenesisState {
return types.DefaultGenesisState()
},
},
{
"balances, no remainder",
func() *types.GenesisState {
err := suite.BankKeeper.MintCoins(
suite.Ctx,
suite.Keeper,
suite.AccountKeeper,
suite.BankKeeper,
types.DefaultGenesisState(),
types.ModuleName,
sdk.NewCoins(sdk.NewCoin(types.IntegerCoinDenom, sdkmath.NewInt(1))),
)
suite.Require().NoError(err)

return types.NewGenesisState(
types.FractionalBalances{
types.NewFractionalBalance(sdk.AccAddress{1}.String(), types.ConversionFactor().QuoRaw(2)),
types.NewFractionalBalance(sdk.AccAddress{2}.String(), types.ConversionFactor().QuoRaw(2)),
},
sdkmath.ZeroInt(),
)
},
},
}

for _, tc := range tests {
suite.Run(tc.name, func() {
tc.maleate()

genesisState := precisebank.ExportGenesis(suite.Ctx, suite.Keeper)

suite.Require().NoError(genesisState.Validate(), "exported genesis state should be valid")
})
}
}

func (suite *GenesisTestSuite) TestExportImportedState() {
// ExportGenesis(InitGenesis(genesisState)) == genesisState

tests := []struct {
name string
initGenesisState *types.GenesisState
}{
{
"InitGenesis(DefaultGenesisState)",
types.DefaultGenesisState(),
"balances, remainder",
func() *types.GenesisState {
err := suite.BankKeeper.MintCoins(
suite.Ctx,
types.ModuleName,
sdk.NewCoins(sdk.NewCoin(types.IntegerCoinDenom, sdkmath.NewInt(1))),
)
suite.Require().NoError(err)

return types.NewGenesisState(
types.FractionalBalances{
types.NewFractionalBalance(sdk.AccAddress{1}.String(), types.ConversionFactor().QuoRaw(2)),
types.NewFractionalBalance(sdk.AccAddress{2}.String(), types.ConversionFactor().QuoRaw(2).SubRaw(1)),
},
sdkmath.OneInt(),
)
},
},
}

for _, tc := range tests {
suite.Run(tc.name, func() {
// Reset state
suite.SetupTest()

initGs := tc.initGenesisState()

suite.Require().NotPanics(func() {
precisebank.InitGenesis(
suite.Ctx,
suite.Keeper,
suite.AccountKeeper,
suite.BankKeeper,
tc.initGenesisState,
initGs,
)
})

genesisState := precisebank.ExportGenesis(suite.Ctx, suite.Keeper)
suite.Require().NoError(genesisState.Validate(), "exported genesis state should be valid")

suite.Require().Equal(
tc.initGenesisState,
initGs,
genesisState,
"exported genesis state should equal initial genesis state",
)
Expand Down

0 comments on commit dbc3ad7

Please sign in to comment.