From 98392f1037055042d12ce281e4f67b5889ae42c9 Mon Sep 17 00:00:00 2001 From: Iuga Mihai Date: Wed, 21 Feb 2024 13:47:46 +0200 Subject: [PATCH 1/3] genesis epoch --- cmd/node/config/config.toml | 1 + config/config.go | 1 + epochStart/bootstrap/process.go | 1 + epochStart/metachain/epochStartData.go | 2 +- factory/processing/processComponents.go | 1 + genesis/process/argGenesisBlockCreator.go | 1 + genesis/process/genesisBlockCreator.go | 4 ++-- node/chainSimulator/chainSimulator.go | 2 ++ node/chainSimulator/chainSimulator_test.go | 14 +++++++++----- node/chainSimulator/configs/configs.go | 3 +++ node/chainSimulator/process/processor.go | 1 + 11 files changed, 23 insertions(+), 8 deletions(-) diff --git a/cmd/node/config/config.toml b/cmd/node/config/config.toml index 08ed541ed82..f0a1dc708fc 100644 --- a/cmd/node/config/config.toml +++ b/cmd/node/config/config.toml @@ -621,6 +621,7 @@ Type = "json" [EpochStartConfig] + GenesisEpoch = 0 MinRoundsBetweenEpochs = 20 RoundsPerEpoch = 200 # Min and Max ShuffledOutRestartThreshold represents the minimum and maximum duration of an epoch (in percentage) after a node which diff --git a/config/config.go b/config/config.go index f2454a6e52f..472378d49fd 100644 --- a/config/config.go +++ b/config/config.go @@ -95,6 +95,7 @@ type EpochStartConfig struct { MinNumConnectedPeersToStart int MinNumOfPeersToConsiderBlockValid int ExtraDelayForRequestBlockInfoInMilliseconds int + GenesisEpoch uint32 } // BlockSizeThrottleConfig will hold the configuration for adaptive block size throttle diff --git a/epochStart/bootstrap/process.go b/epochStart/bootstrap/process.go index d2c0aa199ae..0055fa8995a 100644 --- a/epochStart/bootstrap/process.go +++ b/epochStart/bootstrap/process.go @@ -237,6 +237,7 @@ func NewEpochStartBootstrap(args ArgsEpochStartBootstrap) (*epochStartBootstrap, nodeProcessingMode: args.NodeProcessingMode, nodeOperationMode: common.NormalOperation, stateStatsHandler: args.StateStatsHandler, + startEpoch: args.GeneralConfig.EpochStartConfig.GenesisEpoch, } if epochStartProvider.prefsConfig.FullArchive { diff --git a/epochStart/metachain/epochStartData.go b/epochStart/metachain/epochStartData.go index 1c6bd30516e..1a67b3a3692 100644 --- a/epochStart/metachain/epochStartData.go +++ b/epochStart/metachain/epochStartData.go @@ -289,7 +289,7 @@ func (e *epochStartData) getShardDataFromEpochStartData( } epochStartIdentifier := core.EpochStartIdentifier(prevEpoch) - if prevEpoch == 0 { + if prevEpoch == e.genesisEpoch { return lastMetaHash, []byte(epochStartIdentifier), nil } diff --git a/factory/processing/processComponents.go b/factory/processing/processComponents.go index 9b0dcf43ee8..9fad572d80a 100644 --- a/factory/processing/processComponents.go +++ b/factory/processing/processComponents.go @@ -888,6 +888,7 @@ func (pcf *processComponentsFactory) generateGenesisHeadersAndApplyInitialBalanc GenesisNodePrice: genesisNodePrice, GenesisString: pcf.config.GeneralSettings.GenesisString, TxExecutionOrderHandler: pcf.txExecutionOrderHandler, + GenesisEpoch: pcf.config.EpochStartConfig.GenesisEpoch, } gbc, err := processGenesis.NewGenesisBlockCreator(arg) diff --git a/genesis/process/argGenesisBlockCreator.go b/genesis/process/argGenesisBlockCreator.go index e4374b7f6f0..db18b8df61b 100644 --- a/genesis/process/argGenesisBlockCreator.go +++ b/genesis/process/argGenesisBlockCreator.go @@ -45,6 +45,7 @@ type dataComponentsHandler interface { type ArgsGenesisBlockCreator struct { GenesisTime uint64 StartEpochNum uint32 + GenesisEpoch uint32 Data dataComponentsHandler Core coreComponentsHandler Accounts state.AccountsAdapter diff --git a/genesis/process/genesisBlockCreator.go b/genesis/process/genesisBlockCreator.go index 2e9b14d7db3..ba01b319301 100644 --- a/genesis/process/genesisBlockCreator.go +++ b/genesis/process/genesisBlockCreator.go @@ -82,7 +82,7 @@ func getGenesisBlocksRoundNonceEpoch(arg ArgsGenesisBlockCreator) (uint64, uint6 if arg.HardForkConfig.AfterHardFork { return arg.HardForkConfig.StartRound, arg.HardForkConfig.StartNonce, arg.HardForkConfig.StartEpoch } - return 0, 0, 0 + return 0, 0, arg.GenesisEpoch } func (gbc *genesisBlockCreator) createHardForkImportHandler() error { @@ -212,7 +212,7 @@ func checkArgumentsForBlockCreator(arg ArgsGenesisBlockCreator) error { } func mustDoGenesisProcess(arg ArgsGenesisBlockCreator) bool { - genesisEpoch := uint32(0) + genesisEpoch := arg.GenesisEpoch if arg.HardForkConfig.AfterHardFork { genesisEpoch = arg.HardForkConfig.StartEpoch } diff --git a/node/chainSimulator/chainSimulator.go b/node/chainSimulator/chainSimulator.go index dcd09ce4b65..42d6299085d 100644 --- a/node/chainSimulator/chainSimulator.go +++ b/node/chainSimulator/chainSimulator.go @@ -29,6 +29,7 @@ type ArgsChainSimulator struct { MetaChainMinNodes uint32 GenesisTimestamp int64 InitialRound int64 + InitialEpoch uint32 RoundDurationInMillis uint64 RoundsPerEpoch core.OptionalUint64 ApiInterface components.APIConfigurator @@ -76,6 +77,7 @@ func (s *simulator) createChainHandlers(args ArgsChainSimulator) error { MinNodesPerShard: args.MinNodesPerShard, MetaChainMinNodes: args.MetaChainMinNodes, RoundsPerEpoch: args.RoundsPerEpoch, + InitialEpoch: args.InitialEpoch, }) if err != nil { return err diff --git a/node/chainSimulator/chainSimulator_test.go b/node/chainSimulator/chainSimulator_test.go index 17eebfc81d7..23edab3f9c4 100644 --- a/node/chainSimulator/chainSimulator_test.go +++ b/node/chainSimulator/chainSimulator_test.go @@ -57,11 +57,15 @@ func TestChainSimulator_GenerateBlocksShouldWork(t *testing.T) { NumOfShards: 3, GenesisTimestamp: startTime, RoundDurationInMillis: roundDurationInMillis, - RoundsPerEpoch: core.OptionalUint64{}, - ApiInterface: api.NewNoApiInterface(), - MinNodesPerShard: 1, - MetaChainMinNodes: 1, - InitialRound: 200000000, + RoundsPerEpoch: core.OptionalUint64{ + HasValue: true, + Value: 20, + }, + ApiInterface: api.NewNoApiInterface(), + MinNodesPerShard: 1, + MetaChainMinNodes: 1, + InitialRound: 200000000, + InitialEpoch: 100, }) require.Nil(t, err) require.NotNil(t, chainSimulator) diff --git a/node/chainSimulator/configs/configs.go b/node/chainSimulator/configs/configs.go index 63aa3adc48b..6c94475af36 100644 --- a/node/chainSimulator/configs/configs.go +++ b/node/chainSimulator/configs/configs.go @@ -47,6 +47,7 @@ type ArgsChainSimulatorConfigs struct { TempDir string MinNodesPerShard uint32 MetaChainMinNodes uint32 + InitialEpoch uint32 RoundsPerEpoch core.OptionalUint64 } @@ -117,6 +118,8 @@ func CreateChainSimulatorConfigs(args ArgsChainSimulatorConfigs) (*ArgsConfigsSi configs.GeneralConfig.DbLookupExtensions.Enabled = true configs.GeneralConfig.EpochStartConfig.ExtraDelayForRequestBlockInfoInMilliseconds = 1 + configs.GeneralConfig.EpochStartConfig.GenesisEpoch = args.InitialEpoch + configs.EpochConfig.EnableEpochs.StakingV2EnableEpoch = args.InitialEpoch + 1 if args.RoundsPerEpoch.HasValue { configs.GeneralConfig.EpochStartConfig.RoundsPerEpoch = int64(args.RoundsPerEpoch.Value) diff --git a/node/chainSimulator/process/processor.go b/node/chainSimulator/process/processor.go index bca5b6ac2a1..ccbedcee2cb 100644 --- a/node/chainSimulator/process/processor.go +++ b/node/chainSimulator/process/processor.go @@ -149,6 +149,7 @@ func (creator *blocksCreator) getPreviousHeaderData() (nonce, round uint64, prev prevHash = creator.nodeHandler.GetChainHandler().GetGenesisHeaderHash() prevRandSeed = creator.nodeHandler.GetChainHandler().GetGenesisHeader().GetRandSeed() round = uint64(creator.nodeHandler.GetCoreComponents().RoundHandler().Index()) - 1 + epoch = creator.nodeHandler.GetChainHandler().GetGenesisHeader().GetEpoch() return } From 0f3a9caac7049c3c93c980a74b65363cb7150020 Mon Sep 17 00:00:00 2001 From: Iuga Mihai Date: Wed, 21 Feb 2024 17:25:53 +0200 Subject: [PATCH 2/3] genesis nonce --- genesis/process/genesisBlockCreator.go | 15 ++++++++++++++- node/chainSimulator/chainSimulator.go | 5 +++++ node/chainSimulator/chainSimulator_test.go | 3 ++- .../components/manualRoundHandler.go | 4 +++- node/chainSimulator/process/processor.go | 1 + 5 files changed, 25 insertions(+), 3 deletions(-) diff --git a/genesis/process/genesisBlockCreator.go b/genesis/process/genesisBlockCreator.go index ba01b319301..143dd39ef15 100644 --- a/genesis/process/genesisBlockCreator.go +++ b/genesis/process/genesisBlockCreator.go @@ -38,6 +38,9 @@ import ( const accountStartNonce = uint64(0) +var genesisNonce uint64 +var genesisRound uint64 + type genesisBlockCreator struct { arg ArgsGenesisBlockCreator initialIndexingData map[uint32]*genesis.IndexingData @@ -82,7 +85,17 @@ func getGenesisBlocksRoundNonceEpoch(arg ArgsGenesisBlockCreator) (uint64, uint6 if arg.HardForkConfig.AfterHardFork { return arg.HardForkConfig.StartRound, arg.HardForkConfig.StartNonce, arg.HardForkConfig.StartEpoch } - return 0, 0, arg.GenesisEpoch + return genesisRound, genesisNonce, arg.GenesisEpoch +} + +// SetGenesisRound will set the genesis round +func SetGenesisRound(round uint64) { + genesisRound = round +} + +// SetGenesisNonce will set the genesis nonce +func SetGenesisNonce(nonce uint64) { + genesisNonce = nonce } func (gbc *genesisBlockCreator) createHardForkImportHandler() error { diff --git a/node/chainSimulator/chainSimulator.go b/node/chainSimulator/chainSimulator.go index 42d6299085d..2da45d6c8e0 100644 --- a/node/chainSimulator/chainSimulator.go +++ b/node/chainSimulator/chainSimulator.go @@ -10,6 +10,7 @@ import ( "github.com/multiversx/mx-chain-core-go/core/sharding" "github.com/multiversx/mx-chain-core-go/data/endProcess" crypto "github.com/multiversx/mx-chain-crypto-go" + processGenesis "github.com/multiversx/mx-chain-go/genesis/process" "github.com/multiversx/mx-chain-go/node/chainSimulator/components" "github.com/multiversx/mx-chain-go/node/chainSimulator/configs" "github.com/multiversx/mx-chain-go/node/chainSimulator/dtos" @@ -30,6 +31,7 @@ type ArgsChainSimulator struct { GenesisTimestamp int64 InitialRound int64 InitialEpoch uint32 + InitialNonce uint64 RoundDurationInMillis uint64 RoundsPerEpoch core.OptionalUint64 ApiInterface components.APIConfigurator @@ -59,6 +61,9 @@ func NewChainSimulator(args ArgsChainSimulator) (*simulator, error) { mutex: sync.RWMutex{}, } + processGenesis.SetGenesisNonce(args.InitialNonce) + processGenesis.SetGenesisRound(uint64(args.InitialRound)) + err := instance.createChainHandlers(args) if err != nil { return nil, err diff --git a/node/chainSimulator/chainSimulator_test.go b/node/chainSimulator/chainSimulator_test.go index 23edab3f9c4..a986221c17c 100644 --- a/node/chainSimulator/chainSimulator_test.go +++ b/node/chainSimulator/chainSimulator_test.go @@ -66,13 +66,14 @@ func TestChainSimulator_GenerateBlocksShouldWork(t *testing.T) { MetaChainMinNodes: 1, InitialRound: 200000000, InitialEpoch: 100, + InitialNonce: 100, }) require.Nil(t, err) require.NotNil(t, chainSimulator) time.Sleep(time.Second) - err = chainSimulator.GenerateBlocks(30) + err = chainSimulator.GenerateBlocks(50) require.Nil(t, err) err = chainSimulator.Close() diff --git a/node/chainSimulator/components/manualRoundHandler.go b/node/chainSimulator/components/manualRoundHandler.go index 3639bf23752..479cf63a1f5 100644 --- a/node/chainSimulator/components/manualRoundHandler.go +++ b/node/chainSimulator/components/manualRoundHandler.go @@ -9,6 +9,7 @@ type manualRoundHandler struct { index int64 genesisTimeStamp int64 roundDuration time.Duration + initialRound int64 } // NewManualRoundHandler returns a manual round handler instance @@ -17,6 +18,7 @@ func NewManualRoundHandler(genesisTimeStamp int64, roundDuration time.Duration, genesisTimeStamp: genesisTimeStamp, roundDuration: roundDuration, index: initialRound, + initialRound: initialRound, } } @@ -44,7 +46,7 @@ func (handler *manualRoundHandler) TimeStamp() time.Time { rounds := atomic.LoadInt64(&handler.index) timeFromGenesis := handler.roundDuration * time.Duration(rounds) timestamp := time.Unix(handler.genesisTimeStamp, 0).Add(timeFromGenesis) - + timestamp = time.Unix(timestamp.Unix()-int64(handler.roundDuration.Seconds())*handler.initialRound, 0) return timestamp } diff --git a/node/chainSimulator/process/processor.go b/node/chainSimulator/process/processor.go index ccbedcee2cb..49029c63083 100644 --- a/node/chainSimulator/process/processor.go +++ b/node/chainSimulator/process/processor.go @@ -150,6 +150,7 @@ func (creator *blocksCreator) getPreviousHeaderData() (nonce, round uint64, prev prevRandSeed = creator.nodeHandler.GetChainHandler().GetGenesisHeader().GetRandSeed() round = uint64(creator.nodeHandler.GetCoreComponents().RoundHandler().Index()) - 1 epoch = creator.nodeHandler.GetChainHandler().GetGenesisHeader().GetEpoch() + nonce = creator.nodeHandler.GetChainHandler().GetGenesisHeader().GetNonce() return } From aa16de3cd5e53f54a0de618581941376ace38570 Mon Sep 17 00:00:00 2001 From: Iuga Mihai Date: Wed, 21 Feb 2024 17:41:37 +0200 Subject: [PATCH 3/3] fixes after review --- factory/processing/processComponents.go | 10 ++++++++++ genesis/process/argGenesisBlockCreator.go | 2 ++ genesis/process/genesisBlockCreator.go | 15 +-------------- node/chainSimulator/chainSimulator.go | 5 +---- .../components/processComponents.go | 5 +++++ .../components/testOnlyProcessingNode.go | 3 +++ 6 files changed, 22 insertions(+), 18 deletions(-) diff --git a/factory/processing/processComponents.go b/factory/processing/processComponents.go index 9fad572d80a..8f116c4b9b6 100644 --- a/factory/processing/processComponents.go +++ b/factory/processing/processComponents.go @@ -162,6 +162,9 @@ type ProcessComponentsFactoryArgs struct { StatusComponents factory.StatusComponentsHolder StatusCoreComponents factory.StatusCoreComponentsHolder TxExecutionOrderHandler common.TxExecutionOrderHandler + + GenesisNonce uint64 + GenesisRound uint64 } type processComponentsFactory struct { @@ -196,6 +199,9 @@ type processComponentsFactory struct { statusComponents factory.StatusComponentsHolder statusCoreComponents factory.StatusCoreComponentsHolder txExecutionOrderHandler common.TxExecutionOrderHandler + + genesisNonce uint64 + genesisRound uint64 } // NewProcessComponentsFactory will return a new instance of processComponentsFactory @@ -232,6 +238,8 @@ func NewProcessComponentsFactory(args ProcessComponentsFactoryArgs) (*processCom statusCoreComponents: args.StatusCoreComponents, flagsConfig: args.FlagsConfig, txExecutionOrderHandler: args.TxExecutionOrderHandler, + genesisNonce: args.GenesisNonce, + genesisRound: args.GenesisRound, }, nil } @@ -889,6 +897,8 @@ func (pcf *processComponentsFactory) generateGenesisHeadersAndApplyInitialBalanc GenesisString: pcf.config.GeneralSettings.GenesisString, TxExecutionOrderHandler: pcf.txExecutionOrderHandler, GenesisEpoch: pcf.config.EpochStartConfig.GenesisEpoch, + GenesisNonce: pcf.genesisNonce, + GenesisRound: pcf.genesisRound, } gbc, err := processGenesis.NewGenesisBlockCreator(arg) diff --git a/genesis/process/argGenesisBlockCreator.go b/genesis/process/argGenesisBlockCreator.go index db18b8df61b..05b8e130a20 100644 --- a/genesis/process/argGenesisBlockCreator.go +++ b/genesis/process/argGenesisBlockCreator.go @@ -44,6 +44,8 @@ type dataComponentsHandler interface { // ArgsGenesisBlockCreator holds the arguments which are needed to create a genesis block type ArgsGenesisBlockCreator struct { GenesisTime uint64 + GenesisNonce uint64 + GenesisRound uint64 StartEpochNum uint32 GenesisEpoch uint32 Data dataComponentsHandler diff --git a/genesis/process/genesisBlockCreator.go b/genesis/process/genesisBlockCreator.go index 143dd39ef15..c4ec16e5871 100644 --- a/genesis/process/genesisBlockCreator.go +++ b/genesis/process/genesisBlockCreator.go @@ -38,9 +38,6 @@ import ( const accountStartNonce = uint64(0) -var genesisNonce uint64 -var genesisRound uint64 - type genesisBlockCreator struct { arg ArgsGenesisBlockCreator initialIndexingData map[uint32]*genesis.IndexingData @@ -85,17 +82,7 @@ func getGenesisBlocksRoundNonceEpoch(arg ArgsGenesisBlockCreator) (uint64, uint6 if arg.HardForkConfig.AfterHardFork { return arg.HardForkConfig.StartRound, arg.HardForkConfig.StartNonce, arg.HardForkConfig.StartEpoch } - return genesisRound, genesisNonce, arg.GenesisEpoch -} - -// SetGenesisRound will set the genesis round -func SetGenesisRound(round uint64) { - genesisRound = round -} - -// SetGenesisNonce will set the genesis nonce -func SetGenesisNonce(nonce uint64) { - genesisNonce = nonce + return arg.GenesisRound, arg.GenesisNonce, arg.GenesisEpoch } func (gbc *genesisBlockCreator) createHardForkImportHandler() error { diff --git a/node/chainSimulator/chainSimulator.go b/node/chainSimulator/chainSimulator.go index 2da45d6c8e0..663a503423a 100644 --- a/node/chainSimulator/chainSimulator.go +++ b/node/chainSimulator/chainSimulator.go @@ -10,7 +10,6 @@ import ( "github.com/multiversx/mx-chain-core-go/core/sharding" "github.com/multiversx/mx-chain-core-go/data/endProcess" crypto "github.com/multiversx/mx-chain-crypto-go" - processGenesis "github.com/multiversx/mx-chain-go/genesis/process" "github.com/multiversx/mx-chain-go/node/chainSimulator/components" "github.com/multiversx/mx-chain-go/node/chainSimulator/configs" "github.com/multiversx/mx-chain-go/node/chainSimulator/dtos" @@ -61,9 +60,6 @@ func NewChainSimulator(args ArgsChainSimulator) (*simulator, error) { mutex: sync.RWMutex{}, } - processGenesis.SetGenesisNonce(args.InitialNonce) - processGenesis.SetGenesisRound(uint64(args.InitialRound)) - err := instance.createChainHandlers(args) if err != nil { return nil, err @@ -140,6 +136,7 @@ func (s *simulator) createTestNode( APIInterface: args.ApiInterface, BypassTxSignatureCheck: args.BypassTxSignatureCheck, InitialRound: args.InitialRound, + InitialNonce: args.InitialNonce, MinNodesPerShard: args.MinNodesPerShard, MinNodesMeta: args.MetaChainMinNodes, } diff --git a/node/chainSimulator/components/processComponents.go b/node/chainSimulator/components/processComponents.go index 27b1e358614..1f466c5befe 100644 --- a/node/chainSimulator/components/processComponents.go +++ b/node/chainSimulator/components/processComponents.go @@ -49,6 +49,9 @@ type ArgsProcessComponentsHolder struct { Config config.Config EconomicsConfig config.EconomicsConfig SystemSCConfig config.SystemSmartContractsConfig + + GenesisNonce uint64 + GenesisRound uint64 } type processComponentsHolder struct { @@ -203,6 +206,8 @@ func CreateProcessComponents(args ArgsProcessComponentsHolder) (factory.ProcessC HistoryRepo: historyRepository, FlagsConfig: args.FlagsConfig, TxExecutionOrderHandler: txExecutionOrderHandler, + GenesisNonce: args.GenesisNonce, + GenesisRound: args.GenesisRound, } processComponentsFactory, err := processComp.NewProcessComponentsFactory(processArgs) if err != nil { diff --git a/node/chainSimulator/components/testOnlyProcessingNode.go b/node/chainSimulator/components/testOnlyProcessingNode.go index 8fe8fdaf6b6..43abc6e8076 100644 --- a/node/chainSimulator/components/testOnlyProcessingNode.go +++ b/node/chainSimulator/components/testOnlyProcessingNode.go @@ -39,6 +39,7 @@ type ArgsTestOnlyProcessingNode struct { SyncedBroadcastNetwork SyncedBroadcastNetworkHandler InitialRound int64 + InitialNonce uint64 GasScheduleFilename string NumShards uint32 ShardIDStr string @@ -205,6 +206,8 @@ func NewTestOnlyProcessingNode(args ArgsTestOnlyProcessingNode) (*testOnlyProces ConfigurationPathsHolder: *args.Configs.ConfigurationPathsHolder, NodesCoordinator: instance.NodesCoordinator, DataComponents: instance.DataComponentsHolder, + GenesisNonce: args.InitialNonce, + GenesisRound: uint64(args.InitialRound), }) if err != nil { return nil, err