Skip to content

Commit

Permalink
authz
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt committed May 31, 2024
1 parent 5a8e9d7 commit 1c2a04c
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 34 deletions.
6 changes: 3 additions & 3 deletions client/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ func SetCmdClientContext(cmd *cobra.Command, clientCtx Context) error {
}

func GetViperFromCmd(cmd *cobra.Command) *viper.Viper {
value := cmd.Context().Value(corectx.ViperContextKey{})
value := cmd.Context().Value(corectx.ViperContextKey)
v, ok := value.(*viper.Viper)
if !ok {
return viper.New()
Expand All @@ -388,7 +388,7 @@ func GetViperFromCmd(cmd *cobra.Command) *viper.Viper {
}

func GetConfigFromCmd(cmd *cobra.Command) *cmtcfg.Config {
v := cmd.Context().Value(corectx.ViperContextKey{})
v := cmd.Context().Value(corectx.ViperContextKey)
viper, ok := v.(*viper.Viper)
if !ok {
return cmtcfg.DefaultConfig()
Expand All @@ -397,7 +397,7 @@ func GetConfigFromCmd(cmd *cobra.Command) *cmtcfg.Config {
}

func GetLoggerFromCmd(cmd *cobra.Command) log.Logger {
v := cmd.Context().Value(corectx.LoggerContextKey{})
v := cmd.Context().Value(corectx.LoggerContextKey)
logger, ok := v.(log.Logger)
if !ok {
return log.NewLogger(cmd.OutOrStdout())
Expand Down
4 changes: 2 additions & 2 deletions server/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,8 @@ func SetCmdServerContext(cmd *cobra.Command, serverCtx *Context) error {
}

cmdCtx = context.WithValue(cmdCtx, ServerContextKey, serverCtx)
cmdCtx = context.WithValue(cmdCtx, corectx.ViperContextKey{}, serverCtx.Viper)
cmdCtx = context.WithValue(cmdCtx, corectx.LoggerContextKey{}, serverCtx.Logger)
cmdCtx = context.WithValue(cmdCtx, corectx.ViperContextKey, serverCtx.Viper)
cmdCtx = context.WithValue(cmdCtx, corectx.LoggerContextKey, serverCtx.Logger)

cmd.SetContext(cmdCtx)

Expand Down
4 changes: 2 additions & 2 deletions simapp/simd/cmd/testnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ func Test_TestnetCmd(t *testing.T) {
WithValidatorAddressCodec(cdcOpts.GetValidatorCodec())

ctx := context.Background()
ctx = context.WithValue(ctx, corectx.ViperContextKey{}, viper)
ctx = context.WithValue(ctx, corectx.LoggerContextKey{}, logger)
ctx = context.WithValue(ctx, corectx.ViperContextKey, viper)
ctx = context.WithValue(ctx, corectx.LoggerContextKey, logger)
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
cmd := testnetInitFilesCmd(moduleManager, banktypes.GenesisBalancesIterator{})
cmd.SetArgs([]string{fmt.Sprintf("--%s=test", flags.FlagKeyringBackend), fmt.Sprintf("--output-dir=%s", home)})
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/genutil/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestExportCmd_ConsensusParams(t *testing.T) {
func TestExportCmd_HomeDir(t *testing.T) {
_, ctx, _, cmd := setupApp(t, t.TempDir())

v := ctx.Value(corectx.ViperContextKey{})
v := ctx.Value(corectx.ViperContextKey)
viper, ok := v.(*viper.Viper)
require.True(t, ok)
viper.Set(flags.FlagHome, "foobar")
Expand Down Expand Up @@ -220,7 +220,7 @@ func setupApp(t *testing.T, tempDir string) (*simapp.SimApp, context.Context, ge

ctx := context.Background()
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
ctx = context.WithValue(ctx, corectx.ViperContextKey{}, viper)
ctx = context.WithValue(ctx, corectx.ViperContextKey, viper)

return app, ctx, appGenesis, cmd
}
Expand Down
13 changes: 11 additions & 2 deletions x/bank/types/send_authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"

"cosmossdk.io/core/address"
"cosmossdk.io/core/appmodule/v2"
corecontext "cosmossdk.io/core/context"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/authz"
Expand Down Expand Up @@ -40,12 +42,19 @@ func (a SendAuthorization) Accept(ctx context.Context, msg sdk.Msg) (authz.Accep
return authz.AcceptResponse{}, sdkerrors.ErrInsufficientFunds.Wrapf("requested amount is more than spend limit")
}

authzEnv, ok := ctx.Value(corecontext.EnvironmentContextKey).(appmodule.Environment)
if !ok {
return authz.AcceptResponse{}, sdkerrors.ErrUnauthorized.Wrap("environment not set")
}

isAddrExists := false
toAddr := mSend.ToAddress
allowedList := a.GetAllowList()
sdkCtx := sdk.UnwrapSDKContext(ctx)
for _, addr := range allowedList {
sdkCtx.GasMeter().ConsumeGas(gasCostPerIteration, "send authorization")
if err := authzEnv.GasService.GasMeter(ctx).Consume(gasCostPerIteration, "send authorization"); err != nil {
return authz.AcceptResponse{}, err
}

if addr == toAddr {
isAddrExists = true
break
Expand Down
4 changes: 2 additions & 2 deletions x/genutil/client/cli/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ func NewExportSystem(t *testing.T, exporter types.AppExporter) *ExportSystem {

cCtx := (client.Context{}).WithHomeDir(homeDir)

ctx := context.WithValue(context.Background(), corectx.ViperContextKey{}, viper)
ctx = context.WithValue(ctx, corectx.LoggerContextKey{}, logger)
ctx := context.WithValue(context.Background(), corectx.ViperContextKey, viper)
ctx = context.WithValue(ctx, corectx.LoggerContextKey, logger)

ctx = context.WithValue(ctx, client.ClientContextKey, &cCtx)

Expand Down
4 changes: 2 additions & 2 deletions x/genutil/client/cli/genaccount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ func TestAddGenesisAccountCmd(t *testing.T) {

ctx := context.Background()
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
ctx = context.WithValue(ctx, corectx.ViperContextKey{}, viper)
ctx = context.WithValue(ctx, corectx.LoggerContextKey{}, logger)
ctx = context.WithValue(ctx, corectx.ViperContextKey, viper)
ctx = context.WithValue(ctx, corectx.LoggerContextKey, logger)

cmd := genutilcli.AddGenesisAccountCmd(addresscodec.NewBech32Codec("cosmos"))
cmd.SetArgs([]string{
Expand Down
28 changes: 14 additions & 14 deletions x/genutil/client/cli/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ func TestInitCmd(t *testing.T) {

ctx := context.Background()
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
ctx = context.WithValue(ctx, corectx.ViperContextKey{}, viper)
ctx = context.WithValue(ctx, corectx.LoggerContextKey{}, logger)
ctx = context.WithValue(ctx, corectx.ViperContextKey, viper)
ctx = context.WithValue(ctx, corectx.LoggerContextKey, logger)

cmd := genutilcli.InitCmd(testMbm)
cmd.SetArgs(
Expand Down Expand Up @@ -111,8 +111,8 @@ func TestInitRecover(t *testing.T) {

ctx := context.Background()
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
ctx = context.WithValue(ctx, corectx.ViperContextKey{}, viper)
ctx = context.WithValue(ctx, corectx.LoggerContextKey{}, logger)
ctx = context.WithValue(ctx, corectx.ViperContextKey, viper)
ctx = context.WithValue(ctx, corectx.LoggerContextKey, logger)

cmd := genutilcli.InitCmd(testMbm)
cmd.SetContext(ctx)
Expand Down Expand Up @@ -144,8 +144,8 @@ func TestInitDefaultBondDenom(t *testing.T) {

ctx := context.Background()
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
ctx = context.WithValue(ctx, corectx.ViperContextKey{}, viper)
ctx = context.WithValue(ctx, corectx.LoggerContextKey{}, logger)
ctx = context.WithValue(ctx, corectx.ViperContextKey, viper)
ctx = context.WithValue(ctx, corectx.LoggerContextKey, logger)

cmd := genutilcli.InitCmd(testMbm)

Expand All @@ -172,8 +172,8 @@ func TestEmptyState(t *testing.T) {

ctx := context.Background()
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
ctx = context.WithValue(ctx, corectx.ViperContextKey{}, viper)
ctx = context.WithValue(ctx, corectx.LoggerContextKey{}, logger)
ctx = context.WithValue(ctx, corectx.ViperContextKey, viper)
ctx = context.WithValue(ctx, corectx.LoggerContextKey, logger)

cmd := genutilcli.InitCmd(testMbm)
cmd.SetArgs([]string{"appnode-test"})
Expand Down Expand Up @@ -267,8 +267,8 @@ func TestInitConfig(t *testing.T) {

ctx := context.Background()
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
ctx = context.WithValue(ctx, corectx.ViperContextKey{}, viper)
ctx = context.WithValue(ctx, corectx.LoggerContextKey{}, logger)
ctx = context.WithValue(ctx, corectx.ViperContextKey, viper)
ctx = context.WithValue(ctx, corectx.LoggerContextKey, logger)

cmd := genutilcli.InitCmd(testMbm)
cmd.SetArgs([]string{"testnode"})
Expand Down Expand Up @@ -317,8 +317,8 @@ func TestInitWithHeight(t *testing.T) {

ctx := context.Background()
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
ctx = context.WithValue(ctx, corectx.ViperContextKey{}, viper)
ctx = context.WithValue(ctx, corectx.LoggerContextKey{}, logger)
ctx = context.WithValue(ctx, corectx.ViperContextKey, viper)
ctx = context.WithValue(ctx, corectx.LoggerContextKey, logger)

testInitialHeight := int64(333)

Expand Down Expand Up @@ -354,8 +354,8 @@ func TestInitWithNegativeHeight(t *testing.T) {

ctx := context.Background()
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
ctx = context.WithValue(ctx, corectx.ViperContextKey{}, viper)
ctx = context.WithValue(ctx, corectx.LoggerContextKey{}, logger)
ctx = context.WithValue(ctx, corectx.ViperContextKey, viper)
ctx = context.WithValue(ctx, corectx.LoggerContextKey, logger)

testInitialHeight := int64(-333)

Expand Down
4 changes: 2 additions & 2 deletions x/genutil/client/testutil/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ func ExecInitCmd(mm *module.Manager, home string, cdc codec.Codec) error {

ctx := context.Background()
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
ctx = context.WithValue(ctx, corectx.ViperContextKey{}, viper)
ctx = context.WithValue(ctx, corectx.LoggerContextKey{}, logger)
ctx = context.WithValue(ctx, corectx.ViperContextKey, viper)
ctx = context.WithValue(ctx, corectx.LoggerContextKey, logger)

cmd.SetArgs([]string{"appnode-test"})

Expand Down
17 changes: 14 additions & 3 deletions x/staking/types/authz.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"

"cosmossdk.io/core/address"
"cosmossdk.io/core/appmodule"
corecontext "cosmossdk.io/core/context"
errorsmod "cosmossdk.io/errors"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -100,11 +102,17 @@ func (a StakeAuthorization) Accept(ctx context.Context, msg sdk.Msg) (authz.Acce
return authz.AcceptResponse{}, sdkerrors.ErrInvalidRequest.Wrap("unknown msg type")
}

authzEnv, ok := ctx.Value(corecontext.EnvironmentContextKey).(appmodule.Environment)
if !ok {
return authz.AcceptResponse{}, sdkerrors.ErrUnauthorized.Wrap("environment not set")
}
isValidatorExists := false
allowedList := a.GetAllowList().GetAddress()
sdkCtx := sdk.UnwrapSDKContext(ctx)
for _, validator := range allowedList {
sdkCtx.GasMeter().ConsumeGas(gasCostPerIteration, "stake authorization")
if err := authzEnv.GasService.GasMeter(ctx).Consume(gasCostPerIteration, "stake authorization"); err != nil {
return authz.AcceptResponse{}, err
}

if validator == validatorAddress {
isValidatorExists = true
break
Expand All @@ -113,7 +121,10 @@ func (a StakeAuthorization) Accept(ctx context.Context, msg sdk.Msg) (authz.Acce

denyList := a.GetDenyList().GetAddress()
for _, validator := range denyList {
sdkCtx.GasMeter().ConsumeGas(gasCostPerIteration, "stake authorization")
if err := authzEnv.GasService.GasMeter(ctx).Consume(gasCostPerIteration, "stake authorization"); err != nil {
return authz.AcceptResponse{}, err
}

if validator == validatorAddress {
return authz.AcceptResponse{}, sdkerrors.ErrUnauthorized.Wrapf("cannot delegate/undelegate to %s validator", validator)
}
Expand Down

0 comments on commit 1c2a04c

Please sign in to comment.