Merge branch 'master' into powerreduction_param
This commit is contained in:
@@ -1,18 +1,21 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
||||
authz "github.com/cosmos/cosmos-sdk/x/authz/exported"
|
||||
)
|
||||
|
||||
// TODO: Revisit this once we have propoer gas fee framework.
|
||||
// Tracking issues https://github.com/cosmos/cosmos-sdk/issues/9054, https://github.com/cosmos/cosmos-sdk/discussions/9072
|
||||
const gasCostPerIteration = uint64(10)
|
||||
|
||||
var (
|
||||
_ authz.Authorization = &StakeAuthorization{}
|
||||
TypeDelegate = "/cosmos.staking.v1beta1.Msg/Delegate"
|
||||
TypeUndelegate = "/cosmos.staking.v1beta1.Msg/Undelegate"
|
||||
TypeBeginRedelegate = "/cosmos.staking.v1beta1.Msg/BeginRedelegate"
|
||||
_ authz.Authorization = &StakeAuthorization{}
|
||||
|
||||
TypeDelegate = "/cosmos.staking.v1beta1.Msg/Delegate"
|
||||
TypeUndelegate = "/cosmos.staking.v1beta1.Msg/Undelegate"
|
||||
TypeBeginRedelegate = "/cosmos.staking.v1beta1.Msg/BeginRedelegate"
|
||||
)
|
||||
|
||||
// NewStakeAuthorization creates a new StakeAuthorization object.
|
||||
@@ -46,8 +49,19 @@ func (authorization StakeAuthorization) MethodName() string {
|
||||
return authzType
|
||||
}
|
||||
|
||||
func (authorization StakeAuthorization) ValidateBasic() error {
|
||||
if authorization.MaxTokens != nil && authorization.MaxTokens.IsNegative() {
|
||||
return sdkerrors.Wrapf(sdkerrors.ErrInvalidCoins, "negative coin amount: %v", authorization.MaxTokens)
|
||||
}
|
||||
if authorization.AuthorizationType == AuthorizationType_AUTHORIZATION_TYPE_UNSPECIFIED {
|
||||
return sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "unknown authorization type")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Accept implements Authorization.Accept.
|
||||
func (authorization StakeAuthorization) Accept(msg sdk.ServiceMsg, block tmproto.Header) (updated authz.Authorization, delete bool, err error) {
|
||||
func (authorization StakeAuthorization) Accept(ctx sdk.Context, msg sdk.ServiceMsg) (updated authz.Authorization, delete bool, err error) {
|
||||
var validatorAddress string
|
||||
var amount sdk.Coin
|
||||
|
||||
@@ -68,13 +82,16 @@ func (authorization StakeAuthorization) Accept(msg sdk.ServiceMsg, block tmproto
|
||||
isValidatorExists := false
|
||||
allowedList := authorization.GetAllowList().GetAddress()
|
||||
for _, validator := range allowedList {
|
||||
ctx.GasMeter().ConsumeGas(gasCostPerIteration, "stake authorization")
|
||||
if validator == validatorAddress {
|
||||
isValidatorExists = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
denyList := authorization.GetDenyList().GetAddress()
|
||||
for _, validator := range denyList {
|
||||
ctx.GasMeter().ConsumeGas(gasCostPerIteration, "stake authorization")
|
||||
if validator == validatorAddress {
|
||||
return nil, false, sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, " cannot delegate/undelegate to %s validator", validator)
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/simapp"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
|
||||
)
|
||||
@@ -21,13 +22,21 @@ var (
|
||||
)
|
||||
|
||||
func TestAuthzAuthorizations(t *testing.T) {
|
||||
app := simapp.Setup(false)
|
||||
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
|
||||
|
||||
// verify ValidateBasic returns error for the AUTHORIZATION_TYPE_UNSPECIFIED authorization type
|
||||
delAuth, err := stakingtypes.NewStakeAuthorization([]sdk.ValAddress{val1, val2}, []sdk.ValAddress{}, stakingtypes.AuthorizationType_AUTHORIZATION_TYPE_UNSPECIFIED, &coin100)
|
||||
require.NoError(t, err)
|
||||
require.Error(t, delAuth.ValidateBasic())
|
||||
|
||||
// verify MethodName
|
||||
delAuth, _ := stakingtypes.NewStakeAuthorization([]sdk.ValAddress{val1, val2}, []sdk.ValAddress{}, stakingtypes.AuthorizationType_AUTHORIZATION_TYPE_DELEGATE, &coin100)
|
||||
delAuth, err = stakingtypes.NewStakeAuthorization([]sdk.ValAddress{val1, val2}, []sdk.ValAddress{}, stakingtypes.AuthorizationType_AUTHORIZATION_TYPE_DELEGATE, &coin100)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, delAuth.MethodName(), stakingtypes.TypeDelegate)
|
||||
|
||||
// error both allow & deny list
|
||||
_, err := stakingtypes.NewStakeAuthorization([]sdk.ValAddress{val1, val2}, []sdk.ValAddress{val1}, stakingtypes.AuthorizationType_AUTHORIZATION_TYPE_DELEGATE, &coin100)
|
||||
_, err = stakingtypes.NewStakeAuthorization([]sdk.ValAddress{val1, val2}, []sdk.ValAddress{val1}, stakingtypes.AuthorizationType_AUTHORIZATION_TYPE_DELEGATE, &coin100)
|
||||
require.Error(t, err)
|
||||
|
||||
// verify MethodName
|
||||
@@ -243,7 +252,7 @@ func TestAuthzAuthorizations(t *testing.T) {
|
||||
t.Run(tc.msg, func(t *testing.T) {
|
||||
delAuth, err := stakingtypes.NewStakeAuthorization(tc.allowed, tc.denied, tc.msgType, tc.limit)
|
||||
require.NoError(t, err)
|
||||
updated, del, err := delAuth.Accept(tc.srvMsg, tmproto.Header{})
|
||||
updated, del, err := delAuth.Accept(ctx, tc.srvMsg)
|
||||
if tc.expectErr {
|
||||
require.Error(t, err)
|
||||
require.Equal(t, tc.isDelete, del)
|
||||
|
||||
Reference in New Issue
Block a user