Skip to content

Commit

Permalink
fix lints
Browse files Browse the repository at this point in the history
  • Loading branch information
faddat committed Dec 5, 2024
1 parent 0baedc5 commit c4321b5
Show file tree
Hide file tree
Showing 6 changed files with 245 additions and 632 deletions.
108 changes: 56 additions & 52 deletions app/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,8 @@ func (app *WasmApp) ExportAppStateAndValidators(forZeroHeight bool, jailAllowedA
}, err
}

// prepare for fresh start at zero height
// NOTE zero height genesis is a temporary feature which will be deprecated
//
// in favor of export at a block height
func (app *WasmApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs []string) {
applyAllowedAddrs := false

// check if there is a allowed address list
if len(jailAllowedAddrs) > 0 {
applyAllowedAddrs = true
}

func (app *WasmApp) handleAllowedAddrs(jailAllowedAddrs []string) (map[string]bool, bool) {
applyAllowedAddrs := len(jailAllowedAddrs) > 0
allowedAddrsMap := make(map[string]bool)

for _, addr := range jailAllowedAddrs {
Expand All @@ -71,11 +61,10 @@ func (app *WasmApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs [
allowedAddrsMap[addr] = true
}

/* Just to be safe, assert the invariants on current state. */
app.CrisisKeeper.AssertInvariants(ctx)

/* Handle fee distribution state. */
return allowedAddrsMap, applyAllowedAddrs
}

func (app *WasmApp) handleFeeDistribution(ctx sdk.Context) []stakingtypes.Delegation {
// withdraw all validator commission
err := app.StakingKeeper.IterateValidators(ctx, func(_ int64, val stakingtypes.ValidatorI) (stop bool) {
valBz, err := app.StakingKeeper.ValidatorAddressCodec().StringToBytes(val.GetOperator())
Expand Down Expand Up @@ -108,22 +97,20 @@ func (app *WasmApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs [
}
}

// clear validator slash events
// clear validator slash events and historical rewards
app.DistrKeeper.DeleteAllValidatorSlashEvents(ctx)

// clear validator historical rewards
app.DistrKeeper.DeleteAllValidatorHistoricalRewards(ctx)

// set context height to zero
height := ctx.BlockHeight()
ctx = ctx.WithBlockHeight(0)
return dels
}

// reinitialize all validators
err = app.StakingKeeper.IterateValidators(ctx, func(_ int64, val stakingtypes.ValidatorI) (stop bool) {
func (app *WasmApp) reinitializeValidators(ctx sdk.Context) error {
return app.StakingKeeper.IterateValidators(ctx, func(_ int64, val stakingtypes.ValidatorI) (stop bool) {
valBz, err := app.StakingKeeper.ValidatorAddressCodec().StringToBytes(val.GetOperator())
if err != nil {
panic(err)
}

// donate any unwithdrawn outstanding reward fraction tokens to the community pool
scraps, err := app.DistrKeeper.GetValidatorOutstandingRewardsCoins(ctx, valBz)
if err != nil {
Expand All @@ -143,11 +130,9 @@ func (app *WasmApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs [
}
return false
})
if err != nil {
panic(err)
}
}

// reinitialize all delegations
func (app *WasmApp) reinitializeDelegations(ctx sdk.Context, dels []stakingtypes.Delegation) {
for _, del := range dels {
valAddr, err := sdk.ValAddressFromBech32(del.ValidatorAddress)
if err != nil {
Expand All @@ -156,27 +141,22 @@ func (app *WasmApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs [
delAddr := sdk.MustAccAddressFromBech32(del.DelegatorAddress)

if err := app.DistrKeeper.Hooks().BeforeDelegationCreated(ctx, delAddr, valAddr); err != nil {
// never called as BeforeDelegationCreated always returns nil
panic(fmt.Errorf("error while incrementing period: %w", err))
}

if err := app.DistrKeeper.Hooks().AfterDelegationModified(ctx, delAddr, valAddr); err != nil {
// never called as AfterDelegationModified always returns nil
panic(fmt.Errorf("error while creating a new delegation period record: %w", err))
}
}
}

// reset context height
ctx = ctx.WithBlockHeight(height)

/* Handle staking state. */

// iterate through redelegations, reset creation height
err = app.StakingKeeper.IterateRedelegations(ctx, func(_ int64, red stakingtypes.Redelegation) (stop bool) {
func (app *WasmApp) handleStakingState(ctx sdk.Context, applyAllowedAddrs bool, allowedAddrsMap map[string]bool) {
// Reset redelegations
err := app.StakingKeeper.IterateRedelegations(ctx, func(_ int64, red stakingtypes.Redelegation) (stop bool) {
for i := range red.Entries {
red.Entries[i].CreationHeight = 0
}
err = app.StakingKeeper.SetRedelegation(ctx, red)
err := app.StakingKeeper.SetRedelegation(ctx, red)
if err != nil {
panic(err)
}
Expand All @@ -186,12 +166,12 @@ func (app *WasmApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs [
panic(err)
}

// iterate through unbonding delegations, reset creation height
// Reset unbonding delegations
err = app.StakingKeeper.IterateUnbondingDelegations(ctx, func(_ int64, ubd stakingtypes.UnbondingDelegation) (stop bool) {
for i := range ubd.Entries {
ubd.Entries[i].CreationHeight = 0
}
err = app.StakingKeeper.SetUnbondingDelegation(ctx, ubd)
err := app.StakingKeeper.SetUnbondingDelegation(ctx, ubd)
if err != nil {
panic(err)
}
Expand All @@ -201,10 +181,14 @@ func (app *WasmApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs [
panic(err)
}

// Iterate through validators by power descending, reset bond heights, and
// update bond intra-tx counters.
// Update validators
store := ctx.KVStore(app.GetKey(stakingtypes.StoreKey))
iter := storetypes.KVStoreReversePrefixIterator(store, stakingtypes.ValidatorsKey)
defer func() {
if err := iter.Close(); err != nil {
app.Logger().Error("error while closing the key-value store reverse prefix iterator: ", err)
}
}()

for ; iter.Valid(); iter.Next() {
addr := sdk.ValAddress(stakingtypes.AddressFromValidatorsKey(iter.Key()))
Expand All @@ -224,20 +208,15 @@ func (app *WasmApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs [
}
}

if err := iter.Close(); err != nil {
app.Logger().Error("error while closing the key-value store reverse prefix iterator: ", err)
return
}

_, err = app.StakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx)
if err != nil {
log.Fatal(err)
app.Logger().Error("error while applying validator set updates", "error", err)
return
}
}

/* Handle slashing state. */

// reset start height on signing infos
err = app.SlashingKeeper.IterateValidatorSigningInfos(
func (app *WasmApp) handleSlashingState(ctx sdk.Context) {
err := app.SlashingKeeper.IterateValidatorSigningInfos(
ctx,
func(addr sdk.ConsAddress, info slashingtypes.ValidatorSigningInfo) (stop bool) {
info.StartHeight = 0
Expand All @@ -251,3 +230,28 @@ func (app *WasmApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs [
panic(err)
}
}

func (app *WasmApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs []string) {
allowedAddrsMap, applyAllowedAddrs := app.handleAllowedAddrs(jailAllowedAddrs)

// Assert the invariants on current state
app.CrisisKeeper.AssertInvariants(ctx)

// Handle fee distribution state
height := ctx.BlockHeight()
ctx = ctx.WithBlockHeight(0)
dels := app.handleFeeDistribution(ctx)

// Reinitialize validators and delegations
if err := app.reinitializeValidators(ctx); err != nil {
panic(err)
}
app.reinitializeDelegations(ctx, dels)

// Reset context height
ctx = ctx.WithBlockHeight(height)

// Handle staking and slashing states
app.handleStakingState(ctx, applyAllowedAddrs, allowedAddrsMap)
app.handleSlashingState(ctx)
}
4 changes: 2 additions & 2 deletions cmd/wasmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ func initAppConfig() (string, interface{}) {
func initRootCmd(
rootCmd *cobra.Command,
txConfig client.TxConfig,
interfaceRegistry codectypes.InterfaceRegistry,
appCodec codec.Codec,
_ codectypes.InterfaceRegistry,
_ codec.Codec,
basicManager module.BasicManager,
) {
cfg := sdk.GetConfig()
Expand Down
10 changes: 6 additions & 4 deletions tests/e2e/ibc_callbacks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func TestIBCCallbacks(t *testing.T) {

// then the contract on chain B should receive a receive callback
var response QueryResp
chainB.SmartQuery(contractAddrB.String(), QueryMsg{CallbackStats: struct{}{}}, &response)
require.NoError(t, chainB.SmartQuery(contractAddrB.String(), QueryMsg{CallbackStats: struct{}{}}, &response))
assert.Empty(t, response.IBCAckCallbacks)
assert.Empty(t, response.IBCTimeoutCallbacks)
assert.Len(t, response.IBCDestinationCallbacks, 1)
Expand All @@ -141,7 +141,8 @@ func TestIBCCallbacks(t *testing.T) {
assert.Equal(t, []byte("{\"result\":\"AQ==\"}"), response.IBCDestinationCallbacks[0].Ack.Data)

// and the contract on chain A should receive a callback with the ack
chainA.SmartQuery(contractAddrA.String(), QueryMsg{CallbackStats: struct{}{}}, &response)
err := chainA.SmartQuery(contractAddrA.String(), QueryMsg{CallbackStats: struct{}{}}, &response)
require.NoError(t, err)
assert.Len(t, response.IBCAckCallbacks, 1)
assert.Empty(t, response.IBCTimeoutCallbacks)
assert.Empty(t, response.IBCDestinationCallbacks)
Expand All @@ -154,13 +155,14 @@ func TestIBCCallbacks(t *testing.T) {

// then the contract on chain B should not receive anything
var response QueryResp
chainB.SmartQuery(contractAddrB.String(), QueryMsg{CallbackStats: struct{}{}}, &response)
require.NoError(t, chainB.SmartQuery(contractAddrB.String(), QueryMsg{CallbackStats: struct{}{}}, &response))
assert.Empty(t, response.IBCAckCallbacks)
assert.Empty(t, response.IBCTimeoutCallbacks)
assert.Empty(t, response.IBCDestinationCallbacks)

// and the contract on chain A should receive a callback with the timeout result
chainA.SmartQuery(contractAddrA.String(), QueryMsg{CallbackStats: struct{}{}}, &response)
err := chainA.SmartQuery(contractAddrA.String(), QueryMsg{CallbackStats: struct{}{}}, &response)
require.NoError(t, err)
assert.Empty(t, response.IBCAckCallbacks)
assert.Len(t, response.IBCTimeoutCallbacks, 1)
assert.Empty(t, response.IBCDestinationCallbacks)
Expand Down
3 changes: 1 addition & 2 deletions x/wasm/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
abci "github.com/cometbft/cometbft/abci/types"
"github.com/cometbft/cometbft/libs/rand"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
dbm "github.com/cosmos/cosmos-db"
fuzz "github.com/google/gofuzz"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -332,7 +331,7 @@ func TestCreateDuplicate(t *testing.T) {
func TestCreateWithSimulation(t *testing.T) {
ctx, keepers := CreateTestInput(t, false, AvailableCapabilities)

ctx = ctx.WithBlockHeader(tmproto.Header{Height: 1}).
ctx = ctx.WithBlockHeader(cmtproto.Header{Height: 1}).
WithGasMeter(storetypes.NewInfiniteGasMeter())

deposit := sdk.NewCoins(sdk.NewInt64Coin("denom", 100000))
Expand Down
Loading

0 comments on commit c4321b5

Please sign in to comment.