Skip to content

Commit

Permalink
[staking] check duration upper limit (#4467)
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinxie authored Oct 31, 2024
1 parent 2d2523c commit 5e40508
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
2 changes: 2 additions & 0 deletions action/protocol/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ type (
EnableNewTxTypes bool
VerifyNotContainerBeforeRun bool
ValidateActionWithState bool
CheckStakingDurationUpperLimit bool
}

// FeatureWithHeightCtx provides feature check functions.
Expand Down Expand Up @@ -311,6 +312,7 @@ func WithFeatureCtx(ctx context.Context) context.Context {
EnableNewTxTypes: g.IsVanuatu(height),
VerifyNotContainerBeforeRun: g.IsVanuatu(height),
ValidateActionWithState: g.IsVanuatu(height),
CheckStakingDurationUpperLimit: g.IsVanuatu(height),
},
)
}
Expand Down
23 changes: 19 additions & 4 deletions action/protocol/staking/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ func TestProtocol_HandleCreateStake(t *testing.T) {
require.NoError(csm.putCandidate(candidate))
candidateName := candidate.Name
candidateAddr := candidate.Owner
ctx := genesis.WithGenesisContext(context.Background(), genesis.Default)
g := genesis.Default
g.VanuatuBlockHeight = 1
ctx := genesis.WithGenesisContext(context.Background(), g)
ctx = protocol.WithFeatureWithHeightCtx(ctx)
v, err := p.Start(ctx, sm)
require.NoError(err)
Expand Down Expand Up @@ -174,6 +176,20 @@ func TestProtocol_HandleCreateStake(t *testing.T) {
action.ErrInvalidAmount,
iotextypes.ReceiptStatus_Failure,
},
{
101,
candidateName,
"100000000000000000000",
1051,
false,
10000,
2,
1,
time.Now(),
10000,
ErrDurationTooHigh,
iotextypes.ReceiptStatus_Failure,
},
{
101,
candidateName,
Expand Down Expand Up @@ -203,9 +219,9 @@ func TestProtocol_HandleCreateStake(t *testing.T) {
BlockTimeStamp: test.blkTimestamp,
GasLimit: test.blkGasLimit,
})
ctx = protocol.WithBlockchainCtx(ctx, protocol.BlockchainCtx{Tip: protocol.TipInfo{
ctx = protocol.WithFeatureCtx(protocol.WithBlockchainCtx(ctx, protocol.BlockchainCtx{Tip: protocol.TipInfo{
Height: test.blkHeight - 1,
}})
}}))
act, err := action.NewCreateStake(test.candName, test.amount, test.duration, test.autoStake, nil)
require.NoError(err)
elp := builder.SetNonce(test.nonce).SetGasLimit(test.gasLimit).
Expand All @@ -215,7 +231,6 @@ func TestProtocol_HandleCreateStake(t *testing.T) {
require.EqualError(test.err, errors.Cause(err).Error())
continue
}
ctx = protocol.WithFeatureCtx(ctx)
r, err := p.Handle(ctx, elp, sm)
require.NoError(err)
require.Equal(uint64(test.status), r.Status)
Expand Down
14 changes: 14 additions & 0 deletions action/protocol/staking/validations.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import (
"github.com/iotexproject/iotex-core/v2/action/protocol"
)

const (
_stakeDurationLimit = 1050
)

// Errors
var (
ErrInvalidOwner = errors.New("invalid owner address")
Expand All @@ -22,6 +26,7 @@ var (
ErrInvalidSelfStkIndex = errors.New("invalid self-staking bucket index")
ErrMissingField = errors.New("missing data field")
ErrTypeAssertion = errors.New("failed type assertion")
ErrDurationTooHigh = errors.New("stake duration cannot exceed 1050 days")
)

func (p *Protocol) validateCreateStake(ctx context.Context, act *action.CreateStake) error {
Expand All @@ -31,6 +36,9 @@ func (p *Protocol) validateCreateStake(ctx context.Context, act *action.CreateSt
if act.Amount().Cmp(p.config.MinStakeAmount) == -1 {
return errors.Wrap(action.ErrInvalidAmount, "stake amount is less than the minimum requirement")
}
if protocol.MustGetFeatureCtx(ctx).CheckStakingDurationUpperLimit && act.Duration() > _stakeDurationLimit {
return ErrDurationTooHigh
}
return nil
}

Expand Down Expand Up @@ -58,6 +66,9 @@ func (p *Protocol) validateDepositToStake(ctx context.Context, act *action.Depos
}

func (p *Protocol) validateRestake(ctx context.Context, act *action.Restake) error {
if protocol.MustGetFeatureCtx(ctx).CheckStakingDurationUpperLimit && act.Duration() > _stakeDurationLimit {
return ErrDurationTooHigh
}
return nil
}

Expand All @@ -73,6 +84,9 @@ func (p *Protocol) validateCandidateRegister(ctx context.Context, act *action.Ca
}
return errors.Wrap(action.ErrInvalidAmount, "self staking amount is not valid")
}
if protocol.MustGetFeatureCtx(ctx).CheckStakingDurationUpperLimit && act.Duration() > _stakeDurationLimit {
return ErrDurationTooHigh
}
return nil
}

Expand Down

0 comments on commit 5e40508

Please sign in to comment.