diff --git a/cmd/node/config/enableEpochs.toml b/cmd/node/config/enableEpochs.toml index c45ff1c3332..bbdcd165b5d 100644 --- a/cmd/node/config/enableEpochs.toml +++ b/cmd/node/config/enableEpochs.toml @@ -221,6 +221,9 @@ # FixOldTokenLiquidityEnableEpoch represents the epoch when the fix for old token liquidity is enabled FixOldTokenLiquidityEnableEpoch = 1 + # AutoBalanceDataTriesEnableEpoch represents the epoch when the data tries are automatically balanced by inserting at the hashed key instead of the normal key + AutoBalanceDataTriesEnableEpoch = 1 + # SetSenderInEeiOutputTransferEnableEpoch represents the epoch when setting the sender in eei output transfers will be enabled SetSenderInEeiOutputTransferEnableEpoch = 4 diff --git a/common/enablers/enableEpochsHandler.go b/common/enablers/enableEpochsHandler.go index 76be5268e5d..0cc3ff08eeb 100644 --- a/common/enablers/enableEpochsHandler.go +++ b/common/enablers/enableEpochsHandler.go @@ -113,6 +113,7 @@ func (handler *enableEpochsHandler) EpochConfirmed(epoch uint32, _ uint64) { handler.setFlagValue(epoch >= handler.enableEpochsConfig.RefactorPeersMiniBlocksEnableEpoch, handler.refactorPeersMiniBlocksFlag, "refactorPeersMiniBlocksFlag") handler.setFlagValue(epoch >= handler.enableEpochsConfig.FixAsyncCallBackArgsListEnableEpoch, handler.fixAsyncCallBackArgsList, "fixAsyncCallBackArgsList") handler.setFlagValue(epoch >= handler.enableEpochsConfig.FixOldTokenLiquidityEnableEpoch, handler.fixOldTokenLiquidity, "fixOldTokenLiquidity") + handler.setFlagValue(epoch >= handler.enableEpochsConfig.AutoBalanceDataTriesEnableEpoch, handler.autoBalanceDataTriesFlag, "autoBalanceDataTriesFlag") } func (handler *enableEpochsHandler) setFlagValue(value bool, flag *atomic.Flag, flagName string) { diff --git a/common/enablers/enableEpochsHandler_test.go b/common/enablers/enableEpochsHandler_test.go index cd45dcad5b2..f51209480cd 100644 --- a/common/enablers/enableEpochsHandler_test.go +++ b/common/enablers/enableEpochsHandler_test.go @@ -86,6 +86,7 @@ func createEnableEpochsConfig() config.EnableEpochs { CheckExecuteOnReadOnlyEnableEpoch: 70, FixAsyncCallBackArgsListEnableEpoch: 71, FixOldTokenLiquidityEnableEpoch: 72, + AutoBalanceDataTriesEnableEpoch: 73, } } @@ -204,6 +205,7 @@ func TestNewEnableEpochsHandler_EpochConfirmed(t *testing.T) { assert.True(t, handler.IsCheckExecuteOnReadOnlyFlagEnabled()) assert.True(t, handler.IsChangeDelegationOwnerFlagEnabled()) assert.True(t, handler.IsFixOldTokenLiquidityEnabled()) + assert.True(t, handler.IsAutoBalanceDataTriesEnabled()) }) t.Run("flags with == condition should be set, along with all >=", func(t *testing.T) { t.Parallel() @@ -299,6 +301,7 @@ func TestNewEnableEpochsHandler_EpochConfirmed(t *testing.T) { assert.True(t, handler.IsChangeDelegationOwnerFlagEnabled()) assert.True(t, handler.IsFixAsyncCallBackArgsListFlagEnabled()) assert.True(t, handler.IsFixOldTokenLiquidityEnabled()) + assert.True(t, handler.IsAutoBalanceDataTriesEnabled()) }) t.Run("flags with < should be set", func(t *testing.T) { t.Parallel() @@ -389,5 +392,6 @@ func TestNewEnableEpochsHandler_EpochConfirmed(t *testing.T) { assert.False(t, handler.IsChangeDelegationOwnerFlagEnabled()) assert.False(t, handler.IsFixAsyncCallBackArgsListFlagEnabled()) assert.False(t, handler.IsFixOldTokenLiquidityEnabled()) + assert.False(t, handler.IsAutoBalanceDataTriesEnabled()) }) } diff --git a/common/enablers/epochFlags.go b/common/enablers/epochFlags.go index d70fc3e2b60..917b15dd953 100644 --- a/common/enablers/epochFlags.go +++ b/common/enablers/epochFlags.go @@ -83,6 +83,7 @@ type epochFlagsHolder struct { refactorPeersMiniBlocksFlag *atomic.Flag fixAsyncCallBackArgsList *atomic.Flag fixOldTokenLiquidity *atomic.Flag + autoBalanceDataTriesFlag *atomic.Flag } func newEpochFlagsHolder() *epochFlagsHolder { @@ -167,6 +168,7 @@ func newEpochFlagsHolder() *epochFlagsHolder { refactorPeersMiniBlocksFlag: &atomic.Flag{}, fixAsyncCallBackArgsList: &atomic.Flag{}, fixOldTokenLiquidity: &atomic.Flag{}, + autoBalanceDataTriesFlag: &atomic.Flag{}, } } @@ -622,3 +624,8 @@ func (holder *epochFlagsHolder) IsFixAsyncCallBackArgsListFlagEnabled() bool { func (holder *epochFlagsHolder) IsFixOldTokenLiquidityEnabled() bool { return holder.fixOldTokenLiquidity.IsSet() } + +// IsAutoBalanceDataTriesEnabled returns true if autoBalanceDataTriesFlag is enabled +func (holder *epochFlagsHolder) IsAutoBalanceDataTriesEnabled() bool { + return holder.autoBalanceDataTriesFlag.IsSet() +} diff --git a/common/interface.go b/common/interface.go index 31ae419ebe9..c2857bafab0 100644 --- a/common/interface.go +++ b/common/interface.go @@ -332,6 +332,7 @@ type EnableEpochsHandler interface { IsRefactorPeersMiniBlocksFlagEnabled() bool IsFixAsyncCallBackArgsListFlagEnabled() bool IsFixOldTokenLiquidityEnabled() bool + IsAutoBalanceDataTriesEnabled() bool IsInterfaceNil() bool } diff --git a/config/epochConfig.go b/config/epochConfig.go index e554f101801..a3e29ff3865 100644 --- a/config/epochConfig.go +++ b/config/epochConfig.go @@ -88,6 +88,7 @@ type EnableEpochs struct { FixOldTokenLiquidityEnableEpoch uint32 SetSenderInEeiOutputTransferEnableEpoch uint32 RefactorPeersMiniBlocksEnableEpoch uint32 + AutoBalanceDataTriesEnableEpoch uint32 BLSMultiSignerEnableEpoch []MultiSignerConfig } diff --git a/epochStart/metachain/baseRewards_test.go b/epochStart/metachain/baseRewards_test.go index 9d34a622e2c..467d644eb12 100644 --- a/epochStart/metachain/baseRewards_test.go +++ b/epochStart/metachain/baseRewards_test.go @@ -826,15 +826,20 @@ func TestBaseRewardsCreator_isSystemDelegationSC(t *testing.T) { require.False(t, isDelegationSCAddress) // peer account - peerAccount, err := state.NewPeerAccount([]byte("addressPeer"), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + peerAccount, err := state.NewPeerAccount([]byte("addressPeer")) require.Nil(t, err) err = rwd.userAccountsDB.SaveAccount(peerAccount) require.Nil(t, err) isDelegationSCAddress = rwd.isSystemDelegationSC(peerAccount.AddressBytes()) require.False(t, isDelegationSCAddress) + argsAccCreation := state.ArgsAccountCreation{ + Hasher: &hashingMocks.HasherMock{}, + Marshaller: &testscommon.MarshalizerMock{}, + EnableEpochsHandler: &testscommon.EnableEpochsHandlerStub{}, + } // existing user account - userAccount, err := state.NewUserAccount([]byte("userAddress"), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + userAccount, err := state.NewUserAccount([]byte("userAddress"), argsAccCreation) require.Nil(t, err) userAccount.SetDataTrie(&trieMock.TrieStub{ @@ -1141,7 +1146,13 @@ func getBaseRewardsArguments() BaseRewardsCreatorArgs { storageManagerArgs.Hasher = hasher trieFactoryManager, _ := trie.CreateTrieStorageManager(storageManagerArgs, options) - userAccountsDB := createAccountsDB(hasher, marshalizer, factory.NewAccountCreator(), trieFactoryManager) + argsAccCreator := state.ArgsAccountCreation{ + Hasher: hasher, + Marshaller: marshalizer, + EnableEpochsHandler: &testscommon.EnableEpochsHandlerStub{}, + } + accCreator, _ := factory.NewAccountCreator(argsAccCreator) + userAccountsDB := createAccountsDB(hasher, marshalizer, accCreator, trieFactoryManager) shardCoordinator := mock.NewMultiShardsCoordinatorMock(2) shardCoordinator.CurrentShard = core.MetachainShardId shardCoordinator.ComputeIdCalled = func(address []byte) uint32 { diff --git a/epochStart/metachain/systemSCs_test.go b/epochStart/metachain/systemSCs_test.go index fb0344122c4..8b98a133a20 100644 --- a/epochStart/metachain/systemSCs_test.go +++ b/epochStart/metachain/systemSCs_test.go @@ -912,8 +912,15 @@ func createFullArgumentsForSystemSCProcessing(enableEpochsConfig config.EnableEp storageManagerArgs.CheckpointsStorer = trieStorer trieFactoryManager, _ := trie.CreateTrieStorageManager(storageManagerArgs, options) - userAccountsDB := createAccountsDB(hasher, marshalizer, factory.NewAccountCreator(), trieFactoryManager) - peerAccountsDB := createAccountsDB(hasher, marshalizer, factory.NewPeerAccountCreator(), trieFactoryManager) + argsAccCreator := state.ArgsAccountCreation{ + Hasher: hasher, + Marshaller: marshalizer, + EnableEpochsHandler: &testscommon.EnableEpochsHandlerStub{}, + } + accCreator, _ := factory.NewAccountCreator(argsAccCreator) + peerAccCreator := factory.NewPeerAccountCreator() + userAccountsDB := createAccountsDB(hasher, marshalizer, accCreator, trieFactoryManager) + peerAccountsDB := createAccountsDB(hasher, marshalizer, peerAccCreator, trieFactoryManager) en := forking.NewGenericEpochNotifier() epochsConfig := &config.EpochConfig{ EnableEpochs: enableEpochsConfig, diff --git a/factory/processing/blockProcessorCreator_test.go b/factory/processing/blockProcessorCreator_test.go index b917386d0c1..7053c15eb1c 100644 --- a/factory/processing/blockProcessorCreator_test.go +++ b/factory/processing/blockProcessorCreator_test.go @@ -107,10 +107,17 @@ func Test_newBlockProcessorCreatorForMeta(t *testing.T) { trieStorageManagers[trieFactory.UserAccountTrie] = storageManagerUser trieStorageManagers[trieFactory.PeerAccountTrie] = storageManagerPeer + argsAccCreator := state.ArgsAccountCreation{ + Hasher: coreComponents.Hasher(), + Marshaller: coreComponents.InternalMarshalizer(), + EnableEpochsHandler: coreComponents.EnableEpochsHandler(), + } + accCreator, _ := factoryState.NewAccountCreator(argsAccCreator) + accounts, err := createAccountAdapter( &mock.MarshalizerMock{}, &hashingMocks.HasherMock{}, - factoryState.NewAccountCreator(), + accCreator, trieStorageManagers[trieFactory.UserAccountTrie], ) require.Nil(t, err) diff --git a/factory/processing/processComponents.go b/factory/processing/processComponents.go index 556165b79b2..e8e5d59647f 100644 --- a/factory/processing/processComponents.go +++ b/factory/processing/processComponents.go @@ -882,7 +882,12 @@ func (pcf *processComponentsFactory) indexGenesisAccounts() error { } func (pcf *processComponentsFactory) unmarshalUserAccount(address []byte, userAccountsBytes []byte) (state.UserAccountHandler, error) { - userAccount, err := state.NewUserAccount(address, pcf.coreData.Hasher(), pcf.coreData.InternalMarshalizer()) + argsAccCreation := state.ArgsAccountCreation{ + Hasher: pcf.coreData.Hasher(), + Marshaller: pcf.coreData.InternalMarshalizer(), + EnableEpochsHandler: pcf.coreData.EnableEpochsHandler(), + } + userAccount, err := state.NewUserAccount(address, argsAccCreation) if err != nil { return nil, err } diff --git a/factory/state/stateComponents.go b/factory/state/stateComponents.go index 0ab3e5b69b7..d335a37e797 100644 --- a/factory/state/stateComponents.go +++ b/factory/state/stateComponents.go @@ -118,7 +118,16 @@ func (scf *stateComponentsFactory) Create() (*stateComponents, error) { } func (scf *stateComponentsFactory) createAccountsAdapters(triesContainer common.TriesHolder) (state.AccountsAdapter, state.AccountsAdapter, state.AccountsRepository, error) { - accountFactory := factoryState.NewAccountCreator() + argsAccCreator := state.ArgsAccountCreation{ + Hasher: scf.core.Hasher(), + Marshaller: scf.core.InternalMarshalizer(), + EnableEpochsHandler: scf.core.EnableEpochsHandler(), + } + accountFactory, err := factoryState.NewAccountCreator(argsAccCreator) + if err != nil { + return nil, nil, nil, err + } + merkleTrie := triesContainer.Get([]byte(trieFactory.UserAccountTrie)) storagePruning, err := scf.newStoragePruningManager() if err != nil { diff --git a/genesis/process/genesisBlockCreator.go b/genesis/process/genesisBlockCreator.go index 629177b235b..8bd492a2328 100644 --- a/genesis/process/genesisBlockCreator.go +++ b/genesis/process/genesisBlockCreator.go @@ -22,6 +22,7 @@ import ( "github.com/ElrondNetwork/elrond-go/process" "github.com/ElrondNetwork/elrond-go/process/smartContract/hooks" "github.com/ElrondNetwork/elrond-go/sharding" + "github.com/ElrondNetwork/elrond-go/state" factoryState "github.com/ElrondNetwork/elrond-go/state/factory" "github.com/ElrondNetwork/elrond-go/statusHandler" "github.com/ElrondNetwork/elrond-go/storage" @@ -114,6 +115,7 @@ func (gbc *genesisBlockCreator) createHardForkImportHandler() error { ShardID: gbc.arg.ShardCoordinator.SelfId(), StorageConfig: gbc.arg.HardForkConfig.ImportStateStorageConfig, TrieStorageManagers: gbc.arg.TrieStorageManagers, + EnableEpochsHandler: gbc.arg.Core.EnableEpochsHandler(), } importHandler, err := hardfork.NewStateImport(argsHardForkImport) if err != nil { @@ -475,12 +477,22 @@ func (gbc *genesisBlockCreator) getNewArgForShard(shardID uint32) (ArgsGenesisBl newArgument.Data = newArgument.Data.Clone().(dataComponentsHandler) return newArgument, nil } - newArgument := gbc.arg // copy the arguments + + argsAccCreator := state.ArgsAccountCreation{ + Hasher: newArgument.Core.Hasher(), + Marshaller: newArgument.Core.InternalMarshalizer(), + EnableEpochsHandler: newArgument.Core.EnableEpochsHandler(), + } + accCreator, err := factoryState.NewAccountCreator(argsAccCreator) + if err != nil { + return ArgsGenesisBlockCreator{}, err + } + newArgument.Accounts, err = createAccountAdapter( newArgument.Core.InternalMarshalizer(), newArgument.Core.Hasher(), - factoryState.NewAccountCreator(), + accCreator, gbc.arg.TrieStorageManagers[triesFactory.UserAccountTrie], ) if err != nil { diff --git a/genesis/process/genesisBlockCreator_test.go b/genesis/process/genesisBlockCreator_test.go index b5df301bfcb..ca0467a8fa9 100644 --- a/genesis/process/genesisBlockCreator_test.go +++ b/genesis/process/genesisBlockCreator_test.go @@ -141,11 +141,18 @@ func createMockArgument( SelfShardId: 0, } - var err error + argsAccCreator := state.ArgsAccountCreation{ + Hasher: &hashingMocks.HasherMock{}, + Marshaller: &mock.MarshalizerMock{}, + EnableEpochsHandler: &testscommon.EnableEpochsHandlerStub{}, + } + accCreator, err := factoryState.NewAccountCreator(argsAccCreator) + require.Nil(t, err) + arg.Accounts, err = createAccountAdapter( &mock.MarshalizerMock{}, &hashingMocks.HasherMock{}, - factoryState.NewAccountCreator(), + accCreator, trieStorageManagers[factory.UserAccountTrie], ) require.Nil(t, err) diff --git a/integrationTests/state/stateTrie/stateTrie_test.go b/integrationTests/state/stateTrie/stateTrie_test.go index ebac4406c0a..25ad82adc6a 100644 --- a/integrationTests/state/stateTrie/stateTrie_test.go +++ b/integrationTests/state/stateTrie/stateTrie_test.go @@ -1057,12 +1057,17 @@ func createAccounts( maxTrieLevelInMemory := uint(5) tr, _ := trie.NewTrie(trieStorage, integrationTests.TestMarshalizer, integrationTests.TestHasher, maxTrieLevelInMemory) spm, _ := storagePruningManager.NewStoragePruningManager(ewl, 10) - + argsAccCreator := state.ArgsAccountCreation{ + Hasher: integrationTests.TestHasher, + Marshaller: integrationTests.TestMarshalizer, + EnableEpochsHandler: &testscommon.EnableEpochsHandlerStub{}, + } + accCreator, _ := factory.NewAccountCreator(argsAccCreator) argsAccountsDB := state.ArgsAccountsDB{ Trie: tr, Hasher: integrationTests.TestHasher, Marshaller: integrationTests.TestMarshalizer, - AccountFactory: factory.NewAccountCreator(), + AccountFactory: accCreator, StoragePruningManager: spm, ProcessingMode: common.Normal, ProcessStatusHandler: &testscommon.ProcessStatusHandlerStub{}, @@ -2401,12 +2406,18 @@ func createAccountsDBTestSetup() *state.AccountsDB { maxTrieLevelInMemory := uint(5) tr, _ := trie.NewTrie(trieStorage, integrationTests.TestMarshalizer, integrationTests.TestHasher, maxTrieLevelInMemory) spm, _ := storagePruningManager.NewStoragePruningManager(ewl, 10) + argsAccCreator := state.ArgsAccountCreation{ + Hasher: integrationTests.TestHasher, + Marshaller: integrationTests.TestMarshalizer, + EnableEpochsHandler: &testscommon.EnableEpochsHandlerStub{}, + } + accCreator, _ := factory.NewAccountCreator(argsAccCreator) argsAccountsDB := state.ArgsAccountsDB{ Trie: tr, Hasher: integrationTests.TestHasher, Marshaller: integrationTests.TestMarshalizer, - AccountFactory: factory.NewAccountCreator(), + AccountFactory: accCreator, StoragePruningManager: spm, ProcessingMode: common.Normal, ProcessStatusHandler: &testscommon.ProcessStatusHandlerStub{}, diff --git a/integrationTests/testInitializer.go b/integrationTests/testInitializer.go index f705d0ee76f..27a05d0fc78 100644 --- a/integrationTests/testInitializer.go +++ b/integrationTests/testInitializer.go @@ -436,11 +436,20 @@ func CreateTrieStorageManager(store storage.Storer) (common.StorageManager, stor func CreateAccountsDB( accountType Type, trieStorageManager common.StorageManager, +) (*state.AccountsDB, common.Trie) { + return CreateAccountsDBWithEnableEpochsHandler(accountType, trieStorageManager, &testscommon.EnableEpochsHandlerStub{}) +} + +// CreateAccountsDBWithEnableEpochsHandler creates a new AccountsDb with the given enableEpochsHandler +func CreateAccountsDBWithEnableEpochsHandler( + accountType Type, + trieStorageManager common.StorageManager, + enableEpochsHandler common.EnableEpochsHandler, ) (*state.AccountsDB, common.Trie) { tr, _ := trie.NewTrie(trieStorageManager, TestMarshalizer, TestHasher, maxTrieLevelInMemory) ewl, _ := evictionWaitingList.NewEvictionWaitingList(100, database.NewMemDB(), TestMarshalizer) - accountFactory := getAccountFactory(accountType) + accountFactory, _ := getAccountFactory(accountType, enableEpochsHandler) spm, _ := storagePruningManager.NewStoragePruningManager(ewl, 10) args := state.ArgsAccountsDB{ Trie: tr, @@ -457,14 +466,19 @@ func CreateAccountsDB( return adb, tr } -func getAccountFactory(accountType Type) state.AccountFactory { +func getAccountFactory(accountType Type, enableEpochsHandler common.EnableEpochsHandler) (state.AccountFactory, error) { switch accountType { case UserAccount: - return factory.NewAccountCreator() + argsAccCreator := state.ArgsAccountCreation{ + Hasher: TestHasher, + Marshaller: TestMarshalizer, + EnableEpochsHandler: enableEpochsHandler, + } + return factory.NewAccountCreator(argsAccCreator) case ValidatorAccount: - return factory.NewPeerAccountCreator() + return factory.NewPeerAccountCreator(), nil default: - return nil + return nil, fmt.Errorf("invalid account type provided") } } @@ -803,7 +817,7 @@ func CreateGenesisMetaBlock( newBlkc, _ := blockchain.NewMetaChain(&statusHandlerMock.AppStatusHandlerStub{}) trieStorage, _ := CreateTrieStorageManager(CreateMemUnit()) - newAccounts, _ := CreateAccountsDB(UserAccount, trieStorage) + newAccounts, _ := CreateAccountsDBWithEnableEpochsHandler(UserAccount, trieStorage, coreComponents.EnableEpochsHandler()) argsMetaGenesis.ShardCoordinator = newShardCoordinator argsMetaGenesis.Accounts = newAccounts @@ -900,7 +914,12 @@ func GenerateAddressJournalAccountAccountsDB() ([]byte, state.UserAccountHandler adr := CreateRandomAddress() trieStorage, _ := CreateTrieStorageManager(CreateMemUnit()) adb, _ := CreateAccountsDB(UserAccount, trieStorage) - account, _ := state.NewUserAccount(adr, TestHasher, TestMarshaller) + argsAccCreation := state.ArgsAccountCreation{ + Hasher: TestHasher, + Marshaller: TestMarshaller, + EnableEpochsHandler: &testscommon.EnableEpochsHandlerStub{}, + } + account, _ := state.NewUserAccount(adr, argsAccCreation) return adr, account, adb } diff --git a/integrationTests/testProcessorNode.go b/integrationTests/testProcessorNode.go index 01f621a5466..8144c777340 100644 --- a/integrationTests/testProcessorNode.go +++ b/integrationTests/testProcessorNode.go @@ -522,11 +522,11 @@ func (tpn *TestProcessorNode) initAccountDBsWithPruningStorer() { trieStorageManager := CreateTrieStorageManagerWithPruningStorer(tpn.ShardCoordinator, tpn.EpochStartNotifier) tpn.TrieContainer = state.NewDataTriesHolder() var stateTrie common.Trie - tpn.AccntState, stateTrie = CreateAccountsDB(UserAccount, trieStorageManager) + tpn.AccntState, stateTrie = CreateAccountsDBWithEnableEpochsHandler(UserAccount, trieStorageManager, tpn.EnableEpochsHandler) tpn.TrieContainer.Put([]byte(trieFactory.UserAccountTrie), stateTrie) var peerTrie common.Trie - tpn.PeerState, peerTrie = CreateAccountsDB(ValidatorAccount, trieStorageManager) + tpn.PeerState, peerTrie = CreateAccountsDBWithEnableEpochsHandler(ValidatorAccount, trieStorageManager, tpn.EnableEpochsHandler) tpn.TrieContainer.Put([]byte(trieFactory.PeerAccountTrie), peerTrie) tpn.TrieStorageManagers = make(map[string]common.StorageManager) @@ -538,11 +538,11 @@ func (tpn *TestProcessorNode) initAccountDBs(store storage.Storer) { trieStorageManager, _ := CreateTrieStorageManager(store) tpn.TrieContainer = state.NewDataTriesHolder() var stateTrie common.Trie - tpn.AccntState, stateTrie = CreateAccountsDB(UserAccount, trieStorageManager) + tpn.AccntState, stateTrie = CreateAccountsDBWithEnableEpochsHandler(UserAccount, trieStorageManager, tpn.EnableEpochsHandler) tpn.TrieContainer.Put([]byte(trieFactory.UserAccountTrie), stateTrie) var peerTrie common.Trie - tpn.PeerState, peerTrie = CreateAccountsDB(ValidatorAccount, trieStorageManager) + tpn.PeerState, peerTrie = CreateAccountsDBWithEnableEpochsHandler(ValidatorAccount, trieStorageManager, tpn.EnableEpochsHandler) tpn.TrieContainer.Put([]byte(trieFactory.PeerAccountTrie), peerTrie) tpn.TrieStorageManagers = make(map[string]common.StorageManager) diff --git a/integrationTests/vm/systemVM/stakingSC_test.go b/integrationTests/vm/systemVM/stakingSC_test.go index 7e524181f6d..63e78859af6 100644 --- a/integrationTests/vm/systemVM/stakingSC_test.go +++ b/integrationTests/vm/systemVM/stakingSC_test.go @@ -412,7 +412,7 @@ func manualSetToInactiveStateStakedPeers(t *testing.T, nodes []*integrationTests for index := range nodes { pubKey, _ := hex.DecodeString(generateUniqueKey(index)) - peerAccount, _ := state.NewPeerAccount(pubKey, integrationTests.TestHasher, integrationTests.TestMarshaller) + peerAccount, _ := state.NewPeerAccount(pubKey) peerAccount.List = string(common.InactiveList) peerAccount.BLSPublicKey = pubKey err := node.PeerState.SaveAccount(peerAccount) diff --git a/integrationTests/vm/testInitializer.go b/integrationTests/vm/testInitializer.go index 220cc31ccfe..a95819e1277 100644 --- a/integrationTests/vm/testInitializer.go +++ b/integrationTests/vm/testInitializer.go @@ -259,11 +259,18 @@ func (vmTestContext *VMTestContext) GetVMOutputWithTransientVM(funcName string, } type accountFactory struct { + hasher hashing.Hasher + marshaller marshal.Marshalizer } // CreateAccount - -func (af *accountFactory) CreateAccount(address []byte, hasher hashing.Hasher, marshaller marshal.Marshalizer) (vmcommon.AccountHandler, error) { - return state.NewUserAccount(address, hasher, marshaller) +func (af *accountFactory) CreateAccount(address []byte) (vmcommon.AccountHandler, error) { + argsAccCreation := state.ArgsAccountCreation{ + Hasher: af.hasher, + Marshaller: af.marshaller, + EnableEpochsHandler: &testscommon.EnableEpochsHandlerStub{}, + } + return state.NewUserAccount(address, argsAccCreation) } // IsInterfaceNil returns true if there is no value under the interface @@ -313,10 +320,13 @@ func CreateInMemoryShardAccountsDB() *state.AccountsDB { spm, _ := storagePruningManager.NewStoragePruningManager(ewl, 10) argsAccountsDB := state.ArgsAccountsDB{ - Trie: tr, - Hasher: testHasher, - Marshaller: marshaller, - AccountFactory: &accountFactory{}, + Trie: tr, + Hasher: testHasher, + Marshaller: marshaller, + AccountFactory: &accountFactory{ + hasher: testHasher, + marshaller: testMarshalizer, + }, StoragePruningManager: spm, ProcessingMode: common.Normal, ProcessStatusHandler: &testscommon.ProcessStatusHandlerStub{}, diff --git a/node/nodeLoadAccounts_test.go b/node/nodeLoadAccounts_test.go index b99a8d34345..0707f2b414c 100644 --- a/node/nodeLoadAccounts_test.go +++ b/node/nodeLoadAccounts_test.go @@ -16,7 +16,6 @@ import ( "github.com/ElrondNetwork/elrond-go/testscommon" "github.com/ElrondNetwork/elrond-go/testscommon/dblookupext" "github.com/ElrondNetwork/elrond-go/testscommon/genericMocks" - "github.com/ElrondNetwork/elrond-go/testscommon/hashingMocks" mockState "github.com/ElrondNetwork/elrond-go/testscommon/state" vmcommon "github.com/ElrondNetwork/elrond-vm-common" "github.com/stretchr/testify/require" @@ -25,8 +24,8 @@ import ( func TestNode_GetAccountWithOptionsShouldWork(t *testing.T) { t.Parallel() - alice, _ := state.NewUserAccount(testscommon.TestPubKeyAlice, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - alice.Balance = big.NewInt(100) + alice := createAcc(testscommon.TestPubKeyAlice) + _ = alice.AddToBalance(big.NewInt(100)) accountsRepostitory := &mockState.AccountsRepositoryStub{} accountsRepostitory.GetAccountWithBlockInfoCalled = func(pubkey []byte, options api.AccountQueryOptions) (vmcommon.AccountHandler, common.BlockInfo, error) { diff --git a/node/node_test.go b/node/node_test.go index 85db496820b..2bf8afe0239 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -66,10 +66,21 @@ func createMockPubkeyConverter() *mock.PubkeyConverterMock { return mock.NewPubkeyConverterMock(32) } +func createAcc(address []byte) state.UserAccountHandler { + argsAccCreation := state.ArgsAccountCreation{ + Hasher: &hashingMocks.HasherMock{}, + Marshaller: &testscommon.MarshalizerMock{}, + EnableEpochsHandler: &testscommon.EnableEpochsHandlerStub{}, + } + acc, _ := state.NewUserAccount(address, argsAccCreation) + + return acc +} + func getAccAdapter(balance *big.Int) *stateMock.AccountsStub { accDB := &stateMock.AccountsStub{} accDB.GetExistingAccountCalled = func(address []byte) (handler vmcommon.AccountHandler, e error) { - acc, _ := state.NewUserAccount(address, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acc := createAcc(address) _ = acc.AddToBalance(balance) acc.IncreaseNonce(1) @@ -195,7 +206,12 @@ func TestGetBalance_AccountNotFoundShouldReturnZeroBalance(t *testing.T) { func TestGetBalance(t *testing.T) { t.Parallel() - testAccount, _ := state.NewUserAccount(testscommon.TestPubKeyAlice, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + argsAccCreation := state.ArgsAccountCreation{ + Hasher: &hashingMocks.HasherMock{}, + Marshaller: &testscommon.MarshalizerMock{}, + EnableEpochsHandler: &testscommon.EnableEpochsHandlerStub{}, + } + testAccount, _ := state.NewUserAccount(testscommon.TestPubKeyAlice, argsAccCreation) testAccount.Balance = big.NewInt(100) accountsRepository := &stateMock.AccountsRepositoryStub{ @@ -228,7 +244,12 @@ func TestGetUsername(t *testing.T) { expectedUsername := []byte("elrond") - testAccount, _ := state.NewUserAccount(testscommon.TestPubKeyAlice, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + argsAccCreation := state.ArgsAccountCreation{ + Hasher: &hashingMocks.HasherMock{}, + Marshaller: &testscommon.MarshalizerMock{}, + EnableEpochsHandler: &testscommon.EnableEpochsHandlerStub{}, + } + testAccount, _ := state.NewUserAccount(testscommon.TestPubKeyAlice, argsAccCreation) testAccount.UserName = expectedUsername accountsRepository := &stateMock.AccountsRepositoryStub{ GetAccountWithBlockInfoCalled: func(address []byte, options api.AccountQueryOptions) (vmcommon.AccountHandler, common.BlockInfo, error) { @@ -261,7 +282,12 @@ func TestGetCodeHash(t *testing.T) { expectedCodeHash := []byte("hash") - testAccount, _ := state.NewUserAccount(testscommon.TestPubKeyAlice, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + argsAccCreation := state.ArgsAccountCreation{ + Hasher: &hashingMocks.HasherMock{}, + Marshaller: &testscommon.MarshalizerMock{}, + EnableEpochsHandler: &testscommon.EnableEpochsHandlerStub{}, + } + testAccount, _ := state.NewUserAccount(testscommon.TestPubKeyAlice, argsAccCreation) testAccount.CodeHash = expectedCodeHash accountsRepository := &stateMock.AccountsRepositoryStub{ GetAccountWithBlockInfoCalled: func(address []byte, options api.AccountQueryOptions) (vmcommon.AccountHandler, common.BlockInfo, error) { @@ -292,7 +318,7 @@ func TestGetCodeHash(t *testing.T) { func TestNode_GetKeyValuePairs(t *testing.T) { t.Parallel() - acc, _ := state.NewUserAccount([]byte("newaddress"), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acc := createAcc([]byte("newaddress")) k1, v1 := []byte("key1"), []byte("value1") k2, v2 := []byte("key2"), []byte("value2") @@ -360,7 +386,7 @@ func TestNode_GetKeyValuePairs(t *testing.T) { func TestNode_GetKeyValuePairs_GetAllLeavesShouldFail(t *testing.T) { t.Parallel() - acc, _ := state.NewUserAccount([]byte("newaddress"), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acc := createAcc([]byte("newaddress")) accDB := &stateMock.AccountsStub{} @@ -415,7 +441,7 @@ func TestNode_GetKeyValuePairs_GetAllLeavesShouldFail(t *testing.T) { func TestNode_GetKeyValuePairsContextShouldTimeout(t *testing.T) { t.Parallel() - acc, _ := state.NewUserAccount([]byte("newaddress"), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acc := createAcc([]byte("newaddress")) accDB := &stateMock.AccountsStub{} acc.SetDataTrie( @@ -472,7 +498,7 @@ func TestNode_GetKeyValuePairsContextShouldTimeout(t *testing.T) { func TestNode_GetValueForKey(t *testing.T) { t.Parallel() - acc, _ := state.NewUserAccount([]byte("newaddress"), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acc := createAcc([]byte("newaddress")) k1, v1 := []byte("key1"), []byte("value1") _ = acc.SaveKeyValue(k1, v1) @@ -514,7 +540,7 @@ func TestNode_GetValueForKey(t *testing.T) { func TestNode_GetESDTData(t *testing.T) { t.Parallel() - acc, _ := state.NewUserAccount(testscommon.TestPubKeyAlice, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acc := createAcc(testscommon.TestPubKeyAlice) esdtToken := "newToken" esdtData := &esdt.ESDigitalToken{Value: big.NewInt(10)} @@ -563,7 +589,7 @@ func TestNode_GetESDTData(t *testing.T) { func TestNode_GetESDTDataForNFT(t *testing.T) { t.Parallel() - acc, _ := state.NewUserAccount(testscommon.TestPubKeyAlice, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acc := createAcc(testscommon.TestPubKeyAlice) esdtToken := "newToken" nonce := int64(100) @@ -608,7 +634,7 @@ func TestNode_GetESDTDataForNFT(t *testing.T) { func TestNode_GetAllESDTTokens(t *testing.T) { t.Parallel() - acc, _ := state.NewUserAccount(testscommon.TestPubKeyAlice, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acc := createAcc(testscommon.TestPubKeyAlice) esdtToken := "newToken" esdtKey := []byte(core.ElrondProtectedKeyPrefix + core.ESDTKeyIdentifier + esdtToken) @@ -676,7 +702,7 @@ func TestNode_GetAllESDTTokens(t *testing.T) { func TestNode_GetAllESDTTokens_GetAllLeavesShouldFail(t *testing.T) { t.Parallel() - acc, _ := state.NewUserAccount(testscommon.TestPubKeyAlice, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acc := createAcc(testscommon.TestPubKeyAlice) expectedErr := errors.New("expected error") acc.SetDataTrie( @@ -732,7 +758,7 @@ func TestNode_GetAllESDTTokens_GetAllLeavesShouldFail(t *testing.T) { func TestNode_GetAllESDTTokensContextShouldTimeout(t *testing.T) { t.Parallel() - acc, _ := state.NewUserAccount(testscommon.TestPubKeyAlice, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acc := createAcc(testscommon.TestPubKeyAlice) acc.SetDataTrie( &trieMock.TrieStub{ @@ -790,7 +816,7 @@ func TestNode_GetAllESDTTokensContextShouldTimeout(t *testing.T) { func TestNode_GetAllESDTTokensShouldReturnEsdtAndFormattedNft(t *testing.T) { t.Parallel() - acc, _ := state.NewUserAccount(testscommon.TestPubKeyAlice, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acc := createAcc(testscommon.TestPubKeyAlice) esdtToken := "TKKR-7q8w9e" esdtKey := []byte(core.ElrondProtectedKeyPrefix + core.ESDTKeyIdentifier + esdtToken) @@ -886,7 +912,7 @@ func TestNode_GetAllESDTTokensShouldReturnEsdtAndFormattedNft(t *testing.T) { func TestNode_GetAllIssuedESDTs(t *testing.T) { t.Parallel() - acc, _ := state.NewUserAccount([]byte("newaddress"), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acc := createAcc([]byte("newaddress")) esdtToken := []byte("TCK-RANDOM") sftToken := []byte("SFT-RANDOM") nftToken := []byte("NFT-RANDOM") @@ -983,7 +1009,7 @@ func TestNode_GetESDTsWithRole(t *testing.T) { t.Parallel() addrBytes := testscommon.TestPubKeyAlice - acc, _ := state.NewUserAccount(addrBytes, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acc := createAcc(addrBytes) esdtToken := []byte("TCK-RANDOM") specialRoles := []*systemSmartContracts.ESDTRoles{ @@ -1063,7 +1089,7 @@ func TestNode_GetESDTsRoles(t *testing.T) { t.Parallel() addrBytes := testscommon.TestPubKeyAlice - acc, _ := state.NewUserAccount(addrBytes, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acc := createAcc(addrBytes) esdtToken := []byte("TCK-RANDOM") specialRoles := []*systemSmartContracts.ESDTRoles{ @@ -1135,7 +1161,7 @@ func TestNode_GetNFTTokenIDsRegisteredByAddress(t *testing.T) { t.Parallel() addrBytes := testscommon.TestPubKeyAlice - acc, _ := state.NewUserAccount(addrBytes, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acc := createAcc(addrBytes) esdtToken := []byte("TCK-RANDOM") esdtData := &systemSmartContracts.ESDTDataV2{TokenName: []byte("fungible"), TokenType: []byte(core.SemiFungibleESDT), OwnerAddress: addrBytes} @@ -1200,7 +1226,7 @@ func TestNode_GetNFTTokenIDsRegisteredByAddressContextShouldTimeout(t *testing.T t.Parallel() addrBytes := testscommon.TestPubKeyAlice - acc, _ := state.NewUserAccount(addrBytes, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acc := createAcc(addrBytes) acc.SetDataTrie( &trieMock.TrieStub{ @@ -1371,7 +1397,7 @@ func TestGenerateTransaction_GetAccountReturnsNilShouldWork(t *testing.T) { accAdapter := &stateMock.AccountsStub{ GetExistingAccountCalled: func(address []byte) (vmcommon.AccountHandler, error) { - return state.NewUserAccount(address, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + return createAcc(address), nil }, } privateKey := getPrivateKey() @@ -1518,7 +1544,7 @@ func TestGenerateTransaction_ShouldSetCorrectNonce(t *testing.T) { nonce := uint64(7) accAdapter := &stateMock.AccountsStub{ GetExistingAccountCalled: func(address []byte) (vmcommon.AccountHandler, error) { - acc, _ := state.NewUserAccount(address, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acc := createAcc(address) _ = acc.AddToBalance(big.NewInt(0)) acc.IncreaseNonce(nonce) @@ -2347,7 +2373,7 @@ func TestCreateTransaction_OkValsShouldWork(t *testing.T) { stateComponents := getDefaultStateComponents() stateComponents.AccountsAPI = &stateMock.AccountsStub{ GetExistingAccountCalled: func(addressContainer []byte) (vmcommon.AccountHandler, error) { - return state.NewUserAccount([]byte("address"), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + return createAcc([]byte("address")), nil }, } @@ -2944,7 +2970,7 @@ func TestNode_GetAccountAccountsRepositoryFailsShouldErr(t *testing.T) { func TestNode_GetAccountAccountExistsShouldReturn(t *testing.T) { t.Parallel() - accnt, _ := state.NewUserAccount(testscommon.TestPubKeyBob, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + accnt := createAcc(testscommon.TestPubKeyBob) _ = accnt.AddToBalance(big.NewInt(1)) accnt.IncreaseNonce(2) accnt.SetRootHash([]byte("root hash")) diff --git a/node/trieIterators/delegatedListProcessor_test.go b/node/trieIterators/delegatedListProcessor_test.go index 0c241d8ea39..ee42beecabd 100644 --- a/node/trieIterators/delegatedListProcessor_test.go +++ b/node/trieIterators/delegatedListProcessor_test.go @@ -10,17 +10,11 @@ import ( "time" "github.com/ElrondNetwork/elrond-go-core/core/check" - "github.com/ElrondNetwork/elrond-go-core/core/keyValStorage" "github.com/ElrondNetwork/elrond-go-core/data/api" - "github.com/ElrondNetwork/elrond-go/common" "github.com/ElrondNetwork/elrond-go/epochStart" "github.com/ElrondNetwork/elrond-go/node/mock" "github.com/ElrondNetwork/elrond-go/process" - "github.com/ElrondNetwork/elrond-go/state" - "github.com/ElrondNetwork/elrond-go/testscommon" - "github.com/ElrondNetwork/elrond-go/testscommon/hashingMocks" stateMock "github.com/ElrondNetwork/elrond-go/testscommon/state" - trieMock "github.com/ElrondNetwork/elrond-go/testscommon/trie" vmcommon "github.com/ElrondNetwork/elrond-vm-common" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -126,7 +120,7 @@ func TestDelegatedListProc_GetDelegatorsListContextShouldTimeout(t *testing.T) { } arg.Accounts.AccountsAdapter = &stateMock.AccountsStub{ GetExistingAccountCalled: func(addressContainer []byte) (vmcommon.AccountHandler, error) { - return createDelegationScAccount(addressContainer, delegators, addressContainer, time.Second), nil + return createScAccount(addressContainer, delegators, addressContainer, time.Second), nil }, RecreateTrieCalled: func(rootHash []byte) error { return nil @@ -173,7 +167,7 @@ func TestDelegatedListProc_GetDelegatorsListShouldWork(t *testing.T) { } arg.Accounts.AccountsAdapter = &stateMock.AccountsStub{ GetExistingAccountCalled: func(addressContainer []byte) (vmcommon.AccountHandler, error) { - return createDelegationScAccount(addressContainer, delegators, addressContainer, 0), nil + return createScAccount(addressContainer, delegators, addressContainer, 0), nil }, RecreateTrieCalled: func(rootHash []byte) error { return nil @@ -218,28 +212,3 @@ func TestDelegatedListProc_GetDelegatorsListShouldWork(t *testing.T) { assert.Equal(t, []*api.Delegator{&expectedDelegator1, &expectedDelegator2}, delegatorsValues) } - -func createDelegationScAccount(address []byte, leaves [][]byte, rootHash []byte, timeSleep time.Duration) state.UserAccountHandler { - acc, _ := state.NewUserAccount(address, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acc.SetDataTrie(&trieMock.TrieStub{ - RootCalled: func() ([]byte, error) { - return rootHash, nil - }, - GetAllLeavesOnChannelCalled: func(leavesChannels *common.TrieIteratorChannels, ctx context.Context, rootHash []byte, _ common.KeyBuilder) error { - go func() { - time.Sleep(timeSleep) - for _, leafBuff := range leaves { - leaf := keyValStorage.NewKeyValStorage(leafBuff, nil) - leavesChannels.LeavesChan <- leaf - } - - close(leavesChannels.LeavesChan) - close(leavesChannels.ErrChan) - }() - - return nil - }, - }) - - return acc -} diff --git a/node/trieIterators/directStakedListProcessor_test.go b/node/trieIterators/directStakedListProcessor_test.go index eeef8b11355..89d976786c2 100644 --- a/node/trieIterators/directStakedListProcessor_test.go +++ b/node/trieIterators/directStakedListProcessor_test.go @@ -77,7 +77,7 @@ func TestDirectStakedListProc_GetDelegatorsListContextShouldTimeout(t *testing.T } arg.Accounts.AccountsAdapter = &stateMock.AccountsStub{ GetExistingAccountCalled: func(addressContainer []byte) (vmcommon.AccountHandler, error) { - return createValidatorScAccount(addressContainer, validators, addressContainer, time.Second), nil + return createScAccount(addressContainer, validators, addressContainer, time.Second), nil }, RecreateTrieCalled: func(rootHash []byte) error { return nil @@ -121,7 +121,7 @@ func TestDirectStakedListProc_GetDelegatorsListShouldWork(t *testing.T) { } arg.Accounts.AccountsAdapter = &stateMock.AccountsStub{ GetExistingAccountCalled: func(addressContainer []byte) (vmcommon.AccountHandler, error) { - return createValidatorScAccount(addressContainer, validators, addressContainer, 0), nil + return createScAccount(addressContainer, validators, addressContainer, 0), nil }, RecreateTrieCalled: func(rootHash []byte) error { return nil @@ -149,8 +149,13 @@ func TestDirectStakedListProc_GetDelegatorsListShouldWork(t *testing.T) { assert.Equal(t, []*api.DirectStakedValue{&expectedDirectStake1, &expectedDirectStake2}, directStakedList) } -func createValidatorScAccount(address []byte, leaves [][]byte, rootHash []byte, timeSleep time.Duration) state.UserAccountHandler { - acc, _ := state.NewUserAccount(address, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) +func createScAccount(address []byte, leaves [][]byte, rootHash []byte, timeSleep time.Duration) state.UserAccountHandler { + argsAccCreation := state.ArgsAccountCreation{ + Hasher: &hashingMocks.HasherMock{}, + Marshaller: &testscommon.MarshalizerMock{}, + EnableEpochsHandler: &testscommon.EnableEpochsHandlerStub{}, + } + acc, _ := state.NewUserAccount(address, argsAccCreation) acc.SetDataTrie(&trieMock.TrieStub{ RootCalled: func() ([]byte, error) { return rootHash, nil diff --git a/node/trieIterators/stakeValuesProcessor_test.go b/node/trieIterators/stakeValuesProcessor_test.go index edcb25688be..b9b18a0c872 100644 --- a/node/trieIterators/stakeValuesProcessor_test.go +++ b/node/trieIterators/stakeValuesProcessor_test.go @@ -166,7 +166,12 @@ func TestTotalStakedValueProcessor_GetTotalStakedValue_CannotGetRootHash(t *test t.Parallel() expectedErr := errors.New("expected error") - acc, _ := state.NewUserAccount([]byte("newaddress"), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + argsAccCreation := state.ArgsAccountCreation{ + Hasher: &hashingMocks.HasherMock{}, + Marshaller: &testscommon.MarshalizerMock{}, + EnableEpochsHandler: &testscommon.EnableEpochsHandlerStub{}, + } + acc, _ := state.NewUserAccount([]byte("newaddress"), argsAccCreation) acc.SetDataTrie(&trieMock.TrieStub{ RootCalled: func() ([]byte, error) { return nil, expectedErr @@ -192,7 +197,12 @@ func TestTotalStakedValueProcessor_GetTotalStakedValue_CannotGetRootHash(t *test func TestTotalStakedValueProcessor_GetTotalStakedValue_ContextShouldTimeout(t *testing.T) { t.Parallel() - acc, _ := state.NewUserAccount([]byte("newaddress"), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + argsAccCreation := state.ArgsAccountCreation{ + Hasher: &hashingMocks.HasherMock{}, + Marshaller: &testscommon.MarshalizerMock{}, + EnableEpochsHandler: &testscommon.EnableEpochsHandlerStub{}, + } + acc, _ := state.NewUserAccount([]byte("newaddress"), argsAccCreation) acc.SetDataTrie(&trieMock.TrieStub{ GetAllLeavesOnChannelCalled: func(leavesChannels *common.TrieIteratorChannels, _ context.Context, _ []byte, _ common.KeyBuilder) error { time.Sleep(time.Second) @@ -228,7 +238,12 @@ func TestTotalStakedValueProcessor_GetTotalStakedValue_CannotGetAllLeaves(t *tes t.Parallel() expectedErr := errors.New("expected error") - acc, _ := state.NewUserAccount([]byte("newaddress"), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + argsAccCreation := state.ArgsAccountCreation{ + Hasher: &hashingMocks.HasherMock{}, + Marshaller: &testscommon.MarshalizerMock{}, + EnableEpochsHandler: &testscommon.EnableEpochsHandlerStub{}, + } + acc, _ := state.NewUserAccount([]byte("newaddress"), argsAccCreation) acc.SetDataTrie(&trieMock.TrieStub{ GetAllLeavesOnChannelCalled: func(_ *common.TrieIteratorChannels, _ context.Context, _ []byte, _ common.KeyBuilder) error { return expectedErr @@ -273,7 +288,12 @@ func TestTotalStakedValueProcessor_GetTotalStakedValue(t *testing.T) { leafKey4 := "0123456783" leafKey5 := "0123456780" leafKey6 := "0123456788" - acc, _ := state.NewUserAccount([]byte("newaddress"), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + argsAccCreation := state.ArgsAccountCreation{ + Hasher: &hashingMocks.HasherMock{}, + Marshaller: &testscommon.MarshalizerMock{}, + EnableEpochsHandler: &testscommon.EnableEpochsHandlerStub{}, + } + acc, _ := state.NewUserAccount([]byte("newaddress"), argsAccCreation) acc.SetDataTrie(&trieMock.TrieStub{ RootCalled: func() ([]byte, error) { return rootHash, nil diff --git a/process/block/baseProcess.go b/process/block/baseProcess.go index 3a4da5d7c5c..a7c31a6e535 100644 --- a/process/block/baseProcess.go +++ b/process/block/baseProcess.go @@ -1744,7 +1744,7 @@ func (bp *baseProcessor) commitTrieEpochRootHashIfNeeded(metaBlock *block.MetaBl totalSizeAccountsDataTries := 0 totalSizeCodeLeaves := 0 for leaf := range iteratorChannels.LeavesChan { - userAccount, errUnmarshal := unmarshalUserAccount(leaf.Key(), leaf.Value(), bp.marshalizer, bp.hasher) + userAccount, errUnmarshal := bp.unmarshalUserAccount(leaf.Key(), leaf.Value()) if errUnmarshal != nil { numCodeLeaves++ totalSizeCodeLeaves += len(leaf.Value()) @@ -1814,17 +1814,20 @@ func (bp *baseProcessor) commitTrieEpochRootHashIfNeeded(metaBlock *block.MetaBl return nil } -func unmarshalUserAccount( +func (bp *baseProcessor) unmarshalUserAccount( address []byte, userAccountsBytes []byte, - marshalizer marshal.Marshalizer, - hasher hashing.Hasher, ) (state.UserAccountHandler, error) { - userAccount, err := state.NewUserAccount(address, hasher, marshalizer) + argsAccCreation := state.ArgsAccountCreation{ + Hasher: bp.hasher, + Marshaller: bp.marshalizer, + EnableEpochsHandler: bp.enableEpochsHandler, + } + userAccount, err := state.NewUserAccount(address, argsAccCreation) if err != nil { return nil, err } - err = marshalizer.Unmarshal(userAccount, userAccountsBytes) + err = bp.marshalizer.Unmarshal(userAccount, userAccountsBytes) if err != nil { return nil, err } diff --git a/process/dataValidators/txValidator_test.go b/process/dataValidators/txValidator_test.go index df26a6709da..6d975e60539 100644 --- a/process/dataValidators/txValidator_test.go +++ b/process/dataValidators/txValidator_test.go @@ -21,7 +21,12 @@ import ( func getAccAdapter(nonce uint64, balance *big.Int) *stateMock.AccountsStub { accDB := &stateMock.AccountsStub{} accDB.GetExistingAccountCalled = func(address []byte) (handler vmcommon.AccountHandler, e error) { - acc, _ := state.NewUserAccount(address, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + argsAccCreation := state.ArgsAccountCreation{ + Hasher: &hashingMocks.HasherMock{}, + Marshaller: &testscommon.MarshalizerMock{}, + EnableEpochsHandler: &testscommon.EnableEpochsHandlerStub{}, + } + acc, _ := state.NewUserAccount(address, argsAccCreation) acc.Nonce = nonce acc.Balance = balance @@ -331,7 +336,7 @@ func TestTxValidator_CheckTxValidityWrongAccountTypeShouldReturnFalse(t *testing accDB := &stateMock.AccountsStub{} accDB.GetExistingAccountCalled = func(address []byte) (handler vmcommon.AccountHandler, e error) { - return state.NewPeerAccount(address, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + return state.NewPeerAccount(address) } shardCoordinator := createMockCoordinator("_", 0) maxNonceDeltaAllowed := 100 diff --git a/process/peer/process_test.go b/process/peer/process_test.go index b5c9673ca0f..bf7848fc2da 100644 --- a/process/peer/process_test.go +++ b/process/peer/process_test.go @@ -26,7 +26,6 @@ import ( "github.com/ElrondNetwork/elrond-go/testscommon" dataRetrieverMock "github.com/ElrondNetwork/elrond-go/testscommon/dataRetriever" "github.com/ElrondNetwork/elrond-go/testscommon/epochNotifier" - "github.com/ElrondNetwork/elrond-go/testscommon/hashingMocks" "github.com/ElrondNetwork/elrond-go/testscommon/shardingMocks" stateMock "github.com/ElrondNetwork/elrond-go/testscommon/state" storageStubs "github.com/ElrondNetwork/elrond-go/testscommon/storage" @@ -325,7 +324,7 @@ func TestValidatorStatisticsProcessor_SaveInitialStateSetAddressErrors(t *testin t.Parallel() saveAccountError := errors.New("save account error") - peerAccount, _ := state.NewPeerAccount([]byte("1234"), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + peerAccount, _ := state.NewPeerAccount([]byte("1234")) peerAdapter := &stateMock.AccountsStub{ LoadAccountCalled: func(address []byte) (handler vmcommon.AccountHandler, e error) { return peerAccount, nil @@ -351,7 +350,7 @@ func TestValidatorStatisticsProcessor_SaveInitialStateCommitErrors(t *testing.T) t.Parallel() commitError := errors.New("commit error") - peerAccount, _ := state.NewPeerAccount([]byte("1234"), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + peerAccount, _ := state.NewPeerAccount([]byte("1234")) peerAdapter := &stateMock.AccountsStub{ LoadAccountCalled: func(address []byte) (handler vmcommon.AccountHandler, e error) { return peerAccount, nil @@ -374,7 +373,7 @@ func TestValidatorStatisticsProcessor_SaveInitialStateCommitErrors(t *testing.T) func TestValidatorStatisticsProcessor_SaveInitialStateCommit(t *testing.T) { t.Parallel() - peerAccount, _ := state.NewPeerAccount([]byte("1234"), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + peerAccount, _ := state.NewPeerAccount([]byte("1234")) peerAdapter := &stateMock.AccountsStub{ LoadAccountCalled: func(address []byte) (handler vmcommon.AccountHandler, e error) { return peerAccount, nil @@ -516,7 +515,7 @@ func TestValidatorStatisticsProcessor_UpdatePeerStateGetHeaderError(t *testing.T marshalizer := &mock.MarshalizerStub{} adapter.LoadAccountCalled = func(address []byte) (handler vmcommon.AccountHandler, e error) { - return state.NewPeerAccount(address, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + return state.NewPeerAccount(address) } shardCoordinatorMock := mock.NewOneShardCoordinatorMock() @@ -2487,7 +2486,7 @@ func compare(t *testing.T, peerAccount state.PeerAccountHandler, validatorInfo * func createPeerAccounts(addrBytes0 []byte, addrBytesMeta []byte) (state.PeerAccountHandler, state.PeerAccountHandler) { addr := addrBytes0 - pa0, _ := state.NewPeerAccount(addr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + pa0, _ := state.NewPeerAccount(addr) pa0.PeerAccountData = state.PeerAccountData{ BLSPublicKey: []byte("bls0"), RewardAddress: []byte("reward0"), @@ -2518,7 +2517,7 @@ func createPeerAccounts(addrBytes0 []byte, addrBytesMeta []byte) (state.PeerAcco } addr = addrBytesMeta - paMeta, _ := state.NewPeerAccount(addr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + paMeta, _ := state.NewPeerAccount(addr) paMeta.PeerAccountData = state.PeerAccountData{ BLSPublicKey: []byte("blsM"), RewardAddress: []byte("rewardM"), diff --git a/process/rewardTransaction/process_test.go b/process/rewardTransaction/process_test.go index 5acb18d90d6..173086b450f 100644 --- a/process/rewardTransaction/process_test.go +++ b/process/rewardTransaction/process_test.go @@ -188,7 +188,12 @@ func TestRewardTxProcessor_ProcessRewardTransactionShouldWork(t *testing.T) { accountsDb := &stateMock.AccountsStub{ LoadAccountCalled: func(address []byte) (vmcommon.AccountHandler, error) { - return state.NewUserAccount(address, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + argsAccCreation := state.ArgsAccountCreation{ + Hasher: &hashingMocks.HasherMock{}, + Marshaller: &testscommon.MarshalizerMock{}, + EnableEpochsHandler: &testscommon.EnableEpochsHandlerStub{}, + } + return state.NewUserAccount(address, argsAccCreation) }, SaveAccountCalled: func(accountHandler vmcommon.AccountHandler) error { saveAccountWasCalled = true @@ -220,7 +225,12 @@ func TestRewardTxProcessor_ProcessRewardTransactionToASmartContractShouldWork(t saveAccountWasCalled := false address := []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6} - userAccount, _ := state.NewUserAccount(address, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + argsAccCreation := state.ArgsAccountCreation{ + Hasher: &hashingMocks.HasherMock{}, + Marshaller: &testscommon.MarshalizerMock{}, + EnableEpochsHandler: &testscommon.EnableEpochsHandlerStub{}, + } + userAccount, _ := state.NewUserAccount(address, argsAccCreation) accountsDb := &stateMock.AccountsStub{ LoadAccountCalled: func(address []byte) (vmcommon.AccountHandler, error) { return userAccount, nil diff --git a/process/scToProtocol/stakingToPeer_test.go b/process/scToProtocol/stakingToPeer_test.go index e2efa9be5ea..b6f90f3fcd6 100644 --- a/process/scToProtocol/stakingToPeer_test.go +++ b/process/scToProtocol/stakingToPeer_test.go @@ -57,6 +57,16 @@ func createBlockBody() *block.Body { } } +func createStakingScAccount() state.UserAccountHandler { + argsAccCreation := state.ArgsAccountCreation{ + Hasher: &hashingMocks.HasherMock{}, + Marshaller: &testscommon.MarshalizerMock{}, + EnableEpochsHandler: &testscommon.EnableEpochsHandlerStub{}, + } + userAcc, _ := state.NewUserAccount(vm.StakingSCAddress, argsAccCreation) + return userAcc +} + func TestNewStakingToPeerNilAddrConverterShouldErr(t *testing.T) { t.Parallel() @@ -240,7 +250,7 @@ func TestStakingToPeer_UpdateProtocolRemoveAccountShouldReturnNil(t *testing.T) peerState := &stateMock.AccountsStub{} peerState.LoadAccountCalled = func(address []byte) (handler vmcommon.AccountHandler, e error) { - peerAcc, _ := state.NewPeerAccount(address, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + peerAcc, _ := state.NewPeerAccount(address) _ = peerAcc.SetRewardAddress([]byte("addr")) _ = peerAcc.SetBLSPublicKey([]byte("BlsAddr")) @@ -256,7 +266,7 @@ func TestStakingToPeer_UpdateProtocolRemoveAccountShouldReturnNil(t *testing.T) } arguments := createMockArgumentsNewStakingToPeer() - userAcc, _ := state.NewUserAccount(vm.StakingSCAddress, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + userAcc := createStakingScAccount() baseState := &stateMock.AccountsStub{} baseState.LoadAccountCalled = func(address []byte) (vmcommon.AccountHandler, error) { return userAcc, nil @@ -299,7 +309,7 @@ func TestStakingToPeer_UpdateProtocolCannotSetRewardAddressShouldErr(t *testing. peerState := &stateMock.AccountsStub{} peerState.LoadAccountCalled = func(address []byte) (handler vmcommon.AccountHandler, e error) { - peerAcc, _ := state.NewPeerAccount(address, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + peerAcc, _ := state.NewPeerAccount(address) _ = peerAcc.SetRewardAddress([]byte("key")) return peerAcc, nil @@ -310,7 +320,7 @@ func TestStakingToPeer_UpdateProtocolCannotSetRewardAddressShouldErr(t *testing. } marshalizer := &mock.MarshalizerMock{} - userAcc, _ := state.NewUserAccount(vm.StakingSCAddress, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + userAcc := createStakingScAccount() baseState := &stateMock.AccountsStub{} baseState.LoadAccountCalled = func(address []byte) (vmcommon.AccountHandler, error) { return userAcc, nil @@ -356,7 +366,7 @@ func TestStakingToPeer_UpdateProtocolEmptyDataShouldNotAddToTrie(t *testing.T) { peerState := &stateMock.AccountsStub{} peerState.LoadAccountCalled = func(address []byte) (handler vmcommon.AccountHandler, e error) { - peerAcc, _ := state.NewPeerAccount(address, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + peerAcc, _ := state.NewPeerAccount(address) _ = peerAcc.SetRewardAddress(rwdAddress) return peerAcc, nil @@ -366,7 +376,7 @@ func TestStakingToPeer_UpdateProtocolEmptyDataShouldNotAddToTrie(t *testing.T) { return fmt.Errorf("error") } - userAcc, _ := state.NewUserAccount(vm.StakingSCAddress, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + userAcc := createStakingScAccount() baseState := &stateMock.AccountsStub{} baseState.LoadAccountCalled = func(address []byte) (vmcommon.AccountHandler, error) { return userAcc, nil @@ -422,7 +432,7 @@ func TestStakingToPeer_UpdateProtocolCannotSaveAccountShouldErr(t *testing.T) { } peerState.LoadAccountCalled = func(address []byte) (handler vmcommon.AccountHandler, e error) { - peerAccount, _ := state.NewPeerAccount(address, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + peerAccount, _ := state.NewPeerAccount(address) peerAccount.RewardAddress = address return peerAccount, nil } @@ -433,7 +443,7 @@ func TestStakingToPeer_UpdateProtocolCannotSaveAccountShouldErr(t *testing.T) { } marshalizer := &mock.MarshalizerMock{} - userAcc, _ := state.NewUserAccount(vm.StakingSCAddress, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + userAcc := createStakingScAccount() baseState := &stateMock.AccountsStub{} baseState.LoadAccountCalled = func(address []byte) (vmcommon.AccountHandler, error) { return userAcc, nil @@ -484,7 +494,7 @@ func TestStakingToPeer_UpdateProtocolCannotSaveAccountNonceShouldErr(t *testing. }, } peerState.LoadAccountCalled = func(address []byte) (handler vmcommon.AccountHandler, e error) { - peerAccount, _ := state.NewPeerAccount([]byte("1234"), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + peerAccount, _ := state.NewPeerAccount([]byte("1234")) peerAccount.BLSPublicKey = address peerAccount.Nonce = 1 return peerAccount, nil @@ -496,7 +506,7 @@ func TestStakingToPeer_UpdateProtocolCannotSaveAccountNonceShouldErr(t *testing. } marshalizer := &mock.MarshalizerMock{} - userAcc, _ := state.NewUserAccount(vm.StakingSCAddress, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + userAcc := createStakingScAccount() baseState := &stateMock.AccountsStub{} baseState.LoadAccountCalled = func(address []byte) (vmcommon.AccountHandler, error) { return userAcc, nil @@ -546,7 +556,7 @@ func TestStakingToPeer_UpdateProtocol(t *testing.T) { }, } peerState.LoadAccountCalled = func(address []byte) (handler vmcommon.AccountHandler, e error) { - peerAccount, _ := state.NewPeerAccount([]byte("1234"), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + peerAccount, _ := state.NewPeerAccount([]byte("1234")) peerAccount.BLSPublicKey = address peerAccount.Nonce = 1 return peerAccount, nil @@ -562,7 +572,7 @@ func TestStakingToPeer_UpdateProtocol(t *testing.T) { arguments.CurrTxs = currTx arguments.PeerState = peerState arguments.Marshalizer = marshalizer - userAcc, _ := state.NewUserAccount(vm.StakingSCAddress, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + userAcc := createStakingScAccount() baseState := &stateMock.AccountsStub{} baseState.LoadAccountCalled = func(address []byte) (vmcommon.AccountHandler, error) { return userAcc, nil @@ -609,7 +619,7 @@ func TestStakingToPeer_UpdateProtocolCannotSaveUnStakedNonceShouldErr(t *testing }, } peerState.LoadAccountCalled = func(address []byte) (handler vmcommon.AccountHandler, e error) { - peerAccount, _ := state.NewPeerAccount([]byte("1234"), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + peerAccount, _ := state.NewPeerAccount([]byte("1234")) peerAccount.BLSPublicKey = address peerAccount.IndexInList = 1 return peerAccount, nil @@ -621,7 +631,7 @@ func TestStakingToPeer_UpdateProtocolCannotSaveUnStakedNonceShouldErr(t *testing } marshalizer := &mock.MarshalizerMock{} - userAcc, _ := state.NewUserAccount(vm.StakingSCAddress, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + userAcc := createStakingScAccount() baseState := &stateMock.AccountsStub{} baseState.LoadAccountCalled = func(address []byte) (vmcommon.AccountHandler, error) { return userAcc, nil diff --git a/process/smartContract/hooks/blockChainHook_test.go b/process/smartContract/hooks/blockChainHook_test.go index 768e1bd90ce..816c1f1d164 100644 --- a/process/smartContract/hooks/blockChainHook_test.go +++ b/process/smartContract/hooks/blockChainHook_test.go @@ -77,6 +77,16 @@ func createContractCallInput(function string, sender, receiver []byte) *vmcommon } } +func createAccount(address []byte) state.UserAccountHandler { + argsAccCreation := state.ArgsAccountCreation{ + Hasher: &hashingMocks.HasherMock{}, + Marshaller: &testscommon.MarshalizerMock{}, + EnableEpochsHandler: &testscommon.EnableEpochsHandlerStub{}, + } + account, _ := state.NewUserAccount(address, argsAccCreation) + return account +} + func TestNewBlockChainHookImpl(t *testing.T) { t.Parallel() @@ -251,10 +261,10 @@ func TestBlockChainHookImpl_GetCode(t *testing.T) { } bh, _ := hooks.NewBlockChainHookImpl(args) - account, _ := state.NewUserAccount([]byte("address"), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + account := createAccount([]byte("address")) account.SetCodeHash(expectedCodeHash) - code := bh.GetCode(account) + code := bh.GetCode(account.(vmcommon.UserAccountHandler)) require.Equal(t, expectedCode, code) }) } @@ -311,7 +321,7 @@ func TestBlockChainHookImpl_GetUserAccountWrongTypeShouldErr(t *testing.T) { func TestBlockChainHookImpl_GetUserAccount(t *testing.T) { t.Parallel() - expectedAccount, _ := state.NewUserAccount([]byte("1234"), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + expectedAccount := createAccount([]byte("1234")) args := createMockBlockChainHookArgs() args.Accounts = &stateMock.AccountsStub{ GetExistingAccountCalled: func(address []byte) (handler vmcommon.AccountHandler, e error) { @@ -319,7 +329,7 @@ func TestBlockChainHookImpl_GetUserAccount(t *testing.T) { }, } bh, _ := hooks.NewBlockChainHookImpl(args) - acc, err := bh.GetUserAccount(expectedAccount.Address) + acc, err := bh.GetUserAccount(expectedAccount.AddressBytes()) assert.Nil(t, err) assert.Equal(t, expectedAccount, acc) @@ -421,7 +431,7 @@ func TestBlockChainHookImpl_NewAddressLengthNoGood(t *testing.T) { acnts := &stateMock.AccountsStub{} acnts.GetExistingAccountCalled = func(address []byte) (vmcommon.AccountHandler, error) { - return state.NewUserAccount(address, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + return createAccount(address).(vmcommon.AccountHandler), nil } args := createMockBlockChainHookArgs() args.Accounts = acnts @@ -445,7 +455,7 @@ func TestBlockChainHookImpl_NewAddressVMTypeTooLong(t *testing.T) { acnts := &stateMock.AccountsStub{} acnts.GetExistingAccountCalled = func(address []byte) (vmcommon.AccountHandler, error) { - return state.NewUserAccount(address, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + return createAccount(address).(vmcommon.AccountHandler), nil } args := createMockBlockChainHookArgs() args.Accounts = acnts @@ -465,7 +475,7 @@ func TestBlockChainHookImpl_NewAddress(t *testing.T) { acnts := &stateMock.AccountsStub{} acnts.GetExistingAccountCalled = func(address []byte) (vmcommon.AccountHandler, error) { - return state.NewUserAccount(address, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + return createAccount(address).(vmcommon.AccountHandler), nil } args := createMockBlockChainHookArgs() args.Accounts = acnts diff --git a/process/smartContract/process_test.go b/process/smartContract/process_test.go index b291daa7475..8a5e8e93818 100644 --- a/process/smartContract/process_test.go +++ b/process/smartContract/process_test.go @@ -48,14 +48,24 @@ func createMockPubkeyConverter() *mock.PubkeyConverterMock { return mock.NewPubkeyConverterMock(32) } +func createAccount(address []byte) state.UserAccountHandler { + argsAccCreation := state.ArgsAccountCreation{ + Hasher: &hashingMocks.HasherMock{}, + Marshaller: &testscommon.MarshalizerMock{}, + EnableEpochsHandler: &testscommon.EnableEpochsHandlerStub{}, + } + acc, _ := state.NewUserAccount(address, argsAccCreation) + return acc +} + func createAccounts(tx data.TransactionHandler) (state.UserAccountHandler, state.UserAccountHandler) { - acntSrc, _ := state.NewUserAccount(tx.GetSndAddr(), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntSrc.Balance = acntSrc.Balance.Add(acntSrc.Balance, tx.GetValue()) + acntSrc := createAccount(tx.GetSndAddr()) + _ = acntSrc.AddToBalance(tx.GetValue()) totalFee := big.NewInt(0) totalFee = totalFee.Mul(big.NewInt(int64(tx.GetGasLimit())), big.NewInt(int64(tx.GetGasPrice()))) - acntSrc.Balance.Set(acntSrc.Balance.Add(acntSrc.Balance, totalFee)) + _ = acntSrc.AddToBalance(totalFee) - acntDst, _ := state.NewUserAccount(tx.GetRcvAddr(), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acntDst := createAccount(tx.GetRcvAddr()) return acntSrc, acntDst } @@ -2146,7 +2156,7 @@ func TestScProcessor_GetAccountFromAddr(t *testing.T) { getCalled := 0 accountsDB.LoadAccountCalled = func(address []byte) (handler vmcommon.AccountHandler, e error) { getCalled++ - acc, _ := state.NewUserAccount(address, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acc := createAccount(address) return acc, nil } @@ -2250,8 +2260,7 @@ func TestScProcessor_DeleteAccountsInShard(t *testing.T) { accountsDB := &stateMock.AccountsStub{} removeCalled := 0 accountsDB.LoadAccountCalled = func(address []byte) (handler vmcommon.AccountHandler, e error) { - acc, _ := state.NewUserAccount(address, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - return acc, nil + return createAccount(address), nil } accountsDB.RemoveAccountCalled = func(address []byte) error { removeCalled++ @@ -2328,7 +2337,7 @@ func TestScProcessor_ProcessSCPaymentNotEnoughBalance(t *testing.T) { tx.GasPrice = 10 tx.GasLimit = 15 - acntSrc, _ := state.NewUserAccount(tx.SndAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acntSrc := createAccount(tx.SndAddr) _ = acntSrc.AddToBalance(big.NewInt(45)) currBalance := acntSrc.GetBalance().Uint64() @@ -2618,7 +2627,7 @@ func TestScProcessor_processSCOutputAccounts(t *testing.T) { outputAccounts = append(outputAccounts, outacc1) testAddr := outaddress - testAcc, _ := state.NewUserAccount(testAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + testAcc := createAccount(testAddr) accountsDB.LoadAccountCalled = func(address []byte) (handler vmcommon.AccountHandler, e error) { if bytes.Equal(address, testAddr) { @@ -2645,11 +2654,11 @@ func TestScProcessor_processSCOutputAccounts(t *testing.T) { outacc1.BalanceDelta = big.NewInt(int64(10)) tx.Value = big.NewInt(int64(10)) - currentBalance := testAcc.Balance.Uint64() + currentBalance := testAcc.GetBalance().Uint64() vmOutBalance := outacc1.BalanceDelta.Uint64() _, _, err = sc.processSCOutputAccounts(&vmcommon.VMOutput{}, vmData.DirectCall, outputAccounts, tx, []byte("hash")) require.Nil(t, err) - require.Equal(t, currentBalance+vmOutBalance, testAcc.Balance.Uint64()) + require.Equal(t, currentBalance+vmOutBalance, testAcc.GetBalance().Uint64()) } func TestScProcessor_processSCOutputAccountsNotInShard(t *testing.T) { @@ -2691,7 +2700,7 @@ func TestScProcessor_processSCOutputAccountsNotInShard(t *testing.T) { func TestScProcessor_CreateCrossShardTransactions(t *testing.T) { t.Parallel() - testAccounts, _ := state.NewUserAccount([]byte("address"), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + testAccounts := createAccount([]byte("address")) accountsDB := &stateMock.AccountsStub{ LoadAccountCalled: func(address []byte) (handler vmcommon.AccountHandler, err error) { return testAccounts, nil @@ -2738,7 +2747,7 @@ func TestScProcessor_CreateCrossShardTransactions(t *testing.T) { func TestScProcessor_CreateCrossShardTransactionsWithAsyncCalls(t *testing.T) { t.Parallel() - testAccounts, _ := state.NewUserAccount([]byte("address"), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + testAccounts := createAccount([]byte("address")) accountsDB := &stateMock.AccountsStub{ LoadAccountCalled: func(address []byte) (handler vmcommon.AccountHandler, err error) { return testAccounts, nil @@ -2830,7 +2839,7 @@ func TestScProcessor_CreateCrossShardTransactionsWithAsyncCalls(t *testing.T) { func TestScProcessor_CreateIntraShardTransactionsWithAsyncCalls(t *testing.T) { t.Parallel() - testAccounts, _ := state.NewUserAccount([]byte("address"), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + testAccounts := createAccount([]byte("address")) accountsDB := &stateMock.AccountsStub{ LoadAccountCalled: func(address []byte) (handler vmcommon.AccountHandler, err error) { return testAccounts, nil @@ -2971,10 +2980,10 @@ func TestScProcessor_ProcessSmartContractResultBadAccType(t *testing.T) { func TestScProcessor_ProcessSmartContractResultNotPayable(t *testing.T) { t.Parallel() - userAcc, _ := state.NewUserAccount([]byte("recv address"), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + userAcc := createAccount([]byte("recv address")) accountsDB := &stateMock.AccountsStub{ - LoadAccountCalled: func(address []byte) (handler vmcommon.AccountHandler, e error) { - if bytes.Equal(address, userAcc.Address) { + LoadAccountCalled: func(address []byte) (vmcommon.AccountHandler, error) { + if bytes.Equal(address, userAcc.AddressBytes()) { return userAcc, nil } return state.NewEmptyUserAccount(), nil @@ -3001,7 +3010,7 @@ func TestScProcessor_ProcessSmartContractResultNotPayable(t *testing.T) { require.Nil(t, err) scr := smartContractResult.SmartContractResult{ - RcvAddr: userAcc.Address, + RcvAddr: userAcc.AddressBytes(), SndAddr: []byte("snd addr"), Value: big.NewInt(0), } @@ -3026,8 +3035,8 @@ func TestScProcessor_ProcessSmartContractResultOutputBalanceNil(t *testing.T) { t.Parallel() accountsDB := &stateMock.AccountsStub{ - LoadAccountCalled: func(address []byte) (handler vmcommon.AccountHandler, e error) { - return state.NewUserAccount(address, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + LoadAccountCalled: func(address []byte) (vmcommon.AccountHandler, error) { + return createAccount(address), nil }, SaveAccountCalled: func(accountHandler vmcommon.AccountHandler) error { return nil @@ -3056,7 +3065,7 @@ func TestScProcessor_ProcessSmartContractResultWithCode(t *testing.T) { putCodeCalled := 0 accountsDB := &stateMock.AccountsStub{ LoadAccountCalled: func(address []byte) (handler vmcommon.AccountHandler, e error) { - return state.NewUserAccount(address, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + return createAccount(address), nil }, SaveAccountCalled: func(accountHandler vmcommon.AccountHandler) error { putCodeCalled++ @@ -3091,7 +3100,7 @@ func TestScProcessor_ProcessSmartContractResultWithData(t *testing.T) { saveAccountCalled := 0 accountsDB := &stateMock.AccountsStub{ LoadAccountCalled: func(address []byte) (handler vmcommon.AccountHandler, e error) { - return state.NewUserAccount(address, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + return createAccount(address), nil }, SaveAccountCalled: func(accountHandler vmcommon.AccountHandler) error { saveAccountCalled++ @@ -3133,7 +3142,7 @@ func TestScProcessor_ProcessSmartContractResultDeploySCShouldError(t *testing.T) accountsDB := &stateMock.AccountsStub{ LoadAccountCalled: func(address []byte) (handler vmcommon.AccountHandler, e error) { - return state.NewUserAccount(address, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + return createAccount(address), nil }, SaveAccountCalled: func(accountHandler vmcommon.AccountHandler) error { return nil @@ -3169,7 +3178,7 @@ func TestScProcessor_ProcessSmartContractResultExecuteSC(t *testing.T) { t.Parallel() scAddress := []byte("000000000001234567890123456789012") - dstScAddress, _ := state.NewUserAccount(scAddress, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + dstScAddress := createAccount(scAddress) dstScAddress.SetCode([]byte("code")) accountsDB := &stateMock.AccountsStub{ LoadAccountCalled: func(address []byte) (handler vmcommon.AccountHandler, e error) { @@ -3231,7 +3240,7 @@ func TestScProcessor_ProcessSmartContractResultExecuteSCIfMetaAndBuiltIn(t *test t.Parallel() scAddress := []byte("000000000001234567890123456789012") - dstScAddress, _ := state.NewUserAccount(scAddress, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + dstScAddress := createAccount(scAddress) dstScAddress.SetCode([]byte("code")) accountsDB := &stateMock.AccountsStub{ LoadAccountCalled: func(address []byte) (handler vmcommon.AccountHandler, e error) { @@ -3306,15 +3315,15 @@ func TestScProcessor_ProcessRelayedSCRValueBackToRelayer(t *testing.T) { t.Parallel() scAddress := []byte("000000000001234567890123456789012") - dstScAddress, _ := state.NewUserAccount(scAddress, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + dstScAddress := createAccount(scAddress) dstScAddress.SetCode([]byte("code")) baseValue := big.NewInt(100) userAddress := []byte("111111111111234567890123456789012") - userAcc, _ := state.NewUserAccount(userAddress, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + userAcc := createAccount(userAddress) _ = userAcc.AddToBalance(baseValue) relayedAddress := []byte("211111111111234567890123456789012") - relayedAcc, _ := state.NewUserAccount(relayedAddress, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + relayedAcc := createAccount(relayedAddress) accountsDB := &stateMock.AccountsStub{ LoadAccountCalled: func(address []byte) (handler vmcommon.AccountHandler, e error) { @@ -3399,8 +3408,7 @@ func TestScProcessor_checkUpgradePermission(t *testing.T) { require.Equal(t, process.ErrUpgradeNotAllowed, err) // Create a contract, owned by Alice - contract, err := state.NewUserAccount([]byte("contract"), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - require.Nil(t, err) + contract := createAccount([]byte("contract")) contract.SetOwnerAddress([]byte("alice")) // Not yet upgradeable contract.SetCodeMetadata([]byte{0, 0}) @@ -3695,8 +3703,7 @@ func TestSmartContractProcessor_computeTotalConsumedFeeAndDevRwdWithDifferentSCC t.Parallel() scAccountAddress := []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x1e, 0x2e, 0x61, 0x1a, 0x9c, 0xe1, 0xe0, 0xc8, 0xe3, 0x28, 0x3c, 0xcc, 0x7c, 0x1b, 0x0f, 0x46, 0x61, 0x91, 0x70, 0x79, 0xa7, 0x5c} - acc, err := state.NewUserAccount(scAccountAddress, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - require.Nil(t, err) + acc := createAccount(scAccountAddress) require.NotNil(t, acc) arguments := createMockSmartContractProcessorArguments() @@ -3785,8 +3792,7 @@ func TestSmartContractProcessor_finishSCExecutionV2(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - acc, err := state.NewUserAccount(scAccountAddress, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - require.Nil(t, err) + acc := createAccount(scAccountAddress) require.NotNil(t, acc) arguments := createMockSmartContractProcessorArguments() @@ -3797,6 +3803,7 @@ func TestSmartContractProcessor_finishSCExecutionV2(t *testing.T) { // use a real fee handler args := createRealEconomicsDataArgs() + var err error arguments.EconomicsFee, err = economics.NewEconomicsData(*args) require.Nil(t, err) @@ -3827,7 +3834,7 @@ func TestSmartContractProcessor_finishSCExecutionV2(t *testing.T) { require.Nil(t, err) require.Equal(t, retcode, vmcommon.Ok) require.Nil(t, err) - require.Equal(t, expectedDevFees, acc.DeveloperReward) + require.Equal(t, expectedDevFees, acc.GetDeveloperReward()) require.Equal(t, expectedTotalFee, sc.txFeeHandler.GetAccumulatedFees()) require.Equal(t, expectedDevFees, sc.txFeeHandler.GetDeveloperFees()) }) @@ -4170,7 +4177,7 @@ func TestProcess_createCompletedTxEvent(t *testing.T) { scrWithRefund := &smartContractResult.SmartContractResult{Value: big.NewInt(10), PrevTxHash: scrHash, Data: []byte("@6f6b@aaffaa")} completedLogSaved = false - acntDst, _ := state.NewUserAccount(userAddress, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acntDst := createAccount(userAddress) err := sc.processSimpleSCR(scrWithRefund, []byte("scrHash"), acntDst) assert.Nil(t, err) assert.True(t, completedLogSaved) diff --git a/process/transaction/metaProcess_test.go b/process/transaction/metaProcess_test.go index 88945a249bb..6b228e880d7 100644 --- a/process/transaction/metaProcess_test.go +++ b/process/transaction/metaProcess_test.go @@ -36,6 +36,16 @@ func createMockNewMetaTxArgs() txproc.ArgsNewMetaTxProcessor { return args } +func createUserAcc(address []byte) state.UserAccountHandler { + argsAccCreation := state.ArgsAccountCreation{ + Hasher: &hashingMocks.HasherMock{}, + Marshaller: &testscommon.MarshalizerMock{}, + EnableEpochsHandler: &testscommon.EnableEpochsHandlerStub{}, + } + acc, _ := state.NewUserAccount(address, argsAccCreation) + return acc +} + // ------- NewMetaTxProcessor func TestNewMetaTxProcessor_NilAccountsShouldErr(t *testing.T) { @@ -154,10 +164,8 @@ func TestMetaTxProcessor_ProcessCheckNotPassShouldErr(t *testing.T) { tx.RcvAddr = []byte("DST") tx.Value = big.NewInt(45) - acntSrc, err := state.NewUserAccount(tx.SndAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) - acntDst, err := state.NewUserAccount(tx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) + acntSrc := createUserAcc(tx.SndAddr) + acntDst := createUserAcc(tx.RcvAddr) adb := createAccountStub(tx.SndAddr, tx.RcvAddr, acntSrc, acntDst) @@ -165,7 +173,7 @@ func TestMetaTxProcessor_ProcessCheckNotPassShouldErr(t *testing.T) { args.Accounts = adb txProc, _ := txproc.NewMetaTxProcessor(args) - _, err = txProc.ProcessTransaction(&tx) + _, err := txProc.ProcessTransaction(&tx) assert.Equal(t, process.ErrHigherNonceInTransaction, err) } @@ -180,10 +188,8 @@ func TestMetaTxProcessor_ProcessMoveBalancesShouldCallProcessIfError(t *testing. tx.RcvAddr = []byte("DST") tx.Value = big.NewInt(0) - acntSrc, err := state.NewUserAccount(tx.SndAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) - acntDst, err := state.NewUserAccount(tx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) + acntSrc := createUserAcc(tx.SndAddr) + acntDst := createUserAcc(tx.RcvAddr) adb := createAccountStub(tx.SndAddr, tx.RcvAddr, acntSrc, acntDst) adb.SaveAccountCalled = func(account vmcommon.AccountHandler) error { @@ -202,7 +208,7 @@ func TestMetaTxProcessor_ProcessMoveBalancesShouldCallProcessIfError(t *testing. } txProc, _ := txproc.NewMetaTxProcessor(args) - _, err = txProc.ProcessTransaction(&tx) + _, err := txProc.ProcessTransaction(&tx) assert.Equal(t, nil, err) assert.True(t, called) } @@ -220,13 +226,10 @@ func TestMetaTxProcessor_ProcessTransactionScTxShouldWork(t *testing.T) { tx.GasPrice = 1 tx.GasLimit = 1 - acntSrc, err := state.NewUserAccount(tx.SndAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) - - acntDst, err := state.NewUserAccount(tx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) + acntSrc := createUserAcc(tx.SndAddr) + acntDst := createUserAcc(tx.RcvAddr) - acntSrc.Balance = big.NewInt(46) + _ = acntSrc.AddToBalance(big.NewInt(46)) acntDst.SetCode([]byte{65}) adb := createAccountStub(tx.SndAddr, tx.RcvAddr, acntSrc, acntDst) @@ -252,7 +255,7 @@ func TestMetaTxProcessor_ProcessTransactionScTxShouldWork(t *testing.T) { } txProc, _ := txproc.NewMetaTxProcessor(args) - _, err = txProc.ProcessTransaction(&tx) + _, err := txProc.ProcessTransaction(&tx) assert.Nil(t, err) assert.True(t, wasCalled) assert.Equal(t, 0, saveAccountCalled) @@ -269,11 +272,9 @@ func TestMetaTxProcessor_ProcessTransactionScTxShouldReturnErrWhenExecutionFails tx.RcvAddr = generateRandomByteSlice(createMockPubkeyConverter().Len()) tx.Value = big.NewInt(45) - acntSrc, err := state.NewUserAccount(tx.SndAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) - acntSrc.Balance = big.NewInt(45) - acntDst, err := state.NewUserAccount(tx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) + acntSrc := createUserAcc(tx.SndAddr) + _ = acntSrc.AddToBalance(big.NewInt(45)) + acntDst := createUserAcc(tx.RcvAddr) acntDst.SetCode([]byte{65}) adb := createAccountStub(tx.SndAddr, tx.RcvAddr, acntSrc, acntDst) @@ -300,7 +301,7 @@ func TestMetaTxProcessor_ProcessTransactionScTxShouldReturnErrWhenExecutionFails } txProc, _ := txproc.NewMetaTxProcessor(args) - _, err = txProc.ProcessTransaction(&tx) + _, err := txProc.ProcessTransaction(&tx) assert.Equal(t, process.ErrNoVM, err) assert.True(t, wasCalled) assert.Equal(t, 0, saveAccountCalled) @@ -327,11 +328,9 @@ func TestMetaTxProcessor_ProcessTransactionScTxShouldNotBeCalledWhenAdrDstIsNotI return 0 } - acntSrc, err := state.NewUserAccount(tx.SndAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) - acntSrc.Balance = big.NewInt(45) - acntDst, err := state.NewUserAccount(tx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) + acntSrc := createUserAcc(tx.SndAddr) + _ = acntSrc.AddToBalance(big.NewInt(45)) + acntDst := createUserAcc(tx.RcvAddr) acntDst.SetCode([]byte{65}) adb := createAccountStub(tx.SndAddr, tx.RcvAddr, acntSrc, acntDst) @@ -372,7 +371,7 @@ func TestMetaTxProcessor_ProcessTransactionScTxShouldNotBeCalledWhenAdrDstIsNotI args.ShardCoordinator = shardCoordinator txProc, _ := txproc.NewMetaTxProcessor(args) - _, err = txProc.ProcessTransaction(&tx) + _, err := txProc.ProcessTransaction(&tx) assert.Equal(t, nil, err) assert.False(t, wasCalled) assert.True(t, calledIfError) @@ -391,13 +390,10 @@ func TestMetaTxProcessor_ProcessTransactionBuiltInCallTxShouldWork(t *testing.T) tx.GasPrice = 1 tx.GasLimit = 1 - acntSrc, err := state.NewUserAccount(tx.SndAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) + acntSrc := createUserAcc(tx.SndAddr) + acntDst := createUserAcc(tx.RcvAddr) - acntDst, err := state.NewUserAccount(tx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) - - acntSrc.Balance = big.NewInt(46) + _ = acntSrc.AddToBalance(big.NewInt(46)) acntDst.SetCode([]byte{65}) adb := createAccountStub(tx.SndAddr, tx.RcvAddr, acntSrc, acntDst) @@ -428,7 +424,7 @@ func TestMetaTxProcessor_ProcessTransactionBuiltInCallTxShouldWork(t *testing.T) args.EnableEpochsHandler = enableEpochsHandlerStub txProc, _ := txproc.NewMetaTxProcessor(args) - _, err = txProc.ProcessTransaction(&tx) + _, err := txProc.ProcessTransaction(&tx) assert.Nil(t, err) assert.True(t, wasCalled) assert.Equal(t, 0, saveAccountCalled) diff --git a/process/transaction/shardProcess_test.go b/process/transaction/shardProcess_test.go index 5eea5df8196..19b44ff78a1 100644 --- a/process/transaction/shardProcess_test.go +++ b/process/transaction/shardProcess_test.go @@ -316,8 +316,8 @@ func TestTxProcessor_GetAccountsOkValsSrcShouldWork(t *testing.T) { adr1 := []byte{65} adr2 := []byte{67} - acnt1, _ := state.NewUserAccount(adr1, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acnt2, _ := state.NewUserAccount(adr2, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acnt1 := createUserAcc(adr1) + acnt2 := createUserAcc(adr2) adb.LoadAccountCalled = func(address []byte) (vmcommon.AccountHandler, error) { if bytes.Equal(address, adr1) { @@ -361,8 +361,8 @@ func TestTxProcessor_GetAccountsOkValsDsthouldWork(t *testing.T) { adr1 := []byte{65} adr2 := []byte{67} - acnt1, _ := state.NewUserAccount(adr1, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acnt2, _ := state.NewUserAccount(adr2, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acnt1 := createUserAcc(adr1) + acnt2 := createUserAcc(adr2) adb.LoadAccountCalled = func(address []byte) (vmcommon.AccountHandler, error) { if bytes.Equal(address, adr1) { @@ -404,8 +404,8 @@ func TestTxProcessor_GetAccountsOkValsShouldWork(t *testing.T) { adr1 := []byte{65} adr2 := []byte{67} - acnt1, _ := state.NewUserAccount(adr1, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acnt2, _ := state.NewUserAccount(adr2, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acnt1 := createUserAcc(adr1) + acnt2 := createUserAcc(adr2) adb := createAccountStub(adr1, adr2, acnt1, acnt2) @@ -425,8 +425,8 @@ func TestTxProcessor_GetSameAccountShouldWork(t *testing.T) { adr1 := []byte{65} adr2 := []byte{65} - acnt1, _ := state.NewUserAccount(adr1, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acnt2, _ := state.NewUserAccount(adr2, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acnt1 := createUserAcc(adr1) + acnt2 := createUserAcc(adr2) adb := createAccountStub(adr1, adr2, acnt1, acnt2) @@ -445,14 +445,13 @@ func TestTxProcessor_CheckTxValuesHigherNonceShouldErr(t *testing.T) { t.Parallel() adr1 := []byte{65} - acnt1, err := state.NewUserAccount(adr1, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) + acnt1 := createUserAcc(adr1) execTx := *createTxProcessor() - acnt1.Nonce = 6 + acnt1.IncreaseNonce(6) - err = execTx.CheckTxValues(&transaction.Transaction{Nonce: 7}, acnt1, nil, false) + err := execTx.CheckTxValues(&transaction.Transaction{Nonce: 7}, acnt1, nil, false) assert.Equal(t, process.ErrHigherNonceInTransaction, err) } @@ -460,14 +459,13 @@ func TestTxProcessor_CheckTxValuesLowerNonceShouldErr(t *testing.T) { t.Parallel() adr1 := []byte{65} - acnt1, err := state.NewUserAccount(adr1, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) + acnt1 := createUserAcc(adr1) execTx := *createTxProcessor() - acnt1.Nonce = 6 + acnt1.IncreaseNonce(6) - err = execTx.CheckTxValues(&transaction.Transaction{Nonce: 5}, acnt1, nil, false) + err := execTx.CheckTxValues(&transaction.Transaction{Nonce: 5}, acnt1, nil, false) assert.Equal(t, process.ErrLowerNonceInTransaction, err) } @@ -475,14 +473,13 @@ func TestTxProcessor_CheckTxValuesInsufficientFundsShouldErr(t *testing.T) { t.Parallel() adr1 := []byte{65} - acnt1, err := state.NewUserAccount(adr1, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) + acnt1 := createUserAcc(adr1) execTx := *createTxProcessor() - acnt1.Balance = big.NewInt(67) + _ = acnt1.AddToBalance(big.NewInt(67)) - err = execTx.CheckTxValues(&transaction.Transaction{Value: big.NewInt(68)}, acnt1, nil, false) + err := execTx.CheckTxValues(&transaction.Transaction{Value: big.NewInt(68)}, acnt1, nil, false) assert.Equal(t, process.ErrInsufficientFunds, err) } @@ -490,21 +487,19 @@ func TestTxProcessor_CheckTxValuesMismatchedSenderUsernamesShouldErr(t *testing. t.Parallel() adr1 := []byte{65} - senderAcc, err := state.NewUserAccount(adr1, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - - assert.Nil(t, err) + senderAcc := createUserAcc(adr1) execTx := *createTxProcessor() - senderAcc.Balance = big.NewInt(67) - senderAcc.UserName = []byte("SRC") + _ = senderAcc.AddToBalance(big.NewInt(67)) + senderAcc.SetUserName([]byte("SRC")) tx := &transaction.Transaction{ Value: big.NewInt(10), SndUserName: []byte("notCorrect"), } - err = execTx.CheckTxValues(tx, senderAcc, nil, false) + err := execTx.CheckTxValues(tx, senderAcc, nil, false) assert.Equal(t, process.ErrUserNameDoesNotMatch, err) } @@ -512,21 +507,19 @@ func TestTxProcessor_CheckTxValuesMismatchedReceiverUsernamesShouldErr(t *testin t.Parallel() adr1 := []byte{65} - receiverAcc, err := state.NewUserAccount(adr1, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - - assert.Nil(t, err) + receiverAcc := createUserAcc(adr1) execTx := *createTxProcessor() - receiverAcc.Balance = big.NewInt(67) - receiverAcc.UserName = []byte("RECV") + _ = receiverAcc.AddToBalance(big.NewInt(67)) + receiverAcc.SetUserName([]byte("RECV")) tx := &transaction.Transaction{ Value: big.NewInt(10), RcvUserName: []byte("notCorrect"), } - err = execTx.CheckTxValues(tx, nil, receiverAcc, false) + err := execTx.CheckTxValues(tx, nil, receiverAcc, false) assert.Equal(t, process.ErrUserNameDoesNotMatchInCrossShardTx, err) } @@ -534,26 +527,24 @@ func TestTxProcessor_CheckTxValuesCorrectUserNamesShouldWork(t *testing.T) { t.Parallel() adr1 := []byte{65} - senderAcc, err := state.NewUserAccount(adr1, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) + senderAcc := createUserAcc(adr1) adr2 := []byte{66} - recvAcc, err := state.NewUserAccount(adr2, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) + recvAcc := createUserAcc(adr2) execTx := *createTxProcessor() - senderAcc.Balance = big.NewInt(67) - senderAcc.UserName = []byte("SRC") - recvAcc.UserName = []byte("RECV") + _ = senderAcc.AddToBalance(big.NewInt(67)) + senderAcc.SetUserName([]byte("SRC")) + recvAcc.SetUserName([]byte("RECV")) tx := &transaction.Transaction{ Value: big.NewInt(10), - SndUserName: senderAcc.UserName, - RcvUserName: recvAcc.UserName, + SndUserName: senderAcc.GetUserName(), + RcvUserName: recvAcc.GetUserName(), } - err = execTx.CheckTxValues(tx, senderAcc, recvAcc, false) + err := execTx.CheckTxValues(tx, senderAcc, recvAcc, false) assert.Nil(t, err) } @@ -561,14 +552,13 @@ func TestTxProcessor_CheckTxValuesOkValsShouldErr(t *testing.T) { t.Parallel() adr1 := []byte{65} - acnt1, err := state.NewUserAccount(adr1, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) + acnt1 := createUserAcc(adr1) execTx := *createTxProcessor() - acnt1.Balance = big.NewInt(67) + _ = acnt1.AddToBalance(big.NewInt(67)) - err = execTx.CheckTxValues(&transaction.Transaction{Value: big.NewInt(67)}, acnt1, nil, false) + err := execTx.CheckTxValues(&transaction.Transaction{Value: big.NewInt(67)}, acnt1, nil, false) assert.Nil(t, err) } @@ -578,15 +568,14 @@ func TestTxProcessor_IncreaseNonceOkValsShouldWork(t *testing.T) { t.Parallel() adrSrc := []byte{65} - acntSrc, err := state.NewUserAccount(adrSrc, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) + acntSrc := createUserAcc(adrSrc) execTx := *createTxProcessor() - acntSrc.Nonce = 45 + acntSrc.IncreaseNonce(45) execTx.IncreaseNonce(acntSrc) - assert.Equal(t, uint64(46), acntSrc.Nonce) + assert.Equal(t, uint64(46), acntSrc.GetNonce()) } //------- ProcessTransaction @@ -629,10 +618,8 @@ func TestTxProcessor_ProcessCheckNotPassShouldErr(t *testing.T) { tx.RcvAddr = []byte("DST") tx.Value = big.NewInt(45) - acntSrc, err := state.NewUserAccount(tx.SndAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) - acntDst, err := state.NewUserAccount(tx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) + acntSrc := createUserAcc(tx.SndAddr) + acntDst := createUserAcc(tx.RcvAddr) adb := createAccountStub(tx.SndAddr, tx.RcvAddr, acntSrc, acntDst) @@ -640,7 +627,7 @@ func TestTxProcessor_ProcessCheckNotPassShouldErr(t *testing.T) { args.Accounts = adb execTx, _ := txproc.NewTxProcessor(args) - _, err = execTx.ProcessTransaction(&tx) + _, err := execTx.ProcessTransaction(&tx) assert.Equal(t, process.ErrHigherNonceInTransaction, err) } @@ -665,10 +652,8 @@ func TestTxProcessor_ProcessWithTxFeeHandlerCheckErrorShouldErr(t *testing.T) { tx.RcvAddr = make([]byte, 32) tx.Value = big.NewInt(0) - acntSrc, err := state.NewUserAccount(tx.SndAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) - acntDst, err := state.NewUserAccount(tx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) + acntSrc := createUserAcc(tx.SndAddr) + acntDst := createUserAcc(tx.RcvAddr) adb := createAccountStub(tx.SndAddr, tx.RcvAddr, acntSrc, acntDst) @@ -682,7 +667,7 @@ func TestTxProcessor_ProcessWithTxFeeHandlerCheckErrorShouldErr(t *testing.T) { }} execTx, _ := txproc.NewTxProcessor(args) - _, err = execTx.ProcessTransaction(&tx) + _, err := execTx.ProcessTransaction(&tx) assert.Equal(t, expectedError, err) } @@ -717,12 +702,10 @@ func TestTxProcessor_ProcessWithTxFeeHandlerInsufficientFeeShouldErr(t *testing. tx.RcvAddr = make([]byte, 32) tx.Value = big.NewInt(0) - acntSrc, err := state.NewUserAccount(tx.SndAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) - acntDst, err := state.NewUserAccount(tx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) + acntSrc := createUserAcc(tx.SndAddr) + acntDst := createUserAcc(tx.RcvAddr) - acntSrc.Balance = big.NewInt(9) + _ = acntSrc.AddToBalance(big.NewInt(9)) adb := createAccountStub(tx.SndAddr, tx.RcvAddr, acntSrc, acntDst) @@ -731,12 +714,12 @@ func TestTxProcessor_ProcessWithTxFeeHandlerInsufficientFeeShouldErr(t *testing. args.EconomicsFee = &mock.FeeHandlerStub{ ComputeTxFeeCalled: func(tx data.TransactionWithFeeHandler) *big.Int { - return big.NewInt(0).Add(acntSrc.Balance, big.NewInt(1)) + return big.NewInt(0).Add(acntSrc.GetBalance(), big.NewInt(1)) }} execTx, _ := txproc.NewTxProcessor(args) - _, err = execTx.ProcessTransaction(&tx) + _, err := execTx.ProcessTransaction(&tx) assert.True(t, errors.Is(err, process.ErrInsufficientFee)) } @@ -749,12 +732,10 @@ func TestTxProcessor_ProcessWithInsufficientFundsShouldCreateReceiptErr(t *testi tx.RcvAddr = make([]byte, 32) tx.Value = big.NewInt(0) - acntSrc, err := state.NewUserAccount(tx.SndAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) - acntDst, err := state.NewUserAccount(tx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) + acntSrc := createUserAcc(tx.SndAddr) + acntDst := createUserAcc(tx.RcvAddr) - acntSrc.Balance = big.NewInt(9) + _ = acntSrc.AddToBalance(big.NewInt(9)) adb := createAccountStub(tx.SndAddr, tx.RcvAddr, acntSrc, acntDst) @@ -768,9 +749,9 @@ func TestTxProcessor_ProcessWithInsufficientFundsShouldCreateReceiptErr(t *testi execTx, _ := txproc.NewTxProcessor(args) - _, err = execTx.ProcessTransaction(&tx) + _, err := execTx.ProcessTransaction(&tx) assert.Equal(t, process.ErrFailedTransaction, err) - assert.Equal(t, uint64(1), acntSrc.Nonce) + assert.Equal(t, uint64(1), acntSrc.GetNonce()) } func TestTxProcessor_ProcessWithUsernameMismatchCreateReceiptErr(t *testing.T) { @@ -782,12 +763,10 @@ func TestTxProcessor_ProcessWithUsernameMismatchCreateReceiptErr(t *testing.T) { tx.RcvAddr = make([]byte, 32) tx.Value = big.NewInt(0) - acntSrc, err := state.NewUserAccount(tx.SndAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) - acntDst, err := state.NewUserAccount(tx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) + acntSrc := createUserAcc(tx.SndAddr) + acntDst := createUserAcc(tx.RcvAddr) - acntSrc.Balance = big.NewInt(9) + _ = acntSrc.AddToBalance(big.NewInt(9)) adb := createAccountStub(tx.SndAddr, tx.RcvAddr, acntSrc, acntDst) @@ -801,7 +780,7 @@ func TestTxProcessor_ProcessWithUsernameMismatchCreateReceiptErr(t *testing.T) { execTx, _ := txproc.NewTxProcessor(args) - _, err = execTx.ProcessTransaction(&tx) + _, err := execTx.ProcessTransaction(&tx) assert.Nil(t, err) } @@ -814,12 +793,10 @@ func TestTxProcessor_ProcessWithUsernameMismatchAndSCProcessErrorShouldError(t * tx.RcvAddr = make([]byte, 32) tx.Value = big.NewInt(0) - acntSrc, err := state.NewUserAccount(tx.SndAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) - acntDst, err := state.NewUserAccount(tx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) + acntSrc := createUserAcc(tx.SndAddr) + acntDst := createUserAcc(tx.RcvAddr) - acntSrc.Balance = big.NewInt(9) + _ = acntSrc.AddToBalance(big.NewInt(9)) adb := createAccountStub(tx.SndAddr, tx.RcvAddr, acntSrc, acntDst) @@ -841,7 +818,7 @@ func TestTxProcessor_ProcessWithUsernameMismatchAndSCProcessErrorShouldError(t * execTx, _ := txproc.NewTxProcessor(args) - _, err = execTx.ProcessTransaction(&tx) + _, err := execTx.ProcessTransaction(&tx) assert.Equal(t, expectedError, err) } @@ -861,12 +838,10 @@ func TestTxProcessor_ProcessMoveBalanceToSmartPayableContract(t *testing.T) { return 0 } - acntSrc, err := state.NewUserAccount(tx.SndAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) - acntDst, err := state.NewUserAccount(tx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) + acntSrc := createUserAcc(tx.SndAddr) + acntDst := createUserAcc(tx.RcvAddr) - acntDst.CodeMetadata = []byte{0, vmcommon.MetadataPayable} + acntDst.SetCodeMetadata([]byte{0, vmcommon.MetadataPayable}) adb := createAccountStub(tx.SndAddr, tx.RcvAddr, acntSrc, acntDst) adb.SaveAccountCalled = func(account vmcommon.AccountHandler) error { @@ -879,7 +854,7 @@ func TestTxProcessor_ProcessMoveBalanceToSmartPayableContract(t *testing.T) { args.ShardCoordinator = shardCoordinator execTx, _ := txproc.NewTxProcessor(args) - _, err = execTx.ProcessTransaction(&tx) + _, err := execTx.ProcessTransaction(&tx) assert.Nil(t, err) assert.Equal(t, 2, saveAccountCalled) } @@ -902,10 +877,8 @@ func testProcessCheck(t *testing.T, nonce uint64, value *big.Int) { return 0 } - acntSrc, err := state.NewUserAccount(tx.SndAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) - acntDst, err := state.NewUserAccount(tx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) + acntSrc := createUserAcc(tx.SndAddr) + acntDst := createUserAcc(tx.RcvAddr) adb := createAccountStub(tx.SndAddr, tx.RcvAddr, acntSrc, acntDst) adb.SaveAccountCalled = func(account vmcommon.AccountHandler) error { @@ -918,7 +891,7 @@ func testProcessCheck(t *testing.T, nonce uint64, value *big.Int) { args.ShardCoordinator = shardCoordinator execTx, _ := txproc.NewTxProcessor(args) - _, err = execTx.ProcessTransaction(&tx) + _, err := execTx.ProcessTransaction(&tx) assert.Nil(t, err) assert.Equal(t, 1, saveAccountCalled) } @@ -934,10 +907,8 @@ func TestTxProcessor_ProcessMoveBalancesShouldWork(t *testing.T) { tx.RcvAddr = []byte("DST") tx.Value = big.NewInt(0) - acntSrc, err := state.NewUserAccount(tx.SndAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) - acntDst, err := state.NewUserAccount(tx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) + acntSrc := createUserAcc(tx.SndAddr) + acntDst := createUserAcc(tx.RcvAddr) adb := createAccountStub(tx.SndAddr, tx.RcvAddr, acntSrc, acntDst) adb.SaveAccountCalled = func(account vmcommon.AccountHandler) error { @@ -949,7 +920,7 @@ func TestTxProcessor_ProcessMoveBalancesShouldWork(t *testing.T) { args.Accounts = adb execTx, _ := txproc.NewTxProcessor(args) - _, err = execTx.ProcessTransaction(&tx) + _, err := execTx.ProcessTransaction(&tx) assert.Nil(t, err) assert.Equal(t, 2, saveAccountCalled) } @@ -965,14 +936,12 @@ func TestTxProcessor_ProcessOkValsShouldWork(t *testing.T) { tx.RcvAddr = []byte("DST") tx.Value = big.NewInt(61) - acntSrc, err := state.NewUserAccount(tx.SndAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) - acntDst, err := state.NewUserAccount(tx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) + acntSrc := createUserAcc(tx.SndAddr) + acntDst := createUserAcc(tx.RcvAddr) - acntSrc.Nonce = 4 - acntSrc.Balance = big.NewInt(90) - acntDst.Balance = big.NewInt(10) + acntSrc.IncreaseNonce(4) + _ = acntSrc.AddToBalance(big.NewInt(90)) + _ = acntDst.AddToBalance(big.NewInt(10)) adb := createAccountStub(tx.SndAddr, tx.RcvAddr, acntSrc, acntDst) adb.SaveAccountCalled = func(account vmcommon.AccountHandler) error { @@ -984,11 +953,11 @@ func TestTxProcessor_ProcessOkValsShouldWork(t *testing.T) { args.Accounts = adb execTx, _ := txproc.NewTxProcessor(args) - _, err = execTx.ProcessTransaction(&tx) + _, err := execTx.ProcessTransaction(&tx) assert.Nil(t, err) - assert.Equal(t, uint64(5), acntSrc.Nonce) - assert.Equal(t, big.NewInt(29), acntSrc.Balance) - assert.Equal(t, big.NewInt(71), acntDst.Balance) + assert.Equal(t, uint64(5), acntSrc.GetNonce()) + assert.Equal(t, big.NewInt(29), acntSrc.GetBalance()) + assert.Equal(t, big.NewInt(71), acntDst.GetBalance()) assert.Equal(t, 2, saveAccountCalled) } @@ -1003,14 +972,12 @@ func TestTxProcessor_MoveBalanceWithFeesShouldWork(t *testing.T) { tx.GasPrice = 2 tx.GasLimit = 2 - acntSrc, err := state.NewUserAccount(tx.SndAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) - acntDst, err := state.NewUserAccount(tx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) + acntSrc := createUserAcc(tx.SndAddr) + acntDst := createUserAcc(tx.RcvAddr) - acntSrc.Nonce = 4 - acntSrc.Balance = big.NewInt(90) - acntDst.Balance = big.NewInt(10) + acntSrc.IncreaseNonce(4) + _ = acntSrc.AddToBalance(big.NewInt(90)) + _ = acntDst.AddToBalance(big.NewInt(10)) adb := createAccountStub(tx.SndAddr, tx.RcvAddr, acntSrc, acntDst) adb.SaveAccountCalled = func(account vmcommon.AccountHandler) error { @@ -1033,11 +1000,11 @@ func TestTxProcessor_MoveBalanceWithFeesShouldWork(t *testing.T) { args.EconomicsFee = feeHandler execTx, _ := txproc.NewTxProcessor(args) - _, err = execTx.ProcessTransaction(&tx) + _, err := execTx.ProcessTransaction(&tx) assert.Nil(t, err) - assert.Equal(t, uint64(5), acntSrc.Nonce) - assert.Equal(t, big.NewInt(13), acntSrc.Balance) - assert.Equal(t, big.NewInt(71), acntDst.Balance) + assert.Equal(t, uint64(5), acntSrc.GetNonce()) + assert.Equal(t, big.NewInt(13), acntSrc.GetBalance()) + assert.Equal(t, big.NewInt(71), acntDst.GetBalance()) assert.Equal(t, 2, saveAccountCalled) } @@ -1053,13 +1020,10 @@ func TestTxProcessor_ProcessTransactionScDeployTxShouldWork(t *testing.T) { tx.GasPrice = 1 tx.GasLimit = 1 - acntSrc, err := state.NewUserAccount(tx.SndAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) - - acntDst, err := state.NewUserAccount(tx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) + acntSrc := createUserAcc(tx.SndAddr) + acntDst := createUserAcc(tx.RcvAddr) - acntSrc.Balance = big.NewInt(46) + _ = acntSrc.AddToBalance(big.NewInt(46)) acntDst.SetCode([]byte{65}) adb := createAccountStub(tx.SndAddr, tx.RcvAddr, acntSrc, acntDst) @@ -1086,7 +1050,7 @@ func TestTxProcessor_ProcessTransactionScDeployTxShouldWork(t *testing.T) { } execTx, _ := txproc.NewTxProcessor(args) - _, err = execTx.ProcessTransaction(&tx) + _, err := execTx.ProcessTransaction(&tx) assert.Nil(t, err) assert.True(t, wasCalled) assert.Equal(t, 0, saveAccountCalled) @@ -1104,13 +1068,10 @@ func TestTxProcessor_ProcessTransactionBuiltInFunctionCallShouldWork(t *testing. tx.GasPrice = 1 tx.GasLimit = 1 - acntSrc, err := state.NewUserAccount(tx.SndAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) - - acntDst, err := state.NewUserAccount(tx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) + acntSrc := createUserAcc(tx.SndAddr) + acntDst := createUserAcc(tx.RcvAddr) - acntSrc.Balance = big.NewInt(46) + _ = acntSrc.AddToBalance(big.NewInt(46)) acntDst.SetCode([]byte{65}) adb := createAccountStub(tx.SndAddr, tx.RcvAddr, acntSrc, acntDst) @@ -1137,7 +1098,7 @@ func TestTxProcessor_ProcessTransactionBuiltInFunctionCallShouldWork(t *testing. } execTx, _ := txproc.NewTxProcessor(args) - _, err = execTx.ProcessTransaction(&tx) + _, err := execTx.ProcessTransaction(&tx) assert.Nil(t, err) assert.True(t, wasCalled) assert.Equal(t, 0, saveAccountCalled) @@ -1155,13 +1116,10 @@ func TestTxProcessor_ProcessTransactionScTxShouldWork(t *testing.T) { tx.GasPrice = 1 tx.GasLimit = 1 - acntSrc, err := state.NewUserAccount(tx.SndAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) - - acntDst, err := state.NewUserAccount(tx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) + acntSrc := createUserAcc(tx.SndAddr) + acntDst := createUserAcc(tx.RcvAddr) - acntSrc.Balance = big.NewInt(46) + _ = acntSrc.AddToBalance(big.NewInt(46)) acntDst.SetCode([]byte{65}) adb := createAccountStub(tx.SndAddr, tx.RcvAddr, acntSrc, acntDst) @@ -1188,7 +1146,7 @@ func TestTxProcessor_ProcessTransactionScTxShouldWork(t *testing.T) { } execTx, _ := txproc.NewTxProcessor(args) - _, err = execTx.ProcessTransaction(&tx) + _, err := execTx.ProcessTransaction(&tx) assert.Nil(t, err) assert.True(t, wasCalled) assert.Equal(t, 0, saveAccountCalled) @@ -1205,11 +1163,9 @@ func TestTxProcessor_ProcessTransactionScTxShouldReturnErrWhenExecutionFails(t * tx.RcvAddr = generateRandomByteSlice(createMockPubkeyConverter().Len()) tx.Value = big.NewInt(45) - acntSrc, err := state.NewUserAccount(tx.SndAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) - acntSrc.Balance = big.NewInt(45) - acntDst, err := state.NewUserAccount(tx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) + acntSrc := createUserAcc(tx.SndAddr) + _ = acntSrc.AddToBalance(big.NewInt(45)) + acntDst := createUserAcc(tx.RcvAddr) acntDst.SetCode([]byte{65}) adb := createAccountStub(tx.SndAddr, tx.RcvAddr, acntSrc, acntDst) @@ -1236,7 +1192,7 @@ func TestTxProcessor_ProcessTransactionScTxShouldReturnErrWhenExecutionFails(t * } execTx, _ := txproc.NewTxProcessor(args) - _, err = execTx.ProcessTransaction(&tx) + _, err := execTx.ProcessTransaction(&tx) assert.Equal(t, process.ErrNoVM, err) assert.True(t, wasCalled) assert.Equal(t, 0, saveAccountCalled) @@ -1262,11 +1218,9 @@ func TestTxProcessor_ProcessTransactionScTxShouldNotBeCalledWhenAdrDstIsNotInNod return 0 } - acntSrc, err := state.NewUserAccount(tx.SndAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) - acntSrc.Balance = big.NewInt(45) - acntDst, err := state.NewUserAccount(tx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) + acntSrc := createUserAcc(tx.SndAddr) + _ = acntSrc.AddToBalance(big.NewInt(45)) + acntDst := createUserAcc(tx.RcvAddr) acntDst.SetCode([]byte{65}) adb := createAccountStub(tx.SndAddr, tx.RcvAddr, acntSrc, acntDst) @@ -1302,7 +1256,7 @@ func TestTxProcessor_ProcessTransactionScTxShouldNotBeCalledWhenAdrDstIsNotInNod args.TxTypeHandler = computeType execTx, _ := txproc.NewTxProcessor(args) - _, err = execTx.ProcessTransaction(&tx) + _, err := execTx.ProcessTransaction(&tx) assert.Nil(t, err) assert.False(t, wasCalled) assert.Equal(t, 1, saveAccountCalled) @@ -1533,9 +1487,8 @@ func TestTxProcessor_ProcessTransactionShouldReturnErrForInvalidMetaTx(t *testin tx.GasPrice = 1 tx.GasLimit = 1 - acntSrc, err := state.NewUserAccount(tx.SndAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) - acntSrc.Balance = big.NewInt(100000000) + acntSrc := createUserAcc(tx.SndAddr) + _ = acntSrc.AddToBalance(big.NewInt(100000000)) adb := createAccountStub(tx.SndAddr, tx.RcvAddr, acntSrc, nil) scProcessorMock := &testscommon.SCProcessorMock{ @@ -1563,10 +1516,10 @@ func TestTxProcessor_ProcessTransactionShouldReturnErrForInvalidMetaTx(t *testin } execTx, _ := txproc.NewTxProcessor(args) - _, err = execTx.ProcessTransaction(&tx) + _, err := execTx.ProcessTransaction(&tx) assert.Equal(t, err, process.ErrFailedTransaction) - assert.Equal(t, uint64(1), acntSrc.Nonce) - assert.Equal(t, uint64(99999999), acntSrc.Balance.Uint64()) + assert.Equal(t, uint64(1), acntSrc.GetNonce()) + assert.Equal(t, uint64(99999999), acntSrc.GetBalance().Uint64()) tx.Data = []byte("something") tx.Nonce = tx.Nonce + 1 @@ -1590,9 +1543,8 @@ func TestTxProcessor_ProcessTransactionShouldTreatAsInvalidTxIfTxTypeIsWrong(t * tx.GasPrice = 1 tx.GasLimit = 1 - acntSrc, err := state.NewUserAccount(tx.SndAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) - acntSrc.Balance = big.NewInt(46) + acntSrc := createUserAcc(tx.SndAddr) + _ = acntSrc.AddToBalance(big.NewInt(46)) adb := createAccountStub(tx.SndAddr, tx.RcvAddr, acntSrc, nil) shardC, _ := sharding.NewMultiShardCoordinator(5, 3) @@ -1611,10 +1563,10 @@ func TestTxProcessor_ProcessTransactionShouldTreatAsInvalidTxIfTxTypeIsWrong(t * } execTx, _ := txproc.NewTxProcessor(args) - _, err = execTx.ProcessTransaction(&tx) + _, err := execTx.ProcessTransaction(&tx) assert.Equal(t, err, process.ErrFailedTransaction) - assert.Equal(t, uint64(1), acntSrc.Nonce) - assert.Equal(t, uint64(45), acntSrc.Balance.Uint64()) + assert.Equal(t, uint64(1), acntSrc.GetNonce()) + assert.Equal(t, uint64(45), acntSrc.GetBalance().Uint64()) } func TestTxProcessor_ProcessRelayedTransactionV2NotActiveShouldErr(t *testing.T) { @@ -1647,13 +1599,13 @@ func TestTxProcessor_ProcessRelayedTransactionV2NotActiveShouldErr(t *testing.T) "@" + "01a2") - acntSrc, _ := state.NewUserAccount(tx.SndAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntSrc.Balance = big.NewInt(100) - acntDst, _ := state.NewUserAccount(tx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntDst.Balance = big.NewInt(10) + acntSrc := createUserAcc(tx.SndAddr) + _ = acntSrc.AddToBalance(big.NewInt(100)) + acntDst := createUserAcc(tx.RcvAddr) + _ = acntDst.AddToBalance(big.NewInt(10)) - acntFinal, _ := state.NewUserAccount(userTxDest, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntFinal.Balance = big.NewInt(10) + acntFinal := createUserAcc(userTxDest) + _ = acntFinal.AddToBalance(big.NewInt(10)) adb := &stateMock.AccountsStub{} adb.LoadAccountCalled = func(address []byte) (vmcommon.AccountHandler, error) { @@ -1729,13 +1681,13 @@ func TestTxProcessor_ProcessRelayedTransactionV2WithValueShouldErr(t *testing.T) "@" + "01a2") - acntSrc, _ := state.NewUserAccount(tx.SndAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntSrc.Balance = big.NewInt(100) - acntDst, _ := state.NewUserAccount(tx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntDst.Balance = big.NewInt(10) + acntSrc := createUserAcc(tx.SndAddr) + _ = acntSrc.AddToBalance(big.NewInt(100)) + acntDst := createUserAcc(tx.RcvAddr) + _ = acntDst.AddToBalance(big.NewInt(10)) - acntFinal, _ := state.NewUserAccount(userTxDest, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntFinal.Balance = big.NewInt(10) + acntFinal := createUserAcc(userTxDest) + _ = acntFinal.AddToBalance(big.NewInt(10)) adb := &stateMock.AccountsStub{} adb.LoadAccountCalled = func(address []byte) (vmcommon.AccountHandler, error) { @@ -1811,13 +1763,13 @@ func TestTxProcessor_ProcessRelayedTransactionV2ArgsParserShouldErr(t *testing.T "@" + "01a2") - acntSrc, _ := state.NewUserAccount(tx.SndAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntSrc.Balance = big.NewInt(100) - acntDst, _ := state.NewUserAccount(tx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntDst.Balance = big.NewInt(10) + acntSrc := createUserAcc(tx.SndAddr) + _ = acntSrc.AddToBalance(big.NewInt(100)) + acntDst := createUserAcc(tx.RcvAddr) + _ = acntDst.AddToBalance(big.NewInt(10)) - acntFinal, _ := state.NewUserAccount(userTxDest, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntFinal.Balance = big.NewInt(10) + acntFinal := createUserAcc(userTxDest) + _ = acntFinal.AddToBalance(big.NewInt(10)) adb := &stateMock.AccountsStub{} adb.LoadAccountCalled = func(address []byte) (vmcommon.AccountHandler, error) { @@ -1900,13 +1852,13 @@ func TestTxProcessor_ProcessRelayedTransactionV2InvalidParamCountShouldErr(t *te "@" + "1010") - acntSrc, _ := state.NewUserAccount(tx.SndAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntSrc.Balance = big.NewInt(100) - acntDst, _ := state.NewUserAccount(tx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntDst.Balance = big.NewInt(10) + acntSrc := createUserAcc(tx.SndAddr) + _ = acntSrc.AddToBalance(big.NewInt(100)) + acntDst := createUserAcc(tx.RcvAddr) + _ = acntDst.AddToBalance(big.NewInt(10)) - acntFinal, _ := state.NewUserAccount(userTxDest, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntFinal.Balance = big.NewInt(10) + acntFinal := createUserAcc(userTxDest) + _ = acntFinal.AddToBalance(big.NewInt(10)) adb := &stateMock.AccountsStub{} adb.LoadAccountCalled = func(address []byte) (vmcommon.AccountHandler, error) { @@ -1982,13 +1934,13 @@ func TestTxProcessor_ProcessRelayedTransactionV2(t *testing.T) { "@" + "01a2") - acntSrc, _ := state.NewUserAccount(tx.SndAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntSrc.Balance = big.NewInt(100) - acntDst, _ := state.NewUserAccount(tx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntDst.Balance = big.NewInt(10) + acntSrc := createUserAcc(tx.SndAddr) + _ = acntSrc.AddToBalance(big.NewInt(100)) + acntDst := createUserAcc(tx.RcvAddr) + _ = acntDst.AddToBalance(big.NewInt(10)) - acntFinal, _ := state.NewUserAccount(userTxDest, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntFinal.Balance = big.NewInt(10) + acntFinal := createUserAcc(userTxDest) + _ = acntFinal.AddToBalance(big.NewInt(10)) adb := &stateMock.AccountsStub{} adb.LoadAccountCalled = func(address []byte) (vmcommon.AccountHandler, error) { @@ -2063,12 +2015,12 @@ func TestTxProcessor_ProcessRelayedTransaction(t *testing.T) { userTxMarshalled, _ := marshalizer.Marshal(userTx) tx.Data = []byte(core.RelayedTransaction + "@" + hex.EncodeToString(userTxMarshalled)) - acntSrc, _ := state.NewUserAccount(tx.SndAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntSrc.Balance = big.NewInt(100) - acntDst, _ := state.NewUserAccount(tx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntDst.Balance = big.NewInt(10) - acntFinal, _ := state.NewUserAccount(userTx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntFinal.Balance = big.NewInt(10) + acntSrc := createUserAcc(tx.SndAddr) + _ = acntSrc.AddToBalance(big.NewInt(100)) + acntDst := createUserAcc(tx.RcvAddr) + _ = acntDst.AddToBalance(big.NewInt(10)) + acntFinal := createUserAcc(userTx.RcvAddr) + _ = acntFinal.AddToBalance(big.NewInt(10)) adb := &stateMock.AccountsStub{} adb.LoadAccountCalled = func(address []byte) (vmcommon.AccountHandler, error) { @@ -2159,12 +2111,12 @@ func TestTxProcessor_ProcessRelayedTransactionArgsParserErrorShouldError(t *test return "", nil, parseError }} - acntSrc, _ := state.NewUserAccount(tx.SndAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntSrc.Balance = big.NewInt(100) - acntDst, _ := state.NewUserAccount(tx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntDst.Balance = big.NewInt(10) - acntFinal, _ := state.NewUserAccount(userTx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntFinal.Balance = big.NewInt(10) + acntSrc := createUserAcc(tx.SndAddr) + _ = acntSrc.AddToBalance(big.NewInt(100)) + acntDst := createUserAcc(tx.RcvAddr) + _ = acntDst.AddToBalance(big.NewInt(10)) + acntFinal := createUserAcc(userTx.RcvAddr) + _ = acntFinal.AddToBalance(big.NewInt(10)) adb := &stateMock.AccountsStub{} adb.LoadAccountCalled = func(address []byte) (vmcommon.AccountHandler, error) { @@ -2222,12 +2174,12 @@ func TestTxProcessor_ProcessRelayedTransactionMultipleArgumentsShouldError(t *te return core.RelayedTransaction, [][]byte{[]byte("0"), []byte("1")}, nil }} - acntSrc, _ := state.NewUserAccount(tx.SndAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntSrc.Balance = big.NewInt(100) - acntDst, _ := state.NewUserAccount(tx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntDst.Balance = big.NewInt(10) - acntFinal, _ := state.NewUserAccount(userTx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntFinal.Balance = big.NewInt(10) + acntSrc := createUserAcc(tx.SndAddr) + _ = acntSrc.AddToBalance(big.NewInt(100)) + acntDst := createUserAcc(tx.RcvAddr) + _ = acntDst.AddToBalance(big.NewInt(10)) + acntFinal := createUserAcc(userTx.RcvAddr) + _ = acntFinal.AddToBalance(big.NewInt(10)) adb := &stateMock.AccountsStub{} adb.LoadAccountCalled = func(address []byte) (vmcommon.AccountHandler, error) { @@ -2285,12 +2237,12 @@ func TestTxProcessor_ProcessRelayedTransactionFailUnMarshalInnerShouldError(t *t return core.RelayedTransaction, [][]byte{[]byte("0")}, nil }} - acntSrc, _ := state.NewUserAccount(tx.SndAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntSrc.Balance = big.NewInt(100) - acntDst, _ := state.NewUserAccount(tx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntDst.Balance = big.NewInt(10) - acntFinal, _ := state.NewUserAccount(userTx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntFinal.Balance = big.NewInt(10) + acntSrc := createUserAcc(tx.SndAddr) + _ = acntSrc.AddToBalance(big.NewInt(100)) + acntDst := createUserAcc(tx.RcvAddr) + _ = acntDst.AddToBalance(big.NewInt(10)) + acntFinal := createUserAcc(userTx.RcvAddr) + _ = acntFinal.AddToBalance(big.NewInt(10)) adb := &stateMock.AccountsStub{} adb.LoadAccountCalled = func(address []byte) (vmcommon.AccountHandler, error) { @@ -2348,12 +2300,12 @@ func TestTxProcessor_ProcessRelayedTransactionDifferentSenderInInnerTxThanReceiv return core.RelayedTransaction, [][]byte{userTxMarshalled}, nil }} - acntSrc, _ := state.NewUserAccount(tx.SndAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntSrc.Balance = big.NewInt(100) - acntDst, _ := state.NewUserAccount(tx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntDst.Balance = big.NewInt(10) - acntFinal, _ := state.NewUserAccount(userTx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntFinal.Balance = big.NewInt(10) + acntSrc := createUserAcc(tx.SndAddr) + _ = acntSrc.AddToBalance(big.NewInt(100)) + acntDst := createUserAcc(tx.RcvAddr) + _ = acntDst.AddToBalance(big.NewInt(10)) + acntFinal := createUserAcc(userTx.RcvAddr) + _ = acntFinal.AddToBalance(big.NewInt(10)) adb := &stateMock.AccountsStub{} adb.LoadAccountCalled = func(address []byte) (vmcommon.AccountHandler, error) { @@ -2411,12 +2363,12 @@ func TestTxProcessor_ProcessRelayedTransactionSmallerValueInnerTxShouldError(t * return core.RelayedTransaction, [][]byte{userTxMarshalled}, nil }} - acntSrc, _ := state.NewUserAccount(tx.SndAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntSrc.Balance = big.NewInt(100) - acntDst, _ := state.NewUserAccount(tx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntDst.Balance = big.NewInt(10) - acntFinal, _ := state.NewUserAccount(userTx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntFinal.Balance = big.NewInt(10) + acntSrc := createUserAcc(tx.SndAddr) + _ = acntSrc.AddToBalance(big.NewInt(100)) + acntDst := createUserAcc(tx.RcvAddr) + _ = acntDst.AddToBalance(big.NewInt(10)) + acntFinal := createUserAcc(userTx.RcvAddr) + _ = acntFinal.AddToBalance(big.NewInt(10)) adb := &stateMock.AccountsStub{} adb.LoadAccountCalled = func(address []byte) (vmcommon.AccountHandler, error) { @@ -2474,12 +2426,12 @@ func TestTxProcessor_ProcessRelayedTransactionGasPriceMismatchShouldError(t *tes return core.RelayedTransaction, [][]byte{userTxMarshalled}, nil }} - acntSrc, _ := state.NewUserAccount(tx.SndAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntSrc.Balance = big.NewInt(100) - acntDst, _ := state.NewUserAccount(tx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntDst.Balance = big.NewInt(10) - acntFinal, _ := state.NewUserAccount(userTx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntFinal.Balance = big.NewInt(10) + acntSrc := createUserAcc(tx.SndAddr) + _ = acntSrc.AddToBalance(big.NewInt(100)) + acntDst := createUserAcc(tx.RcvAddr) + _ = acntDst.AddToBalance(big.NewInt(10)) + acntFinal := createUserAcc(userTx.RcvAddr) + _ = acntFinal.AddToBalance(big.NewInt(10)) adb := &stateMock.AccountsStub{} adb.LoadAccountCalled = func(address []byte) (vmcommon.AccountHandler, error) { @@ -2537,12 +2489,12 @@ func TestTxProcessor_ProcessRelayedTransactionGasLimitMismatchShouldError(t *tes return core.RelayedTransaction, [][]byte{userTxMarshalled}, nil }} - acntSrc, _ := state.NewUserAccount(tx.SndAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntSrc.Balance = big.NewInt(100) - acntDst, _ := state.NewUserAccount(tx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntDst.Balance = big.NewInt(10) - acntFinal, _ := state.NewUserAccount(userTx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntFinal.Balance = big.NewInt(10) + acntSrc := createUserAcc(tx.SndAddr) + _ = acntSrc.AddToBalance(big.NewInt(100)) + acntDst := createUserAcc(tx.RcvAddr) + _ = acntDst.AddToBalance(big.NewInt(10)) + acntFinal := createUserAcc(userTx.RcvAddr) + _ = acntFinal.AddToBalance(big.NewInt(10)) adb := &stateMock.AccountsStub{} adb.LoadAccountCalled = func(address []byte) (vmcommon.AccountHandler, error) { @@ -2596,12 +2548,12 @@ func TestTxProcessor_ProcessRelayedTransactionDisabled(t *testing.T) { userTxMarshalled, _ := marshalizer.Marshal(userTx) tx.Data = []byte(core.RelayedTransaction + "@" + hex.EncodeToString(userTxMarshalled)) - acntSrc, _ := state.NewUserAccount(tx.SndAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntSrc.Balance = big.NewInt(100) - acntDst, _ := state.NewUserAccount(tx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntDst.Balance = big.NewInt(10) - acntFinal, _ := state.NewUserAccount(userTx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntFinal.Balance = big.NewInt(10) + acntSrc := createUserAcc(tx.SndAddr) + _ = acntSrc.AddToBalance(big.NewInt(100)) + acntDst := createUserAcc(tx.RcvAddr) + _ = acntDst.AddToBalance(big.NewInt(10)) + acntFinal := createUserAcc(userTx.RcvAddr) + _ = acntFinal.AddToBalance(big.NewInt(10)) adb := &stateMock.AccountsStub{} adb.LoadAccountCalled = func(address []byte) (vmcommon.AccountHandler, error) { @@ -2674,8 +2626,8 @@ func TestTxProcessor_ConsumeMoveBalanceWithUserTx(t *testing.T) { } execTx, _ := txproc.NewTxProcessor(args) - acntSrc, _ := state.NewUserAccount([]byte("address"), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntSrc.Balance = big.NewInt(100) + acntSrc := createUserAcc([]byte("address")) + _ = acntSrc.AddToBalance(big.NewInt(100)) originalTxHash := []byte("originalTxHash") userTx := &transaction.Transaction{ @@ -2687,7 +2639,7 @@ func TestTxProcessor_ConsumeMoveBalanceWithUserTx(t *testing.T) { err := execTx.ProcessMoveBalanceCostRelayedUserTx(userTx, &smartContractResult.SmartContractResult{}, acntSrc, originalTxHash) assert.Nil(t, err) - assert.Equal(t, acntSrc.Balance, big.NewInt(99)) + assert.Equal(t, acntSrc.GetBalance(), big.NewInt(99)) } func TestTxProcessor_IsCrossTxFromMeShouldWork(t *testing.T) { @@ -2734,12 +2686,12 @@ func TestTxProcessor_ProcessUserTxOfTypeRelayedShouldError(t *testing.T) { return core.RelayedTransaction, [][]byte{userTxMarshalled}, nil }} - acntSrc, _ := state.NewUserAccount(tx.SndAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntSrc.Balance = big.NewInt(100) - acntDst, _ := state.NewUserAccount(tx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntDst.Balance = big.NewInt(100) - acntFinal, _ := state.NewUserAccount(userTx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntFinal.Balance = big.NewInt(100) + acntSrc := createUserAcc(tx.SndAddr) + _ = acntSrc.AddToBalance(big.NewInt(100)) + acntDst := createUserAcc(tx.RcvAddr) + _ = acntDst.AddToBalance(big.NewInt(100)) + acntFinal := createUserAcc(userTx.RcvAddr) + _ = acntFinal.AddToBalance(big.NewInt(100)) adb := &stateMock.AccountsStub{} adb.LoadAccountCalled = func(address []byte) (vmcommon.AccountHandler, error) { @@ -2798,12 +2750,12 @@ func TestTxProcessor_ProcessUserTxOfTypeMoveBalanceShouldWork(t *testing.T) { return core.RelayedTransaction, [][]byte{userTxMarshalled}, nil }} - acntSrc, _ := state.NewUserAccount(tx.SndAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntSrc.Balance = big.NewInt(100) - acntDst, _ := state.NewUserAccount(tx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntDst.Balance = big.NewInt(100) - acntFinal, _ := state.NewUserAccount(userTx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntFinal.Balance = big.NewInt(100) + acntSrc := createUserAcc(tx.SndAddr) + _ = acntSrc.AddToBalance(big.NewInt(100)) + acntDst := createUserAcc(tx.RcvAddr) + _ = acntDst.AddToBalance(big.NewInt(100)) + acntFinal := createUserAcc(userTx.RcvAddr) + _ = acntFinal.AddToBalance(big.NewInt(100)) adb := &stateMock.AccountsStub{} adb.LoadAccountCalled = func(address []byte) (vmcommon.AccountHandler, error) { @@ -2862,12 +2814,12 @@ func TestTxProcessor_ProcessUserTxOfTypeSCDeploymentShouldWork(t *testing.T) { return core.RelayedTransaction, [][]byte{userTxMarshalled}, nil }} - acntSrc, _ := state.NewUserAccount(tx.SndAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntSrc.Balance = big.NewInt(100) - acntDst, _ := state.NewUserAccount(tx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntDst.Balance = big.NewInt(100) - acntFinal, _ := state.NewUserAccount(userTx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntFinal.Balance = big.NewInt(100) + acntSrc := createUserAcc(tx.SndAddr) + _ = acntSrc.AddToBalance(big.NewInt(100)) + acntDst := createUserAcc(tx.RcvAddr) + _ = acntDst.AddToBalance(big.NewInt(100)) + acntFinal := createUserAcc(userTx.RcvAddr) + _ = acntFinal.AddToBalance(big.NewInt(100)) adb := &stateMock.AccountsStub{} adb.LoadAccountCalled = func(address []byte) (vmcommon.AccountHandler, error) { @@ -2926,12 +2878,12 @@ func TestTxProcessor_ProcessUserTxOfTypeSCInvokingShouldWork(t *testing.T) { return core.RelayedTransaction, [][]byte{userTxMarshalled}, nil }} - acntSrc, _ := state.NewUserAccount(tx.SndAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntSrc.Balance = big.NewInt(100) - acntDst, _ := state.NewUserAccount(tx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntDst.Balance = big.NewInt(100) - acntFinal, _ := state.NewUserAccount(userTx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntFinal.Balance = big.NewInt(100) + acntSrc := createUserAcc(tx.SndAddr) + _ = acntSrc.AddToBalance(big.NewInt(100)) + acntDst := createUserAcc(tx.RcvAddr) + _ = acntDst.AddToBalance(big.NewInt(100)) + acntFinal := createUserAcc(userTx.RcvAddr) + _ = acntFinal.AddToBalance(big.NewInt(100)) adb := &stateMock.AccountsStub{} adb.LoadAccountCalled = func(address []byte) (vmcommon.AccountHandler, error) { @@ -2990,12 +2942,12 @@ func TestTxProcessor_ProcessUserTxOfTypeBuiltInFunctionCallShouldWork(t *testing return core.RelayedTransaction, [][]byte{userTxMarshalled}, nil }} - acntSrc, _ := state.NewUserAccount(tx.SndAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntSrc.Balance = big.NewInt(100) - acntDst, _ := state.NewUserAccount(tx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntDst.Balance = big.NewInt(100) - acntFinal, _ := state.NewUserAccount(userTx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntFinal.Balance = big.NewInt(100) + acntSrc := createUserAcc(tx.SndAddr) + _ = acntSrc.AddToBalance(big.NewInt(100)) + acntDst := createUserAcc(tx.RcvAddr) + _ = acntDst.AddToBalance(big.NewInt(100)) + acntFinal := createUserAcc(userTx.RcvAddr) + _ = acntFinal.AddToBalance(big.NewInt(100)) adb := &stateMock.AccountsStub{} adb.LoadAccountCalled = func(address []byte) (vmcommon.AccountHandler, error) { @@ -3058,12 +3010,12 @@ func TestTxProcessor_ProcessUserTxErrNotPayableShouldFailRelayTx(t *testing.T) { return false, process.ErrAccountNotPayable }} - acntSrc, _ := state.NewUserAccount(tx.SndAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntSrc.Balance = big.NewInt(100) - acntDst, _ := state.NewUserAccount(tx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntDst.Balance = big.NewInt(100) - acntFinal, _ := state.NewUserAccount(userTx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntFinal.Balance = big.NewInt(100) + acntSrc := createUserAcc(tx.SndAddr) + _ = acntSrc.AddToBalance(big.NewInt(100)) + acntDst := createUserAcc(tx.RcvAddr) + _ = acntDst.AddToBalance(big.NewInt(100)) + acntFinal := createUserAcc(userTx.RcvAddr) + _ = acntFinal.AddToBalance(big.NewInt(100)) adb := &stateMock.AccountsStub{} adb.LoadAccountCalled = func(address []byte) (vmcommon.AccountHandler, error) { @@ -3128,12 +3080,12 @@ func TestTxProcessor_ProcessUserTxFailedBuiltInFunctionCall(t *testing.T) { }, } - acntSrc, _ := state.NewUserAccount(tx.SndAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntSrc.Balance = big.NewInt(100) - acntDst, _ := state.NewUserAccount(tx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntDst.Balance = big.NewInt(100) - acntFinal, _ := state.NewUserAccount(userTx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntFinal.Balance = big.NewInt(100) + acntSrc := createUserAcc(tx.SndAddr) + _ = acntSrc.AddToBalance(big.NewInt(100)) + acntDst := createUserAcc(tx.RcvAddr) + _ = acntDst.AddToBalance(big.NewInt(100)) + acntFinal := createUserAcc(userTx.RcvAddr) + _ = acntFinal.AddToBalance(big.NewInt(100)) adb := &stateMock.AccountsStub{} adb.LoadAccountCalled = func(address []byte) (vmcommon.AccountHandler, error) { @@ -3188,12 +3140,12 @@ func TestTxProcessor_ExecuteFailingRelayedTxShouldNotHaveNegativeFee(t *testing. args := createArgsForTxProcessor() - acntSrc, _ := state.NewUserAccount(tx.SndAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntSrc.Balance = big.NewInt(100) - acntDst, _ := state.NewUserAccount(tx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntDst.Balance = big.NewInt(100) - acntFinal, _ := state.NewUserAccount(userTx.RcvAddr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - acntFinal.Balance = big.NewInt(100) + acntSrc := createUserAcc(tx.SndAddr) + _ = acntSrc.AddToBalance(big.NewInt(100)) + acntDst := createUserAcc(tx.RcvAddr) + _ = acntDst.AddToBalance(big.NewInt(100)) + acntFinal := createUserAcc(userTx.RcvAddr) + _ = acntFinal.AddToBalance(big.NewInt(100)) adb := &stateMock.AccountsStub{} adb.LoadAccountCalled = func(address []byte) (vmcommon.AccountHandler, error) { diff --git a/sharding/mock/enableEpochsHandlerMock.go b/sharding/mock/enableEpochsHandlerMock.go index ff3378185d7..dbf81b7db88 100644 --- a/sharding/mock/enableEpochsHandlerMock.go +++ b/sharding/mock/enableEpochsHandlerMock.go @@ -541,6 +541,11 @@ func (mock *EnableEpochsHandlerMock) IsFixOldTokenLiquidityEnabled() bool { return false } +// IsAutoBalanceDataTriesEnabled - +func (mock *EnableEpochsHandlerMock) IsAutoBalanceDataTriesEnabled() bool { + return false +} + // IsInterfaceNil returns true if there is no value under the interface func (mock *EnableEpochsHandlerMock) IsInterfaceNil() bool { return mock == nil diff --git a/state/accountsDB.go b/state/accountsDB.go index 5000afa8316..3793ec4cb68 100644 --- a/state/accountsDB.go +++ b/state/accountsDB.go @@ -692,7 +692,7 @@ func (adb *AccountsDB) LoadAccount(address []byte) (vmcommon.AccountHandler, err return nil, err } if check.IfNil(acnt) { - return adb.accountFactory.CreateAccount(address, adb.hasher, adb.marshaller) + return adb.accountFactory.CreateAccount(address) } baseAcc, ok := acnt.(baseAccountHandler) @@ -715,7 +715,7 @@ func (adb *AccountsDB) getAccount(address []byte, mainTrie common.Trie) (vmcommo return nil, nil } - acnt, err := adb.accountFactory.CreateAccount(address, adb.hasher, adb.marshaller) + acnt, err := adb.accountFactory.CreateAccount(address) if err != nil { return nil, err } @@ -764,7 +764,7 @@ func (adb *AccountsDB) GetAccountFromBytes(address []byte, accountBytes []byte) return nil, fmt.Errorf("%w in GetAccountFromBytes", ErrNilAddress) } - acnt, err := adb.accountFactory.CreateAccount(address, adb.hasher, adb.marshaller) + acnt, err := adb.accountFactory.CreateAccount(address) if err != nil { return nil, err } diff --git a/state/accountsDBApiWithHistory_test.go b/state/accountsDBApiWithHistory_test.go index b7bae6a2eff..1013f8df998 100644 --- a/state/accountsDBApiWithHistory_test.go +++ b/state/accountsDBApiWithHistory_test.go @@ -15,7 +15,6 @@ import ( "github.com/ElrondNetwork/elrond-go/common/holders" "github.com/ElrondNetwork/elrond-go/state" "github.com/ElrondNetwork/elrond-go/testscommon" - "github.com/ElrondNetwork/elrond-go/testscommon/hashingMocks" mockState "github.com/ElrondNetwork/elrond-go/testscommon/state" vmcommon "github.com/ElrondNetwork/elrond-vm-common" "github.com/stretchr/testify/assert" @@ -130,7 +129,7 @@ func TestAccountsDBApiWithHistory_GetAccountWithBlockInfo(t *testing.T) { }, GetExistingAccountCalled: func(address []byte) (vmcommon.AccountHandler, error) { if bytes.Equal(address, testscommon.TestPubKeyAlice) { - return state.NewUserAccount(address, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + return createUserAcc(address), nil } return nil, errors.New("not found") diff --git a/state/accountsDBApi_test.go b/state/accountsDBApi_test.go index dbe944a6cef..f8a4ab9b5c3 100644 --- a/state/accountsDBApi_test.go +++ b/state/accountsDBApi_test.go @@ -14,7 +14,6 @@ import ( "github.com/ElrondNetwork/elrond-go/common/holders" "github.com/ElrondNetwork/elrond-go/state" "github.com/ElrondNetwork/elrond-go/testscommon" - "github.com/ElrondNetwork/elrond-go/testscommon/hashingMocks" mockState "github.com/ElrondNetwork/elrond-go/testscommon/state" "github.com/ElrondNetwork/elrond-go/testscommon/trie" vmcommon "github.com/ElrondNetwork/elrond-vm-common" @@ -309,7 +308,7 @@ func TestAccountsDBApi_GetExistingAccount(t *testing.T) { return nil }, GetExistingAccountCalled: func(addressContainer []byte) (vmcommon.AccountHandler, error) { - return state.NewUserAccount(addressContainer, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + return createUserAcc(addressContainer), nil }, } @@ -353,7 +352,7 @@ func TestAccountsDBApi_GetAccountFromBytes(t *testing.T) { return nil }, GetAccountFromBytesCalled: func(address []byte, accountBytes []byte) (vmcommon.AccountHandler, error) { - return state.NewUserAccount(address, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + return createUserAcc(address), nil }, } @@ -397,7 +396,7 @@ func TestAccountsDBApi_LoadAccount(t *testing.T) { return nil }, LoadAccountCalled: func(address []byte) (vmcommon.AccountHandler, error) { - return state.NewUserAccount(address, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + return createUserAcc(address), nil }, } diff --git a/state/accountsDB_test.go b/state/accountsDB_test.go index 183cb0b8ba1..7f01a0f6190 100644 --- a/state/accountsDB_test.go +++ b/state/accountsDB_test.go @@ -62,6 +62,19 @@ func createMockAccountsDBArgs() state.ArgsAccountsDB { } } +func getDefaultArgsAccountCreation() state.ArgsAccountCreation { + return state.ArgsAccountCreation{ + Hasher: &hashingMocks.HasherMock{}, + Marshaller: &testscommon.MarshalizerMock{}, + EnableEpochsHandler: &testscommon.EnableEpochsHandlerStub{}, + } +} + +func createUserAcc(address []byte) state.UserAccountHandler { + acc, _ := state.NewUserAccount(address, getDefaultArgsAccountCreation()) + return acc +} + func generateAccountDBFromTrie(trie common.Trie) *state.AccountsDB { args := createMockAccountsDBArgs() args.Trie = trie @@ -113,12 +126,18 @@ func getDefaultStateComponents( tr, _ := trie.NewTrie(trieStorage, marshaller, hasher, 5) ewl, _ := evictionWaitingList.NewEvictionWaitingList(100, testscommon.NewMemDbMock(), marshaller) spm, _ := storagePruningManager.NewStoragePruningManager(ewl, generalCfg.PruningBufferLen) + argsAccCreator := state.ArgsAccountCreation{ + Hasher: hasher, + Marshaller: marshaller, + EnableEpochsHandler: &testscommon.EnableEpochsHandlerStub{}, + } + accCreator, _ := factory.NewAccountCreator(argsAccCreator) argsAccountsDB := state.ArgsAccountsDB{ Trie: tr, Hasher: hasher, Marshaller: marshaller, - AccountFactory: factory.NewAccountCreator(), + AccountFactory: accCreator, StoragePruningManager: spm, ProcessingMode: common.Normal, ProcessStatusHandler: &testscommon.ProcessStatusHandlerStub{}, @@ -260,7 +279,7 @@ func TestAccountsDB_SaveAccountNilOldAccount(t *testing.T) { }, }) - acc, _ := state.NewUserAccount([]byte("someAddress"), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acc := createUserAcc([]byte("someAddress")) err := adb.SaveAccount(acc) assert.Nil(t, err) assert.Equal(t, 1, adb.JournalLen()) @@ -269,8 +288,7 @@ func TestAccountsDB_SaveAccountNilOldAccount(t *testing.T) { func TestAccountsDB_SaveAccountExistingOldAccount(t *testing.T) { t.Parallel() - acc, _ := state.NewUserAccount([]byte("someAddress"), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - + acc := createUserAcc([]byte("someAddress")) adb := generateAccountDBFromTrie(&trieMock.TrieStub{ GetCalled: func(_ []byte) ([]byte, uint32, error) { serializedAcc, err := (&testscommon.MarshalizerMock{}).Marshal(acc) @@ -322,7 +340,7 @@ func TestAccountsDB_SaveAccountSavesCodeAndDataTrieForUserAccount(t *testing.T) }) accCode := []byte("code") - acc, _ := state.NewUserAccount([]byte("someAddress"), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acc := createUserAcc([]byte("someAddress")) acc.SetCode(accCode) _ = acc.SaveKeyValue([]byte("key"), []byte("value")) @@ -1747,7 +1765,12 @@ func TestAccountsDB_MainTrieAutomaticallyMarksCodeUpdatesForEviction(t *testing. argsAccountsDB.Trie = tr argsAccountsDB.Hasher = hasher argsAccountsDB.Marshaller = marshaller - argsAccountsDB.AccountFactory = factory.NewAccountCreator() + argsAccCreator := state.ArgsAccountCreation{ + Hasher: hasher, + Marshaller: marshaller, + EnableEpochsHandler: &testscommon.EnableEpochsHandlerStub{}, + } + argsAccountsDB.AccountFactory, _ = factory.NewAccountCreator(argsAccCreator) argsAccountsDB.StoragePruningManager = spm adb, _ := state.NewAccountsDB(argsAccountsDB) @@ -1831,7 +1854,12 @@ func TestAccountsDB_RemoveAccountMarksObsoleteHashesForEviction(t *testing.T) { argsAccountsDB.Trie = tr argsAccountsDB.Hasher = hasher argsAccountsDB.Marshaller = marshaller - argsAccountsDB.AccountFactory = factory.NewAccountCreator() + argsAccCreator := state.ArgsAccountCreation{ + Hasher: hasher, + Marshaller: marshaller, + EnableEpochsHandler: &testscommon.EnableEpochsHandlerStub{}, + } + argsAccountsDB.AccountFactory, _ = factory.NewAccountCreator(argsAccCreator) argsAccountsDB.StoragePruningManager = spm adb, _ := state.NewAccountsDB(argsAccountsDB) @@ -2258,7 +2286,12 @@ func TestAccountsDB_GetCode(t *testing.T) { argsAccountsDB.Trie = tr argsAccountsDB.Hasher = hasher argsAccountsDB.Marshaller = marshaller - argsAccountsDB.AccountFactory = factory.NewAccountCreator() + argsAccCreator := state.ArgsAccountCreation{ + Hasher: hasher, + Marshaller: marshaller, + EnableEpochsHandler: &testscommon.EnableEpochsHandlerStub{}, + } + argsAccountsDB.AccountFactory, _ = factory.NewAccountCreator(argsAccCreator) argsAccountsDB.StoragePruningManager = spm adb, _ := state.NewAccountsDB(argsAccountsDB) @@ -2410,7 +2443,12 @@ func TestAccountsDB_Close(t *testing.T) { argsAccountsDB.Trie = tr argsAccountsDB.Hasher = hasher argsAccountsDB.Marshaller = marshaller - argsAccountsDB.AccountFactory = factory.NewAccountCreator() + argsAccCreator := state.ArgsAccountCreation{ + Hasher: hasher, + Marshaller: marshaller, + EnableEpochsHandler: &testscommon.EnableEpochsHandlerStub{}, + } + argsAccountsDB.AccountFactory, _ = factory.NewAccountCreator(argsAccCreator) argsAccountsDB.StoragePruningManager = spm adb, _ := state.NewAccountsDB(argsAccountsDB) @@ -2436,7 +2474,7 @@ func TestAccountsDB_GetAccountFromBytes(t *testing.T) { marshaller := &testscommon.MarshalizerMock{} adr := make([]byte, 32) - accountExpected, _ := state.NewUserAccount(adr, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + accountExpected := createUserAcc(adr) accountBytes, _ := marshaller.Marshal(accountExpected) _, adb := getDefaultTrieAndAccountsDb() @@ -2645,7 +2683,12 @@ func BenchmarkAccountsDb_GetCodeEntry(b *testing.B) { argsAccountsDB.Trie = tr argsAccountsDB.Hasher = hasher argsAccountsDB.Marshaller = marshaller - argsAccountsDB.AccountFactory = factory.NewAccountCreator() + argsAccCreator := state.ArgsAccountCreation{ + Hasher: hasher, + Marshaller: marshaller, + EnableEpochsHandler: &testscommon.EnableEpochsHandlerStub{}, + } + argsAccountsDB.AccountFactory, _ = factory.NewAccountCreator(argsAccCreator) argsAccountsDB.StoragePruningManager = spm adb, _ := state.NewAccountsDB(argsAccountsDB) diff --git a/state/disabled/disabledDataTrieHandler.go b/state/disabled/disabledDataTrieHandler.go new file mode 100644 index 00000000000..c63fec6a3fb --- /dev/null +++ b/state/disabled/disabledDataTrieHandler.go @@ -0,0 +1,42 @@ +package disabled + +import ( + "context" + + "github.com/ElrondNetwork/elrond-go/common" +) + +type disabledDataTrieHandler struct { +} + +// NewDisabledDataTrieHandler returns a new instance of disabledDataTrieHandler +func NewDisabledDataTrieHandler() *disabledDataTrieHandler { + return &disabledDataTrieHandler{} +} + +// RootHash returns an empty byte array +func (ddth *disabledDataTrieHandler) RootHash() ([]byte, error) { + return []byte{}, nil +} + +// GetAllLeavesOnChannel does nothing for this implementation +func (ddth *disabledDataTrieHandler) GetAllLeavesOnChannel( + leavesChannels *common.TrieIteratorChannels, + _ context.Context, + _ []byte, + _ common.KeyBuilder, +) error { + if leavesChannels.LeavesChan != nil { + close(leavesChannels.LeavesChan) + } + if leavesChannels.ErrChan != nil { + close(leavesChannels.ErrChan) + } + + return nil +} + +// IsInterfaceNil returns true if there is no value under the interface +func (ddth *disabledDataTrieHandler) IsInterfaceNil() bool { + return ddth == nil +} diff --git a/state/disabled/disabledDataTrieHandler_test.go b/state/disabled/disabledDataTrieHandler_test.go new file mode 100644 index 00000000000..0de29daee95 --- /dev/null +++ b/state/disabled/disabledDataTrieHandler_test.go @@ -0,0 +1,48 @@ +package disabled + +import ( + "testing" + + "github.com/ElrondNetwork/elrond-go-core/core" + "github.com/ElrondNetwork/elrond-go-core/core/check" + "github.com/ElrondNetwork/elrond-go/common" + "github.com/stretchr/testify/assert" +) + +func TestNewDisabledDataTrieHandler(t *testing.T) { + t.Parallel() + + t.Run("new disabledDataTrieHandler", func(t *testing.T) { + t.Parallel() + + assert.False(t, check.IfNil(NewDisabledDataTrieHandler())) + }) + + t.Run("root hash", func(t *testing.T) { + t.Parallel() + + ddth := NewDisabledDataTrieHandler() + + rootHash, err := ddth.RootHash() + assert.Equal(t, 0, len(rootHash)) + assert.Nil(t, err) + }) + + t.Run("get all leaves on channel", func(t *testing.T) { + t.Parallel() + + ddth := NewDisabledDataTrieHandler() + + chans := &common.TrieIteratorChannels{ + LeavesChan: make(chan core.KeyValueHolder, 1), + ErrChan: make(chan error), + } + + err := ddth.GetAllLeavesOnChannel(chans, nil, nil, nil) + assert.Nil(t, err) + _, ok := <-chans.LeavesChan + assert.False(t, ok) + _, ok = <-chans.ErrChan + assert.False(t, ok) + }) +} diff --git a/state/disabled/disabledTrackableDataTrie.go b/state/disabled/disabledTrackableDataTrie.go new file mode 100644 index 00000000000..1c210762223 --- /dev/null +++ b/state/disabled/disabledTrackableDataTrie.go @@ -0,0 +1,40 @@ +package disabled + +import "github.com/ElrondNetwork/elrond-go/common" + +type disabledTrackableDataTrie struct { +} + +// NewDisabledTrackableDataTrie returns a new instance of disabledTrackableDataTrie +func NewDisabledTrackableDataTrie() *disabledTrackableDataTrie { + return &disabledTrackableDataTrie{} +} + +// RetrieveValue returns an empty byte array +func (dtdt *disabledTrackableDataTrie) RetrieveValue(_ []byte) ([]byte, uint32, error) { + return []byte{}, 0, nil +} + +// SaveKeyValue does nothing for this implementation +func (dtdt *disabledTrackableDataTrie) SaveKeyValue(_ []byte, _ []byte) error { + return nil +} + +// SetDataTrie does nothing for this implementation +func (dtdt *disabledTrackableDataTrie) SetDataTrie(_ common.Trie) { +} + +// DataTrie returns a new disabledDataTrieHandler +func (dtdt *disabledTrackableDataTrie) DataTrie() common.DataTrieHandler { + return NewDisabledDataTrieHandler() +} + +// SaveDirtyData does nothing for this implementation +func (dtdt *disabledTrackableDataTrie) SaveDirtyData(_ common.Trie) (map[string][]byte, error) { + return map[string][]byte{}, nil +} + +// IsInterfaceNil returns true if there is no value under the interface +func (dtdt *disabledTrackableDataTrie) IsInterfaceNil() bool { + return dtdt == nil +} diff --git a/state/disabled/disabledTrackableDataTrie_test.go b/state/disabled/disabledTrackableDataTrie_test.go new file mode 100644 index 00000000000..6d383680346 --- /dev/null +++ b/state/disabled/disabledTrackableDataTrie_test.go @@ -0,0 +1,61 @@ +package disabled + +import ( + "testing" + + "github.com/ElrondNetwork/elrond-go-core/core/check" + "github.com/stretchr/testify/assert" +) + +func TestNewDisabledTrackableDataTrie(t *testing.T) { + t.Parallel() + + assert.False(t, check.IfNil(NewDisabledTrackableDataTrie())) +} + +func TestDisabledTrackableDataTrie_RetrieveValue(t *testing.T) { + t.Parallel() + + dtdt := NewDisabledTrackableDataTrie() + + val, depth, err := dtdt.RetrieveValue(nil) + assert.Nil(t, err) + assert.Equal(t, uint32(0), depth) + assert.Equal(t, 0, len(val)) +} + +func TestDisabledTrackableDataTrie_SaveKeyValue(t *testing.T) { + t.Parallel() + + dtdt := NewDisabledTrackableDataTrie() + + err := dtdt.SaveKeyValue(nil, nil) + assert.Nil(t, err) +} + +func TestDisabledTrackableDataTrie_SetAndGetDataTrie(t *testing.T) { + t.Parallel() + + dtdt := NewDisabledTrackableDataTrie() + isDisabledDataTrieHandler := false + dtdt.SetDataTrie(nil) + tr := dtdt.DataTrie() + + switch tr.(type) { + case *disabledDataTrieHandler: + isDisabledDataTrieHandler = true + default: + assert.Fail(t, "this should not have been called") + } + assert.True(t, isDisabledDataTrieHandler) +} + +func TestDisabledTrackableDataTrie_SaveDirtyData(t *testing.T) { + t.Parallel() + + dtdt := NewDisabledTrackableDataTrie() + + oldValues, err := dtdt.SaveDirtyData(nil) + assert.Nil(t, err) + assert.Equal(t, 0, len(oldValues)) +} diff --git a/state/errors.go b/state/errors.go index cab2938cbb7..df2bb129a93 100644 --- a/state/errors.go +++ b/state/errors.go @@ -161,3 +161,6 @@ var ErrNilTrieSyncer = errors.New("trie syncer is nil") // ErrNilSyncStatisticsHandler signals that a nil sync statistics handler was provided var ErrNilSyncStatisticsHandler = errors.New("nil sync statistics handler") + +// ErrNilEnableEpochsHandler signals that a nil enable epochs handler has been provided +var ErrNilEnableEpochsHandler = errors.New("nil enable epochs handler") diff --git a/state/factory/accountCreator.go b/state/factory/accountCreator.go index 2a6745817d0..2b638e084bb 100644 --- a/state/factory/accountCreator.go +++ b/state/factory/accountCreator.go @@ -1,24 +1,37 @@ package factory import ( - "github.com/ElrondNetwork/elrond-go-core/hashing" - "github.com/ElrondNetwork/elrond-go-core/marshal" + "github.com/ElrondNetwork/elrond-go-core/core/check" + "github.com/ElrondNetwork/elrond-go/errors" "github.com/ElrondNetwork/elrond-go/state" vmcommon "github.com/ElrondNetwork/elrond-vm-common" ) // AccountCreator has method to create a new account type AccountCreator struct { + accountArgs state.ArgsAccountCreation } -// NewAccountCreator creates an account creator -func NewAccountCreator() state.AccountFactory { - return &AccountCreator{} +// NewAccountCreator creates a new instance of AccountCreator +func NewAccountCreator(args state.ArgsAccountCreation) (state.AccountFactory, error) { + if check.IfNil(args.Hasher) { + return nil, errors.ErrNilHasher + } + if check.IfNil(args.Marshaller) { + return nil, errors.ErrNilMarshalizer + } + if check.IfNil(args.EnableEpochsHandler) { + return nil, errors.ErrNilEnableEpochsHandler + } + + return &AccountCreator{ + accountArgs: args, + }, nil } // CreateAccount calls the new Account creator and returns the result -func (ac *AccountCreator) CreateAccount(address []byte, hasher hashing.Hasher, marshaller marshal.Marshalizer) (vmcommon.AccountHandler, error) { - return state.NewUserAccount(address, hasher, marshaller) +func (ac *AccountCreator) CreateAccount(address []byte) (vmcommon.AccountHandler, error) { + return state.NewUserAccount(address, ac.accountArgs) } // IsInterfaceNil returns true if there is no value under the interface diff --git a/state/factory/accountCreator_test.go b/state/factory/accountCreator_test.go index 859eb771c46..e8eaafef47b 100644 --- a/state/factory/accountCreator_test.go +++ b/state/factory/accountCreator_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/ElrondNetwork/elrond-go-core/core/check" + "github.com/ElrondNetwork/elrond-go/errors" "github.com/ElrondNetwork/elrond-go/state" "github.com/ElrondNetwork/elrond-go/state/factory" "github.com/ElrondNetwork/elrond-go/testscommon" @@ -11,16 +12,63 @@ import ( "github.com/stretchr/testify/assert" ) +func getDefaultArgs() state.ArgsAccountCreation { + return state.ArgsAccountCreation{ + Hasher: &hashingMocks.HasherMock{}, + Marshaller: &testscommon.MarshalizerMock{}, + EnableEpochsHandler: &testscommon.EnableEpochsHandlerStub{}, + } +} + +func TestNewAccountCreator(t *testing.T) { + t.Parallel() + + t.Run("nil hasher", func(t *testing.T) { + t.Parallel() + + args := getDefaultArgs() + args.Hasher = nil + accF, err := factory.NewAccountCreator(args) + assert.True(t, check.IfNil(accF)) + assert.Equal(t, errors.ErrNilHasher, err) + }) + t.Run("nil marshalizer", func(t *testing.T) { + t.Parallel() + + args := getDefaultArgs() + args.Marshaller = nil + accF, err := factory.NewAccountCreator(args) + assert.True(t, check.IfNil(accF)) + assert.Equal(t, errors.ErrNilMarshalizer, err) + }) + t.Run("nil enableEpochsHandler", func(t *testing.T) { + t.Parallel() + + args := getDefaultArgs() + args.EnableEpochsHandler = nil + accF, err := factory.NewAccountCreator(args) + assert.True(t, check.IfNil(accF)) + assert.Equal(t, errors.ErrNilEnableEpochsHandler, err) + }) + t.Run("should work", func(t *testing.T) { + t.Parallel() + + accF, err := factory.NewAccountCreator(getDefaultArgs()) + assert.False(t, check.IfNil(accF)) + assert.Nil(t, err) + }) +} + func TestAccountCreator_CreateAccountNilAddress(t *testing.T) { t.Parallel() - accF := factory.NewAccountCreator() + accF, _ := factory.NewAccountCreator(getDefaultArgs()) _, ok := accF.(*factory.AccountCreator) assert.Equal(t, true, ok) assert.False(t, check.IfNil(accF)) - acc, err := accF.CreateAccount(nil, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acc, err := accF.CreateAccount(nil) assert.Nil(t, acc) assert.Equal(t, err, state.ErrNilAddress) @@ -29,12 +77,12 @@ func TestAccountCreator_CreateAccountNilAddress(t *testing.T) { func TestAccountCreator_CreateAccountOk(t *testing.T) { t.Parallel() - accF := factory.NewAccountCreator() + accF, _ := factory.NewAccountCreator(getDefaultArgs()) _, ok := accF.(*factory.AccountCreator) assert.Equal(t, true, ok) - acc, err := accF.CreateAccount(make([]byte, 32), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acc, err := accF.CreateAccount(make([]byte, 32)) assert.Nil(t, err) assert.False(t, check.IfNil(acc)) diff --git a/state/factory/peerAccountCreator.go b/state/factory/peerAccountCreator.go index 3f20c56c6fe..a17e3555738 100644 --- a/state/factory/peerAccountCreator.go +++ b/state/factory/peerAccountCreator.go @@ -1,8 +1,6 @@ package factory import ( - "github.com/ElrondNetwork/elrond-go-core/hashing" - "github.com/ElrondNetwork/elrond-go-core/marshal" "github.com/ElrondNetwork/elrond-go/state" vmcommon "github.com/ElrondNetwork/elrond-vm-common" ) @@ -17,8 +15,8 @@ func NewPeerAccountCreator() state.AccountFactory { } // CreateAccount calls the new Account creator and returns the result -func (pac *PeerAccountCreator) CreateAccount(address []byte, hasher hashing.Hasher, marshaller marshal.Marshalizer) (vmcommon.AccountHandler, error) { - return state.NewPeerAccount(address, hasher, marshaller) +func (pac *PeerAccountCreator) CreateAccount(address []byte) (vmcommon.AccountHandler, error) { + return state.NewPeerAccount(address) } // IsInterfaceNil returns true if there is no value under the interface diff --git a/state/factory/peerAccountCreator_test.go b/state/factory/peerAccountCreator_test.go index f23eb0ec0da..5ec6182ad22 100644 --- a/state/factory/peerAccountCreator_test.go +++ b/state/factory/peerAccountCreator_test.go @@ -6,8 +6,6 @@ import ( "github.com/ElrondNetwork/elrond-go-core/core/check" "github.com/ElrondNetwork/elrond-go/state" "github.com/ElrondNetwork/elrond-go/state/factory" - "github.com/ElrondNetwork/elrond-go/testscommon" - "github.com/ElrondNetwork/elrond-go/testscommon/hashingMocks" "github.com/stretchr/testify/assert" ) @@ -19,7 +17,7 @@ func TestPeerAccountCreator_CreateAccountNilAddress(t *testing.T) { _, ok := accF.(*factory.PeerAccountCreator) assert.Equal(t, true, ok) - acc, err := accF.CreateAccount(nil, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acc, err := accF.CreateAccount(nil) assert.Nil(t, acc) assert.Equal(t, err, state.ErrNilAddress) @@ -34,7 +32,7 @@ func TestPeerAccountCreator_CreateAccountOk(t *testing.T) { _, ok := accF.(*factory.PeerAccountCreator) assert.Equal(t, true, ok) - acc, err := accF.CreateAccount(make([]byte, 32), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acc, err := accF.CreateAccount(make([]byte, 32)) assert.NotNil(t, acc) assert.Nil(t, err) diff --git a/state/interface.go b/state/interface.go index 6eabadb195b..26575e8eaee 100644 --- a/state/interface.go +++ b/state/interface.go @@ -5,15 +5,13 @@ import ( "math/big" "github.com/ElrondNetwork/elrond-go-core/data/api" - "github.com/ElrondNetwork/elrond-go-core/hashing" - "github.com/ElrondNetwork/elrond-go-core/marshal" "github.com/ElrondNetwork/elrond-go/common" vmcommon "github.com/ElrondNetwork/elrond-vm-common" ) // AccountFactory creates an account of different types type AccountFactory interface { - CreateAccount(address []byte, hasher hashing.Hasher, marshaller marshal.Marshalizer) (vmcommon.AccountHandler, error) + CreateAccount(address []byte) (vmcommon.AccountHandler, error) IsInterfaceNil() bool } diff --git a/state/journalEntries_test.go b/state/journalEntries_test.go index 476d9b396a8..259def2f043 100644 --- a/state/journalEntries_test.go +++ b/state/journalEntries_test.go @@ -186,7 +186,12 @@ func TestNewJournalEntryDataTrieUpdates_EmptyTrieUpdatesShouldErr(t *testing.T) t.Parallel() trieUpdates := make(map[string][]byte) - accnt, _ := state.NewUserAccount(make([]byte, 32), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + args := state.ArgsAccountCreation{ + Hasher: &hashingMocks.HasherMock{}, + Marshaller: &testscommon.MarshalizerMock{}, + EnableEpochsHandler: &testscommon.EnableEpochsHandlerStub{}, + } + accnt, _ := state.NewUserAccount(make([]byte, 32), args) entry, err := state.NewJournalEntryDataTrieUpdates(trieUpdates, accnt) assert.True(t, check.IfNil(entry)) @@ -198,7 +203,12 @@ func TestNewJournalEntryDataTrieUpdates_OkValsShouldWork(t *testing.T) { trieUpdates := make(map[string][]byte) trieUpdates["a"] = []byte("b") - accnt, _ := state.NewUserAccount(make([]byte, 32), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + args := state.ArgsAccountCreation{ + Hasher: &hashingMocks.HasherMock{}, + Marshaller: &testscommon.MarshalizerMock{}, + EnableEpochsHandler: &testscommon.EnableEpochsHandlerStub{}, + } + accnt, _ := state.NewUserAccount(make([]byte, 32), args) entry, err := state.NewJournalEntryDataTrieUpdates(trieUpdates, accnt) assert.Nil(t, err) diff --git a/state/peerAccount.go b/state/peerAccount.go index adb5338a2ef..eefaa2d60ba 100644 --- a/state/peerAccount.go +++ b/state/peerAccount.go @@ -4,13 +4,13 @@ package state import ( "math/big" - "github.com/ElrondNetwork/elrond-go-core/hashing" - "github.com/ElrondNetwork/elrond-go-core/marshal" "github.com/ElrondNetwork/elrond-go/common" + "github.com/ElrondNetwork/elrond-go/state/disabled" ) // PeerAccount is the struct used in serialization/deserialization type peerAccount struct { + //TODO investigate if *baseAccount is needed in peerAccount, and remove if not *baseAccount PeerAccountData } @@ -26,21 +26,16 @@ func NewEmptyPeerAccount() *peerAccount { } } -// NewPeerAccount creates new simple account wrapper for an PeerAccountContainer (that has just been initialized) -func NewPeerAccount(address []byte, hasher hashing.Hasher, marshaller marshal.Marshalizer) (*peerAccount, error) { +// NewPeerAccount creates a new instance of peerAccount +func NewPeerAccount(address []byte) (*peerAccount, error) { if len(address) == 0 { return nil, ErrNilAddress } - tdt, err := NewTrackableDataTrie(address, nil, hasher, marshaller) - if err != nil { - return nil, err - } - return &peerAccount{ baseAccount: &baseAccount{ address: address, - dataTrieTracker: tdt, + dataTrieTracker: disabled.NewDisabledTrackableDataTrie(), }, PeerAccountData: PeerAccountData{ AccumulatedFees: big.NewInt(0), diff --git a/state/peerAccount_test.go b/state/peerAccount_test.go index 9d4ea9104c2..212f80bcbdb 100644 --- a/state/peerAccount_test.go +++ b/state/peerAccount_test.go @@ -6,8 +6,6 @@ import ( "github.com/ElrondNetwork/elrond-go-core/core/check" "github.com/ElrondNetwork/elrond-go/state" - "github.com/ElrondNetwork/elrond-go/testscommon" - "github.com/ElrondNetwork/elrond-go/testscommon/hashingMocks" "github.com/stretchr/testify/assert" ) @@ -23,7 +21,7 @@ func TestNewEmptyPeerAccount(t *testing.T) { func TestNewPeerAccount_NilAddressContainerShouldErr(t *testing.T) { t.Parallel() - acc, err := state.NewPeerAccount(nil, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acc, err := state.NewPeerAccount(nil) assert.True(t, check.IfNil(acc)) assert.Equal(t, state.ErrNilAddress, err) } @@ -31,7 +29,7 @@ func TestNewPeerAccount_NilAddressContainerShouldErr(t *testing.T) { func TestNewPeerAccount_OkParamsShouldWork(t *testing.T) { t.Parallel() - acc, err := state.NewPeerAccount(make([]byte, 32), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acc, err := state.NewPeerAccount(make([]byte, 32)) assert.Nil(t, err) assert.False(t, check.IfNil(acc)) } @@ -39,7 +37,7 @@ func TestNewPeerAccount_OkParamsShouldWork(t *testing.T) { func TestPeerAccount_SetInvalidBLSPublicKey(t *testing.T) { t.Parallel() - acc, _ := state.NewPeerAccount(make([]byte, 32), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acc, _ := state.NewPeerAccount(make([]byte, 32)) pubKey := []byte("") err := acc.SetBLSPublicKey(pubKey) @@ -49,7 +47,7 @@ func TestPeerAccount_SetInvalidBLSPublicKey(t *testing.T) { func TestPeerAccount_SetAndGetBLSPublicKey(t *testing.T) { t.Parallel() - acc, _ := state.NewPeerAccount(make([]byte, 32), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acc, _ := state.NewPeerAccount(make([]byte, 32)) pubKey := []byte("BLSpubKey") err := acc.SetBLSPublicKey(pubKey) @@ -60,7 +58,7 @@ func TestPeerAccount_SetAndGetBLSPublicKey(t *testing.T) { func TestPeerAccount_SetRewardAddressInvalidAddress(t *testing.T) { t.Parallel() - acc, _ := state.NewPeerAccount(make([]byte, 32), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acc, _ := state.NewPeerAccount(make([]byte, 32)) err := acc.SetRewardAddress([]byte{}) assert.Equal(t, state.ErrEmptyAddress, err) @@ -69,7 +67,7 @@ func TestPeerAccount_SetRewardAddressInvalidAddress(t *testing.T) { func TestPeerAccount_SetAndGetRewardAddress(t *testing.T) { t.Parallel() - acc, _ := state.NewPeerAccount(make([]byte, 32), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acc, _ := state.NewPeerAccount(make([]byte, 32)) addr := []byte("reward address") _ = acc.SetRewardAddress(addr) @@ -79,7 +77,7 @@ func TestPeerAccount_SetAndGetRewardAddress(t *testing.T) { func TestPeerAccount_SetAndGetAccumulatedFees(t *testing.T) { t.Parallel() - acc, _ := state.NewPeerAccount(make([]byte, 32), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acc, _ := state.NewPeerAccount(make([]byte, 32)) fees := big.NewInt(10) acc.AddToAccumulatedFees(fees) @@ -89,7 +87,7 @@ func TestPeerAccount_SetAndGetAccumulatedFees(t *testing.T) { func TestPeerAccount_SetAndGetLeaderSuccessRate(t *testing.T) { t.Parallel() - acc, _ := state.NewPeerAccount(make([]byte, 32), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acc, _ := state.NewPeerAccount(make([]byte, 32)) increaseVal := uint32(5) decreaseVal := uint32(3) @@ -103,7 +101,7 @@ func TestPeerAccount_SetAndGetLeaderSuccessRate(t *testing.T) { func TestPeerAccount_SetAndGetValidatorSuccessRate(t *testing.T) { t.Parallel() - acc, _ := state.NewPeerAccount(make([]byte, 32), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acc, _ := state.NewPeerAccount(make([]byte, 32)) increaseVal := uint32(5) decreaseVal := uint32(3) @@ -117,7 +115,7 @@ func TestPeerAccount_SetAndGetValidatorSuccessRate(t *testing.T) { func TestPeerAccount_IncreaseAndGetSetNumSelectedInSuccessBlocks(t *testing.T) { t.Parallel() - acc, _ := state.NewPeerAccount(make([]byte, 32), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acc, _ := state.NewPeerAccount(make([]byte, 32)) acc.IncreaseNumSelectedInSuccessBlocks() assert.Equal(t, uint32(1), acc.GetNumSelectedInSuccessBlocks()) @@ -126,7 +124,7 @@ func TestPeerAccount_IncreaseAndGetSetNumSelectedInSuccessBlocks(t *testing.T) { func TestPeerAccount_SetAndGetRating(t *testing.T) { t.Parallel() - acc, _ := state.NewPeerAccount(make([]byte, 32), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acc, _ := state.NewPeerAccount(make([]byte, 32)) rating := uint32(10) acc.SetRating(rating) @@ -136,7 +134,7 @@ func TestPeerAccount_SetAndGetRating(t *testing.T) { func TestPeerAccount_SetAndGetTempRating(t *testing.T) { t.Parallel() - acc, _ := state.NewPeerAccount(make([]byte, 32), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acc, _ := state.NewPeerAccount(make([]byte, 32)) rating := uint32(10) acc.SetTempRating(rating) @@ -146,7 +144,7 @@ func TestPeerAccount_SetAndGetTempRating(t *testing.T) { func TestPeerAccount_ResetAtNewEpoch(t *testing.T) { t.Parallel() - acc, _ := state.NewPeerAccount(make([]byte, 32), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acc, _ := state.NewPeerAccount(make([]byte, 32)) acc.AddToAccumulatedFees(big.NewInt(15)) tempRating := uint32(5) acc.SetTempRating(tempRating) @@ -171,7 +169,7 @@ func TestPeerAccount_ResetAtNewEpoch(t *testing.T) { func TestPeerAccount_IncreaseAndGetNonce(t *testing.T) { t.Parallel() - acc, _ := state.NewPeerAccount(make([]byte, 32), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acc, _ := state.NewPeerAccount(make([]byte, 32)) nonce := uint64(5) acc.IncreaseNonce(nonce) diff --git a/state/storagePruningManager/storagePruningManager_test.go b/state/storagePruningManager/storagePruningManager_test.go index 69b51c38c8c..9cebc7f6260 100644 --- a/state/storagePruningManager/storagePruningManager_test.go +++ b/state/storagePruningManager/storagePruningManager_test.go @@ -37,12 +37,18 @@ func getDefaultTrieAndAccountsDbAndStoragePruningManager() (common.Trie, *state. tr, _ := trie.NewTrie(trieStorage, marshaller, hasher, 5) ewl, _ := evictionWaitingList.NewEvictionWaitingList(100, testscommon.NewMemDbMock(), marshaller) spm, _ := NewStoragePruningManager(ewl, generalCfg.PruningBufferLen) + argsAccCreator := state.ArgsAccountCreation{ + Hasher: hasher, + Marshaller: marshaller, + EnableEpochsHandler: &testscommon.EnableEpochsHandlerStub{}, + } + accCreator, _ := factory.NewAccountCreator(argsAccCreator) argsAccountsDB := state.ArgsAccountsDB{ Trie: tr, Hasher: hasher, Marshaller: marshaller, - AccountFactory: factory.NewAccountCreator(), + AccountFactory: accCreator, StoragePruningManager: spm, ProcessingMode: common.Normal, ProcessStatusHandler: &testscommon.ProcessStatusHandlerStub{}, diff --git a/state/userAccount.go b/state/userAccount.go index 457681efc51..0e2e80d8d8a 100644 --- a/state/userAccount.go +++ b/state/userAccount.go @@ -5,8 +5,10 @@ import ( "bytes" "math/big" + "github.com/ElrondNetwork/elrond-go-core/core/check" "github.com/ElrondNetwork/elrond-go-core/hashing" "github.com/ElrondNetwork/elrond-go-core/marshal" + "github.com/ElrondNetwork/elrond-go/common" ) var _ UserAccountHandler = (*userAccount)(nil) @@ -19,7 +21,7 @@ type userAccount struct { var zero = big.NewInt(0) -// NewEmptyUserAccount creates new simple account wrapper for an AccountContainer (that has just been initialized) +// NewEmptyUserAccount creates a new empty instance of userAccount func NewEmptyUserAccount() *userAccount { return &userAccount{ baseAccount: &baseAccount{}, @@ -30,13 +32,32 @@ func NewEmptyUserAccount() *userAccount { } } -// NewUserAccount creates new simple account wrapper for an AccountContainer (that has just been initialized) -func NewUserAccount(address []byte, hasher hashing.Hasher, marshaller marshal.Marshalizer) (*userAccount, error) { +// ArgsAccountCreation holds the arguments needed to create a new instance of userAccount +type ArgsAccountCreation struct { + Hasher hashing.Hasher + Marshaller marshal.Marshalizer + EnableEpochsHandler common.EnableEpochsHandler +} + +// NewUserAccount creates a new instance of userAccount +func NewUserAccount( + address []byte, + args ArgsAccountCreation, +) (*userAccount, error) { if len(address) == 0 { return nil, ErrNilAddress } + if check.IfNil(args.Marshaller) { + return nil, ErrNilMarshalizer + } + if check.IfNil(args.Hasher) { + return nil, ErrNilHasher + } + if check.IfNil(args.EnableEpochsHandler) { + return nil, ErrNilEnableEpochsHandler + } - tdt, err := NewTrackableDataTrie(address, nil, hasher, marshaller) + tdt, err := NewTrackableDataTrie(address, nil, args.Hasher, args.Marshaller) if err != nil { return nil, err } diff --git a/state/userAccount_test.go b/state/userAccount_test.go index 6da524b98dc..ee62386058f 100644 --- a/state/userAccount_test.go +++ b/state/userAccount_test.go @@ -6,31 +6,63 @@ import ( "github.com/ElrondNetwork/elrond-go-core/core/check" "github.com/ElrondNetwork/elrond-go/state" - "github.com/ElrondNetwork/elrond-go/testscommon" - "github.com/ElrondNetwork/elrond-go/testscommon/hashingMocks" "github.com/stretchr/testify/assert" ) -func TestNewUserAccount_NilAddressContainerShouldErr(t *testing.T) { +func TestNewUserAccount(t *testing.T) { t.Parallel() - acc, err := state.NewUserAccount(nil, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.True(t, check.IfNil(acc)) - assert.Equal(t, state.ErrNilAddress, err) -} - -func TestNewUserAccount_OkParamsShouldWork(t *testing.T) { - t.Parallel() - - acc, err := state.NewUserAccount(make([]byte, 32), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) - assert.Nil(t, err) - assert.False(t, check.IfNil(acc)) + t.Run("nil address", func(t *testing.T) { + t.Parallel() + + acc, err := state.NewUserAccount(nil, getDefaultArgsAccountCreation()) + assert.True(t, check.IfNil(acc)) + assert.Equal(t, state.ErrNilAddress, err) + }) + + t.Run("nil hasher", func(t *testing.T) { + t.Parallel() + + args := getDefaultArgsAccountCreation() + args.Hasher = nil + acc, err := state.NewUserAccount(make([]byte, 32), args) + assert.True(t, check.IfNil(acc)) + assert.Equal(t, state.ErrNilHasher, err) + }) + + t.Run("nil marshaller", func(t *testing.T) { + t.Parallel() + + args := getDefaultArgsAccountCreation() + args.Marshaller = nil + acc, err := state.NewUserAccount(make([]byte, 32), args) + assert.True(t, check.IfNil(acc)) + assert.Equal(t, state.ErrNilMarshalizer, err) + }) + + t.Run("nil enableEpochsHandler", func(t *testing.T) { + t.Parallel() + + args := getDefaultArgsAccountCreation() + args.EnableEpochsHandler = nil + acc, err := state.NewUserAccount(make([]byte, 32), args) + assert.True(t, check.IfNil(acc)) + assert.Equal(t, state.ErrNilEnableEpochsHandler, err) + }) + + t.Run("should work", func(t *testing.T) { + t.Parallel() + + acc, err := state.NewUserAccount(make([]byte, 32), getDefaultArgsAccountCreation()) + assert.Nil(t, err) + assert.False(t, check.IfNil(acc)) + }) } func TestUserAccount_AddToBalanceInsufficientFundsShouldErr(t *testing.T) { t.Parallel() - acc, _ := state.NewUserAccount(make([]byte, 32), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acc := createUserAcc(make([]byte, 32)) value := big.NewInt(-1) err := acc.AddToBalance(value) @@ -40,7 +72,7 @@ func TestUserAccount_AddToBalanceInsufficientFundsShouldErr(t *testing.T) { func TestUserAccount_SubFromBalanceInsufficientFundsShouldErr(t *testing.T) { t.Parallel() - acc, _ := state.NewUserAccount(make([]byte, 32), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acc := createUserAcc(make([]byte, 32)) value := big.NewInt(1) err := acc.SubFromBalance(value) @@ -50,7 +82,7 @@ func TestUserAccount_SubFromBalanceInsufficientFundsShouldErr(t *testing.T) { func TestUserAccount_GetBalance(t *testing.T) { t.Parallel() - acc, _ := state.NewUserAccount(make([]byte, 32), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acc := createUserAcc(make([]byte, 32)) balance := big.NewInt(100) subFromBalance := big.NewInt(20) @@ -63,7 +95,7 @@ func TestUserAccount_GetBalance(t *testing.T) { func TestUserAccount_AddToDeveloperReward(t *testing.T) { t.Parallel() - acc, _ := state.NewUserAccount(make([]byte, 32), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acc := createUserAcc(make([]byte, 32)) reward := big.NewInt(10) acc.AddToDeveloperReward(reward) @@ -73,7 +105,7 @@ func TestUserAccount_AddToDeveloperReward(t *testing.T) { func TestUserAccount_ClaimDeveloperRewardsWrongAddressShouldErr(t *testing.T) { t.Parallel() - acc, _ := state.NewUserAccount(make([]byte, 32), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acc := createUserAcc(make([]byte, 32)) val, err := acc.ClaimDeveloperRewards([]byte("wrong address")) assert.Nil(t, val) assert.Equal(t, state.ErrOperationNotPermitted, err) @@ -82,7 +114,7 @@ func TestUserAccount_ClaimDeveloperRewardsWrongAddressShouldErr(t *testing.T) { func TestUserAccount_ClaimDeveloperRewards(t *testing.T) { t.Parallel() - acc, _ := state.NewUserAccount(make([]byte, 32), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acc, _ := state.NewUserAccount(make([]byte, 32), getDefaultArgsAccountCreation()) reward := big.NewInt(10) acc.AddToDeveloperReward(reward) @@ -95,7 +127,7 @@ func TestUserAccount_ClaimDeveloperRewards(t *testing.T) { func TestUserAccount_ChangeOwnerAddressWrongAddressShouldErr(t *testing.T) { t.Parallel() - acc, _ := state.NewUserAccount(make([]byte, 32), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acc := createUserAcc(make([]byte, 32)) err := acc.ChangeOwnerAddress([]byte("wrong address"), []byte{}) assert.Equal(t, state.ErrOperationNotPermitted, err) } @@ -103,7 +135,7 @@ func TestUserAccount_ChangeOwnerAddressWrongAddressShouldErr(t *testing.T) { func TestUserAccount_ChangeOwnerAddressInvalidAddressShouldErr(t *testing.T) { t.Parallel() - acc, _ := state.NewUserAccount(make([]byte, 32), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acc, _ := state.NewUserAccount(make([]byte, 32), getDefaultArgsAccountCreation()) err := acc.ChangeOwnerAddress(acc.OwnerAddress, []byte("new address")) assert.Equal(t, state.ErrInvalidAddressLength, err) } @@ -112,7 +144,7 @@ func TestUserAccount_ChangeOwnerAddress(t *testing.T) { t.Parallel() newAddress := make([]byte, 32) - acc, _ := state.NewUserAccount(make([]byte, 32), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acc, _ := state.NewUserAccount(make([]byte, 32), getDefaultArgsAccountCreation()) err := acc.ChangeOwnerAddress(acc.OwnerAddress, newAddress) assert.Nil(t, err) @@ -123,7 +155,7 @@ func TestUserAccount_SetOwnerAddress(t *testing.T) { t.Parallel() newAddress := []byte("new address") - acc, _ := state.NewUserAccount(make([]byte, 32), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acc := createUserAcc(make([]byte, 32)) acc.SetOwnerAddress(newAddress) assert.Equal(t, newAddress, acc.GetOwnerAddress()) @@ -132,7 +164,7 @@ func TestUserAccount_SetOwnerAddress(t *testing.T) { func TestUserAccount_SetAndGetNonce(t *testing.T) { t.Parallel() - acc, _ := state.NewUserAccount(make([]byte, 32), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acc := createUserAcc(make([]byte, 32)) nonce := uint64(5) acc.IncreaseNonce(nonce) @@ -142,7 +174,7 @@ func TestUserAccount_SetAndGetNonce(t *testing.T) { func TestUserAccount_SetAndGetCodeHash(t *testing.T) { t.Parallel() - acc, _ := state.NewUserAccount(make([]byte, 32), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acc := createUserAcc(make([]byte, 32)) codeHash := []byte("code hash") acc.SetCodeHash(codeHash) @@ -152,7 +184,7 @@ func TestUserAccount_SetAndGetCodeHash(t *testing.T) { func TestUserAccount_SetAndGetRootHash(t *testing.T) { t.Parallel() - acc, _ := state.NewUserAccount(make([]byte, 32), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + acc := createUserAcc(make([]byte, 32)) rootHash := []byte("root hash") acc.SetRootHash(rootHash) diff --git a/testscommon/enableEpochsHandlerStub.go b/testscommon/enableEpochsHandlerStub.go index f98ff7c2f1e..e3ca16227f4 100644 --- a/testscommon/enableEpochsHandlerStub.go +++ b/testscommon/enableEpochsHandlerStub.go @@ -112,6 +112,7 @@ type EnableEpochsHandlerStub struct { IsRefactorPeersMiniBlocksFlagEnabledField bool IsFixAsyncCallBackArgsListFlagEnabledField bool IsFixOldTokenLiquidityEnabledField bool + IsAutoBalanceDataTriesEnabledField bool } // ResetPenalizedTooMuchGasFlag - @@ -969,6 +970,14 @@ func (stub *EnableEpochsHandlerStub) IsFixOldTokenLiquidityEnabled() bool { return stub.IsFixOldTokenLiquidityEnabledField } +// IsAutoBalanceDataTriesEnabled - +func (stub *EnableEpochsHandlerStub) IsAutoBalanceDataTriesEnabled() bool { + stub.RLock() + defer stub.RUnlock() + + return stub.IsAutoBalanceDataTriesEnabledField +} + // IsInterfaceNil - func (stub *EnableEpochsHandlerStub) IsInterfaceNil() bool { return stub == nil diff --git a/testscommon/state/accountFactoryStub.go b/testscommon/state/accountFactoryStub.go index 589fe682955..b11cad5c7b8 100644 --- a/testscommon/state/accountFactoryStub.go +++ b/testscommon/state/accountFactoryStub.go @@ -3,6 +3,8 @@ package state import ( "github.com/ElrondNetwork/elrond-go-core/hashing" "github.com/ElrondNetwork/elrond-go-core/marshal" + "github.com/ElrondNetwork/elrond-go/testscommon" + "github.com/ElrondNetwork/elrond-go/testscommon/hashingMocks" "github.com/ElrondNetwork/elrond-vm-common" ) @@ -12,8 +14,8 @@ type AccountsFactoryStub struct { } // CreateAccount - -func (afs *AccountsFactoryStub) CreateAccount(address []byte, hasher hashing.Hasher, marshaller marshal.Marshalizer) (vmcommon.AccountHandler, error) { - return afs.CreateAccountCalled(address, hasher, marshaller) +func (afs *AccountsFactoryStub) CreateAccount(address []byte) (vmcommon.AccountHandler, error) { + return afs.CreateAccountCalled(address, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) } // IsInterfaceNil returns true if there is no value under the interface diff --git a/update/genesis/base.go b/update/genesis/base.go index 5b61ffbd961..94e5cd13625 100644 --- a/update/genesis/base.go +++ b/update/genesis/base.go @@ -13,6 +13,7 @@ import ( "github.com/ElrondNetwork/elrond-go-core/data/transaction" "github.com/ElrondNetwork/elrond-go-core/hashing" "github.com/ElrondNetwork/elrond-go-core/marshal" + "github.com/ElrondNetwork/elrond-go/common" "github.com/ElrondNetwork/elrond-go/state" "github.com/ElrondNetwork/elrond-go/update" vmcommon "github.com/ElrondNetwork/elrond-vm-common" @@ -91,12 +92,23 @@ func NewObject(objType Type) (interface{}, error) { } // NewEmptyAccount returns a new account according to the given type -func NewEmptyAccount(accType Type, address []byte, hasher hashing.Hasher, marshaller marshal.Marshalizer) (vmcommon.AccountHandler, error) { +func NewEmptyAccount( + accType Type, + address []byte, + hasher hashing.Hasher, + marshaller marshal.Marshalizer, + enableEpochsHandler common.EnableEpochsHandler, +) (vmcommon.AccountHandler, error) { switch accType { case UserAccount: - return state.NewUserAccount(address, hasher, marshaller) + argsAccCreation := state.ArgsAccountCreation{ + Hasher: hasher, + Marshaller: marshaller, + EnableEpochsHandler: enableEpochsHandler, + } + return state.NewUserAccount(address, argsAccCreation) case ValidatorAccount: - return state.NewPeerAccount(address, hasher, marshaller) + return state.NewPeerAccount(address) case DataTrie: return nil, nil } diff --git a/update/genesis/import.go b/update/genesis/import.go index cfab58d5ef2..737d4b23cb3 100644 --- a/update/genesis/import.go +++ b/update/genesis/import.go @@ -16,6 +16,7 @@ import ( "github.com/ElrondNetwork/elrond-go/common" commonDisabled "github.com/ElrondNetwork/elrond-go/common/disabled" "github.com/ElrondNetwork/elrond-go/config" + "github.com/ElrondNetwork/elrond-go/errors" "github.com/ElrondNetwork/elrond-go/state" "github.com/ElrondNetwork/elrond-go/state/factory" "github.com/ElrondNetwork/elrond-go/state/storagePruningManager/disabled" @@ -36,6 +37,7 @@ type ArgsNewStateImport struct { StorageConfig config.StorageConfig TrieStorageManagers map[string]common.StorageManager HardforkStorer update.HardforkStorer + EnableEpochsHandler common.EnableEpochsHandler } type stateImport struct { @@ -54,6 +56,7 @@ type stateImport struct { shardID uint32 storageConfig config.StorageConfig trieStorageManagers map[string]common.StorageManager + enableEpochsHandler common.EnableEpochsHandler } // NewStateImport creates an importer which reads all the files for a new start @@ -70,6 +73,9 @@ func NewStateImport(args ArgsNewStateImport) (*stateImport, error) { if check.IfNil(args.HardforkStorer) { return nil, update.ErrNilHardforkStorer } + if check.IfNil(args.EnableEpochsHandler) { + return nil, errors.ErrNilEnableEpochsHandler + } st := &stateImport{ genesisHeaders: make(map[uint32]data.HeaderHandler), @@ -85,6 +91,7 @@ func NewStateImport(args ArgsNewStateImport) (*stateImport, error) { storageConfig: args.StorageConfig, shardID: args.ShardID, hardforkStorer: args.HardforkStorer, + enableEpochsHandler: args.EnableEpochsHandler, } return st, nil @@ -265,10 +272,20 @@ func (si *stateImport) importMiniBlocks(identifier string, keys [][]byte) error return nil } -func newAccountCreator(accType Type) (state.AccountFactory, error) { +func newAccountCreator( + accType Type, + hasher hashing.Hasher, + marshaller marshal.Marshalizer, + handler common.EnableEpochsHandler, +) (state.AccountFactory, error) { switch accType { case UserAccount: - return factory.NewAccountCreator(), nil + args := state.ArgsAccountCreation{ + Hasher: hasher, + Marshaller: marshaller, + EnableEpochsHandler: handler, + } + return factory.NewAccountCreator(args) case ValidatorAccount: return factory.NewPeerAccountCreator(), nil } @@ -382,7 +399,7 @@ func (si *stateImport) importDataTrie(identifier string, shID uint32, keys [][]b } func (si *stateImport) getAccountsDB(accType Type, shardID uint32) (state.AccountsDBImporter, common.Trie, error) { - accountFactory, err := newAccountCreator(accType) + accountFactory, err := newAccountCreator(accType, si.hasher, si.marshalizer, si.enableEpochsHandler) if err != nil { return nil, nil, err } @@ -515,7 +532,7 @@ func (si *stateImport) unMarshalAndSaveAccount( accountsDB state.AccountsDBImporter, mainTrie common.Trie, ) error { - account, err := NewEmptyAccount(accType, address, si.hasher, si.marshalizer) + account, err := NewEmptyAccount(accType, address, si.hasher, si.marshalizer, si.enableEpochsHandler) if err != nil { return err } diff --git a/update/genesis/import_test.go b/update/genesis/import_test.go index 7273b2f058d..cbb296f2906 100644 --- a/update/genesis/import_test.go +++ b/update/genesis/import_test.go @@ -66,6 +66,7 @@ func TestNewStateImport(t *testing.T) { Marshalizer: &mock.MarshalizerMock{}, Hasher: &mock.HasherStub{}, TrieStorageManagers: trieStorageManagers, + EnableEpochsHandler: &testscommon.EnableEpochsHandlerStub{}, }, exError: nil, }, @@ -92,6 +93,7 @@ func TestImportAll(t *testing.T) { TrieStorageManagers: trieStorageManagers, ShardID: 0, StorageConfig: config.StorageConfig{}, + EnableEpochsHandler: &testscommon.EnableEpochsHandlerStub{}, } importState, _ := NewStateImport(args) @@ -126,6 +128,7 @@ func TestStateImport_ImportUnFinishedMetaBlocksShouldWork(t *testing.T) { TrieStorageManagers: trieStorageManagers, ShardID: 0, StorageConfig: config.StorageConfig{}, + EnableEpochsHandler: &testscommon.EnableEpochsHandlerStub{}, } importState, _ := NewStateImport(args) diff --git a/vm/mock/blockChainHookStub.go b/vm/mock/blockChainHookStub.go index ce88c1f02d3..bce1170b228 100644 --- a/vm/mock/blockChainHookStub.go +++ b/vm/mock/blockChainHookStub.go @@ -76,7 +76,12 @@ func (b *BlockChainHookStub) GetUserAccount(address []byte) (vmcommon.UserAccoun return b.GetUserAccountCalled(address) } - return state.NewUserAccount(address, &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + argsAccCreation := state.ArgsAccountCreation{ + Hasher: &hashingMocks.HasherMock{}, + Marshaller: &testscommon.MarshalizerMock{}, + EnableEpochsHandler: &testscommon.EnableEpochsHandlerStub{}, + } + return state.NewUserAccount(address, argsAccCreation) } // GetShardOfAddress - diff --git a/vm/systemSmartContracts/eei_test.go b/vm/systemSmartContracts/eei_test.go index d152d8ad752..ab657c4dab8 100644 --- a/vm/systemSmartContracts/eei_test.go +++ b/vm/systemSmartContracts/eei_test.go @@ -99,7 +99,12 @@ func TestVmContext_GetBalance(t *testing.T) { addr := []byte("addr") balance := big.NewInt(10) - account, _ := state.NewUserAccount([]byte("123"), &hashingMocks.HasherMock{}, &testscommon.MarshalizerMock{}) + argsAccCreation := state.ArgsAccountCreation{ + Hasher: &hashingMocks.HasherMock{}, + Marshaller: &testscommon.MarshalizerMock{}, + EnableEpochsHandler: &testscommon.EnableEpochsHandlerStub{}, + } + account, _ := state.NewUserAccount([]byte("123"), argsAccCreation) _ = account.AddToBalance(balance) blockChainHook := &mock.BlockChainHookStub{GetUserAccountCalled: func(address []byte) (a vmcommon.UserAccountHandler, e error) {