From a969b74c201ef9e707124b858e7e20d05971573e Mon Sep 17 00:00:00 2001 From: Haaai <55118568+Liuhaai@users.noreply.github.com> Date: Tue, 19 Jul 2022 12:21:30 -0700 Subject: [PATCH] [action] Refactor handleTransfer() (#3557) --- action/protocol/account/transfer.go | 36 +++++++++++++---------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/action/protocol/account/transfer.go b/action/protocol/account/transfer.go index 929088c83d..8c267e8e79 100644 --- a/action/protocol/account/transfer.go +++ b/action/protocol/account/transfer.go @@ -29,11 +29,15 @@ func (p *Protocol) handleTransfer(ctx context.Context, act action.Action, sm pro if !ok { return nil, nil } + var ( + fCtx = protocol.MustGetFeatureCtx(ctx) + actionCtx = protocol.MustGetActionCtx(ctx) + blkCtx = protocol.MustGetBlockCtx(ctx) + ) accountCreationOpts := []state.AccountCreationOption{} - if protocol.MustGetFeatureCtx(ctx).CreateLegacyNonceAccount { + if fCtx.CreateLegacyNonceAccount { accountCreationOpts = append(accountCreationOpts, state.LegacyNonceAccountTypeOption()) } - actionCtx := protocol.MustGetActionCtx(ctx) // check sender sender, err := accountutil.LoadOrCreateAccount(sm, actionCtx.Caller, accountCreationOpts...) if err != nil { @@ -51,10 +55,7 @@ func (p *Protocol) handleTransfer(ctx context.Context, act action.Action, sm pro ) } - var ( - depositLog *action.TransactionLog - fCtx = protocol.MustGetFeatureCtx(ctx) - ) + var depositLog *action.TransactionLog if !fCtx.FixDoubleChargeGas { // charge sender gas if err := sender.SubBalance(gasFee); err != nil { @@ -68,6 +69,13 @@ func (p *Protocol) handleTransfer(ctx context.Context, act action.Action, sm pro } } + if fCtx.FixGasAndNonceUpdate || tsf.Nonce() != 0 { + // update sender Nonce + if err := sender.SetPendingNonce(tsf.Nonce() + 1); err != nil { + return nil, errors.Wrapf(err, "failed to update pending nonce of sender %s", actionCtx.Caller.String()) + } + } + var recipientAddr address.Address if fCtx.TolerateLegacyAddress { recipientAddr, err = address.FromStringLegacy(tsf.Recipient()) @@ -81,15 +89,7 @@ func (p *Protocol) handleTransfer(ctx context.Context, act action.Action, sm pro if err != nil { return nil, errors.Wrapf(err, "failed to load address %s", tsf.Recipient()) } - fixNonce := protocol.MustGetFeatureCtx(ctx).FixGasAndNonceUpdate - blkCtx := protocol.MustGetBlockCtx(ctx) if recipientAcct.IsContract() { - if fixNonce || tsf.Nonce() != 0 { - // update sender Nonce - if err := sender.SetPendingNonce(tsf.Nonce() + 1); err != nil { - return nil, errors.Wrapf(err, "failed to update pending nonce of sender %s", actionCtx.Caller.String()) - } - } // put updated sender's state to trie if err := accountutil.StoreAccount(sm, actionCtx.Caller, sender); err != nil { return nil, errors.Wrap(err, "failed to update pending account changes to trie") @@ -117,16 +117,12 @@ func (p *Protocol) handleTransfer(ctx context.Context, act action.Action, sm pro if err := sender.SubBalance(tsf.Amount()); err != nil { return nil, errors.Wrapf(err, "failed to update the Balance of sender %s", actionCtx.Caller.String()) } - if fixNonce || tsf.Nonce() != 0 { - // update sender Nonce - if err := sender.SetPendingNonce(tsf.Nonce() + 1); err != nil { - return nil, errors.Wrapf(err, "failed to update pending nonce of sender %s", actionCtx.Caller.String()) - } - } + // put updated sender's state to trie if err := accountutil.StoreAccount(sm, actionCtx.Caller, sender); err != nil { return nil, errors.Wrap(err, "failed to update pending account changes to trie") } + // check recipient recipient, err := accountutil.LoadOrCreateAccount(sm, recipientAddr, accountCreationOpts...) if err != nil {