forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsecure_validator_manager.go
56 lines (49 loc) · 1.72 KB
/
insecure_validator_manager.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
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package node
import (
"go.uber.org/zap"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/networking/router"
"github.com/ava-labs/avalanchego/snow/validators"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/version"
)
type insecureValidatorManager struct {
router.Router
log logging.Logger
vdrs validators.Manager
weight uint64
}
func (i *insecureValidatorManager) Connected(vdrID ids.NodeID, nodeVersion *version.Application, subnetID ids.ID) {
if constants.PrimaryNetworkID == subnetID {
// Sybil protection is disabled so we don't have a txID that added the
// peer as a validator. Because each validator needs a txID associated
// with it, we hack one together by padding the nodeID with zeroes.
dummyTxID := ids.Empty
copy(dummyTxID[:], vdrID.Bytes())
err := i.vdrs.AddStaker(constants.PrimaryNetworkID, vdrID, nil, dummyTxID, i.weight)
if err != nil {
i.log.Error("failed to add validator",
zap.Stringer("nodeID", vdrID),
zap.Stringer("subnetID", constants.PrimaryNetworkID),
zap.Error(err),
)
}
}
i.Router.Connected(vdrID, nodeVersion, subnetID)
}
func (i *insecureValidatorManager) Disconnected(vdrID ids.NodeID) {
// RemoveWeight will only error here if there was an error reported during
// Add.
err := i.vdrs.RemoveWeight(constants.PrimaryNetworkID, vdrID, i.weight)
if err != nil {
i.log.Error("failed to remove weight",
zap.Stringer("nodeID", vdrID),
zap.Stringer("subnetID", constants.PrimaryNetworkID),
zap.Error(err),
)
}
i.Router.Disconnected(vdrID)
}