bolt2

Lightning peer protocol, per BOLT #2.
git clone git://git.ppad.tech/bolt2.git
Log | Files | Refs | README | LICENSE

commit 9b9f02e85c0aa7b968d373a752540192ed9bd29d
parent 5f199af68ded0f0dc3704dc5f74a367b80a77060
Author: Jared Tobin <jared@jtobin.io>
Date:   Sun, 25 Jan 2026 15:00:50 +0400

Add larger benchmark cases for scaling analysis

Add CommitmentSigned benchmarks with 100 and 483 (max) HTLC
signatures to measure list accumulation pattern performance at scale.

Fix Weight.hs to handle Either EncodeError return types from
encodeTxSignatures and encodeCommitmentSigned.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

Diffstat:
Mbench/Main.hs | 48+++++++++++++++++++++++++++++++++++++++++++++++-
Mbench/Weight.hs | 62++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
2 files changed, 105 insertions(+), 5 deletions(-)

diff --git a/bench/Main.hs b/bench/Main.hs @@ -200,7 +200,7 @@ encodedUpdateAddHtlc :: BS.ByteString encodedUpdateAddHtlc = encodeUpdateAddHtlc testUpdateAddHtlc {-# NOINLINE encodedUpdateAddHtlc #-} --- | Test CommitmentSigned message with HTLC signatures. +-- | Test CommitmentSigned message with HTLC signatures (10 sigs). testCommitmentSigned :: CommitmentSigned testCommitmentSigned = CommitmentSigned { commitmentSignedChannelId = testChannelId @@ -216,6 +216,40 @@ encodedCommitmentSigned = case encodeCommitmentSigned testCommitmentSigned of Left e -> error $ "encodedCommitmentSigned: " ++ show e {-# NOINLINE encodedCommitmentSigned #-} +-- | Test CommitmentSigned with many HTLC signatures (100 sigs). +testCommitmentSignedLarge :: CommitmentSigned +testCommitmentSignedLarge = CommitmentSigned + { commitmentSignedChannelId = testChannelId + , commitmentSignedSignature = testSignature + , commitmentSignedHtlcSignatures = replicate 100 testSignature + } +{-# NOINLINE testCommitmentSignedLarge #-} + +-- | Encoded large CommitmentSigned for decode benchmarks. +encodedCommitmentSignedLarge :: BS.ByteString +encodedCommitmentSignedLarge = + case encodeCommitmentSigned testCommitmentSignedLarge of + Right bs -> bs + Left e -> error $ "encodedCommitmentSignedLarge: " ++ show e +{-# NOINLINE encodedCommitmentSignedLarge #-} + +-- | Test CommitmentSigned with max HTLC signatures (483 sigs). +testCommitmentSignedMax :: CommitmentSigned +testCommitmentSignedMax = CommitmentSigned + { commitmentSignedChannelId = testChannelId + , commitmentSignedSignature = testSignature + , commitmentSignedHtlcSignatures = replicate 483 testSignature + } +{-# NOINLINE testCommitmentSignedMax #-} + +-- | Encoded max CommitmentSigned for decode benchmarks. +encodedCommitmentSignedMax :: BS.ByteString +encodedCommitmentSignedMax = + case encodeCommitmentSigned testCommitmentSignedMax of + Right bs -> bs + Left e -> error $ "encodedCommitmentSignedMax: " ++ show e +{-# NOINLINE encodedCommitmentSignedMax #-} + -- Benchmark groups ------------------------------------------------------------ main :: IO () @@ -251,5 +285,17 @@ main = defaultMain [ bench "encode" $ nf encodeCommitmentSigned testCommitmentSigned , bench "decode" $ nf decodeCommitmentSigned encodedCommitmentSigned ] + , bgroup "commitment_signed_100" + [ bench "encode" $ + nf encodeCommitmentSigned testCommitmentSignedLarge + , bench "decode" $ + nf decodeCommitmentSigned encodedCommitmentSignedLarge + ] + , bgroup "commitment_signed_483" + [ bench "encode" $ + nf encodeCommitmentSigned testCommitmentSignedMax + , bench "decode" $ + nf decodeCommitmentSigned encodedCommitmentSignedMax + ] ] ] diff --git a/bench/Weight.hs b/bench/Weight.hs @@ -16,6 +16,12 @@ import Lightning.Protocol.BOLT1 (TlvStream(..)) import Lightning.Protocol.BOLT2 import Weigh +-- | Wrapper for encoding functions that return Either. +forceEncode :: Either EncodeError BS.ByteString -> BS.ByteString +forceEncode (Right bs) = bs +forceEncode (Left e) = error $ "forceEncode: " ++ show e +{-# INLINE forceEncode #-} + -- Test data construction ------------------------------------------------------ -- | 32 zero bytes for channel IDs, chain hashes, etc. @@ -207,7 +213,9 @@ testTxSignatures = mkTxSignatures testChannelId testTxId testWitnesses -- | Encoded TxSignatures for decode benchmarks. encodedTxSignatures :: BS.ByteString -encodedTxSignatures = encodeTxSignatures testTxSignatures +encodedTxSignatures = case encodeTxSignatures testTxSignatures of + Right bs -> bs + Left e -> error $ "encodedTxSignatures: " ++ show e {-# NOINLINE encodedTxSignatures #-} -- | Test ClosingSigned message. @@ -244,9 +252,49 @@ testCommitmentSigned = -- | Encoded CommitmentSigned for decode benchmarks. encodedCommitmentSigned :: BS.ByteString -encodedCommitmentSigned = encodeCommitmentSigned testCommitmentSigned +encodedCommitmentSigned = case encodeCommitmentSigned testCommitmentSigned of + Right bs -> bs + Left e -> error $ "encodedCommitmentSigned: " ++ show e {-# NOINLINE encodedCommitmentSigned #-} +-- | Large HTLC signatures for CommitmentSigned (100). +testHtlcSigsLarge :: [Signature] +testHtlcSigsLarge = replicate 100 testSignature +{-# NOINLINE testHtlcSigsLarge #-} + +-- | Test CommitmentSigned message (100 sigs). +testCommitmentSignedLarge :: CommitmentSigned +testCommitmentSignedLarge = + mkCommitmentSigned testChannelId testSignature testHtlcSigsLarge +{-# NOINLINE testCommitmentSignedLarge #-} + +-- | Encoded large CommitmentSigned for decode benchmarks. +encodedCommitmentSignedLarge :: BS.ByteString +encodedCommitmentSignedLarge = + case encodeCommitmentSigned testCommitmentSignedLarge of + Right bs -> bs + Left e -> error $ "encodedCommitmentSignedLarge: " ++ show e +{-# NOINLINE encodedCommitmentSignedLarge #-} + +-- | Max HTLC signatures for CommitmentSigned (483). +testHtlcSigsMax :: [Signature] +testHtlcSigsMax = replicate 483 testSignature +{-# NOINLINE testHtlcSigsMax #-} + +-- | Test CommitmentSigned message (483 sigs). +testCommitmentSignedMax :: CommitmentSigned +testCommitmentSignedMax = + mkCommitmentSigned testChannelId testSignature testHtlcSigsMax +{-# NOINLINE testCommitmentSignedMax #-} + +-- | Encoded max CommitmentSigned for decode benchmarks. +encodedCommitmentSignedMax :: BS.ByteString +encodedCommitmentSignedMax = + case encodeCommitmentSigned testCommitmentSignedMax of + Right bs -> bs + Left e -> error $ "encodedCommitmentSignedMax: " ++ show e +{-# NOINLINE encodedCommitmentSignedMax #-} + -- Weigh benchmarks ------------------------------------------------------------ main :: IO () @@ -267,7 +315,7 @@ main = mainWith $ do wgroup "v2/tx_signatures" $ do func "construct" (mkTxSignatures testChannelId testTxId) testWitnesses - func "encode" encodeTxSignatures testTxSignatures + func "encode" (forceEncode . encodeTxSignatures) testTxSignatures func "decode" decodeTxSignatures encodedTxSignatures -- Close messages @@ -286,5 +334,11 @@ main = mainWith $ do wgroup "normal/commitment_signed" $ do func "construct" (mkCommitmentSigned testChannelId testSignature) testHtlcSigs - func "encode" encodeCommitmentSigned testCommitmentSigned + func "encode" (forceEncode . encodeCommitmentSigned) testCommitmentSigned func "decode" decodeCommitmentSigned encodedCommitmentSigned + + wgroup "normal/commitment_signed_100" $ do + func "decode" decodeCommitmentSigned encodedCommitmentSignedLarge + + wgroup "normal/commitment_signed_483" $ do + func "decode" decodeCommitmentSigned encodedCommitmentSignedMax