Skip to content

Commit

Permalink
account type
Browse files Browse the repository at this point in the history
  • Loading branch information
CoderZhi committed May 23, 2022
1 parent f0b381d commit fb78e66
Show file tree
Hide file tree
Showing 23 changed files with 289 additions and 91 deletions.
104 changes: 86 additions & 18 deletions action/protocol/account/accountpb/account.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions action/protocol/account/accountpb/account.proto
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2018 IoTeX
// Copyright (c) 2022 IoTeX
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
Expand All @@ -8,6 +8,12 @@
// protoc --go_out=plugins=grpc:. *.proto
syntax = "proto3";
package accountpb;
option go_package = "github.com/iotexproject/iotex-core/action/protocol/account/accountpb";

enum AccountType {
DEFAULT = 0;
ZERO_NONCE = 1;
}

message Account {
// used by state-based model
Expand All @@ -17,5 +23,5 @@ message Account {
bytes codeHash = 4;
bool isCandidate = 5;
bytes votingWeight = 6;
AccountType type = 7;
}

12 changes: 10 additions & 2 deletions action/protocol/account/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (p *Protocol) Name() string {
return protocolID
}

func createAccount(sm protocol.StateManager, encodedAddr string, init *big.Int) error {
func createAccount(sm protocol.StateManager, encodedAddr string, init *big.Int, opts ...state.AccountCreationOption) error {
account := state.NewEmptyAccount()
addr, err := address.FromString(encodedAddr)
if err != nil {
Expand All @@ -119,6 +119,10 @@ func createAccount(sm protocol.StateManager, encodedAddr string, init *big.Int)
case nil:
return errors.Errorf("failed to create account %s", encodedAddr)
case state.ErrStateNotExist:
account, err := state.NewAccount(opts...)
if err != nil {
return err
}
if err := account.AddBalance(init); err != nil {
return errors.Wrapf(err, "failed to add balance %s", init)
}
Expand All @@ -144,8 +148,12 @@ func (p *Protocol) CreateGenesisStates(ctx context.Context, sm protocol.StateMan
if err := p.assertAmounts(amounts); err != nil {
return err
}
opts := []state.AccountCreationOption{}
if protocol.MustGetFeatureCtx(ctx).CreateZeroNonceAccount {
opts = append(opts, state.ZeroNonceAccountTypeOption())
}
for i, addr := range addrs {
if err := createAccount(sm, addr.String(), amounts[i]); err != nil {
if err := createAccount(sm, addr.String(), amounts[i], opts...); err != nil {
return err
}
}
Expand Down
2 changes: 1 addition & 1 deletion action/protocol/account/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func TestProtocol_Initialize(t *testing.T) {
ctx := protocol.WithBlockCtx(context.Background(), protocol.BlockCtx{
BlockHeight: 0,
})
ctx = genesis.WithGenesisContext(ctx, ge)
ctx = protocol.WithFeatureCtx(genesis.WithGenesisContext(ctx, ge))
p := NewProtocol(rewarding.DepositGas)
require.NoError(
p.CreateGenesisStates(
Expand Down
14 changes: 9 additions & 5 deletions action/protocol/account/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,17 @@ 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).CreateZeroNonceAccount {
accountCreationOpts = append(accountCreationOpts, state.ZeroNonceAccountTypeOption())
}
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())
}
Expand Down Expand Up @@ -74,12 +77,13 @@ 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 !fCtx.TolerateLegacyAddress {
if err != nil {
return nil, errors.Wrapf(err, "failed to load address %s", tsf.Recipient())
}
}
blkCtx := protocol.MustGetBlockCtx(ctx)
if err == nil && recipientAcct.IsContract() {
// update sender Nonce
if err := sender.SetNonce(tsf.Nonce()); err != nil {
Expand Down Expand Up @@ -121,7 +125,7 @@ func (p *Protocol) handleTransfer(ctx context.Context, act action.Action, sm pro
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())
}
Expand Down
2 changes: 2 additions & 0 deletions action/protocol/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ type (
TolerateLegacyAddress bool
EnableWeb3Staking bool
ValidateRewardProtocol bool
CreateZeroNonceAccount bool
SkipUpdateForSystemAction bool
}

Expand Down Expand Up @@ -235,6 +236,7 @@ func WithFeatureCtx(ctx context.Context) context.Context {
TolerateLegacyAddress: !g.IsNewfoundland(height),
EnableWeb3Staking: g.IsNewfoundland(height),
ValidateRewardProtocol: g.IsNewfoundland(height),
CreateZeroNonceAccount: g.IsToBeEnabled(height),
SkipUpdateForSystemAction: g.IsToBeEnabled(height),
},
)
Expand Down
3 changes: 3 additions & 0 deletions action/protocol/execution/evm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,9 @@ func prepareStateDB(ctx context.Context, sm protocol.StateManager) *StateDBAdapt
blkCtx := protocol.MustGetBlockCtx(ctx)
featureCtx := protocol.MustGetFeatureCtx(ctx)
opts := []StateDBAdapterOption{}
if featureCtx.CreateZeroNonceAccount {
opts = append(opts, ZeroNonceAccountOption())
}
if featureCtx.UsePendingNonceOption {
opts = append(opts, SortCachedContractsOption(), UsePendingNonceOption())
}
Expand Down
Loading

0 comments on commit fb78e66

Please sign in to comment.