Skip to content

Commit

Permalink
[protocol] validate tx with state always (#4541)
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinxie authored Jan 21, 2025
1 parent b0ed281 commit fccf796
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 55 deletions.
4 changes: 0 additions & 4 deletions action/protocol/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,10 @@ type (
EnforceLegacyEndorsement bool
EnableDynamicFeeTx bool
EnableBlobTransaction bool
SufficentBalanceGuarantee bool
EnableCancunEVM bool
UnfoldContainerBeforeValidate bool
UnstakedButNotClearSelfStakeAmount bool
VerifyNotContainerBeforeRun bool
ValidateActionWithState bool
CheckStakingDurationUpperLimit bool
FixRevertSnapshot bool
}
Expand Down Expand Up @@ -303,12 +301,10 @@ func WithFeatureCtx(ctx context.Context) context.Context {
EnforceLegacyEndorsement: !g.IsUpernavik(height),
EnableDynamicFeeTx: g.IsVanuatu(height),
EnableBlobTransaction: g.IsVanuatu(height),
SufficentBalanceGuarantee: g.IsVanuatu(height),
EnableCancunEVM: g.IsVanuatu(height),
UnfoldContainerBeforeValidate: g.IsVanuatu(height),
UnstakedButNotClearSelfStakeAmount: !g.IsVanuatu(height),
VerifyNotContainerBeforeRun: g.IsVanuatu(height),
ValidateActionWithState: g.IsVanuatu(height),
CheckStakingDurationUpperLimit: g.IsVanuatu(height),
FixRevertSnapshot: g.IsVanuatu(height),
},
Expand Down
23 changes: 11 additions & 12 deletions action/protocol/generic_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,17 @@ func (v *GenericValidator) ValidateWithState(ctx context.Context, selp *action.S
return action.ErrNonceTooLow
}
}
if featureCtx.SufficentBalanceGuarantee {
acc, err := v.accountState(ctx, v.sr, caller)
if err != nil {
return errors.Wrapf(err, "invalid state of account %s", caller.String())
}
cost, err := selp.Cost()
if err != nil {
return errors.Wrap(err, "failed to get cost of action")
}
if acc.Balance.Cmp(cost) < 0 {
return errors.Wrapf(state.ErrNotEnoughBalance, "sender %s balance %s, cost %s", caller.String(), acc.Balance, cost)
}
// check whether the account has enough balance
acc, err := v.accountState(ctx, v.sr, caller)
if err != nil {
return errors.Wrapf(err, "invalid state of account %s", caller.String())
}
cost, err := selp.Cost()
if err != nil {
return errors.Wrap(err, "failed to get cost of action")
}
if acc.Balance.Cmp(cost) < 0 {
return errors.Wrapf(state.ErrNotEnoughBalance, "sender %s balance %s, cost %s", caller.String(), acc.Balance, cost)
}
blkCtx := MustGetBlockCtx(ctx)
if featureCtx.EnableDynamicFeeTx {
Expand Down
46 changes: 7 additions & 39 deletions state/factory/workingset.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,34 +114,6 @@ func (ws *workingSet) validate(ctx context.Context) error {
return nil
}

func (ws *workingSet) runActions(
ctx context.Context,
elps []*action.SealedEnvelope,
) ([]*action.Receipt, error) {
// Handle actions
receipts := make([]*action.Receipt, 0)
blkCtx := protocol.MustGetBlockCtx(ctx)
fCtx := protocol.MustGetFeatureCtx(ctx)
for _, elp := range elps {
ctxWithActionContext, err := withActionCtx(ctx, elp)
if err != nil {
return nil, err
}
receipt, err := ws.runAction(protocol.WithBlockCtx(ctxWithActionContext, blkCtx), elp)
if err != nil {
return nil, errors.Wrap(err, "error when run action")
}
receipts = append(receipts, receipt)
if fCtx.EnableDynamicFeeTx && receipt.PriorityFee() != nil {
(&blkCtx.AccumulatedTips).Add(&blkCtx.AccumulatedTips, receipt.PriorityFee())
}
}
if fCtx.CorrectTxLogIndex {
updateReceiptIndex(receipts)
}
return receipts, nil
}

func withActionCtx(ctx context.Context, selp *action.SealedEnvelope) (context.Context, error) {
var actionCtx protocol.ActionCtx
var err error
Expand Down Expand Up @@ -526,10 +498,8 @@ func (ws *workingSet) processWithCorrectOrder(ctx context.Context, actions []*ac
fCtx = protocol.MustGetFeatureCtx(ctx)
)
for _, act := range actions {
if fCtx.ValidateActionWithState {
if err := ws.txValidator.ValidateWithState(ctxWithBlockContext, act); err != nil {
return err
}
if err := ws.txValidator.ValidateWithState(ctxWithBlockContext, act); err != nil {
return err
}
actionCtx, err := withActionCtx(ctxWithBlockContext, act)
if err != nil {
Expand Down Expand Up @@ -670,13 +640,11 @@ func (ws *workingSet) pickAndRunActions(
}
}
}
if fCtx.ValidateActionWithState {
if err := ws.txValidator.ValidateWithState(ctxWithBlockContext, nextAction); err != nil {
log.L().Debug("failed to ValidateWithState", zap.Uint64("height", ws.height), zap.Error(err))
ap.DeleteAction(nextAction.SenderAddress())
actionIterator.PopAccount()
continue
}
if err := ws.txValidator.ValidateWithState(ctxWithBlockContext, nextAction); err != nil {
log.L().Debug("failed to ValidateWithState", zap.Uint64("height", ws.height), zap.Error(err))
ap.DeleteAction(nextAction.SenderAddress())
actionIterator.PopAccount()
continue
}
actionCtx, err := withActionCtx(ctxWithBlockContext, nextAction)
if err == nil {
Expand Down

0 comments on commit fccf796

Please sign in to comment.