Skip to content

Commit

Permalink
chore: upstream more changes from v2 (#20387)
Browse files Browse the repository at this point in the history
Co-authored-by: Matt Kocubinski <mkocubinski@gmail.com>
  • Loading branch information
tac0turtle and kocubinski authored May 17, 2024
1 parent 92dafe6 commit d7cc6de
Show file tree
Hide file tree
Showing 39 changed files with 5,432 additions and 8 deletions.
71 changes: 71 additions & 0 deletions proto/cosmos/streaming/v1/grpc.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
syntax = "proto3";

package cosmos.streaming.v1;

option go_package = "cosmossdk.io/server/v2/streaming";

// ListenDeliverBlockRequest is the request type for the ListenDeliverBlock RPC method
message ListenDeliverBlockRequest {
int64 block_height = 1;
repeated bytes txs = 2;
repeated Event events = 3;
repeated ExecTxResult tx_results = 4;
}

// ListenDeliverBlockResponse is the response type for the ListenDeliverBlock RPC method
message ListenDeliverBlockResponse {}

// ListenStateChangesRequest is the request type for the ListenStateChanges RPC method
message ListenStateChangesRequest {
int64 block_height = 1;
repeated StoreKVPair change_set = 2;
bytes app_hash = 3;
}

// ListenStateChangesResponse is the response type for the ListenStateChanges RPC method
message ListenStateChangesResponse {}

// ListenerService is the service for the Listener interface
service ListenerService {
// ListenDeliverBlock is the corresponding endpoint for Listener.ListenDeliverBlock
rpc ListenDeliverBlock(ListenDeliverBlockRequest) returns (ListenDeliverBlockResponse);
// ListenStateChanges is the corresponding endpoint for Listener.ListenStateChanges
rpc ListenStateChanges(ListenStateChangesRequest) returns (ListenStateChangesResponse);
}

// StoreKVPair is a single key-value pair, associated with a store.
message StoreKVPair {
// address defines the address of the account the state changes are coming from.
// In case of modules you can expect a stringified
bytes address = 1;
// key defines the key of the address that changed.
bytes key = 2;
// value defines the value that changed, empty in case of removal.
bytes value = 3;
// delete defines if the key was removed.
bool delete = 4; // true indicates a delete operation, false indicates a set operation
}

// Event is a single event, associated with a transaction.
message Event {
string type = 1;
repeated EventAttribute attributes = 2;
}

// EventAttribute is a single key-value pair, associated with an event.
message EventAttribute {
string key = 1;
string value = 2;
}

// ExecTxResult contains results of executing one individual transaction.
message ExecTxResult {
uint32 code = 1;
bytes data = 2;
string log = 3;
string info = 4;
int64 gas_wanted = 5;
int64 gas_used = 6;
repeated Event events = 7;
string codespace = 8;
}
34 changes: 34 additions & 0 deletions server/v2/api/grpc/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package grpc

import "math"

func DefaultConfig() *Config {
return &Config{
Enable: true,
// DefaultGRPCAddress defines the default address to bind the gRPC server to.
Address: "localhost:9090",
// DefaultGRPCMaxRecvMsgSize defines the default gRPC max message size in
// bytes the server can receive.
MaxRecvMsgSize: 1024 * 1024 * 10,
// DefaultGRPCMaxSendMsgSize defines the default gRPC max message size in
// bytes the server can send.
MaxSendMsgSize: math.MaxInt32,
}
}

// GRPCConfig defines configuration for the gRPC server.
type Config struct {
// Enable defines if the gRPC server should be enabled.
Enable bool `mapstructure:"enable" toml:"enable" comment:"Enable defines if the gRPC server should be enabled."`

// Address defines the API server to listen on
Address string `mapstructure:"address" toml:"address" comment:"Address defines the gRPC server address to bind to."`

// MaxRecvMsgSize defines the max message size in bytes the server can receive.
// The default value is 10MB.
MaxRecvMsgSize int `mapstructure:"max-recv-msg-size" toml:"max-recv-msg-size" comment:"MaxRecvMsgSize defines the max message size in bytes the server can receive.\nThe default value is 10MB."`

// MaxSendMsgSize defines the max message size in bytes the server can send.
// The default value is math.MaxInt32.
MaxSendMsgSize int `mapstructure:"max-send-msg-size" toml:"max-send-msg-size" comment:"MaxSendMsgSize defines the max message size in bytes the server can send.\nThe default value is math.MaxInt32."`
}
5 changes: 5 additions & 0 deletions server/v2/api/grpc/gogoreflection/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Package gogoreflection implements gRPC reflection for gogoproto consumers
// the normal reflection library does not work as it points to a different
// singleton registry. The API and codebase is taken from the official gRPC
// reflection repository.
package gogoreflection
76 changes: 76 additions & 0 deletions server/v2/api/grpc/gogoreflection/fix_registration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package gogoreflection

import (
"reflect"

_ "github.com/cosmos/gogoproto/gogoproto" // required so it does register the gogoproto file descriptor
gogoproto "github.com/cosmos/gogoproto/proto"

_ "github.com/cosmos/cosmos-proto" // look above
"github.com/golang/protobuf/proto" //nolint:staticcheck // migrate in a future pr
)

func getFileDescriptor(filePath string) []byte {
// Since we got well known descriptors which are not registered into gogoproto
// registry but are instead registered into the proto one, we need to check both.
fd := gogoproto.FileDescriptor(filePath)
if len(fd) != 0 {
return fd
}

return proto.FileDescriptor(filePath) //nolint:staticcheck // keep for backward compatibility
}

func getMessageType(name string) reflect.Type {
typ := gogoproto.MessageType(name)
if typ != nil {
return typ
}

return proto.MessageType(name) //nolint:staticcheck // keep for backward compatibility
}

func getExtension(extID int32, m proto.Message) *gogoproto.ExtensionDesc {
// check first in gogoproto registry
for id, desc := range gogoproto.RegisteredExtensions(m) {
if id == extID {
return desc
}
}

// check into proto registry
for id, desc := range proto.RegisteredExtensions(m) { //nolint:staticcheck // keep for backward compatibility
if id == extID {
return &gogoproto.ExtensionDesc{
ExtendedType: desc.ExtendedType, //nolint:staticcheck // keep for backward compatibility
ExtensionType: desc.ExtensionType, //nolint:staticcheck // keep for backward compatibility
Field: desc.Field, //nolint:staticcheck // keep for backward compatibility
Name: desc.Name, //nolint:staticcheck // keep for backward compatibility
Tag: desc.Tag, //nolint:staticcheck // keep for backward compatibility
Filename: desc.Filename, //nolint:staticcheck // keep for backward compatibility
}
}
}

return nil
}

func getExtensionsNumbers(m proto.Message) []int32 {
gogoProtoExts := gogoproto.RegisteredExtensions(m)

out := make([]int32, 0, len(gogoProtoExts))
for id := range gogoProtoExts {
out = append(out, id)
}
if len(out) != 0 {
return out
}

protoExts := proto.RegisteredExtensions(m) //nolint:staticcheck // kept for backwards compatibility
out = make([]int32, 0, len(protoExts))
for id := range protoExts {
out = append(out, id)
}

return out
}
22 changes: 22 additions & 0 deletions server/v2/api/grpc/gogoreflection/fix_registration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package gogoreflection

import (
"testing"

"google.golang.org/protobuf/runtime/protoimpl"
)

func TestRegistrationFix(t *testing.T) {
res := getFileDescriptor("gogoproto/gogo.proto")
rawDesc, err := decompress(res)
if err != nil {
t.Fatal(err)
}
fd := protoimpl.DescBuilder{
RawDescriptor: rawDesc,
}.Build()

if fd.File.Extensions().Len() == 0 {
t.Fatal("unexpected parsing")
}
}
Loading

0 comments on commit d7cc6de

Please sign in to comment.