-
Notifications
You must be signed in to change notification settings - Fork 324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
account type with zero init nonce #3387
Changes from all commits
99f3ffb
8598056
478d264
b397bcb
878c583
2bae970
524115e
79aeaee
59509d1
877cf1a
50c36f9
fe5c397
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,20 +25,23 @@ const TransferSizeLimit = 32 * 1024 | |
|
||
// handleTransfer handles a transfer | ||
func (p *Protocol) handleTransfer(ctx context.Context, act action.Action, sm protocol.StateManager) (*action.Receipt, error) { | ||
actionCtx := protocol.MustGetActionCtx(ctx) | ||
blkCtx := protocol.MustGetBlockCtx(ctx) | ||
tsf, ok := act.(*action.Transfer) | ||
if !ok { | ||
return nil, nil | ||
} | ||
accountCreationOpts := []state.AccountCreationOption{} | ||
if protocol.MustGetFeatureCtx(ctx).CreateLegacyNonceAccount { | ||
accountCreationOpts = append(accountCreationOpts, state.LegacyNonceAccountTypeOption()) | ||
} | ||
actionCtx := protocol.MustGetActionCtx(ctx) | ||
// check sender | ||
sender, err := accountutil.LoadOrCreateAccount(sm, actionCtx.Caller) | ||
sender, err := accountutil.LoadOrCreateAccount(sm, actionCtx.Caller, accountCreationOpts...) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "failed to load or create the account of sender %s", actionCtx.Caller.String()) | ||
} | ||
|
||
gasFee := big.NewInt(0).Mul(tsf.GasPrice(), big.NewInt(0).SetUint64(actionCtx.IntrinsicGas)) | ||
if big.NewInt(0).Add(tsf.Amount(), gasFee).Cmp(sender.Balance) == 1 { | ||
if !sender.HasSufficientBalance(big.NewInt(0).Add(tsf.Amount(), gasFee)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the original exp is clear enough |
||
return nil, errors.Wrapf( | ||
state.ErrNotEnoughBalance, | ||
"sender %s balance %s, required amount %s", | ||
|
@@ -74,13 +77,19 @@ func (p *Protocol) handleTransfer(ctx context.Context, act action.Action, sm pro | |
if err != nil { | ||
return nil, errors.Wrapf(err, "failed to decode recipient address %s", tsf.Recipient()) | ||
} | ||
recipientAcct, err := accountutil.LoadAccount(sm, recipientAddr) | ||
recipientAcct, err := accountutil.LoadAccount(sm, recipientAddr, accountCreationOpts...) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "failed to load address %s", tsf.Recipient()) | ||
} | ||
fixNonce := protocol.MustGetFeatureCtx(ctx).FixGasAndNonceUpdate | ||
blkCtx := protocol.MustGetBlockCtx(ctx) | ||
Comment on lines
+84
to
+85
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the names There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does it belong to pr There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we only have one PR for now. |
||
if recipientAcct.IsContract() { | ||
// update sender Nonce | ||
accountutil.SetNonce(tsf, sender) | ||
if fixNonce || tsf.Nonce() != 0 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
// 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") | ||
|
@@ -108,18 +117,24 @@ 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()) | ||
} | ||
// update sender Nonce | ||
accountutil.SetNonce(tsf, sender) | ||
if fixNonce || tsf.Nonce() != 0 { | ||
// update sender Nonce | ||
if err := sender.SetPendingNonce(tsf.Nonce() + 1); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how could |
||
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) | ||
recipient, err := accountutil.LoadOrCreateAccount(sm, recipientAddr, accountCreationOpts...) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "failed to load or create the account of recipient %s", tsf.Recipient()) | ||
} | ||
recipient.AddBalance(tsf.Amount()) | ||
if err := recipient.AddBalance(tsf.Amount()); err != nil { | ||
return nil, errors.Wrapf(err, "failed to add balance %s", tsf.Amount()) | ||
} | ||
// put updated recipient's state to trie | ||
if err := accountutil.StoreAccount(sm, recipientAddr, recipient); err != nil { | ||
return nil, errors.Wrap(err, "failed to update pending account changes to trie") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add test to create new type account, assert
s.PendingNonce() == 0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you sure to add unit test to this PR?