Skip to content

Commit d83c17f

Browse files
authored
util.StoreAccount() take address.Address as input 2155 (iotexproject#2156)
1 parent 2d155b3 commit d83c17f

File tree

10 files changed

+26
-25
lines changed

10 files changed

+26
-25
lines changed

action/protocol/account/transfer.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func (p *Protocol) handleTransfer(ctx context.Context, act action.Action, sm pro
7373
// update sender Nonce
7474
accountutil.SetNonce(tsf, sender)
7575
// put updated sender's state to trie
76-
if err := accountutil.StoreAccount(sm, actionCtx.Caller.String(), sender); err != nil {
76+
if err := accountutil.StoreAccount(sm, actionCtx.Caller, sender); err != nil {
7777
return nil, errors.Wrap(err, "failed to update pending account changes to trie")
7878
}
7979
if hu.IsPost(config.Pacific, blkCtx.BlockHeight) {
@@ -99,7 +99,7 @@ func (p *Protocol) handleTransfer(ctx context.Context, act action.Action, sm pro
9999
// update sender Nonce
100100
accountutil.SetNonce(tsf, sender)
101101
// put updated sender's state to trie
102-
if err := accountutil.StoreAccount(sm, actionCtx.Caller.String(), sender); err != nil {
102+
if err := accountutil.StoreAccount(sm, actionCtx.Caller, sender); err != nil {
103103
return nil, errors.Wrap(err, "failed to update pending account changes to trie")
104104
}
105105
// check recipient
@@ -110,9 +110,8 @@ func (p *Protocol) handleTransfer(ctx context.Context, act action.Action, sm pro
110110
if err := recipient.AddBalance(tsf.Amount()); err != nil {
111111
return nil, errors.Wrapf(err, "failed to update the Balance of recipient %s", tsf.Recipient())
112112
}
113-
114113
// put updated recipient's state to trie
115-
if err := accountutil.StoreAccount(sm, tsf.Recipient(), recipient); err != nil {
114+
if err := accountutil.StoreAccount(sm, recipientAddr, recipient); err != nil {
116115
return nil, errors.Wrap(err, "failed to update pending account changes to trie")
117116
}
118117

action/protocol/account/util/util.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,9 @@ func LoadAccount(sm protocol.StateReader, addrHash hash.Hash160) (*state.Account
6767
}
6868

6969
// StoreAccount puts updated account state to trie
70-
func StoreAccount(sm protocol.StateManager, encodedAddr string, account *state.Account) error {
71-
addr, err := address.FromString(encodedAddr)
72-
if err != nil {
73-
return errors.Wrap(err, "failed to get address public key hash from encoded address")
74-
}
70+
func StoreAccount(sm protocol.StateManager, addr address.Address, account *state.Account) error {
7571
addrHash := hash.BytesToHash160(addr.Bytes())
76-
_, err = sm.PutState(account, protocol.LegacyKeyOption(addrHash))
72+
_, err := sm.PutState(account, protocol.LegacyKeyOption(addrHash))
7773
return err
7874
}
7975

action/protocol/execution/evm/evmstatedbadapter.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func (stateDB *StateDBAdapter) SubBalance(evmAddr common.Address, amount *big.In
147147
stateDB.logError(err)
148148
return
149149
}
150-
if err := accountutil.StoreAccount(stateDB.sm, addr.String(), state); err != nil {
150+
if err := accountutil.StoreAccount(stateDB.sm, addr, state); err != nil {
151151
log.L().Error("Failed to update pending account changes to trie.", zap.Error(err))
152152
stateDB.logError(err)
153153
}
@@ -183,7 +183,7 @@ func (stateDB *StateDBAdapter) AddBalance(evmAddr common.Address, amount *big.In
183183
stateDB.logError(err)
184184
return
185185
}
186-
if err := accountutil.StoreAccount(stateDB.sm, addr.String(), state); err != nil {
186+
if err := accountutil.StoreAccount(stateDB.sm, addr, state); err != nil {
187187
log.L().Error("Failed to update pending account changes to trie.", zap.Error(err))
188188
stateDB.logError(err)
189189
}
@@ -242,7 +242,7 @@ func (stateDB *StateDBAdapter) SetNonce(evmAddr common.Address, nonce uint64) {
242242
zap.String("address", addr.String()),
243243
zap.Uint64("nonce", nonce))
244244
s.Nonce = nonce
245-
if err := accountutil.StoreAccount(stateDB.sm, addr.String(), s); err != nil {
245+
if err := accountutil.StoreAccount(stateDB.sm, addr, s); err != nil {
246246
log.L().Error("Failed to set nonce.", zap.Error(err))
247247
stateDB.logError(err)
248248
}

action/protocol/poll/util.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ import (
1111
"fmt"
1212
"math/big"
1313

14-
"github.com/iotexproject/iotex-proto/golang/iotextypes"
1514
"github.com/pkg/errors"
1615
"go.uber.org/zap"
1716

17+
"github.com/iotexproject/iotex-address/address"
18+
"github.com/iotexproject/iotex-proto/golang/iotextypes"
19+
1820
"github.com/iotexproject/iotex-core/action"
1921
"github.com/iotexproject/iotex-core/action/protocol"
2022
accountutil "github.com/iotexproject/iotex-core/action/protocol/account/util"
@@ -178,7 +180,11 @@ func setCandidates(
178180
return err
179181
}
180182
}
181-
if err := accountutil.StoreAccount(sm, candidate.Address, delegate); err != nil {
183+
candAddr, err := address.FromString(candidate.Address)
184+
if err != nil {
185+
errors.Wrap(err, "failed to convert candidate address")
186+
}
187+
if err := accountutil.StoreAccount(sm, candAddr, delegate); err != nil {
182188
return errors.Wrap(err, "failed to update pending account changes to trie")
183189
}
184190
log.L().Debug(

action/protocol/rewarding/fund.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func (p *Protocol) Deposit(
7373
return err
7474
}
7575
acc.Balance = big.NewInt(0).Sub(acc.Balance, amount)
76-
if err := accountutil.StoreAccount(sm, actionCtx.Caller.String(), acc); err != nil {
76+
if err := accountutil.StoreAccount(sm, actionCtx.Caller, acc); err != nil {
7777
return err
7878
}
7979
// Add balance to fund

action/protocol/rewarding/protocol.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ func (p *Protocol) increaseNonce(sm protocol.StateManager, addr address.Address,
283283
if nonce > acc.Nonce {
284284
acc.Nonce = nonce
285285
}
286-
return accountutil.StoreAccount(sm, addr.String(), acc)
286+
return accountutil.StoreAccount(sm, addr, acc)
287287
}
288288

289289
func (p *Protocol) createReceipt(

action/protocol/rewarding/reward.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ func (p *Protocol) claimFromAccount(sm protocol.StateManager, addr address.Addre
365365
return err
366366
}
367367
primAcc.Balance = big.NewInt(0).Add(primAcc.Balance, amount)
368-
return accountutil.StoreAccount(sm, addr.String(), primAcc)
368+
return accountutil.StoreAccount(sm, addr, primAcc)
369369
}
370370

371371
func (p *Protocol) updateRewardHistory(sm protocol.StateManager, prefix []byte, index uint64) error {

action/protocol/staking/handlers.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func (p *Protocol) handleCreateStake(ctx context.Context, act *action.CreateStak
109109
}
110110
}
111111
// put updated staker's account state to trie
112-
if err := accountutil.StoreAccount(csm, actionCtx.Caller.String(), staker); err != nil {
112+
if err := accountutil.StoreAccount(csm, actionCtx.Caller, staker); err != nil {
113113
return log, errors.Wrapf(err, "failed to store account %s", actionCtx.Caller.String())
114114
}
115115

@@ -225,7 +225,7 @@ func (p *Protocol) handleWithdrawStake(ctx context.Context, act *action.Withdraw
225225
}
226226
}
227227
// put updated withdrawer's account state to trie
228-
if err := accountutil.StoreAccount(csm, actionCtx.Caller.String(), withdrawer); err != nil {
228+
if err := accountutil.StoreAccount(csm, actionCtx.Caller, withdrawer); err != nil {
229229
return log, errors.Wrapf(err, "failed to store account %s", actionCtx.Caller.String())
230230
}
231231

@@ -404,7 +404,7 @@ func (p *Protocol) handleDepositToStake(ctx context.Context, act *action.Deposit
404404
}
405405
}
406406
// put updated depositor's account state to trie
407-
if err := accountutil.StoreAccount(csm, actionCtx.Caller.String(), depositor); err != nil {
407+
if err := accountutil.StoreAccount(csm, actionCtx.Caller, depositor); err != nil {
408408
return log, errors.Wrapf(err, "failed to store account %s", actionCtx.Caller.String())
409409
}
410410

@@ -554,7 +554,7 @@ func (p *Protocol) handleCandidateRegister(ctx context.Context, act *action.Cand
554554
}
555555
}
556556
// put updated caller's account state to trie
557-
if err := accountutil.StoreAccount(csm, actCtx.Caller.String(), caller); err != nil {
557+
if err := accountutil.StoreAccount(csm, actCtx.Caller, caller); err != nil {
558558
return log, errors.Wrapf(err, "failed to store account %s", actCtx.Caller.String())
559559
}
560560

action/protocol/staking/handlers_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -2178,7 +2178,7 @@ func setupAccount(sm protocol.StateManager, addr address.Address, balance int64)
21782178
return err
21792179
}
21802180
account.Balance = unit.ConvertIotxToRau(balance)
2181-
return accountutil.StoreAccount(sm, addr.String(), account)
2181+
return accountutil.StoreAccount(sm, addr, account)
21822182
}
21832183

21842184
func depositGas(ctx context.Context, sm protocol.StateManager, gasFee *big.Int) error {
@@ -2189,5 +2189,5 @@ func depositGas(ctx context.Context, sm protocol.StateManager, gasFee *big.Int)
21892189
return err
21902190
}
21912191
acc.Balance = big.NewInt(0).Sub(acc.Balance, gasFee)
2192-
return accountutil.StoreAccount(sm, actionCtx.Caller.String(), acc)
2192+
return accountutil.StoreAccount(sm, actionCtx.Caller, acc)
21932193
}

action/protocol/staking/protocol.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ func (p *Protocol) settleAction(
397397
if actionCtx.Nonce > acc.Nonce {
398398
acc.Nonce = actionCtx.Nonce
399399
}
400-
if err := accountutil.StoreAccount(sm, actionCtx.Caller.String(), acc); err != nil {
400+
if err := accountutil.StoreAccount(sm, actionCtx.Caller, acc); err != nil {
401401
return nil, errors.Wrap(err, "failed to update nonce")
402402
}
403403
r := action.Receipt{

0 commit comments

Comments
 (0)