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 type with zero init nonce #3387

Merged
merged 12 commits into from
Jul 2, 2022
Merged

account type with zero init nonce #3387

merged 12 commits into from
Jul 2, 2022

Conversation

CoderZhi
Copy link
Collaborator

@CoderZhi CoderZhi commented May 13, 2022

Description

Close other PRs per reviewers' request.
In this PR:

  1. Move IsSystemAction into action package as a static function
  2. Set balance via SubBalance and AddBalance
  3. Create account with NewAccount and NewEmptyAccount
  4. Replace state.Nonce with State.PendingNonce
  5. Skip update nonce and gas for system actions
  6. Check nonce value in SetNonce
  7. Introduce new account type

Fixes # (issue)

Type of change

Please delete options that are not relevant.

  • [] Breaking change (fix or feature that would cause a new or changed behavior of existing functionality)

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

  • [] Test A
  • [] Test B

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

@codecov
Copy link

codecov bot commented May 13, 2022

Codecov Report

Merging #3387 (fe5c397) into master (5ee6a4f) will increase coverage by 0.20%.
The diff coverage is 81.75%.

@@            Coverage Diff             @@
##           master    #3387      +/-   ##
==========================================
+ Coverage   75.21%   75.41%   +0.20%     
==========================================
  Files         244      244              
  Lines       22543    22675     +132     
==========================================
+ Hits        16956    17101     +145     
+ Misses       4664     4649      -15     
- Partials      923      925       +2     
Impacted Files Coverage Δ
action/const.go 100.00% <ø> (ø)
state/account.go 66.37% <63.76%> (-0.29%) ⬇️
action/protocol/execution/evm/evmstatedbadapter.go 65.61% <63.79%> (+0.05%) ⬆️
action/protocol/poll/util.go 68.18% <75.00%> (-0.05%) ⬇️
action/protocol/rewarding/protocol.go 79.41% <79.41%> (+2.28%) ⬆️
actpool/actpool.go 87.09% <95.65%> (+0.18%) ⬆️
action/protocol/account/protocol.go 86.90% <100.00%> (+0.65%) ⬆️
action/protocol/account/transfer.go 85.10% <100.00%> (+1.01%) ⬆️
action/protocol/context.go 67.47% <100.00%> (+30.28%) ⬆️
action/protocol/execution/evm/evm.go 45.09% <100.00%> (+1.30%) ⬆️
... and 12 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5ee6a4f...fe5c397. Read the comment docs.

@CoderZhi CoderZhi force-pushed the nonce branch 3 times, most recently from 146f7da to d63b9a1 Compare May 15, 2022 07:35
accountMeta := &iotextypes.AccountMeta{
Address: addrStr,
Balance: state.Balance.String(),
Nonce: state.Nonce,
PendingNonce: pendingNonce,
Nonce: nonce,
Copy link
Member

@dustinxie dustinxie May 18, 2022

Choose a reason for hiding this comment

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

why not use state.Nonce as before? so line 260 to 263 is not necessary?

state/account.go Outdated
// other actions' nonces start from 1
Nonce uint64
nonce uint64
Copy link
Member

Choose a reason for hiding this comment

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

i think it's OK to leave it as it, or is there a particular reason to make nonce unexported?

state/account.go Outdated
switch st.Type {
case 1:
return st.nonce
default: // 0
Copy link
Member

@dustinxie dustinxie May 18, 2022

Choose a reason for hiding this comment

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

maybe

case 0:
    return nonce + 1
default:
    panic("unknown account type")

nonce++
nonce = state.PendingNonce()
if !stateDB.usePendingNonce {
nonce--
Copy link
Member

Choose a reason for hiding this comment

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

is it possible a new account (type = 1) will go to here? then nonce would be 0-1

@@ -318,11 +342,14 @@ func (stateDB *StateDBAdapter) SetNonce(evmAddr common.Address, nonce uint64) {
log.L().Debug("Called SetNonce.",
zap.String("address", addr.String()),
zap.Uint64("nonce", nonce))
s.Nonce = nonce
if err := accountutil.StoreAccount(stateDB.sm, addr, s); err != nil {
if err := accountutil.SetNonce(s, nonce); err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

i don't recall, in the case stateDB.usePendingNonce == true, why in GetNonce() need to do nonce++ and in SetNonce() do nonce-- to bring it back (line 340 above)

does this logic still hold for the new account (type = 1)?

@CoderZhi CoderZhi marked this pull request as ready for review May 18, 2022 06:23
@CoderZhi CoderZhi requested a review from a team as a code owner May 18, 2022 06:23
@CoderZhi CoderZhi force-pushed the nonce branch 9 times, most recently from 6881b43 to fb78e66 Compare May 23, 2022 17:26
@CoderZhi CoderZhi force-pushed the nonce branch 3 times, most recently from 27fb160 to 1bc02b7 Compare June 10, 2022 07:05
@CoderZhi CoderZhi closed this Jun 13, 2022
@CoderZhi CoderZhi reopened this Jun 13, 2022

// create account
require.NoError(createAccount(sm, addrv1.String(), big.NewInt(5)))
s, err = accountutil.LoadAccount(sm, addrv1)
require.NoError(err)
require.Equal("5", s.Balance.String())
require.Equal(uint64(0x0), s.Nonce)
Copy link
Member

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

Copy link
Collaborator Author

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?

case state.ErrStateNotExist:
return state.NewAccount(opts...)
case nil:
return account, nil
Copy link
Member

Choose a reason for hiding this comment

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

nit: move case nil: to the first case, to be consistent with other places

switch errors.Cause(err) {
case nil:
fallthrough
case state.ErrStateNotExist:
Copy link
Member

Choose a reason for hiding this comment

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

nit: case nil, state.ErrStateNotExist is more straightforward

@dustinxie dustinxie linked an issue Jun 28, 2022 that may be closed by this pull request
func EmptyAccount() Account {
return Account{
// NewAccount creates a new account with options
func NewAccount(opts ...AccountCreationOption) (*Account, error) {
Copy link
Member

Choose a reason for hiding this comment

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

move to the head

if recipientAcct.IsContract() {
// update sender Nonce
accountutil.SetNonce(tsf, sender)
if fixNonce || tsf.Nonce() != 0 {
Copy link
Member

Choose a reason for hiding this comment

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

SetPendingNonce can be moved out of if condition?

}

// SetPendingNonce sets the pending nonce
func (st *Account) SetPendingNonce(nonce uint64) error {
Copy link
Member

Choose a reason for hiding this comment

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

IMO, we should keep using SetNonce(nonce), this way SetPendingNonce(nonce+1) feels more confusing

for _, addr := range stateDB.cachedContractAddrs() {
c := stateDB.cachedContract[addr]
if stateDB.disableSortCachedContracts {
for _, c := range stateDB.cachedContract {
Copy link
Member

@dustinxie dustinxie Jun 29, 2022

Choose a reason for hiding this comment

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

IMO it's not necessary to flip the flag? it increases the risk of making accidental error, and make it hard to review
if we really want to do it, better do it in a separate PR, since this is not related to zero-nonce

@@ -26,7 +26,7 @@
"rawGasLimit": 1000000,
"rawGasPrice": "0",
"rawAccessList": [{
"address": "06abb2ca03834bd4f62c610ca7627e3dd12d9a97",
"address": "675f1057F81e9e768e33faddbd5609C09F4c0a5C",
Copy link
Member

Choose a reason for hiding this comment

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

why would the address change?

@iotexproject iotexproject deleted a comment from CoderZhi Jun 29, 2022
@iotexproject iotexproject deleted a comment from CoderZhi Jun 29, 2022
accountMeta := &iotextypes.AccountMeta{
Address: addrStr,
Balance: state.Balance.String(),
Nonce: state.Nonce,
Copy link
Member

Choose a reason for hiding this comment

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

for safety better do this in another PR as TODO mentioned, my worry is that this might breaking the API behavior, some user may be using meta.Nonce from returned value and this will break their code?

return st.nonce
default: // 0
return st.nonce + 1
}
Copy link
Member

Choose a reason for hiding this comment

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

same as in SetNonce:

case 0:
    return st.nonce + 1
default:
    return ErrUnknownAccountType

@CoderZhi CoderZhi merged commit 7382ab7 into master Jul 2, 2022
@CoderZhi CoderZhi deleted the nonce branch July 2, 2022 05:08
pocockn added a commit to pocockn/iotex-core that referenced this pull request Jul 4, 2022
* upstream/master: (24 commits)
  account type with zero init nonce (iotexproject#3387)
  [api] Separate Server and Server Handler (iotexproject#3485)
  [ioctl] Build hdwallet derive command line into new ioctl (iotexproject#3418)
  [ioctl] Build hdwallet create command line into new ioctl (iotexproject#3470)
  [makefile] add go mod tidy (iotexproject#3471)
  [api] update chain metrics (iotexproject#3484)
  remove config.EVMNetworkID() (iotexproject#3460)
  [filedao] remove checkMasterChainDBFile() (iotexproject#3463)
  [api] add crashlog (iotexproject#3456)
  [api] Move generateBlockMeta to grpcserver.go (iotexproject#3303)
  [ioctl] Build action hash command line into new ioctl (iotexproject#3425)
  [ioctl] Build hdwallet export command line into new ioctl (iotexproject#3423)
  [ioctl] Refactor nodereward command in new ioctl (iotexproject#3416)
  [ioctl] Cleanup TestNewNodeDelegateCmd (iotexproject#3421)
  [blockchain] Remove BoltDBDaoOption (iotexproject#3465)
  remove InMemDaoOption (iotexproject#3464)
  [action] add evm london test (iotexproject#3402)
  [ioctl] create main for ioctl/newcmd (iotexproject#3296)
  [ioctl] Build block bucket command line into new ioctl (iotexproject#3386)
  [ioctl] Build hdwallet import command line into new ioctl (iotexproject#3419)
  ...
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.

nonce-0 account
3 participants