Skip to content

Commit

Permalink
Merge pull request #5988 from multiversx/fix-genesis-flags
Browse files Browse the repository at this point in the history
Fix genesis flags
  • Loading branch information
iulianpascalau authored Mar 5, 2024
2 parents 4a0b699 + 6207335 commit 64154cd
Show file tree
Hide file tree
Showing 31 changed files with 231 additions and 223 deletions.
6 changes: 4 additions & 2 deletions factory/processing/processComponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ func NewProcessComponentsFactory(args ProcessComponentsFactoryArgs) (*processCom
txExecutionOrderHandler: args.TxExecutionOrderHandler,
genesisNonce: args.GenesisNonce,
genesisRound: args.GenesisRound,
roundConfig: args.RoundConfig,
}, nil
}

Expand Down Expand Up @@ -889,8 +890,9 @@ func (pcf *processComponentsFactory) generateGenesisHeadersAndApplyInitialBalanc
HardForkConfig: pcf.config.Hardfork,
TrieStorageManagers: pcf.state.TrieStorageManagers(),
SystemSCConfig: *pcf.systemSCConfig,
RoundConfig: &pcf.roundConfig,
EpochConfig: &pcf.epochConfig,
RoundConfig: pcf.roundConfig,
EpochConfig: pcf.epochConfig,
HeaderVersionConfigs: pcf.config.Versions,
BlockSignKeyGen: pcf.crypto.BlockSignKeyGen(),
HistoryRepository: pcf.historyRepo,
GenesisNodePrice: genesisNodePrice,
Expand Down
1 change: 1 addition & 0 deletions factory/processing/processComponents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func createMockProcessComponentsFactoryArgs() processComp.ProcessComponentsFacto
args := processComp.ProcessComponentsFactoryArgs{
Config: testscommon.GetGeneralConfig(),
EpochConfig: config.EpochConfig{},
RoundConfig: testscommon.GetDefaultRoundsConfig(),
PrefConfigs: config.Preferences{},
ImportDBConfig: config.ImportDbConfig{},
FlagsConfig: config.ContextFlagsConfig{
Expand Down
8 changes: 7 additions & 1 deletion genesis/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ type InitialSmartContractHandler interface {
}

// InitialSmartContractParser contains the parsed genesis initial smart contracts
//json file and has some functionality regarding processed data
// json file and has some functionality regarding processed data
type InitialSmartContractParser interface {
InitialSmartContractsSplitOnOwnersShards(shardCoordinator sharding.Coordinator) (map[uint32][]InitialSmartContractHandler, error)
GetDeployedSCAddresses(scType string) (map[string]struct{}, error)
Expand Down Expand Up @@ -115,3 +115,9 @@ type DeployProcessor interface {
Deploy(sc InitialSmartContractHandler) ([][]byte, error)
IsInterfaceNil() bool
}

// VersionedHeaderFactory creates versioned headers
type VersionedHeaderFactory interface {
Create(epoch uint32) data.HeaderHandler
IsInterfaceNil() bool
}
9 changes: 6 additions & 3 deletions genesis/process/argGenesisBlockCreator.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,18 @@ type ArgsGenesisBlockCreator struct {
HardForkConfig config.HardforkConfig
TrieStorageManagers map[string]common.StorageManager
SystemSCConfig config.SystemSmartContractsConfig
RoundConfig *config.RoundConfig
EpochConfig *config.EpochConfig
RoundConfig config.RoundConfig
EpochConfig config.EpochConfig
HeaderVersionConfigs config.VersionsConfig
WorkingDir string
BlockSignKeyGen crypto.KeyGenerator
HistoryRepository dblookupext.HistoryRepository
TxExecutionOrderHandler common.TxExecutionOrderHandler

GenesisNodePrice *big.Int
GenesisString string

// created components
importHandler update.ImportHandler
importHandler update.ImportHandler
versionedHeaderFactory genesis.VersionedHeaderFactory
}
37 changes: 28 additions & 9 deletions genesis/process/genesisBlockCreator.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/multiversx/mx-chain-go/config"
"github.com/multiversx/mx-chain-go/dataRetriever"
"github.com/multiversx/mx-chain-go/dataRetriever/blockchain"
factoryBlock "github.com/multiversx/mx-chain-go/factory/block"
"github.com/multiversx/mx-chain-go/genesis"
"github.com/multiversx/mx-chain-go/genesis/process/disabled"
"github.com/multiversx/mx-chain-go/genesis/process/intermediate"
Expand Down Expand Up @@ -195,12 +196,6 @@ func checkArgumentsForBlockCreator(arg ArgsGenesisBlockCreator) error {
if arg.TrieStorageManagers == nil {
return genesis.ErrNilTrieStorageManager
}
if arg.EpochConfig == nil {
return genesis.ErrNilEpochConfig
}
if arg.RoundConfig == nil {
return genesis.ErrNilRoundConfig
}
if check.IfNil(arg.HistoryRepository) {
return process.ErrNilHistoryRepository
}
Expand All @@ -225,7 +220,7 @@ func mustDoGenesisProcess(arg ArgsGenesisBlockCreator) bool {
}

func (gbc *genesisBlockCreator) createEmptyGenesisBlocks() (map[uint32]data.HeaderHandler, error) {
err := gbc.computeDNSAddresses(createGenesisConfig())
err := gbc.computeDNSAddresses(createGenesisConfig(gbc.arg.EpochConfig.EnableEpochs))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -486,12 +481,17 @@ func (gbc *genesisBlockCreator) getNewArgForShard(shardID uint32) (ArgsGenesisBl
var err error

isCurrentShard := shardID == gbc.arg.ShardCoordinator.SelfId()
newArgument := gbc.arg // copy the arguments
newArgument.versionedHeaderFactory, err = gbc.createVersionedHeaderFactory()
if err != nil {
return ArgsGenesisBlockCreator{}, fmt.Errorf("'%w' while generating a VersionedHeaderFactory instance for shard %d",
err, shardID)

Check warning on line 488 in genesis/process/genesisBlockCreator.go

View check run for this annotation

Codecov / codecov/patch

genesis/process/genesisBlockCreator.go#L487-L488

Added lines #L487 - L488 were not covered by tests
}

if isCurrentShard {
newArgument := gbc.arg // copy the arguments
newArgument.Data = newArgument.Data.Clone().(dataComponentsHandler)
return newArgument, nil
}
newArgument := gbc.arg // copy the arguments

argsAccCreator := factoryState.ArgsAccountCreator{
Hasher: newArgument.Core.Hasher(),
Expand Down Expand Up @@ -530,6 +530,25 @@ func (gbc *genesisBlockCreator) getNewArgForShard(shardID uint32) (ArgsGenesisBl
return newArgument, err
}

func (gbc *genesisBlockCreator) createVersionedHeaderFactory() (genesis.VersionedHeaderFactory, error) {
cacheConfig := factory.GetCacherFromConfig(gbc.arg.HeaderVersionConfigs.Cache)
cache, err := storageunit.NewCache(cacheConfig)
if err != nil {
return nil, err

Check warning on line 537 in genesis/process/genesisBlockCreator.go

View check run for this annotation

Codecov / codecov/patch

genesis/process/genesisBlockCreator.go#L537

Added line #L537 was not covered by tests
}

headerVersionHandler, err := factoryBlock.NewHeaderVersionHandler(
gbc.arg.HeaderVersionConfigs.VersionsByEpochs,
gbc.arg.HeaderVersionConfigs.DefaultVersion,
cache,
)
if err != nil {
return nil, err

Check warning on line 546 in genesis/process/genesisBlockCreator.go

View check run for this annotation

Codecov / codecov/patch

genesis/process/genesisBlockCreator.go#L546

Added line #L546 was not covered by tests
}

return factoryBlock.NewShardHeaderFactory(headerVersionHandler)
}

func (gbc *genesisBlockCreator) saveGenesisBlock(header data.HeaderHandler) error {
blockBuff, err := gbc.arg.Core.InternalMarshalizer().Marshal(header)
if err != nil {
Expand Down
35 changes: 13 additions & 22 deletions genesis/process/genesisBlockCreator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"testing"

"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/data"
"github.com/multiversx/mx-chain-core-go/data/block"
"github.com/multiversx/mx-chain-go/common"
"github.com/multiversx/mx-chain-go/config"
"github.com/multiversx/mx-chain-go/dataRetriever"
Expand Down Expand Up @@ -165,23 +167,22 @@ func createMockArgument(
TrieStorageManagers: trieStorageManagers,
BlockSignKeyGen: &mock.KeyGenMock{},
GenesisNodePrice: nodePrice,
EpochConfig: &config.EpochConfig{
EpochConfig: config.EpochConfig{
EnableEpochs: config.EnableEpochs{
BuiltInFunctionsEnableEpoch: 0,
SCDeployEnableEpoch: 0,
RelayedTransactionsEnableEpoch: 0,
PenalizedTooMuchGasEnableEpoch: 0,
},
},
RoundConfig: &config.RoundConfig{
RoundActivations: map[string]config.ActivationRoundByName{
"DisableAsyncCallV1": {
Round: "18446744073709551615",
},
SCDeployEnableEpoch: unreachableEpoch,
CleanUpInformativeSCRsEnableEpoch: unreachableEpoch,
SCProcessorV2EnableEpoch: unreachableEpoch,
},
},
RoundConfig: testscommon.GetDefaultRoundsConfig(),
HeaderVersionConfigs: testscommon.GetDefaultHeaderVersionConfig(),
HistoryRepository: &dblookupext.HistoryRepositoryStub{},
TxExecutionOrderHandler: &commonMocks.TxExecutionOrderHandlerStub{},
versionedHeaderFactory: &testscommon.VersionedHeaderFactoryStub{
CreateCalled: func(epoch uint32) data.HeaderHandler {
return &block.Header{}
},
},
}

arg.ShardCoordinator = &mock.ShardCoordinatorMock{
Expand Down Expand Up @@ -427,16 +428,6 @@ func TestNewGenesisBlockCreator(t *testing.T) {
require.True(t, errors.Is(err, genesis.ErrNilTrieStorageManager))
require.Nil(t, gbc)
})
t.Run("nil EpochConfig should error", func(t *testing.T) {
t.Parallel()

arg := createMockArgument(t, "testdata/genesisTest1.json", &mock.InitialNodesHandlerStub{}, big.NewInt(22000))
arg.EpochConfig = nil

gbc, err := NewGenesisBlockCreator(arg)
require.True(t, errors.Is(err, genesis.ErrNilEpochConfig))
require.Nil(t, gbc)
})
t.Run("invalid GenesisNodePrice should error", func(t *testing.T) {
t.Parallel()

Expand Down
13 changes: 7 additions & 6 deletions genesis/process/metaGenesisBlockCreator.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ import (
"github.com/multiversx/mx-chain-vm-common-go/parsers"
)

const unreachableEpoch = ^uint32(0)
const unreachableRound = ^uint64(0)

// CreateMetaGenesisBlock will create a metachain genesis block
func CreateMetaGenesisBlock(
arg ArgsGenesisBlockCreator,
Expand All @@ -70,7 +67,11 @@ func CreateMetaGenesisBlock(
DeployInitialScTxs: make([]data.TransactionHandler, 0),
}

processors, err := createProcessorsForMetaGenesisBlock(arg, createGenesisConfig(), createGenesisRoundConfig())
processors, err := createProcessorsForMetaGenesisBlock(
arg,
createGenesisConfig(arg.EpochConfig.EnableEpochs),
createGenesisRoundConfig(arg.RoundConfig),
)
if err != nil {
return nil, nil, nil, err
}
Expand Down Expand Up @@ -295,7 +296,7 @@ func saveGenesisMetaToStorage(
return nil
}

func createProcessorsForMetaGenesisBlock(arg ArgsGenesisBlockCreator, enableEpochsConfig config.EnableEpochs, roundConfig *config.RoundConfig) (*genesisProcessors, error) {
func createProcessorsForMetaGenesisBlock(arg ArgsGenesisBlockCreator, enableEpochsConfig config.EnableEpochs, roundConfig config.RoundConfig) (*genesisProcessors, error) {
epochNotifier := forking.NewGenericEpochNotifier()
temporaryMetaHeader := &block.MetaBlock{
Epoch: arg.StartEpochNum,
Expand All @@ -308,7 +309,7 @@ func createProcessorsForMetaGenesisBlock(arg ArgsGenesisBlockCreator, enableEpoc
epochNotifier.CheckEpoch(temporaryMetaHeader)

roundNotifier := forking.NewGenericRoundNotifier()
enableRoundsHandler, err := enablers.NewEnableRoundsHandler(*roundConfig, roundNotifier)
enableRoundsHandler, err := enablers.NewEnableRoundsHandler(roundConfig, roundNotifier)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 64154cd

Please sign in to comment.