github.com/prysmaticlabs/prysm@v1.4.4/beacon-chain/gateway/helpers.go (about)

     1  package gateway
     2  
     3  import (
     4  	gwruntime "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
     5  	pbrpc "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1"
     6  	ethpbv1 "github.com/prysmaticlabs/prysm/proto/eth/v1"
     7  	ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
     8  	"github.com/prysmaticlabs/prysm/shared/gateway"
     9  	"google.golang.org/protobuf/encoding/protojson"
    10  )
    11  
    12  // MuxConfig contains configuration that should be used when registering the beacon node in the gateway.
    13  type MuxConfig struct {
    14  	Handler       gateway.MuxHandler
    15  	V1PbMux       gateway.PbMux
    16  	V1Alpha1PbMux gateway.PbMux
    17  }
    18  
    19  // DefaultConfig returns a fully configured MuxConfig with standard gateway behavior.
    20  func DefaultConfig(enableDebugRPCEndpoints bool) MuxConfig {
    21  	v1Alpha1Registrations := []gateway.PbHandlerRegistration{
    22  		ethpb.RegisterNodeHandler,
    23  		ethpb.RegisterBeaconChainHandler,
    24  		ethpb.RegisterBeaconNodeValidatorHandler,
    25  		pbrpc.RegisterHealthHandler,
    26  	}
    27  	v1Registrations := []gateway.PbHandlerRegistration{
    28  		ethpbv1.RegisterBeaconNodeHandler,
    29  		ethpbv1.RegisterBeaconChainHandler,
    30  		ethpbv1.RegisterBeaconValidatorHandler,
    31  		ethpbv1.RegisterEventsHandler,
    32  	}
    33  	if enableDebugRPCEndpoints {
    34  		v1Alpha1Registrations = append(v1Alpha1Registrations, pbrpc.RegisterDebugHandler)
    35  		v1Registrations = append(v1Registrations, ethpbv1.RegisterBeaconDebugHandler)
    36  
    37  	}
    38  	v1Alpha1Mux := gwruntime.NewServeMux(
    39  		gwruntime.WithMarshalerOption(gwruntime.MIMEWildcard, &gwruntime.HTTPBodyMarshaler{
    40  			Marshaler: &gwruntime.JSONPb{
    41  				MarshalOptions: protojson.MarshalOptions{
    42  					EmitUnpopulated: true,
    43  				},
    44  				UnmarshalOptions: protojson.UnmarshalOptions{
    45  					DiscardUnknown: true,
    46  				},
    47  			},
    48  		}),
    49  		gwruntime.WithMarshalerOption(
    50  			"text/event-stream", &gwruntime.EventSourceJSONPb{},
    51  		),
    52  	)
    53  	v1Mux := gwruntime.NewServeMux(
    54  		gwruntime.WithMarshalerOption(gwruntime.MIMEWildcard, &gwruntime.HTTPBodyMarshaler{
    55  			Marshaler: &gwruntime.JSONPb{
    56  				MarshalOptions: protojson.MarshalOptions{
    57  					UseProtoNames:   true,
    58  					EmitUnpopulated: true,
    59  				},
    60  				UnmarshalOptions: protojson.UnmarshalOptions{
    61  					DiscardUnknown: true,
    62  				},
    63  			},
    64  		}),
    65  	)
    66  	v1Alpha1PbHandler := gateway.PbMux{
    67  		Registrations: v1Alpha1Registrations,
    68  		Patterns:      []string{"/eth/v1alpha1/"},
    69  		Mux:           v1Alpha1Mux,
    70  	}
    71  	v1PbHandler := gateway.PbMux{
    72  		Registrations: v1Registrations,
    73  		Patterns:      []string{"/eth/v1/"},
    74  		Mux:           v1Mux,
    75  	}
    76  
    77  	return MuxConfig{
    78  		V1PbMux:       v1PbHandler,
    79  		V1Alpha1PbMux: v1Alpha1PbHandler,
    80  	}
    81  }