From fccf796bd6fc16dab7493533074608e7bb59e9d6 Mon Sep 17 00:00:00 2001 From: dustinxie Date: Mon, 20 Jan 2025 23:42:19 -0800 Subject: [PATCH] [protocol] validate tx with state always (#4541) --- action/protocol/context.go | 4 --- action/protocol/generic_validator.go | 23 +++++++------- state/factory/workingset.go | 46 +++++----------------------- 3 files changed, 18 insertions(+), 55 deletions(-) diff --git a/action/protocol/context.go b/action/protocol/context.go index 1c4338f411..1e5f2073ac 100644 --- a/action/protocol/context.go +++ b/action/protocol/context.go @@ -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 } @@ -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), }, diff --git a/action/protocol/generic_validator.go b/action/protocol/generic_validator.go index dea2021f5d..8076f29579 100644 --- a/action/protocol/generic_validator.go +++ b/action/protocol/generic_validator.go @@ -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 { diff --git a/state/factory/workingset.go b/state/factory/workingset.go index 3dbbbcc0ba..9c9b3f6352 100644 --- a/state/factory/workingset.go +++ b/state/factory/workingset.go @@ -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 @@ -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 { @@ -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 {