Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add network connectivity to health-checks #644

Merged
merged 22 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
8adf571
healthcheck handle request
iansuvak Jan 22, 2025
d770db5
use existing ConnectToCanonicalValidators to set up for the test
iansuvak Jan 23, 2025
4676674
actually check for connections
iansuvak Jan 23, 2025
110508b
Merge branch 'bump-icm-contracts' into healthcheck-sig-agg
iansuvak Jan 23, 2025
10ecb86
retry connection with backoff
iansuvak Jan 24, 2025
944f1d4
lint
iansuvak Jan 24, 2025
61b12bf
use subnet-evm instead of coreth for warpnumerator
iansuvak Jan 24, 2025
16ac01d
extract blocking connnect method to a helper
iansuvak Jan 24, 2025
4be815c
mock network.Network
iansuvak Jan 24, 2025
2bb6490
Split out connections from getting validators + add unit tests
iansuvak Jan 27, 2025
280e5e4
pass in healthcheck func instead of accessing network from healthchec…
iansuvak Jan 27, 2025
6457892
Merge remote-tracking branch 'origin/main' into healthcheck-sig-agg
iansuvak Jan 27, 2025
bfe6729
connect to trackedSubnets as well and include them in healthcheck
iansuvak Jan 27, 2025
8030249
extend healthcheck to relayer
iansuvak Jan 27, 2025
ce5266f
review feedback
iansuvak Jan 29, 2025
fb823bd
Merge remote-tracking branch 'origin/main' into healthcheck-sig-agg
iansuvak Jan 29, 2025
eeec832
Update signature-aggregator/aggregator/aggregator.go
iansuvak Jan 29, 2025
29829f7
lower minimum timeout and tie apprequest deadlines to the maximum tim…
iansuvak Jan 29, 2025
78dd329
update GetConnectedCanonicalValidators comment
iansuvak Jan 29, 2025
e6c2924
remove unnecessary constant
iansuvak Jan 30, 2025
9e1ad56
Merge branch 'main' into healthcheck-sig-agg
iansuvak Jan 30, 2025
f9aa4be
Merge branch 'main' into healthcheck-sig-agg
geoff-vball Jan 31, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 46 additions & 9 deletions peers/app_request_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
// See the file LICENSE for licensing terms.

//go:generate mockgen -source=$GOFILE -destination=./mocks/mock_app_request_network.go -package=mocks
//go:generate mockgen -destination=./avago_mocks/mock_network.go -package=avago_mocks github.com/ava-labs/avalanchego/network Network

package peers

import (
"context"
"encoding/hex"
"errors"
"fmt"
"math/big"
"sync"
"time"

Expand All @@ -28,6 +31,9 @@ import (
"github.com/ava-labs/avalanchego/vms/platformvm/warp"
"github.com/ava-labs/icm-services/peers/utils"
"github.com/ava-labs/icm-services/peers/validators"
subnetWarp "github.com/ava-labs/subnet-evm/precompile/contracts/warp"

sharedUtils "github.com/ava-labs/icm-services/utils"
"github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"
)
Expand All @@ -39,8 +45,12 @@ const (
NumBootstrapNodes = 5
)

var (
errNotEnoughConnectedStake = errors.New("failed to connect to a threshold of stake")
)

type AppRequestNetwork interface {
ConnectToCanonicalValidators(subnetID ids.ID) (
GetConnectedCanonicalValidators(subnetID ids.ID) (
*ConnectedCanonicalValidators,
error,
)
Expand All @@ -66,7 +76,7 @@ type appRequestNetwork struct {
infoAPI *InfoAPI
logger logging.Logger
lock *sync.RWMutex
validatorClient *validators.CanonicalValidatorClient
validatorClient validators.CanonicalValidatorState
metrics *AppRequestNetworkMetrics

trackedSubnets set.Set[ids.ID]
Expand Down Expand Up @@ -233,6 +243,8 @@ func (n *appRequestNetwork) containsSubnet(subnetID ids.ID) bool {
return n.trackedSubnets.Contains(subnetID)
}

// TrackSubnet adds the subnet to the list of tracked subnets
// and initiates the connections to the subnet's validators asynchronously
func (n *appRequestNetwork) TrackSubnet(subnetID ids.ID) {
if n.containsSubnet(subnetID) {
return
Expand Down Expand Up @@ -325,12 +337,9 @@ func (c *ConnectedCanonicalValidators) GetValidator(nodeID ids.NodeID) (*warp.Va
return c.ValidatorSet[c.NodeValidatorIndexMap[nodeID]], c.NodeValidatorIndexMap[nodeID]
}

// ConnectToCanonicalValidators connects to the canonical validators of the given subnet and returns the connected
// validator information
func (n *appRequestNetwork) ConnectToCanonicalValidators(subnetID ids.ID) (*ConnectedCanonicalValidators, error) {
// Track the subnet
n.TrackSubnet(subnetID)

// GetConnectedCanonicalValidators returns the connected validator information for the given subnet
// at the time of the call.
func (n *appRequestNetwork) GetConnectedCanonicalValidators(subnetID ids.ID) (*ConnectedCanonicalValidators, error) {
// Get the subnet's current canonical validator set
startPChainAPICall := time.Now()
validatorSet, totalValidatorWeight, err := n.validatorClient.GetCurrentCanonicalValidatorSet(subnetID)
Expand All @@ -351,8 +360,16 @@ func (n *appRequestNetwork) ConnectToCanonicalValidators(subnetID ids.ID) (*Conn
}
}

peerInfo := n.network.PeerInfo(nodeIDs.List())
connectedPeers := set.NewSet[ids.NodeID](len(nodeIDs))
for _, peer := range peerInfo {
if nodeIDs.Contains(peer.ID) {
connectedPeers.Add(peer.ID)
}
}

// Calculate the total weight of connected validators.
connectedWeight := calculateConnectedWeight(validatorSet, nodeValidatorIndexMap, nodeIDs)
connectedWeight := calculateConnectedWeight(validatorSet, nodeValidatorIndexMap, connectedPeers)

return &ConnectedCanonicalValidators{
ConnectedWeight: connectedWeight,
Expand Down Expand Up @@ -391,6 +408,26 @@ func (n *appRequestNetwork) setPChainAPICallLatencyMS(latency float64) {

// Non-receiver util functions

func GetNetworkHealthFunc(network AppRequestNetwork, subnetIDs []ids.ID) func(context.Context) error {
return func(context.Context) error {
for _, subnetID := range subnetIDs {
connectedValidators, err := network.GetConnectedCanonicalValidators(subnetID)
if err != nil {
return fmt.Errorf(
"failed to get connected validators: %s, %w", subnetID, err)
}
if !sharedUtils.CheckStakeWeightExceedsThreshold(
big.NewInt(0).SetUint64(connectedValidators.ConnectedWeight),
connectedValidators.TotalValidatorWeight,
subnetWarp.WarpDefaultQuorumNumerator,
) {
return errNotEnoughConnectedStake
}
}
return nil
}
}

func calculateConnectedWeight(
validatorSet []*warp.Validator,
nodeValidatorIndexMap map[ids.NodeID]int,
Expand Down
109 changes: 109 additions & 0 deletions peers/app_request_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@ import (
"testing"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/network/peer"
"github.com/ava-labs/avalanchego/utils/crypto/bls"
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/vms/platformvm/warp"
"github.com/ava-labs/icm-services/peers/avago_mocks"
validator_mocks "github.com/ava-labs/icm-services/peers/validators/mocks"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
)

func TestCalculateConnectedWeight(t *testing.T) {
Expand Down Expand Up @@ -48,6 +53,110 @@ func TestCalculateConnectedWeight(t *testing.T) {
require.Equal(t, uint64(60), connectedWeight3)
}

func TestConnectToCanonicalValidators(t *testing.T) {
ctrl := gomock.NewController(t)

subnetID := ids.GenerateTestID()
validator1_1 := makeValidator(t, 1, 1)
validator2_1 := makeValidator(t, 2, 1)
validator3_2 := makeValidator(t, 3, 2)
metrics, _ := newAppRequestNetworkMetrics(prometheus.DefaultRegisterer)

testCases := []struct {
name string
validators []*warp.Validator
connectedNodes []ids.NodeID
expectedConnectedWeight uint64
expectedTotalWeight uint64
}{
{
name: "no connected nodes, one validator",
validators: []*warp.Validator{&validator1_1},
connectedNodes: []ids.NodeID{},
expectedConnectedWeight: 0,
expectedTotalWeight: 1,
},
{
name: "all validators, missing one nodeID",
validators: []*warp.Validator{&validator1_1, &validator2_1, &validator3_2},
connectedNodes: []ids.NodeID{
validator1_1.NodeIDs[0],
validator2_1.NodeIDs[0],
validator3_2.NodeIDs[0],
validator3_2.NodeIDs[1],
},
expectedConnectedWeight: 6,
expectedTotalWeight: 6,
},
{
name: "fully connected",
validators: []*warp.Validator{&validator1_1, &validator2_1, &validator3_2},
connectedNodes: []ids.NodeID{
validator1_1.NodeIDs[0],
validator2_1.NodeIDs[0],
validator3_2.NodeIDs[0],
validator3_2.NodeIDs[1],
},
expectedConnectedWeight: 6,
expectedTotalWeight: 6,
},
{
name: "missing conn to double node validator",
validators: []*warp.Validator{&validator1_1, &validator2_1, &validator3_2},
connectedNodes: []ids.NodeID{
validator1_1.NodeIDs[0],
validator2_1.NodeIDs[0],
},
expectedConnectedWeight: 3,
expectedTotalWeight: 6,
},
{
name: "irrelevant nodes",
validators: []*warp.Validator{&validator1_1, &validator2_1},
connectedNodes: []ids.NodeID{
validator1_1.NodeIDs[0],
validator2_1.NodeIDs[0],
validator3_2.NodeIDs[0], // this nodeID does not map to the validator
},
expectedConnectedWeight: 3,
expectedTotalWeight: 3,
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
mockNetwork := avago_mocks.NewMockNetwork(ctrl)
mockValidatorClient := validator_mocks.NewMockCanonicalValidatorState(ctrl)
arNetwork := appRequestNetwork{
network: mockNetwork,
validatorClient: mockValidatorClient,
metrics: metrics,
}
var totalWeight uint64
for _, vdr := range testCase.validators {
totalWeight += vdr.Weight
}
mockValidatorClient.EXPECT().GetCurrentCanonicalValidatorSet(subnetID).Return(
testCase.validators,
totalWeight,
nil,
).Times(1)

peerInfo := make([]peer.Info, len(testCase.validators))
for _, node := range testCase.connectedNodes {
peerInfo = append(peerInfo, peer.Info{
ID: node,
})
}
mockNetwork.EXPECT().PeerInfo(gomock.Any()).Return(peerInfo).Times(1)

ret, err := arNetwork.GetConnectedCanonicalValidators(subnetID)
require.Equal(t, testCase.expectedConnectedWeight, ret.ConnectedWeight)
require.Equal(t, testCase.expectedTotalWeight, ret.TotalValidatorWeight)
require.NoError(t, err)
})
}
}

func makeValidator(t *testing.T, weight uint64, numNodeIDs int) warp.Validator {
sk, err := bls.NewSecretKey()
require.NoError(t, err)
Expand Down
Loading