Merge pull request #5346 from joostjager/custom-messages

lnrpc+peer: custom peer messages
This commit is contained in:
Olaoluwa Osuntokun authored and GitHub committed 2021-10-18 19:07:39 -07:00
commit e6c65f1cbd
18 files changed
+5041 -3845

No files matched your search

+64
View File
@@ -568,6 +568,14 @@ func MainRPCServerPermissions() map[string][]bakery.Op {
Entity: "macaroon",
Action: "write",
}},
"/lnrpc.Lightning/SendCustomMessage": {{
Entity: "offchain",
Action: "write",
}},
"/lnrpc.Lightning/SubscribeCustomMessages": {{
Entity: "offchain",
Action: "read",
}},
}
}
@@ -7326,3 +7334,59 @@ func (r *rpcServer) RegisterRPCMiddleware(
return middleware.Run()
}
// SendCustomMessage sends a custom peer message.
func (r *rpcServer) SendCustomMessage(ctx context.Context, req *lnrpc.SendCustomMessageRequest) (
*lnrpc.SendCustomMessageResponse, error) {
peer, err := route.NewVertexFromBytes(req.Peer)
if err != nil {
return nil, err
}
err = r.server.SendCustomMessage(
peer, lnwire.MessageType(req.Type), req.Data,
)
switch {
case err == ErrPeerNotConnected:
return nil, status.Error(codes.NotFound, err.Error())
case err != nil:
return nil, err
}
return &lnrpc.SendCustomMessageResponse{}, nil
}
// SubscribeCustomMessages subscribes to a stream of incoming custom peer
// messages.
func (r *rpcServer) SubscribeCustomMessages(req *lnrpc.SubscribeCustomMessagesRequest,
server lnrpc.Lightning_SubscribeCustomMessagesServer) error {
client, err := r.server.SubscribeCustomMessages()
if err != nil {
return err
}
defer client.Cancel()
for {
select {
case <-client.Quit():
return errors.New("shutdown")
case <-server.Context().Done():
return server.Context().Err()
case update := <-client.Updates():
customMsg := update.(*CustomMessage)
err := server.Send(&lnrpc.CustomMessage{
Peer: customMsg.Peer[:],
Data: customMsg.Msg.Data,
Type: uint32(customMsg.Msg.Type),
})
if err != nil {
return err
}
}
}
}