github.com/Finschia/finschia-sdk@v0.48.1/types/msgservice/msg_service.go (about)

     1  package msgservice
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"google.golang.org/grpc"
     8  
     9  	codectypes "github.com/Finschia/finschia-sdk/codec/types"
    10  	sdk "github.com/Finschia/finschia-sdk/types"
    11  )
    12  
    13  // RegisterMsgServiceDesc registers all type_urls from Msg services described
    14  // in `sd` into the registry.
    15  func RegisterMsgServiceDesc(registry codectypes.InterfaceRegistry, sd *grpc.ServiceDesc) {
    16  	// Adds a top-level type_url based on the Msg service name.
    17  	for _, method := range sd.Methods {
    18  		fqMethod := fmt.Sprintf("/%s/%s", sd.ServiceName, method.MethodName)
    19  		methodHandler := method.Handler
    20  
    21  		// NOTE: This is how we pull the concrete request type for each handler for registering in the InterfaceRegistry.
    22  		// This approach is maybe a bit hacky, but less hacky than reflecting on the handler object itself.
    23  		// We use a no-op interceptor to avoid actually calling into the handler itself.
    24  		_, _ = methodHandler(nil, context.Background(), func(i interface{}) error {
    25  			msg, ok := i.(sdk.Msg)
    26  			if !ok {
    27  				// We panic here because there is no other alternative and the app cannot be initialized correctly
    28  				// this should only happen if there is a problem with code generation in which case the app won't
    29  				// work correctly anyway.
    30  				panic(fmt.Errorf("can't register request type %T for service method %s", i, fqMethod))
    31  			}
    32  
    33  			registry.RegisterImplementations((*sdk.Msg)(nil), msg)
    34  
    35  			return nil
    36  		}, noopInterceptor)
    37  
    38  	}
    39  }
    40  
    41  // gRPC NOOP interceptor
    42  func noopInterceptor(_ context.Context, _ interface{}, _ *grpc.UnaryServerInfo, _ grpc.UnaryHandler) (interface{}, error) {
    43  	return nil, nil
    44  }