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 2, 2024
1 parent 6c5ecae commit a36e72f
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 17 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ build-readtip:
.PHONY: fmt
fmt:
$(GOCMD) fmt ./...
$(GOCMD) mod tidy

.PHONY: lint
lint:
Expand Down
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
13 changes: 13 additions & 0 deletions action/protocol/execution/evm/evm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package evm

import (
"context"
"encoding/base64"
"errors"
"math/big"
"testing"
Expand All @@ -16,6 +17,7 @@ import (
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"

"github.com/iotexproject/go-pkgs/crypto"
"github.com/iotexproject/go-pkgs/hash"
"github.com/iotexproject/iotex-address/address"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
Expand Down Expand Up @@ -467,3 +469,14 @@ func gasExecuteInEVM(gas, consume, refund, size uint64) (uint64, uint64, error)
remainingGas -= consume
return remainingGas, remainingGas + refund, nil
}

func TestFk(t *testing.T) {
r := require.New(t)
sk, err := base64.StdEncoding.DecodeString("MzUyMDAyNGNjNDcyZjUxNGYwOTM2N2FhNTAzMjBiMjRmZmRmODI2MmM2YjYxYTNkZDZiMTQ3OGJmNDE2MWRjYQ==")
r.NoError(err)
r.Equal(64, len(sk))
println("sk =", string(sk))
key, err := crypto.HexStringToPrivateKey(string(sk))
r.NoError(err)
println("addr =", key.PublicKey().Address().String())
}
4 changes: 2 additions & 2 deletions action/protocol/execution/evm/evmstatedbadapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func (stateDB *StateDBAdapter) CreateAccount(evmAddr common.Address) {
log.L().Error("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 err != nil {
log.L().Error("Failed to create account.", zap.Error(err))
stateDB.logError(err)
Expand Down Expand Up @@ -286,7 +286,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 err != nil {
log.L().Error("Failed to add balance.", log.Hex("addrHash", evmAddr[:]))
stateDB.logError(err)
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 a36e72f

Please sign in to comment.