Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CORE-794] enable authz module to allow granting privileges #960

Merged
merged 12 commits into from
Jan 25, 2024
43 changes: 36 additions & 7 deletions protocol/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package app
import (
"context"
"encoding/json"
custommodule "github.com/dydxprotocol/v4-chain/protocol/app/module"
"io"
"math/big"
"net/http"
Expand All @@ -13,6 +12,8 @@ import (
"sync"
"time"

custommodule "github.com/dydxprotocol/v4-chain/protocol/app/module"

autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
reflectionv1 "cosmossdk.io/api/cosmos/reflection/v1"
"cosmossdk.io/log"
Expand Down Expand Up @@ -53,6 +54,9 @@ import (
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
authz "github.com/cosmos/cosmos-sdk/x/authz"
authzkeeper "github.com/cosmos/cosmos-sdk/x/authz/keeper"
authzmodule "github.com/cosmos/cosmos-sdk/x/authz/module"
"github.com/cosmos/cosmos-sdk/x/bank"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
Expand Down Expand Up @@ -92,6 +96,7 @@ import (
"google.golang.org/grpc"

// App
appconstants "github.com/dydxprotocol/v4-chain/protocol/app/constants"
"github.com/dydxprotocol/v4-chain/protocol/app/flags"
"github.com/dydxprotocol/v4-chain/protocol/app/middleware"
"github.com/dydxprotocol/v4-chain/protocol/app/prepare"
Expand Down Expand Up @@ -207,7 +212,7 @@ func init() {
panic(err)
}

DefaultNodeHome = filepath.Join(userHomeDir, "."+AppName)
DefaultNodeHome = filepath.Join(userHomeDir, "."+appconstants.AppName)

// Set DefaultPowerReduction to 1e18 to avoid overflow whe calculating
// consensus power.
Expand All @@ -234,6 +239,7 @@ type App struct {

// keepers
AccountKeeper authkeeper.AccountKeeper
AuthzKeeper authzkeeper.Keeper
BankKeeper bankkeeper.Keeper
CapabilityKeeper *capabilitykeeper.Keeper
StakingKeeper *stakingkeeper.Keeper
Expand Down Expand Up @@ -340,17 +346,27 @@ func New(
interfaceRegistry := encodingConfig.InterfaceRegistry
txConfig := encodingConfig.TxConfig

bApp := baseapp.NewBaseApp(AppName, logger, db, txConfig.TxDecoder(), baseAppOptions...)
bApp := baseapp.NewBaseApp(appconstants.AppName, logger, db, txConfig.TxDecoder(), baseAppOptions...)
bApp.SetCommitMultiStoreTracer(traceStore)
bApp.SetVersion(version.Version)
bApp.SetInterfaceRegistry(interfaceRegistry)
bApp.SetTxEncoder(txConfig.TxEncoder())

keys := storetypes.NewKVStoreKeys(
authtypes.StoreKey, banktypes.StoreKey, stakingtypes.StoreKey, crisistypes.StoreKey,
distrtypes.StoreKey, slashingtypes.StoreKey,
govtypes.StoreKey, paramstypes.StoreKey, consensusparamtypes.StoreKey, upgradetypes.StoreKey, feegrant.StoreKey,
ibcexported.StoreKey, ibctransfertypes.StoreKey,
authtypes.StoreKey,
authzkeeper.StoreKey,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

weird this one is not set up in the same way... no authztypes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah not sure how authz was bootstrapped initially, but there is no authz/types and the key is under keeper/keys.go 😕

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looked into this quickly. types got removed in this PR. still not sure why though

banktypes.StoreKey,
stakingtypes.StoreKey,
crisistypes.StoreKey,
distrtypes.StoreKey,
slashingtypes.StoreKey,
govtypes.StoreKey,
paramstypes.StoreKey,
consensusparamtypes.StoreKey,
upgradetypes.StoreKey,
feegrant.StoreKey,
ibcexported.StoreKey,
ibctransfertypes.StoreKey,
ratelimitmoduletypes.StoreKey,
icacontrollertypes.StoreKey,
icahosttypes.StoreKey,
Expand Down Expand Up @@ -430,6 +446,14 @@ func New(
sdk.GetConfig().GetBech32AccountAddrPrefix(),
lib.GovModuleAddress.String(),
)

app.AuthzKeeper = authzkeeper.NewKeeper(
runtime.NewKVStoreService(keys[authzkeeper.StoreKey]),
appCodec,
Comment on lines +451 to +452
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the order of these params is also reversed vs most NewKeeper methods?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah authz always have store key before codec, not sure how it was bootstrapped

app.MsgServiceRouter(),
app.AccountKeeper,
)

app.BankKeeper = bankkeeper.NewBaseKeeper(
appCodec,
runtime.NewKVStoreService(keys[banktypes.StoreKey]),
Expand Down Expand Up @@ -990,6 +1014,7 @@ func New(
),
auth.NewAppModule(appCodec, app.AccountKeeper, nil, app.getSubspace(authtypes.ModuleName)),
bank.NewAppModule(appCodec, app.BankKeeper, app.AccountKeeper, app.getSubspace(banktypes.ModuleName)),
authzmodule.NewAppModule(appCodec, app.AuthzKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry),
capability.NewAppModule(appCodec, *app.CapabilityKeeper, false),
feegrantmodule.NewAppModule(appCodec, app.AccountKeeper, app.BankKeeper, app.FeeGrantKeeper, app.interfaceRegistry),
crisis.NewAppModule(app.CrisisKeeper, skipGenesisInvariants, app.getSubspace(crisistypes.ModuleName)),
Expand Down Expand Up @@ -1051,6 +1076,7 @@ func New(
// NOTE: staking module is required if HistoricalEntries param > 0
app.ModuleManager.SetOrderBeginBlockers(
blocktimemoduletypes.ModuleName, // Must be first
authz.ModuleName, // Delete expired grants.
jayy04 marked this conversation as resolved.
Show resolved Hide resolved
epochsmoduletypes.ModuleName,
capabilitytypes.ModuleName,
distrtypes.ModuleName,
Expand Down Expand Up @@ -1119,6 +1145,7 @@ func New(
rewardsmoduletypes.ModuleName,
epochsmoduletypes.ModuleName,
delaymsgmoduletypes.ModuleName,
authz.ModuleName, // No-op.
blocktimemoduletypes.ModuleName, // Must be last
)

Expand Down Expand Up @@ -1160,6 +1187,7 @@ func New(
rewardsmoduletypes.ModuleName,
sendingmoduletypes.ModuleName,
delaymsgmoduletypes.ModuleName,
authz.ModuleName,
)

// NOTE: by default, set migration order here to be the same as init genesis order,
Expand Down Expand Up @@ -1197,6 +1225,7 @@ func New(
rewardsmoduletypes.ModuleName,
sendingmoduletypes.ModuleName,
delaymsgmoduletypes.ModuleName,
authz.ModuleName,

// Auth must be migrated after staking.
authtypes.ModuleName,
Expand Down
2 changes: 2 additions & 0 deletions protocol/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/auth"
authzmodule "github.com/cosmos/cosmos-sdk/x/authz/module"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/cosmos/cosmos-sdk/x/consensus"
"github.com/cosmos/cosmos-sdk/x/crisis"
Expand Down Expand Up @@ -198,6 +199,7 @@ func TestModuleBasics(t *testing.T) {
upgrade.AppModuleBasic{},
transfer.AppModuleBasic{},
consensus.AppModuleBasic{},
authzmodule.AppModuleBasic{},

// Custom modules
pricesmodule.AppModuleBasic{},
Expand Down
2 changes: 2 additions & 0 deletions protocol/app/basic_manager/basic_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"cosmossdk.io/x/upgrade"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/auth"
authzmodule "github.com/cosmos/cosmos-sdk/x/authz/module"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/cosmos/cosmos-sdk/x/consensus"
"github.com/cosmos/cosmos-sdk/x/crisis"
Expand Down Expand Up @@ -69,6 +70,7 @@ var (
evidence.AppModuleBasic{},
transfer.AppModuleBasic{},
consensus.AppModuleBasic{},
authzmodule.AppModuleBasic{},

// Custom modules
pricesmodule.AppModuleBasic{},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package app
package constants

const (
AppName = "dydxprotocol"
Expand Down
5 changes: 3 additions & 2 deletions protocol/app/datadog.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"cosmossdk.io/log"
"github.com/cosmos/cosmos-sdk/version"
"github.com/dydxprotocol/v4-chain/protocol/app/constants"
"github.com/dydxprotocol/v4-chain/protocol/app/flags"
errorspkg "github.com/pkg/errors"
"github.com/rs/zerolog"
Expand All @@ -28,9 +29,9 @@ func configureDatadogProfilerOptions(
err error,
) {
// Use a default application name unless overridden by the DD_SERVICE environment variable.
ddService = ServiceName
ddService = constants.ServiceName
if found := os.Getenv("DD_SERVICE"); found != "" {
logger.Info(fmt.Sprintf("DD_SERVICE defined, overriding default of '%s'.", ServiceName))
logger.Info(fmt.Sprintf("DD_SERVICE defined, overriding default of '%s'.", constants.ServiceName))
ddService = found
}

Expand Down
9 changes: 9 additions & 0 deletions protocol/app/msgs/all_msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ var (
"/cosmos.auth.v1beta1.ModuleCredential": {},
"/cosmos.auth.v1beta1.MsgUpdateParams": {},

// authz
"/cosmos.authz.v1beta1.GenericAuthorization": {},
"/cosmos.authz.v1beta1.MsgExec": {},
"/cosmos.authz.v1beta1.MsgExecResponse": {},
"/cosmos.authz.v1beta1.MsgGrant": {},
"/cosmos.authz.v1beta1.MsgGrantResponse": {},
"/cosmos.authz.v1beta1.MsgRevoke": {},
"/cosmos.authz.v1beta1.MsgRevokeResponse": {},

// bank
"/cosmos.bank.v1beta1.MsgMultiSend": {},
"/cosmos.bank.v1beta1.MsgMultiSendResponse": {},
Expand Down
34 changes: 21 additions & 13 deletions protocol/app/msgs/internal_msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,14 @@ var (
// InternalMsgSamplesGovAuth are msgs that are used only used internally.
// GovAuth means that these messages must originate from the gov module and
// signed by gov module account.
InternalMsgSamplesGovAuth = map[string]sdk.Msg{
// ------- CosmosSDK default modules
// InternalMsgSamplesAll are msgs that are used only used internally.
InternalMsgSamplesGovAuth = lib.MergeAllMapsMustHaveDistinctKeys(
InternalMsgSamplesDefault,
InternalMsgSamplesDydxCustom,
)

// CosmosSDK default modules
InternalMsgSamplesDefault = map[string]sdk.Msg{
// auth
"/cosmos.auth.v1beta1.MsgUpdateParams": &auth.MsgUpdateParams{},

Expand Down Expand Up @@ -81,7 +87,19 @@ var (
"/cosmos.upgrade.v1beta1.MsgSoftwareUpgrade": &upgrade.MsgSoftwareUpgrade{},
"/cosmos.upgrade.v1beta1.MsgSoftwareUpgradeResponse": nil,

// ------- Custom modules
// ibc
"/ibc.applications.interchain_accounts.host.v1.MsgUpdateParams": &icahosttypes.MsgUpdateParams{},
"/ibc.applications.interchain_accounts.host.v1.MsgUpdateParamsResponse": nil,
"/ibc.applications.transfer.v1.MsgUpdateParams": &ibctransfer.MsgUpdateParams{},
"/ibc.applications.transfer.v1.MsgUpdateParamsResponse": nil,
"/ibc.core.client.v1.MsgUpdateParams": &ibcclient.MsgUpdateParams{},
"/ibc.core.client.v1.MsgUpdateParamsResponse": nil,
"/ibc.core.connection.v1.MsgUpdateParams": &ibcconn.MsgUpdateParams{},
"/ibc.core.connection.v1.MsgUpdateParamsResponse": nil,
}

// Custom modules
InternalMsgSamplesDydxCustom = map[string]sdk.Msg{
// blocktime
"/dydxprotocol.blocktime.MsgUpdateDowntimeParams": &blocktime.MsgUpdateDowntimeParams{},
"/dydxprotocol.blocktime.MsgUpdateDowntimeParamsResponse": nil,
Expand Down Expand Up @@ -149,15 +167,5 @@ var (
"/dydxprotocol.vest.MsgSetVestEntryResponse": nil,
"/dydxprotocol.vest.MsgDeleteVestEntry": &vest.MsgDeleteVestEntry{},
"/dydxprotocol.vest.MsgDeleteVestEntryResponse": nil,

// ibc
"/ibc.applications.interchain_accounts.host.v1.MsgUpdateParams": &icahosttypes.MsgUpdateParams{},
"/ibc.applications.interchain_accounts.host.v1.MsgUpdateParamsResponse": nil,
"/ibc.applications.transfer.v1.MsgUpdateParams": &ibctransfer.MsgUpdateParams{},
"/ibc.applications.transfer.v1.MsgUpdateParamsResponse": nil,
"/ibc.core.client.v1.MsgUpdateParams": &ibcclient.MsgUpdateParams{},
"/ibc.core.client.v1.MsgUpdateParamsResponse": nil,
"/ibc.core.connection.v1.MsgUpdateParams": &ibcconn.MsgUpdateParams{},
"/ibc.core.connection.v1.MsgUpdateParamsResponse": nil,
}
)
5 changes: 5 additions & 0 deletions protocol/app/msgs/nested_msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ package msgs

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/authz"
gov "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
)

var (
// NestedMsgSamples are msgs that have can include other arbitrary messages inside.
NestedMsgSamples = map[string]sdk.Msg{
// authz
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't dug into how nested messages are verified - will someone be able to execute an otherwise disallowed messages as a nested message under MsgExec? Could you reply to @ttl33 's relevant question here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so this PR also updates nested messages verification (see this commit).

see validateInnerMsg for more details . Essentially we disallow inner messages to be

  1. unsupported (some ICA controller messages)
  2. app injected
  3. another nested message

"/cosmos.authz.v1beta1.MsgExec": &authz.MsgExec{},
"/cosmos.authz.v1beta1.MsgExecResponse": nil,

// gov
"/cosmos.gov.v1.MsgSubmitProposal": &gov.MsgSubmitProposal{},
"/cosmos.gov.v1.MsgSubmitProposalResponse": nil,
Expand Down
4 changes: 4 additions & 0 deletions protocol/app/msgs/nested_msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import (

func TestNestedMsgs_Key(t *testing.T) {
expectedMsgs := []string{
// authz
"/cosmos.authz.v1beta1.MsgExec",
"/cosmos.authz.v1beta1.MsgExecResponse",

// gov
"/cosmos.gov.v1.MsgSubmitProposal",
"/cosmos.gov.v1.MsgSubmitProposalResponse",
Expand Down
53 changes: 34 additions & 19 deletions protocol/app/msgs/normal_msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
evidence "cosmossdk.io/x/evidence/types"
feegrant "cosmossdk.io/x/feegrant"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/authz"
bank "github.com/cosmos/cosmos-sdk/x/bank/types"
crisis "github.com/cosmos/cosmos-sdk/x/crisis/types"
distr "github.com/cosmos/cosmos-sdk/x/distribution/types"
Expand All @@ -15,18 +16,29 @@ import (
ibcclient "github.com/cosmos/ibc-go/v8/modules/core/02-client/types" //nolint:staticcheck
ibcconn "github.com/cosmos/ibc-go/v8/modules/core/03-connection/types"
ibccore "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
"github.com/dydxprotocol/v4-chain/protocol/lib"
clob "github.com/dydxprotocol/v4-chain/protocol/x/clob/types"
sending "github.com/dydxprotocol/v4-chain/protocol/x/sending/types"
)

var (
// NormalMsgs are messages that can be submitted by external users.
NormalMsgs = map[string]sdk.Msg{
NormalMsgs = lib.MergeAllMapsMustHaveDistinctKeys(NormalMsgsDefault, NormalMsgsDydxCustom)

// Default modules
NormalMsgsDefault = map[string]sdk.Msg{
// auth
"/cosmos.auth.v1beta1.BaseAccount": nil,
"/cosmos.auth.v1beta1.ModuleAccount": nil,
"/cosmos.auth.v1beta1.ModuleCredential": nil,

// authz
"/cosmos.authz.v1beta1.GenericAuthorization": nil,
"/cosmos.authz.v1beta1.MsgGrant": &authz.MsgGrant{},
"/cosmos.authz.v1beta1.MsgGrantResponse": nil,
"/cosmos.authz.v1beta1.MsgRevoke": &authz.MsgRevoke{},
"/cosmos.authz.v1beta1.MsgRevokeResponse": nil,

// bank
"/cosmos.bank.v1beta1.MsgMultiSend": &bank.MsgMultiSend{},
"/cosmos.bank.v1beta1.MsgMultiSendResponse": nil,
Expand Down Expand Up @@ -124,24 +136,6 @@ var (
"/cosmos.upgrade.v1beta1.CancelSoftwareUpgradeProposal": nil,
"/cosmos.upgrade.v1beta1.SoftwareUpgradeProposal": nil,

// clob
"/dydxprotocol.clob.MsgCancelOrder": &clob.MsgCancelOrder{},
"/dydxprotocol.clob.MsgCancelOrderResponse": nil,
"/dydxprotocol.clob.MsgPlaceOrder": &clob.MsgPlaceOrder{},
"/dydxprotocol.clob.MsgPlaceOrderResponse": nil,

// perpetuals

// prices

// sending
"/dydxprotocol.sending.MsgCreateTransfer": &sending.MsgCreateTransfer{},
"/dydxprotocol.sending.MsgCreateTransferResponse": nil,
"/dydxprotocol.sending.MsgDepositToSubaccount": &sending.MsgDepositToSubaccount{},
"/dydxprotocol.sending.MsgDepositToSubaccountResponse": nil,
"/dydxprotocol.sending.MsgWithdrawFromSubaccount": &sending.MsgWithdrawFromSubaccount{},
"/dydxprotocol.sending.MsgWithdrawFromSubaccountResponse": nil,

// ibc.applications
"/ibc.applications.transfer.v1.MsgTransfer": &ibctransfer.MsgTransfer{},
"/ibc.applications.transfer.v1.MsgTransferResponse": nil,
Expand Down Expand Up @@ -219,4 +213,25 @@ var (
// ica
"/ibc.applications.interchain_accounts.v1.InterchainAccount": nil,
}

// Custom modules
NormalMsgsDydxCustom = map[string]sdk.Msg{
// clob
"/dydxprotocol.clob.MsgCancelOrder": &clob.MsgCancelOrder{},
"/dydxprotocol.clob.MsgCancelOrderResponse": nil,
"/dydxprotocol.clob.MsgPlaceOrder": &clob.MsgPlaceOrder{},
"/dydxprotocol.clob.MsgPlaceOrderResponse": nil,

// perpetuals

// prices

// sending
"/dydxprotocol.sending.MsgCreateTransfer": &sending.MsgCreateTransfer{},
"/dydxprotocol.sending.MsgCreateTransferResponse": nil,
"/dydxprotocol.sending.MsgDepositToSubaccount": &sending.MsgDepositToSubaccount{},
"/dydxprotocol.sending.MsgDepositToSubaccountResponse": nil,
"/dydxprotocol.sending.MsgWithdrawFromSubaccount": &sending.MsgWithdrawFromSubaccount{},
"/dydxprotocol.sending.MsgWithdrawFromSubaccountResponse": nil,
}
)
Loading
Loading