mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-08-27 14:11:04 +02:00
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:
committed by
Olaoluwa Osuntokun
parent
53b33c75c4
commit
3549e329df
@@ -698,21 +698,12 @@ func (f *FailTemporaryChannelFailure) Decode(r io.Reader, pver uint32) error {
|
|||||||
func (f *FailTemporaryChannelFailure) Encode(w *bytes.Buffer,
|
func (f *FailTemporaryChannelFailure) Encode(w *bytes.Buffer,
|
||||||
pver uint32) error {
|
pver uint32) error {
|
||||||
|
|
||||||
var payload []byte
|
|
||||||
if f.Update != nil {
|
if f.Update != nil {
|
||||||
var bw bytes.Buffer
|
return writeOnionErrorChanUpdate(w, f.Update, pver)
|
||||||
if err := f.Update.Encode(&bw, pver); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
payload = bw.Bytes()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := WriteUint16(w, uint16(len(payload))); err != nil {
|
// Write zero length to indicate no channel_update is present.
|
||||||
return err
|
return WriteUint16(w, 0)
|
||||||
}
|
|
||||||
|
|
||||||
_, err := w.Write(payload)
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// FailAmountBelowMinimum is returned if the HTLC does not reach the current
|
// 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
|
// First, we encode the channel update in a temporary buffer in order
|
||||||
// to get the exact serialized size.
|
// to get the exact serialized size.
|
||||||
var b bytes.Buffer
|
var b bytes.Buffer
|
||||||
if err := chanUpdate.Encode(&b, pver); err != nil {
|
updateLen, err := WriteMessage(&b, chanUpdate, pver)
|
||||||
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now that we know the size, we can write the length out in the main
|
// Now that we know the size, we can write the length out in the main
|
||||||
// writer.
|
// writer.
|
||||||
updateLen := b.Len()
|
|
||||||
if err := WriteUint16(w, uint16(updateLen)); err != nil {
|
if err := WriteUint16(w, uint16(updateLen)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@@ -184,15 +184,15 @@ func TestWriteOnionErrorChanUpdate(t *testing.T) {
|
|||||||
// raw serialized length.
|
// raw serialized length.
|
||||||
var b bytes.Buffer
|
var b bytes.Buffer
|
||||||
update := testChannelUpdate
|
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)
|
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
|
// Next, we'll use the function to encode the update as we would in a
|
||||||
// onion error message.
|
// onion error message.
|
||||||
var errorBuf bytes.Buffer
|
var errorBuf bytes.Buffer
|
||||||
err := writeOnionErrorChanUpdate(&errorBuf, &update, 0)
|
err = writeOnionErrorChanUpdate(&errorBuf, &update, 0)
|
||||||
require.NoError(t, err, "unable to encode onion error")
|
require.NoError(t, err, "unable to encode onion error")
|
||||||
|
|
||||||
// Finally, read the length encoded and ensure that it matches the raw
|
// Finally, read the length encoded and ensure that it matches the raw
|
||||||
|
Reference in New Issue
Block a user