From b11424783659ad7608d60b5d16a544cfa1beb98b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Kapka?= Date: Fri, 25 Jun 2021 13:02:11 +0200 Subject: [PATCH] Move common gateway registration code to new package (#9092) * Move common gateway registration code to new package * no shared inside of beaconchain * beacon gateway Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com> Co-authored-by: Raul Jordan --- beacon-chain/gateway/BUILD.bazel | 27 +++++++++ beacon-chain/gateway/helpers.go | 87 ++++++++++++++++++++++++++++ beacon-chain/gateway/helpers_test.go | 36 ++++++++++++ beacon-chain/node/BUILD.bazel | 6 +- beacon-chain/node/node.go | 70 ++-------------------- beacon-chain/server/BUILD.bazel | 6 +- beacon-chain/server/main.go | 69 ++-------------------- 7 files changed, 160 insertions(+), 141 deletions(-) create mode 100644 beacon-chain/gateway/BUILD.bazel create mode 100644 beacon-chain/gateway/helpers.go create mode 100644 beacon-chain/gateway/helpers_test.go diff --git a/beacon-chain/gateway/BUILD.bazel b/beacon-chain/gateway/BUILD.bazel new file mode 100644 index 000000000000..c91b73a9a016 --- /dev/null +++ b/beacon-chain/gateway/BUILD.bazel @@ -0,0 +1,27 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_test") +load("@prysm//tools/go:def.bzl", "go_library") + +go_library( + name = "go_default_library", + srcs = ["helpers.go"], + importpath = "github.com/prysmaticlabs/prysm/beacon-chain/gateway", + visibility = ["//beacon-chain:__subpackages__"], + deps = [ + "//proto/beacon/rpc/v1:go_default_library", + "//proto/eth/v1:go_default_library", + "//proto/eth/v1alpha1:go_default_library", + "//shared/gateway:go_default_library", + "@com_github_grpc_ecosystem_grpc_gateway_v2//runtime:go_default_library", + "@org_golang_google_protobuf//encoding/protojson:go_default_library", + ], +) + +go_test( + name = "go_default_test", + srcs = ["helpers_test.go"], + embed = [":go_default_library"], + deps = [ + "//shared/testutil/assert:go_default_library", + "//shared/testutil/require:go_default_library", + ], +) diff --git a/beacon-chain/gateway/helpers.go b/beacon-chain/gateway/helpers.go new file mode 100644 index 000000000000..027b6c24f483 --- /dev/null +++ b/beacon-chain/gateway/helpers.go @@ -0,0 +1,87 @@ +package gateway + +import ( + "net/http" + + gwruntime "github.com/grpc-ecosystem/grpc-gateway/v2/runtime" + pbrpc "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1" + ethpbv1 "github.com/prysmaticlabs/prysm/proto/eth/v1" + ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1" + "github.com/prysmaticlabs/prysm/shared/gateway" + "google.golang.org/protobuf/encoding/protojson" +) + +// MuxConfig contains configuration that should be used when registering the beacon node in the gateway. +type MuxConfig struct { + Handler gateway.MuxHandler + V1PbMux gateway.PbMux + V1Alpha1PbMux gateway.PbMux +} + +// DefaultConfig returns a fully configured MuxConfig with standard gateway behavior. +func DefaultConfig(enableDebugRPCEndpoints bool) MuxConfig { + v1Alpha1Registrations := []gateway.PbHandlerRegistration{ + ethpb.RegisterNodeHandler, + ethpb.RegisterBeaconChainHandler, + ethpb.RegisterBeaconNodeValidatorHandler, + pbrpc.RegisterHealthHandler, + } + v1Registrations := []gateway.PbHandlerRegistration{ + ethpbv1.RegisterBeaconNodeHandler, + ethpbv1.RegisterBeaconChainHandler, + ethpbv1.RegisterBeaconValidatorHandler, + ethpbv1.RegisterEventsHandler, + } + if enableDebugRPCEndpoints { + v1Alpha1Registrations = append(v1Alpha1Registrations, pbrpc.RegisterDebugHandler) + v1Registrations = append(v1Registrations, ethpbv1.RegisterBeaconDebugHandler) + + } + v1Alpha1Mux := gwruntime.NewServeMux( + gwruntime.WithMarshalerOption(gwruntime.MIMEWildcard, &gwruntime.HTTPBodyMarshaler{ + Marshaler: &gwruntime.JSONPb{ + MarshalOptions: protojson.MarshalOptions{ + EmitUnpopulated: true, + }, + UnmarshalOptions: protojson.UnmarshalOptions{ + DiscardUnknown: true, + }, + }, + }), + gwruntime.WithMarshalerOption( + "text/event-stream", &gwruntime.EventSourceJSONPb{}, + ), + ) + v1Mux := gwruntime.NewServeMux( + gwruntime.WithMarshalerOption(gwruntime.MIMEWildcard, &gwruntime.HTTPBodyMarshaler{ + Marshaler: &gwruntime.JSONPb{ + MarshalOptions: protojson.MarshalOptions{ + UseProtoNames: true, + EmitUnpopulated: true, + }, + UnmarshalOptions: protojson.UnmarshalOptions{ + DiscardUnknown: true, + }, + }, + }), + ) + muxHandler := func(h http.Handler, w http.ResponseWriter, req *http.Request) { + h.ServeHTTP(w, req) + } + v1Alpha1PbHandler := gateway.PbMux{ + Registrations: v1Alpha1Registrations, + Patterns: []string{"/eth/v1alpha1/"}, + Mux: v1Alpha1Mux, + } + v1PbHandler := gateway.PbMux{ + Registrations: v1Registrations, + Patterns: []string{"/eth/v1/"}, + Mux: v1Mux, + } + + return MuxConfig{ + Handler: muxHandler, + V1PbMux: v1PbHandler, + V1Alpha1PbMux: v1Alpha1PbHandler, + } +} diff --git a/beacon-chain/gateway/helpers_test.go b/beacon-chain/gateway/helpers_test.go new file mode 100644 index 000000000000..02399f1f7123 --- /dev/null +++ b/beacon-chain/gateway/helpers_test.go @@ -0,0 +1,36 @@ +package gateway + +import ( + "testing" + + "github.com/prysmaticlabs/prysm/shared/testutil/assert" + "github.com/prysmaticlabs/prysm/shared/testutil/require" +) + +func TestDefaultConfig(t *testing.T) { + t.Run("Without debug endpoints", func(t *testing.T) { + cfg := DefaultConfig(false) + assert.NotNil(t, cfg.Handler) + assert.NotNil(t, cfg.V1PbMux.Mux) + require.Equal(t, 1, len(cfg.V1PbMux.Patterns)) + assert.Equal(t, "/eth/v1/", cfg.V1PbMux.Patterns[0]) + assert.Equal(t, 4, len(cfg.V1PbMux.Registrations)) + assert.NotNil(t, cfg.V1Alpha1PbMux.Mux) + require.Equal(t, 1, len(cfg.V1Alpha1PbMux.Patterns)) + assert.Equal(t, "/eth/v1alpha1/", cfg.V1Alpha1PbMux.Patterns[0]) + assert.Equal(t, 4, len(cfg.V1Alpha1PbMux.Registrations)) + }) + + t.Run("With debug endpoints", func(t *testing.T) { + cfg := DefaultConfig(true) + assert.NotNil(t, cfg.Handler) + assert.NotNil(t, cfg.V1PbMux.Mux) + require.Equal(t, 1, len(cfg.V1PbMux.Patterns)) + assert.Equal(t, "/eth/v1/", cfg.V1PbMux.Patterns[0]) + assert.Equal(t, 5, len(cfg.V1PbMux.Registrations)) + assert.NotNil(t, cfg.V1Alpha1PbMux.Mux) + require.Equal(t, 1, len(cfg.V1Alpha1PbMux.Patterns)) + assert.Equal(t, "/eth/v1alpha1/", cfg.V1Alpha1PbMux.Patterns[0]) + assert.Equal(t, 5, len(cfg.V1Alpha1PbMux.Registrations)) + }) +} diff --git a/beacon-chain/node/BUILD.bazel b/beacon-chain/node/BUILD.bazel index edad3e61cc67..762bca657a63 100644 --- a/beacon-chain/node/BUILD.bazel +++ b/beacon-chain/node/BUILD.bazel @@ -22,6 +22,7 @@ go_library( "//beacon-chain/db/kv:go_default_library", "//beacon-chain/forkchoice:go_default_library", "//beacon-chain/forkchoice/protoarray:go_default_library", + "//beacon-chain/gateway:go_default_library", "//beacon-chain/interop-cold-start:go_default_library", "//beacon-chain/node/registration:go_default_library", "//beacon-chain/operations/attestations:go_default_library", @@ -35,9 +36,6 @@ go_library( "//beacon-chain/sync:go_default_library", "//beacon-chain/sync/initial-sync:go_default_library", "//cmd/beacon-chain/flags:go_default_library", - "//proto/beacon/rpc/v1:go_default_library", - "//proto/eth/v1:go_default_library", - "//proto/eth/v1alpha1:go_default_library", "//shared:go_default_library", "//shared/backuputil:go_default_library", "//shared/cmd:go_default_library", @@ -52,13 +50,11 @@ go_library( "//shared/tracing:go_default_library", "//shared/version:go_default_library", "@com_github_ethereum_go_ethereum//common:go_default_library", - "@com_github_grpc_ecosystem_grpc_gateway_v2//runtime:go_default_library", "@com_github_pkg_errors//:go_default_library", "@com_github_prometheus_client_golang//prometheus:go_default_library", "@com_github_prysmaticlabs_eth2_types//:go_default_library", "@com_github_sirupsen_logrus//:go_default_library", "@com_github_urfave_cli_v2//:go_default_library", - "@org_golang_google_protobuf//encoding/protojson:go_default_library", ], ) diff --git a/beacon-chain/node/node.go b/beacon-chain/node/node.go index a986dcf1b37d..551a907a931b 100644 --- a/beacon-chain/node/node.go +++ b/beacon-chain/node/node.go @@ -7,7 +7,6 @@ import ( "bytes" "context" "fmt" - "net/http" "os" "os/signal" "path/filepath" @@ -16,7 +15,6 @@ import ( "syscall" "github.com/ethereum/go-ethereum/common" - gwruntime "github.com/grpc-ecosystem/grpc-gateway/v2/runtime" "github.com/pkg/errors" "github.com/prysmaticlabs/prysm/beacon-chain/blockchain" "github.com/prysmaticlabs/prysm/beacon-chain/cache/depositcache" @@ -25,6 +23,7 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/db/kv" "github.com/prysmaticlabs/prysm/beacon-chain/forkchoice" "github.com/prysmaticlabs/prysm/beacon-chain/forkchoice/protoarray" + gateway2 "github.com/prysmaticlabs/prysm/beacon-chain/gateway" interopcoldstart "github.com/prysmaticlabs/prysm/beacon-chain/interop-cold-start" "github.com/prysmaticlabs/prysm/beacon-chain/node/registration" "github.com/prysmaticlabs/prysm/beacon-chain/operations/attestations" @@ -38,9 +37,6 @@ import ( regularsync "github.com/prysmaticlabs/prysm/beacon-chain/sync" initialsync "github.com/prysmaticlabs/prysm/beacon-chain/sync/initial-sync" "github.com/prysmaticlabs/prysm/cmd/beacon-chain/flags" - pbrpc "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1" - ethpbv1 "github.com/prysmaticlabs/prysm/proto/eth/v1" - ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1" "github.com/prysmaticlabs/prysm/shared" "github.com/prysmaticlabs/prysm/shared/backuputil" "github.com/prysmaticlabs/prysm/shared/cmd" @@ -55,7 +51,6 @@ import ( "github.com/prysmaticlabs/prysm/shared/version" "github.com/sirupsen/logrus" "github.com/urfave/cli/v2" - "google.golang.org/protobuf/encoding/protojson" ) const testSkipPowFlag = "test-skip-pow" @@ -661,69 +656,12 @@ func (b *BeaconNode) registerGRPCGateway() error { selfCert := b.cliCtx.String(flags.CertFlag.Name) maxCallSize := b.cliCtx.Uint64(cmd.GrpcMaxCallRecvMsgSizeFlag.Name) - v1Alpha1Registrations := []gateway.PbHandlerRegistration{ - ethpb.RegisterNodeHandler, - ethpb.RegisterBeaconChainHandler, - ethpb.RegisterBeaconNodeValidatorHandler, - pbrpc.RegisterHealthHandler, - } - v1Registrations := []gateway.PbHandlerRegistration{ - ethpbv1.RegisterBeaconNodeHandler, - ethpbv1.RegisterBeaconChainHandler, - ethpbv1.RegisterBeaconValidatorHandler, - ethpbv1.RegisterEventsHandler, - } - if enableDebugRPCEndpoints { - v1Alpha1Registrations = append(v1Alpha1Registrations, pbrpc.RegisterDebugHandler) - v1Registrations = append(v1Registrations, ethpbv1.RegisterBeaconDebugHandler) - - } - v1Alpha1Mux := gwruntime.NewServeMux( - gwruntime.WithMarshalerOption(gwruntime.MIMEWildcard, &gwruntime.HTTPBodyMarshaler{ - Marshaler: &gwruntime.JSONPb{ - MarshalOptions: protojson.MarshalOptions{ - EmitUnpopulated: true, - }, - UnmarshalOptions: protojson.UnmarshalOptions{ - DiscardUnknown: true, - }, - }, - }), - gwruntime.WithMarshalerOption( - "text/event-stream", &gwruntime.EventSourceJSONPb{}, - ), - ) - v1Mux := gwruntime.NewServeMux( - gwruntime.WithMarshalerOption(gwruntime.MIMEWildcard, &gwruntime.HTTPBodyMarshaler{ - Marshaler: &gwruntime.JSONPb{ - MarshalOptions: protojson.MarshalOptions{ - UseProtoNames: true, - EmitUnpopulated: true, - }, - UnmarshalOptions: protojson.UnmarshalOptions{ - DiscardUnknown: true, - }, - }, - }), - ) - muxHandler := func(h http.Handler, w http.ResponseWriter, req *http.Request) { - h.ServeHTTP(w, req) - } - v1Alpha1PbHandler := gateway.PbMux{ - Registrations: v1Alpha1Registrations, - Patterns: []string{"/eth/v1alpha1/"}, - Mux: v1Alpha1Mux, - } - v1PbHandler := gateway.PbMux{ - Registrations: v1Registrations, - Patterns: []string{"/eth/v1/"}, - Mux: v1Mux, - } + gatewayConfig := gateway2.DefaultConfig(enableDebugRPCEndpoints) g := gateway.New( b.ctx, - []gateway.PbMux{v1Alpha1PbHandler, v1PbHandler}, - muxHandler, + []gateway.PbMux{gatewayConfig.V1Alpha1PbMux, gatewayConfig.V1PbMux}, + gatewayConfig.Handler, selfAddress, gatewayAddress, ).WithAllowedOrigins(allowedOrigins). diff --git a/beacon-chain/server/BUILD.bazel b/beacon-chain/server/BUILD.bazel index e730245b8863..92f417a15bbd 100644 --- a/beacon-chain/server/BUILD.bazel +++ b/beacon-chain/server/BUILD.bazel @@ -10,16 +10,12 @@ go_library( importpath = "github.com/prysmaticlabs/prysm/beacon-chain/server", visibility = ["//visibility:private"], deps = [ + "//beacon-chain/gateway:go_default_library", "//beacon-chain/rpc/apimiddleware:go_default_library", - "//proto/beacon/rpc/v1:go_default_library", - "//proto/eth/v1:go_default_library", - "//proto/eth/v1alpha1:go_default_library", "//shared/gateway:go_default_library", "//shared/maxprocs:go_default_library", - "@com_github_grpc_ecosystem_grpc_gateway_v2//runtime:go_default_library", "@com_github_joonix_log//:go_default_library", "@com_github_sirupsen_logrus//:go_default_library", - "@org_golang_google_protobuf//encoding/protojson:go_default_library", ], ) diff --git a/beacon-chain/server/main.go b/beacon-chain/server/main.go index f45ed3296add..c83bf899fe8e 100644 --- a/beacon-chain/server/main.go +++ b/beacon-chain/server/main.go @@ -9,16 +9,12 @@ import ( "net/http" "strings" - gwruntime "github.com/grpc-ecosystem/grpc-gateway/v2/runtime" joonix "github.com/joonix/log" + beaconGateway "github.com/prysmaticlabs/prysm/beacon-chain/gateway" "github.com/prysmaticlabs/prysm/beacon-chain/rpc/apimiddleware" - pbrpc "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1" - ethpbv1 "github.com/prysmaticlabs/prysm/proto/eth/v1" - ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1" "github.com/prysmaticlabs/prysm/shared/gateway" _ "github.com/prysmaticlabs/prysm/shared/maxprocs" "github.com/sirupsen/logrus" - "google.golang.org/protobuf/encoding/protojson" ) var ( @@ -42,69 +38,12 @@ func main() { log.SetLevel(logrus.DebugLevel) } - v1Alpha1Registrations := []gateway.PbHandlerRegistration{ - ethpb.RegisterNodeHandler, - ethpb.RegisterBeaconChainHandler, - ethpb.RegisterBeaconNodeValidatorHandler, - pbrpc.RegisterHealthHandler, - } - v1Registrations := []gateway.PbHandlerRegistration{ - ethpbv1.RegisterBeaconNodeHandler, - ethpbv1.RegisterBeaconChainHandler, - ethpbv1.RegisterBeaconValidatorHandler, - ethpbv1.RegisterEventsHandler, - } - if *enableDebugRPCEndpoints { - v1Alpha1Registrations = append(v1Alpha1Registrations, pbrpc.RegisterDebugHandler) - v1Registrations = append(v1Registrations, ethpbv1.RegisterBeaconDebugHandler) - - } - v1Alpha1Mux := gwruntime.NewServeMux( - gwruntime.WithMarshalerOption(gwruntime.MIMEWildcard, &gwruntime.HTTPBodyMarshaler{ - Marshaler: &gwruntime.JSONPb{ - MarshalOptions: protojson.MarshalOptions{ - EmitUnpopulated: true, - }, - UnmarshalOptions: protojson.UnmarshalOptions{ - DiscardUnknown: true, - }, - }, - }), - gwruntime.WithMarshalerOption( - "text/event-stream", &gwruntime.EventSourceJSONPb{}, - ), - ) - v1Mux := gwruntime.NewServeMux( - gwruntime.WithMarshalerOption(gwruntime.MIMEWildcard, &gwruntime.HTTPBodyMarshaler{ - Marshaler: &gwruntime.JSONPb{ - MarshalOptions: protojson.MarshalOptions{ - UseProtoNames: true, - EmitUnpopulated: true, - }, - UnmarshalOptions: protojson.UnmarshalOptions{ - DiscardUnknown: true, - }, - }, - }), - ) - muxHandler := func(h http.Handler, w http.ResponseWriter, req *http.Request) { - h.ServeHTTP(w, req) - } - v1Alpha1PbHandler := gateway.PbMux{ - Registrations: v1Alpha1Registrations, - Patterns: []string{"/eth/v1alpha1/"}, - Mux: v1Alpha1Mux, - } - v1PbHandler := gateway.PbMux{ - Registrations: v1Registrations, - Patterns: []string{"/eth/v1/"}, - Mux: v1Mux, - } + gatewayConfig := beaconGateway.DefaultConfig(*enableDebugRPCEndpoints) gw := gateway.New( context.Background(), - []gateway.PbMux{v1Alpha1PbHandler, v1PbHandler}, - muxHandler, + []gateway.PbMux{gatewayConfig.V1Alpha1PbMux, gatewayConfig.V1PbMux}, + gatewayConfig.Handler, *beaconRPC, fmt.Sprintf("%s:%d", *host, *port), ).WithAllowedOrigins(strings.Split(*allowedOrigins, ",")).