diff --git a/chain/params.go b/chain/params.go index 33fabc4bfa..d16317fd98 100644 --- a/chain/params.go +++ b/chain/params.go @@ -89,20 +89,24 @@ const ( ) // Forks is map which contains all forks and their starting blocks from genesis -type Forks map[string]*Fork +type Forks map[string]Fork // IsActive returns true if fork defined by name exists and defined for the block func (f *Forks) IsActive(name string, block uint64) bool { - ff := (*f)[name] + ff, exists := (*f)[name] - return ff != nil && ff.Active(block) + return exists && ff.Active(block) } // SetFork adds/updates fork defined by name -func (f *Forks) SetFork(name string, value *Fork) { +func (f *Forks) SetFork(name string, value Fork) { (*f)[name] = value } +func (f *Forks) RemoveFork(name string) { + delete(*f, name) +} + // At returns ForksInTime instance that shows which supported forks are enabled for the block func (f *Forks) At(block uint64) ForksInTime { return ForksInTime{ @@ -123,6 +127,7 @@ type ForkParams struct { // MaxValidatorSetSize indicates the maximum size of validator set MaxValidatorSetSize *uint64 `json:"maxValidatorSetSize,omitempty"` + // EpochSize is size of epoch EpochSize *uint64 `json:"epochSize,omitempty"` // SprintSize is size of sprint @@ -140,8 +145,8 @@ type Fork struct { Params *ForkParams `json:"params,omitempty"` } -func NewFork(n uint64) *Fork { - return &Fork{ +func NewFork(n uint64) Fork { + return Fork{ Block: n, } } diff --git a/command/genesis/params.go b/command/genesis/params.go index 5039d4becb..3abf4da668 100644 --- a/command/genesis/params.go +++ b/command/genesis/params.go @@ -369,7 +369,7 @@ func (p *genesisParams) initGenesisConfig() error { // Disable london hardfork if burn contract address is not provided enabledForks := chain.AllForksEnabled if len(p.burnContracts) == 0 { - enabledForks.SetFork(chain.London, nil) + enabledForks.RemoveFork(chain.London) } chainConfig := &chain.Chain{ diff --git a/command/genesis/polybft_params.go b/command/genesis/polybft_params.go index 7220007620..efc5652dca 100644 --- a/command/genesis/polybft_params.go +++ b/command/genesis/polybft_params.go @@ -159,7 +159,7 @@ func (p *genesisParams) generatePolyBftChainConfig(o command.OutputFormatter) er // Disable london hardfork if burn contract address is not provided enabledForks := chain.AllForksEnabled if len(p.burnContracts) == 0 { - enabledForks.SetFork(chain.London, nil) + enabledForks.RemoveFork(chain.London) } chainConfig := &chain.Chain{ diff --git a/forkmanager/fork.go b/forkmanager/fork.go index a056438627..51bae5019a 100644 --- a/forkmanager/fork.go +++ b/forkmanager/fork.go @@ -18,7 +18,8 @@ type Fork struct { Name string // after the fork is activated, `FromBlockNumber` shows from which block is enabled FromBlockNumber uint64 - Params *chain.ForkParams + // fork consensus parameters + Params *chain.ForkParams // this value is false if fork is registered but not activated IsActive bool // map of all handlers registered for this fork @@ -62,11 +63,7 @@ func ForkManagerInit( return fmt.Errorf("fork is not available: %s", name) } - if f != nil { - fm.RegisterFork(name, f.Params) - } else { - fm.RegisterFork(name, nil) - } + fm.RegisterFork(name, f.Params) } // Register handlers and additional forks here @@ -81,10 +78,6 @@ func ForkManagerInit( // Activate forks for name, f := range *forks { - if f == nil { - continue - } - if err := fm.ActivateFork(name, f.Block); err != nil { return err } diff --git a/forkmanager/fork_manager.go b/forkmanager/fork_manager.go index d3d233bfcf..0b7d7e2899 100644 --- a/forkmanager/fork_manager.go +++ b/forkmanager/fork_manager.go @@ -9,17 +9,6 @@ import ( "github.com/0xPolygon/polygon-edge/chain" ) -/* -Regarding whether it is okay to use the Singleton pattern in Go, it's a topic of some debate. -The Singleton pattern can introduce global state and make testing and concurrent programming more challenging. -It can also make code tightly coupled and harder to maintain. -In general, it's recommended to favor dependency injection and explicit collaboration over singletons. - -However, there might be scenarios where the Singleton pattern is still useful, -such as managing shared resources or maintaining a global configuration. -Just be mindful of the potential drawbacks and consider alternative patterns when appropriate. -*/ - var ( forkManagerInstance *forkManager forkManagerInstanceLock sync.Mutex @@ -84,7 +73,7 @@ func (fm *forkManager) RegisterHandler(forkName string, handlerName HandlerDesc, } // ActivateFork activates fork from some block number -// All handlers and parameters belong to this fork are also activated +// All handlers and parameters belonging to this fork are also activated func (fm *forkManager) ActivateFork(forkName string, blockNumber uint64) error { fm.lock.Lock() defer fm.lock.Unlock()