-
Notifications
You must be signed in to change notification settings - Fork 724
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Yacov Manevich <yacov.manevich@avalabs.org>
- Loading branch information
Showing
13 changed files
with
203 additions
and
319 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package network | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/ava-labs/avalanchego/ids" | ||
"github.com/ava-labs/avalanchego/snow/validators" | ||
"github.com/ava-labs/avalanchego/utils/constants" | ||
) | ||
|
||
// ErrNoIngressConnections denotes that no node is connected to this validator. | ||
var ErrNoIngressConnections = errors.New("no ingress connections") | ||
|
||
type ingressConnectionCounter interface { | ||
IngressConnCount() int | ||
} | ||
|
||
type validatorRetriever interface { | ||
GetValidator(subnetID ids.ID, nodeID ids.NodeID) (*validators.Validator, bool) | ||
} | ||
|
||
type noIngressConnAlert struct { | ||
selfID ids.NodeID | ||
ingressConnections ingressConnectionCounter | ||
validators validatorRetriever | ||
} | ||
|
||
func ingressConnResult(n int, areWeValidator bool) map[string]interface{} { | ||
return map[string]interface{}{"ingressConnectionCount": n, "primary network validator": areWeValidator} | ||
} | ||
|
||
func (nica *noIngressConnAlert) checkHealth() (interface{}, error) { | ||
connCount := nica.ingressConnections.IngressConnCount() | ||
_, areWeValidator := nica.validators.GetValidator(constants.PrimaryNetworkID, nica.selfID) | ||
|
||
if connCount > 0 { | ||
return ingressConnResult(connCount, areWeValidator), nil | ||
} | ||
|
||
if !areWeValidator { | ||
return ingressConnResult(connCount, areWeValidator), nil | ||
} | ||
|
||
return ingressConnResult(connCount, areWeValidator), ErrNoIngressConnections | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package network | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/ava-labs/avalanchego/ids" | ||
"github.com/ava-labs/avalanchego/snow/validators" | ||
) | ||
|
||
type fakeValidatorRetriever struct { | ||
result bool | ||
} | ||
|
||
func (m *fakeValidatorRetriever) GetValidator(ids.ID, ids.NodeID) (*validators.Validator, bool) { | ||
return nil, m.result | ||
} | ||
|
||
type fakeIngressConnectionCounter struct { | ||
res int | ||
} | ||
|
||
func (m *fakeIngressConnectionCounter) IngressConnCount() int { | ||
return m.res | ||
} | ||
|
||
func TestNoIngressConnAlertHealthCheck(t *testing.T) { | ||
for _, testCase := range []struct { | ||
name string | ||
getValidatorResult bool | ||
ingressConnCountResult int | ||
expectedErr error | ||
expectedResult interface{} | ||
}{ | ||
{ | ||
name: "not a validator of a primary network", | ||
expectedResult: map[string]interface{}{"ingressConnectionCount": 0, "primary network validator": false}, | ||
}, | ||
{ | ||
name: "a validator of the primary network", | ||
getValidatorResult: true, | ||
expectedResult: map[string]interface{}{ | ||
"ingressConnectionCount": 0, "primary network validator": true, | ||
}, | ||
expectedErr: ErrNoIngressConnections, | ||
}, | ||
{ | ||
name: "a validator with ingress connections", | ||
expectedResult: map[string]interface{}{"ingressConnectionCount": 42, "primary network validator": true}, | ||
expectedErr: nil, | ||
ingressConnCountResult: 42, | ||
getValidatorResult: true, | ||
}, | ||
} { | ||
t.Run(testCase.name, func(t *testing.T) { | ||
nica := &noIngressConnAlert{ | ||
selfID: ids.EmptyNodeID, | ||
validators: &fakeValidatorRetriever{result: testCase.getValidatorResult}, | ||
ingressConnections: &fakeIngressConnectionCounter{res: testCase.ingressConnCountResult}, | ||
} | ||
|
||
result, err := nica.checkHealth() | ||
require.Equal(t, testCase.expectedErr, err) | ||
require.Equal(t, testCase.expectedResult, result) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.