bolt5

On-chain transaction handling for Lightning (docs.ppad.tech/bolt5).
git clone git://git.ppad.tech/bolt5.git
Log | Files | Refs | README | LICENSE

Weight.hs (3761B)


      1 {-# LANGUAGE BangPatterns #-}
      2 {-# LANGUAGE DeriveGeneric #-}
      3 {-# LANGUAGE OverloadedStrings #-}
      4 {-# OPTIONS_GHC -fno-warn-orphans #-}
      5 
      6 module Main where
      7 
      8 import Bitcoin.Prim.Tx.Sighash (SighashType(..))
      9 import Control.DeepSeq (NFData(..))
     10 import qualified Data.ByteString as BS
     11 import qualified Data.List.NonEmpty as NE
     12 import Lightning.Protocol.BOLT3 hiding
     13   (txout_value, txout_script)
     14 import qualified Lightning.Protocol.BOLT5 as B5
     15 import Weigh
     16 
     17 -- NFData orphan instances for weigh ---------------------------------
     18 
     19 instance NFData Satoshi where
     20   rnf (Satoshi x) = rnf x
     21 
     22 instance NFData Script where
     23   rnf (Script bs) = rnf bs
     24 
     25 instance NFData Pubkey where
     26   rnf (Pubkey x) = rnf x
     27 
     28 instance NFData Point where
     29   rnf (Point x) = rnf x
     30 
     31 instance NFData RevocationPubkey where
     32   rnf (RevocationPubkey x) = rnf x
     33 
     34 instance NFData LocalDelayedPubkey where
     35   rnf (LocalDelayedPubkey p) = rnf p
     36 
     37 instance NFData FundingPubkey where
     38   rnf (FundingPubkey p) = rnf p
     39 
     40 instance NFData FeeratePerKw where
     41   rnf (FeeratePerKw x) = rnf x
     42 
     43 instance NFData ToSelfDelay where
     44   rnf (ToSelfDelay x) = rnf x
     45 
     46 instance NFData SighashType
     47 
     48 instance NFData B5.OutputResolution where
     49   rnf B5.Resolved = ()
     50   rnf (B5.Revoke rk) = rnf rk
     51   rnf _ = ()
     52 
     53 instance NFData B5.UnresolvedOutput where
     54   rnf (B5.UnresolvedOutput op v t) =
     55     rnf op `seq` rnf v `seq` rnf t
     56 
     57 instance NFData B5.SpendingTx where
     58   rnf (B5.SpendingTx tx scr val sh) =
     59     rnf tx `seq` rnf scr `seq` rnf val `seq` rnf sh
     60 
     61 instance NFData B5.PenaltyContext where
     62   rnf (B5.PenaltyContext os rk d f) =
     63     rnf os `seq` rnf rk `seq` rnf d `seq` rnf f
     64 
     65 -- note that 'weigh' doesn't work properly in a repl
     66 main :: IO ()
     67 main = mainWith $ do
     68   spend_weights
     69   batch_weights
     70 
     71 -- fixtures -----------------------------------------------------------
     72 
     73 dummyPubkey :: Pubkey
     74 dummyPubkey = case pubkey (BS.pack (0x02 : replicate 32 0x01)) of
     75   Just pk -> pk
     76   Nothing -> error "impossible"
     77 
     78 dummyTxId :: TxId
     79 dummyTxId = case mkTxId (BS.replicate 32 0x00) of
     80   Just tid -> tid
     81   Nothing  -> error "impossible"
     82 
     83 dummyOutPoint :: OutPoint
     84 dummyOutPoint = OutPoint dummyTxId 0
     85 
     86 dummyRevPk :: RevocationPubkey
     87 dummyRevPk = RevocationPubkey dummyPubkey
     88 
     89 dummyDelayedPk :: LocalDelayedPubkey
     90 dummyDelayedPk = LocalDelayedPubkey dummyPubkey
     91 
     92 dummyFundPk :: FundingPubkey
     93 dummyFundPk = FundingPubkey dummyPubkey
     94 
     95 dummyDest :: Script
     96 dummyDest = Script $ BS.pack [0x00, 0x14] <>
     97   BS.replicate 20 0xCC
     98 
     99 dummyFeerate :: FeeratePerKw
    100 dummyFeerate = FeeratePerKw 253
    101 
    102 dummyDelay :: ToSelfDelay
    103 dummyDelay = ToSelfDelay 144
    104 
    105 mkRevokedOutputs :: Int -> NE.NonEmpty B5.UnresolvedOutput
    106 mkRevokedOutputs n =
    107   let uo i = B5.UnresolvedOutput
    108         (OutPoint dummyTxId (fromIntegral i))
    109         (Satoshi 10000)
    110         (B5.Revoke dummyRevPk)
    111   in  uo 0 NE.:| [ uo i | i <- [1..n-1] ]
    112 
    113 -- weights ------------------------------------------------------------
    114 
    115 spend_weights :: Weigh ()
    116 spend_weights = wgroup "spend" $ do
    117   func "spend_to_local"
    118     (\v -> B5.spend_to_local
    119       dummyOutPoint v dummyRevPk dummyDelay
    120       dummyDelayedPk dummyDest dummyFeerate)
    121     (Satoshi 100000)
    122 
    123   func "spend_anchor_owner"
    124     (\v -> B5.spend_anchor_owner
    125       dummyOutPoint v dummyFundPk dummyDest)
    126     (Satoshi 330)
    127 
    128 batch_weights :: Weigh ()
    129 batch_weights = wgroup "batch" $ do
    130   func "spend_revoked_batch/10"
    131     B5.spend_revoked_batch
    132     (B5.PenaltyContext
    133       (mkRevokedOutputs 10)
    134       dummyRevPk dummyDest dummyFeerate)
    135 
    136   func "spend_revoked_batch/100"
    137     B5.spend_revoked_batch
    138     (B5.PenaltyContext
    139       (mkRevokedOutputs 100)
    140       dummyRevPk dummyDest dummyFeerate)
    141 
    142   func "spend_revoked_batch/483"
    143     B5.spend_revoked_batch
    144     (B5.PenaltyContext
    145       (mkRevokedOutputs 483)
    146       dummyRevPk dummyDest dummyFeerate)