Message.hs (3688B)
1 {-# OPTIONS_HADDOCK hide #-} 2 {-# LANGUAGE DeriveGeneric #-} 3 4 -- | 5 -- Module: Lightning.Protocol.BOLT1.Message 6 -- Copyright: (c) 2025 Jared Tobin 7 -- License: MIT 8 -- Maintainer: Jared Tobin <jared@ppad.tech> 9 -- 10 -- The messages defined by BOLT #1. 11 12 module Lightning.Protocol.BOLT1.Message ( 13 Init(..) 14 , Error(..) 15 , Warning(..) 16 , Ping(..) 17 , Pong(..) 18 , PeerStorage(..) 19 , PeerStorageRetrieval(..) 20 , Message(..) 21 , message_type 22 ) where 23 24 import Control.DeepSeq (NFData) 25 import qualified Data.ByteString as BS 26 import Data.Word (Word16) 27 import GHC.Generics (Generic) 28 import Lightning.Protocol.BOLT1.Prim 29 import Lightning.Protocol.BOLT1.TLV 30 import qualified Lightning.Protocol.BOLT9 as BOLT9 31 32 -- | The @init@ message (type 16). 33 -- 34 -- The two feature vectors are kept as received. Per BOLT #1, a 35 -- receiver treats their union ('BOLT9.union') as the peer's 36 -- features. 37 data Init = Init 38 { init_global_features :: !BOLT9.FeatureVector 39 , init_features :: !BOLT9.FeatureVector 40 , init_networks :: !(Maybe [ChainHash]) 41 -- ^ @networks@ (TLV type 1): chains the node is interested in 42 , init_remote_addr :: !(Maybe BS.ByteString) 43 -- ^ @remote_addr@ (TLV type 3): a raw BOLT #7 address descriptor 44 , init_tlvs :: !TlvStream 45 -- ^ unknown odd TLV records 46 } deriving (Eq, Show, Generic) 47 48 instance NFData Init 49 50 -- | The @error@ message (type 17). 51 data Error = Error 52 { error_channel_id :: !ChannelId 53 , error_data :: !BS.ByteString 54 , error_tlvs :: !TlvStream 55 -- ^ extension TLV records (all unknown odd) 56 } deriving (Eq, Show, Generic) 57 58 instance NFData Error 59 60 -- | The @warning@ message (type 1). 61 data Warning = Warning 62 { warning_channel_id :: !ChannelId 63 , warning_data :: !BS.ByteString 64 , warning_tlvs :: !TlvStream 65 -- ^ extension TLV records (all unknown odd) 66 } deriving (Eq, Show, Generic) 67 68 instance NFData Warning 69 70 -- | The @ping@ message (type 18). 71 data Ping = Ping 72 { ping_num_pong_bytes :: {-# UNPACK #-} !Word16 73 , ping_ignored :: !BS.ByteString 74 , ping_tlvs :: !TlvStream 75 -- ^ extension TLV records (all unknown odd) 76 } deriving (Eq, Show, Generic) 77 78 instance NFData Ping 79 80 -- | The @pong@ message (type 19). 81 data Pong = Pong 82 { pong_ignored :: !BS.ByteString 83 , pong_tlvs :: !TlvStream 84 -- ^ extension TLV records (all unknown odd) 85 } deriving (Eq, Show, Generic) 86 87 instance NFData Pong 88 89 -- | The @peer_storage@ message (type 7). 90 data PeerStorage = PeerStorage 91 { peer_storage_blob :: !BS.ByteString 92 , peer_storage_tlvs :: !TlvStream 93 -- ^ extension TLV records (all unknown odd) 94 } deriving (Eq, Show, Generic) 95 96 instance NFData PeerStorage 97 98 -- | The @peer_storage_retrieval@ message (type 9). 99 data PeerStorageRetrieval = PeerStorageRetrieval 100 { peer_storage_retrieval_blob :: !BS.ByteString 101 , peer_storage_retrieval_tlvs :: !TlvStream 102 -- ^ extension TLV records (all unknown odd) 103 } deriving (Eq, Show, Generic) 104 105 instance NFData PeerStorageRetrieval 106 107 -- | A BOLT #1 message. 108 data Message 109 = MsgInit !Init 110 | MsgError !Error 111 | MsgWarning !Warning 112 | MsgPing !Ping 113 | MsgPong !Pong 114 | MsgPeerStorage !PeerStorage 115 | MsgPeerStorageRetrieval !PeerStorageRetrieval 116 deriving (Eq, Show, Generic) 117 118 instance NFData Message 119 120 -- | The wire type of a 'Message'. 121 -- 122 -- >>> message_type (MsgPong (Pong "" empty_tlv_stream)) 123 -- 19 124 message_type :: Message -> Word16 125 message_type m = case m of 126 MsgInit {} -> 16 127 MsgError {} -> 17 128 MsgWarning {} -> 1 129 MsgPing {} -> 18 130 MsgPong {} -> 19 131 MsgPeerStorage {} -> 7 132 MsgPeerStorageRetrieval {} -> 9