Skip to content

Commit 091d6f6

Browse files
lizhefengCoderZhi
authored andcommitted
Remove AccountState from Factory interface (iotexproject#1791)
1 parent 0fbf742 commit 091d6f6

28 files changed

+151
-201
lines changed

action/protocol/account/util/util.go

+19
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"github.com/iotexproject/go-pkgs/hash"
1515
"github.com/iotexproject/iotex-address/address"
16+
1617
"github.com/iotexproject/iotex-core/action/protocol"
1718
"github.com/iotexproject/iotex-core/state"
1819
)
@@ -87,3 +88,21 @@ func Recorded(sm protocol.StateReader, addr address.Address) (bool, error) {
8788
}
8889
return false, err
8990
}
91+
92+
// AccountState returns the confirmed account state on the chain
93+
func AccountState(sr protocol.StateReader, encodedAddr string) (*state.Account, error) {
94+
addr, err := address.FromString(encodedAddr)
95+
if err != nil {
96+
return nil, errors.Wrap(err, "error when getting the pubkey hash")
97+
}
98+
pkHash := hash.BytesToHash160(addr.Bytes())
99+
var account state.Account
100+
if err := sr.State(pkHash, &account); err != nil {
101+
if errors.Cause(err) == state.ErrStateNotExist {
102+
account = state.EmptyAccount()
103+
return &account, nil
104+
}
105+
return nil, errors.Wrapf(err, "error when loading state of %x", pkHash)
106+
}
107+
return &account, nil
108+
}

action/protocol/execution/protocol_test.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"github.com/iotexproject/iotex-core/action"
3333
"github.com/iotexproject/iotex-core/action/protocol"
3434
"github.com/iotexproject/iotex-core/action/protocol/account"
35+
accountutil "github.com/iotexproject/iotex-core/action/protocol/account/util"
3536
"github.com/iotexproject/iotex-core/action/protocol/execution/evm"
3637
"github.com/iotexproject/iotex-core/action/protocol/rewarding"
3738
"github.com/iotexproject/iotex-core/action/protocol/rolldpos"
@@ -211,7 +212,7 @@ func runExecution(
211212
contractAddr string,
212213
) ([]byte, *action.Receipt, error) {
213214
log.S().Info(ecfg.Comment)
214-
state, err := sf.AccountState(ecfg.Executor().String())
215+
state, err := accountutil.AccountState(sf, ecfg.Executor().String())
215216
if err != nil {
216217
return nil, nil, err
217218
}
@@ -314,7 +315,7 @@ func (sct *SmartContractTest) prepareBlockchain(
314315
r.NoError(reward.Register(registry))
315316

316317
r.NotNil(bc)
317-
bc.Validator().AddActionEnvelopeValidators(protocol.NewGenericValidator(sf.AccountState))
318+
bc.Validator().AddActionEnvelopeValidators(protocol.NewGenericValidator(sf, accountutil.AccountState))
318319
execution := NewProtocol(dao.GetBlockHash)
319320
r.NoError(execution.Register(registry))
320321
r.NoError(bc.Start(ctx))
@@ -420,7 +421,7 @@ func (sct *SmartContractTest) run(r *require.Assertions) {
420421
if account == "" {
421422
account = contractAddr
422423
}
423-
state, err := sf.AccountState(account)
424+
state, err := accountutil.AccountState(sf, account)
424425
r.NoError(err)
425426
r.Equal(
426427
0,
@@ -487,7 +488,7 @@ func TestProtocol_Handle(t *testing.T) {
487488
)
488489
exeProtocol := NewProtocol(dao.GetBlockHash)
489490
require.NoError(exeProtocol.Register(registry))
490-
bc.Validator().AddActionEnvelopeValidators(protocol.NewGenericValidator(sf.AccountState))
491+
bc.Validator().AddActionEnvelopeValidators(protocol.NewGenericValidator(sf, accountutil.AccountState))
491492
require.NoError(bc.Start(ctx))
492493
require.NotNil(bc)
493494
defer func() {

action/protocol/generic_validator.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,19 @@ import (
1818

1919
type (
2020
// AccountState defines a function to return the account state of a given address
21-
AccountState func(string) (*state.Account, error)
21+
AccountState func(StateReader, string) (*state.Account, error)
2222
// GenericValidator is the validator for generic action verification
2323
GenericValidator struct {
2424
mu sync.RWMutex
2525
accountState AccountState
26+
sr StateReader
2627
}
2728
)
2829

2930
// NewGenericValidator constructs a new genericValidator
30-
func NewGenericValidator(accountState AccountState) *GenericValidator {
31+
func NewGenericValidator(sr StateReader, accountState AccountState) *GenericValidator {
3132
return &GenericValidator{
33+
sr: sr,
3234
accountState: accountState,
3335
}
3436
}
@@ -46,7 +48,7 @@ func (v *GenericValidator) Validate(ctx context.Context, act action.SealedEnvelo
4648
return errors.Wrap(err, "failed to verify action signature")
4749
}
4850
// Reject action if nonce is too low
49-
confirmedState, err := v.accountState(actionCtx.Caller.String())
51+
confirmedState, err := v.accountState(v.sr, actionCtx.Caller.String())
5052
if err != nil {
5153
return errors.Wrapf(err, "invalid state of account %s", actionCtx.Caller.String())
5254
}

action/protocol/generic_validator_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func TestActionProto(t *testing.T) {
5151
},
5252
})
5353

54-
valid := NewGenericValidator(func(addr string) (*state.Account, error) {
54+
valid := NewGenericValidator(nil, func(sr StateReader, addr string) (*state.Account, error) {
5555
if strings.EqualFold("io1emxf8zzqckhgjde6dqd97ts0y3q496gm3fdrl6", addr) {
5656
return nil, errors.New("MockChainManager nonce error")
5757
}

actpool/actpool.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,19 @@ import (
1212
"strings"
1313
"sync"
1414

15-
"github.com/iotexproject/iotex-core/pkg/prometheustimer"
1615
"github.com/pkg/errors"
1716
"github.com/prometheus/client_golang/prometheus"
1817
"go.uber.org/zap"
1918

2019
"github.com/iotexproject/go-pkgs/hash"
2120
"github.com/iotexproject/iotex-address/address"
21+
2222
"github.com/iotexproject/iotex-core/action"
2323
"github.com/iotexproject/iotex-core/action/protocol"
24+
"github.com/iotexproject/iotex-core/action/protocol/account/util"
2425
"github.com/iotexproject/iotex-core/config"
2526
"github.com/iotexproject/iotex-core/pkg/log"
27+
"github.com/iotexproject/iotex-core/pkg/prometheustimer"
2628
"github.com/iotexproject/iotex-core/state/factory"
2729
)
2830

@@ -256,7 +258,7 @@ func (ap *actPool) GetPendingNonce(addr string) (uint64, error) {
256258
if queue, ok := ap.accountActs[addr]; ok {
257259
return queue.PendingNonce(), nil
258260
}
259-
confirmedState, err := ap.sf.AccountState(addr)
261+
confirmedState, err := accountutil.AccountState(ap.sf, addr)
260262
if err != nil {
261263
return 0, err
262264
}
@@ -326,7 +328,7 @@ func (ap *actPool) GetGasCapacity() uint64 {
326328
// private functions
327329
//======================================
328330
func (ap *actPool) enqueueAction(sender string, act action.SealedEnvelope, actHash hash.Hash256, actNonce uint64) error {
329-
confirmedState, err := ap.sf.AccountState(sender)
331+
confirmedState, err := accountutil.AccountState(ap.sf, sender)
330332
if err != nil {
331333
actpoolMtc.WithLabelValues("failedToGetNonce").Inc()
332334
return errors.Wrapf(err, "failed to get sender's nonce for action %x", actHash)
@@ -342,7 +344,7 @@ func (ap *actPool) enqueueAction(sender string, act action.SealedEnvelope, actHa
342344
pendingNonce := confirmedNonce + 1
343345
queue.SetPendingNonce(pendingNonce)
344346
// Initialize balance for new account
345-
state, err := ap.sf.AccountState(sender)
347+
state, err := accountutil.AccountState(ap.sf, sender)
346348
if err != nil {
347349
actpoolMtc.WithLabelValues("failedToGetBalance").Inc()
348350
return errors.Wrapf(err, "failed to get sender's balance for action %x", actHash)
@@ -412,7 +414,7 @@ func (ap *actPool) enqueueAction(sender string, act action.SealedEnvelope, actHa
412414
// removeConfirmedActs removes processed (committed to block) actions from pool
413415
func (ap *actPool) removeConfirmedActs() {
414416
for from, queue := range ap.accountActs {
415-
confirmedState, err := ap.sf.AccountState(from)
417+
confirmedState, err := accountutil.AccountState(ap.sf, from)
416418
if err != nil {
417419
log.L().Error("Error when removing confirmed actions", zap.Error(err))
418420
return
@@ -476,7 +478,7 @@ func (ap *actPool) reset() {
476478
ap.removeConfirmedActs()
477479
for from, queue := range ap.accountActs {
478480
// Reset pending balance for each account
479-
state, err := ap.sf.AccountState(from)
481+
state, err := accountutil.AccountState(ap.sf, from)
480482
if err != nil {
481483
log.L().Error("Error when resetting actpool state.", zap.Error(err))
482484
return

actpool/actpool_test.go

+14-13
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package actpool
88

99
import (
1010
"context"
11+
accountutil "github.com/iotexproject/iotex-core/action/protocol/account/util"
1112
"math/big"
1213
"strings"
1314
"testing"
@@ -107,7 +108,7 @@ func TestActPool_validateGenericAction(t *testing.T) {
107108
require.NoError(err)
108109
ap, ok := Ap.(*actPool)
109110
require.True(ok)
110-
ap.AddActionEnvelopeValidators(protocol.NewGenericValidator(sf.AccountState))
111+
ap.AddActionEnvelopeValidators(protocol.NewGenericValidator(sf, accountutil.AccountState))
111112
validator := ap.actionEnvelopeValidators[0]
112113
ctx := protocol.WithActionCtx(context.Background(), protocol.ActionCtx{})
113114
// Case I: Insufficient gas
@@ -183,7 +184,7 @@ func TestActPool_AddActs(t *testing.T) {
183184
require.NoError(err)
184185
ap, ok := Ap.(*actPool)
185186
require.True(ok)
186-
ap.AddActionEnvelopeValidators(protocol.NewGenericValidator(sf.AccountState))
187+
ap.AddActionEnvelopeValidators(protocol.NewGenericValidator(sf, accountutil.AccountState))
187188
// Test actpool status after adding a sequence of Tsfs/votes: need to check confirmed nonce, pending nonce, and pending balance
188189
tsf1, err := testutil.SignedTransfer(addr1, priKey1, uint64(1), big.NewInt(10), []byte{}, uint64(100000), big.NewInt(0))
189190
require.NoError(err)
@@ -351,7 +352,7 @@ func TestActPool_PickActs(t *testing.T) {
351352
require.NoError(err)
352353
ap, ok := Ap.(*actPool)
353354
require.True(ok)
354-
ap.AddActionEnvelopeValidators(protocol.NewGenericValidator(sf.AccountState))
355+
ap.AddActionEnvelopeValidators(protocol.NewGenericValidator(sf, accountutil.AccountState))
355356

356357
tsf1, err := testutil.SignedTransfer(addr1, priKey1, uint64(1), big.NewInt(10), []byte{}, uint64(100000), big.NewInt(0))
357358
require.NoError(err)
@@ -435,7 +436,7 @@ func TestActPool_removeConfirmedActs(t *testing.T) {
435436
require.NoError(err)
436437
ap, ok := Ap.(*actPool)
437438
require.True(ok)
438-
ap.AddActionEnvelopeValidators(protocol.NewGenericValidator(sf.AccountState))
439+
ap.AddActionEnvelopeValidators(protocol.NewGenericValidator(sf, accountutil.AccountState))
439440

440441
tsf1, err := testutil.SignedTransfer(addr1, priKey1, uint64(1), big.NewInt(10), []byte{}, uint64(100000), big.NewInt(0))
441442
require.NoError(err)
@@ -503,12 +504,12 @@ func TestActPool_Reset(t *testing.T) {
503504
require.NoError(err)
504505
ap1, ok := Ap1.(*actPool)
505506
require.True(ok)
506-
ap1.AddActionEnvelopeValidators(protocol.NewGenericValidator(sf.AccountState))
507+
ap1.AddActionEnvelopeValidators(protocol.NewGenericValidator(sf, accountutil.AccountState))
507508
Ap2, err := NewActPool(sf, apConfig, EnableExperimentalActions())
508509
require.NoError(err)
509510
ap2, ok := Ap2.(*actPool)
510511
require.True(ok)
511-
ap2.AddActionEnvelopeValidators(protocol.NewGenericValidator(sf.AccountState))
512+
ap2.AddActionEnvelopeValidators(protocol.NewGenericValidator(sf, accountutil.AccountState))
512513

513514
// Tsfs to be added to ap1
514515
tsf1, err := testutil.SignedTransfer(addr2, priKey1, uint64(1), big.NewInt(50), []byte{}, uint64(20000), big.NewInt(0))
@@ -865,7 +866,7 @@ func TestActPool_removeInvalidActs(t *testing.T) {
865866
require.NoError(err)
866867
ap, ok := Ap.(*actPool)
867868
require.True(ok)
868-
ap.AddActionEnvelopeValidators(protocol.NewGenericValidator(sf.AccountState))
869+
ap.AddActionEnvelopeValidators(protocol.NewGenericValidator(sf, accountutil.AccountState))
869870

870871
tsf1, err := testutil.SignedTransfer(addr1, priKey1, uint64(1), big.NewInt(10), []byte{}, uint64(100000), big.NewInt(0))
871872
require.NoError(err)
@@ -918,7 +919,7 @@ func TestActPool_GetPendingNonce(t *testing.T) {
918919
require.NoError(err)
919920
ap, ok := Ap.(*actPool)
920921
require.True(ok)
921-
ap.AddActionEnvelopeValidators(protocol.NewGenericValidator(sf.AccountState))
922+
ap.AddActionEnvelopeValidators(protocol.NewGenericValidator(sf, accountutil.AccountState))
922923

923924
tsf1, err := testutil.SignedTransfer(addr1, priKey1, uint64(1), big.NewInt(10), []byte{}, uint64(100000), big.NewInt(0))
924925
require.NoError(err)
@@ -967,7 +968,8 @@ func TestActPool_GetUnconfirmedActs(t *testing.T) {
967968
require.NoError(err)
968969
ap, ok := Ap.(*actPool)
969970
require.True(ok)
970-
ap.AddActionEnvelopeValidators(protocol.NewGenericValidator(sf.AccountState))
971+
ap.AddActionEnvelopeValidators(protocol.NewGenericValidator(sf, accountutil.AccountState))
972+
971973
tsf1, err := testutil.SignedTransfer(addr1, priKey1, uint64(1), big.NewInt(10), []byte{}, uint64(100000), big.NewInt(0))
972974
require.NoError(err)
973975
tsf3, err := testutil.SignedTransfer(addr1, priKey1, uint64(3), big.NewInt(30), []byte{}, uint64(100000), big.NewInt(0))
@@ -1073,7 +1075,7 @@ func TestActPool_GetSize(t *testing.T) {
10731075
require.NoError(err)
10741076
ap, ok := Ap.(*actPool)
10751077
require.True(ok)
1076-
ap.AddActionEnvelopeValidators(protocol.NewGenericValidator(sf.AccountState))
1078+
ap.AddActionEnvelopeValidators(protocol.NewGenericValidator(sf, accountutil.AccountState))
10771079
require.Zero(ap.GetSize())
10781080
require.Zero(ap.GetGasSize())
10791081

@@ -1154,8 +1156,7 @@ func (ap *actPool) getPendingNonce(addr string) (uint64, error) {
11541156
if queue, ok := ap.accountActs[addr]; ok {
11551157
return queue.PendingNonce(), nil
11561158
}
1157-
committedState, err := ap.sf.AccountState(addr)
1158-
1159+
committedState, err := accountutil.AccountState(ap.sf, addr)
11591160
return committedState.Nonce + 1, err
11601161
}
11611162

@@ -1164,7 +1165,7 @@ func (ap *actPool) getPendingBalance(addr string) (*big.Int, error) {
11641165
if queue, ok := ap.accountActs[addr]; ok {
11651166
return queue.PendingBalance(), nil
11661167
}
1167-
state, err := ap.sf.AccountState(addr)
1168+
state, err := accountutil.AccountState(ap.sf, addr)
11681169
if err != nil {
11691170
return nil, err
11701171
}

actpool/actqueue.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ package actpool
88

99
import (
1010
"container/heap"
11+
"go.uber.org/zap"
1112
"math/big"
1213
"sort"
1314
"time"
1415

15-
"github.com/iotexproject/iotex-core/pkg/log"
16-
"go.uber.org/zap"
17-
1816
"github.com/facebookgo/clock"
1917
"github.com/pkg/errors"
2018

2119
"github.com/iotexproject/iotex-core/action"
20+
accountutil "github.com/iotexproject/iotex-core/action/protocol/account/util"
21+
"github.com/iotexproject/iotex-core/pkg/log"
2222
)
2323

2424
type nonceWithTTL struct {
@@ -232,7 +232,7 @@ func (q *actQueue) PendingActs() []action.SealedEnvelope {
232232
return []action.SealedEnvelope{}
233233
}
234234
acts := make([]action.SealedEnvelope, 0, len(q.items))
235-
confirmedState, err := q.ap.sf.AccountState(q.address)
235+
confirmedState, err := accountutil.AccountState(q.ap.sf, q.address)
236236
if err != nil {
237237
log.L().Error("Error when getting the nonce", zap.String("address", q.address), zap.Error(err))
238238
return nil

actpool/actqueue_test.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ import (
1717
"github.com/stretchr/testify/assert"
1818
"github.com/stretchr/testify/require"
1919

20+
"github.com/iotexproject/go-pkgs/hash"
21+
2022
"github.com/iotexproject/iotex-core/action"
2123
"github.com/iotexproject/iotex-core/config"
2224
"github.com/iotexproject/iotex-core/state"
25+
"github.com/iotexproject/iotex-core/test/identityset"
2326
"github.com/iotexproject/iotex-core/test/mock/mock_factory"
2427
"github.com/iotexproject/iotex-core/testutil"
2528
)
@@ -123,10 +126,12 @@ func TestActQueuePendingActs(t *testing.T) {
123126
require := require.New(t)
124127
cfg := config.Default
125128
sf := mock_factory.NewMockFactory(ctrl)
126-
sf.EXPECT().AccountState(gomock.Any()).Return(&state.Account{Nonce: 1}, nil).Times(1)
129+
sf.EXPECT().State(gomock.Any(), gomock.Any()).Do(func(_ hash.Hash160, accountState *state.Account) {
130+
accountState.Nonce = uint64(1)
131+
}).Return(nil).Times(1)
127132
ap, err := NewActPool(sf, cfg.ActPool, EnableExperimentalActions())
128133
require.NoError(err)
129-
q := NewActQueue(ap.(*actPool), "").(*actQueue)
134+
q := NewActQueue(ap.(*actPool), identityset.Address(0).String()).(*actQueue)
130135
tsf1, err := testutil.SignedTransfer(addr2, priKey1, 2, big.NewInt(100), nil, uint64(0), big.NewInt(0))
131136
require.NoError(err)
132137
tsf2, err := testutil.SignedTransfer(addr2, priKey1, 3, big.NewInt(100), nil, uint64(0), big.NewInt(0))

api/api.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package api
99
import (
1010
"context"
1111
"encoding/hex"
12+
accountutil "github.com/iotexproject/iotex-core/action/protocol/account/util"
1213
"math"
1314
"math/big"
1415
"net"
@@ -161,7 +162,7 @@ func NewServer(
161162

162163
// GetAccount returns the metadata of an account
163164
func (api *Server) GetAccount(ctx context.Context, in *iotexapi.GetAccountRequest) (*iotexapi.GetAccountResponse, error) {
164-
state, err := api.sf.AccountState(in.Address)
165+
state, err := accountutil.AccountState(api.sf, in.Address)
165166
if err != nil {
166167
return nil, status.Error(codes.NotFound, err.Error())
167168
}
@@ -402,7 +403,7 @@ func (api *Server) ReadContract(ctx context.Context, in *iotexapi.ReadContractRe
402403
return nil, status.Error(codes.InvalidArgument, err.Error())
403404
}
404405

405-
state, err := api.sf.AccountState(in.CallerAddress)
406+
state, err := accountutil.AccountState(api.sf, in.CallerAddress)
406407
if err != nil {
407408
return nil, status.Error(codes.InvalidArgument, err.Error())
408409
}
@@ -1280,7 +1281,7 @@ func (api *Server) estimateActionGasConsumptionForExecution(exec *iotextypes.Exe
12801281
if err := sc.LoadProto(exec); err != nil {
12811282
return nil, status.Error(codes.InvalidArgument, err.Error())
12821283
}
1283-
state, err := api.sf.AccountState(sender)
1284+
state, err := accountutil.AccountState(api.sf, sender)
12841285
if err != nil {
12851286
return nil, status.Error(codes.InvalidArgument, err.Error())
12861287
}

0 commit comments

Comments
 (0)