forked from ava-labs/avalanchego
/
config.go
82 lines (70 loc) · 2.45 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
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package syncer
import (
"fmt"
"github.com/MetalBlockchain/metalgo/ids"
"github.com/MetalBlockchain/metalgo/snow"
"github.com/MetalBlockchain/metalgo/snow/engine/common"
"github.com/MetalBlockchain/metalgo/snow/engine/common/tracker"
"github.com/MetalBlockchain/metalgo/snow/engine/snowman/block"
"github.com/MetalBlockchain/metalgo/snow/validators"
)
type Config struct {
common.AllGetsServer
Ctx *snow.ConsensusContext
StartupTracker tracker.Startup
Sender common.Sender
// 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.Manager
VM block.ChainVM
}
func NewConfig(
snowGetHandler common.AllGetsServer,
ctx *snow.ConsensusContext,
startupTracker tracker.Startup,
sender common.Sender,
beacons validators.Manager,
sampleK int,
alpha uint64,
stateSyncerIDs []ids.NodeID,
vm block.ChainVM,
) (Config, error) {
// Initialize the beacons that will be used if stateSyncerIDs is empty.
stateSyncBeacons := beacons
// If the user has manually provided state syncer IDs, then override the
// state sync beacons to them.
if len(stateSyncerIDs) != 0 {
stateSyncBeacons = validators.NewManager()
for _, peerID := range stateSyncerIDs {
// Invariant: We never use the TxID or BLS keys populated here.
if err := stateSyncBeacons.AddStaker(ctx.SubnetID, peerID, nil, ids.Empty, 1); err != nil {
return Config{}, err
}
}
stateSyncingWeight, err := stateSyncBeacons.TotalWeight(ctx.SubnetID)
if err != nil {
return Config{}, fmt.Errorf("failed to calculate total weight of state sync beacons for subnet %s: %w", ctx.SubnetID, err)
}
sampleK = int(min(uint64(sampleK), stateSyncingWeight))
alpha = stateSyncingWeight/2 + 1 // must be > 50%
}
return Config{
AllGetsServer: snowGetHandler,
Ctx: ctx,
StartupTracker: startupTracker,
Sender: sender,
SampleK: sampleK,
Alpha: alpha,
StateSyncBeacons: stateSyncBeacons,
VM: vm,
}, nil
}