-
Notifications
You must be signed in to change notification settings - Fork 647
/
config.go
70 lines (61 loc) · 1.96 KB
/
config.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
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package syncer
import (
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/engine/common"
"github.com/ava-labs/avalanchego/snow/engine/snowman/block"
"github.com/ava-labs/avalanchego/snow/validators"
)
type Config struct {
common.Config
common.AllGetsServer
// SampleK determines the number of nodes to attempt to fetch the latest
// state sync summary from. In order for a round of voting to succeed, there
// must be at least one correct node sampled.
SampleK int
// Alpha specifies the amount of weight that validators must put behind a
// state summary to consider it valid to sync to.
Alpha uint64
// StateSyncBeacons are the nodes that will be used to sample and vote over
// state summaries.
StateSyncBeacons validators.Set
VM block.ChainVM
}
func NewConfig(
commonCfg common.Config,
stateSyncerIDs []ids.NodeID,
snowGetHandler common.AllGetsServer,
vm block.ChainVM,
) (Config, error) {
// Initialize the default values that will be used if stateSyncerIDs is
// empty.
var (
stateSyncBeacons = commonCfg.Beacons
syncAlpha = commonCfg.Alpha
syncSampleK = commonCfg.SampleK
)
// If the user has manually provided state syncer IDs, then override the
// state sync beacons to them.
if len(stateSyncerIDs) != 0 {
stateSyncBeacons = validators.NewSet()
for _, peerID := range stateSyncerIDs {
if err := stateSyncBeacons.AddWeight(peerID, 1); err != nil {
return Config{}, err
}
}
stateSyncingWeight := stateSyncBeacons.Weight()
if uint64(syncSampleK) > stateSyncingWeight {
syncSampleK = int(stateSyncingWeight)
}
syncAlpha = stateSyncingWeight/2 + 1 // must be > 50%
}
return Config{
Config: commonCfg,
AllGetsServer: snowGetHandler,
SampleK: syncSampleK,
Alpha: syncAlpha,
StateSyncBeacons: stateSyncBeacons,
VM: vm,
}, nil
}