Skip to content

lnwire: encode channel_update type in onion errors #7665

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

Merged
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
19 changes: 5 additions & 14 deletions lnwire/onion_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -697,21 +697,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()
}

if err := WriteUint16(w, uint16(len(payload))); err != nil {
return err
return writeOnionErrorChanUpdate(w, f.Update, pver)
}

_, 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
Expand Down Expand Up @@ -1468,13 +1459,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
}
Expand Down
6 changes: 3 additions & 3 deletions lnwire/onion_error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down