Skip to content

Commit

Permalink
[account] let LoadOrCreateAccount() return new account created flag
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinxie committed Apr 10, 2024
1 parent 1791f82 commit ca514db
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 16 deletions.
4 changes: 2 additions & 2 deletions action/protocol/account/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (p *Protocol) handleTransfer(ctx context.Context, tsf *action.Transfer, sm
accountCreationOpts = append(accountCreationOpts, state.LegacyNonceAccountTypeOption())
}
// check sender
sender, err := accountutil.LoadOrCreateAccount(sm, actionCtx.Caller, accountCreationOpts...)
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 @@ -119,7 +119,7 @@ func (p *Protocol) handleTransfer(ctx context.Context, tsf *action.Transfer, sm
}

// check recipient
recipient, err := accountutil.LoadOrCreateAccount(sm, recipientAddr, accountCreationOpts...)
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
12 changes: 6 additions & 6 deletions action/protocol/account/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,26 @@ import (
)

// LoadOrCreateAccount either loads an account state or creates an account state
func LoadOrCreateAccount(sm protocol.StateManager, addr address.Address, opts ...state.AccountCreationOption) (*state.Account, error) {
func LoadOrCreateAccount(sm protocol.StateManager, addr address.Address, opts ...state.AccountCreationOption) (*state.Account, bool, error) {
var (
account = &state.Account{}
addrHash = hash.BytesToHash160(addr.Bytes())
)
_, err := sm.State(account, protocol.LegacyKeyOption(addrHash))
switch errors.Cause(err) {
case nil:
return account, nil
return account, false, nil
case state.ErrStateNotExist:
account, err := state.NewAccount(opts...)
if err != nil {
return nil, errors.Wrapf(err, "failed to create state account for %x", addrHash)
return nil, false, errors.Wrapf(err, "failed to create state account for %x", addrHash)
}
if _, err := sm.PutState(account, protocol.LegacyKeyOption(addrHash)); err != nil {
return nil, errors.Wrapf(err, "failed to put state for account %x", addrHash)
return nil, false, errors.Wrapf(err, "failed to put state for account %x", addrHash)
}
return account, nil
return account, true, nil
default:
return nil, err
return nil, false, err
}
}

Expand Down
6 changes: 4 additions & 2 deletions action/protocol/execution/evm/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ func TestCreateContract(t *testing.T) {
}).AnyTimes()

addr := identityset.Address(28)
_, err = accountutil.LoadOrCreateAccount(sm, addr)
_, created, err := accountutil.LoadOrCreateAccount(sm, addr)
require.NoError(err)
require.True(created)
stateDB, err := NewStateDBAdapter(sm, 0, hash.ZeroHash256, NotFixTopicCopyBugOption())
require.NoError(err)

Expand All @@ -86,8 +87,9 @@ func TestCreateContract(t *testing.T) {
require.NoError(stateDB.CommitContracts())
stateDB.clear()
// reload same contract
contract1, err := accountutil.LoadOrCreateAccount(sm, addr)
contract1, created, err := accountutil.LoadOrCreateAccount(sm, addr)
require.NoError(err)
require.False(created)
require.Equal(codeHash[:], contract1.CodeHash)
}

Expand Down
4 changes: 2 additions & 2 deletions action/protocol/execution/evm/evmstatedbadapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func (stateDB *StateDBAdapter) CreateAccount(evmAddr common.Address) {
if stateDB.assertError(err, "Failed to convert evm address.", zap.Error(err)) {
return
}
_, err = accountutil.LoadOrCreateAccount(stateDB.sm, addr, stateDB.accountCreationOpts()...)
_, _, err = accountutil.LoadOrCreateAccount(stateDB.sm, addr, stateDB.accountCreationOpts()...)
if stateDB.assertError(err, "Failed to create account.", zap.Error(err), zap.String("address", evmAddr.Hex())) {
return
}
Expand Down Expand Up @@ -299,7 +299,7 @@ func (stateDB *StateDBAdapter) AddBalance(evmAddr common.Address, a256 *uint256.
if contract, ok := stateDB.cachedContract[addrHash]; ok {
state = contract.SelfState()
} else {
state, err = accountutil.LoadOrCreateAccount(stateDB.sm, addr, stateDB.accountCreationOpts()...)
state, _, err = accountutil.LoadOrCreateAccount(stateDB.sm, addr, stateDB.accountCreationOpts()...)
if stateDB.assertError(err, "Failed to get account.", zap.Error(err), zap.String("address", evmAddr.Hex())) {
return
}
Expand Down
2 changes: 1 addition & 1 deletion action/protocol/poll/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func setCandidates(
if err != nil {
return errors.Wrapf(err, "failed to decode delegate address %s", candidate.Address)
}
delegate, err := accountutil.LoadOrCreateAccount(sm, addr, accountCreationOpts...)
delegate, _, err := accountutil.LoadOrCreateAccount(sm, addr, accountCreationOpts...)
if err != nil {
return errors.Wrapf(err, "failed to load or create the account for delegate %s", candidate.Address)
}
Expand Down
2 changes: 1 addition & 1 deletion action/protocol/rewarding/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ func (p *Protocol) increaseNonce(ctx context.Context, sm protocol.StateManager,
if protocol.MustGetFeatureCtx(ctx).CreateLegacyNonceAccount {
accountCreationOpts = append(accountCreationOpts, state.LegacyNonceAccountTypeOption())
}
acc, err := accountutil.LoadOrCreateAccount(sm, addr, accountCreationOpts...)
acc, _, err := accountutil.LoadOrCreateAccount(sm, addr, accountCreationOpts...)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion action/protocol/rewarding/reward.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ func (p *Protocol) claimFromAccount(ctx context.Context, sm protocol.StateManage
accountCreationOpts = append(accountCreationOpts, state.LegacyNonceAccountTypeOption())
}
// Update primary account
primAcc, err := accountutil.LoadOrCreateAccount(sm, addr, accountCreationOpts...)
primAcc, _, err := accountutil.LoadOrCreateAccount(sm, addr, accountCreationOpts...)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion action/protocol/staking/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3239,7 +3239,7 @@ func setupAccount(sm protocol.StateManager, addr address.Address, balance int64)
if balance < 0 {
return errors.New("balance cannot be negative")
}
account, err := accountutil.LoadOrCreateAccount(sm, addr, state.LegacyNonceAccountTypeOption())
account, _, err := accountutil.LoadOrCreateAccount(sm, addr, state.LegacyNonceAccountTypeOption())
if err != nil {
return err
}
Expand Down

0 comments on commit ca514db

Please sign in to comment.