11package configs
22
33import (
4- "errors"
54 "fmt"
65 "sort"
76
7+ "github.com/multiversx/mx-chain-core-go/core/check"
8+ "github.com/multiversx/mx-chain-go/common/configs/dto"
89 "github.com/multiversx/mx-chain-go/config"
910 "github.com/multiversx/mx-chain-go/process"
11+ logger "github.com/multiversx/mx-chain-logger-go"
1012)
1113
12- const minRoundsToKeepUnprocessedData = uint64 (1 )
13-
14- const (
15- defaultMaxMetaNoncesBehind = 15
16- defaultMaxMetaNoncesBehindForGlobalStuck = 30
17- defaultMaxShardNoncesBehind = 15
18- defaultMaxRoundsWithoutNewBlockReceived = 10
19- defaultMaxRoundsWithoutCommittedBlock = 10
20- defaultRoundModulusTriggerWhenSyncIsStuck = 20
21- defaultMaxSyncWithErrorsAllowed = 20
22- defaultMaxRoundsToKeepUnprocessedMiniBlocks = 3000
23- defaultMaxRoundsToKeepUnprocessedTransactions = 3000
24- )
25-
26- // ErrEmptyProcessConfigsByEpoch signals that an empty process configs by epoch has been provided
27- var ErrEmptyProcessConfigsByEpoch = errors .New ("empty process configs by epoch" )
28-
29- // ErrEmptyProcessConfigsByRound signals that an empty process configs by round has been provided
30- var ErrEmptyProcessConfigsByRound = errors .New ("empty process configs by round" )
31-
32- // ErrDuplicatedEpochConfig signals that a duplicated config section has been provided
33- var ErrDuplicatedEpochConfig = errors .New ("duplicated epoch config" )
34-
35- // ErrDuplicatedRoundConfig signals that a duplicated config section has been provided
36- var ErrDuplicatedRoundConfig = errors .New ("duplicated round config" )
37-
38- // ErrMissingEpochZeroConfig signals that epoch zero configuration is missing
39- var ErrMissingEpochZeroConfig = errors .New ("missing configuration for epoch 0" )
14+ var log = logger .GetOrCreate ("processConfigs" )
4015
41- // ErrMissingRoundZeroConfig signals that base round configuration is missing
42- var ErrMissingRoundZeroConfig = errors .New ("missing base configuration for round" )
16+ type configByRoundSelector [T any ] func (config.ProcessConfigByRound ) T
17+ type configVariableHandler struct {
18+ valueSelector configByRoundSelector [uint64 ]
19+ defaultValue uint64
20+ }
4321
4422// processConfigsByEpoch holds the process configuration for epoch changes
4523type processConfigsByEpoch struct {
4624 orderedConfigByEpoch []config.ProcessConfigByEpoch
4725 orderedConfigByRound []config.ProcessConfigByRound
26+ roundNotifier process.RoundNotifier
27+ variablesMap map [dto.ConfigVariable ]configVariableHandler
4828}
4929
5030// NewProcessConfigsHandler creates a new process configs by epoch component
5131func NewProcessConfigsHandler (
5232 configsByEpoch []config.ProcessConfigByEpoch ,
5333 configsByRound []config.ProcessConfigByRound ,
34+ roundNotifier process.RoundNotifier ,
5435) (* processConfigsByEpoch , error ) {
36+ if check .IfNil (roundNotifier ) {
37+ return nil , fmt .Errorf ("%w in NewProcessConfigsHandler" , process .ErrNilRoundNotifier )
38+ }
39+
5540 err := checkConfigsByEpoch (configsByEpoch )
5641 if err != nil {
5742 return nil , err
@@ -65,6 +50,8 @@ func NewProcessConfigsHandler(
6550 pce := & processConfigsByEpoch {
6651 orderedConfigByEpoch : make ([]config.ProcessConfigByEpoch , len (configsByEpoch )),
6752 orderedConfigByRound : make ([]config.ProcessConfigByRound , len (configsByRound )),
53+ roundNotifier : roundNotifier ,
54+ variablesMap : initCfgVarMap (),
6855 }
6956
7057 // sort the config values in ascending order
@@ -142,10 +129,24 @@ func checkRoundConfigValues(cfg config.ProcessConfigByRound) error {
142129 return fmt .Errorf ("%w for MaxRoundsToKeepUnprocessedMiniBlocks, received %d, min expected %d" ,
143130 process .ErrInvalidValue , cfg .MaxRoundsToKeepUnprocessedMiniBlocks , minRoundsToKeepUnprocessedData )
144131 }
132+ if cfg .NumFloodingRoundsFastReacting < minFloodingRounds {
133+ return fmt .Errorf ("%w for NumFloodingRoundsFastReacting, received %d, min expected %d" ,
134+ process .ErrInvalidValue , cfg .NumFloodingRoundsFastReacting , minFloodingRounds )
135+ }
136+ if cfg .NumFloodingRoundsSlowReacting < minFloodingRounds {
137+ return fmt .Errorf ("%w for NumFloodingRoundsSlowReacting, received %d, min expected %d" ,
138+ process .ErrInvalidValue , cfg .NumFloodingRoundsSlowReacting , minFloodingRounds )
139+ }
140+ if cfg .NumFloodingRoundsOutOfSpecs < minFloodingRounds {
141+ return fmt .Errorf ("%w for NumFloodingRoundsOutOfSpecs, received %d, min expected %d" ,
142+ process .ErrInvalidValue , cfg .NumFloodingRoundsOutOfSpecs , minFloodingRounds )
143+ }
145144
146145 return nil
147146}
148147
148+ // TODO: We should probably get rid of all these functions and use only GetValue func, similar to how enableEpochsHandler works
149+
149150// GetMaxMetaNoncesBehindByEpoch returns the max meta nonces behind by epoch
150151func (pce * processConfigsByEpoch ) GetMaxMetaNoncesBehindByEpoch (epoch uint32 ) uint32 {
151152 for i := len (pce .orderedConfigByEpoch ) - 1 ; i >= 0 ; i -- {
@@ -182,7 +183,7 @@ func (pce *processConfigsByEpoch) GetMaxShardNoncesBehindByEpoch(epoch uint32) u
182183func getConfigValueByRound [T any ](
183184 configs []config.ProcessConfigByRound ,
184185 round uint64 ,
185- selector func (config. ProcessConfigByRound ) T ,
186+ selector configByRoundSelector [ T ] ,
186187 defaultValue T ,
187188) T {
188189 for i := len (configs ) - 1 ; i >= 0 ; i -- {
@@ -266,6 +267,29 @@ func (pce *processConfigsByEpoch) GetMaxRoundsToKeepUnprocessedTransactions(roun
266267 )
267268}
268269
270+ // GetValue returns the value of the provided variable for the current round
271+ func (pce * processConfigsByEpoch ) GetValue (variable dto.ConfigVariable ) uint64 {
272+ return pce .getValueByRound (variable , pce .roundNotifier .CurrentRound ())
273+ }
274+
275+ func (pce * processConfigsByEpoch ) getValueByRound (
276+ variable dto.ConfigVariable ,
277+ round uint64 ,
278+ ) uint64 {
279+ cfgVarHandler , ok := pce .variablesMap [variable ]
280+ if ! ok {
281+ log .Warn ("processConfigsByEpoch.getValueByRound: variable not found in pce.variablesMap" , "variable" , variable )
282+ return 0
283+ }
284+
285+ return getConfigValueByRound (
286+ pce .orderedConfigByRound ,
287+ round ,
288+ cfgVarHandler .valueSelector ,
289+ cfgVarHandler .defaultValue ,
290+ )
291+ }
292+
269293// IsInterfaceNil checks if the instance is nil
270294func (pce * processConfigsByEpoch ) IsInterfaceNil () bool {
271295 return pce == nil
0 commit comments