bolt1

Base Lightning protocol, per BOLT #1 (docs.ppad.tech/bolt1).
git clone git://git.ppad.tech/bolt1.git
Log | Files | Refs | README | LICENSE

commit c0537890280dc5d5d35fb26dd3cbd1526128e25f
parent 03232f36d797c377d8cd3ebbf9370c0a7e7ae0e8
Author: Jared Tobin <jared@jtobin.io>
Date:   Sat, 10 Oct 2026 13:22:20 +0400

bench: rewrite for the new API

Share fixtures between the criterion and weigh suites (the old
Fixtures module was compiled but never imported), and force results
fully.

Diffstat:
Abench/Fixtures.hs | 50++++++++++++++++++++++++++++++++++++++++++++++++++
Abench/Main.hs | 33+++++++++++++++++++++++++++++++++
Abench/Weight.hs | 28++++++++++++++++++++++++++++
3 files changed, 111 insertions(+), 0 deletions(-)

diff --git a/bench/Fixtures.hs b/bench/Fixtures.hs @@ -0,0 +1,50 @@ +{-# LANGUAGE OverloadedStrings #-} + +module Fixtures where + +import qualified Data.ByteString as BS +import Data.Maybe (fromMaybe) +import Lightning.Protocol.BOLT1 +import qualified Lightning.Protocol.BOLT9 as BOLT9 + +{-# NOINLINE tlvs #-} +tlvs :: TlvStream +tlvs = fromMaybe empty_tlv_stream $ + tlv_stream [ TlvRecord t (BS.replicate 32 0x2a) | t <- [1, 3 .. 41] ] + +{-# NOINLINE tlvs_bytes #-} +tlvs_bytes :: BS.ByteString +tlvs_bytes = encode_tlv_stream tlvs + +{-# NOINLINE init_msg #-} +init_msg :: Message +init_msg = MsgInit Init { + init_global_features = BOLT9.parse "" + , init_features = BOLT9.parse "\x02\xaa\x52\x69\xa1" + , init_networks = fmap pure (chain_hash (BS.replicate 32 0x6f)) + , init_remote_addr = Just "\x01\x7f\x00\x00\x01\x26\x07" + , init_tlvs = empty_tlv_stream + } + +{-# NOINLINE ping_msg #-} +ping_msg :: Message +ping_msg = MsgPing (Ping 128 (BS.replicate 64 0) empty_tlv_stream) + +{-# NOINLINE error_msg #-} +error_msg :: Message +error_msg = MsgError (Error all_channels (BS.replicate 128 0x61) tlvs) + +wire :: Message -> BS.ByteString +wire m = either (const BS.empty) id (encode_message m) + +{-# NOINLINE init_bytes #-} +init_bytes :: BS.ByteString +init_bytes = wire init_msg + +{-# NOINLINE ping_bytes #-} +ping_bytes :: BS.ByteString +ping_bytes = wire ping_msg + +{-# NOINLINE error_bytes #-} +error_bytes :: BS.ByteString +error_bytes = wire error_msg diff --git a/bench/Main.hs b/bench/Main.hs @@ -0,0 +1,33 @@ +{-# LANGUAGE OverloadedStrings #-} + +module Main where + +import Criterion.Main +import Fixtures +import Lightning.Protocol.BOLT1 + +main :: IO () +main = defaultMain [ + bgroup "primitives" [ + bench "encode_u64" $ nf encode_u64 0x0102030405060708 + , bench "decode_u64" $ nf decode_u64 "\x01\x02\x03\x04\x05\x06\x07\x08" + , bench "encode_tu64" $ nf encode_tu64 0x010203 + , bench "decode_tu64" $ nf decode_tu64 "\x01\x02\x03" + , bench "encode_bigsize (9 bytes)" $ nf encode_bigsize 0x100000000 + , bench "decode_bigsize (9 bytes)" $ + nf decode_bigsize "\xff\x00\x00\x00\x01\x00\x00\x00\x00" + ] + , bgroup "tlv" [ + bench "encode_tlv_stream (21 records)" $ nf encode_tlv_stream tlvs + , bench "decode_tlv_stream (21 records)" $ + nf (decode_tlv_stream (const False)) tlvs_bytes + ] + , bgroup "messages" [ + bench "encode init" $ nf encode_message init_msg + , bench "decode init" $ nf decode_message init_bytes + , bench "encode ping" $ nf encode_message ping_msg + , bench "decode ping" $ nf decode_message ping_bytes + , bench "encode error (with tlvs)" $ nf encode_message error_msg + , bench "decode error (with tlvs)" $ nf decode_message error_bytes + ] + ] diff --git a/bench/Weight.hs b/bench/Weight.hs @@ -0,0 +1,28 @@ +{-# LANGUAGE OverloadedStrings #-} + +module Main where + +import Fixtures +import Lightning.Protocol.BOLT1 +import Weigh + +main :: IO () +main = mainWith $ do + wgroup "primitives" $ do + func "baseline (weigh overhead)" (+ (1 :: Int)) 1 + func "encode_u16" encode_u16 0x0102 + func "encode_u64" encode_u64 0x0102030405060708 + func "decode_u64" decode_u64 "\x01\x02\x03\x04\x05\x06\x07\x08" + func "encode_tu64" encode_tu64 0x010203 + func "encode_bigsize (9 bytes)" encode_bigsize 0x100000000 + wgroup "tlv" $ do + func "encode_tlv_stream (21 records)" encode_tlv_stream tlvs + func "decode_tlv_stream (21 records)" + (decode_tlv_stream (const False)) tlvs_bytes + wgroup "messages" $ do + func "encode init" encode_message init_msg + func "decode init" decode_message init_bytes + func "encode ping" encode_message ping_msg + func "decode ping" decode_message ping_bytes + func "encode error (with tlvs)" encode_message error_msg + func "decode error (with tlvs)" decode_message error_bytes