grpc

package
v0.0.0-...-16063ba Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 2, 2022 License: MIT Imports: 12 Imported by: 0

README

gRPC

Metadata gRPC API exposes on 7778 port. Link to proto file.

Endpoints

Metadata service implements following endpoints:

service MetadataService {
    rpc SubscribeOnMetadata(DefaultRequest) returns (stream SubscriptionMetadata);
    rpc UnsubscribeFromMetadata(UnsubscribeRequest) returns (UnsubscribeResponse);

    rpc GetMetadata(GetMetadataRequest) returns (Metadata);
    rpc ListMetadata(ListMetadataRequest) returns (ListMetadataResponse);
    rpc GetMetadataByMethodSinature(GetMetadataByMethodSinatureRequest) returns (ListMetadataResponse);
    rpc GetMetadataByTopic(GetMetadataByTopicRequest) returns (ListMetadataResponse);
}
  • SubscribeOnMetadata - subscribes on new metadata receiving events.
message DefaultRequest {}

// stream of Metadata

message SubscriptionMetadata {
    SubscribeResponse subscription = 1;
    Metadata metadata = 2;
}

message SubscribeResponse {
    uint64 id = 1;
}

message Metadata {
    string address = 1;
    bytes metadata = 2;
    bytes json_schema = 3;
}

  • UnsubscribeFromMetadata - unsubscribes from metadata stream
message UnsubscribeRequest {
    uint64 id = 1;
}

message UnsubscribeResponse {
    uint64 id = 1;
    Message response = 2;
}
message Message {
    string message = 1;
}
  • GetMetadata - receives ABI by contract address.
message GetMetadataRequest {
    string address = 1;
}
message Metadata {
    string address = 1;
    bytes metadata = 2;
    bytes json_schema = 3;
}
  • ListMetadata - receives all ABIs with pagination and sorting.
enum SortOrder {
    ASC = 0;
    DESC = 1;
}

message Page {
    uint64 limit = 1;
    uint64 offset = 2;
    SortOrder order = 3;
}

message ListMetadataRequest {
    Page page = 1;
}

message ListMetadataResponse {
    repeated Metadata metadata = 1;
}
  • GetMetadataByMethodSinature - receives all metadata contains certain method signature with sorting and pagination.
message GetMetadataByMethodSinatureRequest {
    Page page = 1;
    string signature = 2;
}
  • GetMetadataByTopic - receives all metadata contains certain method signature with sorting and pagination.
message GetMetadataByTopicRequest {
    Page page = 1;
    string topic = 2;
}

Usage

There are server and client modules in the package.

To run server write the following code which can be found here:

grpcModule, err := grpc.NewServer(cfg.GRPC.Server, storage)
if err != nil {
    log.Panic().Err(err).Msg("creating grpc module")
    cancel()
    return
}

metadataIndexer.Subscribe(grpcModule.Subscriber, messages.TopicMetadata)
grpcModule.Start(ctx)

// your code here

if err := grpcModule.Close(); err != nil {
    log.Panic().Err(err).Msg("closing grpc server")
}

To create client module write the following code:

grpcClient := grpc.NewClient(cfg.GRPC.Client)                       // create module

if err := grpcClient.Connect(ctx); err != nil {                     // create connection to server
    log.Panic().Err(err).Msg("Connect")
    return
}

grpcClient.Start(ctx)                                                          // listening for server events
id, err := grpcClient.SubscribeOnMetadata(ctx, yourModule.Subscriber)          // subscribe on internal events. retruns subscription id which required on unsubscribe.
if err != nil {
    log.Panic().Err(err).Msg("SubscribeOnMetadata")
    return
}

data, err := grpcClient.GetMetadata(ctx, "0x...")                   // receiving metadata by gRPC
if err != nil {
    log.Panic().Err(err).Msg("GetMetadata")
    return
}

data, err := grpcClient.ListMetadata(ctx, 10, 0, pb.SortOrder_ASC)  // receiving list metadata by gRPC
if err != nil {
    log.Panic().Err(err).Msg("ListMetadata")
    return
}

// your code here

if err := grpcClient.UnsubscribeFromMetadata(ctx, yourModule.Subscriber, id); err != nil {
    log.Panic().Err(err).Msg("UnsubscribeFromMetadata")
    return
}

if err := grpcClient.Close(); err != nil {
	log.Panic().Err(err).Msg("closing grpc client")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ListMetadataResponse

func ListMetadataResponse(metadata []*storage.Metadata) *pb.ListMetadataResponse

ListMetadataResponse -

func Metadata

func Metadata(metadata *storage.Metadata) *pb.Metadata

Metadata -

func MetadataRequest

func MetadataRequest() *generalPB.DefaultRequest

HeadRequest -

func SubscriptionMetadata

func SubscriptionMetadata(id uint64, metadata *storage.Metadata) *pb.SubscriptionMetadata

SubscriptionMetadata -

Types

type Client

type Client struct {
	*grpcModules.Client
	// contains filtered or unexported fields
}

Client -

func NewClient

func NewClient(cfg *ClientConfig) *Client

NewClient -

func (*Client) AttachTo

func (client *Client) AttachTo(name string, input *modules.Input) error

AttachTo -

func (*Client) GetMetadata

func (client *Client) GetMetadata(ctx context.Context, address string) (*pb.Metadata, error)

GetMetadata -

func (*Client) GetMetadataByMethodSinature

func (client *Client) GetMetadataByMethodSinature(ctx context.Context, limit, offset uint64, order generalPB.SortOrder, signature string) ([]*pb.Metadata, error)

GetMetadataByMethodSinature -

func (*Client) GetMetadataByTopic

func (client *Client) GetMetadataByTopic(ctx context.Context, limit, offset uint64, order generalPB.SortOrder, topic string) ([]*pb.Metadata, error)

GetMetadataByTopic -

func (*Client) Input

func (client *Client) Input(name string) (*modules.Input, error)

Input -

func (*Client) ListMetadata

func (client *Client) ListMetadata(ctx context.Context, limit, offset uint64, order generalPB.SortOrder) ([]*pb.Metadata, error)

ListMetadata -

func (*Client) Name

func (client *Client) Name() string

Name -

func (*Client) Output

func (client *Client) Output(name string) (*modules.Output, error)

Output -

func (*Client) Start

func (client *Client) Start(ctx context.Context)

Start -

func (*Client) SubscribeOnMetadata

func (client *Client) SubscribeOnMetadata(ctx context.Context) (uint64, error)

SubscribeOnMetadata -

func (*Client) UnsubscribeFromMetadata

func (client *Client) UnsubscribeFromMetadata(ctx context.Context, id uint64) error

UnsubscribeFromMetadata -

type ClientConfig

type ClientConfig struct {
	ServerAddress string         `yaml:"server_address" validate:"required"`
	Subscriptions *Subscriptions `yaml:"subscriptions" validate:"omitempty"`
}

ClientConfig -

type Config

type Config struct {
	Server *grpc.ServerConfig `yaml:"server" validate:"omitempty"`
	Client *ClientConfig      `yaml:"client" validate:"omitempty"`
}

Config -

type MetadataSubscription

type MetadataSubscription struct {
	// contains filtered or unexported fields
}

MetadataSubscription -

func NewMetadataSubscription

func NewMetadataSubscription() *MetadataSubscription

NewMetadataSubscription -

func (*MetadataSubscription) Close

func (m *MetadataSubscription) Close() error

Close -

func (*MetadataSubscription) Filter

Filter -

func (*MetadataSubscription) Listen

func (m *MetadataSubscription) Listen() <-chan *pb.SubscriptionMetadata

Listen -

func (*MetadataSubscription) Send

Send -

type Server

type Server struct {
	*grpc.Server
	pb.UnimplementedMetadataServiceServer
	// contains filtered or unexported fields
}

Server -

func NewServer

func NewServer(
	cfg *grpc.ServerConfig,
	metadataRepo storage.IMetadata,
) (*Server, error)

NewServer -

func (*Server) AttachTo

func (server *Server) AttachTo(name string, input *modules.Input) error

AttachTo -

func (*Server) Close

func (server *Server) Close() error

Close -

func (*Server) GetMetadata

func (server *Server) GetMetadata(ctx context.Context, req *pb.GetMetadataRequest) (*pb.Metadata, error)

GetMetadata -

func (*Server) GetMetadataByMethodSinature

func (server *Server) GetMetadataByMethodSinature(ctx context.Context, req *pb.GetMetadataByMethodSinatureRequest) (*pb.ListMetadataResponse, error)

GetMetadataByMethodSinature -

func (*Server) GetMetadataByTopic

func (server *Server) GetMetadataByTopic(ctx context.Context, req *pb.GetMetadataByTopicRequest) (*pb.ListMetadataResponse, error)

GetMetadataByTopic -

func (*Server) Input

func (server *Server) Input(name string) (*modules.Input, error)

Input -

func (*Server) ListMetadata

func (server *Server) ListMetadata(ctx context.Context, req *pb.ListMetadataRequest) (*pb.ListMetadataResponse, error)

ListMetadata -

func (*Server) Name

func (server *Server) Name() string

Name -

func (*Server) Output

func (server *Server) Output(name string) (*modules.Output, error)

Output -

func (*Server) Start

func (server *Server) Start(ctx context.Context)

Start -

func (*Server) SubscribeOnMetadata

func (server *Server) SubscribeOnMetadata(req *generalPB.DefaultRequest, stream pb.MetadataService_SubscribeOnMetadataServer) error

SubscribeOnMetadata -

func (*Server) UnsubscribeFromMetadata

func (server *Server) UnsubscribeFromMetadata(ctx context.Context, req *generalPB.UnsubscribeRequest) (*generalPB.UnsubscribeResponse, error)

UnsubscribeFromMetadata -

type Subscriptions

type Subscriptions struct {
	Metadata bool `yaml:"head,omitempty"`
}

Subscriptions -

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL