-
Notifications
You must be signed in to change notification settings - Fork 669
/
parameters.go
102 lines (91 loc) · 4.38 KB
/
parameters.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package snowball
import (
"errors"
"fmt"
"time"
)
const (
// MinPercentConnectedBuffer is the safety buffer for calculation of
// MinPercentConnected. This increases the required percentage above
// alpha/k. This value must be [0-1].
// 0 means MinPercentConnected = alpha/k.
// 1 means MinPercentConnected = 1 (fully connected).
MinPercentConnectedBuffer = .2
errMsg = "" +
`__________ .___` + "\n" +
`\______ \____________ __| _/__.__.` + "\n" +
` | | _/\_ __ \__ \ / __ < | |` + "\n" +
` | | \ | | \// __ \_/ /_/ |\___ |` + "\n" +
` |______ / |__| (____ /\____ |/ ____|` + "\n" +
` \/ \/ \/\/` + "\n" +
"\n" +
` 🏆 🏆 🏆 🏆 🏆 🏆 🏆` + "\n" +
` ________ ________ ________________` + "\n" +
` / _____/ \_____ \ / _ \__ ___/` + "\n" +
`/ \ ___ / | \ / /_\ \| |` + "\n" +
`\ \_\ \/ | \/ | \ |` + "\n" +
` \______ /\_______ /\____|__ /____|` + "\n" +
` \/ \/ \/` + "\n"
)
var (
DefaultParameters = Parameters{
K: 20,
Alpha: 15,
BetaVirtuous: 15,
BetaRogue: 20,
ConcurrentRepolls: 4,
OptimalProcessing: 10,
MaxOutstandingItems: 256,
MaxItemProcessingTime: 30 * time.Second,
}
ErrParametersInvalid = errors.New("parameters invalid")
)
// Parameters required for snowball consensus
type Parameters struct {
K int `json:"k" yaml:"k"`
Alpha int `json:"alpha" yaml:"alpha"`
BetaVirtuous int `json:"betaVirtuous" yaml:"betaVirtuous"`
BetaRogue int `json:"betaRogue" yaml:"betaRogue"`
ConcurrentRepolls int `json:"concurrentRepolls" yaml:"concurrentRepolls"`
OptimalProcessing int `json:"optimalProcessing" yaml:"optimalProcessing"`
// Reports unhealthy if more than this number of items are outstanding.
MaxOutstandingItems int `json:"maxOutstandingItems" yaml:"maxOutstandingItems"`
// Reports unhealthy if there is an item processing for longer than this
// duration.
MaxItemProcessingTime time.Duration `json:"maxItemProcessingTime" yaml:"maxItemProcessingTime"`
}
// Verify returns nil if the parameters describe a valid initialization.
func (p Parameters) Verify() error {
switch {
case p.Alpha <= p.K/2:
return fmt.Errorf("%w: k = %d, alpha = %d: fails the condition that: k/2 < alpha", ErrParametersInvalid, p.K, p.Alpha)
case p.K < p.Alpha:
return fmt.Errorf("%w: k = %d, alpha = %d: fails the condition that: alpha <= k", ErrParametersInvalid, p.K, p.Alpha)
case p.BetaVirtuous <= 0:
return fmt.Errorf("%w: betaVirtuous = %d: fails the condition that: 0 < betaVirtuous", ErrParametersInvalid, p.BetaVirtuous)
case p.BetaRogue == 3 && p.BetaVirtuous == 28:
return fmt.Errorf("%w: betaVirtuous = %d, betaRogue = %d: fails the condition that: betaVirtuous <= betaRogue\n%s", ErrParametersInvalid, p.BetaVirtuous, p.BetaRogue, errMsg)
case p.BetaRogue < p.BetaVirtuous:
return fmt.Errorf("%w: betaVirtuous = %d, betaRogue = %d: fails the condition that: betaVirtuous <= betaRogue", ErrParametersInvalid, p.BetaVirtuous, p.BetaRogue)
case p.ConcurrentRepolls <= 0:
return fmt.Errorf("%w: concurrentRepolls = %d: fails the condition that: 0 < concurrentRepolls", ErrParametersInvalid, p.ConcurrentRepolls)
case p.ConcurrentRepolls > p.BetaRogue:
return fmt.Errorf("%w: concurrentRepolls = %d, betaRogue = %d: fails the condition that: concurrentRepolls <= betaRogue", ErrParametersInvalid, p.ConcurrentRepolls, p.BetaRogue)
case p.OptimalProcessing <= 0:
return fmt.Errorf("%w: optimalProcessing = %d: fails the condition that: 0 < optimalProcessing", ErrParametersInvalid, p.OptimalProcessing)
case p.MaxOutstandingItems <= 0:
return fmt.Errorf("%w: maxOutstandingItems = %d: fails the condition that: 0 < maxOutstandingItems", ErrParametersInvalid, p.MaxOutstandingItems)
case p.MaxItemProcessingTime <= 0:
return fmt.Errorf("%w: maxItemProcessingTime = %d: fails the condition that: 0 < maxItemProcessingTime", ErrParametersInvalid, p.MaxItemProcessingTime)
default:
return nil
}
}
func (p Parameters) MinPercentConnectedHealthy() float64 {
alpha := p.Alpha
k := p.K
r := float64(alpha) / float64(k)
return r*(1-MinPercentConnectedBuffer) + MinPercentConnectedBuffer
}