commit 5c5272f7ba961a46826ff390a31b167f2b063b2b
parent 20dea434be34eee354da2b7acd7365ba9de68b30
Author: Jared Tobin <jared@jtobin.io>
Date: Sun, 19 Apr 2026 12:46:07 +0800
test/bench: update for shared bolt1 types
Use scidFromBytes for ByteString-based ShortChannelId construction
(shortChannelId now takes Word32/Word32/Word16 from bolt1). Add
mkScid helper in tests. Fix bench ChannelUpdate construction to
use proper type constructors instead of numeric literals.
Diffstat:
3 files changed, 42 insertions(+), 36 deletions(-)
diff --git a/bench/Main.hs b/bench/Main.hs
@@ -51,7 +51,7 @@ testChainHash = case chainHash zeroBytes32 of
-- | 8-byte short channel ID.
testShortChannelId :: ShortChannelId
-testShortChannelId = case shortChannelId zeroBytes8 of
+testShortChannelId = case scidFromBytes zeroBytes8 of
Just s -> s
Nothing -> error "testShortChannelId: invalid"
{-# NOINLINE testShortChannelId #-}
@@ -112,7 +112,7 @@ emptyFeatures = featureBits BS.empty
testScidList :: [ShortChannelId]
testScidList = map mkScid [1..100]
where
- mkScid n = case shortChannelId (BS.pack [0, 0, 0, n, 0, 0, 0, n]) of
+ mkScid n = case scidFromBytes (BS.pack [0, 0, 0, n, 0, 0, 0, n]) of
Just s -> s
Nothing -> error "mkScid: invalid"
{-# NOINLINE testScidList #-}
@@ -173,13 +173,13 @@ testChannelUpdate = ChannelUpdate
, chanUpdateChainHash = testChainHash
, chanUpdateShortChanId = testShortChannelId
, chanUpdateTimestamp = 1234567890
- , chanUpdateMsgFlags = 0x01
- , chanUpdateChanFlags = 0x00
- , chanUpdateCltvExpDelta = 144
- , chanUpdateHtlcMinMsat = 1000
- , chanUpdateFeeBaseMsat = 1000
- , chanUpdateFeeProportional = 100
- , chanUpdateHtlcMaxMsat = Just 1000000000
+ , chanUpdateMsgFlags = MessageFlags True
+ , chanUpdateChanFlags = ChannelFlags False False
+ , chanUpdateCltvExpDelta = CltvExpiryDelta 144
+ , chanUpdateHtlcMinMsat = HtlcMinimumMsat 1000
+ , chanUpdateFeeBaseMsat = FeeBaseMsat 1000
+ , chanUpdateFeeProportional = FeeProportionalMillionths 100
+ , chanUpdateHtlcMaxMsat = Just (HtlcMaximumMsat 1000000000)
}
{-# NOINLINE testChannelUpdate #-}
diff --git a/bench/Weight.hs b/bench/Weight.hs
@@ -51,7 +51,7 @@ testChainHash = case chainHash zeroBytes32 of
-- | 8-byte short channel ID.
testShortChannelId :: ShortChannelId
-testShortChannelId = case shortChannelId zeroBytes8 of
+testShortChannelId = case scidFromBytes zeroBytes8 of
Just s -> s
Nothing -> error "testShortChannelId: invalid"
{-# NOINLINE testShortChannelId #-}
@@ -112,7 +112,7 @@ emptyTlvs = unsafeTlvStream []
testScidList :: [ShortChannelId]
testScidList = map mkScid [1..100]
where
- mkScid n = case shortChannelId (BS.pack [0, 0, 0, n, 0, 0, 0, n]) of
+ mkScid n = case scidFromBytes (BS.pack [0, 0, 0, n, 0, 0, 0, n]) of
Just s -> s
Nothing -> error "mkScid: invalid"
{-# NOINLINE testScidList #-}
@@ -165,13 +165,13 @@ mkChannelUpdate !sig !ch !scid = ChannelUpdate
, chanUpdateChainHash = ch
, chanUpdateShortChanId = scid
, chanUpdateTimestamp = 1234567890
- , chanUpdateMsgFlags = 0x01
- , chanUpdateChanFlags = 0x00
- , chanUpdateCltvExpDelta = 144
- , chanUpdateHtlcMinMsat = 1000
- , chanUpdateFeeBaseMsat = 1000
- , chanUpdateFeeProportional = 100
- , chanUpdateHtlcMaxMsat = Just 1000000000
+ , chanUpdateMsgFlags = MessageFlags True
+ , chanUpdateChanFlags = ChannelFlags False False
+ , chanUpdateCltvExpDelta = CltvExpiryDelta 144
+ , chanUpdateHtlcMinMsat = HtlcMinimumMsat 1000
+ , chanUpdateFeeBaseMsat = FeeBaseMsat 1000
+ , chanUpdateFeeProportional = FeeProportionalMillionths 100
+ , chanUpdateHtlcMaxMsat = Just (HtlcMaximumMsat 1000000000)
}
-- | Construct AnnouncementSignatures message.
diff --git a/test/Main.hs b/test/Main.hs
@@ -35,7 +35,7 @@ testChainHash = fromJust $ chainHash (BS.replicate 32 0x01)
-- | Create a valid ShortChannelId (8 bytes).
testShortChannelId :: ShortChannelId
-testShortChannelId = fromJust $ shortChannelId (BS.replicate 8 0xab)
+testShortChannelId = fromJust $ scidFromBytes (BS.replicate 8 0xab)
-- | Create a valid ChannelId (32 bytes).
testChannelId :: ChannelId
@@ -73,6 +73,12 @@ emptyTlvs = unsafeTlvStream []
emptyFeatures :: FeatureBits
emptyFeatures = featureBits BS.empty
+-- | Total ShortChannelId constructor for test fixtures.
+mkScid :: Word32 -> Word32 -> Word16 -> ShortChannelId
+mkScid b t o = case shortChannelId b t o of
+ Just s -> s
+ Nothing -> error "mkScid: invalid test fixture"
+
-- Type Tests ------------------------------------------------------------------
type_tests :: TestTree
@@ -80,36 +86,36 @@ type_tests = testGroup "Types" [
testGroup "ShortChannelId" [
testCase "scidBlockHeight" $ do
-- 8 bytes: block=0x123456, tx=0x789abc, output=0xdef0
- let scid = fromJust $ shortChannelId (BS.pack
+ let scid = fromJust $ scidFromBytes (BS.pack
[0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0])
scidBlockHeight scid @?= 0x123456
, testCase "scidTxIndex" $ do
- let scid = fromJust $ shortChannelId (BS.pack
+ let scid = fromJust $ scidFromBytes (BS.pack
[0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0])
scidTxIndex scid @?= 0x789abc
, testCase "scidOutputIndex" $ do
- let scid = fromJust $ shortChannelId (BS.pack
+ let scid = fromJust $ scidFromBytes (BS.pack
[0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0])
scidOutputIndex scid @?= 0xdef0
- , testCase "mkShortChannelId roundtrip" $ do
- let scid = mkShortChannelId 539268 845 1
+ , testCase "shortChannelId roundtrip" $ do
+ let scid = fromJust $ shortChannelId 539268 845 1
scidBlockHeight scid @?= 539268
scidTxIndex scid @?= 845
scidOutputIndex scid @?= 1
, testCase "formatScid" $ do
- let scid = mkShortChannelId 539268 845 1
+ let scid = fromJust $ shortChannelId 539268 845 1
formatScid scid @?= "539268x845x1"
, testCase "formatScid zero values" $ do
- let scid = mkShortChannelId 0 0 0
+ let scid = fromJust $ shortChannelId 0 0 0
formatScid scid @?= "0x0x0"
]
, testGroup "Smart constructors" [
testCase "chainHash rejects wrong length" $ do
chainHash (BS.replicate 31 0x00) @?= Nothing
chainHash (BS.replicate 33 0x00) @?= Nothing
- , testCase "shortChannelId rejects wrong length" $ do
- shortChannelId (BS.replicate 7 0x00) @?= Nothing
- shortChannelId (BS.replicate 9 0x00) @?= Nothing
+ , testCase "scidFromBytes rejects wrong length" $ do
+ scidFromBytes (BS.replicate 7 0x00) @?= Nothing
+ scidFromBytes (BS.replicate 9 0x00) @?= Nothing
, testCase "signature rejects wrong length" $ do
signature (BS.replicate 63 0x00) @?= Nothing
signature (BS.replicate 65 0x00) @?= Nothing
@@ -119,7 +125,7 @@ type_tests = testGroup "Types" [
]
, testGroup "Constants" [
testCase "mainnetChainHash has correct length" $ do
- BS.length (getChainHash mainnetChainHash) @?= 32
+ BS.length (unChainHash mainnetChainHash) @?= 32
]
, testGroup "NodeId ordering" [
testCase "NodeId Ord is lexicographic" $ do
@@ -343,22 +349,22 @@ scid_list_tests = testGroup "SCID List Encoding" [
Right decoded -> decoded @?= []
Left e -> assertFailure $ "decode failed: " ++ show e
, testCase "encode/decode roundtrip single SCID" $ do
- let scids = [mkShortChannelId 539268 845 1]
+ let scids = [mkScid 539268 845 1]
encoded = encodeShortChannelIdList scids
case decodeShortChannelIdList encoded of
Right decoded -> decoded @?= scids
Left e -> assertFailure $ "decode failed: " ++ show e
, testCase "encode/decode roundtrip multiple SCIDs" $ do
- let scids = [ mkShortChannelId 100000 1 0
- , mkShortChannelId 200000 2 1
- , mkShortChannelId 300000 3 2
+ let scids = [ mkScid 100000 1 0
+ , mkScid 200000 2 1
+ , mkScid 300000 3 2
]
encoded = encodeShortChannelIdList scids
case decodeShortChannelIdList encoded of
Right decoded -> decoded @?= scids
Left e -> assertFailure $ "decode failed: " ++ show e
, testCase "encoding has correct format" $ do
- let scids = [mkShortChannelId 1 2 3]
+ let scids = [mkScid 1 2 3]
encoded = encodeShortChannelIdList scids
-- First byte should be 0 (encoding type)
BS.index encoded 0 @?= 0
@@ -366,7 +372,7 @@ scid_list_tests = testGroup "SCID List Encoding" [
BS.length encoded @?= 9
, testCase "decode rejects unknown encoding type" $ do
-- Encoding type 1 (zlib compressed) is not supported
- let badEncoded = BS.cons 1 (getShortChannelId testShortChannelId)
+ let badEncoded = BS.cons 1 (scidToBytes testShortChannelId)
case decodeShortChannelIdList badEncoded of
Left _ -> pure ()
Right _ -> assertFailure "should reject encoding type 1"