-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NOD-1151] Added gRPC server for seeding peers
- Loading branch information
1 parent
3be0894
commit 4d13db3
Showing
9 changed files
with
329 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
# Temp files | ||
*~ | ||
testdata | ||
|
||
# Databases | ||
kaspad.db | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/kaspanet/kaspad/rpc/client" | ||
"strings" | ||
) | ||
|
||
func main() { | ||
connCfg := &client.ConnConfig{ | ||
Host: "localhost:16630", | ||
User: "test", | ||
Pass: "test", | ||
HTTPPostMode: true, | ||
DisableTLS: true, | ||
} | ||
cl, err := client.New(connCfg, nil) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
res, err := cl.GetPeerAddresses() | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
var ips []string | ||
|
||
for _, address := range res.Addresses { | ||
ips = append(ips, address.Addr) | ||
} | ||
|
||
fmt.Printf(strings.Join(ips, ",")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
pb2 "github.com/kaspanet/kaspad/dnsseed/pb" | ||
"github.com/kaspanet/kaspad/util/subnetworkid" | ||
"github.com/kaspanet/kaspad/wire" | ||
"github.com/miekg/dns" | ||
"github.com/pkg/errors" | ||
"google.golang.org/grpc" | ||
"net" | ||
) | ||
|
||
type GRPCServer interface { | ||
Start(listenInterface string) error | ||
Stop() | ||
} | ||
|
||
type grpcServer struct { | ||
pb2.UnimplementedPeerServiceServer | ||
|
||
server *grpc.Server | ||
amgr *Manager | ||
} | ||
|
||
func NewGRPCServer(amgr *Manager) GRPCServer { | ||
return &grpcServer{ amgr: amgr } | ||
} | ||
|
||
func (s *grpcServer) Start(listenInterface string) error { | ||
s.server = grpc.NewServer() | ||
pb2.RegisterPeerServiceServer(s.server, s) | ||
|
||
lis, err := net.Listen("tcp", fmt.Sprintf(listenInterface)) | ||
if err != nil { | ||
return errors.WithStack(err) | ||
} | ||
|
||
spawn("gRPC server", func() { | ||
err = s.server.Serve(lis) | ||
if err != nil { | ||
fmt.Printf("%+v", err) | ||
} | ||
}) | ||
|
||
|
||
return nil | ||
} | ||
|
||
func (s *grpcServer) Stop() { | ||
s.server.Stop() | ||
s.server = nil | ||
} | ||
|
||
func (s *grpcServer) GetPeersList(ctx context.Context, req *pb2.GetPeersListRequest) (*pb2.GetPeersListResponse, error) { | ||
|
||
subnetworkID, err := FromProtobufSubnetworkID(req.SubnetworkID) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// mb, we should move DNS-related logic out of manager? | ||
ipv4Addresses := s.amgr.GoodAddresses(dns.TypeA, wire.ServiceFlag(req.ServiceFlag), req.IncludeAllSubnetworks, subnetworkID) | ||
ipv6Addresses := s.amgr.GoodAddresses(dns.TypeAAAA, wire.ServiceFlag(req.ServiceFlag), req.IncludeAllSubnetworks, subnetworkID) | ||
|
||
addresses := ToProtobufAddresses(append(ipv4Addresses, ipv6Addresses...)) | ||
log.Error("ADDRESSES: %+v", addresses) | ||
|
||
return &pb2.GetPeersListResponse{ Addresses: addresses}, nil | ||
} | ||
|
||
func FromProtobufSubnetworkID(proto []byte) (*subnetworkid.SubnetworkID, error) { | ||
if len(proto) == 0 { | ||
return nil, nil | ||
} | ||
|
||
subnetworkID, err := subnetworkid.New(proto) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return subnetworkID, nil | ||
} | ||
|
||
func ToProtobufAddresses(addresses []*wire.NetAddress) []*pb2.NetAddress { | ||
var protoAddresses []*pb2.NetAddress | ||
|
||
for _, addr := range addresses { | ||
proto := &pb2.NetAddress{ | ||
Timestamp: addr.Timestamp.UnixSeconds(), | ||
Services: uint64(addr.Services), | ||
IP: []byte(addr.IP), | ||
Port: uint32(addr.Port), | ||
} | ||
protoAddresses = append(protoAddresses, proto) | ||
} | ||
|
||
return protoAddresses | ||
} |
Oops, something went wrong.