Skip to content

Commit

Permalink
module dao for app
Browse files Browse the repository at this point in the history
  • Loading branch information
DongLieu committed Sep 16, 2024
1 parent 199362f commit dd41c73
Show file tree
Hide file tree
Showing 12 changed files with 203 additions and 85 deletions.
26 changes: 4 additions & 22 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io"
"net/http"
"os"
"path/filepath"

Expand Down Expand Up @@ -76,8 +77,7 @@ import (
v1_1_1 "github.com/onomyprotocol/onomy/app/upgrades/v1.1.1"
v1_1_2 "github.com/onomyprotocol/onomy/app/upgrades/v1.1.2"
v1_1_4 "github.com/onomyprotocol/onomy/app/upgrades/v1.1.4"
// "github.com/onomyprotocol/onomy/docs"
// "github.com/onomyprotocol/onomy/x/dao"
"github.com/onomyprotocol/onomy/docs"
// daoclient "github.com/onomyprotocol/onomy/x/dao/client"
// daokeeper "github.com/onomyprotocol/onomy/x/dao/keeper"
// daotypes "github.com/onomyprotocol/onomy/x/dao/types"
Expand All @@ -90,26 +90,6 @@ const (
AppName = "onomy"
)

// func getGovProposalHandlers() []govclient.ProposalHandler {
// var govProposalHandlers []govclient.ProposalHandler

// govProposalHandlers = append(govProposalHandlers,
// paramsclient.ProposalHandler,
// distrclient.ProposalHandler,
// upgradeclient.ProposalHandler,
// upgradeclient.CancelProposalHandler,
// ibcclientclient.UpdateClientProposalHandler,
// ibcclientclient.UpgradeProposalHandler,
// daoclient.FundTreasuryProposalHandler,
// daoclient.ExchangeWithTreasuryProposalProposalHandler,
// ibcproviderclient.ConsumerAdditionProposalHandler,
// ibcproviderclient.ConsumerRemovalProposalHandler,
// ibcproviderclient.EquivocationProposalHandler,
// )

// return govProposalHandlers
// }

var (
// DefaultNodeHome default home directories for the application daemon.
DefaultNodeHome string // nolint:gochecknoglobals // cosmos-sdk application style
Expand Down Expand Up @@ -488,6 +468,8 @@ func (app *OnomyApp) RegisterAPIRoutes(apiSvr *api.Server, apiConfig config.APIC
if err := server.RegisterSwaggerAPI(apiSvr.ClientCtx, apiSvr.Router, apiConfig.Swagger); err != nil {
panic(err)
}
// register app's OpenAPI routes.
apiSvr.Router.Handle("/openapi/openapi.yml", http.FileServer(http.FS(docs.Docs)))
}

// RegisterTxService implements the Application.RegisterTxService method.
Expand Down
137 changes: 113 additions & 24 deletions app/keepers/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,22 @@ import (
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
capabilitykeeper "github.com/cosmos/ibc-go/modules/capability/keeper"
capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types"

"github.com/cosmos/ibc-go/v8/modules/apps/transfer"
ibctransfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
ibcclienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
ibcconnectiontypes "github.com/cosmos/ibc-go/v8/modules/core/03-connection/types"
ibcexported "github.com/cosmos/ibc-go/v8/modules/core/exported"
ibckeeper "github.com/cosmos/ibc-go/v8/modules/core/keeper"

ibctransferkeeper "github.com/cosmos/ibc-go/v8/modules/apps/transfer/keeper"
porttypes "github.com/cosmos/ibc-go/v8/modules/core/05-port/types"
icsprovider "github.com/cosmos/interchain-security/v5/x/ccv/provider"
icsproviderkeeper "github.com/cosmos/interchain-security/v5/x/ccv/provider/keeper"
providertypes "github.com/cosmos/interchain-security/v5/x/ccv/provider/types"

"github.com/onomyprotocol/onomy/x/dao"
daokeeper "github.com/onomyprotocol/onomy/x/dao/keeper"
daotypes "github.com/onomyprotocol/onomy/x/dao/types"
)

type AppKeepers struct {
Expand All @@ -65,34 +72,35 @@ type AppKeepers struct {
memKeys map[string]*storetypes.MemoryStoreKey

// keepers
AccountKeeper authkeeper.AccountKeeper
BankKeeper bankkeeper.Keeper
CapabilityKeeper *capabilitykeeper.Keeper
StakingKeeper *stakingkeeper.Keeper
SlashingKeeper slashingkeeper.Keeper
MintKeeper mintkeeper.Keeper
DistrKeeper distrkeeper.Keeper
GovKeeper *govkeeper.Keeper
CrisisKeeper *crisiskeeper.Keeper
UpgradeKeeper *upgradekeeper.Keeper
ParamsKeeper paramskeeper.Keeper

AccountKeeper authkeeper.AccountKeeper
BankKeeper bankkeeper.Keeper
CapabilityKeeper *capabilitykeeper.Keeper
StakingKeeper *stakingkeeper.Keeper
SlashingKeeper slashingkeeper.Keeper
MintKeeper mintkeeper.Keeper
DistrKeeper distrkeeper.Keeper
GovKeeper *govkeeper.Keeper
CrisisKeeper *crisiskeeper.Keeper
UpgradeKeeper *upgradekeeper.Keeper
ParamsKeeper paramskeeper.Keeper
EvidenceKeeper evidencekeeper.Keeper
FeeGrantKeeper feegrantkeeper.Keeper
AuthzKeeper authzkeeper.Keeper
ConsensusParamsKeeper consensusparamkeeper.Keeper

IBCKeeper *ibckeeper.Keeper
IBCKeeper *ibckeeper.Keeper
TransferKeeper ibctransferkeeper.Keeper
ProviderKeeper icsproviderkeeper.Keeper

// make scoped keepers public for test purposes
ScopedIBCKeeper capabilitykeeper.ScopedKeeper
ScopedTransferKeeper capabilitykeeper.ScopedKeeper

//
TransferKeeper ibctransferkeeper.Keeper
ProviderKeeper icsproviderkeeper.Keeper
ScopedIBCProviderKeeper capabilitykeeper.ScopedKeeper
// DaoKeeper daokeeper.Keeper
ScopedIBCKeeper capabilitykeeper.ScopedKeeper
ScopedTransferKeeper capabilitykeeper.ScopedKeeper
ScopedICSproviderkeeper capabilitykeeper.ScopedKeeper

// Modules
TransferModule transfer.AppModule
ProviderModule icsprovider.AppModule

DaoKeeper daokeeper.Keeper
}

func NewAppKeeper(
Expand Down Expand Up @@ -149,6 +157,7 @@ func NewAppKeeper(

appKeepers.ScopedIBCKeeper = appKeepers.CapabilityKeeper.ScopeToModule(ibcexported.ModuleName)
appKeepers.ScopedTransferKeeper = appKeepers.CapabilityKeeper.ScopeToModule(ibctransfertypes.ModuleName)
appKeepers.ScopedICSproviderkeeper = appKeepers.CapabilityKeeper.ScopeToModule(providertypes.ModuleName)

// Applications that wish to enforce statically created ScopedKeepers should call `Seal` after creating
// their scoped modules in `NewApp` with `ScopeToModule`
Expand Down Expand Up @@ -231,9 +240,16 @@ func NewAppKeeper(
stakingtypes.NewMultiStakingHooks(
appKeepers.DistrKeeper.Hooks(),
appKeepers.SlashingKeeper.Hooks(),
appKeepers.ProviderKeeper.Hooks(),
),
)

// protect the dao module form the slashing
appKeepers.StakingKeeper = appKeepers.StakingKeeper.SetSlashingProtestedModules(func() map[string]struct{} {
return map[string]struct{}{
daotypes.ModuleName: {},
}
})
// UpgradeKeeper must be created before IBCKeeper
appKeepers.UpgradeKeeper = upgradekeeper.NewKeeper(
skipUpgradeHeights,
Expand All @@ -255,6 +271,27 @@ func NewAppKeeper(
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)

appKeepers.ProviderKeeper = icsproviderkeeper.NewKeeper(
appCodec,
appKeepers.keys[providertypes.StoreKey],
appKeepers.GetSubspace(providertypes.ModuleName),
appKeepers.ScopedICSproviderkeeper,
appKeepers.IBCKeeper.ChannelKeeper,
appKeepers.IBCKeeper.PortKeeper,
appKeepers.IBCKeeper.ConnectionKeeper,
appKeepers.IBCKeeper.ClientKeeper,
appKeepers.StakingKeeper,
appKeepers.SlashingKeeper,
appKeepers.AccountKeeper,
appKeepers.DistrKeeper,
appKeepers.BankKeeper,
govkeeper.Keeper{},
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
authcodec.NewBech32Codec(sdk.GetConfig().GetBech32ValidatorAddrPrefix()),
authcodec.NewBech32Codec(sdk.GetConfig().GetBech32ConsensusAddrPrefix()),
authtypes.FeeCollectorName,
)

// gov depends on provider, so needs to be set after
govConfig := govtypes.DefaultConfig()
// set the MaxMetadataLen for proposals to the same value as it was pre-sdk v0.47.x
Expand Down Expand Up @@ -283,19 +320,40 @@ func NewAppKeeper(
authtypes.FeeCollectorName,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)
ibcmodule := transfer.NewIBCModule(appKeepers.TransferKeeper)

// appKeepers.ProviderKeeper.SetGovKeeper(*appKeepers.GovKeeper)

appKeepers.ProviderModule = icsprovider.NewAppModule(
&appKeepers.ProviderKeeper,
appKeepers.GetSubspace(providertypes.ModuleName),
)

// Create static IBC router, add transfer route, then set and seal it
ibcRouter := porttypes.NewRouter()
ibcRouter.AddRoute(ibctransfertypes.ModuleName, ibcmodule)
ibcRouter.AddRoute(providertypes.ModuleName, appKeepers.ProviderModule)
appKeepers.IBCKeeper.SetRouter(ibcRouter)
// Register the proposal types
// Deprecated: Avoid adding new handlers, instead use the new proposal flow
// by granting the governance module the right to execute the message.
// See: https://docs.cosmos.network/main/modules/gov#proposal-messages
govRouter := govv1beta1.NewRouter()
govRouter.
AddRoute(govtypes.RouterKey, govv1beta1.ProposalHandler).
AddRoute(paramproposal.RouterKey, params.NewParamChangeProposalHandler(appKeepers.ParamsKeeper))
AddRoute(paramproposal.RouterKey, params.NewParamChangeProposalHandler(appKeepers.ParamsKeeper)).
AddRoute(daotypes.RouterKey, dao.NewProposalHandler(appKeepers.DaoKeeper)).
AddRoute(providertypes.RouterKey, icsprovider.NewProviderProposalHandler(appKeepers.ProviderKeeper))

// Set legacy router for backwards compatibility with gov v1beta1
appKeepers.GovKeeper.SetLegacyRouter(govRouter)

appKeepers.GovKeeper = appKeepers.GovKeeper.SetHooks(
govtypes.NewMultiGovHooks(
appKeepers.ProviderKeeper.Hooks(),
),
)

evidenceKeeper := evidencekeeper.NewKeeper(
appCodec,
runtime.NewKVStoreService(appKeepers.keys[evidencetypes.StoreKey]),
Expand All @@ -305,6 +363,34 @@ func NewAppKeeper(
runtime.ProvideCometInfoService(),
)

appKeepers.TransferKeeper = ibctransferkeeper.NewKeeper(
appCodec,
appKeepers.keys[ibctransfertypes.StoreKey],
appKeepers.GetSubspace(ibctransfertypes.ModuleName),
appKeepers.IBCKeeper.ChannelKeeper,
appKeepers.IBCKeeper.ChannelKeeper,
appKeepers.IBCKeeper.PortKeeper,
appKeepers.AccountKeeper,
appKeepers.BankKeeper,
appKeepers.ScopedTransferKeeper,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)

appKeepers.TransferModule = transfer.NewAppModule(appKeepers.TransferKeeper)

appKeepers.DaoKeeper = *daokeeper.NewKeeper(
appCodec,
runtime.NewKVStoreService(appKeepers.keys[daotypes.StoreKey]),
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
appKeepers.GetSubspace(daotypes.ModuleName),
appKeepers.BankKeeper,
appKeepers.AccountKeeper,
appKeepers.DistrKeeper,
appKeepers.GovKeeper,
appKeepers.MintKeeper,
appKeepers.StakingKeeper,
)

// If evidence needs to be handled for the app, set routes in router here and seal
appKeepers.EvidenceKeeper = *evidenceKeeper

Expand Down Expand Up @@ -336,6 +422,9 @@ func initParamsKeeper(appCodec codec.BinaryCodec, legacyAmino *codec.LegacyAmino
paramsKeeper.Subspace(govtypes.ModuleName).WithKeyTable(govv1.ParamKeyTable()) //nolint: staticcheck // SA1019
paramsKeeper.Subspace(crisistypes.ModuleName).WithKeyTable(crisistypes.ParamKeyTable()) //nolint: staticcheck // SA1019
paramsKeeper.Subspace(ibcexported.ModuleName).WithKeyTable(keyTable)
paramsKeeper.Subspace(ibctransfertypes.ModuleName).WithKeyTable(ibctransfertypes.ParamKeyTable())
paramsKeeper.Subspace(providertypes.ModuleName).WithKeyTable(providertypes.ParamKeyTable())
paramsKeeper.Subspace(daotypes.ModuleName)

return paramsKeeper
}
Expand Down
4 changes: 4 additions & 0 deletions app/keepers/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
providertypes "github.com/cosmos/interchain-security/v5/x/ccv/provider/types"
daotypes "github.com/onomyprotocol/onomy/x/dao/types"
)

func (appKeepers *AppKeepers) GenerateKeys() {
Expand All @@ -49,6 +51,8 @@ func (appKeepers *AppKeepers) GenerateKeys() {
capabilitytypes.StoreKey,
feegrant.StoreKey,
authzkeeper.StoreKey,
providertypes.StoreKey,
daotypes.StoreKey,
consensusparamtypes.StoreKey,
)

Expand Down
16 changes: 16 additions & 0 deletions app/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,16 @@ import (
ibc "github.com/cosmos/ibc-go/v8/modules/core"
ibcexported "github.com/cosmos/ibc-go/v8/modules/core/exported"
providertypes "github.com/cosmos/interchain-security/v5/x/ccv/provider/types"

"github.com/onomyprotocol/onomy/x/dao"
daotypes "github.com/onomyprotocol/onomy/x/dao/types"
)

var (
// module account permissions.
maccPerms = map[string][]string{ // nolint:gochecknoglobals // cosmos-sdk application style
authtypes.FeeCollectorName: nil,
daotypes.ModuleName: {authtypes.Minter},
distrtypes.ModuleName: nil,
minttypes.ModuleName: {authtypes.Minter},
stakingtypes.BondedPoolName: {authtypes.Burner, authtypes.Staking},
Expand All @@ -67,6 +71,7 @@ var (
// module accounts that are allowed to receive tokens.
allowedReceivingModAcc = map[string]bool{ // nolint:gochecknoglobals // cosmos-sdk application style
distrtypes.ModuleName: true,
daotypes.ModuleName: true,
// provider chain note: the fee-pool is allowed to receive tokens
authtypes.FeeCollectorName: true,
}
Expand All @@ -82,6 +87,7 @@ func appModules(
auth.NewAppModule(appCodec, app.AccountKeeper, nil, app.GetSubspace(authtypes.ModuleName)),
vesting.NewAppModule(app.AccountKeeper, app.BankKeeper),
bank.NewAppModule(appCodec, app.BankKeeper, app.AccountKeeper, app.GetSubspace(banktypes.ModuleName)),
dao.NewAppModule(appCodec, app.DaoKeeper),
capability.NewAppModule(appCodec, *app.CapabilityKeeper, false),
crisis.NewAppModule(app.CrisisKeeper, skipGenesisInvariants, app.GetSubspace(crisistypes.ModuleName)),
gov.NewAppModule(appCodec, app.GovKeeper, app.AccountKeeper, app.BankKeeper, app.GetSubspace(govtypes.ModuleName)),
Expand All @@ -97,6 +103,8 @@ func appModules(
consensus.NewAppModule(appCodec, app.ConsensusParamsKeeper),
staking.NewAppModule(appCodec, app.StakingKeeper, app.AccountKeeper, app.BankKeeper, app.GetSubspace(stakingtypes.ModuleName)),
genutil.NewAppModule(app.AccountKeeper, app.StakingKeeper, app, txConfig),
app.ProviderModule,
app.TransferModule,
// and
}

Expand All @@ -110,6 +118,8 @@ func newBasicManagerFromManager(app *OnomyApp) module.BasicManager {
govtypes.ModuleName: gov.NewAppModuleBasic(
[]govclient.ProposalHandler{
paramsclient.ProposalHandler,
dao.FundTreasuryProposalHandler,
dao.ExchangeWithTreasuryProposalProposalHandler,
},
),
})
Expand Down Expand Up @@ -142,6 +152,7 @@ func orderBeginBlockers() []string {
consensusparamtypes.ModuleName,
providertypes.ModuleName,
// and
daotypes.ModuleName,
}
}

Expand Down Expand Up @@ -170,6 +181,7 @@ func orderEndBlockers() []string {
consensusparamtypes.ModuleName,
providertypes.ModuleName,
// and
daotypes.ModuleName,
}
}

Expand Down Expand Up @@ -197,6 +209,7 @@ func orderInitBlockers() []string {
consensusparamtypes.ModuleName,
crisistypes.ModuleName,
providertypes.ModuleName,
daotypes.ModuleName,
}
}

Expand All @@ -208,6 +221,7 @@ func simulationModules(
return []module.AppModuleSimulation{
auth.NewAppModule(appCodec, app.AccountKeeper, authsims.RandomGenesisAccounts, app.GetSubspace(authtypes.ModuleName)),
bank.NewAppModule(appCodec, app.BankKeeper, app.AccountKeeper, app.GetSubspace(banktypes.ModuleName)),
dao.NewAppModule(appCodec, app.DaoKeeper),
capability.NewAppModule(appCodec, *app.CapabilityKeeper, false),
feegrantmodule.NewAppModule(appCodec, app.AccountKeeper, app.BankKeeper, app.FeeGrantKeeper, app.interfaceRegistry),
gov.NewAppModule(appCodec, app.GovKeeper, app.AccountKeeper, app.BankKeeper, app.GetSubspace(govtypes.ModuleName)),
Expand All @@ -219,5 +233,7 @@ func simulationModules(
evidence.NewAppModule(app.EvidenceKeeper),
authzmodule.NewAppModule(appCodec, app.AuthzKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry),
ibc.NewAppModule(app.IBCKeeper),
app.ProviderModule,
app.TransferModule,
}
}
7 changes: 6 additions & 1 deletion x/dao/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ func BeginBlocker(ctx context.Context, k keeper.Keeper) {
// EndBlocker calls the dao re-balancing every block.
func EndBlocker(ctx context.Context, k keeper.Keeper) {
// defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyEndBlocker)
totalStakingSupply, err := k.GetDaoDelegationSupply(ctx)
if err != nil {
k.Logger(ctx).Error("dao EndBlocker error: %v", err)
debug.PrintStack()
}

if k.GetDaoDelegationSupply(ctx).GT(math.LegacyZeroDec()) {
if totalStakingSupply.GT(math.LegacyZeroDec()) {
if err := k.VoteAbstain(ctx); err != nil {
k.Logger(ctx).Error("dao EndBlocker error: %v", err)
debug.PrintStack()
Expand Down
Loading

0 comments on commit dd41c73

Please sign in to comment.