mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-03-29 03:01:52 +01:00
lnwire: add custom records field to type UpdateFulfillHtlc
- Introduce the field `CustomRecords` to the type `UpdateFulfillHtlc`. - Encode and decode the new field into the `ExtraData` field of the `update_fulfill_htlc` wire message. - Empty `ExtraData` field is set to `nil`.
This commit is contained in:
parent
6fa2db0663
commit
d247cc9498
@ -38,7 +38,6 @@ func TestNetworkResultSerialization(t *testing.T) {
|
|||||||
ChanID: chanID,
|
ChanID: chanID,
|
||||||
ID: 2,
|
ID: 2,
|
||||||
PaymentPreimage: preimage,
|
PaymentPreimage: preimage,
|
||||||
ExtraData: make([]byte, 0),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fail := &lnwire.UpdateFailHTLC{
|
fail := &lnwire.UpdateFailHTLC{
|
||||||
|
@ -2,7 +2,10 @@ package lnwire
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
|
"github.com/lightningnetwork/lnd/tlv"
|
||||||
)
|
)
|
||||||
|
|
||||||
// UpdateFulfillHTLC is sent by Alice to Bob when she wishes to settle a
|
// UpdateFulfillHTLC is sent by Alice to Bob when she wishes to settle a
|
||||||
@ -23,6 +26,10 @@ type UpdateFulfillHTLC struct {
|
|||||||
// HTLC.
|
// HTLC.
|
||||||
PaymentPreimage [32]byte
|
PaymentPreimage [32]byte
|
||||||
|
|
||||||
|
// CustomRecords maps TLV types to byte slices, storing arbitrary data
|
||||||
|
// intended for inclusion in the ExtraData field.
|
||||||
|
CustomRecords CustomRecords
|
||||||
|
|
||||||
// ExtraData is the set of data that was appended to this message to
|
// ExtraData is the set of data that was appended to this message to
|
||||||
// fill out the full maximum transport message size. These fields can
|
// fill out the full maximum transport message size. These fields can
|
||||||
// be used to specify optional data such as custom TLV fields.
|
// be used to specify optional data such as custom TLV fields.
|
||||||
@ -49,12 +56,75 @@ var _ Message = (*UpdateFulfillHTLC)(nil)
|
|||||||
//
|
//
|
||||||
// This is part of the lnwire.Message interface.
|
// This is part of the lnwire.Message interface.
|
||||||
func (c *UpdateFulfillHTLC) Decode(r io.Reader, pver uint32) error {
|
func (c *UpdateFulfillHTLC) Decode(r io.Reader, pver uint32) error {
|
||||||
return ReadElements(r,
|
// msgExtraData is a temporary variable used to read the message extra
|
||||||
|
// data field from the reader.
|
||||||
|
var msgExtraData ExtraOpaqueData
|
||||||
|
|
||||||
|
if err := ReadElements(r,
|
||||||
&c.ChanID,
|
&c.ChanID,
|
||||||
&c.ID,
|
&c.ID,
|
||||||
c.PaymentPreimage[:],
|
c.PaymentPreimage[:],
|
||||||
&c.ExtraData,
|
&msgExtraData,
|
||||||
|
); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract TLV records from the message extra data field.
|
||||||
|
extraDataTlvMap, err := msgExtraData.ExtractRecords()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Any records from the extra data TLV map which are in the custom
|
||||||
|
// records TLV type range will be included in the custom records field
|
||||||
|
// and removed from the extra data field.
|
||||||
|
customRecordsTlvMap := make(tlv.TypeMap, len(extraDataTlvMap))
|
||||||
|
for k, v := range extraDataTlvMap {
|
||||||
|
// Skip records that are not in the custom records TLV type
|
||||||
|
// range.
|
||||||
|
if k < MinCustomRecordsTlvType {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Include the record in the custom records map.
|
||||||
|
customRecordsTlvMap[k] = v
|
||||||
|
|
||||||
|
// Now that the record is included in the custom records map,
|
||||||
|
// we can remove it from the extra data TLV map.
|
||||||
|
delete(extraDataTlvMap, k)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the custom records field to the TLV record map.
|
||||||
|
customRecords, err := NewCustomRecordsFromTlvTypeMap(
|
||||||
|
customRecordsTlvMap,
|
||||||
)
|
)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
c.CustomRecords = customRecords
|
||||||
|
|
||||||
|
// Set custom records to nil if we didn't parse anything out of it so
|
||||||
|
// that we can use assert.Equal in tests.
|
||||||
|
if len(customRecordsTlvMap) == 0 {
|
||||||
|
c.CustomRecords = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set extra data to nil if we didn't parse anything out of it so that
|
||||||
|
// we can use assert.Equal in tests.
|
||||||
|
if len(extraDataTlvMap) == 0 {
|
||||||
|
c.ExtraData = nil
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Encode the remaining records back into the extra data field. These
|
||||||
|
// records are not in the custom records TLV type range and do not
|
||||||
|
// have associated fields in the UpdateFulfillHTLC struct.
|
||||||
|
c.ExtraData, err = NewExtraOpaqueDataFromTlvTypeMap(extraDataTlvMap)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Encode serializes the target UpdateFulfillHTLC into the passed io.Writer
|
// Encode serializes the target UpdateFulfillHTLC into the passed io.Writer
|
||||||
@ -74,7 +144,34 @@ func (c *UpdateFulfillHTLC) Encode(w *bytes.Buffer, pver uint32) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return WriteBytes(w, c.ExtraData)
|
// Construct a slice of all the records that we should include in the
|
||||||
|
// message extra data field. We will start by including any records from
|
||||||
|
// the extra data field.
|
||||||
|
msgExtraDataRecords, err := c.ExtraData.RecordProducers()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Include custom records in the extra data wire field if they are
|
||||||
|
// present. Ensure that the custom records are validated before encoding
|
||||||
|
// them.
|
||||||
|
if err := c.CustomRecords.Validate(); err != nil {
|
||||||
|
return fmt.Errorf("custom records validation error: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extend the message extra data records slice with TLV records from the
|
||||||
|
// custom records field.
|
||||||
|
customTlvRecords := c.CustomRecords.RecordProducers()
|
||||||
|
msgExtraDataRecords = append(msgExtraDataRecords, customTlvRecords...)
|
||||||
|
|
||||||
|
// We will now construct the message extra data field that will be
|
||||||
|
// encoded into the byte writer.
|
||||||
|
var msgExtraData ExtraOpaqueData
|
||||||
|
if err := msgExtraData.PackRecords(msgExtraDataRecords...); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return WriteBytes(w, msgExtraData)
|
||||||
}
|
}
|
||||||
|
|
||||||
// MsgType returns the integer uniquely identifying this message type on the
|
// MsgType returns the integer uniquely identifying this message type on the
|
||||||
|
@ -2033,8 +2033,9 @@ func messageSummary(msg lnwire.Message) string {
|
|||||||
msg.ID, msg.Reason)
|
msg.ID, msg.Reason)
|
||||||
|
|
||||||
case *lnwire.UpdateFulfillHTLC:
|
case *lnwire.UpdateFulfillHTLC:
|
||||||
return fmt.Sprintf("chan_id=%v, id=%v, pre_image=%x",
|
return fmt.Sprintf("chan_id=%v, id=%v, pre_image=%x, "+
|
||||||
msg.ChanID, msg.ID, msg.PaymentPreimage[:])
|
"custom_records=%v", msg.ChanID, msg.ID,
|
||||||
|
msg.PaymentPreimage[:], msg.CustomRecords)
|
||||||
|
|
||||||
case *lnwire.CommitSig:
|
case *lnwire.CommitSig:
|
||||||
return fmt.Sprintf("chan_id=%v, num_htlcs=%v", msg.ChanID,
|
return fmt.Sprintf("chan_id=%v, num_htlcs=%v", msg.ChanID,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user