-
Notifications
You must be signed in to change notification settings - Fork 1
/
node_info.go
175 lines (142 loc) · 4.73 KB
/
node_info.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package rpc
import (
"context"
"github.com/c13n-io/c13n-go/app"
"github.com/c13n-io/c13n-go/model"
pb "github.com/c13n-io/c13n-go/rpc/services"
"github.com/c13n-io/c13n-go/slog"
)
type nodeInfoServiceServer struct {
Log *slog.Logger
App *app.App
pb.UnimplementedNodeInfoServiceServer
}
func (s *nodeInfoServiceServer) logError(err error) error {
if err != nil {
s.Log.Errorf("%+v", err)
}
return err
}
// Interface implementation
// GetVersion returns information about the current c13n build.
func (s *nodeInfoServiceServer) GetVersion(_ context.Context,
_ *pb.VersionRequest) (*pb.Version, error) {
version := app.Version()
commit, commitHash := app.BuildInfo()
return &pb.Version{
Version: version,
Commit: commit,
CommitHash: commitHash,
}, nil
}
// GetSelfInfo queries the lightning network and returns
// the current underlying node's info.
func (s *nodeInfoServiceServer) GetSelfInfo(ctx context.Context, _ *pb.SelfInfoRequest) (*pb.SelfInfoResponse, error) {
nodeInfo, err := s.App.GetSelfInfo(ctx)
if err != nil {
return nil, associateStatusCode(s.logError(err))
}
selfInfo := nodeModelToNodeInfo(nodeInfo.Node)
chains := make([]*pb.Chain, len(nodeInfo.Chains))
for i, c := range nodeInfo.Chains {
chains[i] = &pb.Chain{
Chain: c.Chain,
Network: c.Network,
}
}
return &pb.SelfInfoResponse{
Info: selfInfo,
Chains: chains,
}, nil
}
// GetSelfBalance returns the total balance for the
// underlying lnd node, both for wallet and channels.
func (s *nodeInfoServiceServer) GetSelfBalance(ctx context.Context,
_ *pb.SelfBalanceRequest) (*pb.SelfBalanceResponse, error) {
balance, err := s.App.GetSelfBalance(ctx)
if err != nil {
return nil, associateStatusCode(s.logError(err))
}
return &pb.SelfBalanceResponse{
WalletConfirmedSat: balance.WalletConfirmedBalanceSat,
WalletUnconfirmedSat: balance.WalletUnconfirmedBalanceSat,
ChannelLocalMsat: balance.ChannelBalance.LocalMsat,
ChannelRemoteMsat: balance.ChannelBalance.RemoteMsat,
PendingOpenLocalMsat: balance.PendingOpenBalance.LocalMsat,
PendingOpenRemoteMsat: balance.PendingOpenBalance.RemoteMsat,
UnsettledLocalMsat: balance.UnsettledBalance.LocalMsat,
UnsettledRemoteMsat: balance.UnsettledBalance.RemoteMsat,
}, nil
}
// GetNodes queries the lightning network and returns
// a list of all visible nodes.
func (s *nodeInfoServiceServer) GetNodes(ctx context.Context, _ *pb.GetNodesRequest) (*pb.NodeInfoResponse, error) {
var nodes []model.Node
var err error
// Search everything
nodes, err = s.App.GetNodes(ctx)
if err != nil {
return nil, associateStatusCode(s.logError(err))
}
// Marshal data to result
responseNodes := make([]*pb.NodeInfo, len(nodes))
for i, u := range nodes {
responseNodes[i] = nodeModelToNodeInfo(u)
}
return &pb.NodeInfoResponse{
Nodes: responseNodes,
}, nil
}
// SearchNodeByAddress queries the lightning network and returns
// a list of all visible nodes, based on the requested address.
func (s *nodeInfoServiceServer) SearchNodeByAddress(ctx context.Context, req *pb.SearchNodeByAddressRequest) (*pb.NodeInfoResponse, error) {
var nodes []model.Node
var err error
nodes, err = s.App.GetNodesByAddress(ctx, req.GetAddress())
if err != nil {
return nil, associateStatusCode(s.logError(err))
}
// Marshal data to result
responseNodes := make([]*pb.NodeInfo, len(nodes))
for i, u := range nodes {
responseNodes[i] = nodeModelToNodeInfo(u)
}
return &pb.NodeInfoResponse{
Nodes: responseNodes,
}, nil
}
// SearchNodeByAlias queries the lightning network and returns
// a list of all visible nodes, based on the requested alias substring.
func (s *nodeInfoServiceServer) SearchNodeByAlias(ctx context.Context, req *pb.SearchNodeByAliasRequest) (*pb.NodeInfoResponse, error) {
var nodes []model.Node
var err error
nodes, err = s.App.GetNodesByAlias(ctx, req.GetAlias())
if err != nil {
return nil, associateStatusCode(s.logError(err))
}
// Marshal data to result
responseNodes := make([]*pb.NodeInfo, len(nodes))
for i, u := range nodes {
responseNodes[i] = nodeModelToNodeInfo(u)
}
return &pb.NodeInfoResponse{
Nodes: responseNodes,
}, nil
}
// ConnectNode creates a peer connection with a node
// as specified in the request parameters.
func (s *nodeInfoServiceServer) ConnectNode(ctx context.Context,
req *pb.ConnectNodeRequest) (*pb.ConnectNodeResponse, error) {
err := s.App.ConnectNode(ctx, req.GetAddress(), req.GetHostport())
if err != nil {
return nil, associateStatusCode(s.logError(err))
}
return &pb.ConnectNodeResponse{}, nil
}
// NewNodeInfoServiceServer initializes a new node info service.
func NewNodeInfoServiceServer(app *app.App) pb.NodeInfoServiceServer {
return &nodeInfoServiceServer{
Log: slog.NewLogger("node_info-service"),
App: app,
}
}