Skip to content

Commit

Permalink
Linting
Browse files Browse the repository at this point in the history
  • Loading branch information
alpe committed Nov 14, 2023
1 parent cdc2431 commit f3c17bd
Show file tree
Hide file tree
Showing 9 changed files with 332 additions and 41 deletions.
12 changes: 6 additions & 6 deletions demo/app/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"testing"
"time"

"github.com/CosmWasm/wasmd/x/wasm"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
dbm "github.com/cometbft/cometbft-db"
abci "github.com/cometbft/cometbft/abci/types"
tmjson "github.com/cometbft/cometbft/libs/json"
Expand Down Expand Up @@ -50,10 +50,10 @@ type SetupOptions struct {
Logger log.Logger
DB *dbm.MemDB
AppOpts servertypes.AppOptions
WasmOpts []wasm.Option
WasmOpts []wasmkeeper.Option
}

func setup(t testing.TB, chainID string, withGenesis bool, invCheckPeriod uint, opts ...wasm.Option) (*MeshApp, GenesisState) {
func setup(t testing.TB, chainID string, withGenesis bool, invCheckPeriod uint, opts ...wasmkeeper.Option) (*MeshApp, GenesisState) {
db := dbm.NewMemDB()
nodeHome := t.TempDir()
snapshotDir := filepath.Join(nodeHome, "data", "snapshots")
Expand Down Expand Up @@ -117,7 +117,7 @@ func NewMeshAppWithCustomOptions(t *testing.T, isCheckTx bool, options SetupOpti
}

// Setup initializes a new MeshApp. A Nop logger is set in MeshApp.
func Setup(t *testing.T, opts ...wasm.Option) *MeshApp {
func Setup(t *testing.T, opts ...wasmkeeper.Option) *MeshApp {
t.Helper()

privVal := mock.NewPV()
Expand Down Expand Up @@ -145,7 +145,7 @@ func Setup(t *testing.T, opts ...wasm.Option) *MeshApp {
// that also act as delegators. For simplicity, each validator is bonded with a delegation
// of one consensus engine unit in the default token of the MeshApp from first genesis
// account. A Nop logger is set in MeshApp.
func SetupWithGenesisValSet(t *testing.T, valSet *tmtypes.ValidatorSet, genAccs []authtypes.GenesisAccount, chainID string, opts []wasm.Option, balances ...banktypes.Balance) *MeshApp {
func SetupWithGenesisValSet(t *testing.T, valSet *tmtypes.ValidatorSet, genAccs []authtypes.GenesisAccount, chainID string, opts []wasmkeeper.Option, balances ...banktypes.Balance) *MeshApp {
t.Helper()

app, genesisState := setup(t, chainID, true, 5, opts...)
Expand Down Expand Up @@ -267,7 +267,7 @@ func ModuleAccountAddrs() map[string]bool {
return BlockedAddresses()
}

var emptyWasmOptions []wasm.Option
var emptyWasmOptions []wasmkeeper.Option

// NewTestNetworkFixture returns a new MeshApp AppConstructor for network simulation tests
func NewTestNetworkFixture() network.TestFixture {
Expand Down
4 changes: 2 additions & 2 deletions demo/app/test_support.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package app

import (
"github.com/CosmWasm/wasmd/x/wasm"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
ibckeeper "github.com/cosmos/ibc-go/v7/modules/core/keeper"

"github.com/cosmos/cosmos-sdk/baseapp"
Expand Down Expand Up @@ -35,6 +35,6 @@ func (app *MeshApp) GetAccountKeeper() authkeeper.AccountKeeper {
return app.AccountKeeper
}

func (app *MeshApp) GetWasmKeeper() wasm.Keeper {
func (app *MeshApp) GetWasmKeeper() wasmkeeper.Keeper {
return app.WasmKeeper
}
4 changes: 2 additions & 2 deletions demo/cmd/meshd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func newApp(
) servertypes.Application {
baseappOptions := server.DefaultBaseappOptions(appOpts)

var wasmOpts []wasm.Option
var wasmOpts []wasmkeeper.Option
if cast.ToBool(appOpts.Get("telemetry.enabled")) {
wasmOpts = append(wasmOpts, wasmkeeper.WithVMCacheMetrics(prometheus.DefaultRegisterer))
}
Expand Down Expand Up @@ -277,7 +277,7 @@ func appExport(
viperAppOpts.Set(server.FlagInvCheckPeriod, 1)
appOpts = viperAppOpts

var emptyWasmOpts []wasm.Option
var emptyWasmOpts []wasmkeeper.Option
wasmApp = app.NewMeshApp(
logger,
db,
Expand Down
72 changes: 54 additions & 18 deletions x/meshsecurity/keeper/adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ func TestCaptureTombstone(t *testing.T) {
require.NoError(t, err)
keepers.StakingKeeper.SetValidatorByConsAddr(pCtx, val)
keepers.StakingKeeper.SetValidator(pCtx, val)
mock, capturedTombstones := NewMockEvidenceSlashingKeeper()
cap := CaptureTombstoneDecorator(keepers.MeshKeeper, mock, keepers.StakingKeeper)
skMock, capturedTombstones := NewMockEvidenceSlashingKeeper()
decorator := CaptureTombstoneDecorator(keepers.MeshKeeper, skMock, keepers.StakingKeeper)
otherConsAddress := rand.Bytes(address.Len)
specs := map[string]struct {
addr sdk.ConsAddress
Expand All @@ -49,31 +49,67 @@ func TestCaptureTombstone(t *testing.T) {
ctx, _ := pCtx.CacheContext()
*capturedTombstones = make([]sdk.ConsAddress, 0, 1)
// when
cap.Tombstone(ctx, spec.addr)
decorator.Tombstone(ctx, spec.addr)

// then
assert.Equal(t, spec.expPassed, *capturedTombstones)
// and stored for async propagation
operations := fetchAllStoredOperations(t, keepers.MeshKeeper, ctx, val)
assert.Equal(t, spec.expStored, operations)
appStoredOps := fetchAllStoredOperations(t, ctx, keepers.MeshKeeper)
assert.Equal(t, spec.expStored, appStoredOps[val.OperatorAddress])
})
}
}

func fetchAllStoredOperations(t *testing.T, msKeeper *Keeper, ctx sdk.Context, val stakingtypes.Validator) []types.PipedValsetOperation {
index := make(map[string][]types.PipedValsetOperation, 1)

err := msKeeper.iteratePipedValsetOperations(ctx, func(valAddr sdk.ValAddress, op types.PipedValsetOperation) bool {
ops, ok := index[valAddr.String()]
if !ok {
ops = []types.PipedValsetOperation{}
}
index[valAddr.String()] = append(ops, op)
return false
})
func TestCaptureStakingEvents(t *testing.T) {
pCtx, keepers := CreateDefaultTestInput(t)

val := validatorFixture(t)
myConsAddress, err := val.GetConsAddr()
require.NoError(t, err)
operations := index[val.OperatorAddress]
return operations
keepers.StakingKeeper.SetValidatorByConsAddr(pCtx, val)
keepers.StakingKeeper.SetValidator(pCtx, val)

valJailed := validatorFixture(t)
valJailed.Jailed = true
myConsAddressJailed, err := valJailed.GetConsAddr()
require.NoError(t, err)
keepers.StakingKeeper.SetValidatorByConsAddr(pCtx, valJailed)
keepers.StakingKeeper.SetValidator(pCtx, valJailed)

decorator := NewStakingDecorator(keepers.StakingKeeper, keepers.MeshKeeper)
specs := map[string]struct {
consAddr sdk.ConsAddress
op func(sdk.Context, sdk.ConsAddress)
expStored []types.PipedValsetOperation
expJailed bool
}{
"jail": {
consAddr: myConsAddress,
op: decorator.Jail,
expStored: []types.PipedValsetOperation{types.ValidatorJailed},
expJailed: true,
},
"unjail": {
consAddr: myConsAddressJailed,
op: decorator.Unjail,
expStored: []types.PipedValsetOperation{types.ValidatorUnjailed},
},
}
for name, spec := range specs {
t.Run(name, func(t *testing.T) {
ctx, _ := pCtx.CacheContext()

// when
spec.op(ctx, spec.consAddr)

// then
loadedVal := keepers.StakingKeeper.ValidatorByConsAddr(ctx, spec.consAddr)
assert.Equal(t, spec.expJailed, loadedVal.IsJailed())
// and stored for async propagation
allStoredOps := fetchAllStoredOperations(t, ctx, keepers.MeshKeeper)
assert.Equal(t, spec.expStored, allStoredOps[loadedVal.GetOperator().String()])
})
}
}

type MockEvidenceSlashingKeeper struct {
Expand Down
22 changes: 22 additions & 0 deletions x/meshsecurity/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,25 @@ func CreateDefaultTestInput(t testing.TB) (sdk.Context, TestKeepers) {
Faucet: faucet,
}
}

func fetchAllStoredOperations(t *testing.T, ctx sdk.Context, msKeeper *Keeper) map[string][]types.PipedValsetOperation {
index := make(map[string][]types.PipedValsetOperation, 1)
err := msKeeper.iteratePipedValsetOperations(ctx, func(valAddr sdk.ValAddress, op types.PipedValsetOperation) bool {
ops, ok := index[valAddr.String()]
if !ok {
ops = []types.PipedValsetOperation{}
}
index[valAddr.String()] = append(ops, op)
return false
})
require.NoError(t, err)
return index
}

// for test code only
func must[t any](s t, err error) t {
if err != nil {
panic(err)
}
return s
}
9 changes: 0 additions & 9 deletions x/meshsecurity/keeper/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,6 @@ func (k Keeper) GetNextScheduledTaskHeight(ctx sdk.Context, tp types.SchedulerTa
return
}

func (k Keeper) getScheduledTaskAt(ctx sdk.Context, tp types.SchedulerTaskType, contract sdk.AccAddress, height uint64) (repeat, exists bool) {
key, err := types.BuildSchedulerContractKey(tp, height, contract)
if err != nil {
return false, false
}
bz := ctx.KVStore(k.storeKey).Get(key)
return isRepeat(bz), bz != nil
}

// ScheduleOneShotTask register a new task to be executed at given block height.
// The task is not repeating and registered only once for a given contract and height. Duplicates are silently ignored
func (k Keeper) ScheduleOneShotTask(ctx sdk.Context, tp types.SchedulerTaskType, contract sdk.AccAddress, execBlockHeight uint64) error {
Expand Down
9 changes: 9 additions & 0 deletions x/meshsecurity/keeper/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,12 @@ func TestDeleteAllScheduledTasks(t *testing.T) {
})
}
}

func (k Keeper) getScheduledTaskAt(ctx sdk.Context, tp types.SchedulerTaskType, contract sdk.AccAddress, height uint64) (repeat, exists bool) {
key, err := types.BuildSchedulerContractKey(tp, height, contract)
if err != nil {
return false, false
}
bz := ctx.KVStore(k.storeKey).Get(key)
return isRepeat(bz), bz != nil
}
8 changes: 4 additions & 4 deletions x/meshsecurity/keeper/valset_updates.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ func (k Keeper) sendAsync(ctx sdk.Context, op types.PipedValsetOperation, valAdd
}

// ValsetUpdateReport aggregate all stored changes of the current block. Should be called by an end-blocker.
// The events reported are categorized by type and not time. Conflicting events as Bonded/ Unbonded
// are not supposed to happen within the same block
func (k Keeper) ValsetUpdateReport(ctx sdk.Context) (contract.ValsetUpdate, error) {
var innerErr error
appendValidator := func(set *[]wasmvmtypes.Validator, valAddr sdk.ValAddress) bool {
Expand Down Expand Up @@ -97,7 +99,7 @@ func (k Keeper) ValsetUpdateReport(ctx sdk.Context) (contract.ValsetUpdate, erro
case types.ValidatorModified:
return appendValidator(&r.Updated, valAddr)
default:
innerErr = types.ErrUnknown.Wrapf("undefined operation type %X", op)
innerErr = types.ErrInvalid.Wrapf("undefined operation type %X", op)
return true
}
return false
Expand Down Expand Up @@ -126,8 +128,6 @@ func (k Keeper) ClearPipedValsetOperations(ctx sdk.Context) {
func (k Keeper) iteratePipedValsetOperations(ctx sdk.Context, cb func(valAddress sdk.ValAddress, op types.PipedValsetOperation) bool) error {
pStore := prefix.NewStore(ctx.KVStore(k.memKey), types.PipedValsetPrefix)
iter := pStore.Iterator(nil, nil)
defer iter.Close()

for ; iter.Valid(); iter.Next() {
key := iter.Key()
addrLen := key[0]
Expand All @@ -136,7 +136,7 @@ func (k Keeper) iteratePipedValsetOperations(ctx sdk.Context, cb func(valAddress
break
}
}
return nil
return iter.Close()
}

// ConvertSdkValidatorToWasm helper method
Expand Down
Loading

0 comments on commit f3c17bd

Please sign in to comment.