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 (4148B)


      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 CltvExpiry where
     47   rnf (CltvExpiry x) = rnf x
     48 
     49 instance NFData SighashType
     50 
     51 instance NFData B5.OutputResolution where
     52   rnf B5.Resolved = ()
     53   rnf (B5.Revoke rk) = rnf rk
     54   rnf _ = ()
     55 
     56 instance NFData B5.HTLCOutputType where
     57   rnf (B5.HTLCOfferedOutput e) = rnf e
     58   rnf (B5.HTLCReceivedOutput e) = rnf e
     59 
     60 instance NFData B5.UnresolvedOutput where
     61   rnf (B5.UnresolvedOutput op v t) =
     62     rnf op `seq` rnf v `seq` rnf t
     63 
     64 instance NFData B5.RevokedOutputType where
     65   rnf B5.RevokedToLocal = ()
     66   rnf (B5.RevokedHTLC h) = rnf h
     67 
     68 instance NFData B5.RevokedOutput where
     69   rnf (B5.RevokedOutput op v t) =
     70     rnf op `seq` rnf v `seq` rnf t
     71 
     72 instance NFData B5.SpendingTx where
     73   rnf (B5.SpendingTx tx scr val sh) =
     74     rnf tx `seq` rnf scr `seq` rnf val `seq` rnf sh
     75 
     76 instance NFData B5.PenaltyContext where
     77   rnf (B5.PenaltyContext os rk d f) =
     78     rnf os `seq` rnf rk `seq` rnf d `seq` rnf f
     79 
     80 -- note that 'weigh' doesn't work properly in a repl
     81 main :: IO ()
     82 main = mainWith $ do
     83   spend_weights
     84   batch_weights
     85 
     86 -- fixtures -----------------------------------------------------------
     87 
     88 dummyPubkey :: Pubkey
     89 dummyPubkey = case pubkey (BS.pack (0x02 : replicate 32 0x01)) of
     90   Just pk -> pk
     91   Nothing -> error "impossible"
     92 
     93 dummyTxId :: TxId
     94 dummyTxId = case mkTxId (BS.replicate 32 0x00) of
     95   Just tid -> tid
     96   Nothing  -> error "impossible"
     97 
     98 dummyOutPoint :: OutPoint
     99 dummyOutPoint = OutPoint dummyTxId 0
    100 
    101 dummyRevPk :: RevocationPubkey
    102 dummyRevPk = RevocationPubkey dummyPubkey
    103 
    104 dummyDelayedPk :: LocalDelayedPubkey
    105 dummyDelayedPk = LocalDelayedPubkey dummyPubkey
    106 
    107 dummyFundPk :: FundingPubkey
    108 dummyFundPk = FundingPubkey dummyPubkey
    109 
    110 dummyDest :: Script
    111 dummyDest = Script $ BS.pack [0x00, 0x14] <>
    112   BS.replicate 20 0xCC
    113 
    114 dummyFeerate :: FeeratePerKw
    115 dummyFeerate = FeeratePerKw 253
    116 
    117 dummyDelay :: ToSelfDelay
    118 dummyDelay = ToSelfDelay 144
    119 
    120 mkRevokedOutputs :: Int -> NE.NonEmpty B5.RevokedOutput
    121 mkRevokedOutputs n =
    122   let ro i = B5.RevokedOutput
    123         (OutPoint dummyTxId (fromIntegral i))
    124         (Satoshi 10000)
    125         B5.RevokedToLocal
    126   in  ro 0 NE.:| [ ro i | i <- [1..n-1] ]
    127 
    128 -- weights ------------------------------------------------------------
    129 
    130 spend_weights :: Weigh ()
    131 spend_weights = wgroup "spend" $ do
    132   func "spend_to_local"
    133     (\v -> B5.spend_to_local
    134       dummyOutPoint v dummyRevPk dummyDelay
    135       dummyDelayedPk dummyDest dummyFeerate)
    136     (Satoshi 100000)
    137 
    138   func "spend_anchor_owner"
    139     (\v -> B5.spend_anchor_owner
    140       dummyOutPoint v dummyFundPk dummyDest)
    141     (Satoshi 330)
    142 
    143 batch_weights :: Weigh ()
    144 batch_weights = wgroup "batch" $ do
    145   func "spend_revoked_batch/10"
    146     B5.spend_revoked_batch
    147     (B5.PenaltyContext
    148       (mkRevokedOutputs 10)
    149       dummyRevPk dummyDest dummyFeerate)
    150 
    151   func "spend_revoked_batch/100"
    152     B5.spend_revoked_batch
    153     (B5.PenaltyContext
    154       (mkRevokedOutputs 100)
    155       dummyRevPk dummyDest dummyFeerate)
    156 
    157   func "spend_revoked_batch/483"
    158     B5.spend_revoked_batch
    159     (B5.PenaltyContext
    160       (mkRevokedOutputs 483)
    161       dummyRevPk dummyDest dummyFeerate)