This repository has been archived by the owner on Jun 17, 2022. It is now read-only.
/
config_emitter.go
88 lines (69 loc) · 2.86 KB
/
config_emitter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package gossip
import (
"math/rand"
"time"
"github.com/ethereum/go-ethereum/common"
_params "github.com/ethereum/go-ethereum/params"
"github.com/Fantom-foundation/go-lachesis/inter/idx"
"github.com/Fantom-foundation/go-lachesis/lachesis/params"
)
// EmitIntervals is the configuration of emit intervals.
type EmitIntervals struct {
Min time.Duration `json:"min"`
Max time.Duration `json:"max"`
Confirming time.Duration `json:"confirming"` // emit time when there's no txs to originate, but at least 1 tx to confirm
SelfForkProtection time.Duration `json:"selfForkProtection"`
}
// EmitterConfig is the configuration of events emitter.
type EmitterConfig struct {
VersionToPublish string
Validator common.Address `json:"validator"`
EmitIntervals EmitIntervals `json:"emitIntervals"` // event emission intervals
MaxGasRateGrowthFactor float64 `json:"maxGasRateGrowthFactor"` // fine to use float, because no need in determinism
MaxTxsFromSender int `json:"maxTxsFromSender"`
EpochTailLength idx.Frame `json:"epochTailLength"` // number of frames before event is considered epoch
MaxParents int `json:"maxParents"`
// thresholds on GasLeft
SmoothTpsThreshold uint64 `json:"smoothTpsThreshold"`
NoTxsThreshold uint64 `json:"noTxsThreshold"`
EmergencyThreshold uint64 `json:"emergencyThreshold"`
}
// DefaultEmitterConfig returns the default configurations for the events emitter.
func DefaultEmitterConfig() EmitterConfig {
return EmitterConfig{
VersionToPublish: _params.VersionWithMeta(),
EmitIntervals: EmitIntervals{
Min: 400 * time.Millisecond,
Max: 12 * time.Minute,
Confirming: 500 * time.Millisecond,
SelfForkProtection: 30 * time.Minute, // should be at least 2x of MaxEmitInterval
},
MaxGasRateGrowthFactor: 3.0,
MaxTxsFromSender: TxTurnNonces,
EpochTailLength: 1,
MaxParents: 7,
SmoothTpsThreshold: (params.EventGas + params.TxGas) * 500,
NoTxsThreshold: params.EventGas * 30,
EmergencyThreshold: params.EventGas * 5,
}
}
// RandomizeEmitTime and return new config
func (cfg *EmitIntervals) RandomizeEmitTime(r *rand.Rand) *EmitIntervals {
config := *cfg
// value = value - 0.1 * value + 0.1 * random value
if config.Max > 10 {
config.Max = config.Max - config.Max/10 + time.Duration(r.Int63n(int64(config.Max/10)))
}
// value = value + 0.1 * random value
if config.SelfForkProtection > 10 {
config.SelfForkProtection = config.SelfForkProtection + time.Duration(r.Int63n(int64(config.SelfForkProtection/10)))
}
return &config
}
// FakeEmitterConfig returns the testing configurations for the events emitter.
func FakeEmitterConfig() EmitterConfig {
cfg := DefaultEmitterConfig()
cfg.EmitIntervals.Max = 10 * time.Second // don't wait long in fakenet
cfg.EmitIntervals.SelfForkProtection = cfg.EmitIntervals.Max * 3 / 2
return cfg
}