-
Notifications
You must be signed in to change notification settings - Fork 133
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
Changes from 10 commits
01ef875
3cc63a5
247a97f
e1b924f
030a4be
76adfd6
287efa5
46e0f31
9ff75f7
902985d
697482c
07ce065
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,6 +53,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" | ||
|
@@ -234,6 +237,7 @@ type App struct { | |
|
||
// keepers | ||
AccountKeeper authkeeper.AccountKeeper | ||
AuthzKeeper authzkeeper.Keeper | ||
BankKeeper bankkeeper.Keeper | ||
CapabilityKeeper *capabilitykeeper.Keeper | ||
StakingKeeper *stakingkeeper.Keeper | ||
|
@@ -347,10 +351,20 @@ func New( | |
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, | ||
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, | ||
|
@@ -430,6 +444,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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the order of these params is also reversed vs most There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah |
||
app.MsgServiceRouter(), | ||
app.AccountKeeper, | ||
) | ||
|
||
app.BankKeeper = bankkeeper.NewBaseKeeper( | ||
appCodec, | ||
runtime.NewKVStoreService(keys[banktypes.StoreKey]), | ||
|
@@ -990,6 +1012,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)), | ||
|
@@ -1051,6 +1074,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, | ||
|
@@ -1119,6 +1143,7 @@ func New( | |
rewardsmoduletypes.ModuleName, | ||
epochsmoduletypes.ModuleName, | ||
delaymsgmoduletypes.ModuleName, | ||
authz.ModuleName, // No-op. | ||
blocktimemoduletypes.ModuleName, // Must be last | ||
) | ||
|
||
|
@@ -1160,6 +1185,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, | ||
|
@@ -1197,6 +1223,7 @@ func New( | |
rewardsmoduletypes.ModuleName, | ||
sendingmoduletypes.ModuleName, | ||
delaymsgmoduletypes.ModuleName, | ||
authz.ModuleName, | ||
|
||
// Auth must be migrated after staking. | ||
authtypes.ModuleName, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so this PR also updates nested messages verification (see this commit). see
|
||
"/cosmos.authz.v1beta1.MsgExec": &authz.MsgExec{}, | ||
"/cosmos.authz.v1beta1.MsgExecResponse": nil, | ||
|
||
// gov | ||
"/cosmos.gov.v1.MsgSubmitProposal": &gov.MsgSubmitProposal{}, | ||
"/cosmos.gov.v1.MsgSubmitProposalResponse": nil, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,9 @@ | |
}, | ||
"accounts": [] | ||
}, | ||
"authz": { | ||
"authorization": [] | ||
}, | ||
"bank": { | ||
"params": { | ||
"send_enabled": [], | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ package v_4_0_0 | |
import ( | ||
store "cosmossdk.io/store/types" | ||
circuittypes "cosmossdk.io/x/circuit/types" | ||
authzkeeper "github.com/cosmos/cosmos-sdk/x/authz/keeper" | ||
icacontrollertypes "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts/controller/types" | ||
"github.com/dydxprotocol/v4-chain/protocol/app/upgrades" | ||
) | ||
|
@@ -21,6 +22,9 @@ var Upgrade = upgrades.Upgrade{ | |
|
||
// Add new ICA stores that are needed by ICA host types as of v8. | ||
icacontrollertypes.StoreKey, | ||
|
||
// Add authz module to allow granting arbitrary privileges from one account to another acocunt. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there anything else needed in the upgrade to initialize There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah I think this should be empty. there is currently no authorizations since it was not enabled previously Each |
||
authzkeeper.StoreKey, | ||
}, | ||
}, | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,73 +2,99 @@ package ante | |
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
authz "github.com/cosmos/cosmos-sdk/x/authz" | ||
gov "github.com/cosmos/cosmos-sdk/x/gov/types/v1" | ||
"github.com/dydxprotocol/v4-chain/protocol/lib/metrics" | ||
) | ||
|
||
const DYDX_MSG_PREFIX = "/dydxprotocol" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. mega nit: might be better to use the AppName |
||
|
||
// IsNestedMsg returns true if the given msg is a nested msg. | ||
func IsNestedMsg(msg sdk.Msg) bool { | ||
switch msg.(type) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Question: do we need There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good callout. I think so since it also wraps other messages. Let's add this in a separate PR since it's unrelated. I also have less context on delay messages so maybe one of the OTE eng can pick this up? |
||
case | ||
// ------- CosmosSDK default modules | ||
// authz | ||
*authz.MsgExec, | ||
// gov | ||
*gov.MsgSubmitProposal: | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
// IsDydxMsg returns true if the given msg is a dYdX custom msg. | ||
func IsDydxMsg(msg sdk.Msg) bool { | ||
return strings.HasPrefix(sdk.MsgTypeURL(msg), DYDX_MSG_PREFIX) | ||
} | ||
|
||
// ValidateNestedMsg returns err if the given msg is an invalid nested msg. | ||
func ValidateNestedMsg(msg sdk.Msg) error { | ||
if !IsNestedMsg(msg) { | ||
return fmt.Errorf("not a nested msg") | ||
} | ||
|
||
// Get inner msgs. | ||
innerMsgs, err := getInnerMsgs(msg) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Check that the inner msgs are valid. | ||
if err := validateInnerMsg(innerMsgs); err != nil { | ||
if err := validateInnerMsg(msg); err != nil { | ||
return err | ||
} | ||
|
||
return nil // is valid nested msg. | ||
} | ||
|
||
// getInnerMsgs returns the inner msgs of the given msg. | ||
func getInnerMsgs(msg sdk.Msg) ([]sdk.Msg, error) { | ||
switch msg := msg.(type) { | ||
case | ||
*gov.MsgSubmitProposal: | ||
return msg.GetMsgs() | ||
default: | ||
return nil, fmt.Errorf("unsupported msg type: %T", msg) | ||
// validateInnerMsg returns err if the given inner msgs contain an invalid msg. | ||
func validateInnerMsg(msg sdk.Msg) error { | ||
// Get inner msgs. | ||
innerMsgs, err := getInnerMsgs(msg) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
// validateInnerMsg returns err if the given inner msgs contain an invalid msg. | ||
func validateInnerMsg(innerMsgs []sdk.Msg) error { | ||
for _, msg := range innerMsgs { | ||
for _, inner := range innerMsgs { | ||
// 1. unsupported msgs. | ||
if IsUnsupportedMsg(msg) { | ||
if IsUnsupportedMsg(inner) { | ||
return fmt.Errorf("Invalid nested msg: unsupported msg type") | ||
} | ||
|
||
// 2. app-injected msgs. | ||
if IsAppInjectedMsg(msg) { | ||
if IsAppInjectedMsg(inner) { | ||
return fmt.Errorf("Invalid nested msg: app-injected msg type") | ||
} | ||
|
||
// 3. double-nested msgs. | ||
if IsNestedMsg(msg) { | ||
if IsNestedMsg(inner) { | ||
return fmt.Errorf("Invalid nested msg: double-nested msg type") | ||
} | ||
|
||
// 4.Reject nested dydxprotocol messages in `MsgExec`. | ||
jayy04 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if _, ok := msg.(*authz.MsgExec); ok { | ||
metrics.IncrCountMetricWithLabels( | ||
jayy04 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
metrics.Ante, | ||
metrics.MsgExec, | ||
metrics.GetLabelForStringValue(metrics.InnerMsg, sdk.MsgTypeURL(inner)), | ||
) | ||
if IsDydxMsg(inner) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this mean we don't allow Is there any context/discussion behind this? If so could you document briefly? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah there was a comment discussion in the authz one pager that got resolved. I added a short section in the doc saying that dydx messages will be explicitly disallowed for now and an allowlist can be maintained in the future if there are use cases. |
||
return fmt.Errorf("Invalid nested msg for MsgExec: dydx msg type") | ||
} | ||
} | ||
|
||
// For "internal msgs", we allow them, because they are designed to be nested. | ||
} | ||
return nil | ||
} | ||
|
||
// getInnerMsgs returns the inner msgs of the given msg. | ||
func getInnerMsgs(msg sdk.Msg) ([]sdk.Msg, error) { | ||
switch msg := msg.(type) { | ||
case | ||
*gov.MsgSubmitProposal: | ||
jayy04 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return msg.GetMsgs() | ||
case *authz.MsgExec: | ||
return msg.GetMessages() | ||
default: | ||
return nil, fmt.Errorf("unsupported msg type: %T", msg) | ||
} | ||
} |
There was a problem hiding this comment.
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
?There was a problem hiding this comment.
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 noauthz/types
and the key is underkeeper/keys.go
😕There was a problem hiding this comment.
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