Skip to content

Commit

Permalink
lnrpc+rpcserver: add and populate custom channel data
Browse files Browse the repository at this point in the history
  • Loading branch information
guggero committed May 21, 2024
1 parent 643cdc2 commit aa6154d
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 7 deletions.
51 changes: 44 additions & 7 deletions lnrpc/lightning.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions lnrpc/lightning.proto
Original file line number Diff line number Diff line change
Expand Up @@ -1590,6 +1590,11 @@ message Channel {
the channel's operation.
*/
string memo = 36;

/*
Custom channel data that might be populated in custom channels.
*/
bytes custom_channel_data = 37;
}

message ListChannelsRequest {
Expand Down Expand Up @@ -2707,6 +2712,11 @@ message PendingChannelsResponse {
impacts the channel's operation.
*/
string memo = 13;

/*
Custom channel data that might be populated in custom channels.
*/
bytes custom_channel_data = 34;
}

message PendingOpenChannel {
Expand Down Expand Up @@ -2966,6 +2976,12 @@ message ChannelBalanceResponse {

// Sum of channels pending remote balances.
Amount pending_open_remote_balance = 8;

/*
Custom channel data that might be populated if there are custom channels
present.
*/
bytes custom_channel_data = 9;
}

message QueryRoutesRequest {
Expand Down
15 changes: 15 additions & 0 deletions lnrpc/lightning.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -3111,6 +3111,11 @@
"memo": {
"type": "string",
"description": "An optional note-to-self to go along with the channel containing some\nuseful information. This is only ever stored locally and in no way\nimpacts the channel's operation."
},
"custom_channel_data": {
"type": "string",
"format": "byte",
"description": "Custom channel data that might be populated in custom channels."
}
}
},
Expand Down Expand Up @@ -3805,6 +3810,11 @@
"memo": {
"type": "string",
"description": "An optional note-to-self to go along with the channel containing some\nuseful information. This is only ever stored locally and in no way impacts\nthe channel's operation."
},
"custom_channel_data": {
"type": "string",
"format": "byte",
"description": "Custom channel data that might be populated in custom channels."
}
}
},
Expand Down Expand Up @@ -4008,6 +4018,11 @@
"pending_open_remote_balance": {
"$ref": "#/definitions/lnrpcAmount",
"description": "Sum of channels pending remote balances."
},
"custom_channel_data": {
"type": "string",
"format": "byte",
"description": "Custom channel data that might be populated if there are custom channels\npresent."
}
}
},
Expand Down
68 changes: 68 additions & 0 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3477,13 +3477,20 @@ func (r *rpcServer) ChannelBalance(ctx context.Context,
unsettledRemoteBalance lnwire.MilliSatoshi
pendingOpenLocalBalance lnwire.MilliSatoshi
pendingOpenRemoteBalance lnwire.MilliSatoshi
customDataBuf bytes.Buffer
)

openChannels, err := r.server.chanStateDB.FetchAllOpenChannels()
if err != nil {
return nil, err
}

// Encode the number of open channels to the custom data buffer.
err = wire.WriteVarInt(&customDataBuf, 0, uint64(len(openChannels)))
if err != nil {
return nil, err
}

for _, channel := range openChannels {
c := channel.LocalCommitment
localBalance += c.LocalBalance
Expand All @@ -3497,17 +3504,37 @@ func (r *rpcServer) ChannelBalance(ctx context.Context,
unsettledRemoteBalance += htlc.Amt
}
}

// Encode the custom data for this open channel.
openChanData := channel.LocalCommitment.CustomBlob.UnwrapOr(nil)
err = wire.WriteVarBytes(&customDataBuf, 0, openChanData)
if err != nil {
return nil, err
}
}

pendingChannels, err := r.server.chanStateDB.FetchPendingChannels()
if err != nil {
return nil, err
}

// Encode the number of pending channels to the custom data buffer.
err = wire.WriteVarInt(&customDataBuf, 0, uint64(len(pendingChannels)))
if err != nil {
return nil, err
}

for _, channel := range pendingChannels {
c := channel.LocalCommitment
pendingOpenLocalBalance += c.LocalBalance
pendingOpenRemoteBalance += c.RemoteBalance

// Encode the custom data for this pending channel.
openChanData := channel.LocalCommitment.CustomBlob.UnwrapOr(nil)
err = wire.WriteVarBytes(&customDataBuf, 0, openChanData)
if err != nil {
return nil, err
}
}

rpcsLog.Debugf("[channelbalance] local_balance=%v remote_balance=%v "+
Expand Down Expand Up @@ -3542,6 +3569,7 @@ func (r *rpcServer) ChannelBalance(ctx context.Context,
Sat: uint64(pendingOpenRemoteBalance.ToSatoshis()),
Msat: uint64(pendingOpenRemoteBalance),
},
CustomChannelData: customDataBuf.Bytes(),

// Deprecated fields.
Balance: int64(localBalance.ToSatoshis()),
Expand Down Expand Up @@ -3602,6 +3630,12 @@ func (r *rpcServer) fetchPendingOpenChannels() (pendingOpenChannels, error) {
pendingChan.BroadcastHeight()
fundingExpiryBlocks := int32(maxFundingHeight) - currentHeight

customChanBytes, err := encodeCustomChanData(pendingChan)
if err != nil {
return nil, fmt.Errorf("unable to encode open chan "+
"data: %w", err)
}

result[i] = &lnrpc.PendingChannelsResponse_PendingOpenChannel{
Channel: &lnrpc.PendingChannelsResponse_PendingChannel{
RemoteNodePub: hex.EncodeToString(pub),
Expand All @@ -3615,6 +3649,7 @@ func (r *rpcServer) fetchPendingOpenChannels() (pendingOpenChannels, error) {
CommitmentType: rpcCommitmentType(pendingChan.ChanType),
Private: isPrivate(pendingChan),
Memo: string(pendingChan.Memo),
CustomChannelData: customChanBytes,
},
CommitWeight: commitWeight,
CommitFee: int64(localCommitment.CommitFee),
Expand Down Expand Up @@ -4345,6 +4380,30 @@ func isPrivate(dbChannel *channeldb.OpenChannel) bool {
return dbChannel.ChannelFlags&lnwire.FFAnnounceChannel != 1
}

// encodeCustomChanData encodes the custom channel data for the open channel.
// It encodes that data as a pair of var bytes blobs.
func encodeCustomChanData(lnChan *channeldb.OpenChannel) ([]byte, error) {
customOpenChanData := lnChan.CustomBlob.UnwrapOr(nil)
customLocalCommitData := lnChan.LocalCommitment.CustomBlob.UnwrapOr(nil)

// We'll encode our custom channel data as two blobs. The first is a
// set of var bytes encoding of the open chan data, the second is an
// encoding of the local commitment data.
var customChanDataBuf bytes.Buffer
err := wire.WriteVarBytes(&customChanDataBuf, 0, customOpenChanData)
if err != nil {
return nil, fmt.Errorf("unable to encode open chan "+
"data: %w", err)
}
err = wire.WriteVarBytes(&customChanDataBuf, 0, customLocalCommitData)
if err != nil {
return nil, fmt.Errorf("unable to encode local commit "+
"data: %w", err)
}

return customChanDataBuf.Bytes(), nil
}

// createRPCOpenChannel creates an *lnrpc.Channel from the *channeldb.Channel.
func createRPCOpenChannel(r *rpcServer, dbChannel *channeldb.OpenChannel,
isActive, peerAliasLookup bool) (*lnrpc.Channel, error) {
Expand Down Expand Up @@ -4399,6 +4458,14 @@ func createRPCOpenChannel(r *rpcServer, dbChannel *channeldb.OpenChannel,
// is returned and peerScidAlias will be an empty ShortChannelID.
peerScidAlias, _ := r.server.aliasMgr.GetPeerAlias(chanID)

// Finally we'll attempt to encode the custom channel data if any
// exists.
customChanBytes, err := encodeCustomChanData(dbChannel)
if err != nil {
return nil, fmt.Errorf("unable to encode open chan data: %w",
err)
}

channel := &lnrpc.Channel{
Active: isActive,
Private: isPrivate(dbChannel),
Expand Down Expand Up @@ -4431,6 +4498,7 @@ func createRPCOpenChannel(r *rpcServer, dbChannel *channeldb.OpenChannel,
ZeroConf: dbChannel.IsZeroConf(),
ZeroConfConfirmedScid: dbChannel.ZeroConfRealScid().ToUint64(),
Memo: string(dbChannel.Memo),
CustomChannelData: customChanBytes,
// TODO: remove the following deprecated fields
CsvDelay: uint32(dbChannel.LocalChanCfg.CsvDelay),
LocalChanReserveSat: int64(dbChannel.LocalChanCfg.ChanReserve),
Expand Down

0 comments on commit aa6154d

Please sign in to comment.