From f3ebb946f3f0de7d6616f78b0c410fb7d29accd1 Mon Sep 17 00:00:00 2001 From: Marko Atanasievski Date: Thu, 1 Dec 2022 16:40:58 +0100 Subject: [PATCH] fix: subscribe to topic --- network/frost/frost_stream.go | 1 - network/frost/proto/frost.pb.go | 152 ++++++++++++++++++++++++++++++++ network/frost/proto/frost.proto | 12 +++ network/server.go | 13 +++ network/server_frost.go | 19 ++++ 5 files changed, 196 insertions(+), 1 deletion(-) create mode 100644 network/frost/proto/frost.pb.go create mode 100644 network/frost/proto/frost.proto diff --git a/network/frost/frost_stream.go b/network/frost/frost_stream.go index 6f3e3bb1c9..4511b28562 100644 --- a/network/frost/frost_stream.go +++ b/network/frost/frost_stream.go @@ -33,7 +33,6 @@ func (s *FrostStream) Client(stream network.Stream) *network.Stream { } func (s *FrostStream) Handler() func(network.Stream) { - fmt.Println(">>>>>>>>>>>>>>> FROST Handler called:") return func(stream network.Stream) { go func() { diff --git a/network/frost/proto/frost.pb.go b/network/frost/proto/frost.pb.go new file mode 100644 index 0000000000..1a23730df0 --- /dev/null +++ b/network/frost/proto/frost.pb.go @@ -0,0 +1,152 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.28.1 +// protoc v3.21.6 +// source: frost.proto + +package proto + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +// Block contains a block data +type FrostMessage struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Message string `protobuf:"bytes,1,opt,name=message,proto3" json:"message,omitempty"` + Number string `protobuf:"bytes,2,opt,name=number,proto3" json:"number,omitempty"` +} + +func (x *FrostMessage) Reset() { + *x = FrostMessage{} + if protoimpl.UnsafeEnabled { + mi := &file_frost_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *FrostMessage) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*FrostMessage) ProtoMessage() {} + +func (x *FrostMessage) ProtoReflect() protoreflect.Message { + mi := &file_frost_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use FrostMessage.ProtoReflect.Descriptor instead. +func (*FrostMessage) Descriptor() ([]byte, []int) { + return file_frost_proto_rawDescGZIP(), []int{0} +} + +func (x *FrostMessage) GetMessage() string { + if x != nil { + return x.Message + } + return "" +} + +func (x *FrostMessage) GetNumber() string { + if x != nil { + return x.Number + } + return "" +} + +var File_frost_proto protoreflect.FileDescriptor + +var file_frost_proto_rawDesc = []byte{ + 0x0a, 0x0b, 0x66, 0x72, 0x6f, 0x73, 0x74, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x02, 0x76, + 0x31, 0x22, 0x40, 0x0a, 0x0c, 0x46, 0x72, 0x6f, 0x73, 0x74, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, + 0x65, 0x12, 0x18, 0x0a, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x18, 0x01, 0x20, 0x01, + 0x28, 0x09, 0x52, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x6e, + 0x75, 0x6d, 0x62, 0x65, 0x72, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x6e, 0x75, 0x6d, + 0x62, 0x65, 0x72, 0x42, 0x0e, 0x5a, 0x0c, 0x2f, 0x66, 0x72, 0x6f, 0x73, 0x74, 0x2f, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_frost_proto_rawDescOnce sync.Once + file_frost_proto_rawDescData = file_frost_proto_rawDesc +) + +func file_frost_proto_rawDescGZIP() []byte { + file_frost_proto_rawDescOnce.Do(func() { + file_frost_proto_rawDescData = protoimpl.X.CompressGZIP(file_frost_proto_rawDescData) + }) + return file_frost_proto_rawDescData +} + +var file_frost_proto_msgTypes = make([]protoimpl.MessageInfo, 1) +var file_frost_proto_goTypes = []interface{}{ + (*FrostMessage)(nil), // 0: v1.FrostMessage +} +var file_frost_proto_depIdxs = []int32{ + 0, // [0:0] is the sub-list for method output_type + 0, // [0:0] is the sub-list for method input_type + 0, // [0:0] is the sub-list for extension type_name + 0, // [0:0] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name +} + +func init() { file_frost_proto_init() } +func file_frost_proto_init() { + if File_frost_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_frost_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*FrostMessage); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_frost_proto_rawDesc, + NumEnums: 0, + NumMessages: 1, + NumExtensions: 0, + NumServices: 0, + }, + GoTypes: file_frost_proto_goTypes, + DependencyIndexes: file_frost_proto_depIdxs, + MessageInfos: file_frost_proto_msgTypes, + }.Build() + File_frost_proto = out.File + file_frost_proto_rawDesc = nil + file_frost_proto_goTypes = nil + file_frost_proto_depIdxs = nil +} diff --git a/network/frost/proto/frost.proto b/network/frost/proto/frost.proto new file mode 100644 index 0000000000..c41efe4c67 --- /dev/null +++ b/network/frost/proto/frost.proto @@ -0,0 +1,12 @@ +syntax = "proto3"; + +package v1; + +option go_package = "/frost/proto"; + +// Block contains a block data +message FrostMessage { + string message = 1; + string number = 2; +} + diff --git a/network/server.go b/network/server.go index add20fc53e..d65df97c58 100644 --- a/network/server.go +++ b/network/server.go @@ -10,6 +10,7 @@ import ( "github.com/0xPolygon/polygon-edge/network/common" "github.com/0xPolygon/polygon-edge/network/dial" "github.com/0xPolygon/polygon-edge/network/discovery" + "github.com/0xPolygon/polygon-edge/network/frost/proto" "github.com/armon/go-metrics" "github.com/libp2p/go-libp2p" "github.com/libp2p/go-libp2p/p2p/security/noise" @@ -92,6 +93,7 @@ type Server struct { frostPeersLock sync.Mutex // lock for the peer map of topos nodes frostPendingPeerConnections sync.Map // Map that keeps track of the frost pending status frostConnectionCounts *ConnectionInfo + frostTopic *Topic } // NewServer returns a new instance of the networking server @@ -285,6 +287,17 @@ func (s *Server) Start() error { return fmt.Errorf("unable to setup frost, %w", setupErr) } + topic, err := s.NewTopic("/frost", &proto.FrostMessage{}) + if err != nil { + return err + } + + if err := topic.Subscribe(s.handleFrostStatusUpdate); err != nil { + return fmt.Errorf("unable to subscribe to gossip topic, %w", err) + } + + s.frostTopic = topic + // Set up the peer discovery mechanism if needed if !s.config.NoDiscover { // Parse the bootnode data diff --git a/network/server_frost.go b/network/server_frost.go index 944d34ec60..1113bf9a8a 100644 --- a/network/server_frost.go +++ b/network/server_frost.go @@ -6,6 +6,7 @@ import ( "github.com/0xPolygon/polygon-edge/network/common" peerEvent "github.com/0xPolygon/polygon-edge/network/event" "github.com/0xPolygon/polygon-edge/network/frost" + "github.com/0xPolygon/polygon-edge/network/frost/proto" "github.com/armon/go-metrics" "github.com/libp2p/go-libp2p/core/network" "github.com/libp2p/go-libp2p/core/peer" @@ -137,3 +138,21 @@ func (s *Server) registerFrostService(frostService *frost.FrostService) { frostStream := frost.NewFrostStream() s.RegisterRawProtocol(common.Frost, frostStream) } + +// handleFrostStatusUpdate is a handler of gossip +func (s *Server) handleFrostStatusUpdate(obj interface{}, from peer.ID) { + frostMessage, ok := obj.(*proto.FrostMessage) + fmt.Println(">>>>>>>>>>>>>>>>>> Received frost message:", frostMessage) + if !ok { + s.logger.Error("failed to cast gossiped frost message") + return + } + + // if !s.host.Network().IsConnected(from) { + // if m.id != from.String() { + // m.logger.Debug("received status from non-connected peer, ignore", "id", from) + // } + + // return + // } +}