Skip to content

Commit aa6154d

Browse files
committed
lnrpc+rpcserver: add and populate custom channel data
1 parent 643cdc2 commit aa6154d

File tree

4 files changed

+143
-7
lines changed

4 files changed

+143
-7
lines changed

lnrpc/lightning.pb.go

Lines changed: 44 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lnrpc/lightning.proto

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,6 +1590,11 @@ message Channel {
15901590
the channel's operation.
15911591
*/
15921592
string memo = 36;
1593+
1594+
/*
1595+
Custom channel data that might be populated in custom channels.
1596+
*/
1597+
bytes custom_channel_data = 37;
15931598
}
15941599

15951600
message ListChannelsRequest {
@@ -2707,6 +2712,11 @@ message PendingChannelsResponse {
27072712
impacts the channel's operation.
27082713
*/
27092714
string memo = 13;
2715+
2716+
/*
2717+
Custom channel data that might be populated in custom channels.
2718+
*/
2719+
bytes custom_channel_data = 34;
27102720
}
27112721

27122722
message PendingOpenChannel {
@@ -2966,6 +2976,12 @@ message ChannelBalanceResponse {
29662976

29672977
// Sum of channels pending remote balances.
29682978
Amount pending_open_remote_balance = 8;
2979+
2980+
/*
2981+
Custom channel data that might be populated if there are custom channels
2982+
present.
2983+
*/
2984+
bytes custom_channel_data = 9;
29692985
}
29702986

29712987
message QueryRoutesRequest {

lnrpc/lightning.swagger.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3111,6 +3111,11 @@
31113111
"memo": {
31123112
"type": "string",
31133113
"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."
3114+
},
3115+
"custom_channel_data": {
3116+
"type": "string",
3117+
"format": "byte",
3118+
"description": "Custom channel data that might be populated in custom channels."
31143119
}
31153120
}
31163121
},
@@ -3805,6 +3810,11 @@
38053810
"memo": {
38063811
"type": "string",
38073812
"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."
3813+
},
3814+
"custom_channel_data": {
3815+
"type": "string",
3816+
"format": "byte",
3817+
"description": "Custom channel data that might be populated in custom channels."
38083818
}
38093819
}
38103820
},
@@ -4008,6 +4018,11 @@
40084018
"pending_open_remote_balance": {
40094019
"$ref": "#/definitions/lnrpcAmount",
40104020
"description": "Sum of channels pending remote balances."
4021+
},
4022+
"custom_channel_data": {
4023+
"type": "string",
4024+
"format": "byte",
4025+
"description": "Custom channel data that might be populated if there are custom channels\npresent."
40114026
}
40124027
}
40134028
},

rpcserver.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3477,13 +3477,20 @@ func (r *rpcServer) ChannelBalance(ctx context.Context,
34773477
unsettledRemoteBalance lnwire.MilliSatoshi
34783478
pendingOpenLocalBalance lnwire.MilliSatoshi
34793479
pendingOpenRemoteBalance lnwire.MilliSatoshi
3480+
customDataBuf bytes.Buffer
34803481
)
34813482

34823483
openChannels, err := r.server.chanStateDB.FetchAllOpenChannels()
34833484
if err != nil {
34843485
return nil, err
34853486
}
34863487

3488+
// Encode the number of open channels to the custom data buffer.
3489+
err = wire.WriteVarInt(&customDataBuf, 0, uint64(len(openChannels)))
3490+
if err != nil {
3491+
return nil, err
3492+
}
3493+
34873494
for _, channel := range openChannels {
34883495
c := channel.LocalCommitment
34893496
localBalance += c.LocalBalance
@@ -3497,17 +3504,37 @@ func (r *rpcServer) ChannelBalance(ctx context.Context,
34973504
unsettledRemoteBalance += htlc.Amt
34983505
}
34993506
}
3507+
3508+
// Encode the custom data for this open channel.
3509+
openChanData := channel.LocalCommitment.CustomBlob.UnwrapOr(nil)
3510+
err = wire.WriteVarBytes(&customDataBuf, 0, openChanData)
3511+
if err != nil {
3512+
return nil, err
3513+
}
35003514
}
35013515

35023516
pendingChannels, err := r.server.chanStateDB.FetchPendingChannels()
35033517
if err != nil {
35043518
return nil, err
35053519
}
35063520

3521+
// Encode the number of pending channels to the custom data buffer.
3522+
err = wire.WriteVarInt(&customDataBuf, 0, uint64(len(pendingChannels)))
3523+
if err != nil {
3524+
return nil, err
3525+
}
3526+
35073527
for _, channel := range pendingChannels {
35083528
c := channel.LocalCommitment
35093529
pendingOpenLocalBalance += c.LocalBalance
35103530
pendingOpenRemoteBalance += c.RemoteBalance
3531+
3532+
// Encode the custom data for this pending channel.
3533+
openChanData := channel.LocalCommitment.CustomBlob.UnwrapOr(nil)
3534+
err = wire.WriteVarBytes(&customDataBuf, 0, openChanData)
3535+
if err != nil {
3536+
return nil, err
3537+
}
35113538
}
35123539

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

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

3633+
customChanBytes, err := encodeCustomChanData(pendingChan)
3634+
if err != nil {
3635+
return nil, fmt.Errorf("unable to encode open chan "+
3636+
"data: %w", err)
3637+
}
3638+
36053639
result[i] = &lnrpc.PendingChannelsResponse_PendingOpenChannel{
36063640
Channel: &lnrpc.PendingChannelsResponse_PendingChannel{
36073641
RemoteNodePub: hex.EncodeToString(pub),
@@ -3615,6 +3649,7 @@ func (r *rpcServer) fetchPendingOpenChannels() (pendingOpenChannels, error) {
36153649
CommitmentType: rpcCommitmentType(pendingChan.ChanType),
36163650
Private: isPrivate(pendingChan),
36173651
Memo: string(pendingChan.Memo),
3652+
CustomChannelData: customChanBytes,
36183653
},
36193654
CommitWeight: commitWeight,
36203655
CommitFee: int64(localCommitment.CommitFee),
@@ -4345,6 +4380,30 @@ func isPrivate(dbChannel *channeldb.OpenChannel) bool {
43454380
return dbChannel.ChannelFlags&lnwire.FFAnnounceChannel != 1
43464381
}
43474382

4383+
// encodeCustomChanData encodes the custom channel data for the open channel.
4384+
// It encodes that data as a pair of var bytes blobs.
4385+
func encodeCustomChanData(lnChan *channeldb.OpenChannel) ([]byte, error) {
4386+
customOpenChanData := lnChan.CustomBlob.UnwrapOr(nil)
4387+
customLocalCommitData := lnChan.LocalCommitment.CustomBlob.UnwrapOr(nil)
4388+
4389+
// We'll encode our custom channel data as two blobs. The first is a
4390+
// set of var bytes encoding of the open chan data, the second is an
4391+
// encoding of the local commitment data.
4392+
var customChanDataBuf bytes.Buffer
4393+
err := wire.WriteVarBytes(&customChanDataBuf, 0, customOpenChanData)
4394+
if err != nil {
4395+
return nil, fmt.Errorf("unable to encode open chan "+
4396+
"data: %w", err)
4397+
}
4398+
err = wire.WriteVarBytes(&customChanDataBuf, 0, customLocalCommitData)
4399+
if err != nil {
4400+
return nil, fmt.Errorf("unable to encode local commit "+
4401+
"data: %w", err)
4402+
}
4403+
4404+
return customChanDataBuf.Bytes(), nil
4405+
}
4406+
43484407
// createRPCOpenChannel creates an *lnrpc.Channel from the *channeldb.Channel.
43494408
func createRPCOpenChannel(r *rpcServer, dbChannel *channeldb.OpenChannel,
43504409
isActive, peerAliasLookup bool) (*lnrpc.Channel, error) {
@@ -4399,6 +4458,14 @@ func createRPCOpenChannel(r *rpcServer, dbChannel *channeldb.OpenChannel,
43994458
// is returned and peerScidAlias will be an empty ShortChannelID.
44004459
peerScidAlias, _ := r.server.aliasMgr.GetPeerAlias(chanID)
44014460

4461+
// Finally we'll attempt to encode the custom channel data if any
4462+
// exists.
4463+
customChanBytes, err := encodeCustomChanData(dbChannel)
4464+
if err != nil {
4465+
return nil, fmt.Errorf("unable to encode open chan data: %w",
4466+
err)
4467+
}
4468+
44024469
channel := &lnrpc.Channel{
44034470
Active: isActive,
44044471
Private: isPrivate(dbChannel),
@@ -4431,6 +4498,7 @@ func createRPCOpenChannel(r *rpcServer, dbChannel *channeldb.OpenChannel,
44314498
ZeroConf: dbChannel.IsZeroConf(),
44324499
ZeroConfConfirmedScid: dbChannel.ZeroConfRealScid().ToUint64(),
44334500
Memo: string(dbChannel.Memo),
4501+
CustomChannelData: customChanBytes,
44344502
// TODO: remove the following deprecated fields
44354503
CsvDelay: uint32(dbChannel.LocalChanCfg.CsvDelay),
44364504
LocalChanReserveSat: int64(dbChannel.LocalChanCfg.ChanReserve),

0 commit comments

Comments
 (0)