|
4 | 4 | "context" |
5 | 5 | "crypto/rand" |
6 | 6 | "crypto/sha256" |
| 7 | + "errors" |
7 | 8 | "fmt" |
8 | 9 | "sync" |
9 | 10 | "time" |
@@ -135,17 +136,55 @@ func newLoopInSwap(globalCtx context.Context, cfg *swapConfig, |
135 | 136 | return nil, err |
136 | 137 | } |
137 | 138 |
|
| 139 | + // Create the probe invoice in lnd. Derive the payment hash |
| 140 | + // deterministically from the swap hash in such a way that the server |
| 141 | + // can be sure that we don't know the preimage. |
| 142 | + probeHash := lntypes.Hash(sha256.Sum256(swapHash[:])) |
| 143 | + probeHash[0] ^= 1 |
| 144 | + |
| 145 | + log.Infof("Creating probe invoice %v", probeHash) |
| 146 | + probeInvoice, err := cfg.lnd.Invoices.AddHoldInvoice( |
| 147 | + globalCtx, &invoicesrpc.AddInvoiceData{ |
| 148 | + Hash: &probeHash, |
| 149 | + Value: lnwire.NewMSatFromSatoshis(swapInvoiceAmt), |
| 150 | + Memo: "loop in probe", |
| 151 | + Expiry: 3600, |
| 152 | + }, |
| 153 | + ) |
| 154 | + if err != nil { |
| 155 | + return nil, err |
| 156 | + } |
| 157 | + |
| 158 | + // Create a cancellable context that is used for monitoring the probe. |
| 159 | + probeWaitCtx, probeWaitCancel := context.WithCancel(globalCtx) |
| 160 | + |
| 161 | + // Launch a goroutine to monitor the probe. |
| 162 | + probeResult, err := awaitProbe(probeWaitCtx, *cfg.lnd, probeHash) |
| 163 | + if err != nil { |
| 164 | + probeWaitCancel() |
| 165 | + return nil, fmt.Errorf("probe failed: %v", err) |
| 166 | + } |
| 167 | + |
138 | 168 | // Post the swap parameters to the swap server. The response contains |
139 | 169 | // the server success key and the expiry height of the on-chain swap |
140 | 170 | // htlc. |
141 | 171 | log.Infof("Initiating swap request at height %v", currentHeight) |
142 | 172 | swapResp, err := cfg.server.NewLoopInSwap(globalCtx, swapHash, |
143 | | - request.Amount, senderKey, swapInvoice, request.LastHop, |
| 173 | + request.Amount, senderKey, swapInvoice, probeInvoice, |
| 174 | + request.LastHop, |
144 | 175 | ) |
| 176 | + probeWaitCancel() |
145 | 177 | if err != nil { |
146 | 178 | return nil, fmt.Errorf("cannot initiate swap: %v", err) |
147 | 179 | } |
148 | 180 |
|
| 181 | + // Because the context is cancelled, it is guaranteed that we will be |
| 182 | + // able to read from the probeResult channel. |
| 183 | + err = <-probeResult |
| 184 | + if err != nil { |
| 185 | + return nil, fmt.Errorf("probe error: %v", err) |
| 186 | + } |
| 187 | + |
149 | 188 | // Validate the response parameters the prevent us continuing with a |
150 | 189 | // swap that is based on parameters outside our allowed range. |
151 | 190 | err = validateLoopInContract(cfg.lnd, currentHeight, request, swapResp) |
@@ -209,6 +248,72 @@ func newLoopInSwap(globalCtx context.Context, cfg *swapConfig, |
209 | 248 | }, nil |
210 | 249 | } |
211 | 250 |
|
| 251 | +// awaitProbe waits for a probe payment to arrive and cancels it. This is a |
| 252 | +// workaround for the current lack of multi-path probing. |
| 253 | +func awaitProbe(ctx context.Context, lnd lndclient.LndServices, |
| 254 | + probeHash lntypes.Hash) (chan error, error) { |
| 255 | + |
| 256 | + // Subscribe to the probe invoice. |
| 257 | + updateChan, errChan, err := lnd.Invoices.SubscribeSingleInvoice( |
| 258 | + ctx, probeHash, |
| 259 | + ) |
| 260 | + if err != nil { |
| 261 | + return nil, err |
| 262 | + } |
| 263 | + |
| 264 | + // Wait in the background for the probe to arrive. |
| 265 | + probeResult := make(chan error, 1) |
| 266 | + |
| 267 | + go func() { |
| 268 | + for { |
| 269 | + select { |
| 270 | + case update := <-updateChan: |
| 271 | + switch update.State { |
| 272 | + case channeldb.ContractAccepted: |
| 273 | + log.Infof("Server probe successful") |
| 274 | + probeResult <- nil |
| 275 | + |
| 276 | + // Cancel probe invoice so that the |
| 277 | + // server will know that its probe was |
| 278 | + // successful. |
| 279 | + err := lnd.Invoices.CancelInvoice( |
| 280 | + ctx, probeHash, |
| 281 | + ) |
| 282 | + if err != nil { |
| 283 | + log.Errorf("Cancel probe "+ |
| 284 | + "invoice: %v", err) |
| 285 | + } |
| 286 | + |
| 287 | + return |
| 288 | + |
| 289 | + case channeldb.ContractCanceled: |
| 290 | + probeResult <- errors.New( |
| 291 | + "probe invoice expired") |
| 292 | + |
| 293 | + return |
| 294 | + |
| 295 | + case channeldb.ContractSettled: |
| 296 | + probeResult <- errors.New( |
| 297 | + "impossible that probe " + |
| 298 | + "invoice was settled") |
| 299 | + |
| 300 | + return |
| 301 | + } |
| 302 | + |
| 303 | + case err := <-errChan: |
| 304 | + probeResult <- err |
| 305 | + return |
| 306 | + |
| 307 | + case <-ctx.Done(): |
| 308 | + probeResult <- ctx.Err() |
| 309 | + return |
| 310 | + } |
| 311 | + } |
| 312 | + }() |
| 313 | + |
| 314 | + return probeResult, nil |
| 315 | +} |
| 316 | + |
212 | 317 | // resumeLoopInSwap returns a swap object representing a pending swap that has |
213 | 318 | // been restored from the database. |
214 | 319 | func resumeLoopInSwap(reqContext context.Context, cfg *swapConfig, |
|
0 commit comments