Skip to content

Commit f0b1015

Browse files
authored
Merge pull request #9187 from guggero/fix-custom-data
rpcserver+lncli: fix custom channel data encoding issue
2 parents 5f86e25 + 8c79940 commit f0b1015

File tree

3 files changed

+15
-20
lines changed

3 files changed

+15
-20
lines changed

cmd/commands/commands.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ var (
5454

5555
// replaceCustomData replaces the custom channel data hex string with the
5656
// decoded custom channel data in the JSON response.
57-
func replaceCustomData(jsonBytes []byte) ([]byte, error) {
57+
func replaceCustomData(jsonBytes []byte) []byte {
5858
// If there's nothing to replace, return the original JSON.
5959
if !customDataPattern.Match(jsonBytes) {
60-
return jsonBytes, nil
60+
return jsonBytes
6161
}
6262

6363
replacedBytes := customDataPattern.ReplaceAllFunc(
@@ -78,10 +78,12 @@ func replaceCustomData(jsonBytes []byte) ([]byte, error) {
7878
var buf bytes.Buffer
7979
err := json.Indent(&buf, replacedBytes, "", " ")
8080
if err != nil {
81-
return nil, err
81+
// If we can't indent the JSON, it likely means the replacement
82+
// data wasn't correct, so we return the original JSON.
83+
return jsonBytes
8284
}
8385

84-
return buf.Bytes(), nil
86+
return buf.Bytes()
8587
}
8688

8789
func getContext() context.Context {
@@ -118,11 +120,7 @@ func printRespJSON(resp proto.Message) {
118120
return
119121
}
120122

121-
jsonBytesReplaced, err := replaceCustomData(jsonBytes)
122-
if err != nil {
123-
fmt.Println("unable to replace custom data: ", err)
124-
jsonBytesReplaced = jsonBytes
125-
}
123+
jsonBytesReplaced := replaceCustomData(jsonBytes)
126124

127125
fmt.Printf("%s\n", jsonBytesReplaced)
128126
}

cmd/commands/commands_test.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ func TestReplaceCustomData(t *testing.T) {
131131
data string
132132
replaceData string
133133
expected string
134-
expectedErr string
135134
}{
136135
{
137136
name: "no replacement necessary",
@@ -175,7 +174,8 @@ func TestReplaceCustomData(t *testing.T) {
175174
name: "invalid json",
176175
data: "this ain't json, " +
177176
"\"custom_channel_data\":\"a\"",
178-
expectedErr: "invalid character 'h' in literal true",
177+
expected: "this ain't json, " +
178+
"\"custom_channel_data\":\"a\"",
179179
},
180180
{
181181
name: "valid json, invalid hex, just formatted",
@@ -186,15 +186,7 @@ func TestReplaceCustomData(t *testing.T) {
186186

187187
for _, tc := range testCases {
188188
t.Run(tc.name, func(t *testing.T) {
189-
result, err := replaceCustomData([]byte(tc.data))
190-
191-
if tc.expectedErr != "" {
192-
require.ErrorContains(t, err, tc.expectedErr)
193-
return
194-
}
195-
196-
require.NoError(t, err)
197-
189+
result := replaceCustomData([]byte(tc.data))
198190
require.Equal(t, tc.expected, string(result))
199191
})
200192
}

rpcserver.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4606,6 +4606,11 @@ func encodeCustomChanData(lnChan *channeldb.OpenChannel) ([]byte, error) {
46064606
customOpenChanData := lnChan.CustomBlob.UnwrapOr(nil)
46074607
customLocalCommitData := lnChan.LocalCommitment.CustomBlob.UnwrapOr(nil)
46084608

4609+
// Don't write any custom data if both blobs are empty.
4610+
if len(customOpenChanData) == 0 && len(customLocalCommitData) == 0 {
4611+
return nil, nil
4612+
}
4613+
46094614
// We'll encode our custom channel data as two blobs. The first is a
46104615
// set of var bytes encoding of the open chan data, the second is an
46114616
// encoding of the local commitment data.

0 commit comments

Comments
 (0)