github.com/unicornultrafoundation/go-u2u@v1.0.0-rc1.0.20240205080301-e74a83d3fadc/gossip/emitter/config.go (about)

     1  package emitter
     2  
     3  import (
     4  	"math/rand"
     5  	"time"
     6  
     7  	"github.com/unicornultrafoundation/go-helios/native/idx"
     8  	"github.com/unicornultrafoundation/go-u2u/params"
     9  
    10  	"github.com/unicornultrafoundation/go-u2u/native/validatorpk"
    11  	"github.com/unicornultrafoundation/go-u2u/u2u"
    12  )
    13  
    14  // EmitIntervals is the configuration of emit intervals.
    15  type EmitIntervals struct {
    16  	Min                        time.Duration
    17  	Max                        time.Duration
    18  	Confirming                 time.Duration // emit time when there's no txs to originate, but at least 1 tx to confirm
    19  	ParallelInstanceProtection time.Duration
    20  	DoublesignProtection       time.Duration
    21  }
    22  
    23  type ValidatorConfig struct {
    24  	ID     idx.ValidatorID
    25  	PubKey validatorpk.PubKey
    26  }
    27  
    28  type FileConfig struct {
    29  	Path     string
    30  	SyncMode bool
    31  }
    32  
    33  // Config is the configuration of events emitter.
    34  type Config struct {
    35  	VersionToPublish string
    36  
    37  	Validator ValidatorConfig
    38  
    39  	EmitIntervals EmitIntervals // event emission intervals
    40  
    41  	MaxTxsPerAddress int
    42  
    43  	MaxParents idx.Event
    44  
    45  	// thresholds on GasLeft
    46  	LimitedTpsThreshold uint64
    47  	NoTxsThreshold      uint64
    48  	EmergencyThreshold  uint64
    49  
    50  	TxsCacheInvalidation time.Duration
    51  
    52  	PrevEmittedEventFile FileConfig
    53  	PrevBlockVotesFile   FileConfig
    54  	PrevEpochVoteFile    FileConfig
    55  }
    56  
    57  // DefaultConfig returns the default configurations for the events emitter.
    58  func DefaultConfig() Config {
    59  	return Config{
    60  		VersionToPublish: params.VersionWithMeta(),
    61  
    62  		EmitIntervals: EmitIntervals{
    63  			Min:                        150 * time.Millisecond,
    64  			Max:                        1 * time.Second,
    65  			Confirming:                 170 * time.Millisecond,
    66  			DoublesignProtection:       27 * time.Minute, // should be greater than MaxEmitInterval
    67  			ParallelInstanceProtection: 1 * time.Minute,
    68  		},
    69  
    70  		MaxTxsPerAddress: TxTurnNonces,
    71  
    72  		MaxParents: 0,
    73  
    74  		LimitedTpsThreshold: u2u.DefaultEventGas * 120,
    75  		NoTxsThreshold:      u2u.DefaultEventGas * 30,
    76  		EmergencyThreshold:  u2u.DefaultEventGas * 5,
    77  
    78  		TxsCacheInvalidation: 200 * time.Millisecond,
    79  	}
    80  }
    81  
    82  // RandomizeEmitTime and return new config
    83  func (cfg EmitIntervals) RandomizeEmitTime(r *rand.Rand) EmitIntervals {
    84  	config := cfg
    85  	// value = value - 0.1 * value + 0.1 * random value
    86  	if config.Max > 10 {
    87  		config.Max = config.Max - config.Max/10 + time.Duration(r.Int63n(int64(config.Max/10)))
    88  	}
    89  	// value = value + 0.33 * random value
    90  	if config.DoublesignProtection > 3 {
    91  		config.DoublesignProtection = config.DoublesignProtection + time.Duration(r.Int63n(int64(config.DoublesignProtection/3)))
    92  	}
    93  	return config
    94  }
    95  
    96  // FakeConfig returns the testing configurations for the events emitter.
    97  func FakeConfig(num idx.Validator) Config {
    98  	cfg := DefaultConfig()
    99  	cfg.EmitIntervals.Max = 10 * time.Second // don't wait long in fakenet
   100  	cfg.EmitIntervals.DoublesignProtection = cfg.EmitIntervals.Max / 2
   101  	if num <= 1 {
   102  		// disable self-fork protection if fakenet 1/1
   103  		cfg.EmitIntervals.DoublesignProtection = 0
   104  	}
   105  	return cfg
   106  }