Skip to content

Commit

Permalink
feat(simulator): progress
Browse files Browse the repository at this point in the history
  • Loading branch information
fmorency committed Jun 14, 2024
1 parent d898472 commit 5558615
Show file tree
Hide file tree
Showing 7 changed files with 258 additions and 24 deletions.
5 changes: 5 additions & 0 deletions keeper/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@ import (
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
)

type AccountKeeper interface {
GetAccount(ctx context.Context, addr sdk.AccAddress) sdk.AccountI
}

type BankKeeper interface {
GetBalance(ctx context.Context, addr sdk.AccAddress, denom string) sdk.Coin
MintCoins(ctx context.Context, moduleName string, amt sdk.Coins) error
SendCoinsFromModuleToAccount(ctx context.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error
SendCoinsFromModuleToModule(ctx context.Context, senderModule, recipientModule string, amt sdk.Coins) error
SpendableCoins(ctx context.Context, addr sdk.AccAddress) sdk.Coins
}

type SlashingKeeper interface {
Expand Down
7 changes: 7 additions & 0 deletions keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Keeper struct {
cdc codec.BinaryCodec

stakingKeeper *stakingkeeper.Keeper
accountKeeper AccountKeeper
slashKeeper SlashingKeeper
bankKeeper BankKeeper

Expand All @@ -46,6 +47,7 @@ func NewKeeper(
sk *stakingkeeper.Keeper,
slk SlashingKeeper,
bk BankKeeper,
ak AccountKeeper,
logger log.Logger,
) Keeper {
logger = logger.With(log.ModuleKey, "x/"+poa.ModuleName)
Expand All @@ -57,6 +59,7 @@ func NewKeeper(
stakingKeeper: sk,
slashKeeper: slk,
bankKeeper: bk,
accountKeeper: ak,
logger: logger,

// Stores
Expand Down Expand Up @@ -96,6 +99,10 @@ func (k Keeper) GetBankKeeper() BankKeeper {
return k.bankKeeper
}

func (k Keeper) GetAccountKeeper() AccountKeeper {
return k.accountKeeper
}

// GetAdmins returns the module's administrators with delegation of power control.
func (k Keeper) GetAdmins(ctx context.Context) []string {
p, err := k.GetParams(ctx)
Expand Down
2 changes: 1 addition & 1 deletion keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func SetupTest(t *testing.T, baseValShares int64) *testFixture {
registerBaseSDKModules(f, encCfg, storeService, logger, require)

// Setup POA Keeper.
f.k = keeper.NewKeeper(encCfg.Codec, storeService, f.stakingKeeper, f.slashingKeeper, f.bankkeeper, logger)
f.k = keeper.NewKeeper(encCfg.Codec, storeService, f.stakingKeeper, f.slashingKeeper, f.bankkeeper, f.accountkeeper, logger)
f.msgServer = keeper.NewMsgServerImpl(f.k)
f.queryServer = keeper.NewQueryServerImpl(f.k)
f.appModule = poamodule.NewAppModule(encCfg.Codec, f.k)
Expand Down
4 changes: 3 additions & 1 deletion module/depinject.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/types/module"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
accountkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
Expand Down Expand Up @@ -47,6 +48,7 @@ type ModuleInputs struct {
StakingKeeper stakingkeeper.Keeper
SlashingKeeper slashingkeeper.Keeper
BankKeeper bankkeeper.Keeper
AccountKeeper accountkeeper.AccountKeeper
}

type ModuleOutputs struct {
Expand All @@ -57,7 +59,7 @@ type ModuleOutputs struct {
}

func ProvideModule(in ModuleInputs) ModuleOutputs {
k := keeper.NewKeeper(in.Cdc, in.StoreService, &in.StakingKeeper, in.SlashingKeeper, in.BankKeeper, log.NewLogger(os.Stderr))
k := keeper.NewKeeper(in.Cdc, in.StoreService, &in.StakingKeeper, in.SlashingKeeper, in.BankKeeper, in.AccountKeeper, log.NewLogger(os.Stderr))
m := NewAppModule(in.Cdc, k)

return ModuleOutputs{Module: m, Keeper: k, Out: depinject.Out{}}
Expand Down
1 change: 1 addition & 0 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ func NewSimApp(
app.StakingKeeper,
app.SlashingKeeper,
app.BankKeeper,
app.AccountKeeper,
logger,
)

Expand Down
26 changes: 15 additions & 11 deletions simulation/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import (
"math/rand"

"github.com/cosmos/cosmos-sdk/types/module"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/strangelove-ventures/poa"
)

Expand All @@ -16,22 +15,27 @@ const (
allowValSelfExit = "allow_validator_self_exit"
)

// TODO: Fix randomization
func genAdmins(r *rand.Rand) []string {
govModuleAddress := authtypes.NewModuleAddress(govtypes.ModuleName).String()
return []string{govModuleAddress}
}

func RandomizedGenState(simState *module.SimulationState) {
var (
adm []string
allowSelfExit bool
)

// The POA admin is the governance module address N% of the time
// Allow validator self exit is enabled 50% of the time
simState.AppParams.GetOrGenerate(admins, &adm, simState.Rand, func(r *rand.Rand) {
// Select a random number of admins from the simState accounts
adminSet := make(map[string]bool)
numAdmins := simulation.RandIntBetween(r, 1, len(simState.Accounts))
for i := 0; i < numAdmins; i++ {
acc, _ := simulation.RandomAcc(r, simState.Accounts)
adminSet[acc.Address.String()] = true
}

simState.AppParams.GetOrGenerate(admins, &adm, simState.Rand, func(r *rand.Rand) { adm = genAdmins(r) })
for k := range adminSet {
adm = append(adm, k)
}
})

// Allow validator self exit is enabled 50% of the time
simState.AppParams.GetOrGenerate(allowValSelfExit, &allowSelfExit, simState.Rand, func(r *rand.Rand) { allowSelfExit = r.Intn(2) == 1 })

poaGenesis := poa.GenesisState{
Expand Down
Loading

0 comments on commit 5558615

Please sign in to comment.