Skip to content

Commit

Permalink
fix(ante)!: ensure ante handlers can deal with no signers in msg (axe…
Browse files Browse the repository at this point in the history
  • Loading branch information
cgorenflo authored Apr 4, 2024
1 parent ea078c8 commit 8aba6e5
Show file tree
Hide file tree
Showing 10 changed files with 403 additions and 267 deletions.
293 changes: 146 additions & 147 deletions app/app.go

Large diffs are not rendered by default.

67 changes: 67 additions & 0 deletions app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,24 @@ import (
"github.com/CosmWasm/wasmd/x/wasm"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/simapp/helpers"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/version"
"github.com/stretchr/testify/assert"
abcitypes "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/log"
abci "github.com/tendermint/tendermint/proto/tendermint/types"
dbm "github.com/tendermint/tm-db"
"google.golang.org/grpc/encoding"
encproto "google.golang.org/grpc/encoding/proto"

"github.com/axelarnetwork/axelar-core/app"
"github.com/axelarnetwork/axelar-core/app/params"
"github.com/axelarnetwork/axelar-core/testutils/fake"
multisig "github.com/axelarnetwork/axelar-core/x/multisig/types"
"github.com/axelarnetwork/axelar-core/x/nexus/exported"
"github.com/axelarnetwork/utils/funcs"
)

func TestNewAxelarApp(t *testing.T) {
Expand Down Expand Up @@ -105,3 +113,62 @@ func TestGRPCEncodingSetDuringInit(t *testing.T) {
assert.NoError(t, err)
assert.NoError(t, codec.Unmarshal(bz, &keyResponse))
}

func TestAnteHandlersCanHandleWasmMsgsWithoutSigners(t *testing.T) {
app.SetConfig()
app.WasmEnabled = "true"
app.IBCWasmHooksEnabled = "true"
version.Version = "0.35.0"
encConfig := app.MakeEncodingConfig()

tx := prepareTx(encConfig, &exported.WasmMessage{})
anteHandler := prepareAnteHandler(encConfig)
ctx := prepareCtx()

_, err := anteHandler(ctx, tx, true)
assert.NoError(t, err)
_, err = anteHandler(ctx, tx, false)
assert.NoError(t, err)
}

func prepareTx(encConfig params.EncodingConfig, msg sdk.Msg) sdk.Tx {
sk, _, _ := testdata.KeyTestPubAddr()

tx := funcs.Must(helpers.GenTx(
encConfig.TxConfig,
[]sdk.Msg{msg},
sdk.NewCoins(sdk.NewInt64Coin("testcoin", 0)),
1000000000,
"testchain",
[]uint64{0},
[]uint64{0},
sk,
))
return tx
}

func prepareAnteHandler(cfg params.EncodingConfig) sdk.AnteHandler {
axelarApp := app.NewAxelarApp(
log.TestingLogger(),
dbm.NewMemDB(),
nil,
true,
nil,
"",
"",
0,
cfg,
simapp.EmptyAppOptions{},
[]wasm.Option{},
)

anteHandler := app.InitCustomAnteDecorators(cfg, axelarApp.Keys, axelarApp.Keepers, simapp.EmptyAppOptions{})
return sdk.ChainAnteDecorators(anteHandler...)
}

func prepareCtx() sdk.Context {
return sdk.NewContext(fake.NewMultiStore(), abci.Header{}, false, log.TestingLogger()).
WithConsensusParams(&abcitypes.ConsensusParams{
Block: &abcitypes.BlockParams{MaxGas: 1000000000},
})
}
60 changes: 35 additions & 25 deletions app/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ import (

servertypes "github.com/cosmos/cosmos-sdk/server/types"
sdk "github.com/cosmos/cosmos-sdk/types"
crisiskeeper "github.com/cosmos/cosmos-sdk/x/crisis/keeper"
distrkeeper "github.com/cosmos/cosmos-sdk/x/distribution/keeper"
slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
"github.com/cosmos/cosmos-sdk/x/staking"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
)
Expand All @@ -33,7 +37,8 @@ func (app *AxelarApp) ExportAppStateAndValidators(forZeroHeight bool, jailAllowe
return servertypes.ExportedApp{}, err
}

validators, err := staking.WriteValidators(ctx, app.stakingKeeper)
stakingKeeper := GetKeeper[stakingkeeper.Keeper](app.Keepers)
validators, err := staking.WriteValidators(ctx, *stakingKeeper)
if err != nil {
return servertypes.ExportedApp{}, err
}
Expand Down Expand Up @@ -66,49 +71,54 @@ func (app *AxelarApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs
allowedAddrsMap[addr] = true
}

crisisKeeper := GetKeeper[crisiskeeper.Keeper](app.Keepers)
stakingKeeper := GetKeeper[stakingkeeper.Keeper](app.Keepers)
distrKeeper := GetKeeper[distrkeeper.Keeper](app.Keepers)
slashingKeeper := GetKeeper[slashingkeeper.Keeper](app.Keepers)

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

/* Handle fee distribution state. */

// withdraw all validator commission
app.stakingKeeper.IterateValidators(ctx, func(_ int64, val stakingtypes.ValidatorI) (stop bool) {
_, _ = app.distrKeeper.WithdrawValidatorCommission(ctx, val.GetOperator())
stakingKeeper.IterateValidators(ctx, func(_ int64, val stakingtypes.ValidatorI) (stop bool) {
_, _ = distrKeeper.WithdrawValidatorCommission(ctx, val.GetOperator())
return false
})

// withdraw all delegator rewards
dels := app.stakingKeeper.GetAllDelegations(ctx)
dels := stakingKeeper.GetAllDelegations(ctx)
for _, delegation := range dels {
_, _ = app.distrKeeper.WithdrawDelegationRewards(ctx, delegation.GetDelegatorAddr(), delegation.GetValidatorAddr())
_, _ = distrKeeper.WithdrawDelegationRewards(ctx, delegation.GetDelegatorAddr(), delegation.GetValidatorAddr())
}

// clear validator slash events
app.distrKeeper.DeleteAllValidatorSlashEvents(ctx)
distrKeeper.DeleteAllValidatorSlashEvents(ctx)

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

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

// reinitialize all validators
app.stakingKeeper.IterateValidators(ctx, func(_ int64, val stakingtypes.ValidatorI) (stop bool) {
stakingKeeper.IterateValidators(ctx, func(_ int64, val stakingtypes.ValidatorI) (stop bool) {
// donate any unwithdrawn outstanding reward fraction tokens to the community pool
scraps := app.distrKeeper.GetValidatorOutstandingRewardsCoins(ctx, val.GetOperator())
feePool := app.distrKeeper.GetFeePool(ctx)
scraps := distrKeeper.GetValidatorOutstandingRewardsCoins(ctx, val.GetOperator())
feePool := distrKeeper.GetFeePool(ctx)
feePool.CommunityPool = feePool.CommunityPool.Add(scraps...)
app.distrKeeper.SetFeePool(ctx, feePool)
distrKeeper.SetFeePool(ctx, feePool)

app.distrKeeper.Hooks().AfterValidatorCreated(ctx, val.GetOperator())
distrKeeper.Hooks().AfterValidatorCreated(ctx, val.GetOperator())
return false
})

// reinitialize all delegations
for _, del := range dels {
app.distrKeeper.Hooks().BeforeDelegationCreated(ctx, del.GetDelegatorAddr(), del.GetValidatorAddr())
app.distrKeeper.Hooks().AfterDelegationModified(ctx, del.GetDelegatorAddr(), del.GetValidatorAddr())
distrKeeper.Hooks().BeforeDelegationCreated(ctx, del.GetDelegatorAddr(), del.GetValidatorAddr())
distrKeeper.Hooks().AfterDelegationModified(ctx, del.GetDelegatorAddr(), del.GetValidatorAddr())
}

// reset context height
Expand All @@ -117,32 +127,32 @@ func (app *AxelarApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs
/* Handle staking state. */

// iterate through redelegations, reset creation height
app.stakingKeeper.IterateRedelegations(ctx, func(_ int64, red stakingtypes.Redelegation) (stop bool) {
stakingKeeper.IterateRedelegations(ctx, func(_ int64, red stakingtypes.Redelegation) (stop bool) {
for i := range red.Entries {
red.Entries[i].CreationHeight = 0
}
app.stakingKeeper.SetRedelegation(ctx, red)
stakingKeeper.SetRedelegation(ctx, red)
return false
})

// iterate through unbonding delegations, reset creation height
app.stakingKeeper.IterateUnbondingDelegations(ctx, func(_ int64, ubd stakingtypes.UnbondingDelegation) (stop bool) {
stakingKeeper.IterateUnbondingDelegations(ctx, func(_ int64, ubd stakingtypes.UnbondingDelegation) (stop bool) {
for i := range ubd.Entries {
ubd.Entries[i].CreationHeight = 0
}
app.stakingKeeper.SetUnbondingDelegation(ctx, ubd)
stakingKeeper.SetUnbondingDelegation(ctx, ubd)
return false
})

// Iterate through validators by power descending, reset bond heights, and
// update bond intra-tx counters.
store := ctx.KVStore(app.keys[stakingtypes.StoreKey])
store := ctx.KVStore(app.Keys[stakingtypes.StoreKey])
iter := sdk.KVStoreReversePrefixIterator(store, stakingtypes.ValidatorsKey)
counter := int16(0)

for ; iter.Valid(); iter.Next() {
addr := sdk.ValAddress(iter.Key()[1:])
validator, found := app.stakingKeeper.GetValidator(ctx, addr)
validator, found := stakingKeeper.GetValidator(ctx, addr)
if !found {
panic("expected validator, not found")
}
Expand All @@ -152,24 +162,24 @@ func (app *AxelarApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs
validator.Jailed = true
}

app.stakingKeeper.SetValidator(ctx, validator)
stakingKeeper.SetValidator(ctx, validator)
counter++
}

_ = iter.Close()

if _, err := app.stakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx); err != nil {
if _, err := stakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx); err != nil {
panic(err)
}

/* Handle slashing state. */

// reset start height on signing infos
app.slashingKeeper.IterateValidatorSigningInfos(
slashingKeeper.IterateValidatorSigningInfos(
ctx,
func(addr sdk.ConsAddress, info slashingtypes.ValidatorSigningInfo) (stop bool) {
info.StartHeight = 0
app.slashingKeeper.SetValidatorSigningInfo(ctx, addr, info)
slashingKeeper.SetValidatorSigningInfo(ctx, addr, info)
return false
},
)
Expand Down
Loading

0 comments on commit 8aba6e5

Please sign in to comment.