mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-09-27 21:36:17 +02:00
tlv+record+routing: add payload size calculation
This commit is contained in:
@@ -184,6 +184,53 @@ func (h *Hop) PackHopPayload(w io.Writer, nextChanID uint64) error {
|
||||
return tlvStream.Encode(w)
|
||||
}
|
||||
|
||||
// Size returns the total size this hop's payload would take up in the onion
|
||||
// packet.
|
||||
func (h *Hop) PayloadSize(nextChanID uint64) uint64 {
|
||||
if h.LegacyPayload {
|
||||
return sphinx.LegacyHopDataSize
|
||||
}
|
||||
|
||||
var payloadSize uint64
|
||||
|
||||
addRecord := func(tlvType tlv.Type, length uint64) {
|
||||
payloadSize += tlv.VarIntSize(uint64(tlvType)) +
|
||||
tlv.VarIntSize(length) + length
|
||||
}
|
||||
|
||||
// Add amount size.
|
||||
addRecord(record.AmtOnionType, tlv.SizeTUint64(uint64(h.AmtToForward)))
|
||||
|
||||
// Add lock time size.
|
||||
addRecord(
|
||||
record.LockTimeOnionType,
|
||||
tlv.SizeTUint64(uint64(h.OutgoingTimeLock)),
|
||||
)
|
||||
|
||||
// Add next hop if present.
|
||||
if nextChanID != 0 {
|
||||
addRecord(record.NextHopOnionType, 8)
|
||||
}
|
||||
|
||||
// Add mpp if present.
|
||||
if h.MPP != nil {
|
||||
addRecord(record.MPPOnionType, h.MPP.PayloadSize())
|
||||
}
|
||||
|
||||
// Add custom records.
|
||||
for k, v := range h.CustomRecords {
|
||||
addRecord(tlv.Type(k), uint64(len(v)))
|
||||
}
|
||||
|
||||
// Add the size required to encode the payload length.
|
||||
payloadSize += tlv.VarIntSize(payloadSize)
|
||||
|
||||
// Add HMAC.
|
||||
payloadSize += sphinx.HMACSize
|
||||
|
||||
return payloadSize
|
||||
}
|
||||
|
||||
// Route represents a path through the channel graph which runs over one or
|
||||
// more channels in succession. This struct carries all the information
|
||||
// required to craft the Sphinx onion packet, and send the payment along the
|
||||
|
Reference in New Issue
Block a user