Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cln: replace glightning socket api with grpc api #215

Merged
merged 5 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
426 changes: 250 additions & 176 deletions cln/cln_client.go

Large diffs are not rendered by default.

16 changes: 12 additions & 4 deletions cln/custom_msg_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"context"
"encoding/binary"
"encoding/hex"
"fmt"
"log"
"sync"
"time"

"github.com/breez/lspd/cln/rpc"
"github.com/breez/lspd/cln_plugin/proto"
"github.com/breez/lspd/config"
"github.com/breez/lspd/lightning"
Expand Down Expand Up @@ -160,12 +162,18 @@ func (c *CustomMsgClient) Send(msg *lightning.CustomMessage) error {
var t [2]byte
binary.BigEndian.PutUint16(t[:], uint16(msg.Type))

m := hex.EncodeToString(t[:]) + hex.EncodeToString(msg.Data)
client, err := c.client.getClient()
peerid, err := hex.DecodeString(msg.PeerId)
if err != nil {
return err
return fmt.Errorf("failed to decode peer id %s: %w", msg.PeerId, err)
}
_, err = client.SendCustomMessage(msg.PeerId, m)

_, err = c.client.client.SendCustomMsg(
context.Background(),
&rpc.SendcustommsgRequest{
NodeId: peerid,
Msg: append(t[:], msg.Data...),
},
)
return err
}

Expand Down
84 changes: 43 additions & 41 deletions cln/forwards_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"strconv"
"time"

"github.com/breez/lspd/cln/rpc"
"github.com/breez/lspd/history"
"github.com/breez/lspd/lightning"
"github.com/elementsproject/glightning/glightning"
)

type ForwardSync struct {
Expand Down Expand Up @@ -61,7 +61,8 @@ func (s *ForwardSync) forwardCreatedSynchronizeOnce(ctx context.Context) {
var forwards []*history.Forward
var newCreatedOffset uint64
var err error
forwards, newCreatedOffset, endReached, err = s.listForwards("created", s.createdOffset, limit)
forwards, newCreatedOffset, endReached, err = s.listForwards(
rpc.ListforwardsRequest_CREATED, s.createdOffset, limit)
if err != nil {
log.Printf("forwardCreatedSynchronizeOnce(%x)- ListForwards error: %v", s.nodeid, err)
return
Expand Down Expand Up @@ -100,7 +101,8 @@ func (s *ForwardSync) forwardUpdatedSynchronizeOnce(ctx context.Context) {
var forwards []*history.Forward
var newUpdatedOffset uint64
var err error
forwards, newUpdatedOffset, endReached, err = s.listForwards("updated", s.updatedOffset, limit)
forwards, newUpdatedOffset, endReached, err = s.listForwards(
rpc.ListforwardsRequest_UPDATED, s.updatedOffset, limit)
if err != nil {
log.Printf("forwardUpdatedSynchronizeOnce(%x)- ListForwards error: %v", s.nodeid, err)
return
Expand Down Expand Up @@ -129,22 +131,22 @@ func (s *ForwardSync) forwardUpdatedSynchronizeOnce(ctx context.Context) {
log.Printf("forwardUpdatedSynchronizeOnce(%x) - Done", s.nodeid)
}

func (s *ForwardSync) listForwards(index string, offset uint64, limit uint32) ([]*history.Forward, uint64, bool, error) {
var response struct {
Forwards []Forward `json:"forwards"`
}

client, err := s.client.getClient()
if err != nil {
return nil, 0, false, err
}

err = client.Request(&glightning.ListForwardsRequest{
Status: "settled",
Index: index,
Start: offset,
Limit: limit * 2,
}, &response)
func (s *ForwardSync) listForwards(
index rpc.ListforwardsRequest_ListforwardsIndex,
offset uint64,
limit uint32,
) ([]*history.Forward, uint64, bool, error) {
rLimit := limit * 2
status := rpc.ListforwardsRequest_SETTLED
response, err := s.client.client.ListForwards(
context.Background(),
&rpc.ListforwardsRequest{
Status: &status,
Index: &index,
Start: &offset,
Limit: &rLimit,
},
)
if err != nil {
return nil, 0, false, err
}
Expand All @@ -153,28 +155,41 @@ func (s *ForwardSync) listForwards(index string, offset uint64, limit uint32) ([
endReached := len(response.Forwards) < int(limit)
var lastIndex *uint64
for _, forward := range response.Forwards {
if forward.OutChannel == nil {
continue
}
if forward.CreatedIndex == nil {
continue
}
if forward.InMsat == nil {
continue
}
if forward.OutMsat == nil {
continue
}

in, err := lightning.NewShortChannelIDFromString(forward.InChannel)
if err != nil {
return nil, 0, false, fmt.Errorf("NewShortChannelIDFromString(%s) error: %w", forward.InChannel, err)
}
out, err := lightning.NewShortChannelIDFromString(forward.OutChannel)
out, err := lightning.NewShortChannelIDFromString(*forward.OutChannel)
if err != nil {
return nil, 0, false, fmt.Errorf("NewShortChannelIDFromString(%s) error: %w", forward.OutChannel, err)
return nil, 0, false, fmt.Errorf("NewShortChannelIDFromString(%s) error: %w", *forward.OutChannel, err)
}

sec, dec := math.Modf(forward.ResolvedTime)
sec, dec := math.Modf(forward.ReceivedTime)
result = append(result, &history.Forward{
Identifier: strconv.FormatUint(forward.CreatedIndex, 10),
Identifier: strconv.FormatUint(*forward.CreatedIndex, 10),
InChannel: *in,
OutChannel: *out,
InMsat: forward.InMsat.MSat(),
OutMsat: forward.OutMsat.MSat(),
InMsat: forward.InMsat.Msat,
OutMsat: forward.OutMsat.Msat,
ResolvedTime: time.Unix(int64(sec), int64(dec*(1e9))),
})
if index == "created" {
lastIndex = &forward.CreatedIndex
if index == rpc.ListforwardsRequest_CREATED {
lastIndex = forward.CreatedIndex
} else {
lastIndex = &forward.UpdatedIndex
lastIndex = forward.UpdatedIndex
}
}

Expand All @@ -186,16 +201,3 @@ func (s *ForwardSync) listForwards(index string, offset uint64, limit uint32) ([
}
return result, newOffset, endReached, nil
}

type Forward struct {
CreatedIndex uint64 `json:"created_index"`
UpdatedIndex uint64 `json:"updated_index"`
InChannel string `json:"in_channel"`
OutChannel string `json:"out_channel"`
InMsat glightning.Amount `json:"in_msat"`
OutMsat glightning.Amount `json:"out_msat"`
Status string `json:"status"`
PaymentHash string `json:"payment_hash"`
ReceivedTime float64 `json:"received_time"`
ResolvedTime float64 `json:"resolved_time"`
}
4 changes: 4 additions & 0 deletions cln/rpc/genproto.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
SCRIPTDIR=$(dirname $0)

protoc --go_out=$SCRIPTDIR --go_opt=paths=source_relative --go-grpc_out=$SCRIPTDIR --go-grpc_opt=paths=source_relative -I=$SCRIPTDIR $SCRIPTDIR/*.proto
Loading
Loading