Skip to content

Commit

Permalink
Merge branch '__develop' into misc-api-fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rkapka committed Jun 25, 2021
2 parents 8a49d25 + b114247 commit 544ee5c
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 141 deletions.
27 changes: 27 additions & 0 deletions beacon-chain/gateway/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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",
],
)
87 changes: 87 additions & 0 deletions beacon-chain/gateway/helpers.go
Original file line number Diff line number Diff line change
@@ -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,
}
}
36 changes: 36 additions & 0 deletions beacon-chain/gateway/helpers_test.go
Original file line number Diff line number Diff line change
@@ -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))
})
}
6 changes: 1 addition & 5 deletions beacon-chain/node/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
],
)

Expand Down
70 changes: 4 additions & 66 deletions beacon-chain/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"bytes"
"context"
"fmt"
"net/http"
"os"
"os/signal"
"path/filepath"
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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).
Expand Down
6 changes: 1 addition & 5 deletions beacon-chain/server/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
)

Expand Down
Loading

0 comments on commit 544ee5c

Please sign in to comment.