-
Notifications
You must be signed in to change notification settings - Fork 671
/
appsender_server.go
82 lines (69 loc) · 2.28 KB
/
appsender_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
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package appsender
import (
"context"
"google.golang.org/protobuf/types/known/emptypb"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/engine/common"
"github.com/ava-labs/avalanchego/utils/set"
appsenderpb "github.com/ava-labs/avalanchego/proto/pb/appsender"
)
var _ appsenderpb.AppSenderServer = (*Server)(nil)
type Server struct {
appsenderpb.UnsafeAppSenderServer
appSender common.AppSender
}
// NewServer returns a messenger connected to a remote channel
func NewServer(appSender common.AppSender) *Server {
return &Server{appSender: appSender}
}
func (s *Server) SendAppRequest(ctx context.Context, req *appsenderpb.SendAppRequestMsg) (*emptypb.Empty, error) {
nodeIDs := set.NewSet[ids.NodeID](len(req.NodeIds))
for _, nodeIDBytes := range req.NodeIds {
nodeID, err := ids.ToNodeID(nodeIDBytes)
if err != nil {
return nil, err
}
nodeIDs.Add(nodeID)
}
err := s.appSender.SendAppRequest(ctx, nodeIDs, req.RequestId, req.Request)
return &emptypb.Empty{}, err
}
func (s *Server) SendAppResponse(ctx context.Context, req *appsenderpb.SendAppResponseMsg) (*emptypb.Empty, error) {
nodeID, err := ids.ToNodeID(req.NodeId)
if err != nil {
return nil, err
}
err = s.appSender.SendAppResponse(ctx, nodeID, req.RequestId, req.Response)
return &emptypb.Empty{}, err
}
func (s *Server) SendAppError(ctx context.Context, req *appsenderpb.SendAppErrorMsg) (*emptypb.Empty, error) {
nodeID, err := ids.ToNodeID(req.NodeId)
if err != nil {
return nil, err
}
err = s.appSender.SendAppError(ctx, nodeID, req.RequestId, req.ErrorCode, req.ErrorMessage)
return &emptypb.Empty{}, err
}
func (s *Server) SendAppGossip(ctx context.Context, req *appsenderpb.SendAppGossipMsg) (*emptypb.Empty, error) {
nodeIDs := set.NewSet[ids.NodeID](len(req.NodeIds))
for _, nodeIDBytes := range req.NodeIds {
nodeID, err := ids.ToNodeID(nodeIDBytes)
if err != nil {
return nil, err
}
nodeIDs.Add(nodeID)
}
err := s.appSender.SendAppGossip(
ctx,
common.SendConfig{
NodeIDs: nodeIDs,
Validators: int(req.Validators),
NonValidators: int(req.NonValidators),
Peers: int(req.Peers),
},
req.Msg,
)
return &emptypb.Empty{}, err
}