Skip to content

Commit

Permalink
Merge branch 'master' into fix_gosec_recovery
Browse files Browse the repository at this point in the history
  • Loading branch information
Liuhaai committed Jul 19, 2022
2 parents 746ae35 + a969b74 commit fd151a7
Showing 1 changed file with 16 additions and 20 deletions.
36 changes: 16 additions & 20 deletions action/protocol/account/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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())
Expand All @@ -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")
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit fd151a7

Please sign in to comment.