Skip to content
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] revert ConvertFreshAccountToZeroNonceType func #4163

Merged
merged 4 commits into from
Mar 12, 2024

Conversation

Liuhaai
Copy link
Member

@Liuhaai Liuhaai commented Mar 5, 2024

Description

Please include a summary of the changes and the related issue. Please also include relevant motivation and context. List any dependencies that are required for this change.

Fixes #(issue)

Type of change

Please delete options that are not relevant.

  • [] Bug fix (non-breaking change which fixes an issue)
  • [] New feature (non-breaking change which adds functionality)
  • [] Code refactor or improvement
  • [] Breaking change (fix or feature that would cause a new or changed behavior of existing functionality)
  • [] This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • [] make test
  • [] fullsync
  • [] Other test (please specify)

Test Configuration:

  • Firmware version:
  • Hardware:
  • Toolchain:
  • SDK:

Checklist:

  • [] My code follows the style guidelines of this project
  • [] I have performed a self-review of my code
  • [] I have commented my code, particularly in hard-to-understand areas
  • [] I have made corresponding changes to the documentation
  • [] My changes generate no new warnings
  • [] I have added tests that prove my fix is effective or that my feature works
  • [] New and existing unit tests pass locally with my changes
  • [] Any dependent changes have been merged and published in downstream modules

Copy link

codecov bot commented Mar 5, 2024

Codecov Report

Attention: Patch coverage is 57.62712% with 25 lines in your changes are missing coverage. Please review.

Project coverage is 76.14%. Comparing base (e1f0636) to head (ba64dc8).
Report is 199 commits behind head on master.

Files Patch % Lines
api/coreservice.go 50.00% 12 Missing and 6 partials ⚠️
state/factory/workingset.go 44.44% 4 Missing and 1 partial ⚠️
action/protocol/execution/evm/evm.go 0.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4163      +/-   ##
==========================================
+ Coverage   75.38%   76.14%   +0.76%     
==========================================
  Files         303      340      +37     
  Lines       25923    29171    +3248     
==========================================
+ Hits        19541    22212    +2671     
- Misses       5360     5851     +491     
- Partials     1022     1108      +86     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

require.NoError(err)
require.Equal(uint64(1), acct.PendingNonce())
require.Error(acct.SetPendingNonce(0))
require.Error(acct.SetPendingNonce(3))
Copy link
Member

@dustinxie dustinxie Mar 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see above comment about SetPendingNonce(2)

// so we can convert it to zero-nonce account
if st.accountType == 0 && st.nonce == 0 && nonce == 1 {
st.accountType = 1
}
Copy link
Member

@dustinxie dustinxie Mar 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

calling SetPendingNonce(2) will skip this conversion, and remain legacy account.
Do we want to allow this behavior? i.e., the legacy account could convert to zero-nonce type (by sending first tx nonce=0), or remain legacy (by sending first tx nonce=1)

Copy link
Member Author

@Liuhaai Liuhaai Mar 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the account isn't a fresh account if its pending nonce is set to 2. And the nonce of a fresh account will be guaranteed by checkNonceContinuity()

Comment on lines +152 to +154
if st.accountType == 0 && st.nonce == 0 && nonce == 1 {
st.accountType = 1
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be a hard-fork?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pendingNonce = state.PendingNonceConsideringFreshAccount()
} else {
pendingNonce = state.PendingNonce()
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. consider the case, where the legacy address send a first tx with tx.Nonce=2
    here it returns pendingNonce = 0
  2. inside EVM code, it calls SetNonce(GetNonce()+1) so it calls SetNonce(1)
  3. then in our SetNonce(), it will hit
if !stateDB.useConfirmedNonce {
		nonce--
}

nonce=0, and finally calls SetPendingNonce(nonce+1) --> SetPendingNonce(1)
4. SetPendingNonce(1) will trigger the conversion, convert the address to zero-nonce type with s.nonce = 1
5. but the first tx of this address now has nonce=2

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For Step 1, the tx.nonce is 0 if state.PendingNonceConsideringFreshAccount() is triggered in the EVM. I might ignore the case it's triggered when tx.nonce is 2. Could you give more details about how to reproduce?
For Step 5, What do you mean the address has nonce=2?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the above steps illustrate a possible attack, someone just sends a tx with tx.Nonce=2, or a malicious delegate just run such a tx, when state.PendingNonceConsideringFreshAccount() is triggered in the EVM, it is GetNonce() = 0, it will pass SetNonce(1) and trigger the conversion
in other words, the first tx of such address will always trigger the conversion, regardless of the tx itself has Nonce=2 or even Nonce=5
and this will be caught by checkNonceContinuity, but it becomes a potential attack

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the problem has been settled in offline discussion.

pendingNonce = state.PendingNonceConsideringFreshAccount()
} else {
pendingNonce = state.PendingNonce()
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the problem has been settled in offline discussion.

@@ -117,6 +117,7 @@ type (
UseZeroNonceForFreshAccount bool
SharedGasWithDapp bool
DisableDelegateEndorsement bool
RefactorFreshAccountConversion bool
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default value (false) is the value after hard fork

Copy link
Member

@envestcc envestcc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so should use PendingNonceConsideringFreshAccount() to get pending nonce of any account instead of PendingNonce() from now on, right?

@Liuhaai
Copy link
Member Author

Liuhaai commented Mar 12, 2024

so should use PendingNonceConsideringFreshAccount() to get pending nonce of any account instead of PendingNonce() from now on, right?

PendingNonce() can be removed in another pr after the activated height since it's used in API now

Copy link

sonarcloud bot commented Mar 12, 2024

Quality Gate Failed Quality Gate failed

Failed conditions
11.4% Duplication on New Code (required ≤ 3%)

See analysis details on SonarCloud

@Liuhaai Liuhaai merged commit 6eb9c5f into iotexproject:master Mar 12, 2024
2 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants