|
| 1 | +package invoices |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "sync" |
| 6 | + |
| 7 | + "github.com/lightningnetwork/lnd/fn" |
| 8 | + "github.com/lightningnetwork/lnd/lnwire" |
| 9 | +) |
| 10 | + |
| 11 | +// InterceptSession creates a session that is returned to the caller when an |
| 12 | +// invoice is submitted to this service. This session allows the caller to block |
| 13 | +// until the invoice is processed. |
| 14 | +type InterceptSession struct { |
| 15 | + HtlcModifyRequest |
| 16 | + |
| 17 | + // ClientResponseChannel is a channel that is populated with the |
| 18 | + // client's interceptor response during an interceptor session. |
| 19 | + ClientResponseChannel chan HtlcModifyResponse |
| 20 | + |
| 21 | + // ClientErrChannel is a channel that is populated with any errors that |
| 22 | + // occur during the client's interceptor session. |
| 23 | + ClientErrChannel chan error |
| 24 | + |
| 25 | + // Quit is a channel that is closed when the session is no longer |
| 26 | + // needed. |
| 27 | + Quit chan struct{} |
| 28 | +} |
| 29 | + |
| 30 | +// HtlcModificationInterceptor is a service that intercepts HTLCs that aim to |
| 31 | +// settle an invoice, enabling a subscribed client to modify certain aspects of |
| 32 | +// those HTLCs. |
| 33 | +type HtlcModificationInterceptor struct { |
| 34 | + wg sync.WaitGroup |
| 35 | + |
| 36 | + // mu is a mutex that protects the callback and activeSessions fields. |
| 37 | + mu sync.Mutex |
| 38 | + |
| 39 | + // callback is the client callback function that is called when an |
| 40 | + // invoice is intercepted. This function gives the client the ability to |
| 41 | + // determine how the invoice should be settled. |
| 42 | + callback HtlcModifyCallback |
| 43 | + |
| 44 | + // activeSessions is a map of active intercept sessions that are used to |
| 45 | + // manage the client query/response for a given invoice payment hash. |
| 46 | + activeSessions map[CircuitKey]InterceptSession |
| 47 | +} |
| 48 | + |
| 49 | +// NewHtlcModificationInterceptor creates a new HtlcModificationInterceptor. |
| 50 | +func NewHtlcModificationInterceptor() *HtlcModificationInterceptor { |
| 51 | + return &HtlcModificationInterceptor{ |
| 52 | + activeSessions: make(map[CircuitKey]InterceptSession), |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +// Intercept generates a new intercept session for the given invoice HTLC. The |
| 57 | +// session is returned to the caller so that they can block until the client |
| 58 | +// resolution is received. |
| 59 | +func (s *HtlcModificationInterceptor) Intercept( |
| 60 | + clientRequest HtlcModifyRequest) fn.Option[InterceptSession] { |
| 61 | + |
| 62 | + s.mu.Lock() |
| 63 | + defer s.mu.Unlock() |
| 64 | + |
| 65 | + // If there is no client callback set we will not handle the invoice |
| 66 | + // further. |
| 67 | + if s.callback == nil { |
| 68 | + return fn.None[InterceptSession]() |
| 69 | + } |
| 70 | + |
| 71 | + // Create and store a new intercept session for the invoice. We will use |
| 72 | + // the payment hash as the storage/retrieval key for the session. |
| 73 | + sessionKey := clientRequest.ExitHtlcCircuitKey |
| 74 | + session := InterceptSession{ |
| 75 | + HtlcModifyRequest: clientRequest, |
| 76 | + ClientResponseChannel: make(chan HtlcModifyResponse, 1), |
| 77 | + ClientErrChannel: make(chan error, 1), |
| 78 | + Quit: make(chan struct{}, 1), |
| 79 | + } |
| 80 | + s.activeSessions[sessionKey] = session |
| 81 | + |
| 82 | + // The callback function will block at the client's discretion. We will |
| 83 | + // therefore execute it in a separate goroutine. |
| 84 | + s.wg.Add(1) |
| 85 | + go func(cb HtlcModifyCallback) { |
| 86 | + defer s.wg.Done() |
| 87 | + |
| 88 | + // By this point, we've already checked that the client callback |
| 89 | + // is set. However, if the client callback has been set to nil |
| 90 | + // since that check then Exec will return an error. |
| 91 | + err := cb(clientRequest) |
| 92 | + if err != nil { |
| 93 | + err = fmt.Errorf("client callback failed: %w", err) |
| 94 | + log.Error(err) |
| 95 | + session.ClientErrChannel <- err |
| 96 | + } |
| 97 | + }(s.callback) |
| 98 | + |
| 99 | + // Return the session to the caller so that they can block until the |
| 100 | + // resolution is received. |
| 101 | + return fn.Some(session) |
| 102 | +} |
| 103 | + |
| 104 | +// Modify changes parts of the HTLC based on the client's response. |
| 105 | +func (s *HtlcModificationInterceptor) Modify(htlc CircuitKey, |
| 106 | + amountPaid lnwire.MilliSatoshi) error { |
| 107 | + |
| 108 | + s.mu.Lock() |
| 109 | + defer s.mu.Unlock() |
| 110 | + |
| 111 | + // Retrieve the intercept session for the invoice payment hash. |
| 112 | + session, ok := s.activeSessions[htlc] |
| 113 | + if !ok { |
| 114 | + return fmt.Errorf("invoice intercept session not found "+ |
| 115 | + "(circuit_key=%s)", htlc) |
| 116 | + } |
| 117 | + |
| 118 | + // Send the resolution to the session resolution channel. |
| 119 | + resolution := HtlcModifyResponse{ |
| 120 | + AmountPaid: amountPaid, |
| 121 | + } |
| 122 | + sendSuccessful := fn.SendOrQuit( |
| 123 | + session.ClientResponseChannel, resolution, session.Quit, |
| 124 | + ) |
| 125 | + if !sendSuccessful { |
| 126 | + return fmt.Errorf("failed to send modification to client") |
| 127 | + } |
| 128 | + |
| 129 | + return nil |
| 130 | +} |
| 131 | + |
| 132 | +// SetClientCallback sets the client callback function that will be called when |
| 133 | +// an invoice is intercepted. |
| 134 | +func (s *HtlcModificationInterceptor) SetClientCallback( |
| 135 | + callback HtlcModifyCallback) { |
| 136 | + |
| 137 | + s.mu.Lock() |
| 138 | + defer s.mu.Unlock() |
| 139 | + |
| 140 | + s.callback = callback |
| 141 | +} |
| 142 | + |
| 143 | +// QuitSession closes the quit channel for the session associated with the |
| 144 | +// given invoice. This signals to the client that the session has ended. |
| 145 | +func (s *HtlcModificationInterceptor) QuitSession( |
| 146 | + session InterceptSession) error { |
| 147 | + |
| 148 | + s.mu.Lock() |
| 149 | + defer s.mu.Unlock() |
| 150 | + |
| 151 | + // Retrieve the intercept session and delete it from the local cache. |
| 152 | + sessionKey := session.ExitHtlcCircuitKey |
| 153 | + activeSession, ok := s.activeSessions[sessionKey] |
| 154 | + if !ok { |
| 155 | + // If the session is not found, no further action is necessary. |
| 156 | + return nil |
| 157 | + } |
| 158 | + |
| 159 | + // Send to the quit channel to signal the client that the session has |
| 160 | + // ended. |
| 161 | + close(activeSession.Quit) |
| 162 | + delete(s.activeSessions, sessionKey) |
| 163 | + |
| 164 | + return nil |
| 165 | +} |
| 166 | + |
| 167 | +// QuitActiveSessions quits all active sessions by sending on each session quit |
| 168 | +// channel. |
| 169 | +func (s *HtlcModificationInterceptor) QuitActiveSessions() error { |
| 170 | + s.mu.Lock() |
| 171 | + defer s.mu.Unlock() |
| 172 | + |
| 173 | + for key := range s.activeSessions { |
| 174 | + session := s.activeSessions[key] |
| 175 | + close(session.Quit) |
| 176 | + delete(s.activeSessions, key) |
| 177 | + } |
| 178 | + |
| 179 | + return nil |
| 180 | +} |
| 181 | + |
| 182 | +// Start starts the service. |
| 183 | +func (s *HtlcModificationInterceptor) Start() error { |
| 184 | + return nil |
| 185 | +} |
| 186 | + |
| 187 | +// Stop stops the service. |
| 188 | +func (s *HtlcModificationInterceptor) Stop() error { |
| 189 | + // If the service is stopping, we will quit all active sessions. |
| 190 | + return s.QuitActiveSessions() |
| 191 | +} |
| 192 | + |
| 193 | +// Ensure that HtlcModificationInterceptor implements the HtlcAcceptor and |
| 194 | +// HtlcModifier interfaces. |
| 195 | +var _ HtlcModifier = (*HtlcModificationInterceptor)(nil) |
| 196 | + |
| 197 | +// MockHtlcModifier is a mock implementation of the HtlcModifier interface. |
| 198 | +type MockHtlcModifier struct { |
| 199 | +} |
| 200 | + |
| 201 | +// Intercept generates a new intercept session for the given invoice. The |
| 202 | +// session is returned to the caller so that they can block until the client |
| 203 | +// resolution is received. |
| 204 | +func (m *MockHtlcModifier) Intercept( |
| 205 | + _ HtlcModifyRequest) fn.Option[InterceptSession] { |
| 206 | + |
| 207 | + return fn.None[InterceptSession]() |
| 208 | +} |
| 209 | + |
| 210 | +// SetClientCallback sets the client callback function that will be called when |
| 211 | +// an invoice is intercepted. |
| 212 | +func (m *MockHtlcModifier) SetClientCallback(HtlcModifyCallback) { |
| 213 | +} |
| 214 | + |
| 215 | +// Modify changes parts of the HTLC based on the client's response. |
| 216 | +func (m *MockHtlcModifier) Modify(CircuitKey, |
| 217 | + lnwire.MilliSatoshi) error { |
| 218 | + |
| 219 | + return nil |
| 220 | +} |
| 221 | + |
| 222 | +// Ensure that MockHtlcModifier implements the HtlcAcceptor |
| 223 | +// interface. |
| 224 | +var _ HtlcModifier = (*MockHtlcModifier)(nil) |
0 commit comments