From e10e418971c4beb31ba95ebcef0a9de57300c8ae Mon Sep 17 00:00:00 2001 From: oudeis Date: Tue, 8 Sep 2020 18:31:59 +0300 Subject: [PATCH] [NOD-1151] Update DNSSeeder to use grpc (#903) * [NOD-1151] Added gRPC server for seeding peers * [NOD-1151] Fix branch after rebase * [NOD-1151] Lint infrastructure/config/config.go * [NOD-1151] Use Warnf instead of Infof * [NOD-1151] Check if a.cfg.GRPCSeed is defined * [NOD-1151] Delete Makefile - Use go generate instead of Makefile * [NOD-1151] Panic in case of GRPCSeed and DNSSeed are both defined * [NOD-1151] Add generate.go file * [NOD-1151] Allow dnsseed and grpcseed to work together Co-authored-by: Bogdan Ovsiannikov Co-authored-by: yaroslavr-itd Co-authored-by: Ori Newman --- app/app.go | 7 + infrastructure/config/config.go | 1 + infrastructure/network/dnsseed/pb/generate.go | 3 + .../network/dnsseed/pb/peer_service.pb.go | 333 ++++++++++++++++++ .../network/dnsseed/pb/peer_service.proto | 24 ++ .../dnsseed/pb/peer_service_grpc.pb.go | 90 +++++ infrastructure/network/dnsseed/seed.go | 74 +++- 7 files changed, 531 insertions(+), 1 deletion(-) create mode 100644 infrastructure/network/dnsseed/pb/generate.go create mode 100644 infrastructure/network/dnsseed/pb/peer_service.pb.go create mode 100644 infrastructure/network/dnsseed/pb/peer_service.proto create mode 100644 infrastructure/network/dnsseed/pb/peer_service_grpc.pb.go diff --git a/app/app.go b/app/app.go index 60ec5a8b3..b5dc61809 100644 --- a/app/app.go +++ b/app/app.go @@ -173,6 +173,13 @@ func (a *App) maybeSeedFromDNS() { a.addressManager.AddAddresses(addresses, addresses[0], nil) }) } + + if a.cfg.GRPCSeed != "" { + dnsseed.SeedFromGRPC(a.cfg.NetParams(), a.cfg.GRPCSeed, appmessage.SFNodeNetwork, false, nil, + func(addresses []*appmessage.NetAddress) { + a.addressManager.AddAddresses(addresses, addresses[0], nil) + }) + } } func setupDAG(cfg *config.Config, databaseContext *dbaccess.DatabaseContext, interrupt <-chan struct{}, sigCache *txscript.SigCache, indexManager blockdag.IndexManager) (*blockdag.BlockDAG, error) { diff --git a/infrastructure/config/config.go b/infrastructure/config/config.go index 80a13b693..0e671c6ba 100644 --- a/infrastructure/config/config.go +++ b/infrastructure/config/config.go @@ -106,6 +106,7 @@ type Flags struct { DisableTLS bool `long:"notls" description:"Disable TLS for the RPC server -- NOTE: This is only allowed if the RPC server is bound to localhost"` DisableDNSSeed bool `long:"nodnsseed" description:"Disable DNS seeding for peers"` DNSSeed string `long:"dnsseed" description:"Override DNS seeds with specified hostname (Only 1 hostname allowed)"` + GRPCSeed string `long:"grpcseed" description:"Hostname of gRPC server for seeding peers"` ExternalIPs []string `long:"externalip" description:"Add an ip to the list of local addresses we claim to listen on to peers"` Proxy string `long:"proxy" description:"Connect via SOCKS5 proxy (eg. 127.0.0.1:9050)"` ProxyUser string `long:"proxyuser" description:"Username for proxy server"` diff --git a/infrastructure/network/dnsseed/pb/generate.go b/infrastructure/network/dnsseed/pb/generate.go new file mode 100644 index 000000000..7ec628814 --- /dev/null +++ b/infrastructure/network/dnsseed/pb/generate.go @@ -0,0 +1,3 @@ +//go:generate protoc --go_out=. --go-grpc_out=. --go_opt=paths=source_relative --go-grpc_opt=paths=source_relative peer_service.proto + +package pb diff --git a/infrastructure/network/dnsseed/pb/peer_service.pb.go b/infrastructure/network/dnsseed/pb/peer_service.pb.go new file mode 100644 index 000000000..c045bacd0 --- /dev/null +++ b/infrastructure/network/dnsseed/pb/peer_service.pb.go @@ -0,0 +1,333 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.25.0-devel +// protoc v3.6.1 +// source: peer_service.proto + +package pb + +import ( + proto "github.com/golang/protobuf/proto" + 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) +) + +// This is a compile-time assertion that a sufficiently up-to-date version +// of the legacy proto package is being used. +const _ = proto.ProtoPackageIsVersion4 + +type GetPeersListRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + ServiceFlag uint64 `protobuf:"varint,1,opt,name=serviceFlag,proto3" json:"serviceFlag,omitempty"` + SubnetworkID []byte `protobuf:"bytes,2,opt,name=subnetworkID,proto3" json:"subnetworkID,omitempty"` + IncludeAllSubnetworks bool `protobuf:"varint,3,opt,name=includeAllSubnetworks,proto3" json:"includeAllSubnetworks,omitempty"` +} + +func (x *GetPeersListRequest) Reset() { + *x = GetPeersListRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_peer_service_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *GetPeersListRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*GetPeersListRequest) ProtoMessage() {} + +func (x *GetPeersListRequest) ProtoReflect() protoreflect.Message { + mi := &file_peer_service_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 GetPeersListRequest.ProtoReflect.Descriptor instead. +func (*GetPeersListRequest) Descriptor() ([]byte, []int) { + return file_peer_service_proto_rawDescGZIP(), []int{0} +} + +func (x *GetPeersListRequest) GetServiceFlag() uint64 { + if x != nil { + return x.ServiceFlag + } + return 0 +} + +func (x *GetPeersListRequest) GetSubnetworkID() []byte { + if x != nil { + return x.SubnetworkID + } + return nil +} + +func (x *GetPeersListRequest) GetIncludeAllSubnetworks() bool { + if x != nil { + return x.IncludeAllSubnetworks + } + return false +} + +type GetPeersListResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Addresses []*NetAddress `protobuf:"bytes,1,rep,name=addresses,proto3" json:"addresses,omitempty"` +} + +func (x *GetPeersListResponse) Reset() { + *x = GetPeersListResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_peer_service_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *GetPeersListResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*GetPeersListResponse) ProtoMessage() {} + +func (x *GetPeersListResponse) ProtoReflect() protoreflect.Message { + mi := &file_peer_service_proto_msgTypes[1] + 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 GetPeersListResponse.ProtoReflect.Descriptor instead. +func (*GetPeersListResponse) Descriptor() ([]byte, []int) { + return file_peer_service_proto_rawDescGZIP(), []int{1} +} + +func (x *GetPeersListResponse) GetAddresses() []*NetAddress { + if x != nil { + return x.Addresses + } + return nil +} + +type NetAddress struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Timestamp int64 `protobuf:"varint,1,opt,name=timestamp,proto3" json:"timestamp,omitempty"` + Services uint64 `protobuf:"varint,2,opt,name=services,proto3" json:"services,omitempty"` + IP []byte `protobuf:"bytes,3,opt,name=IP,proto3" json:"IP,omitempty"` + Port uint32 `protobuf:"varint,4,opt,name=port,proto3" json:"port,omitempty"` +} + +func (x *NetAddress) Reset() { + *x = NetAddress{} + if protoimpl.UnsafeEnabled { + mi := &file_peer_service_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *NetAddress) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*NetAddress) ProtoMessage() {} + +func (x *NetAddress) ProtoReflect() protoreflect.Message { + mi := &file_peer_service_proto_msgTypes[2] + 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 NetAddress.ProtoReflect.Descriptor instead. +func (*NetAddress) Descriptor() ([]byte, []int) { + return file_peer_service_proto_rawDescGZIP(), []int{2} +} + +func (x *NetAddress) GetTimestamp() int64 { + if x != nil { + return x.Timestamp + } + return 0 +} + +func (x *NetAddress) GetServices() uint64 { + if x != nil { + return x.Services + } + return 0 +} + +func (x *NetAddress) GetIP() []byte { + if x != nil { + return x.IP + } + return nil +} + +func (x *NetAddress) GetPort() uint32 { + if x != nil { + return x.Port + } + return 0 +} + +var File_peer_service_proto protoreflect.FileDescriptor + +var file_peer_service_proto_rawDesc = []byte{ + 0x0a, 0x12, 0x70, 0x65, 0x65, 0x72, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x91, 0x01, 0x0a, 0x13, 0x47, 0x65, 0x74, 0x50, 0x65, 0x65, 0x72, + 0x73, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x20, 0x0a, 0x0b, + 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x46, 0x6c, 0x61, 0x67, 0x18, 0x01, 0x20, 0x01, 0x28, + 0x04, 0x52, 0x0b, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x46, 0x6c, 0x61, 0x67, 0x12, 0x22, + 0x0a, 0x0c, 0x73, 0x75, 0x62, 0x6e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x49, 0x44, 0x18, 0x02, + 0x20, 0x01, 0x28, 0x0c, 0x52, 0x0c, 0x73, 0x75, 0x62, 0x6e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, + 0x49, 0x44, 0x12, 0x34, 0x0a, 0x15, 0x69, 0x6e, 0x63, 0x6c, 0x75, 0x64, 0x65, 0x41, 0x6c, 0x6c, + 0x53, 0x75, 0x62, 0x6e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x73, 0x18, 0x03, 0x20, 0x01, 0x28, + 0x08, 0x52, 0x15, 0x69, 0x6e, 0x63, 0x6c, 0x75, 0x64, 0x65, 0x41, 0x6c, 0x6c, 0x53, 0x75, 0x62, + 0x6e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x73, 0x22, 0x41, 0x0a, 0x14, 0x47, 0x65, 0x74, 0x50, + 0x65, 0x65, 0x72, 0x73, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, + 0x12, 0x29, 0x0a, 0x09, 0x61, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x65, 0x73, 0x18, 0x01, 0x20, + 0x03, 0x28, 0x0b, 0x32, 0x0b, 0x2e, 0x4e, 0x65, 0x74, 0x41, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, + 0x52, 0x09, 0x61, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x65, 0x73, 0x22, 0x6a, 0x0a, 0x0a, 0x4e, + 0x65, 0x74, 0x41, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x12, 0x1c, 0x0a, 0x09, 0x74, 0x69, 0x6d, + 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x09, 0x74, 0x69, + 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x12, 0x1a, 0x0a, 0x08, 0x73, 0x65, 0x72, 0x76, 0x69, + 0x63, 0x65, 0x73, 0x18, 0x02, 0x20, 0x01, 0x28, 0x04, 0x52, 0x08, 0x73, 0x65, 0x72, 0x76, 0x69, + 0x63, 0x65, 0x73, 0x12, 0x0e, 0x0a, 0x02, 0x49, 0x50, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0c, 0x52, + 0x02, 0x49, 0x50, 0x12, 0x12, 0x0a, 0x04, 0x70, 0x6f, 0x72, 0x74, 0x18, 0x04, 0x20, 0x01, 0x28, + 0x0d, 0x52, 0x04, 0x70, 0x6f, 0x72, 0x74, 0x32, 0x4c, 0x0a, 0x0b, 0x50, 0x65, 0x65, 0x72, 0x53, + 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x3d, 0x0a, 0x0c, 0x47, 0x65, 0x74, 0x50, 0x65, 0x65, + 0x72, 0x73, 0x4c, 0x69, 0x73, 0x74, 0x12, 0x14, 0x2e, 0x47, 0x65, 0x74, 0x50, 0x65, 0x65, 0x72, + 0x73, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x15, 0x2e, 0x47, + 0x65, 0x74, 0x50, 0x65, 0x65, 0x72, 0x73, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, + 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, 0x1f, 0x5a, 0x1d, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, + 0x63, 0x6f, 0x6d, 0x2f, 0x6b, 0x61, 0x73, 0x70, 0x61, 0x6e, 0x65, 0x74, 0x2f, 0x6b, 0x61, 0x73, + 0x70, 0x61, 0x64, 0x2f, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_peer_service_proto_rawDescOnce sync.Once + file_peer_service_proto_rawDescData = file_peer_service_proto_rawDesc +) + +func file_peer_service_proto_rawDescGZIP() []byte { + file_peer_service_proto_rawDescOnce.Do(func() { + file_peer_service_proto_rawDescData = protoimpl.X.CompressGZIP(file_peer_service_proto_rawDescData) + }) + return file_peer_service_proto_rawDescData +} + +var file_peer_service_proto_msgTypes = make([]protoimpl.MessageInfo, 3) +var file_peer_service_proto_goTypes = []interface{}{ + (*GetPeersListRequest)(nil), // 0: GetPeersListRequest + (*GetPeersListResponse)(nil), // 1: GetPeersListResponse + (*NetAddress)(nil), // 2: NetAddress +} +var file_peer_service_proto_depIdxs = []int32{ + 2, // 0: GetPeersListResponse.addresses:type_name -> NetAddress + 0, // 1: PeerService.GetPeersList:input_type -> GetPeersListRequest + 1, // 2: PeerService.GetPeersList:output_type -> GetPeersListResponse + 2, // [2:3] is the sub-list for method output_type + 1, // [1:2] is the sub-list for method input_type + 1, // [1:1] is the sub-list for extension type_name + 1, // [1:1] is the sub-list for extension extendee + 0, // [0:1] is the sub-list for field type_name +} + +func init() { file_peer_service_proto_init() } +func file_peer_service_proto_init() { + if File_peer_service_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_peer_service_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*GetPeersListRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_peer_service_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*GetPeersListResponse); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_peer_service_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*NetAddress); 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_peer_service_proto_rawDesc, + NumEnums: 0, + NumMessages: 3, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_peer_service_proto_goTypes, + DependencyIndexes: file_peer_service_proto_depIdxs, + MessageInfos: file_peer_service_proto_msgTypes, + }.Build() + File_peer_service_proto = out.File + file_peer_service_proto_rawDesc = nil + file_peer_service_proto_goTypes = nil + file_peer_service_proto_depIdxs = nil +} diff --git a/infrastructure/network/dnsseed/pb/peer_service.proto b/infrastructure/network/dnsseed/pb/peer_service.proto new file mode 100644 index 000000000..bbcc63a7b --- /dev/null +++ b/infrastructure/network/dnsseed/pb/peer_service.proto @@ -0,0 +1,24 @@ +syntax = "proto3"; + +option go_package = "github.com/kaspanet/kaspad/pb"; + +service PeerService { + rpc GetPeersList(GetPeersListRequest) returns (GetPeersListResponse) {} +} + +message GetPeersListRequest { + uint64 serviceFlag = 1; + bytes subnetworkID = 2; + bool includeAllSubnetworks = 3; +} + +message GetPeersListResponse { + repeated NetAddress addresses = 1; +} + +message NetAddress { + int64 timestamp = 1; + uint64 services = 2; + bytes IP = 3; + uint32 port = 4; +} \ No newline at end of file diff --git a/infrastructure/network/dnsseed/pb/peer_service_grpc.pb.go b/infrastructure/network/dnsseed/pb/peer_service_grpc.pb.go new file mode 100644 index 000000000..975d57909 --- /dev/null +++ b/infrastructure/network/dnsseed/pb/peer_service_grpc.pb.go @@ -0,0 +1,90 @@ +// Code generated by protoc-gen-go-grpc. DO NOT EDIT. + +package pb + +import ( + context "context" + grpc "google.golang.org/grpc" + codes "google.golang.org/grpc/codes" + status "google.golang.org/grpc/status" +) + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the grpc package it is being compiled against. +const _ = grpc.SupportPackageIsVersion6 + +// PeerServiceClient is the client API for PeerService service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. +type PeerServiceClient interface { + GetPeersList(ctx context.Context, in *GetPeersListRequest, opts ...grpc.CallOption) (*GetPeersListResponse, error) +} + +type peerServiceClient struct { + cc grpc.ClientConnInterface +} + +func NewPeerServiceClient(cc grpc.ClientConnInterface) PeerServiceClient { + return &peerServiceClient{cc} +} + +func (c *peerServiceClient) GetPeersList(ctx context.Context, in *GetPeersListRequest, opts ...grpc.CallOption) (*GetPeersListResponse, error) { + out := new(GetPeersListResponse) + err := c.cc.Invoke(ctx, "/PeerService/GetPeersList", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// PeerServiceServer is the server API for PeerService service. +// All implementations must embed UnimplementedPeerServiceServer +// for forward compatibility +type PeerServiceServer interface { + GetPeersList(context.Context, *GetPeersListRequest) (*GetPeersListResponse, error) + mustEmbedUnimplementedPeerServiceServer() +} + +// UnimplementedPeerServiceServer must be embedded to have forward compatible implementations. +type UnimplementedPeerServiceServer struct { +} + +func (*UnimplementedPeerServiceServer) GetPeersList(context.Context, *GetPeersListRequest) (*GetPeersListResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method GetPeersList not implemented") +} +func (*UnimplementedPeerServiceServer) mustEmbedUnimplementedPeerServiceServer() {} + +func RegisterPeerServiceServer(s *grpc.Server, srv PeerServiceServer) { + s.RegisterService(&_PeerService_serviceDesc, srv) +} + +func _PeerService_GetPeersList_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(GetPeersListRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(PeerServiceServer).GetPeersList(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/PeerService/GetPeersList", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(PeerServiceServer).GetPeersList(ctx, req.(*GetPeersListRequest)) + } + return interceptor(ctx, in, info, handler) +} + +var _PeerService_serviceDesc = grpc.ServiceDesc{ + ServiceName: "PeerService", + HandlerType: (*PeerServiceServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "GetPeersList", + Handler: _PeerService_GetPeersList_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "peer_service.proto", +} diff --git a/infrastructure/network/dnsseed/seed.go b/infrastructure/network/dnsseed/seed.go index d28c9a317..a9cd481b9 100644 --- a/infrastructure/network/dnsseed/seed.go +++ b/infrastructure/network/dnsseed/seed.go @@ -5,13 +5,17 @@ package dnsseed import ( + "context" "fmt" - "github.com/kaspanet/kaspad/app/appmessage" "math/rand" "net" "strconv" "time" + "github.com/kaspanet/kaspad/app/appmessage" + pb2 "github.com/kaspanet/kaspad/infrastructure/network/dnsseed/pb" + "google.golang.org/grpc" + "github.com/kaspanet/kaspad/util/mstime" "github.com/kaspanet/kaspad/util/subnetworkid" @@ -97,3 +101,71 @@ func SeedFromDNS(dagParams *dagconfig.Params, customSeed string, reqServices app }) } } + +// SeedFromGRPC send gRPC request to get list of peers for a given host +func SeedFromGRPC(dagParams *dagconfig.Params, host string, reqServices appmessage.ServiceFlag, includeAllSubnetworks bool, + subnetworkID *subnetworkid.SubnetworkID, seedFn OnSeed) { + + spawn("SeedFromGRPC", func() { + + randSource := rand.New(rand.NewSource(time.Now().UnixNano())) + + conn, err := grpc.Dial(host, grpc.WithInsecure()) + client := pb2.NewPeerServiceClient(conn) + if err != nil { + log.Warnf("Failed to connect to gRPC server: %s", host) + } + + var subnetID []byte + if subnetworkID != nil { + subnetID = subnetworkID.CloneBytes() + } else { + subnetID = nil + } + + req := &pb2.GetPeersListRequest{ + ServiceFlag: uint64(reqServices), + SubnetworkID: subnetID, + IncludeAllSubnetworks: includeAllSubnetworks, + } + res, err := client.GetPeersList(context.Background(), req) + + if err != nil { + log.Infof("gRPC request to get peers failed (host=%s): %s", host, err) + return + } + + seedPeers := fromProtobufAddresses(res.Addresses) + + numPeers := len(seedPeers) + + log.Infof("%d addresses found from DNS seed %s", numPeers, host) + + if numPeers == 0 { + return + } + addresses := make([]*appmessage.NetAddress, len(seedPeers)) + // if this errors then we have *real* problems + intPort, _ := strconv.Atoi(dagParams.DefaultPort) + for i, peer := range seedPeers { + addresses[i] = appmessage.NewNetAddressTimestamp( + // seed with addresses from a time randomly selected + // between 3 and 7 days ago. + mstime.Now().Add(-1*time.Second*time.Duration(secondsIn3Days+ + randSource.Int31n(secondsIn4Days))), + 0, peer, uint16(intPort)) + } + + seedFn(addresses) + }) +} + +func fromProtobufAddresses(proto []*pb2.NetAddress) []net.IP { + var addresses []net.IP + + for _, pbAddr := range proto { + addresses = append(addresses, net.IP(pbAddr.IP)) + } + + return addresses +}