lnwire: encode channel_update type in onion errors

For about a year [1], the spec has prescribed encoding channel_updates
with their type prefix (0x0102) in onion failure messages. LND can
decode correctly with or without the prefix but hasn't been writing the
prefix during encoding. This commit starts writing the prefix.

[1] https://github.com/lightning/bolts/pull/979
This commit is contained in:
Matt Morehouse 2023-05-03 15:46:32 -05:00 committed by Olaoluwa Osuntokun
parent 53b33c75c4
commit 3549e329df
2 changed files with 8 additions and 17 deletions

View File

@ -698,21 +698,12 @@ func (f *FailTemporaryChannelFailure) Decode(r io.Reader, pver uint32) error {
func (f *FailTemporaryChannelFailure) Encode(w *bytes.Buffer,
pver uint32) error {
var payload []byte
if f.Update != nil {
var bw bytes.Buffer
if err := f.Update.Encode(&bw, pver); err != nil {
return err
}
payload = bw.Bytes()
return writeOnionErrorChanUpdate(w, f.Update, pver)
}
if err := WriteUint16(w, uint16(len(payload))); err != nil {
return err
}
_, err := w.Write(payload)
return err
// Write zero length to indicate no channel_update is present.
return WriteUint16(w, 0)
}
// FailAmountBelowMinimum is returned if the HTLC does not reach the current
@ -1474,13 +1465,13 @@ func writeOnionErrorChanUpdate(w *bytes.Buffer, chanUpdate *ChannelUpdate,
// First, we encode the channel update in a temporary buffer in order
// to get the exact serialized size.
var b bytes.Buffer
if err := chanUpdate.Encode(&b, pver); err != nil {
updateLen, err := WriteMessage(&b, chanUpdate, pver)
if err != nil {
return err
}
// Now that we know the size, we can write the length out in the main
// writer.
updateLen := b.Len()
if err := WriteUint16(w, uint16(updateLen)); err != nil {
return err
}

View File

@ -184,15 +184,15 @@ func TestWriteOnionErrorChanUpdate(t *testing.T) {
// raw serialized length.
var b bytes.Buffer
update := testChannelUpdate
if err := update.Encode(&b, 0); err != nil {
trueUpdateLength, err := WriteMessage(&b, &update, 0)
if err != nil {
t.Fatalf("unable to write update: %v", err)
}
trueUpdateLength := b.Len()
// Next, we'll use the function to encode the update as we would in a
// onion error message.
var errorBuf bytes.Buffer
err := writeOnionErrorChanUpdate(&errorBuf, &update, 0)
err = writeOnionErrorChanUpdate(&errorBuf, &update, 0)
require.NoError(t, err, "unable to encode onion error")
// Finally, read the length encoded and ensure that it matches the raw