Skip to content

Commit

Permalink
cr fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
igorcrevar committed Jun 23, 2023
1 parent d1ca973 commit 15f8076
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 30 deletions.
17 changes: 11 additions & 6 deletions chain/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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
Expand All @@ -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,
}
}
Expand Down
2 changes: 1 addition & 1 deletion command/genesis/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion command/genesis/polybft_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
13 changes: 3 additions & 10 deletions forkmanager/fork.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down
13 changes: 1 addition & 12 deletions forkmanager/fork_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 15f8076

Please sign in to comment.