This repository has been archived by the owner on Jul 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grpc_server.go
116 lines (90 loc) · 2.96 KB
/
grpc_server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Package api provides the local go-spacemesh API endpoints. e.g. json-http and grpc-http2
package main
import (
"fmt"
"strconv"
"log"
"net"
"oracle_server/pb"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)
// Server is a grpc server providing the Spacemesh api
type Server struct {
wSwitch *WorldSwitch
Server *grpc.Server
Port uint
}
// Register
func (s Server) Register(ctx context.Context, in *pb.Registration) (*pb.SimpleMessage, error) {
log.Printf("registering %v in world %v \n", in.ID, in.World)
s.wSwitch.Register(in.World, in.Honest, in.ID)
return &pb.SimpleMessage{Value: "OK"}, nil
}
// Unregister
func (s Server) Unregister(ctx context.Context, in *pb.Registration) (*pb.SimpleMessage, error) {
log.Printf("Unregistering %v in world %v \n", in.ID, in.World)
s.wSwitch.Unregister(in.World, in.Honest, in.ID)
return &pb.SimpleMessage{Value: "OK"}, nil
}
// Validate validates an id. DEPRECATED: use ValidateMap and get full map.
func (s Server) Validate(ctx context.Context, in *pb.ValidReq) (*pb.ValidRes, error) {
log.Println("Got validate req")
fmt.Println("inst : ", in.InstanceID)
fmt.Println("committee ", in.CommitteeSize)
fmt.Println("proof ", in.ID)
v := &pb.ValidRes{Valid: s.wSwitch.Get(in.World).Eligible(in.InstanceID, int(in.CommitteeSize), in.ID, []byte{})}
return v, nil
}
// ValidateMap will return the full map
func (s Server) ValidateMap(ctx context.Context, in *pb.ValidReq) (*pb.ValidList, error) {
log.Println("Got validate req")
fmt.Println("inst : ", in.InstanceID)
fmt.Println("committee ", in.CommitteeSize)
v := s.wSwitch.Get(in.World).Export(in.InstanceID, int(in.CommitteeSize))
return MapToList(v), nil
}
func MapToList(elgmap map[string]struct{}) *pb.ValidList {
vl := &pb.ValidList{}
for k, _ := range elgmap {
vl.IDs = append(vl.IDs, k)
}
return vl
}
// StopService stops the grpc service.
func (s Server) StopService() {
s.Server.Stop()
}
// NewGrpcService create a new grpc service using config data.
func NewGrpcService(port int, ws *WorldSwitch) *Server {
server := grpc.NewServer()
return &Server{Server: server, Port: uint(port), wSwitch: ws}
}
// StartService starts the grpc service.
func (s Server) StartService(status chan bool) {
go s.startServiceInternal(status)
}
// This is a blocking method designed to be called using a go routine
func (s Server) startServiceInternal(status chan bool) {
addr := ":" + strconv.Itoa(int(s.Port))
lis, err := net.Listen("tcp", addr)
if err != nil {
log.Println("failed to listen", err)
return
}
pb.RegisterServiceServer(s.Server, s)
// SubscribeOnNewConnections reflection service on gRPC server
reflection.Register(s.Server)
log.Println("grpc API listening on port ", s.Port)
if status != nil {
status <- true
}
// start serving - this blocks until err or server is stopped
if err := s.Server.Serve(lis); err != nil {
log.Println("grpc stopped serving", err)
}
if status != nil {
status <- true
}
}