Skip to content

Commit

Permalink
add AppReqNet mocks and a few initial tests
Browse files Browse the repository at this point in the history
  • Loading branch information
feuGeneA committed Aug 22, 2024
1 parent c1d6b8c commit 77cf6d0
Show file tree
Hide file tree
Showing 7 changed files with 268 additions and 0 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: Check generated code is up to date

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
2 changes: 2 additions & 0 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
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.

3 changes: 3 additions & 0 deletions scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
# 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
)

"$root"/scripts/generate.sh
"$root"/scripts/build_relayer.sh
"$root"/scripts/build_signature_aggregator.sh
9 changes: 9 additions & 0 deletions scripts/generate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash
# Copyright (C) 2024, Ava Labs, Inc. All rights reserved.
# See the file LICENSE for licensing terms.

set -e errexit

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 ./...
4 changes: 4 additions & 0 deletions signature-aggregator/aggregator/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,8 @@ func (s *SignatureAggregator) CreateSignedMessage(
return nil, errNotEnoughSignatures
}

// TODO: consider making this function private. its only reference seems to be
// within this module.
func (s *SignatureAggregator) GetSubnetID(blockchainID ids.ID) (ids.ID, error) {
s.subnetsMapLock.RLock()
subnetID, ok := s.subnetIDsByBlockchainID[blockchainID]
Expand All @@ -359,6 +361,8 @@ func (s *SignatureAggregator) GetSubnetID(blockchainID ids.ID) (ids.ID, error) {
return subnetID, nil
}

// TODO: consider making this function private. its only reference seems to be
// within this module.
func (s *SignatureAggregator) SetSubnetID(blockchainID ids.ID, subnetID ids.ID) {
s.subnetsMapLock.Lock()
s.subnetIDsByBlockchainID[blockchainID] = subnetID
Expand Down
87 changes: 87 additions & 0 deletions signature-aggregator/aggregator/aggregator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package aggregator

import (
"testing"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/message"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/vms/platformvm/warp"
"github.com/ava-labs/awm-relayer/peers"
"github.com/ava-labs/awm-relayer/peers/mocks"
"github.com/ava-labs/awm-relayer/signature-aggregator/metrics"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
)

var sigAggMetrics *metrics.SignatureAggregatorMetrics
var messageCreator message.Creator

func instantiateAggregator(t *testing.T) (
*SignatureAggregator,
*mocks.MockAppRequestNetwork,
) {
mockNetwork := mocks.NewMockAppRequestNetwork(gomock.NewController(t))
if sigAggMetrics == nil {
sigAggMetrics = metrics.NewSignatureAggregatorMetrics(prometheus.DefaultRegisterer)
}
if messageCreator == nil {
var err error
messageCreator, err = message.NewCreator(
logging.NoLog{},
prometheus.DefaultRegisterer,
constants.DefaultNetworkCompressionType,
constants.DefaultNetworkMaximumInboundTimeout,
)
require.Equal(t, err, nil)
}
aggregator, err := NewSignatureAggregator(
mockNetwork,
logging.NoLog{},
1024,
sigAggMetrics,
messageCreator,
)
require.Equal(t, err, nil)
return aggregator, mockNetwork
}

func TestCreateSignedMessageFailsWithNoValidators(t *testing.T) {
aggregator, mockNetwork := instantiateAggregator(t)
msg, err := warp.NewUnsignedMessage(0, ids.Empty, []byte{})
require.Equal(t, err, nil)
mockNetwork.EXPECT().GetSubnetID(ids.Empty).Return(ids.Empty, nil)
mockNetwork.EXPECT().ConnectToCanonicalValidators(ids.Empty).Return(
&peers.ConnectedCanonicalValidators{
ConnectedWeight: 0,
TotalValidatorWeight: 0,
ValidatorSet: []*warp.Validator{},
},
nil,
)
_, err = aggregator.CreateSignedMessage(msg, ids.Empty, 80)
require.ErrorContains(t, err, "no signatures")
}

func TestCreateSignedMessageFailsWithoutSufficientConnectedStake(t *testing.T) {
aggregator, mockNetwork := instantiateAggregator(t)
msg, err := warp.NewUnsignedMessage(0, ids.Empty, []byte{})
require.Equal(t, err, nil)
mockNetwork.EXPECT().GetSubnetID(ids.Empty).Return(ids.Empty, nil)
mockNetwork.EXPECT().ConnectToCanonicalValidators(ids.Empty).Return(
&peers.ConnectedCanonicalValidators{
ConnectedWeight: 0,
TotalValidatorWeight: 1,
ValidatorSet: []*warp.Validator{},
},
nil,
)
_, err = aggregator.CreateSignedMessage(msg, ids.Empty, 80)
require.ErrorContains(
t,
err,
"failed to connect to a threshold of stake",
)
}

0 comments on commit 77cf6d0

Please sign in to comment.