Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor AccountsParser #6125

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
91 changes: 43 additions & 48 deletions cmd/sovereignnode/sovereignNodeRunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"io"
"io/ioutil"
"math/big"
"os"
"os/signal"
"path"
Expand Down Expand Up @@ -48,6 +47,7 @@ import (
statusComp "github.com/multiversx/mx-chain-go/factory/status"
"github.com/multiversx/mx-chain-go/factory/statusCore"
"github.com/multiversx/mx-chain-go/genesis"
"github.com/multiversx/mx-chain-go/genesis/data"
"github.com/multiversx/mx-chain-go/genesis/parsing"
"github.com/multiversx/mx-chain-go/health"
"github.com/multiversx/mx-chain-go/node"
Expand Down Expand Up @@ -313,14 +313,8 @@ func (snr *sovereignNodeRunner) executeOneComponentCreationCycle(
return true, err
}

log.Debug("creating args for runType components")
argsSovereignRunTypeComponents, err := snr.CreateArgsRunTypeComponents(managedCoreComponents, managedCryptoComponents)
if err != nil {
return true, err
}

log.Debug("creating runType components")
managedRunTypeComponents, err := snr.CreateManagedRunTypeComponents(*argsSovereignRunTypeComponents)
managedRunTypeComponents, err := snr.CreateManagedRunTypeComponents(managedCoreComponents, managedCryptoComponents)
if err != nil {
return true, err
}
Expand Down Expand Up @@ -1227,33 +1221,6 @@ func (snr *sovereignNodeRunner) CreateManagedProcessComponents(
return nil, err
}

totalSupply, ok := big.NewInt(0).SetString(configs.EconomicsConfig.GlobalSettings.GenesisTotalSupply, 10)
if !ok {
return nil, fmt.Errorf("can not parse total suply from economics.toml, %s is not a valid value",
configs.EconomicsConfig.GlobalSettings.GenesisTotalSupply)
}

mintingSenderAddress := configs.EconomicsConfig.GlobalSettings.GenesisMintingSenderAddress

args := genesis.AccountsParserArgs{
GenesisFilePath: configurationPaths.Genesis,
EntireSupply: totalSupply,
MinterAddress: mintingSenderAddress,
PubkeyConverter: coreComponents.AddressPubKeyConverter(),
KeyGenerator: cryptoComponents.TxSignKeyGen(),
Hasher: coreComponents.Hasher(),
Marshalizer: coreComponents.InternalMarshalizer(),
}

accountsParser, err := parsing.NewAccountsParser(args)
if err != nil {
return nil, err
}
sovereignAccountsParser, err := parsing.NewSovereignAccountsParser(accountsParser)
if err != nil {
return nil, err
}

smartContractParser, err := parsing.NewSmartContractsParser(
configurationPaths.SmartContracts,
coreComponents.AddressPubKeyConverter(),
Expand Down Expand Up @@ -1306,7 +1273,6 @@ func (snr *sovereignNodeRunner) CreateManagedProcessComponents(
RoundConfig: *configs.RoundConfig,
PrefConfigs: *configs.PreferencesConfig,
ImportDBConfig: *configs.ImportDbConfig,
AccountsParser: sovereignAccountsParser,
SmartContractParser: smartContractParser,
GasSchedule: gasScheduleNotifier,
NodesCoordinator: nodesCoordinator,
Expand Down Expand Up @@ -1630,42 +1596,71 @@ func (snr *sovereignNodeRunner) CreateManagedCryptoComponents(
return managedCryptoComponents, nil
}

// CreateArgsRunTypeComponents creates the arguments for sovereign runType components
func (snr *sovereignNodeRunner) CreateArgsRunTypeComponents(coreComp mainFactory.CoreComponentsHandler, cryptoComp mainFactory.CryptoComponentsHandler) (*runType.ArgsSovereignRunTypeComponents, error) {
sovereignCfg := snr.configs.SovereignExtraConfig
// CreateArgsRunTypeComponents - creates the arguments for runType components
func (snr *sovereignNodeRunner) CreateArgsRunTypeComponents(coreComponents mainFactory.CoreComponentsHandler, cryptoComponents mainFactory.CryptoComponentsHandler) (*runType.ArgsRunTypeComponents, error) {
initialAccounts := make([]*data.InitialAccount, 0)
err := core.LoadJsonFile(&initialAccounts, snr.configs.ConfigurationPathsHolder.Genesis)
if err != nil {
return nil, err
}

codec := abi.NewDefaultCodec()
argsDataCodec := dataCodec.ArgsDataCodec{
Serializer: abi.NewSerializer(codec),
var accounts []genesis.InitialAccountHandler
for _, ia := range initialAccounts {
accounts = append(accounts, ia)
}
mariusmihaic marked this conversation as resolved.
Show resolved Hide resolved

dataCodecHandler, err := dataCodec.NewDataCodec(argsDataCodec)
return &runType.ArgsRunTypeComponents{
CoreComponents: coreComponents,
CryptoComponents: cryptoComponents,
Configs: *snr.configs.Configs,
InitialAccounts: accounts,
}, nil
}

// CreateSovereignArgsRunTypeComponents creates the arguments for sovereign runType components
func (snr *sovereignNodeRunner) CreateSovereignArgsRunTypeComponents(coreComponents mainFactory.CoreComponentsHandler, cryptoComponents mainFactory.CryptoComponentsHandler) (*runType.ArgsSovereignRunTypeComponents, error) {
args, err := snr.CreateArgsRunTypeComponents(coreComponents, cryptoComponents)
if err != nil {
return nil, err
}

runTypeComponentsFactory, err := runType.NewRunTypeComponentsFactory(coreComp)
runTypeComponentsFactory, err := runType.NewRunTypeComponentsFactory(*args)
if err != nil {
return nil, fmt.Errorf("NewRunTypeComponentsFactory failed: %w", err)
}

sovHeaderSigVerifier, err := headerCheck.NewSovereignHeaderSigVerifier(cryptoComp.BlockSigner())
codec := abi.NewDefaultCodec()
argsDataCodec := dataCodec.ArgsDataCodec{
Serializer: abi.NewSerializer(codec),
}

dataCodecHandler, err := dataCodec.NewDataCodec(argsDataCodec)
if err != nil {
return nil, err
}

sovHeaderSigVerifier, err := headerCheck.NewSovereignHeaderSigVerifier(cryptoComponents.BlockSigner())
if err != nil {
return nil, err
}

return &runType.ArgsSovereignRunTypeComponents{
RunTypeComponentsFactory: runTypeComponentsFactory,
Config: *sovereignCfg,
Config: *snr.configs.SovereignExtraConfig,
DataCodec: dataCodecHandler,
TopicsChecker: incomingHeader.NewTopicsChecker(),
ExtraVerifier: sovHeaderSigVerifier,
}, nil
}

// CreateManagedRunTypeComponents creates the managed runType components
func (snr *sovereignNodeRunner) CreateManagedRunTypeComponents(args runType.ArgsSovereignRunTypeComponents) (mainFactory.RunTypeComponentsHandler, error) {
sovereignRunTypeComponentsFactory, err := runType.NewSovereignRunTypeComponentsFactory(args)
func (snr *sovereignNodeRunner) CreateManagedRunTypeComponents(coreComponents mainFactory.CoreComponentsHandler, cryptoComponents mainFactory.CryptoComponentsHandler) (mainFactory.RunTypeComponentsHandler, error) {
args, err := snr.CreateSovereignArgsRunTypeComponents(coreComponents, cryptoComponents)
if err != nil {
return nil, err
}

sovereignRunTypeComponentsFactory, err := runType.NewSovereignRunTypeComponentsFactory(*args)
if err != nil {
return nil, fmt.Errorf("NewSovereignRunTypeComponentsFactory failed: %w", err)
}
Expand Down
24 changes: 24 additions & 0 deletions common/runType/runTypeComponentsHelper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package runType

import (
"github.com/multiversx/mx-chain-go/genesis"
"github.com/multiversx/mx-chain-go/genesis/data"

"github.com/multiversx/mx-chain-core-go/core"
)

// ReadInitialAccounts returns the genesis accounts from a file
func ReadInitialAccounts(filePath string) ([]genesis.InitialAccountHandler, error) {
initialAccounts := make([]*data.InitialAccount, 0)
err := core.LoadJsonFile(&initialAccounts, filePath)
if err != nil {
return nil, err
}

var accounts []genesis.InitialAccountHandler
for _, ia := range initialAccounts {
accounts = append(accounts, ia)
}

return accounts, nil
}
32 changes: 32 additions & 0 deletions common/runType/runTypeComponentsHelper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package runType

import (
"math/big"
"testing"

"github.com/stretchr/testify/require"
)

func TestReadInitialAccounts(t *testing.T) {
t.Parallel()

t.Run("empty path should error", func(t *testing.T) {
accounts, err := ReadInitialAccounts("")
require.Nil(t, accounts)
require.Error(t, err)
})
t.Run("should work", func(t *testing.T) {
accounts, err := ReadInitialAccounts("../../genesis/process/testdata/genesisTest1.json")
require.Nil(t, err)
require.NotNil(t, accounts)
require.True(t, len(accounts) > 0)
require.Equal(t, "a00102030405060708090001020304050607080900010203040506070809000a", accounts[0].GetAddress())
require.Equal(t, "b00102030405060708090001020304050607080900010203040506070809000b", accounts[1].GetAddress())
require.Equal(t, "c00102030405060708090001020304050607080900010203040506070809000c", accounts[2].GetAddress())
require.Equal(t, big.NewInt(10000), accounts[2].GetSupply())
require.Equal(t, big.NewInt(0), accounts[2].GetBalanceValue())
require.Equal(t, big.NewInt(0), accounts[2].GetStakingValue())
require.Equal(t, "00000000000000000500f080f48551abf03e12e27e20d9f077abedffdccc0102", accounts[2].GetDelegationHandler().GetAddress())
require.Equal(t, big.NewInt(10000), accounts[2].GetDelegationHandler().GetValue())
})
}
6 changes: 5 additions & 1 deletion errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,7 @@ var ErrInvalidNodeOperationMode = errors.New("invalid node operation mode")

// ErrNilSentSignatureTracker defines the error for setting a nil SentSignatureTracker
var ErrNilSentSignatureTracker = errors.New("nil sent signature tracker")

// ErrNilNodesCoordinatorFactory signals that a nil nodes coordinator factory has been provided
var ErrNilNodesCoordinatorFactory = errors.New("nil nodes coordinator factory provided")

Expand Down Expand Up @@ -799,5 +800,8 @@ var ErrNilVmContainerMetaFactoryCreator = errors.New("nil vm container meta fact
// ErrNilVmContainerShardFactoryCreator signals that a nil vm container shard factory creator has been provided
var ErrNilVmContainerShardFactoryCreator = errors.New("nil vm container shard factory creator")

// ErrNilAccountsCreator signals that a nil accoutns creator has been provided
// ErrNilAccountsCreator signals that a nil accounts creator has been provided
var ErrNilAccountsCreator = errors.New("nil accounts creator")

// ErrNilInitialAccounts signals that a nil initial accounts has been provided
var ErrNilInitialAccounts = errors.New("nil initial accounts")
1 change: 1 addition & 0 deletions factory/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,7 @@ type RunTypeComponentsHolder interface {
ConsensusModel() consensus.ConsensusModel
VmContainerMetaFactoryCreator() factoryVm.VmContainerCreator
VmContainerShardFactoryCreator() factoryVm.VmContainerCreator
AccountsParser() genesis.AccountsParser
AccountsCreator() state.AccountFactory
OutGoingOperationsPoolHandler() sovereignBlock.OutGoingOperationsPool
DataCodecHandler() sovereign.DataCodecHandler
Expand Down
29 changes: 11 additions & 18 deletions factory/processing/processComponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ type ProcessComponentsFactoryArgs struct {
PrefConfigs config.Preferences
ImportDBConfig config.ImportDbConfig
EconomicsConfig config.EconomicsConfig
AccountsParser genesis.AccountsParser
SmartContractParser genesis.InitialSmartContractParser
GasSchedule core.GasScheduleNotifier
NodesCoordinator nodesCoordinator.NodesCoordinator
Expand Down Expand Up @@ -178,7 +177,6 @@ type processComponentsFactory struct {
prefConfigs config.Preferences
importDBConfig config.ImportDbConfig
economicsConfig config.EconomicsConfig
accountsParser genesis.AccountsParser
smartContractParser genesis.InitialSmartContractParser
gasSchedule core.GasScheduleNotifier
nodesCoordinator nodesCoordinator.NodesCoordinator
Expand Down Expand Up @@ -227,7 +225,6 @@ func NewProcessComponentsFactory(args ProcessComponentsFactoryArgs) (*processCom
prefConfigs: args.PrefConfigs,
importDBConfig: args.ImportDBConfig,
economicsConfig: args.EconomicsConfig,
accountsParser: args.AccountsParser,
smartContractParser: args.SmartContractParser,
gasSchedule: args.GasSchedule,
nodesCoordinator: args.NodesCoordinator,
Expand Down Expand Up @@ -636,7 +633,7 @@ func (pcf *processComponentsFactory) Create() (*processComponents, error) {
}

nodesSetupChecker, err := checking.NewNodesSetupChecker(
pcf.accountsParser,
pcf.runTypeComponents.AccountsParser(),
genesisNodePrice,
pcf.coreData.ValidatorPubKeyConverter(),
pcf.crypto.BlockSignKeyGen(),
Expand Down Expand Up @@ -735,7 +732,7 @@ func (pcf *processComponentsFactory) Create() (*processComponents, error) {
hardforkTrigger: hardforkTrigger,
processedMiniBlocksTracker: processedMiniBlocksTracker,
esdtDataStorageForApi: pcf.esdtNftStorage,
accountsParser: pcf.accountsParser,
accountsParser: pcf.runTypeComponents.AccountsParser(),
receiptsRepository: receiptsRepository,
sentSignaturesTracker: sentSignaturesTracker,
}, nil
Expand Down Expand Up @@ -893,19 +890,15 @@ func (pcf *processComponentsFactory) generateGenesisHeadersAndApplyInitialBalanc
}

arg := processGenesis.ArgsGenesisBlockCreator{

GenesisTime: uint64(pcf.coreData.GenesisNodesSetup().GetStartTime()),
StartEpochNum: pcf.bootstrapComponents.EpochBootstrapParams().Epoch(), Data: pcf.data,
Core: pcf.coreData,
Accounts: pcf.state.AccountsAdapter(),
ValidatorAccounts: pcf.state.PeerAccounts(), InitialNodesSetup: pcf.coreData.GenesisNodesSetup(),
Economics: pcf.coreData.EconomicsData(),
ShardCoordinator: pcf.bootstrapComponents.ShardCoordinator(),
AccountsParser: pcf.accountsParser,
SmartContractParser: pcf.smartContractParser,

GasSchedule: pcf.gasSchedule,

Economics: pcf.coreData.EconomicsData(),
ShardCoordinator: pcf.bootstrapComponents.ShardCoordinator(),
SmartContractParser: pcf.smartContractParser,
GasSchedule: pcf.gasSchedule,
TxLogsProcessor: pcf.txLogsProcessor,
VirtualMachineConfig: genesisVmConfig, HardForkConfig: pcf.config.Hardfork,
TrieStorageManagers: pcf.state.TrieStorageManagers(),
Expand Down Expand Up @@ -1161,7 +1154,7 @@ func (pcf *processComponentsFactory) indexGenesisBlocks(
return err
}

miniBlocks, txsPoolPerShard, errGenerate := pcf.accountsParser.GenerateInitialTransactions(pcf.bootstrapComponents.ShardCoordinator(), initialIndexingData)
miniBlocks, txsPoolPerShard, errGenerate := pcf.runTypeComponents.AccountsParser().GenerateInitialTransactions(pcf.bootstrapComponents.ShardCoordinator(), initialIndexingData)
if errGenerate != nil {
return errGenerate
}
Expand All @@ -1173,7 +1166,7 @@ func (pcf *processComponentsFactory) indexGenesisBlocks(
log.Info("indexGenesisBlocks(): indexer.SaveBlock", "hash", genesisBlockHash)

// manually add the genesis minting address as it is not exist in the trie
genesisAddress := pcf.accountsParser.GenesisMintingAddress()
genesisAddress := pcf.runTypeComponents.AccountsParser().GenesisMintingAddress()

alteredAccounts[genesisAddress] = &alteredAccount.AlteredAccount{
Address: genesisAddress,
Expand Down Expand Up @@ -1945,9 +1938,6 @@ func createCache(cacheConfig config.CacheConfig) (storage.Cacher, error) {

func checkProcessComponentsArgs(args ProcessComponentsFactoryArgs) error {
baseErrMessage := "error creating process components"
if check.IfNil(args.AccountsParser) {
return fmt.Errorf("%s: %w", baseErrMessage, errorsMx.ErrNilAccountsParser)
}
if check.IfNil(args.GasSchedule) {
return fmt.Errorf("%s: %w", baseErrMessage, errorsMx.ErrNilGasSchedule)
}
Expand Down Expand Up @@ -2093,6 +2083,9 @@ func checkProcessComponentsArgs(args ProcessComponentsFactoryArgs) error {
if check.IfNil(args.RunTypeComponents.VmContainerShardFactoryCreator()) {
return fmt.Errorf("%s: %w", baseErrMessage, errorsMx.ErrNilVmContainerShardFactoryCreator)
}
if check.IfNil(args.RunTypeComponents.AccountsParser()) {
return fmt.Errorf("%s: %w", baseErrMessage, errorsMx.ErrNilAccountsParser)
}
if check.IfNil(args.RunTypeComponents.AccountsCreator()) {
return fmt.Errorf("%s: %w", baseErrMessage, errorsMx.ErrNilAccountsCreator)
}
Expand Down