-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpcserver.go
68 lines (57 loc) · 1.76 KB
/
rpcserver.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
package main
import (
"github.com/go-errors/errors"
"github.com/the-lightning-land/balanced/balancer"
"github.com/the-lightning-land/balanced/bdb"
"github.com/the-lightning-land/balanced/rpc"
"golang.org/x/net/context"
)
type rpcServerConfig struct {
balancer *balancer.Balancer
version string
commit string
}
type rpcServer struct {
balancer *balancer.Balancer
version string
commit string
}
// A compile time check to ensure that rpcServer fully implements the SweetServer gRPC service.
var _ rpc.BalanceServer = (*rpcServer)(nil)
func newRPCServer(config *rpcServerConfig) *rpcServer {
return &rpcServer{
balancer: config.balancer,
version: config.version,
commit: config.commit,
}
}
func (s *rpcServer) GetInfo(ctx context.Context, req *rpc.GetInfoRequest) (*rpc.GetInfoResponse, error) {
identityPubKey, _ := s.balancer.IdentityPubKey()
return &rpc.GetInfoResponse{
Version: s.version,
Commit: s.commit,
IdentityPubKey: string(identityPubKey),
}, nil
}
func (s *rpcServer) Balance(ctx context.Context, req *rpc.BalanceRequest) (*rpc.BalanceResponse, error) {
var err error
rebalanced := false
amtMsat := int64(500000000)
if req.AmtMsat != 0 {
amtMsat = int64(req.AmtMsat)
}
if req.FromChanId != 0 && req.ToChanId == 0 {
rebalanced, err = s.balancer.Balance(amtMsat, bdb.ChanId(req.FromChanId), bdb.ChanId(req.ToChanId))
if err != nil {
return nil, errors.Errorf("Could not balance channel: %v", err)
}
} else if req.FromChanId != 0 && req.ToChanId != 0 {
rebalanced, err = s.balancer.Balance(amtMsat, bdb.ChanId(req.FromChanId), bdb.ChanId(req.ToChanId))
if err != nil {
return nil, errors.Errorf("Could not balance channel: %v", err)
}
}
return &rpc.BalanceResponse{
Rebalanced: rebalanced,
}, nil
}