Skip to content

Commit

Permalink
Merge branch 'master' into ci-monitoring-actions
Browse files Browse the repository at this point in the history
  • Loading branch information
marun authored Jul 9, 2024
2 parents ea1b24c + 2af3890 commit ca36d5f
Show file tree
Hide file tree
Showing 65 changed files with 311 additions and 53 deletions.
5 changes: 5 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ run:
# By default, it isn't set.
modules-download-mode: readonly

# Include non-test files tagged as test-only.
# Context: https://github.com/ava-labs/avalanchego/pull/3173
build-tags:
- test

output:
# Make issues output unique by line.
# Default: true
Expand Down
11 changes: 11 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"gopls": {
"build.buildFlags": [
// Context: https://github.com/ava-labs/avalanchego/pull/3173
// Without this tag, the language server won't build the test-only
// code in non-_test.go files.
"--tags='test'",
],
},
"go.testTags": "test",
}
2 changes: 2 additions & 0 deletions cache/test_cacher.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

//go:build test

package cache

import (
Expand Down
2 changes: 2 additions & 0 deletions chains/atomic/test_shared_memory.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

//go:build test

package atomic

import (
Expand Down
2 changes: 2 additions & 0 deletions codec/test_codec.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

//go:build test

package codec

import (
Expand Down
2 changes: 2 additions & 0 deletions database/benchmark_database.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

//go:build test

package database

import (
Expand Down
2 changes: 2 additions & 0 deletions database/test_database.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

//go:build test

package database

import (
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/DataDog/zstd v1.5.2
github.com/NYTimes/gziphandler v1.1.1
github.com/antithesishq/antithesis-sdk-go v0.3.8
github.com/ava-labs/coreth v0.13.6-rc.1
github.com/ava-labs/coreth v0.13.6-rc.1.0.20240702201359-ba2ce5367874
github.com/ava-labs/ledger-avalanche/go v0.0.0-20240610153809-9c955cc90a95
github.com/btcsuite/btcd/btcutil v1.1.3
github.com/cockroachdb/pebble v0.0.0-20230928194634-aa077af62593
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156/go.mod h1:Cb/ax
github.com/antithesishq/antithesis-sdk-go v0.3.8 h1:OvGoHxIcOXFJLyn9IJQ5DzByZ3YVAWNBc394ObzDRb8=
github.com/antithesishq/antithesis-sdk-go v0.3.8/go.mod h1:IUpT2DPAKh6i/YhSbt6Gl3v2yvUZjmKncl7U91fup7E=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/ava-labs/coreth v0.13.6-rc.1 h1:gRXRokmu0WOlPqyx+mTLWB655e8/w++u6qFcq9Mo7qA=
github.com/ava-labs/coreth v0.13.6-rc.1/go.mod h1:vm9T8qzP7RLo/jR2MKkliPfaiGgWeEpu/PG6fvvPmog=
github.com/ava-labs/coreth v0.13.6-rc.1.0.20240702201359-ba2ce5367874 h1:aTDg0jvO07EvUvBYebmLO25bffe1DAaZZPPL0ooGhIA=
github.com/ava-labs/coreth v0.13.6-rc.1.0.20240702201359-ba2ce5367874/go.mod h1:VhNDxZBsqZQQaUTmIkzdyY8UicIsoTDXlRmPaPL9lkA=
github.com/ava-labs/ledger-avalanche/go v0.0.0-20240610153809-9c955cc90a95 h1:dOVbtdnZL++pENdTCNZ1nu41eYDQkTML4sWebDnnq8c=
github.com/ava-labs/ledger-avalanche/go v0.0.0-20240610153809-9c955cc90a95/go.mod h1:pJxaT9bUgeRNVmNRgtCHb7sFDIRKy7CzTQVi8gGNT6g=
github.com/aymerick/raymond v2.0.3-0.20180322193309-b565731e1464+incompatible/go.mod h1:osfaiScAUVup+UC9Nfq76eWqDhXlp+4UYaA8uhTBO6g=
Expand Down
2 changes: 2 additions & 0 deletions ids/test_aliases.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

//go:build test

package ids

import "github.com/stretchr/testify/require"
Expand Down
33 changes: 33 additions & 0 deletions network/p2p/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package p2p

import "github.com/ava-labs/avalanchego/snow/engine/common"

var (
// ErrUnexpected should be used to indicate that a request failed due to a
// generic error
ErrUnexpected = &common.AppError{
Code: -1,
Message: "unexpected error",
}
// ErrUnregisteredHandler should be used to indicate that a request failed
// due to it not matching a registered handler
ErrUnregisteredHandler = &common.AppError{
Code: -2,
Message: "unregistered handler",
}
// ErrNotValidator should be used to indicate that a request failed due to
// the requesting peer not being a validator
ErrNotValidator = &common.AppError{
Code: -3,
Message: "not a validator",
}
// ErrThrottled should be used to indicate that a request failed due to the
// requesting peer exceeding a rate limit
ErrThrottled = &common.AppError{
Code: -4,
Message: "throttled",
}
)
16 changes: 11 additions & 5 deletions network/p2p/gossip/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/network/p2p"
"github.com/ava-labs/avalanchego/snow/engine/common"
"github.com/ava-labs/avalanchego/utils/bloom"
"github.com/ava-labs/avalanchego/utils/logging"
)
Expand Down Expand Up @@ -43,10 +44,10 @@ type Handler[T Gossipable] struct {
targetResponseSize int
}

func (h Handler[T]) AppRequest(_ context.Context, _ ids.NodeID, _ time.Time, requestBytes []byte) ([]byte, error) {
func (h Handler[T]) AppRequest(_ context.Context, _ ids.NodeID, _ time.Time, requestBytes []byte) ([]byte, *common.AppError) {
filter, salt, err := ParseAppRequest(requestBytes)
if err != nil {
return nil, err
return nil, p2p.ErrUnexpected
}

responseSize := 0
Expand All @@ -73,14 +74,19 @@ func (h Handler[T]) AppRequest(_ context.Context, _ ids.NodeID, _ time.Time, req
return responseSize <= h.targetResponseSize
})
if err != nil {
return nil, err
return nil, p2p.ErrUnexpected
}

if err := h.metrics.observeMessage(sentPullLabels, len(gossipBytes), responseSize); err != nil {
return nil, err
return nil, p2p.ErrUnexpected
}

return MarshalAppResponse(gossipBytes)
response, err := MarshalAppResponse(gossipBytes)
if err != nil {
return nil, p2p.ErrUnexpected
}

return response, nil
}

func (h Handler[_]) AppGossip(_ context.Context, nodeID ids.NodeID, gossipBytes []byte) {
Expand Down
18 changes: 8 additions & 10 deletions network/p2p/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package p2p

import (
"context"
"errors"
"time"

"go.uber.org/zap"
Expand All @@ -17,8 +16,6 @@ import (
)

var (
ErrNotValidator = errors.New("not a validator")

_ Handler = (*NoOpHandler)(nil)
_ Handler = (*TestHandler)(nil)
_ Handler = (*ValidatorHandler)(nil)
Expand All @@ -33,13 +30,14 @@ type Handler interface {
gossipBytes []byte,
)
// AppRequest is called when handling an AppRequest message.
// Returns the bytes for the response corresponding to [requestBytes]
// Sends a response with the response corresponding to [requestBytes] or
// an application-defined error.
AppRequest(
ctx context.Context,
nodeID ids.NodeID,
deadline time.Time,
requestBytes []byte,
) ([]byte, error)
) ([]byte, *common.AppError)
// CrossChainAppRequest is called when handling a CrossChainAppRequest
// message.
// Returns the bytes for the response corresponding to [requestBytes]
Expand All @@ -56,7 +54,7 @@ type NoOpHandler struct{}

func (NoOpHandler) AppGossip(context.Context, ids.NodeID, []byte) {}

func (NoOpHandler) AppRequest(context.Context, ids.NodeID, time.Time, []byte) ([]byte, error) {
func (NoOpHandler) AppRequest(context.Context, ids.NodeID, time.Time, []byte) ([]byte, *common.AppError) {
return nil, nil
}

Expand Down Expand Up @@ -95,7 +93,7 @@ func (v ValidatorHandler) AppGossip(ctx context.Context, nodeID ids.NodeID, goss
v.handler.AppGossip(ctx, nodeID, gossipBytes)
}

func (v ValidatorHandler) AppRequest(ctx context.Context, nodeID ids.NodeID, deadline time.Time, requestBytes []byte) ([]byte, error) {
func (v ValidatorHandler) AppRequest(ctx context.Context, nodeID ids.NodeID, deadline time.Time, requestBytes []byte) ([]byte, *common.AppError) {
if !v.validatorSet.Has(ctx, nodeID) {
return nil, ErrNotValidator
}
Expand Down Expand Up @@ -128,7 +126,7 @@ func (r *responder) AppRequest(ctx context.Context, nodeID ids.NodeID, requestID
zap.Binary("message", request),
zap.Error(err),
)
return nil
return r.sender.SendAppError(ctx, nodeID, requestID, err.Code, err.Message)
}

return r.sender.SendAppResponse(ctx, nodeID, requestID, appResponse)
Expand All @@ -155,7 +153,7 @@ func (r *responder) CrossChainAppRequest(ctx context.Context, chainID ids.ID, re

type TestHandler struct {
AppGossipF func(ctx context.Context, nodeID ids.NodeID, gossipBytes []byte)
AppRequestF func(ctx context.Context, nodeID ids.NodeID, deadline time.Time, requestBytes []byte) ([]byte, error)
AppRequestF func(ctx context.Context, nodeID ids.NodeID, deadline time.Time, requestBytes []byte) ([]byte, *common.AppError)
CrossChainAppRequestF func(ctx context.Context, chainID ids.ID, deadline time.Time, requestBytes []byte) ([]byte, error)
}

Expand All @@ -167,7 +165,7 @@ func (t TestHandler) AppGossip(ctx context.Context, nodeID ids.NodeID, gossipByt
t.AppGossipF(ctx, nodeID, gossipBytes)
}

func (t TestHandler) AppRequest(ctx context.Context, nodeID ids.NodeID, deadline time.Time, requestBytes []byte) ([]byte, error) {
func (t TestHandler) AppRequest(ctx context.Context, nodeID ids.NodeID, deadline time.Time, requestBytes []byte) ([]byte, *common.AppError) {
if t.AppRequestF == nil {
return nil, nil
}
Expand Down
3 changes: 2 additions & 1 deletion network/p2p/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/engine/common"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/set"
)
Expand Down Expand Up @@ -79,7 +80,7 @@ func TestValidatorHandlerAppRequest(t *testing.T) {
name string
validatorSet ValidatorSet
nodeID ids.NodeID
expected error
expected *common.AppError
}{
{
name: "message dropped",
Expand Down
Loading

0 comments on commit ca36d5f

Please sign in to comment.