github.com/decred/dcrlnd@v0.7.6/htlcswitch/interfaces.go (about) 1 package htlcswitch 2 3 import ( 4 "github.com/decred/dcrd/wire" 5 "github.com/decred/dcrlnd/channeldb" 6 "github.com/decred/dcrlnd/invoices" 7 "github.com/decred/dcrlnd/lnpeer" 8 "github.com/decred/dcrlnd/lntypes" 9 "github.com/decred/dcrlnd/lnwallet" 10 "github.com/decred/dcrlnd/lnwallet/chainfee" 11 "github.com/decred/dcrlnd/lnwire" 12 "github.com/decred/dcrlnd/record" 13 ) 14 15 // InvoiceDatabase is an interface which represents the persistent subsystem 16 // which may search, lookup and settle invoices. 17 type InvoiceDatabase interface { 18 // LookupInvoice attempts to look up an invoice according to its 32 19 // byte payment hash. 20 LookupInvoice(lntypes.Hash) (channeldb.Invoice, error) 21 22 // NotifyExitHopHtlc attempts to mark an invoice as settled. If the 23 // invoice is a debug invoice, then this method is a noop as debug 24 // invoices are never fully settled. The return value describes how the 25 // htlc should be resolved. If the htlc cannot be resolved immediately, 26 // the resolution is sent on the passed in hodlChan later. The eob 27 // field passes the entire onion hop payload into the invoice registry 28 // for decoding purposes. 29 NotifyExitHopHtlc(payHash lntypes.Hash, paidAmount lnwire.MilliAtom, 30 expiry uint32, currentHeight int32, 31 circuitKey channeldb.CircuitKey, hodlChan chan<- interface{}, 32 payload invoices.Payload) (invoices.HtlcResolution, error) 33 34 // CancelInvoice attempts to cancel the invoice corresponding to the 35 // passed payment hash. 36 CancelInvoice(payHash lntypes.Hash) error 37 38 // SettleHodlInvoice settles a hold invoice. 39 SettleHodlInvoice(preimage lntypes.Preimage) error 40 41 // HodlUnsubscribeAll unsubscribes from all htlc resolutions. 42 HodlUnsubscribeAll(subscriber chan<- interface{}) 43 } 44 45 // packetHandler is an interface used exclusively by the Switch to handle 46 // htlcPacket and pass them to the link implementation. 47 type packetHandler interface { 48 // handleSwitchPacket handles the switch packets. These packets might 49 // be forwarded to us from another channel link in case the htlc 50 // update came from another peer or if the update was created by user 51 // initially. 52 // 53 // NOTE: This function should block as little as possible. 54 handleSwitchPacket(*htlcPacket) error 55 56 // handleLocalAddPacket handles a locally-initiated UpdateAddHTLC 57 // packet. It will be processed synchronously. 58 handleLocalAddPacket(*htlcPacket) error 59 } 60 61 // dustHandler is an interface used exclusively by the Switch to evaluate 62 // whether a link has too much dust exposure. 63 type dustHandler interface { 64 // getDustSum returns the dust sum on either the local or remote 65 // commitment. 66 getDustSum(remote bool) lnwire.MilliAtom 67 68 // getFeeRate returns the current channel feerate. 69 getFeeRate() chainfee.AtomPerKByte 70 71 // getDustClosure returns a closure that can evaluate whether a passed 72 // HTLC is dust. 73 getDustClosure() dustClosure 74 } 75 76 // ChannelUpdateHandler is an interface that provides methods that allow 77 // sending lnwire.Message to the underlying link as well as querying state. 78 type ChannelUpdateHandler interface { 79 // HandleChannelUpdate handles the htlc requests as settle/add/fail 80 // which sent to us from remote peer we have a channel with. 81 // 82 // NOTE: This function MUST be non-blocking (or block as little as 83 // possible). 84 HandleChannelUpdate(lnwire.Message) 85 86 // ChanID returns the channel ID for the channel link. The channel ID 87 // is a more compact representation of a channel's full outpoint. 88 ChanID() lnwire.ChannelID 89 90 // Bandwidth returns the amount of milli-satoshis which current link 91 // might pass through channel link. The value returned from this method 92 // represents the up to date available flow through the channel. This 93 // takes into account any forwarded but un-cleared HTLC's, and any 94 // HTLC's which have been set to the over flow queue. 95 Bandwidth() lnwire.MilliAtom 96 97 // EligibleToForward returns a bool indicating if the channel is able 98 // to actively accept requests to forward HTLC's. A channel may be 99 // active, but not able to forward HTLC's if it hasn't yet finalized 100 // the pre-channel operation protocol with the remote peer. The switch 101 // will use this function in forwarding decisions accordingly. 102 EligibleToForward() bool 103 104 // MayAddOutgoingHtlc returns an error if we may not add an outgoing 105 // htlc to the channel, taking the amount of the htlc to add as a 106 // parameter. 107 MayAddOutgoingHtlc(lnwire.MilliAtom) error 108 109 // ShutdownIfChannelClean shuts the link down if the channel state is 110 // clean. This can be used with dynamic commitment negotiation or coop 111 // close negotiation which require a clean channel state. 112 ShutdownIfChannelClean() error 113 } 114 115 // ChannelLink is an interface which represents the subsystem for managing the 116 // incoming htlc requests, applying the changes to the channel, and also 117 // propagating/forwarding it to htlc switch. 118 // 119 // abstraction level 120 // ^ 121 // | 122 // | - - - - - - - - - - - - Lightning - - - - - - - - - - - - - 123 // | 124 // | (Switch) (Switch) (Switch) 125 // | Alice <-- channel link --> Bob <-- channel link --> Carol 126 // | 127 // | - - - - - - - - - - - - - TCP - - - - - - - - - - - - - - - 128 // | 129 // | (Peer) (Peer) (Peer) 130 // | Alice <----- tcp conn --> Bob <---- tcp conn -----> Carol 131 // | 132 type ChannelLink interface { 133 // TODO(roasbeef): modify interface to embed mail boxes? 134 135 // Embed the packetHandler interface. 136 packetHandler 137 138 // Embed the ChannelUpdateHandler interface. 139 ChannelUpdateHandler 140 141 // Embed the dustHandler interface. 142 dustHandler 143 144 // ChannelPoint returns the channel outpoint for the channel link. 145 ChannelPoint() *wire.OutPoint 146 147 // ShortChanID returns the short channel ID for the channel link. The 148 // short channel ID encodes the exact location in the main chain that 149 // the original funding output can be found. 150 ShortChanID() lnwire.ShortChannelID 151 152 // UpdateShortChanID updates the short channel ID for a link. This may 153 // be required in the event that a link is created before the short 154 // chan ID for it is known, or a re-org occurs, and the funding 155 // transaction changes location within the chain. 156 UpdateShortChanID() (lnwire.ShortChannelID, error) 157 158 // UpdateForwardingPolicy updates the forwarding policy for the target 159 // ChannelLink. Once updated, the link will use the new forwarding 160 // policy to govern if it an incoming HTLC should be forwarded or not. 161 UpdateForwardingPolicy(ForwardingPolicy) 162 163 // CheckHtlcForward should return a nil error if the passed HTLC 164 // details satisfy the current forwarding policy fo the target link. 165 // Otherwise, a LinkError with a valid protocol failure message should 166 // be returned in order to signal to the source of the HTLC, the policy 167 // consistency issue. 168 CheckHtlcForward(payHash [32]byte, incomingAmt lnwire.MilliAtom, 169 amtToForward lnwire.MilliAtom, 170 incomingTimeout, outgoingTimeout uint32, 171 heightNow uint32) *LinkError 172 173 // CheckHtlcTransit should return a nil error if the passed HTLC 174 // details satisfy the current channel policy. Otherwise, a LinkError 175 // with a valid protocol failure message should be returned in order to 176 // signal the violation. This call is intended to be used for locally 177 // initiated payments for which there is no corresponding incoming 178 // htlc. 179 CheckHtlcTransit(payHash [32]byte, amt lnwire.MilliAtom, 180 timeout uint32, heightNow uint32) *LinkError 181 182 // Bandwidth returns the amount of milli-atoms which current link might 183 // pass through channel link. The value returned from this method 184 // represents the up to date available flow through the channel. This 185 // takes into account any forwarded but un-cleared HTLC's, and any 186 // HTLC's which have been set to the over flow queue. 187 Bandwidth() lnwire.MilliAtom 188 189 // Stats return the statistics of channel link. Number of updates, 190 // total sent/received milli-atoms. 191 Stats() (uint64, lnwire.MilliAtom, lnwire.MilliAtom) 192 193 // Peer returns the representation of remote peer with which we have 194 // the channel link opened. 195 Peer() lnpeer.Peer 196 197 // AttachMailBox delivers an active MailBox to the link. The MailBox may 198 // have buffered messages. 199 AttachMailBox(MailBox) 200 201 // Start/Stop are used to initiate the start/stop of the channel link 202 // functioning. 203 Start() error 204 Stop() 205 } 206 207 // ForwardingLog is an interface that represents a time series database which 208 // keep track of all successfully completed payment circuits. Every few 209 // seconds, the switch will collate and flush out all the successful payment 210 // circuits during the last interval. 211 type ForwardingLog interface { 212 // AddForwardingEvents is a method that should write out the set of 213 // forwarding events in a batch to persistent storage. Outside 214 // sub-systems can then query the contents of the log for analysis, 215 // visualizations, etc. 216 AddForwardingEvents([]channeldb.ForwardingEvent) error 217 } 218 219 // TowerClient is the primary interface used by the daemon to backup pre-signed 220 // justice transactions to watchtowers. 221 type TowerClient interface { 222 // RegisterChannel persistently initializes any channel-dependent 223 // parameters within the client. This should be called during link 224 // startup to ensure that the client is able to support the link during 225 // operation. 226 RegisterChannel(lnwire.ChannelID) error 227 228 // BackupState initiates a request to back up a particular revoked 229 // state. If the method returns nil, the backup is guaranteed to be 230 // successful unless the tower is unavailable and client is force quit, 231 // or the justice transaction would create dust outputs when trying to 232 // abide by the negotiated policy. If the channel we're trying to back 233 // up doesn't have a tweak for the remote party's output, then 234 // isTweakless should be true. 235 BackupState(*lnwire.ChannelID, *lnwallet.BreachRetribution, 236 channeldb.ChannelType) error 237 } 238 239 // InterceptableHtlcForwarder is the interface to set the interceptor 240 // implementation that intercepts htlc forwards. 241 type InterceptableHtlcForwarder interface { 242 // SetInterceptor sets a ForwardInterceptor. 243 SetInterceptor(interceptor ForwardInterceptor) 244 } 245 246 // ForwardInterceptor is a function that is invoked from the switch for every 247 // incoming htlc that is intended to be forwarded. It is passed with the 248 // InterceptedForward that contains the information about the packet and a way 249 // to resolve it manually later in case it is held. 250 // The return value indicates if this handler will take control of this forward 251 // and resolve it later or let the switch execute its default behavior. 252 type ForwardInterceptor func(InterceptedForward) bool 253 254 // InterceptedPacket contains the relevant information for the interceptor about 255 // an htlc. 256 type InterceptedPacket struct { 257 // IncomingCircuit contains the incoming channel and htlc id of the 258 // packet. 259 IncomingCircuit channeldb.CircuitKey 260 261 // OutgoingChanID is the destination channel for this packet. 262 OutgoingChanID lnwire.ShortChannelID 263 264 // Hash is the payment hash of the htlc. 265 Hash lntypes.Hash 266 267 // OutgoingExpiry is the absolute block height at which the outgoing 268 // htlc expires. 269 OutgoingExpiry uint32 270 271 // OutgoingAmount is the amount to forward. 272 OutgoingAmount lnwire.MilliAtom 273 274 // IncomingExpiry is the absolute block height at which the incoming 275 // htlc expires. 276 IncomingExpiry uint32 277 278 // IncomingAmount is the amount of the accepted htlc. 279 IncomingAmount lnwire.MilliAtom 280 281 // CustomRecords are user-defined records in the custom type range that 282 // were included in the payload. 283 CustomRecords record.CustomSet 284 285 // OnionBlob is the onion packet for the next hop 286 OnionBlob [lnwire.OnionPacketSize]byte 287 } 288 289 // InterceptedForward is passed to the ForwardInterceptor for every forwarded 290 // htlc. It contains all the information about the packet which accordingly 291 // the interceptor decides if to hold or not. 292 // In addition this interface allows a later resolution by calling either 293 // Resume, Settle or Fail. 294 type InterceptedForward interface { 295 // Packet returns the intercepted packet. 296 Packet() InterceptedPacket 297 298 // Resume notifies the intention to resume an existing hold forward. This 299 // basically means the caller wants to resume with the default behavior for 300 // this htlc which usually means forward it. 301 Resume() error 302 303 // Settle notifies the intention to settle an existing hold 304 // forward with a given preimage. 305 Settle(lntypes.Preimage) error 306 307 // Fails notifies the intention to fail an existing hold forward 308 Fail() error 309 } 310 311 // htlcNotifier is an interface which represents the input side of the 312 // HtlcNotifier which htlc events are piped through. This interface is intended 313 // to allow for mocking of the htlcNotifier in tests, so is unexported because 314 // it is not needed outside of the htlcSwitch package. 315 type htlcNotifier interface { 316 // NotifyForwardingEvent notifies the HtlcNotifier than a htlc has been 317 // forwarded. 318 NotifyForwardingEvent(key HtlcKey, info HtlcInfo, 319 eventType HtlcEventType) 320 321 // NotifyIncomingLinkFailEvent notifies that a htlc has failed on our 322 // incoming link. It takes an isReceive bool to differentiate between 323 // our node's receives and forwards. 324 NotifyLinkFailEvent(key HtlcKey, info HtlcInfo, 325 eventType HtlcEventType, linkErr *LinkError, incoming bool) 326 327 // NotifyForwardingFailEvent notifies the HtlcNotifier that a htlc we 328 // forwarded has failed down the line. 329 NotifyForwardingFailEvent(key HtlcKey, eventType HtlcEventType) 330 331 // NotifySettleEvent notifies the HtlcNotifier that a htlc that we 332 // committed to as part of a forward or a receive to our node has been 333 // settled. 334 NotifySettleEvent(key HtlcKey, preimage lntypes.Preimage, 335 eventType HtlcEventType) 336 }