Skip to content

Commit

Permalink
PaymentLifecycle handle disconnected peers (#1688)
Browse files Browse the repository at this point in the history
When a peer is disconnected, the register will return a forward failure.
This can happen if the peer is connected when we start the payment FSM and
then disconnects before we send them an HTLC.
  • Loading branch information
t-bast authored Feb 12, 2021
1 parent 15c1837 commit 72179de
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ class PaymentLifecycle(nodeParams: NodeParams, cfg: SendPaymentConfig, router: A
case Event(RES_ADD_FAILED(_, t: ChannelException, _), d: WaitingForComplete) =>
handleLocalFail(d, t, isFatal = false)

case Event(_: Register.ForwardShortIdFailure[CMD_ADD_HTLC], d: WaitingForComplete) =>
handleLocalFail(d, DisconnectedException, isFatal = false)

case Event(RES_ADD_SETTLED(_, htlc, fulfill: HtlcResult.Fulfill), d: WaitingForComplete) =>
Metrics.PaymentAttempt.withTag(Tags.MultiPart, value = false).record(d.failures.size + 1)
val p = PartialPayment(id, d.c.finalPayload.amount, d.cmd.amount - d.c.finalPayload.amount, htlc.channelId, Some(cfg.fullRoute(d.route)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import fr.acinq.bitcoin.Script.{pay2wsh, write}
import fr.acinq.bitcoin.{Block, ByteVector32, Crypto, SatoshiLong, Transaction, TxOut}
import fr.acinq.eclair._
import fr.acinq.eclair.blockchain.{UtxoStatus, ValidateRequest, ValidateResult, WatchSpentBasic}
import fr.acinq.eclair.channel.Register.ForwardShortId
import fr.acinq.eclair.channel.Register.{ForwardShortId, ForwardShortIdFailure}
import fr.acinq.eclair.channel._
import fr.acinq.eclair.crypto.Sphinx
import fr.acinq.eclair.db.{OutgoingPayment, OutgoingPaymentStatus, PaymentType}
Expand Down Expand Up @@ -266,13 +266,11 @@ class PaymentLifecycleSpec extends BaseRouterSpec {
val request = SendPayment(sender.ref, d, FinalLegacyPayload(defaultAmountMsat, defaultExpiry), 2)
sender.send(paymentFSM, request)
awaitCond(paymentFSM.stateName == WAITING_FOR_ROUTE && nodeParams.db.payments.getOutgoingPayment(id).exists(_.status === OutgoingPaymentStatus.Pending))

val WaitingForRoute(_, Nil, _) = paymentFSM.stateData
routerForwarder.expectMsg(defaultRouteRequest(nodeParams.nodeId, d, cfg))
routerForwarder.expectMsgType[RouteRequest]
routerForwarder.forward(routerFixture.router)
awaitCond(paymentFSM.stateName == WAITING_FOR_PAYMENT_COMPLETE)
val WaitingForComplete(_, cmd1, Nil, _, _, _) = paymentFSM.stateData

val WaitingForComplete(_, cmd1, Nil, _, _, _) = paymentFSM.stateData
register.expectMsg(ForwardShortId(paymentFSM, channelId_ab, cmd1))
sender.send(paymentFSM, RES_ADD_FAILED(cmd1, ChannelUnavailable(ByteVector32.Zeroes), None))

Expand All @@ -281,6 +279,26 @@ class PaymentLifecycleSpec extends BaseRouterSpec {
awaitCond(paymentFSM.stateName == WAITING_FOR_ROUTE && nodeParams.db.payments.getOutgoingPayment(id).exists(_.status === OutgoingPaymentStatus.Pending)) // payment is still pending because the error is recoverable
}

test("payment failed (register error)") { routerFixture =>
val payFixture = createPaymentLifecycle()
import payFixture._
import cfg._

val request = SendPayment(sender.ref, d, FinalLegacyPayload(defaultAmountMsat, defaultExpiry), 2)
sender.send(paymentFSM, request)
awaitCond(paymentFSM.stateName == WAITING_FOR_ROUTE && nodeParams.db.payments.getOutgoingPayment(id).exists(_.status === OutgoingPaymentStatus.Pending))
routerForwarder.expectMsgType[RouteRequest]
routerForwarder.forward(routerFixture.router)
awaitCond(paymentFSM.stateName == WAITING_FOR_PAYMENT_COMPLETE)

val fwd = register.expectMsgType[ForwardShortId[CMD_ADD_HTLC]]
register.send(paymentFSM, ForwardShortIdFailure(fwd))

// then the payment lifecycle will ask for a new route excluding the channel
routerForwarder.expectMsg(defaultRouteRequest(nodeParams.nodeId, d, cfg).copy(ignore = Ignore(Set.empty, Set(ChannelDesc(channelId_ab, a, b)))))
awaitCond(paymentFSM.stateName == WAITING_FOR_ROUTE && nodeParams.db.payments.getOutgoingPayment(id).exists(_.status === OutgoingPaymentStatus.Pending)) // payment is still pending because the error is recoverable
}

test("payment failed (first hop returns an UpdateFailMalformedHtlc)") { routerFixture =>
val payFixture = createPaymentLifecycle()
import payFixture._
Expand Down

0 comments on commit 72179de

Please sign in to comment.