commit d928a05a6e8b89d13eddbfa7a217fe5a61b1f069
parent 1d7de451be25581b74b4018415f75cb35338a469
Author: Jared Tobin <jared@jtobin.io>
Date: Sun, 25 Jan 2026 15:58:53 +0400
Merge impl/len-prefixed: extract encodeLenPrefixed helper
Diffstat:
1 file changed, 10 insertions(+), 12 deletions(-)
diff --git a/lib/Lightning/Protocol/BOLT7/Codec.hs b/lib/Lightning/Protocol/BOLT7/Codec.hs
@@ -135,6 +135,11 @@ decodeLenPrefixed bs = do
else Right (BS.splitAt n rest)
{-# INLINE decodeLenPrefixed #-}
+-- | Encode with u16 length prefix.
+encodeLenPrefixed :: ByteString -> ByteString
+encodeLenPrefixed bs = Prim.encodeU16 (fromIntegral $ BS.length bs) <> bs
+{-# INLINE encodeLenPrefixed #-}
+
-- | Decode fixed-length validated type.
decodeFixed :: Int -> DecodeError -> (ByteString -> Maybe a)
-> ByteString -> Either DecodeError (a, ByteString)
@@ -249,8 +254,7 @@ encodeChannelAnnouncement msg = mconcat
, getSignature (channelAnnNodeSig2 msg)
, getSignature (channelAnnBitcoinSig1 msg)
, getSignature (channelAnnBitcoinSig2 msg)
- , Prim.encodeU16 (fromIntegral $ BS.length features)
- , features
+ , encodeLenPrefixed (getFeatureBits (channelAnnFeatures msg))
, getChainHash (channelAnnChainHash msg)
, getShortChannelId (channelAnnShortChanId msg)
, getNodeId (channelAnnNodeId1 msg)
@@ -258,8 +262,6 @@ encodeChannelAnnouncement msg = mconcat
, getPoint (channelAnnBitcoinKey1 msg)
, getPoint (channelAnnBitcoinKey2 msg)
]
- where
- features = getFeatureBits (channelAnnFeatures msg)
-- | Decode channel_announcement message.
decodeChannelAnnouncement :: ByteString
@@ -302,14 +304,12 @@ encodeNodeAnnouncement msg = do
then Left EncodeLengthOverflow
else Right $ mconcat
[ getSignature (nodeAnnSignature msg)
- , Prim.encodeU16 (fromIntegral $ BS.length features)
- , features
+ , encodeLenPrefixed features
, Prim.encodeU32 (nodeAnnTimestamp msg)
, getNodeId (nodeAnnNodeId msg)
, getRgbColor (nodeAnnRgbColor msg)
, getAlias (nodeAnnAlias msg)
- , Prim.encodeU16 (fromIntegral $ BS.length addrData)
- , addrData
+ , encodeLenPrefixed addrData
]
-- | Encode address list.
@@ -455,8 +455,7 @@ encodeQueryShortChannelIds msg = do
then Left EncodeLengthOverflow
else Right $ mconcat
[ getChainHash (queryScidsChainHash msg)
- , Prim.encodeU16 (fromIntegral $ BS.length scidData)
- , scidData
+ , encodeLenPrefixed scidData
, TLV.encodeTlvStream (queryScidsTlvs msg)
]
@@ -535,8 +534,7 @@ encodeReplyChannelRange msg = do
, Prim.encodeU32 (replyRangeFirstBlock msg)
, Prim.encodeU32 (replyRangeNumBlocks msg)
, BS.singleton (replyRangeSyncComplete msg)
- , Prim.encodeU16 (fromIntegral $ BS.length rangeData)
- , rangeData
+ , encodeLenPrefixed rangeData
, TLV.encodeTlvStream (replyRangeTlvs msg)
]