Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Peerswap atomic swap based local liquidity management protocol #2342

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
fd714ea
Fix timeouts caused by a slow machine
remyers Jul 7, 2022
7678204
Add serialization for PeerSwap messages
remyers Mar 31, 2022
ef88c3e
Add on-chain transactions for peerswap
remyers Jun 21, 2022
584ef48
Add shared peerswap helper functions and data structures
remyers Jul 1, 2022
ed0b0f8
Add new SwapKeyManager for peerswap swaps
remyers Jun 21, 2022
559a2da
Add SwapInSender peerswap workflow actor
remyers Jun 21, 2022
2e17561
Add SwapInReceiver peerswap workflow actor
remyers Jul 1, 2022
d7e6c5b
Add SwapRegister to forward peerswap messages to swap actors
remyers Jun 21, 2022
522fd3a
Update Peer actor to forward peerswap messages from peers to the Swap…
remyers Jun 21, 2022
a3dae29
Update Channel actor to forward peerswap messages to Peer
remyers Jul 7, 2022
df69b97
Add SwapInSend/Receive integration test
remyers Jul 7, 2022
dc6ad3c
add cli/api for peerswap swapin, status and cancel
remyers Jul 15, 2022
1ef5a99
Fix errors found during integration testing
remyers Sep 7, 2022
d8c41d2
Add swap-out support and basic tests
remyers Sep 14, 2022
c07b552
Change Register to use simple swapId String for key
remyers Sep 16, 2022
00032d6
Rename actors to SwapMaker and SwapTaker
remyers Sep 16, 2022
1270c07
Add swaps db and update swap actors to use it
remyers Sep 19, 2022
32c2a96
Modify to work with PeerSwap as a plugin
remyers Oct 20, 2022
eb91acb
Move Peerswap functionality into a plugin
remyers Oct 20, 2022
d3118f4
Create random seed file if seed file not found
remyers Oct 26, 2022
c34a722
Clean up handling/responses for 'listswaps' and 'cancelswap'
remyers Oct 26, 2022
4da5074
Add some TODOs to discuss later
remyers Oct 26, 2022
805e602
Cleanup register and prevent multiple swaps per channel
remyers Oct 26, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion eclair-core/src/main/scala/fr/acinq/eclair/Setup.scala
Original file line number Diff line number Diff line change
Expand Up @@ -303,12 +303,12 @@ class Setup(val datadir: File,

txPublisherFactory = Channel.SimpleTxPublisherFactory(nodeParams, watcher, bitcoinClient)
channelFactory = Peer.SimpleChannelFactory(nodeParams, watcher, relayer, bitcoinClient, txPublisherFactory)
paymentInitiator = system.actorOf(SimpleSupervisor.props(PaymentInitiator.props(nodeParams, PaymentInitiator.SimplePaymentFactory(nodeParams, router, register)), "payment-initiator", SupervisorStrategy.Restart))
peerFactory = Switchboard.SimplePeerFactory(nodeParams, bitcoinClient, channelFactory)

switchboard = system.actorOf(SimpleSupervisor.props(Switchboard.props(nodeParams, peerFactory), "switchboard", SupervisorStrategy.Resume))
clientSpawner = system.actorOf(SimpleSupervisor.props(ClientSpawner.props(nodeParams.keyPair, nodeParams.socksProxy_opt, nodeParams.peerConnectionConf, switchboard, router), "client-spawner", SupervisorStrategy.Restart))
server = system.actorOf(SimpleSupervisor.props(Server.props(nodeParams.keyPair, nodeParams.peerConnectionConf, switchboard, router, serverBindingAddress, Some(tcpBound)), "server", SupervisorStrategy.Restart))
paymentInitiator = system.actorOf(SimpleSupervisor.props(PaymentInitiator.props(nodeParams, PaymentInitiator.SimplePaymentFactory(nodeParams, router, register)), "payment-initiator", SupervisorStrategy.Restart))
_ = for (i <- 0 until config.getInt("autoprobe-count")) yield system.actorOf(SimpleSupervisor.props(Autoprobe.props(nodeParams, router, paymentInitiator), s"payment-autoprobe-$i", SupervisorStrategy.Restart))

balanceActor = system.spawn(BalanceActor(nodeParams.db, bitcoinClient, channelsListener, nodeParams.balanceCheckInterval), name = "balance-actor")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1641,6 +1641,11 @@ class Channel(val nodeParams: NodeParams, val wallet: OnChainChannelFunder, val
case Event(WatchFundingSpentTriggered(tx), d: PersistentChannelData) if tx.txid == d.commitments.localCommit.commitTxAndRemoteSig.commitTx.tx.txid =>
log.warning(s"processing local commit spent in catch-all handler")
spendLocalCurrent(d)

// forward unknown messages that originate from loaded plugins
case Event(unknownMsg: UnknownMessage, _) if nodeParams.pluginMessageTags.contains(unknownMsg.tag) =>
send(unknownMsg)
stay()
}

onTransition {
Expand Down
6 changes: 6 additions & 0 deletions eclair-core/src/main/scala/fr/acinq/eclair/io/Peer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ class Peer(val nodeParams: NodeParams, remoteNodeId: PublicKey, wallet: OnChainA
replyTo_opt.foreach(_ ! MessageRelay.Sent(messageId))
stay()

// TODO: plugin actors should register to receive messages with certain tags
case Event(unknownMsg: UnknownMessage, d: ConnectedData) if nodeParams.pluginMessageTags.contains(unknownMsg.tag) =>
context.system.eventStream.publish(UnknownMessageReceived(self, remoteNodeId, unknownMsg, d.connectionInfo))
stay()
Expand Down Expand Up @@ -403,6 +404,11 @@ class Peer(val nodeParams: NodeParams, remoteNodeId: PublicKey, wallet: OnChainA
self ! Peer.OutgoingMessage(msg, peerConnection)
}

def replyUnknownSwap(peerConnection: ActorRef, unknownSwapId: String): Unit = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this method does not seem to be used anywhere yet ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

val msg = Warning(s"unknown swap id $unknownSwapId")
self ! Peer.OutgoingMessage(msg, peerConnection)
}

def handleOpenChannel(open: Either[protocol.OpenChannel, protocol.OpenDualFundedChannel], temporaryChannelId: ByteVector32, fundingAmount: Satoshi, channelFlags: ChannelFlags, channelType_opt: Option[ChannelType], d: ConnectedData): Unit = {
validateRemoteChannelType(temporaryChannelId, channelFlags, channelType_opt, d.localFeatures, d.remoteFeatures) match {
case Right(channelType) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import fr.acinq.eclair.io.Peer.PeerInfoResponse
import fr.acinq.eclair.remote.EclairInternalsSerializer.RemoteTypes
import fr.acinq.eclair.router.Router.RouterConf
import fr.acinq.eclair.wire.protocol.OnionMessage
import fr.acinq.eclair.{SubscriptionsComplete, NodeParams}
import fr.acinq.eclair.{NodeParams, SubscriptionsComplete}

/**
* Ties network connections to peers.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

package fr.acinq.eclair.transactions

import fr.acinq.bitcoin.ScriptFlags
import fr.acinq.bitcoin.SigHash._
import fr.acinq.bitcoin.SigVersion._
import fr.acinq.bitcoin.scalacompat.Crypto.{PrivateKey, PublicKey, ripemd160}
import fr.acinq.bitcoin.scalacompat.Script._
import fr.acinq.bitcoin.scalacompat._
import fr.acinq.bitcoin.SigHash._
import fr.acinq.bitcoin.SigVersion._
import fr.acinq.bitcoin.ScriptFlags
import fr.acinq.eclair._
import fr.acinq.eclair.blockchain.fee.FeeratePerKw
import fr.acinq.eclair.transactions.CommitmentOutput._
Expand Down Expand Up @@ -100,7 +100,7 @@ object Transactions {
case object Remote extends TxOwner
}

sealed trait TransactionWithInputInfo {
trait TransactionWithInputInfo {
def input: InputInfo
def desc: String
def tx: Transaction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@ object LightningMessageCodecs {
.typecase(264, replyChannelRangeCodec)
.typecase(265, gossipTimestampFilterCodec)
.typecase(513, onionMessageCodec)

// NB: blank lines to minimize merge conflicts

//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import fr.acinq.eclair.transactions.Transactions
import fr.acinq.eclair.{randomBytes32, randomKey}
import scodec.bits._

import scala.collection.concurrent.TrieMap
import scala.concurrent.{ExecutionContext, Future, Promise}

/**
Expand All @@ -35,10 +36,12 @@ class DummyOnChainWallet extends OnChainWallet {

import DummyOnChainWallet._

val funded = collection.concurrent.TrieMap.empty[ByteVector32, Transaction]
var confirmedBalance: Satoshi = 1105 sat
var unconfirmedBalance: Satoshi = 561 sat
val funded: TrieMap[ByteVector32, Transaction] = collection.concurrent.TrieMap.empty[ByteVector32, Transaction]
var rolledback = Set.empty[Transaction]

override def onChainBalance()(implicit ec: ExecutionContext): Future[OnChainBalance] = Future.successful(OnChainBalance(1105 sat, 561 sat))
override def onChainBalance()(implicit ec: ExecutionContext): Future[OnChainBalance] = Future.successful(OnChainBalance(confirmedBalance, unconfirmedBalance))

override def getReceiveAddress(label: String)(implicit ec: ExecutionContext): Future[String] = Future.successful(dummyReceiveAddress)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package fr.acinq.eclair.crypto.keymanager

import java.io.File
import java.nio.file.Files
import fr.acinq.bitcoin.scalacompat.Crypto.{PrivateKey, PublicKey}
import fr.acinq.bitcoin.scalacompat.DeterministicWallet.KeyPath
import fr.acinq.bitcoin.scalacompat.{Block, ByteVector32, DeterministicWallet}
Expand All @@ -28,6 +26,9 @@ import fr.acinq.eclair.{NodeParams, TestConstants, TestUtils}
import org.scalatest.funsuite.AnyFunSuite
import scodec.bits._

import java.io.File
import java.nio.file.Files


class LocalChannelKeyManagerSpec extends AnyFunSuite {
test("generate the same secrets from the same seed") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@

package fr.acinq.eclair.crypto.keymanager

import java.io.File
import java.nio.file.Files

import fr.acinq.bitcoin.scalacompat.Crypto.PublicKey
import fr.acinq.bitcoin.scalacompat.DeterministicWallet.KeyPath
import fr.acinq.bitcoin.scalacompat.{Block, ByteVector32, Crypto}
Expand All @@ -27,6 +24,9 @@ import fr.acinq.eclair.{NodeParams, TestUtils}
import org.scalatest.funsuite.AnyFunSuite
import scodec.bits._

import java.io.File
import java.nio.file.Files


class LocalNodeKeyManagerSpec extends AnyFunSuite {
test("generate the same node id from the same seed") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import fr.acinq.eclair.payment.send.PaymentInitiator
import fr.acinq.eclair.router.Router
import fr.acinq.eclair.wire.protocol.IPAddress
import fr.acinq.eclair.{BlockHeight, MilliSatoshi, NodeParams, RealShortChannelId, SubscriptionsComplete, TestBitcoinCoreClient, TestDatabases, TestFeeEstimator}
import org.scalatest.concurrent.{Eventually, IntegrationPatience}
import org.scalatest.concurrent.{Eventually, IntegrationPatience, PatienceConfiguration}
import org.scalatest.{Assertions, EitherValues}

import java.net.InetAddress
Expand Down Expand Up @@ -84,10 +84,10 @@ object MinimalNodeFixture extends Assertions with Eventually with IntegrationPat
val relayer = system.actorOf(Relayer.props(nodeParams, router, register, paymentHandler), "relayer")
val txPublisherFactory = Channel.SimpleTxPublisherFactory(nodeParams, watcherTyped, bitcoinClient)
val channelFactory = Peer.SimpleChannelFactory(nodeParams, watcherTyped, relayer, wallet, txPublisherFactory)
val peerFactory = Switchboard.SimplePeerFactory(nodeParams, wallet, channelFactory)
val switchboard = system.actorOf(Switchboard.props(nodeParams, peerFactory), "switchboard")
val paymentFactory = PaymentInitiator.SimplePaymentFactory(nodeParams, router, register)
val paymentInitiator = system.actorOf(PaymentInitiator.props(nodeParams, paymentFactory), "payment-initiator")
val peerFactory = Switchboard.SimplePeerFactory(nodeParams, wallet, channelFactory)
val switchboard = system.actorOf(Switchboard.props(nodeParams, peerFactory), "switchboard")
readyListener.expectMsgAllOf(
SubscriptionsComplete(classOf[Router]),
SubscriptionsComplete(classOf[Register]),
Expand Down Expand Up @@ -180,7 +180,7 @@ object MinimalNodeFixture extends Assertions with Eventually with IntegrationPat
watch1.replyTo ! WatchFundingConfirmedTriggered(blockHeight, txIndex, fundingTx)
watch2.replyTo ! WatchFundingConfirmedTriggered(blockHeight, txIndex, fundingTx)

eventually {
eventually(PatienceConfiguration.Timeout(2 seconds), PatienceConfiguration.Interval(1 second)) {
assert(getChannelState(node1, channelId) == NORMAL)
assert(getChannelState(node2, channelId) == NORMAL)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ package fr.acinq.eclair.api

import akka.actor.ActorSystem
import akka.http.scaladsl.server._
import fr.acinq.eclair.{Eclair, RouteProvider}
import fr.acinq.eclair.api.directives.EclairDirectives
import fr.acinq.eclair.api.handlers._
import fr.acinq.eclair.{Eclair, RouteProvider}
import grizzled.slf4j.Logging

trait Service extends EclairDirectives with WebSocket with Node with Channel with Fees with PathFinding with Invoice with Payment with Message with OnChain with Logging {
Expand Down
28 changes: 28 additions & 0 deletions plugins/peerswap/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Peerswap plugin

This plugin allows implements the PeerSwap protocol: https://github.com/ElementsProject/peerswap-spec/blob/main/peer-protocol.md

## Build

To build this plugin, run the following command in this directory:

```sh
mvn package
```

## Run

To run eclair with this plugin, start eclair with the following command:

```sh
eclair-node-<version>/bin/eclair-node.sh <path-to-plugin-jar>/peerswap-plugin-<version>.jar
```

## Commands

```sh
eclair-cli swapin --shortChannelId=<short-channel-id>> --amountSat=<amount>
eclair-cli swapout --shortChannelId=<short-channel-id>> --amountSat=<amount>
eclair-cli listswaps
eclair-cli cancelswap --swapId=<swap-id>
```
163 changes: 163 additions & 0 deletions plugins/peerswap/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2022 ACINQ SAS
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>fr.acinq.eclair</groupId>
<artifactId>eclair_2.13</artifactId>
<version>0.7.1-SNAPSHOT</version>
</parent>

<artifactId>peerswap-plugin_2.13</artifactId>
<packaging>jar</packaging>
<name>peerswap-plugin</name>

<build>
<plugins>
<plugin>
<groupId>com.googlecode.maven-download-plugin</groupId>
<artifactId>download-maven-plugin</artifactId>
<version>1.3.0</version>
<executions>
<execution>
<id>download-bitcoind</id>
<phase>generate-test-resources</phase>
<goals>
<goal>wget</goal>
</goals>
<configuration>
<skip>${maven.test.skip}</skip>
<url>${bitcoind.url}</url>
<unpack>true</unpack>
<outputDirectory>${project.build.directory}</outputDirectory>
<md5>${bitcoind.md5}</md5>
<sha1>${bitcoind.sha1}</sha1>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<transformers>
<!-- Add a manifest entry for Main-Class with the FQDN of the implementation. -->
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>fr.acinq.eclair.plugins.peerswap.PeerSwapPlugin</Main-Class>
</manifestEntries>
</transformer>
</transformers>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<bitcoind.url>https://bitcoincore.org/bin/bitcoin-core-0.21.1/bitcoin-0.21.1-x86_64-linux-gnu.tar.gz</bitcoind.url>
<bitcoind.md5>e283a98b5e9f0b58e625e1dde661201d</bitcoind.md5>
<bitcoind.sha1>5101e29b39c33cc8e40d5f3b46dda37991b037a0</bitcoind.sha1>
</properties>
</profile>
<profile>
<id>Mac</id>
<activation>
<os>
<family>mac</family>
</os>
</activation>
<properties>
<bitcoind.url>https://bitcoincore.org/bin/bitcoin-core-0.21.1/bitcoin-0.21.1-osx64.tar.gz</bitcoind.url>
<bitcoind.md5>dfd1f323678eede14ae2cf6afb26ff6a</bitcoind.md5>
<bitcoind.sha1>4273696f90a2648f90142438221f5d1ade16afa2</bitcoind.sha1>
</properties>
</profile>
<profile>
<id>Windows</id>
<activation>
<os>
<family>Windows</family>
</os>
</activation>
<properties>
<bitcoind.url>https://bitcoincore.org/bin/bitcoin-core-0.21.1/bitcoin-0.21.1-win64.zip</bitcoind.url>
<bitcoind.md5>1c6f5081ea68dcec7eddb9e6cdfc508d</bitcoind.md5>
<bitcoind.sha1>a782cd413fc736f05fad3831d6a9f59dde779520</bitcoind.sha1>
</properties>
</profile>
</profiles>

<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>fr.acinq.eclair</groupId>
<artifactId>eclair-core_${scala.version.short}</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>fr.acinq.eclair</groupId>
<artifactId>eclair-node_${scala.version.short}</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<!-- TESTS -->
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-testkit_${scala.version.short}</artifactId>
<version>${akka.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-actor-testkit-typed_${scala.version.short}</artifactId>
<version>${akka.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>fr.acinq.eclair</groupId>
<artifactId>eclair-core_${scala.version.short}</artifactId>
<version>${project.version}</version>
<classifier>tests</classifier>
<type>test-jar</type>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Loading