Skip to content

Commit 140f52d

Browse files
authored
[staking] early resolve invalid staking or conflict error (iotexproject#2146)
1 parent 4e62e06 commit 140f52d

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

action/protocol/staking/handlers.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -581,8 +581,7 @@ func (p *Protocol) handleCandidateUpdate(ctx context.Context, act *action.Candid
581581
}
582582

583583
if err := csm.Upsert(c); err != nil {
584-
log.L().Debug("failed to put state of candidate", zap.Error(err))
585-
return p.settleAction(ctx, csm, uint64(iotextypes.ReceiptStatus_ErrCandidateConflict), gasFee)
584+
return nil, errors.Wrapf(err, "failed to put state of candidate %s", c.Owner.String())
586585
}
587586

588587
log := p.createLog(ctx, HandleCandidateUpdate, nil, actCtx.Caller, nil)

action/protocol/staking/protocol.go

+27-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"time"
1313

1414
"github.com/golang/protobuf/proto"
15+
"github.com/iotexproject/iotex-proto/golang/iotextypes"
1516
"github.com/pkg/errors"
1617

1718
"github.com/iotexproject/go-pkgs/hash"
@@ -211,7 +212,16 @@ func (p *Protocol) Handle(ctx context.Context, act action.Action, sm protocol.St
211212
if err != nil {
212213
return nil, err
213214
}
214-
return p.handle(ctx, act, csm)
215+
216+
r, err := p.handle(ctx, act, csm)
217+
if err != nil {
218+
if status, ok := canEarlyResolve(err); ok {
219+
actionCtx := protocol.MustGetActionCtx(ctx)
220+
gasFee := big.NewInt(0).Mul(actionCtx.GasPrice, big.NewInt(0).SetUint64(actionCtx.IntrinsicGas))
221+
return p.settleAction(ctx, csm, status, gasFee)
222+
}
223+
}
224+
return r, err
215225
}
216226

217227
func (p *Protocol) handle(ctx context.Context, act action.Action, csm CandidateStateManager) (*action.Receipt, error) {
@@ -342,3 +352,19 @@ func (p *Protocol) Name() string {
342352
func (p *Protocol) calculateVoteWeight(v *VoteBucket, selfStake bool) *big.Int {
343353
return calculateVoteWeight(p.config.VoteWeightCalConsts, v, selfStake)
344354
}
355+
356+
func canEarlyResolve(err error) (uint64, bool) {
357+
cause := errors.Cause(err)
358+
if cause == ErrInvalidCanName ||
359+
cause == ErrInvalidOperator ||
360+
cause == ErrInvalidSelfStkIndex {
361+
return uint64(iotextypes.ReceiptStatus_ErrCandidateConflict), true
362+
}
363+
364+
if cause == ErrInvalidAmount ||
365+
cause == ErrInvalidOwner ||
366+
cause == ErrInvalidReward {
367+
return uint64(iotextypes.ReceiptStatus_ErrCandidateNotExist), true
368+
}
369+
return 0, false
370+
}

0 commit comments

Comments
 (0)