From 411cde2924ee7406b732e4f6fd846a51f72e84db Mon Sep 17 00:00:00 2001 From: Seoyoung Ko <31757627+koseoyoung@users.noreply.github.com> Date: Wed, 12 Feb 2020 23:14:17 -0800 Subject: [PATCH] refactor StateManger/StateReader (#1886) * refactor statemanger/statereader * Update blockchain.go Co-authored-by: CoderZhi --- action/protocol/account/protocol.go | 4 +- action/protocol/account/protocol_test.go | 48 +++++++++----- action/protocol/account/transfer_test.go | 45 +++++++++----- action/protocol/account/util/util.go | 15 ++--- .../protocol/execution/evm/contract_test.go | 25 +++++--- action/protocol/execution/evm/evm_test.go | 4 +- .../execution/evm/evmstatedbadapter.go | 6 +- .../execution/evm/evmstatedbadapter_test.go | 26 +++++--- action/protocol/managers.go | 25 +++++++- .../protocol/poll/governance_protocol_test.go | 47 +++++++++----- .../protocol/poll/lifelong_protocol_test.go | 25 +++++--- .../protocol/poll/staking_committee_test.go | 44 ++++++++----- action/protocol/poll/util.go | 6 +- action/protocol/rewarding/protocol.go | 9 ++- action/protocol/rewarding/protocol_test.go | 48 +++++++++----- action/protocol/rewarding/reward_test.go | 24 ++++--- .../vote/candidatesutil/candidatesutil.go | 9 +-- actpool/actqueue_test.go | 7 +-- api/api_test.go | 8 +-- e2etest/staking_test.go | 6 +- state/factory/factory.go | 18 +++--- state/factory/factory_test.go | 57 +++++++++++------ state/factory/statedb.go | 12 ++-- state/factory/statetx.go | 32 +++++----- state/factory/workingset.go | 36 ++++++----- .../mock_chainmanager/mock_chainmanager.go | 62 ++++++++++--------- test/mock/mock_factory/mock_factory.go | 16 ++--- test/mock/mock_factory/mock_workingset.go | 46 +++++++------- .../internal/client/client_test.go | 3 +- 29 files changed, 439 insertions(+), 274 deletions(-) diff --git a/action/protocol/account/protocol.go b/action/protocol/account/protocol.go index 0cf1eb74a1..773bdcccee 100644 --- a/action/protocol/account/protocol.go +++ b/action/protocol/account/protocol.go @@ -103,14 +103,14 @@ func createAccount(sm protocol.StateManager, encodedAddr string, init *big.Int) return errors.Wrap(err, "failed to get address public key hash from encoded address") } addrHash := hash.BytesToHash160(addr.Bytes()) - err = sm.State(addrHash, &account) + _, err = sm.State(&account, protocol.LegacyKeyOption(addrHash)) switch errors.Cause(err) { case nil: return errors.Errorf("failed to create account %s", encodedAddr) case state.ErrStateNotExist: account.Balance = init account.VotingWeight = big.NewInt(0) - if err := sm.PutState(addrHash, account); err != nil { + if _, err := sm.PutState(account, protocol.LegacyKeyOption(addrHash)); err != nil { return errors.Wrapf(err, "failed to put state for account %x", addrHash) } return nil diff --git a/action/protocol/account/protocol_test.go b/action/protocol/account/protocol_test.go index adf9e8f663..cedd243ba0 100644 --- a/action/protocol/account/protocol_test.go +++ b/action/protocol/account/protocol_test.go @@ -33,21 +33,29 @@ func TestLoadOrCreateAccountState(t *testing.T) { sm := mock_chainmanager.NewMockStateManager(ctrl) cb := batch.NewCachedBatch() sm.EXPECT().State(gomock.Any(), gomock.Any()).DoAndReturn( - func(addrHash hash.Hash160, account interface{}) error { - val, err := cb.Get("state", addrHash[:]) + func(account interface{}, opts ...protocol.StateOption) (uint64, error) { + cfg, err := protocol.CreateStateConfig(opts...) if err != nil { - return state.ErrStateNotExist + return 0, err } - return state.Deserialize(account, val) + val, err := cb.Get("state", cfg.Key) + if err != nil { + return 0, state.ErrStateNotExist + } + return 0, state.Deserialize(account, val) }).AnyTimes() sm.EXPECT().PutState(gomock.Any(), gomock.Any()).DoAndReturn( - func(addrHash hash.Hash160, account interface{}) error { + func(account interface{}, opts ...protocol.StateOption) (uint64, error) { + cfg, err := protocol.CreateStateConfig(opts...) + if err != nil { + return 0, err + } ss, err := state.Serialize(account) if err != nil { - return err + return 0, err } - cb.Put("state", addrHash[:], ss, "failed to put state") - return nil + cb.Put("state", cfg.Key, ss, "failed to put state") + return 0, nil }).AnyTimes() addrv1 := identityset.Address(27) @@ -71,21 +79,29 @@ func TestProtocol_Initialize(t *testing.T) { sm := mock_chainmanager.NewMockStateManager(ctrl) cb := batch.NewCachedBatch() sm.EXPECT().State(gomock.Any(), gomock.Any()).DoAndReturn( - func(addrHash hash.Hash160, account interface{}) error { - val, err := cb.Get("state", addrHash[:]) + func(account interface{}, opts ...protocol.StateOption) (uint64, error) { + cfg, err := protocol.CreateStateConfig(opts...) if err != nil { - return state.ErrStateNotExist + return 0, err } - return state.Deserialize(account, val) + val, err := cb.Get("state", cfg.Key) + if err != nil { + return 0, state.ErrStateNotExist + } + return 0, state.Deserialize(account, val) }).AnyTimes() sm.EXPECT().PutState(gomock.Any(), gomock.Any()).DoAndReturn( - func(addrHash hash.Hash160, account interface{}) error { + func(account interface{}, opts ...protocol.StateOption) (uint64, error) { + cfg, err := protocol.CreateStateConfig(opts...) + if err != nil { + return 0, err + } ss, err := state.Serialize(account) if err != nil { - return err + return 0, err } - cb.Put("state", addrHash[:], ss, "failed to put state") - return nil + cb.Put("state", cfg.Key, ss, "failed to put state") + return 0, nil }).AnyTimes() ge := config.Default.Genesis diff --git a/action/protocol/account/transfer_test.go b/action/protocol/account/transfer_test.go index f0797da478..d0b9d0b64c 100644 --- a/action/protocol/account/transfer_test.go +++ b/action/protocol/account/transfer_test.go @@ -41,21 +41,29 @@ func TestProtocol_HandleTransfer(t *testing.T) { sm := mock_chainmanager.NewMockStateManager(ctrl) cb := batch.NewCachedBatch() sm.EXPECT().State(gomock.Any(), gomock.Any()).DoAndReturn( - func(addrHash hash.Hash160, account interface{}) error { - val, err := cb.Get("state", addrHash[:]) + func(account interface{}, opts ...protocol.StateOption) (uint64, error) { + cfg, err := protocol.CreateStateConfig(opts...) if err != nil { - return state.ErrStateNotExist + return 0, err } - return state.Deserialize(account, val) + val, err := cb.Get("state", cfg.Key) + if err != nil { + return 0, state.ErrStateNotExist + } + return 0, state.Deserialize(account, val) }).AnyTimes() sm.EXPECT().PutState(gomock.Any(), gomock.Any()).DoAndReturn( - func(addrHash hash.Hash160, account interface{}) error { + func(account interface{}, opts ...protocol.StateOption) (uint64, error) { + cfg, err := protocol.CreateStateConfig(opts...) + if err != nil { + return 0, err + } ss, err := state.Serialize(account) if err != nil { - return err + return 0, err } - cb.Put("state", addrHash[:], ss, "failed to put state") - return nil + cb.Put("state", cfg.Key, ss, "failed to put state") + return 0, nil }).AnyTimes() p := NewProtocol(rewarding.DepositGas) @@ -104,9 +112,12 @@ func TestProtocol_HandleTransfer(t *testing.T) { pubKeyBravo := hash.BytesToHash160(identityset.Address(29).Bytes()) pubKeyCharlie := hash.BytesToHash160(identityset.Address(30).Bytes()) - require.NoError(sm.PutState(pubKeyAlfa, &accountAlfa)) - require.NoError(sm.PutState(pubKeyBravo, &accountBravo)) - require.NoError(sm.PutState(pubKeyCharlie, &accountCharlie)) + _, err := sm.PutState(&accountAlfa, protocol.LegacyKeyOption(pubKeyAlfa)) + require.NoError(err) + _, err = sm.PutState(&accountBravo, protocol.LegacyKeyOption(pubKeyBravo)) + require.NoError(err) + _, err = sm.PutState(&accountCharlie, protocol.LegacyKeyOption(pubKeyCharlie)) + require.NoError(err) transfer, err := action.NewTransfer( uint64(1), @@ -138,17 +149,20 @@ func TestProtocol_HandleTransfer(t *testing.T) { require.Equal(uint64(iotextypes.ReceiptStatus_Success), receipt.Status) var acct state.Account - require.NoError(sm.State(pubKeyAlfa, &acct)) + _, err = sm.State(&acct, protocol.LegacyKeyOption(pubKeyAlfa)) + require.NoError(err) require.Equal("40003", acct.Balance.String()) require.Equal(uint64(1), acct.Nonce) - require.NoError(sm.State(pubKeyBravo, &acct)) + _, err = sm.State(&acct, protocol.LegacyKeyOption(pubKeyBravo)) + require.NoError(err) require.Equal("2", acct.Balance.String()) contractAcct := state.Account{ CodeHash: []byte("codeHash"), } contractAddr := hash.BytesToHash160(identityset.Address(32).Bytes()) - require.NoError(sm.PutState(contractAddr, &contractAcct)) + _, err = sm.PutState(&contractAcct, protocol.LegacyKeyOption(contractAddr)) + require.NoError(err) transfer, err = action.NewTransfer( uint64(2), big.NewInt(3), @@ -162,7 +176,8 @@ func TestProtocol_HandleTransfer(t *testing.T) { receipt, err = p.Handle(ctx, transfer, sm) require.NoError(err) require.Equal(uint64(iotextypes.ReceiptStatus_Failure), receipt.Status) - require.NoError(sm.State(pubKeyAlfa, &acct)) + _, err = sm.State(&acct, protocol.LegacyKeyOption(pubKeyAlfa)) + require.NoError(err) require.Equal(uint64(2), acct.Nonce) require.Equal("20003", acct.Balance.String()) } diff --git a/action/protocol/account/util/util.go b/action/protocol/account/util/util.go index d9584c76c5..358ba721d9 100644 --- a/action/protocol/account/util/util.go +++ b/action/protocol/account/util/util.go @@ -38,14 +38,14 @@ func LoadOrCreateAccount(sm protocol.StateManager, encodedAddr string) (*state.A return &account, errors.Wrap(err, "failed to get address public key hash from encoded address") } addrHash := hash.BytesToHash160(addr.Bytes()) - err = sm.State(addrHash, &account) + _, err = sm.State(&account, protocol.LegacyKeyOption(addrHash)) if err == nil { return &account, nil } if errors.Cause(err) == state.ErrStateNotExist { account.Balance = big.NewInt(0) account.VotingWeight = big.NewInt(0) - if err := sm.PutState(addrHash, account); err != nil { + if _, err := sm.PutState(account, protocol.LegacyKeyOption(addrHash)); err != nil { return nil, errors.Wrapf(err, "failed to put state for account %x", addrHash) } return &account, nil @@ -56,7 +56,7 @@ func LoadOrCreateAccount(sm protocol.StateManager, encodedAddr string) (*state.A // LoadAccount loads an account state func LoadAccount(sm protocol.StateReader, addrHash hash.Hash160) (*state.Account, error) { var account state.Account - if err := sm.State(addrHash, &account); err != nil { + if _, err := sm.State(&account, protocol.LegacyKeyOption(addrHash)); err != nil { if errors.Cause(err) == state.ErrStateNotExist { account = state.EmptyAccount() return &account, nil @@ -73,13 +73,14 @@ func StoreAccount(sm protocol.StateManager, encodedAddr string, account *state.A return errors.Wrap(err, "failed to get address public key hash from encoded address") } addrHash := hash.BytesToHash160(addr.Bytes()) - return sm.PutState(addrHash, account) + _, err = sm.PutState(account, protocol.LegacyKeyOption(addrHash)) + return err } // Recorded tests if an account has been actually stored func Recorded(sm protocol.StateReader, addr address.Address) (bool, error) { var account state.Account - err := sm.State(hash.BytesToHash160(addr.Bytes()), &account) + _, err := sm.State(&account, protocol.LegacyKeyOption(hash.BytesToHash160(addr.Bytes()))) if err == nil { return true, nil } @@ -97,7 +98,7 @@ func AccountState(sr protocol.StateReader, encodedAddr string) (*state.Account, } pkHash := hash.BytesToHash160(addr.Bytes()) var account state.Account - if err := sr.State(pkHash, &account); err != nil { + if _, err := sr.State(&account, protocol.LegacyKeyOption(pkHash)); err != nil { if errors.Cause(err) == state.ErrStateNotExist { account = state.EmptyAccount() return &account, nil @@ -115,7 +116,7 @@ func AccountStateAtHeight(sr protocol.StateReader, encodedAddr string, height ui } pkHash := hash.BytesToHash160(addr.Bytes()) var account state.Account - if err := sr.State(pkHash, &account, protocol.BlockHeightOption(height)); err != nil { + if _, err := sr.State(&account, protocol.BlockHeightOption(height), protocol.LegacyKeyOption(pkHash)); err != nil { if errors.Cause(err) == state.ErrStateNotExist { account = state.EmptyAccount() return &account, nil diff --git a/action/protocol/execution/evm/contract_test.go b/action/protocol/execution/evm/contract_test.go index c186f52e87..d80519d04f 100644 --- a/action/protocol/execution/evm/contract_test.go +++ b/action/protocol/execution/evm/contract_test.go @@ -18,6 +18,7 @@ import ( "github.com/pkg/errors" "github.com/stretchr/testify/require" + "github.com/iotexproject/iotex-core/action/protocol" accountutil "github.com/iotexproject/iotex-core/action/protocol/account/util" "github.com/iotexproject/iotex-core/config" "github.com/iotexproject/iotex-core/db" @@ -40,21 +41,29 @@ func TestCreateContract(t *testing.T) { sm := mock_chainmanager.NewMockStateManager(ctrl) cb := batch.NewCachedBatch() sm.EXPECT().State(gomock.Any(), gomock.Any()).DoAndReturn( - func(addrHash hash.Hash160, account interface{}) error { - val, err := cb.Get("state", addrHash[:]) + func(account interface{}, opts ...protocol.StateOption) (uint64, error) { + cfg, err := protocol.CreateStateConfig(opts...) if err != nil { - return state.ErrStateNotExist + return 0, err } - return state.Deserialize(account, val) + val, err := cb.Get("state", cfg.Key) + if err != nil { + return 0, state.ErrStateNotExist + } + return 0, state.Deserialize(account, val) }).AnyTimes() sm.EXPECT().PutState(gomock.Any(), gomock.Any()).DoAndReturn( - func(addrHash hash.Hash160, account interface{}) error { + func(account interface{}, opts ...protocol.StateOption) (uint64, error) { + cfg, err := protocol.CreateStateConfig(opts...) + if err != nil { + return 0, err + } ss, err := state.Serialize(account) if err != nil { - return err + return 0, err } - cb.Put("state", addrHash[:], ss, "failed to put state") - return nil + cb.Put("state", cfg.Key, ss, "failed to put state") + return 0, nil }).AnyTimes() flusher, err := db.NewKVStoreFlusher(db.NewMemKVStore(), cb) diff --git a/action/protocol/execution/evm/evm_test.go b/action/protocol/execution/evm/evm_test.go index b911905f5d..e7b2cba646 100644 --- a/action/protocol/execution/evm/evm_test.go +++ b/action/protocol/execution/evm/evm_test.go @@ -36,8 +36,8 @@ func TestExecuteContractFailure(t *testing.T) { flusher, err := db.NewKVStoreFlusher(db.NewMemKVStore(), batch.NewCachedBatch()) require.NoError(t, err) sm.EXPECT().GetDB().Return(flusher.KVStoreWithBuffer()).AnyTimes() - sm.EXPECT().State(gomock.Any(), gomock.Any()).Return(state.ErrStateNotExist).AnyTimes() - sm.EXPECT().PutState(gomock.Any(), gomock.Any()).Return(nil).AnyTimes() + sm.EXPECT().State(gomock.Any(), gomock.Any()).Return(uint64(0), state.ErrStateNotExist).AnyTimes() + sm.EXPECT().PutState(gomock.Any(), gomock.Any()).Return(uint64(0), nil).AnyTimes() sm.EXPECT().Snapshot().Return(1).AnyTimes() e, err := action.NewExecution( diff --git a/action/protocol/execution/evm/evmstatedbadapter.go b/action/protocol/execution/evm/evmstatedbadapter.go index 0f37cf85e7..ccf7ffe0e8 100644 --- a/action/protocol/execution/evm/evmstatedbadapter.go +++ b/action/protocol/execution/evm/evmstatedbadapter.go @@ -289,7 +289,7 @@ func (stateDB *StateDBAdapter) Suicide(evmAddr common.Address) bool { s.Balance = nil s.Balance = big.NewInt(0) addrHash := hash.BytesToHash160(evmAddr.Bytes()) - if err := stateDB.sm.PutState(addrHash, s); err != nil { + if _, err := stateDB.sm.PutState(s, protocol.LegacyKeyOption(addrHash)); err != nil { log.L().Error("Failed to kill contract.", zap.Error(err)) stateDB.logError(err) return false @@ -639,7 +639,7 @@ func (stateDB *StateDBAdapter) CommitContracts() error { } state := contract.SelfState() // store the account (with new storage trie root) into account trie - if err := stateDB.sm.PutState(addr, state); err != nil { + if _, err := stateDB.sm.PutState(state, protocol.LegacyKeyOption(addr)); err != nil { stateDB.logError(err) return errors.Wrap(err, "failed to update pending account changes to trie") } @@ -658,7 +658,7 @@ func (stateDB *StateDBAdapter) CommitContracts() error { return errors.Wrap(err, "failed to decode address hash") } copy(addr[:], addrBytes) - if err := stateDB.sm.DelState(addr); err != nil { + if _, err := stateDB.sm.DelState(protocol.LegacyKeyOption(addr)); err != nil { stateDB.logError(err) return errors.Wrapf(err, "failed to delete suicide account/contract %x", addr[:]) } diff --git a/action/protocol/execution/evm/evmstatedbadapter_test.go b/action/protocol/execution/evm/evmstatedbadapter_test.go index ed4db15b4b..d832712e07 100644 --- a/action/protocol/execution/evm/evmstatedbadapter_test.go +++ b/action/protocol/execution/evm/evmstatedbadapter_test.go @@ -30,21 +30,29 @@ func initMockStateManager(ctrl *gomock.Controller) (protocol.StateManager, error sm := mock_chainmanager.NewMockStateManager(ctrl) cb := batch.NewCachedBatch() sm.EXPECT().State(gomock.Any(), gomock.Any()).DoAndReturn( - func(addrHash hash.Hash160, account interface{}) error { - val, err := cb.Get("state", addrHash[:]) + func(account interface{}, opts ...protocol.StateOption) (uint64, error) { + cfg, err := protocol.CreateStateConfig(opts...) if err != nil { - return state.ErrStateNotExist + return 0, err } - return state.Deserialize(account, val) + val, err := cb.Get("state", cfg.Key) + if err != nil { + return 0, state.ErrStateNotExist + } + return 0, state.Deserialize(account, val) }).AnyTimes() sm.EXPECT().PutState(gomock.Any(), gomock.Any()).DoAndReturn( - func(addrHash hash.Hash160, account interface{}) error { + func(account interface{}, opts ...protocol.StateOption) (uint64, error) { + cfg, err := protocol.CreateStateConfig(opts...) + if err != nil { + return 0, err + } ss, err := state.Serialize(account) if err != nil { - return err + return 0, err } - cb.Put("state", addrHash[:], ss, "failed to put state") - return nil + cb.Put("state", cfg.Key, ss, "failed to put state") + return 0, nil }).AnyTimes() flusher, err := db.NewKVStoreFlusher(db.NewMemKVStore(), cb) if err != nil { @@ -408,7 +416,7 @@ func TestGetBalanceOnError(t *testing.T) { errors.New("other error"), } for _, err := range errs { - sm.EXPECT().State(gomock.Any(), gomock.Any()).Return(err).Times(1) + sm.EXPECT().State(gomock.Any(), gomock.Any()).Return(uint64(0), err).Times(1) addr := common.HexToAddress("test address") stateDB := NewStateDBAdapter(sm, 1, true, hash.ZeroHash256) amount := stateDB.GetBalance(addr) diff --git a/action/protocol/managers.go b/action/protocol/managers.go index 946396906e..adafcdc5b4 100644 --- a/action/protocol/managers.go +++ b/action/protocol/managers.go @@ -24,6 +24,24 @@ func BlockHeightOption(height uint64) StateOption { } } +// KeyOption sets the key for call +func KeyOption(key []byte) StateOption { + return func(cfg *StateConfig) error { + cfg.Key = make([]byte, len(key)) + copy(cfg.Key, key) + return nil + } +} + +// LegacyKeyOption sets the key for call with legacy key +func LegacyKeyOption(key hash.Hash160) StateOption { + return func(cfg *StateConfig) error { + cfg.Key = make([]byte, len(key[:])) + copy(cfg.Key, key[:]) + return nil + } +} + // CreateStateConfig creates a config for accessing stateDB func CreateStateConfig(opts ...StateOption) (*StateConfig, error) { cfg := StateConfig{AtHeight: false} @@ -41,6 +59,7 @@ type ( Namespace string // namespace used by state's storage AtHeight bool Height uint64 + Key []byte } // StateOption sets parameter for access state @@ -49,7 +68,7 @@ type ( // StateReader defines an interface to read stateDB StateReader interface { Height() (uint64, error) - State(hash.Hash160, interface{}, ...StateOption) error + State(interface{}, ...StateOption) (uint64, error) } // StateManager defines the stateDB interface atop IoTeX blockchain @@ -59,8 +78,8 @@ type ( Snapshot() int Revert(int) error // General state - PutState(hash.Hash160, interface{}, ...StateOption) error - DelState(hash.Hash160, ...StateOption) error + PutState(interface{}, ...StateOption) (uint64, error) + DelState(...StateOption) (uint64, error) GetDB() db.KVStore } ) diff --git a/action/protocol/poll/governance_protocol_test.go b/action/protocol/poll/governance_protocol_test.go index 4b9771ca27..55dc4a3dc1 100644 --- a/action/protocol/poll/governance_protocol_test.go +++ b/action/protocol/poll/governance_protocol_test.go @@ -65,21 +65,29 @@ func initConstruct(ctrl *gomock.Controller) (Protocol, context.Context, protocol committee := mock_committee.NewMockCommittee(ctrl) cb := batch.NewCachedBatch() sm.EXPECT().State(gomock.Any(), gomock.Any()).DoAndReturn( - func(addrHash hash.Hash160, account interface{}) error { - val, err := cb.Get("state", addrHash[:]) + func(account interface{}, opts ...protocol.StateOption) (uint64, error) { + cfg, err := protocol.CreateStateConfig(opts...) if err != nil { - return state.ErrStateNotExist + return 0, err } - return state.Deserialize(account, val) + val, err := cb.Get("state", cfg.Key) + if err != nil { + return 0, state.ErrStateNotExist + } + return 0, state.Deserialize(account, val) }).AnyTimes() sm.EXPECT().PutState(gomock.Any(), gomock.Any()).DoAndReturn( - func(addrHash hash.Hash160, account interface{}) error { + func(account interface{}, opts ...protocol.StateOption) (uint64, error) { + cfg, err := protocol.CreateStateConfig(opts...) + if err != nil { + return 0, err + } ss, err := state.Serialize(account) if err != nil { - return err + return 0, err } - cb.Put("state", addrHash[:], ss, "failed to put state") - return nil + cb.Put("state", cfg.Key, ss, "failed to put state") + return 0, nil }).AnyTimes() sm.EXPECT().Snapshot().Return(1).AnyTimes() r := types.NewElectionResultForTest(time.Now()) @@ -162,7 +170,8 @@ func TestCreateGenesisStates(t *testing.T) { require.NoError(err) require.NoError(p.CreateGenesisStates(ctx, sm)) var sc state.CandidateList - require.NoError(sm.State(candidatesutil.ConstructKey(1), &sc)) + _, err = sm.State(&sc, protocol.LegacyKeyOption(candidatesutil.ConstructKey(1))) + require.NoError(err) candidates, err := state.CandidatesToMap(sc) require.NoError(err) require.Equal(2, len(candidates)) @@ -243,7 +252,8 @@ func TestCreatePreStates(t *testing.T) { ) require.NoError(psc.CreatePreStates(ctx, sm)) bl := &vote.Blacklist{} - require.NoError(sm.State(candidatesutil.ConstructBlackListKey(epochNum+1), bl)) + _, err = sm.State(bl, protocol.LegacyKeyOption(candidatesutil.ConstructBlackListKey(epochNum+1))) + require.NoError(err) expected := test[epochNum+1] require.Equal(len(expected), len(bl.BlacklistInfos)) for addr, count := range bl.BlacklistInfos { @@ -284,7 +294,7 @@ func TestHandle(t *testing.T) { require.NoError(err) require.NoError(p2.CreateGenesisStates(ctx2, sm2)) var sc2 state.CandidateList - require.NoError(sm2.State(candidatesutil.ConstructKey(1), &sc2)) + _, err = sm2.State(&sc2, protocol.LegacyKeyOption(candidatesutil.ConstructKey(1))) act2 := action.NewPutPollResult(1, 1, sc2) elp = bd.SetGasLimit(uint64(100000)). SetGasPrice(big.NewInt(10)). @@ -333,7 +343,8 @@ func TestProtocol_Validate(t *testing.T) { require.NoError(err) require.NoError(p2.CreateGenesisStates(ctx2, sm2)) var sc2 state.CandidateList - require.NoError(sm2.State(candidatesutil.ConstructKey(1), &sc2)) + _, err = sm2.State(&sc2, protocol.LegacyKeyOption(candidatesutil.ConstructKey(1))) + require.NoError(err) act2 := action.NewPutPollResult(1, 1, sc2) elp = bd.SetGasLimit(uint64(100000)). SetGasPrice(big.NewInt(10)). @@ -363,7 +374,8 @@ func TestProtocol_Validate(t *testing.T) { require.NoError(err) require.NoError(p3.CreateGenesisStates(ctx3, sm3)) var sc3 state.CandidateList - require.NoError(sm3.State(candidatesutil.ConstructKey(1), &sc3)) + _, err = sm3.State(&sc3, protocol.LegacyKeyOption(candidatesutil.ConstructKey(1))) + require.NoError(err) sc3 = append(sc3, &state.Candidate{"1", big.NewInt(10), "2", nil}) sc3 = append(sc3, &state.Candidate{"1", big.NewInt(10), "2", nil}) act3 := action.NewPutPollResult(1, 1, sc3) @@ -394,7 +406,8 @@ func TestProtocol_Validate(t *testing.T) { require.NoError(err) require.NoError(p4.CreateGenesisStates(ctx4, sm4)) var sc4 state.CandidateList - require.NoError(sm4.State(candidatesutil.ConstructKey(1), &sc4)) + _, err = sm4.State(&sc4, protocol.LegacyKeyOption(candidatesutil.ConstructKey(1))) + require.NoError(err) sc4 = append(sc4, &state.Candidate{"1", big.NewInt(10), "2", nil}) act4 := action.NewPutPollResult(1, 1, sc4) bd4 := &action.EnvelopeBuilder{} @@ -424,7 +437,8 @@ func TestProtocol_Validate(t *testing.T) { require.NoError(err) require.NoError(p5.CreateGenesisStates(ctx5, sm5)) var sc5 state.CandidateList - require.NoError(sm5.State(candidatesutil.ConstructKey(1), &sc5)) + _, err = sm5.State(&sc5, protocol.LegacyKeyOption(candidatesutil.ConstructKey(1))) + require.NoError(err) sc5[0].Votes = big.NewInt(10) act5 := action.NewPutPollResult(1, 1, sc5) bd5 := &action.EnvelopeBuilder{} @@ -454,7 +468,8 @@ func TestProtocol_Validate(t *testing.T) { require.NoError(err) require.NoError(p6.CreateGenesisStates(ctx6, sm6)) var sc6 state.CandidateList - require.NoError(sm6.State(candidatesutil.ConstructKey(1), &sc6)) + _, err = sm6.State(&sc6, protocol.LegacyKeyOption(candidatesutil.ConstructKey(1))) + require.NoError(err) act6 := action.NewPutPollResult(1, 1, sc6) bd6 := &action.EnvelopeBuilder{} elp6 := bd6.SetGasLimit(uint64(100000)). diff --git a/action/protocol/poll/lifelong_protocol_test.go b/action/protocol/poll/lifelong_protocol_test.go index 12dfee6258..85bd4ec5eb 100644 --- a/action/protocol/poll/lifelong_protocol_test.go +++ b/action/protocol/poll/lifelong_protocol_test.go @@ -13,7 +13,6 @@ import ( "github.com/golang/mock/gomock" "github.com/stretchr/testify/require" - "github.com/iotexproject/go-pkgs/hash" "github.com/iotexproject/iotex-core/action/protocol" "github.com/iotexproject/iotex-core/config" "github.com/iotexproject/iotex-core/db/batch" @@ -45,21 +44,29 @@ func initLifeLongDelegateProtocol(ctrl *gomock.Controller) (Protocol, context.Co sm := mock_chainmanager.NewMockStateManager(ctrl) cb := batch.NewCachedBatch() sm.EXPECT().State(gomock.Any(), gomock.Any()).DoAndReturn( - func(addrHash hash.Hash160, account interface{}) error { - val, err := cb.Get("state", addrHash[:]) + func(account interface{}, opts ...protocol.StateOption) (uint64, error) { + cfg, err := protocol.CreateStateConfig(opts...) if err != nil { - return state.ErrStateNotExist + return 0, err } - return state.Deserialize(account, val) + val, err := cb.Get("state", cfg.Key) + if err != nil { + return 0, state.ErrStateNotExist + } + return 0, state.Deserialize(account, val) }).AnyTimes() sm.EXPECT().PutState(gomock.Any(), gomock.Any()).DoAndReturn( - func(addrHash hash.Hash160, account interface{}) error { + func(account interface{}, opts ...protocol.StateOption) (uint64, error) { + cfg, err := protocol.CreateStateConfig(opts...) + if err != nil { + return 0, err + } ss, err := state.Serialize(account) if err != nil { - return err + return 0, err } - cb.Put("state", addrHash[:], ss, "failed to put state") - return nil + cb.Put("state", cfg.Key, ss, "failed to put state") + return 0, nil }).AnyTimes() return p, ctx, sm, nil } diff --git a/action/protocol/poll/staking_committee_test.go b/action/protocol/poll/staking_committee_test.go index 0526d90b22..297b990fa3 100644 --- a/action/protocol/poll/staking_committee_test.go +++ b/action/protocol/poll/staking_committee_test.go @@ -65,21 +65,29 @@ func initConstructStakingCommittee(ctrl *gomock.Controller) (Protocol, context.C committee := mock_committee.NewMockCommittee(ctrl) cb := batch.NewCachedBatch() sm.EXPECT().State(gomock.Any(), gomock.Any()).DoAndReturn( - func(addrHash hash.Hash160, account interface{}) error { - val, err := cb.Get("state", addrHash[:]) + func(account interface{}, opts ...protocol.StateOption) (uint64, error) { + cfg, err := protocol.CreateStateConfig(opts...) if err != nil { - return state.ErrStateNotExist + return 0, err } - return state.Deserialize(account, val) + val, err := cb.Get("state", cfg.Key) + if err != nil { + return 0, state.ErrStateNotExist + } + return 0, state.Deserialize(account, val) }).AnyTimes() sm.EXPECT().PutState(gomock.Any(), gomock.Any()).DoAndReturn( - func(addrHash hash.Hash160, account interface{}) error { + func(account interface{}, opts ...protocol.StateOption) (uint64, error) { + cfg, err := protocol.CreateStateConfig(opts...) + if err != nil { + return 0, err + } ss, err := state.Serialize(account) if err != nil { - return err + return 0, err } - cb.Put("state", addrHash[:], ss, "failed to put state") - return nil + cb.Put("state", cfg.Key, ss, "failed to put state") + return 0, nil }).AnyTimes() sm.EXPECT().Snapshot().Return(1).AnyTimes() r := types.NewElectionResultForTest(time.Now()) @@ -130,7 +138,8 @@ func TestCreateGenesisStates_StakingCommittee(t *testing.T) { require.NoError(err) require.NoError(p.CreateGenesisStates(ctx, sm)) var candlist state.CandidateList - require.NoError(sm.State(candidatesutil.ConstructKey(1), &candlist)) + _, err = sm.State(&candlist, protocol.LegacyKeyOption(candidatesutil.ConstructKey(1))) + require.NoError(err) candidates, err := state.CandidatesToMap(candlist) require.NoError(err) require.Equal(2, len(candidates)) @@ -200,7 +209,8 @@ func TestHandle_StakingCommittee(t *testing.T) { require.NoError(err) require.NoError(p2.CreateGenesisStates(ctx2, sm2)) var sc2 state.CandidateList - require.NoError(sm2.State(candidatesutil.ConstructKey(1), &sc2)) + _, err = sm2.State(&sc2, protocol.LegacyKeyOption(candidatesutil.ConstructKey(1))) + require.NoError(err) act2 := action.NewPutPollResult(1, 1, sc2) elp = bd.SetGasLimit(uint64(100000)). SetGasPrice(big.NewInt(10)). @@ -249,7 +259,8 @@ func TestProtocol_Validate_StakingCommittee(t *testing.T) { require.NoError(err) require.NoError(p2.CreateGenesisStates(ctx2, sm2)) var sc2 state.CandidateList - require.NoError(sm2.State(candidatesutil.ConstructKey(1), &sc2)) + _, err = sm2.State(&sc2, protocol.LegacyKeyOption(candidatesutil.ConstructKey(1))) + require.NoError(err) act2 := action.NewPutPollResult(1, 1, sc2) elp = bd.SetGasLimit(uint64(100000)). SetGasPrice(big.NewInt(10)). @@ -279,7 +290,8 @@ func TestProtocol_Validate_StakingCommittee(t *testing.T) { require.NoError(err) require.NoError(p3.CreateGenesisStates(ctx3, sm3)) var sc3 state.CandidateList - require.NoError(sm3.State(candidatesutil.ConstructKey(1), &sc3)) + _, err = sm3.State(&sc3, protocol.LegacyKeyOption(candidatesutil.ConstructKey(1))) + require.NoError(err) sc3 = append(sc3, &state.Candidate{"1", big.NewInt(10), "2", nil}) sc3 = append(sc3, &state.Candidate{"1", big.NewInt(10), "2", nil}) act3 := action.NewPutPollResult(1, 1, sc3) @@ -310,7 +322,8 @@ func TestProtocol_Validate_StakingCommittee(t *testing.T) { require.NoError(err) require.NoError(p4.CreateGenesisStates(ctx4, sm4)) var sc4 state.CandidateList - require.NoError(sm4.State(candidatesutil.ConstructKey(1), &sc4)) + _, err = sm4.State(&sc4, protocol.LegacyKeyOption(candidatesutil.ConstructKey(1))) + require.NoError(err) sc4 = append(sc4, &state.Candidate{"1", big.NewInt(10), "2", nil}) act4 := action.NewPutPollResult(1, 1, sc4) bd4 := &action.EnvelopeBuilder{} @@ -340,7 +353,7 @@ func TestProtocol_Validate_StakingCommittee(t *testing.T) { require.NoError(err) require.NoError(p5.CreateGenesisStates(ctx5, sm5)) var sc5 state.CandidateList - require.NoError(sm5.State(candidatesutil.ConstructKey(1), &sc5)) + _, err = sm5.State(&sc5, protocol.LegacyKeyOption(candidatesutil.ConstructKey(1))) sc5[0].Votes = big.NewInt(10) act5 := action.NewPutPollResult(1, 1, sc5) bd5 := &action.EnvelopeBuilder{} @@ -370,7 +383,8 @@ func TestProtocol_Validate_StakingCommittee(t *testing.T) { require.NoError(err) require.NoError(p6.CreateGenesisStates(ctx6, sm6)) var sc6 state.CandidateList - require.NoError(sm6.State(candidatesutil.ConstructKey(1), &sc6)) + _, err = sm6.State(&sc6, protocol.LegacyKeyOption(candidatesutil.ConstructKey(1))) + require.NoError(err) act6 := action.NewPutPollResult(1, 1, sc6) bd6 := &action.EnvelopeBuilder{} elp6 := bd6.SetGasLimit(uint64(100000)). diff --git a/action/protocol/poll/util.go b/action/protocol/poll/util.go index 8816002720..6bf5812471 100644 --- a/action/protocol/poll/util.go +++ b/action/protocol/poll/util.go @@ -170,7 +170,8 @@ func setCandidates( zap.String("score", candidate.Votes.String()), ) } - return sm.PutState(candidatesutil.ConstructKey(height), &candidates) + _, err := sm.PutState(&candidates, protocol.LegacyKeyOption(candidatesutil.ConstructKey(height))) + return err } // setKickoutBlackList sets the blacklist for kick-out for corresponding epoch @@ -180,5 +181,6 @@ func setKickoutBlackList( epochNum uint64, ) error { blackListKey := candidatesutil.ConstructBlackListKey(epochNum) - return sm.PutState(blackListKey, blackList) + _, err := sm.PutState(blackList, protocol.LegacyKeyOption(blackListKey)) + return err } diff --git a/action/protocol/rewarding/protocol.go b/action/protocol/rewarding/protocol.go index c3bc583a20..9ef421cffe 100644 --- a/action/protocol/rewarding/protocol.go +++ b/action/protocol/rewarding/protocol.go @@ -244,17 +244,20 @@ func (p *Protocol) ForceRegister(r *protocol.Registry) error { func (p *Protocol) state(sm protocol.StateReader, key []byte, value interface{}) error { keyHash := hash.Hash160b(append(p.keyPrefix, key...)) - return sm.State(keyHash, value) + _, err := sm.State(value, protocol.LegacyKeyOption(keyHash)) + return err } func (p *Protocol) putState(sm protocol.StateManager, key []byte, value interface{}) error { keyHash := hash.Hash160b(append(p.keyPrefix, key...)) - return sm.PutState(keyHash, value) + _, err := sm.PutState(value, protocol.LegacyKeyOption(keyHash)) + return err } func (p *Protocol) deleteState(sm protocol.StateManager, key []byte) error { keyHash := hash.Hash160b(append(p.keyPrefix, key...)) - return sm.DelState(keyHash) + _, err := sm.DelState(protocol.LegacyKeyOption(keyHash)) + return err } func (p *Protocol) settleAction( diff --git a/action/protocol/rewarding/protocol_test.go b/action/protocol/rewarding/protocol_test.go index ec1eb961b4..194074b13b 100644 --- a/action/protocol/rewarding/protocol_test.go +++ b/action/protocol/rewarding/protocol_test.go @@ -40,21 +40,29 @@ func testProtocol(t *testing.T, test func(*testing.T, context.Context, protocol. cb := batch.NewCachedBatch() sm.EXPECT().State(gomock.Any(), gomock.Any()).DoAndReturn( - func(addrHash hash.Hash160, account interface{}) error { - val, err := cb.Get("state", addrHash[:]) + func(account interface{}, opts ...protocol.StateOption) (uint64, error) { + cfg, err := protocol.CreateStateConfig(opts...) if err != nil { - return state.ErrStateNotExist + return 0, err } - return state.Deserialize(account, val) + val, err := cb.Get("state", cfg.Key) + if err != nil { + return 0, state.ErrStateNotExist + } + return 0, state.Deserialize(account, val) }).AnyTimes() sm.EXPECT().PutState(gomock.Any(), gomock.Any()).DoAndReturn( - func(addrHash hash.Hash160, account interface{}) error { + func(account interface{}, opts ...protocol.StateOption) (uint64, error) { + cfg, err := protocol.CreateStateConfig(opts...) + if err != nil { + return 0, err + } ss, err := state.Serialize(account) if err != nil { - return err + return 0, err } - cb.Put("state", addrHash[:], ss, "failed to put state") - return nil + cb.Put("state", cfg.Key, ss, "failed to put state") + return 0, nil }).AnyTimes() rp := rolldpos.NewProtocol( @@ -207,21 +215,29 @@ func TestProtocol_Handle(t *testing.T) { sm := mock_chainmanager.NewMockStateManager(ctrl) cb := batch.NewCachedBatch() sm.EXPECT().State(gomock.Any(), gomock.Any()).DoAndReturn( - func(addrHash hash.Hash160, account interface{}) error { - val, err := cb.Get("state", addrHash[:]) + func(account interface{}, opts ...protocol.StateOption) (uint64, error) { + cfg, err := protocol.CreateStateConfig(opts...) if err != nil { - return state.ErrStateNotExist + return 0, err } - return state.Deserialize(account, val) + val, err := cb.Get("state", cfg.Key) + if err != nil { + return 0, state.ErrStateNotExist + } + return 0, state.Deserialize(account, val) }).AnyTimes() sm.EXPECT().PutState(gomock.Any(), gomock.Any()).DoAndReturn( - func(addrHash hash.Hash160, account interface{}) error { + func(account interface{}, opts ...protocol.StateOption) (uint64, error) { + cfg, err := protocol.CreateStateConfig(opts...) + if err != nil { + return 0, err + } ss, err := state.Serialize(account) if err != nil { - return err + return 0, err } - cb.Put("state", addrHash[:], ss, "failed to put state") - return nil + cb.Put("state", cfg.Key, ss, "failed to put state") + return 0, nil }).AnyTimes() sm.EXPECT().Snapshot().Return(1).AnyTimes() sm.EXPECT().Revert(gomock.Any()).Return(nil).AnyTimes() diff --git a/action/protocol/rewarding/reward_test.go b/action/protocol/rewarding/reward_test.go index a5ad7d5aba..e1b4460c99 100644 --- a/action/protocol/rewarding/reward_test.go +++ b/action/protocol/rewarding/reward_test.go @@ -279,21 +279,29 @@ func TestProtocol_NoRewardAddr(t *testing.T) { sm := mock_chainmanager.NewMockStateManager(ctrl) cb := batch.NewCachedBatch() sm.EXPECT().State(gomock.Any(), gomock.Any()).DoAndReturn( - func(addrHash hash.Hash160, account interface{}) error { - val, err := cb.Get("state", addrHash[:]) + func(account interface{}, opts ...protocol.StateOption) (uint64, error) { + cfg, err := protocol.CreateStateConfig(opts...) if err != nil { - return state.ErrStateNotExist + return 0, err } - return state.Deserialize(account, val) + val, err := cb.Get("state", cfg.Key) + if err != nil { + return 0, state.ErrStateNotExist + } + return 0, state.Deserialize(account, val) }).AnyTimes() sm.EXPECT().PutState(gomock.Any(), gomock.Any()).DoAndReturn( - func(addrHash hash.Hash160, account interface{}) error { + func(account interface{}, opts ...protocol.StateOption) (uint64, error) { + cfg, err := protocol.CreateStateConfig(opts...) + if err != nil { + return 0, err + } ss, err := state.Serialize(account) if err != nil { - return err + return 0, err } - cb.Put("state", addrHash[:], ss, "failed to put state") - return nil + cb.Put("state", cfg.Key, ss, "failed to put state") + return 0, nil }).AnyTimes() p := NewProtocol( diff --git a/action/protocol/vote/candidatesutil/candidatesutil.go b/action/protocol/vote/candidatesutil/candidatesutil.go index da1fdb9059..daadd7add9 100644 --- a/action/protocol/vote/candidatesutil/candidatesutil.go +++ b/action/protocol/vote/candidatesutil/candidatesutil.go @@ -33,7 +33,7 @@ func CandidatesByHeight(sr protocol.StateReader, height uint64) ([]*state.Candid var candidates state.CandidateList // Load Candidates on the given height from underlying db candidatesKey := ConstructKey(height) - err := sr.State(candidatesKey, &candidates) + _, err := sr.State(&candidates, protocol.LegacyKeyOption(candidatesKey)) log.L().Debug( "CandidatesByHeight", zap.Uint64("height", height), @@ -61,7 +61,7 @@ func KickoutListByEpoch(sr protocol.StateReader, epochNum uint64) (*vote.Blackli } // Load kick out list on the given epochNum from underlying db blackListKey := ConstructBlackListKey(epochNum) - err := sr.State(blackListKey, blackList) + _, err := sr.State(blackList, protocol.LegacyKeyOption(blackListKey)) log.L().Debug( "KickoutListByEpoch", zap.Uint64("epoch number", epochNum), @@ -96,7 +96,7 @@ func GetMostRecentCandidateMap(sm protocol.StateManager, blkHeight uint64) (map[ for h := int(blkHeight); h >= 0; h-- { candidatesKey := ConstructKey(uint64(h)) var err error - if err = sm.State(candidatesKey, &sc); err == nil { + if _, err = sm.State(&sc, protocol.LegacyKeyOption(candidatesKey)); err == nil { return state.CandidatesToMap(sc) } if errors.Cause(err) != state.ErrStateNotExist { @@ -149,5 +149,6 @@ func storeCandidates(candidateMap map[hash.Hash160]*state.Candidate, sm protocol } sort.Sort(candidateList) candidatesKey := ConstructKey(blkHeight) - return sm.PutState(candidatesKey, &candidateList) + _, err = sm.PutState(&candidateList, protocol.LegacyKeyOption(candidatesKey)) + return err } diff --git a/actpool/actqueue_test.go b/actpool/actqueue_test.go index 968d4c09b4..4cef07f208 100644 --- a/actpool/actqueue_test.go +++ b/actpool/actqueue_test.go @@ -17,9 +17,8 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/iotexproject/go-pkgs/hash" - "github.com/iotexproject/iotex-core/action" + "github.com/iotexproject/iotex-core/action/protocol" "github.com/iotexproject/iotex-core/config" "github.com/iotexproject/iotex-core/state" "github.com/iotexproject/iotex-core/test/identityset" @@ -126,9 +125,9 @@ func TestActQueuePendingActs(t *testing.T) { require := require.New(t) cfg := config.Default sf := mock_factory.NewMockFactory(ctrl) - sf.EXPECT().State(gomock.Any(), gomock.Any()).Do(func(_ hash.Hash160, accountState *state.Account) { + sf.EXPECT().State(gomock.Any(), gomock.Any()).Do(func(accountState *state.Account, _ protocol.StateOption) { accountState.Nonce = uint64(1) - }).Return(nil).Times(1) + }).Return(uint64(0), nil).Times(1) ap, err := NewActPool(sf, cfg.ActPool, EnableExperimentalActions()) require.NoError(err) q := NewActQueue(ap.(*actPool), identityset.Address(0).String()).(*actQueue) diff --git a/api/api_test.go b/api/api_test.go index 75276157ad..742cecb713 100644 --- a/api/api_test.go +++ b/api/api_test.go @@ -1348,8 +1348,8 @@ func TestServer_ReadActiveBlockProducersByEpoch(t *testing.T) { defer ctrl.Finish() committee := mock_committee.NewMockCommittee(ctrl) sm := mock_chainmanager.NewMockStateManager(ctrl) - sm.EXPECT().State(gomock.Any(), gomock.Any()).Return(state.ErrStateNotExist).AnyTimes() - sm.EXPECT().PutState(gomock.Any(), gomock.Any()).Return(nil).AnyTimes() + sm.EXPECT().State(gomock.Any(), gomock.Any()).Return(uint64(0), state.ErrStateNotExist).AnyTimes() + sm.EXPECT().PutState(gomock.Any(), gomock.Any()).Return(uint64(0), nil).AnyTimes() candidates := []*state.Candidate{ { Address: "address1", @@ -1444,8 +1444,8 @@ func TestServer_GetEpochMeta(t *testing.T) { defer ctrl.Finish() sm := mock_chainmanager.NewMockStateManager(ctrl) - sm.EXPECT().State(gomock.Any(), gomock.Any()).Return(state.ErrStateNotExist).AnyTimes() - sm.EXPECT().PutState(gomock.Any(), gomock.Any()).Return(nil).AnyTimes() + sm.EXPECT().State(gomock.Any(), gomock.Any()).Return(uint64(0), state.ErrStateNotExist).AnyTimes() + sm.EXPECT().PutState(gomock.Any(), gomock.Any()).Return(uint64(0), nil).AnyTimes() svr, err := createServer(cfg, false) require.NoError(err) for _, test := range getEpochMetaTests { diff --git a/e2etest/staking_test.go b/e2etest/staking_test.go index 923b9e7500..200dfda508 100644 --- a/e2etest/staking_test.go +++ b/e2etest/staking_test.go @@ -56,7 +56,8 @@ func TestStakingContract(t *testing.T) { addr0 := identityset.Address(26).String() state0 := hash.BytesToHash160(identityset.Address(26).Bytes()) var s state.Account - require.NoError(sf.State(state0, &s)) + _, err = sf.State(&s, protocol.LegacyKeyOption(state0)) + require.NoError(err) require.Equal(unit.ConvertIotxToRau(100000000), s.Balance) // deploy staking contract @@ -108,7 +109,8 @@ func TestStakingContract(t *testing.T) { require.NoError(bc.CommitBlock(blk)) state0 = hash.BytesToHash160(identityset.Address(i).Bytes()) - require.NoError(sf.State(state0, &s)) + _, err = sf.State(&s, protocol.LegacyKeyOption(state0)) + require.NoError(err) require.Equal(unit.ConvertIotxToRau(100000000-int64(numBucket)*200), s.Balance) } diff --git a/state/factory/factory.go b/state/factory/factory.go index dd1db8686d..6147b855b8 100644 --- a/state/factory/factory.go +++ b/state/factory/factory.go @@ -369,17 +369,17 @@ func (sf *factory) Commit(ctx context.Context, blk *block.Block) error { } // State returns a confirmed state in the state factory -func (sf *factory) State(addr hash.Hash160, state interface{}, opts ...protocol.StateOption) error { +func (sf *factory) State(state interface{}, opts ...protocol.StateOption) (uint64, error) { sf.mutex.RLock() defer sf.mutex.RUnlock() cfg, err := protocol.CreateStateConfig(opts...) if err != nil { - return err + return 0, err } if cfg.AtHeight { - return sf.stateAtHeight(cfg.Height, addr, state) + return sf.currentChainHeight, sf.stateAtHeight(cfg.Height, cfg.Key, state) } - return sf.state(addr, state) + return sf.currentChainHeight, sf.state(cfg.Key, state) } // DeleteWorkingSet returns true if it remove ws from workingsets cache successfully @@ -400,8 +400,8 @@ func (sf *factory) rootHash() hash.Hash256 { return hash.BytesToHash256(sf.accountTrie.RootHash()) } -func (sf *factory) state(addr hash.Hash160, s interface{}) error { - data, err := sf.accountTrie.Get(addr[:]) +func (sf *factory) state(addr []byte, s interface{}) error { + data, err := sf.accountTrie.Get(addr) if err != nil { if errors.Cause(err) == trie.ErrNotExist { return errors.Wrapf(state.ErrStateNotExist, "state of %x doesn't exist", addr) @@ -414,7 +414,7 @@ func (sf *factory) state(addr hash.Hash160, s interface{}) error { return nil } -func (sf *factory) stateAtHeight(height uint64, addr hash.Hash160, s interface{}) error { +func (sf *factory) stateAtHeight(height uint64, addr []byte, s interface{}) error { if !sf.saveHistory { return ErrNoArchiveData } @@ -436,9 +436,9 @@ func (sf *factory) stateAtHeight(height uint64, addr hash.Hash160, s interface{} return err } defer tr.Stop(context.Background()) - mstate, err := tr.Get(addr[:]) + mstate, err := tr.Get(addr) if errors.Cause(err) == trie.ErrNotExist { - return errors.Wrapf(state.ErrStateNotExist, "addrHash = %x", addr[:]) + return errors.Wrapf(state.ErrStateNotExist, "addrHash = %x", addr) } if err != nil { return errors.Wrapf(err, "failed to get account of %x", addr) diff --git a/state/factory/factory_test.go b/state/factory/factory_test.go index 5c91483dc3..61199fd758 100644 --- a/state/factory/factory_test.go +++ b/state/factory/factory_test.go @@ -130,10 +130,12 @@ func testRevert(ws WorkingSet, t *testing.T) { s.Balance.Add(s.Balance, big.NewInt(5)) require.Equal(big.NewInt(10), s.Balance) - require.NoError(ws.PutState(sHash, s)) + _, err = ws.PutState(s, protocol.LegacyKeyOption(sHash)) + require.NoError(err) require.NoError(ws.Revert(s0)) - require.NoError(ws.State(sHash, s)) + _, err = ws.State(s, protocol.LegacyKeyOption(sHash)) + require.NoError(err) require.Equal(big.NewInt(5), s.Balance) } @@ -149,10 +151,12 @@ func testSDBRevert(ws WorkingSet, t *testing.T) { s.Balance.Add(s.Balance, big.NewInt(5)) require.Equal(big.NewInt(10), s.Balance) - require.NoError(ws.PutState(sHash, s)) + _, err = ws.PutState(s, protocol.LegacyKeyOption(sHash)) + require.NoError(err) require.NoError(ws.Revert(s0)) - require.NoError(ws.State(sHash, s)) + _, err = ws.State(s, protocol.LegacyKeyOption(sHash)) + require.NoError(err) require.Equal(big.NewInt(5), s.Balance) } @@ -171,12 +175,14 @@ func testSnapshot(ws WorkingSet, t *testing.T) { require.Zero(s0) s.Balance.Add(s.Balance, big.NewInt(5)) require.Equal(big.NewInt(10), s.Balance) - require.NoError(ws.PutState(sHash, s)) + _, err = ws.PutState(s, protocol.LegacyKeyOption(sHash)) + require.NoError(err) s1 := ws.Snapshot() require.Equal(1, s1) s.Balance.Add(s.Balance, big.NewInt(5)) require.Equal(big.NewInt(15), s.Balance) - require.NoError(ws.PutState(sHash, s)) + _, err = ws.PutState(s, protocol.LegacyKeyOption(sHash)) + require.NoError(err) s, err = accountutil.LoadAccount(ws, tHash) require.NoError(err) @@ -185,18 +191,23 @@ func testSnapshot(ws WorkingSet, t *testing.T) { require.Equal(2, s2) require.NoError(s.AddBalance(big.NewInt(6))) require.Equal(big.NewInt(13), s.Balance) - require.NoError(ws.PutState(tHash, s)) + _, err = ws.PutState(s, protocol.LegacyKeyOption(tHash)) + require.NoError(err) require.NoError(ws.Revert(s2)) - require.NoError(ws.State(sHash, s)) + _, err = ws.State(s, protocol.LegacyKeyOption(sHash)) + require.NoError(err) require.Equal(big.NewInt(15), s.Balance) - require.NoError(ws.State(tHash, s)) + _, err = ws.State(s, protocol.LegacyKeyOption(tHash)) + require.NoError(err) require.Equal(big.NewInt(7), s.Balance) require.NoError(ws.Revert(s1)) - require.NoError(ws.State(sHash, s)) + _, err = ws.State(s, protocol.LegacyKeyOption(sHash)) + require.NoError(err) require.Equal(big.NewInt(10), s.Balance) require.NoError(ws.Revert(s0)) - require.NoError(ws.State(sHash, s)) + _, err = ws.State(s, protocol.LegacyKeyOption(sHash)) + require.NoError(err) require.Equal(big.NewInt(5), s.Balance) } @@ -405,7 +416,7 @@ func testState(sf Factory, t *testing.T) { accountA, err := accountutil.AccountState(sf, a) require.NoError(t, err) sHash := hash.BytesToHash160(identityset.Address(28).Bytes()) - err = sf.State(sHash, &testAccount) + _, err = sf.State(&testAccount, protocol.LegacyKeyOption(sHash)) require.NoError(t, err) require.Equal(t, accountA, &testAccount) require.Equal(t, big.NewInt(90), accountA.Balance) @@ -988,20 +999,22 @@ func testCachedBatch(ws WorkingSet, t *testing.T) { hashA := hash.BytesToHash160(identityset.Address(28).Bytes()) accountA := state.EmptyAccount() accountA.Balance = big.NewInt(70) - err := ws.PutState(hashA, accountA) + _, err := ws.PutState(accountA, protocol.LegacyKeyOption(hashA)) require.NoError(err) // test State() testAccount := state.EmptyAccount() - require.NoError(ws.State(hashA, &testAccount)) + _, err = ws.State(&testAccount, protocol.LegacyKeyOption(hashA)) + require.NoError(err) require.Equal(accountA, testAccount) // test DelState() - err = ws.DelState(hashA) + _, err = ws.DelState(protocol.LegacyKeyOption(hashA)) require.NoError(err) // can't state account "alfa" anymore - require.Error(ws.State(hashA, &testAccount)) + _, err = ws.State(&testAccount, protocol.LegacyKeyOption(hashA)) + require.Error(err) } func TestGetDB(t *testing.T) { @@ -1034,10 +1047,14 @@ func TestDeleteAndPutSameKey(t *testing.T) { acc := state.Account{ Nonce: 1, } - require.NoError(t, ws.PutState(key, acc)) - require.NoError(t, ws.DelState(key)) - require.Equal(t, state.ErrStateNotExist, errors.Cause(ws.State(key, &acc))) - require.Equal(t, state.ErrStateNotExist, errors.Cause(ws.State(hash.Hash160b([]byte("other")), &acc))) + _, err := ws.PutState(acc, protocol.LegacyKeyOption(key)) + require.NoError(t, err) + _, err = ws.DelState(protocol.LegacyKeyOption(key)) + require.NoError(t, err) + _, err = ws.State(&acc, protocol.LegacyKeyOption(key)) + require.Equal(t, state.ErrStateNotExist, errors.Cause(err)) + _, err = ws.State(&acc, protocol.LegacyKeyOption(hash.Hash160b([]byte("other")))) + require.Equal(t, state.ErrStateNotExist, errors.Cause(err)) } t.Run("workingSet", func(t *testing.T) { sf, err := NewFactory(config.Default, InMemTrieOption()) diff --git a/state/factory/statedb.go b/state/factory/statedb.go index daf92faaed..8e562e32f9 100644 --- a/state/factory/statedb.go +++ b/state/factory/statedb.go @@ -269,23 +269,23 @@ func (sdb *stateDB) Commit(ctx context.Context, blk *block.Block) error { } // State returns a confirmed state in the state factory -func (sdb *stateDB) State(addr hash.Hash160, state interface{}, opts ...protocol.StateOption) error { +func (sdb *stateDB) State(state interface{}, opts ...protocol.StateOption) (uint64, error) { sdb.mutex.Lock() defer sdb.mutex.Unlock() cfg, err := protocol.CreateStateConfig(opts...) if err != nil { - return err + return 0, err } if cfg.AtHeight { - return ErrNotSupported + return 0, ErrNotSupported } ns := AccountKVNamespace if cfg.Namespace != "" { ns = cfg.Namespace } - return sdb.state(ns, addr, state) + return sdb.currentChainHeight, sdb.state(ns, cfg.Key, state) } // DeleteWorkingSet returns true if it remove ws from workingsets cache successfully @@ -321,8 +321,8 @@ func (sdb *stateDB) flusherOptions(ctx context.Context, height uint64) []db.KVSt ) } -func (sdb *stateDB) state(ns string, addr hash.Hash160, s interface{}) error { - data, err := sdb.dao.Get(ns, addr[:]) +func (sdb *stateDB) state(ns string, addr []byte, s interface{}) error { + data, err := sdb.dao.Get(ns, addr) if err != nil { if errors.Cause(err) == db.ErrNotExist { return errors.Wrapf(state.ErrStateNotExist, "state of %x doesn't exist", addr) diff --git a/state/factory/statetx.go b/state/factory/statetx.go index 3d65cf9c0a..0ef308f8c6 100644 --- a/state/factory/statetx.go +++ b/state/factory/statetx.go @@ -194,36 +194,36 @@ func (stx *stateTX) GetDB() db.KVStore { } // State pulls a state from DB -func (stx *stateTX) State(hash hash.Hash160, s interface{}, opts ...protocol.StateOption) error { +func (stx *stateTX) State(s interface{}, opts ...protocol.StateOption) (uint64, error) { stateDBMtc.WithLabelValues("get").Inc() cfg, err := protocol.CreateStateConfig(opts...) if err != nil { - return err + return 0, err } if cfg.AtHeight { - return ErrNotSupported + return 0, ErrNotSupported } ns := AccountKVNamespace if cfg.Namespace != "" { ns = cfg.Namespace } - mstate, err := stx.flusher.KVStoreWithBuffer().Get(ns, hash[:]) + mstate, err := stx.flusher.KVStoreWithBuffer().Get(ns, cfg.Key) switch errors.Cause(err) { case db.ErrNotExist: - return errors.Wrapf(state.ErrStateNotExist, "k = %x doesn't exist", hash) + return 0, errors.Wrapf(state.ErrStateNotExist, "k = %x doesn't exist", cfg.Key) case nil: - return state.Deserialize(s, mstate) + return stx.blockHeight, state.Deserialize(s, mstate) } - return errors.Wrapf(err, "failed to get account of %x", hash) + return 0, errors.Wrapf(err, "failed to get account of %x", cfg.Key) } // PutState puts a state into DB -func (stx *stateTX) PutState(pkHash hash.Hash160, s interface{}, opts ...protocol.StateOption) error { +func (stx *stateTX) PutState(s interface{}, opts ...protocol.StateOption) (uint64, error) { stateDBMtc.WithLabelValues("put").Inc() cfg, err := protocol.CreateStateConfig(opts...) if err != nil { - return err + return 0, err } ns := AccountKVNamespace @@ -233,26 +233,26 @@ func (stx *stateTX) PutState(pkHash hash.Hash160, s interface{}, opts ...protoco ss, err := state.Serialize(s) if err != nil { - return errors.Wrapf(err, "failed to convert account %v to bytes", s) + return 0, errors.Wrapf(err, "failed to convert account %v to bytes", s) } - stx.flusher.KVStoreWithBuffer().MustPut(ns, pkHash[:], ss) + stx.flusher.KVStoreWithBuffer().MustPut(ns, cfg.Key, ss) - return nil + return stx.blockHeight, nil } // DelState deletes a state from DB -func (stx *stateTX) DelState(pkHash hash.Hash160, opts ...protocol.StateOption) error { +func (stx *stateTX) DelState(opts ...protocol.StateOption) (uint64, error) { cfg, err := protocol.CreateStateConfig(opts...) if err != nil { - return err + return 0, err } ns := AccountKVNamespace if cfg.Namespace != "" { ns = cfg.Namespace } - stx.flusher.KVStoreWithBuffer().MustDelete(ns, pkHash[:]) + stx.flusher.KVStoreWithBuffer().MustDelete(ns, cfg.Key) - return nil + return stx.blockHeight, nil } diff --git a/state/factory/workingset.go b/state/factory/workingset.go index 4684a5dc2c..986d0eaa79 100644 --- a/state/factory/workingset.go +++ b/state/factory/workingset.go @@ -264,43 +264,51 @@ func (ws *workingSet) GetDB() db.KVStore { } // State pulls a state from DB -func (ws *workingSet) State(hash hash.Hash160, s interface{}, opts ...protocol.StateOption) error { +func (ws *workingSet) State(s interface{}, opts ...protocol.StateOption) (uint64, error) { cfg, err := protocol.CreateStateConfig(opts...) if err != nil { - return err + return 0, err } if cfg.AtHeight { - return ErrNotSupported + return 0, ErrNotSupported } stateDBMtc.WithLabelValues("get").Inc() - mstate, err := ws.accountTrie.Get(hash[:]) + mstate, err := ws.accountTrie.Get(cfg.Key) if errors.Cause(err) == trie.ErrNotExist { - return errors.Wrapf(state.ErrStateNotExist, "addrHash = %x", hash[:]) + return 0, errors.Wrapf(state.ErrStateNotExist, "addrHash = %x", cfg.Key) } if err != nil { - return errors.Wrapf(err, "failed to get account of %x", hash) + return 0, errors.Wrapf(err, "failed to get account of %x", cfg.Key) } - return state.Deserialize(s, mstate) + return ws.blockHeight, state.Deserialize(s, mstate) } // PutState puts a state into DB -func (ws *workingSet) PutState(pkHash hash.Hash160, s interface{}, opts ...protocol.StateOption) error { +func (ws *workingSet) PutState(s interface{}, opts ...protocol.StateOption) (uint64, error) { stateDBMtc.WithLabelValues("put").Inc() + cfg, err := protocol.CreateStateConfig(opts...) + if err != nil { + return 0, err + } ss, err := state.Serialize(s) if err != nil { - return errors.Wrapf(err, "failed to convert account %v to bytes", s) + return 0, errors.Wrapf(err, "failed to convert account %v to bytes", s) } - ws.flusher.KVStoreWithBuffer().MustPut(AccountKVNamespace, pkHash[:], ss) + ws.flusher.KVStoreWithBuffer().MustPut(AccountKVNamespace, cfg.Key, ss) - return ws.accountTrie.Upsert(pkHash[:], ss) + return ws.blockHeight, ws.accountTrie.Upsert(cfg.Key, ss) } // DelState deletes a state from DB -func (ws *workingSet) DelState(pkHash hash.Hash160, opts ...protocol.StateOption) error { - ws.flusher.KVStoreWithBuffer().MustDelete(AccountKVNamespace, pkHash[:]) +func (ws *workingSet) DelState(opts ...protocol.StateOption) (uint64, error) { + cfg, err := protocol.CreateStateConfig(opts...) + if err != nil { + return 0, err + } + ws.flusher.KVStoreWithBuffer().MustDelete(AccountKVNamespace, cfg.Key) - return ws.accountTrie.Delete(pkHash[:]) + return ws.blockHeight, ws.accountTrie.Delete(cfg.Key) } // clearCache removes all local changes after committing to trie diff --git a/test/mock/mock_chainmanager/mock_chainmanager.go b/test/mock/mock_chainmanager/mock_chainmanager.go index 589948430a..e98716a0ae 100644 --- a/test/mock/mock_chainmanager/mock_chainmanager.go +++ b/test/mock/mock_chainmanager/mock_chainmanager.go @@ -6,7 +6,6 @@ package mock_chainmanager import ( gomock "github.com/golang/mock/gomock" - hash "github.com/iotexproject/go-pkgs/hash" protocol "github.com/iotexproject/iotex-core/action/protocol" db "github.com/iotexproject/iotex-core/db" reflect "reflect" @@ -51,21 +50,22 @@ func (mr *MockStateReaderMockRecorder) Height() *gomock.Call { } // State mocks base method -func (m *MockStateReader) State(arg0 hash.Hash160, arg1 interface{}, arg2 ...protocol.StateOption) error { +func (m *MockStateReader) State(arg0 interface{}, arg1 ...protocol.StateOption) (uint64, error) { m.ctrl.T.Helper() - varargs := []interface{}{arg0, arg1} - for _, a := range arg2 { + varargs := []interface{}{arg0} + for _, a := range arg1 { varargs = append(varargs, a) } ret := m.ctrl.Call(m, "State", varargs...) - ret0, _ := ret[0].(error) - return ret0 + ret0, _ := ret[0].(uint64) + ret1, _ := ret[1].(error) + return ret0, ret1 } // State indicates an expected call of State -func (mr *MockStateReaderMockRecorder) State(arg0, arg1 interface{}, arg2 ...interface{}) *gomock.Call { +func (mr *MockStateReaderMockRecorder) State(arg0 interface{}, arg1 ...interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - varargs := append([]interface{}{arg0, arg1}, arg2...) + varargs := append([]interface{}{arg0}, arg1...) return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "State", reflect.TypeOf((*MockStateReader)(nil).State), varargs...) } @@ -108,21 +108,22 @@ func (mr *MockStateManagerMockRecorder) Height() *gomock.Call { } // State mocks base method -func (m *MockStateManager) State(arg0 hash.Hash160, arg1 interface{}, arg2 ...protocol.StateOption) error { +func (m *MockStateManager) State(arg0 interface{}, arg1 ...protocol.StateOption) (uint64, error) { m.ctrl.T.Helper() - varargs := []interface{}{arg0, arg1} - for _, a := range arg2 { + varargs := []interface{}{arg0} + for _, a := range arg1 { varargs = append(varargs, a) } ret := m.ctrl.Call(m, "State", varargs...) - ret0, _ := ret[0].(error) - return ret0 + ret0, _ := ret[0].(uint64) + ret1, _ := ret[1].(error) + return ret0, ret1 } // State indicates an expected call of State -func (mr *MockStateManagerMockRecorder) State(arg0, arg1 interface{}, arg2 ...interface{}) *gomock.Call { +func (mr *MockStateManagerMockRecorder) State(arg0 interface{}, arg1 ...interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - varargs := append([]interface{}{arg0, arg1}, arg2...) + varargs := append([]interface{}{arg0}, arg1...) return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "State", reflect.TypeOf((*MockStateManager)(nil).State), varargs...) } @@ -155,41 +156,42 @@ func (mr *MockStateManagerMockRecorder) Revert(arg0 interface{}) *gomock.Call { } // PutState mocks base method -func (m *MockStateManager) PutState(arg0 hash.Hash160, arg1 interface{}, arg2 ...protocol.StateOption) error { +func (m *MockStateManager) PutState(arg0 interface{}, arg1 ...protocol.StateOption) (uint64, error) { m.ctrl.T.Helper() - varargs := []interface{}{arg0, arg1} - for _, a := range arg2 { + varargs := []interface{}{arg0} + for _, a := range arg1 { varargs = append(varargs, a) } ret := m.ctrl.Call(m, "PutState", varargs...) - ret0, _ := ret[0].(error) - return ret0 + ret0, _ := ret[0].(uint64) + ret1, _ := ret[1].(error) + return ret0, ret1 } // PutState indicates an expected call of PutState -func (mr *MockStateManagerMockRecorder) PutState(arg0, arg1 interface{}, arg2 ...interface{}) *gomock.Call { +func (mr *MockStateManagerMockRecorder) PutState(arg0 interface{}, arg1 ...interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - varargs := append([]interface{}{arg0, arg1}, arg2...) + varargs := append([]interface{}{arg0}, arg1...) return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "PutState", reflect.TypeOf((*MockStateManager)(nil).PutState), varargs...) } // DelState mocks base method -func (m *MockStateManager) DelState(arg0 hash.Hash160, arg1 ...protocol.StateOption) error { +func (m *MockStateManager) DelState(arg0 ...protocol.StateOption) (uint64, error) { m.ctrl.T.Helper() - varargs := []interface{}{arg0} - for _, a := range arg1 { + varargs := []interface{}{} + for _, a := range arg0 { varargs = append(varargs, a) } ret := m.ctrl.Call(m, "DelState", varargs...) - ret0, _ := ret[0].(error) - return ret0 + ret0, _ := ret[0].(uint64) + ret1, _ := ret[1].(error) + return ret0, ret1 } // DelState indicates an expected call of DelState -func (mr *MockStateManagerMockRecorder) DelState(arg0 interface{}, arg1 ...interface{}) *gomock.Call { +func (mr *MockStateManagerMockRecorder) DelState(arg0 ...interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - varargs := append([]interface{}{arg0}, arg1...) - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DelState", reflect.TypeOf((*MockStateManager)(nil).DelState), varargs...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DelState", reflect.TypeOf((*MockStateManager)(nil).DelState), arg0...) } // GetDB mocks base method diff --git a/test/mock/mock_factory/mock_factory.go b/test/mock/mock_factory/mock_factory.go index 67d12d2976..c3c13dd110 100644 --- a/test/mock/mock_factory/mock_factory.go +++ b/test/mock/mock_factory/mock_factory.go @@ -7,7 +7,6 @@ package mock_factory import ( context "context" gomock "github.com/golang/mock/gomock" - hash "github.com/iotexproject/go-pkgs/hash" address "github.com/iotexproject/iotex-address/address" action "github.com/iotexproject/iotex-core/action" protocol "github.com/iotexproject/iotex-core/action/protocol" @@ -84,21 +83,22 @@ func (mr *MockFactoryMockRecorder) Height() *gomock.Call { } // State mocks base method -func (m *MockFactory) State(arg0 hash.Hash160, arg1 interface{}, arg2 ...protocol.StateOption) error { +func (m *MockFactory) State(arg0 interface{}, arg1 ...protocol.StateOption) (uint64, error) { m.ctrl.T.Helper() - varargs := []interface{}{arg0, arg1} - for _, a := range arg2 { + varargs := []interface{}{arg0} + for _, a := range arg1 { varargs = append(varargs, a) } ret := m.ctrl.Call(m, "State", varargs...) - ret0, _ := ret[0].(error) - return ret0 + ret0, _ := ret[0].(uint64) + ret1, _ := ret[1].(error) + return ret0, ret1 } // State indicates an expected call of State -func (mr *MockFactoryMockRecorder) State(arg0, arg1 interface{}, arg2 ...interface{}) *gomock.Call { +func (mr *MockFactoryMockRecorder) State(arg0 interface{}, arg1 ...interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - varargs := append([]interface{}{arg0, arg1}, arg2...) + varargs := append([]interface{}{arg0}, arg1...) return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "State", reflect.TypeOf((*MockFactory)(nil).State), varargs...) } diff --git a/test/mock/mock_factory/mock_workingset.go b/test/mock/mock_factory/mock_workingset.go index b9e69d4384..a53eae9059 100644 --- a/test/mock/mock_factory/mock_workingset.go +++ b/test/mock/mock_factory/mock_workingset.go @@ -53,21 +53,22 @@ func (mr *MockWorkingSetMockRecorder) Height() *gomock.Call { } // State mocks base method -func (m *MockWorkingSet) State(arg0 hash.Hash160, arg1 interface{}, arg2 ...protocol.StateOption) error { +func (m *MockWorkingSet) State(arg0 interface{}, arg1 ...protocol.StateOption) (uint64, error) { m.ctrl.T.Helper() - varargs := []interface{}{arg0, arg1} - for _, a := range arg2 { + varargs := []interface{}{arg0} + for _, a := range arg1 { varargs = append(varargs, a) } ret := m.ctrl.Call(m, "State", varargs...) - ret0, _ := ret[0].(error) - return ret0 + ret0, _ := ret[0].(uint64) + ret1, _ := ret[1].(error) + return ret0, ret1 } // State indicates an expected call of State -func (mr *MockWorkingSetMockRecorder) State(arg0, arg1 interface{}, arg2 ...interface{}) *gomock.Call { +func (mr *MockWorkingSetMockRecorder) State(arg0 interface{}, arg1 ...interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - varargs := append([]interface{}{arg0, arg1}, arg2...) + varargs := append([]interface{}{arg0}, arg1...) return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "State", reflect.TypeOf((*MockWorkingSet)(nil).State), varargs...) } @@ -100,41 +101,42 @@ func (mr *MockWorkingSetMockRecorder) Revert(arg0 interface{}) *gomock.Call { } // PutState mocks base method -func (m *MockWorkingSet) PutState(arg0 hash.Hash160, arg1 interface{}, arg2 ...protocol.StateOption) error { +func (m *MockWorkingSet) PutState(arg0 interface{}, arg1 ...protocol.StateOption) (uint64, error) { m.ctrl.T.Helper() - varargs := []interface{}{arg0, arg1} - for _, a := range arg2 { + varargs := []interface{}{arg0} + for _, a := range arg1 { varargs = append(varargs, a) } ret := m.ctrl.Call(m, "PutState", varargs...) - ret0, _ := ret[0].(error) - return ret0 + ret0, _ := ret[0].(uint64) + ret1, _ := ret[1].(error) + return ret0, ret1 } // PutState indicates an expected call of PutState -func (mr *MockWorkingSetMockRecorder) PutState(arg0, arg1 interface{}, arg2 ...interface{}) *gomock.Call { +func (mr *MockWorkingSetMockRecorder) PutState(arg0 interface{}, arg1 ...interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - varargs := append([]interface{}{arg0, arg1}, arg2...) + varargs := append([]interface{}{arg0}, arg1...) return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "PutState", reflect.TypeOf((*MockWorkingSet)(nil).PutState), varargs...) } // DelState mocks base method -func (m *MockWorkingSet) DelState(arg0 hash.Hash160, arg1 ...protocol.StateOption) error { +func (m *MockWorkingSet) DelState(arg0 ...protocol.StateOption) (uint64, error) { m.ctrl.T.Helper() - varargs := []interface{}{arg0} - for _, a := range arg1 { + varargs := []interface{}{} + for _, a := range arg0 { varargs = append(varargs, a) } ret := m.ctrl.Call(m, "DelState", varargs...) - ret0, _ := ret[0].(error) - return ret0 + ret0, _ := ret[0].(uint64) + ret1, _ := ret[1].(error) + return ret0, ret1 } // DelState indicates an expected call of DelState -func (mr *MockWorkingSetMockRecorder) DelState(arg0 interface{}, arg1 ...interface{}) *gomock.Call { +func (mr *MockWorkingSetMockRecorder) DelState(arg0 ...interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - varargs := append([]interface{}{arg0}, arg1...) - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DelState", reflect.TypeOf((*MockWorkingSet)(nil).DelState), varargs...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DelState", reflect.TypeOf((*MockWorkingSet)(nil).DelState), arg0...) } // GetDB mocks base method diff --git a/tools/actioninjector.v2/internal/client/client_test.go b/tools/actioninjector.v2/internal/client/client_test.go index 0ec7db70c6..dac258efda 100644 --- a/tools/actioninjector.v2/internal/client/client_test.go +++ b/tools/actioninjector.v2/internal/client/client_test.go @@ -12,6 +12,7 @@ import ( "github.com/stretchr/testify/require" "github.com/iotexproject/iotex-core/action" + "github.com/iotexproject/iotex-core/action/protocol" "github.com/iotexproject/iotex-core/api" "github.com/iotexproject/iotex-core/blockindex" "github.com/iotexproject/iotex-core/config" @@ -49,7 +50,7 @@ func TestClient(t *testing.T) { sf := mock_factory.NewMockFactory(mockCtrl) ap := mock_actpool.NewMockActPool(mockCtrl) - sf.EXPECT().State(gomock.Any(), gomock.Any()).Do(func(_ hash.Hash160, accountState *state.Account) { + sf.EXPECT().State(gomock.Any(), gomock.Any()).Do(func(accountState *state.Account, _ protocol.StateOption) { *accountState = state.EmptyAccount() }) bc.EXPECT().ChainID().Return(chainID).AnyTimes()