Skip to content

Commit c9911e4

Browse files
committed
Refactor to use Peer instead of Switchboard
- use existing GetPeerInfo when creating or restoring a swap to find the remote peer actor - use Peer.RelayUnknownMessage to send custom Lightning messages
1 parent 3b6e111 commit c9911e4

File tree

13 files changed

+205
-154
lines changed

13 files changed

+205
-154
lines changed

peerswap/src/main/scala/fr/acinq/eclair/plugins/peerswap/PeerSwapPlugin.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class PeerSwapPlugin extends Plugin with RouteProvider with Logging {
8888
}
8989

9090
override def onKit(kit: Kit): Unit = {
91-
val data = db.restore().toSet
91+
val data = db.restore()
9292
val swapRegister = kit.system.spawn(Behaviors.supervise(SwapRegister(kit.nodeParams, kit.paymentInitiator, kit.watcher, kit.register, kit.switchboard, kit.wallet, swapKeyManager, db, data)).onFailure(SupervisorStrategy.restart), "peerswap-plugin-swap-register")
9393
pluginKit = PeerSwapKit(kit.nodeParams, kit.system, swapRegister, db)
9494
}
@@ -101,10 +101,10 @@ case class PeerSwapKit(nodeParams: NodeParams, system: ActorSystem, swapRegister
101101
private implicit val ec: ExecutionContext = ExecutionContext.fromExecutor(Executors.newSingleThreadExecutor(new ThreadFactoryBuilder().setNameFormat("db-pending-commands").build()))
102102

103103
def swapIn(shortChannelId: ShortChannelId, amount: Satoshi)(implicit timeout: Timeout): Future[Response] =
104-
swapRegister.ask(ref => SwapRegister.SwapRequested(ref, Maker, amount, shortChannelId, None))(timeout, system.scheduler.toTyped)
104+
swapRegister.ask(ref => SwapRegister.SwapRequested(ref, Maker, amount, shortChannelId, None, None))(timeout, system.scheduler.toTyped)
105105

106106
def swapOut(shortChannelId: ShortChannelId, amount: Satoshi)(implicit timeout: Timeout): Future[Response] =
107-
swapRegister.ask(ref => SwapRegister.SwapRequested(ref, Taker, amount, shortChannelId, None))(timeout, system.scheduler.toTyped)
107+
swapRegister.ask(ref => SwapRegister.SwapRequested(ref, Taker, amount, shortChannelId, None, None))(timeout, system.scheduler.toTyped)
108108

109109
def listSwaps()(implicit timeout: Timeout): Future[Iterable[Status]] =
110110
swapRegister.ask(ref => SwapRegister.ListPendingSwaps(ref))(timeout, system.scheduler.toTyped)

peerswap/src/main/scala/fr/acinq/eclair/plugins/peerswap/SwapHelpers.scala

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import akka.actor.typed.{ActorRef, Behavior}
2323
import fr.acinq.bitcoin.ScriptFlags
2424
import fr.acinq.bitcoin.scalacompat.Crypto.PublicKey
2525
import fr.acinq.bitcoin.scalacompat.{ByteVector32, Crypto, Satoshi, Transaction}
26-
import fr.acinq.eclair.Features.RouteBlinding
2726
import fr.acinq.eclair.MilliSatoshi.toMilliSatoshi
2827
import fr.acinq.eclair.blockchain.OnChainWallet
2928
import fr.acinq.eclair.blockchain.OnChainWallet.MakeFundingTxResponse
@@ -32,7 +31,7 @@ import fr.acinq.eclair.blockchain.bitcoind.ZmqWatcher._
3231
import fr.acinq.eclair.blockchain.fee.FeeratePerKw
3332
import fr.acinq.eclair.db.OutgoingPaymentStatus.{Failed, Pending, Succeeded}
3433
import fr.acinq.eclair.db.PaymentType
35-
import fr.acinq.eclair.io.Switchboard.ForwardUnknownMessage
34+
import fr.acinq.eclair.io.Peer.RelayUnknownMessage
3635
import fr.acinq.eclair.payment.send.PaymentInitiator.SendPaymentToNode
3736
import fr.acinq.eclair.payment.{Bolt11Invoice, PaymentFailed, PaymentReceived, PaymentSent}
3837
import fr.acinq.eclair.plugins.peerswap.SwapCommands._
@@ -99,8 +98,9 @@ private object SwapHelpers {
9998
UnknownMessage(encoded.sliceToInt(0, 16, signed = false), encoded.drop(16).toByteVector)
10099
}
101100

102-
def send(switchboard: actor.ActorRef, remoteNodeId: PublicKey)(message: HasSwapId): Unit =
103-
switchboard ! ForwardUnknownMessage(remoteNodeId, makeUnknownMessage(message))
101+
def send(remotePeer: actor.ActorRef)(message: HasSwapId): Unit = {
102+
remotePeer ! RelayUnknownMessage(makeUnknownMessage(message))
103+
}
104104

105105
def fundOpening(wallet: OnChainWallet, feeRatePerKw: FeeratePerKw)(amount: Satoshi, makerPubkey: PublicKey, takerPubkey: PublicKey, invoice: Bolt11Invoice)(implicit context: ActorContext[SwapCommand]): Unit = {
106106
// setup conditions satisfied, create the opening tx
@@ -158,10 +158,9 @@ private object SwapHelpers {
158158
def createInvoice(nodeParams: NodeParams, amount: Satoshi, description: String)(implicit context: ActorContext[SwapCommand]): Try[Bolt11Invoice] =
159159
Try {
160160
val paymentPreimage = randomBytes32()
161-
val invoiceFeatures = nodeParams.features.invoiceFeatures().remove(RouteBlinding)
162161
val invoice: Bolt11Invoice = Bolt11Invoice(nodeParams.chainHash, Some(toMilliSatoshi(amount)), Crypto.sha256(paymentPreimage), nodeParams.privateKey, Left(description),
163162
nodeParams.channelConf.minFinalExpiryDelta, fallbackAddress = None, expirySeconds = Some(nodeParams.invoiceExpiry.toSeconds),
164-
extraHops = Nil, timestamp = TimestampSecond.now(), paymentSecret = paymentPreimage, paymentMetadata = None, features = invoiceFeatures)
163+
extraHops = Nil, timestamp = TimestampSecond.now(), paymentSecret = paymentPreimage, paymentMetadata = None)
165164
context.log.debug("generated invoice={} from amount={} sat, description={}", invoice.toString, amount, description)
166165
nodeParams.db.payments.addIncomingPayment(invoice, paymentPreimage, PaymentType.Standard)
167166
invoice

peerswap/src/main/scala/fr/acinq/eclair/plugins/peerswap/SwapMaker.scala

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,21 +105,21 @@ object SwapMaker {
105105
106106
*/
107107

108-
def apply(remoteNodeId: PublicKey, nodeParams: NodeParams, watcher: ActorRef[ZmqWatcher.Command], switchboard: actor.ActorRef, wallet: OnChainWallet, keyManager: SwapKeyManager, db: SwapsDb): Behavior[SwapCommands.SwapCommand] =
108+
def apply(remoteNodeId: PublicKey, nodeParams: NodeParams, watcher: ActorRef[ZmqWatcher.Command], remotePeer: actor.ActorRef, wallet: OnChainWallet, keyManager: SwapKeyManager, db: SwapsDb): Behavior[SwapCommands.SwapCommand] =
109109
Behaviors.setup { context =>
110110
Behaviors.receiveMessagePartial {
111-
case StartSwapInSender(amount, swapId, shortChannelId) => new SwapMaker(remoteNodeId, shortChannelId, nodeParams, watcher, switchboard, wallet, keyManager, db, context)
111+
case StartSwapInSender(amount, swapId, shortChannelId) => new SwapMaker(remoteNodeId, shortChannelId, nodeParams, watcher, remotePeer, wallet, keyManager, db, context)
112112
.createSwap(amount, swapId)
113113
case StartSwapOutReceiver(request: SwapOutRequest) =>
114114
ShortChannelId.fromCoordinates(request.scid) match {
115-
case Success(shortChannelId) => new SwapMaker(remoteNodeId, shortChannelId, nodeParams, watcher, switchboard, wallet, keyManager, db, context)
115+
case Success(shortChannelId) => new SwapMaker(remoteNodeId, shortChannelId, nodeParams, watcher, remotePeer, wallet, keyManager, db, context)
116116
.validateRequest(request)
117117
case Failure(e) => context.log.error(s"received swap request with invalid shortChannelId: $request, $e")
118118
Behaviors.stopped
119119
}
120120
case RestoreSwap(d) =>
121121
ShortChannelId.fromCoordinates(d.request.scid) match {
122-
case Success(shortChannelId) => new SwapMaker(remoteNodeId, shortChannelId, nodeParams, watcher, switchboard, wallet, keyManager, db, context)
122+
case Success(shortChannelId) => new SwapMaker(remoteNodeId, shortChannelId, nodeParams, watcher, remotePeer, wallet, keyManager, db, context)
123123
.awaitClaimPayment(d.request, d.agreement, d.invoice, d.openingTxBroadcasted, d.isInitiator)
124124
case Failure(e) =>
125125
context.log.error(s"Could not restore from a checkpoint with an invalid shortChannelId: $d, $e")
@@ -130,7 +130,7 @@ object SwapMaker {
130130
}
131131
}
132132

133-
private class SwapMaker(remoteNodeId: PublicKey, shortChannelId: ShortChannelId, nodeParams: NodeParams, watcher: ActorRef[ZmqWatcher.Command], switchboard: actor.ActorRef, wallet: OnChainWallet, keyManager: SwapKeyManager, db: SwapsDb, implicit val context: ActorContext[SwapCommands.SwapCommand]) {
133+
private class SwapMaker(remoteNodeId: PublicKey, shortChannelId: ShortChannelId, nodeParams: NodeParams, watcher: ActorRef[ZmqWatcher.Command], remotePeer: actor.ActorRef, wallet: OnChainWallet, keyManager: SwapKeyManager, db: SwapsDb, implicit val context: ActorContext[SwapCommands.SwapCommand]) {
134134
private val protocolVersion = 3
135135
private val noAsset = ""
136136
private implicit val feeRatePerKw: FeeratePerKw = nodeParams.onChainFeeConf.feeEstimator.getFeeratePerKw(target = nodeParams.onChainFeeConf.feeTargets.fundingBlockTarget)
@@ -167,7 +167,7 @@ private class SwapMaker(remoteNodeId: PublicKey, shortChannelId: ShortChannelId,
167167

168168
private def awaitFeePayment(request: SwapOutRequest, agreement: SwapOutAgreement, invoice: Bolt11Invoice): Behavior[SwapCommand] = {
169169
watchForPaymentReceived(watch = true)
170-
send(switchboard, remoteNodeId)(agreement)
170+
send(remotePeer)(agreement)
171171
Behaviors.withTimers { timers =>
172172
timers.startSingleTimer(swapFeeExpiredTimer(request.swapId), InvoiceExpired, invoice.createdAt + invoice.relativeExpiry.toSeconds - TimestampSecond.now())
173173
receiveSwapMessage[AwaitFeePaymentMessages](context, "awaitFeePayment") {
@@ -189,7 +189,7 @@ private class SwapMaker(remoteNodeId: PublicKey, shortChannelId: ShortChannelId,
189189
}
190190

191191
private def awaitAgreement(request: SwapInRequest): Behavior[SwapCommand] = {
192-
send(switchboard, remoteNodeId)(request)
192+
send(remotePeer)(request)
193193
receiveSwapMessage[AwaitAgreementMessages](context, "awaitAgreement") {
194194
case SwapMessageReceived(agreement: SwapInAgreement) if agreement.protocolVersion != protocolVersion =>
195195
swapCanceled(WrongVersion(request.swapId, protocolVersion))
@@ -242,7 +242,7 @@ private class SwapMaker(remoteNodeId: PublicKey, shortChannelId: ShortChannelId,
242242
swapCompleted(ClaimByInvoicePaid(request.swapId))
243243
case _ =>
244244
watchForPaymentReceived(watch = true)
245-
send(switchboard, remoteNodeId)(openingTxBroadcasted)
245+
send(remotePeer)(openingTxBroadcasted)
246246
Behaviors.withTimers { timers =>
247247
timers.startSingleTimer(swapInvoiceExpiredTimer(request.swapId), InvoiceExpired, invoice.createdAt + invoice.relativeExpiry.toSeconds - TimestampSecond.now())
248248
receiveSwapMessage[AwaitClaimPaymentMessages](context, "awaitClaimPayment") {
@@ -342,7 +342,7 @@ private class SwapMaker(remoteNodeId: PublicKey, shortChannelId: ShortChannelId,
342342
private def swapCanceled(failure: Fail): Behavior[SwapCommand] = {
343343
context.system.eventStream ! Publish(Canceled(failure.swapId, failure.toString))
344344
context.log.error(s"canceled swap: $failure")
345-
if (!failure.isInstanceOf[PeerCanceled]) send(switchboard, remoteNodeId)(CancelSwap(failure.swapId, failure.toString))
345+
if (!failure.isInstanceOf[PeerCanceled]) send(remotePeer)(CancelSwap(failure.swapId, failure.toString))
346346
Behaviors.stopped
347347
}
348348

0 commit comments

Comments
 (0)