Skip to content

Commit

Permalink
Merge pull request #457 from ava-labs/sig-agg-unit-tests
Browse files Browse the repository at this point in the history
Initial signature aggregator unit tests
  • Loading branch information
feuGeneA authored Aug 30, 2024
2 parents a1dd060 + ad87ee7 commit ccc7a60
Show file tree
Hide file tree
Showing 10 changed files with 315 additions and 26 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/mock_checker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Mock Checker

on:
push:
branches:
- main
pull_request:
branches:
- "**"

jobs:
generated_code:
name: generated_code
runs-on: ubuntu-22.04

steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
submodules: recursive

- name: Setup Go
uses: actions/setup-go@v5
with:
go-version-file: "go.mod"

- name: Generate code
run: |
scripts/generate.sh
- name: Print diff
run: git --no-pager diff

- name: Fail if diff exists
run: git --no-pager diff --quiet
44 changes: 33 additions & 11 deletions peers/app_request_network.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (C) 2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

//go:generate mockgen -source=$GOFILE -destination=./mocks/mock_app_request_network.go -package=mocks

package peers

import (
Expand Down Expand Up @@ -28,7 +30,27 @@ const (
DefaultAppRequestTimeout = time.Second * 2
)

type AppRequestNetwork struct {
type AppRequestNetwork interface {
ConnectPeers(nodeIDs set.Set[ids.NodeID]) set.Set[ids.NodeID]
ConnectToCanonicalValidators(subnetID ids.ID) (
*ConnectedCanonicalValidators,
error,
)
GetSubnetID(blockchainID ids.ID) (ids.ID, error)
RegisterAppRequest(requestID ids.RequestID)
RegisterRequestID(
requestID uint32,
numExpectedResponse int,
) chan message.InboundMessage
Send(
msg message.OutboundMessage,
nodeIDs set.Set[ids.NodeID],
subnetID ids.ID,
allower subnets.Allower,
) set.Set[ids.NodeID]
}

type appRequestNetwork struct {
network network.Network
handler *RelayerExternalHandler
infoAPI *InfoAPI
Expand All @@ -44,7 +66,7 @@ func NewNetwork(
registerer prometheus.Registerer,
trackedSubnets set.Set[ids.ID],
cfg Config,
) (*AppRequestNetwork, error) {
) (AppRequestNetwork, error) {
logger := logging.NewLogger(
"p2p-network",
logging.NewWrappedCore(
Expand Down Expand Up @@ -98,7 +120,7 @@ func NewNetwork(

validatorClient := validators.NewCanonicalValidatorClient(logger, cfg.GetPChainAPI())

arNetwork := &AppRequestNetwork{
arNetwork := &appRequestNetwork{
network: testNetwork,
handler: handler,
infoAPI: infoAPI,
Expand All @@ -116,7 +138,7 @@ func NewNetwork(

// ConnectPeers connects the network to peers with the given nodeIDs.
// Returns the set of nodeIDs that were successfully connected to.
func (n *AppRequestNetwork) ConnectPeers(nodeIDs set.Set[ids.NodeID]) set.Set[ids.NodeID] {
func (n *appRequestNetwork) ConnectPeers(nodeIDs set.Set[ids.NodeID]) set.Set[ids.NodeID] {
n.lock.Lock()
defer n.lock.Unlock()

Expand Down Expand Up @@ -200,7 +222,7 @@ func (c *ConnectedCanonicalValidators) GetValidator(nodeID ids.NodeID) (*warp.Va

// 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) {
func (n *appRequestNetwork) ConnectToCanonicalValidators(subnetID ids.ID) (*ConnectedCanonicalValidators, error) {
// Get the subnet's current canonical validator set
startPChainAPICall := time.Now()
validatorSet, totalValidatorWeight, err := n.validatorClient.GetCurrentCanonicalValidatorSet(subnetID)
Expand Down Expand Up @@ -240,7 +262,7 @@ func (n *AppRequestNetwork) ConnectToCanonicalValidators(subnetID ids.ID) (*Conn
}, nil
}

func (n *AppRequestNetwork) Send(
func (n *appRequestNetwork) Send(
msg message.OutboundMessage,
nodeIDs set.Set[ids.NodeID],
subnetID ids.ID,
Expand All @@ -249,24 +271,24 @@ func (n *AppRequestNetwork) Send(
return n.network.Send(msg, avagoCommon.SendConfig{NodeIDs: nodeIDs}, subnetID, allower)
}

func (n *AppRequestNetwork) RegisterAppRequest(requestID ids.RequestID) {
func (n *appRequestNetwork) RegisterAppRequest(requestID ids.RequestID) {
n.handler.RegisterAppRequest(requestID)
}
func (n *AppRequestNetwork) RegisterRequestID(requestID uint32, numExpectedResponse int) chan message.InboundMessage {
func (n *appRequestNetwork) RegisterRequestID(requestID uint32, numExpectedResponse int) chan message.InboundMessage {
return n.handler.RegisterRequestID(requestID, numExpectedResponse)
}
func (n *AppRequestNetwork) GetSubnetID(blockchainID ids.ID) (ids.ID, error) {
func (n *appRequestNetwork) GetSubnetID(blockchainID ids.ID) (ids.ID, error) {
return n.validatorClient.GetSubnetID(context.Background(), blockchainID)
}

//
// Metrics
//

func (n *AppRequestNetwork) setInfoAPICallLatencyMS(latency float64) {
func (n *appRequestNetwork) setInfoAPICallLatencyMS(latency float64) {
n.metrics.infoAPICallLatencyMS.Observe(latency)
}

func (n *AppRequestNetwork) setPChainAPICallLatencyMS(latency float64) {
func (n *appRequestNetwork) setPChainAPICallLatencyMS(latency float64) {
n.metrics.pChainAPICallLatencyMS.Observe(latency)
}
128 changes: 128 additions & 0 deletions peers/mocks/mock_app_request_network.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions relayer/application_relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type CheckpointManager interface {
type ApplicationRelayer struct {
logger logging.Logger
metrics *ApplicationRelayerMetrics
network *peers.AppRequestNetwork
network peers.AppRequestNetwork
sourceBlockchain config.SourceBlockchain
signingSubnetID ids.ID
destinationClient vms.DestinationClient
Expand All @@ -71,7 +71,7 @@ type ApplicationRelayer struct {
func NewApplicationRelayer(
logger logging.Logger,
metrics *ApplicationRelayerMetrics,
network *peers.AppRequestNetwork,
network peers.AppRequestNetwork,
relayerID database.RelayerID,
destinationClient vms.DestinationClient,
sourceBlockchain config.SourceBlockchain,
Expand Down
4 changes: 2 additions & 2 deletions relayer/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ func createApplicationRelayers(
relayerMetrics *relayer.ApplicationRelayerMetrics,
db database.RelayerDatabase,
ticker *utils.Ticker,
network *peers.AppRequestNetwork,
network peers.AppRequestNetwork,
cfg *config.Config,
sourceClients map[ids.ID]ethclient.Client,
destinationClients map[ids.ID]vms.DestinationClient,
Expand Down Expand Up @@ -426,7 +426,7 @@ func createApplicationRelayersForSourceChain(
db database.RelayerDatabase,
ticker *utils.Ticker,
sourceBlockchain config.SourceBlockchain,
network *peers.AppRequestNetwork,
network peers.AppRequestNetwork,
cfg *config.Config,
currentHeight uint64,
destinationClients map[ids.ID]vms.DestinationClient,
Expand Down
6 changes: 3 additions & 3 deletions relayer/network_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
// or if the subnet supports all destinations, by the quora of all configured destinations.
func InitializeConnectionsAndCheckStake(
logger logging.Logger,
network *peers.AppRequestNetwork,
network peers.AppRequestNetwork,
cfg *config.Config,
) error {
for _, sourceBlockchain := range cfg.SourceBlockchains {
Expand All @@ -53,7 +53,7 @@ func InitializeConnectionsAndCheckStake(
// verify that we have connected to a threshold of stake.
func connectToNonPrimaryNetworkPeers(
logger logging.Logger,
network *peers.AppRequestNetwork,
network peers.AppRequestNetwork,
cfg *config.Config,
sourceBlockchain *config.SourceBlockchain,
) error {
Expand Down Expand Up @@ -87,7 +87,7 @@ func connectToNonPrimaryNetworkPeers(
// to a threshold of stake for each blockchain.
func connectToPrimaryNetworkPeers(
logger logging.Logger,
network *peers.AppRequestNetwork,
network peers.AppRequestNetwork,
cfg *config.Config,
sourceBlockchain *config.SourceBlockchain,
) error {
Expand Down
15 changes: 15 additions & 0 deletions scripts/generate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env bash
# Copyright (C) 2024, Ava Labs, Inc. All rights reserved.
# See the file LICENSE for licensing terms.

set -e errexit

# Root directory
root=$(
cd "$(dirname "${BASH_SOURCE[0]}")"
cd .. && pwd
)

source "$root"/scripts/versions.sh
go install -v "go.uber.org/mock/mockgen@$(getDepVersion go.uber.org/mock)"
PATH="$PATH:$(go env GOPATH)/bin" go generate ./...
6 changes: 4 additions & 2 deletions scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ if [ "$HELP" = true ]; then
fi

# Directory above this script
RELAYER_PATH=$(
root=$(
cd "$(dirname "${BASH_SOURCE[0]}")"
cd .. && pwd
)
source "$RELAYER_PATH"/scripts/constants.sh
source "$root"/scripts/constants.sh

go build -o tests/cmd/decider/decider ./tests/cmd/decider/

"$root"/scripts/generate.sh

go test -tags testing $VERBOSE ./...
Loading

0 comments on commit ccc7a60

Please sign in to comment.