Skip to content

Commit db70aa6

Browse files
committed
Use official splice messages
We replace our experimental version of `splice_init`, `splice_ack` and `splice_locked` by their official version. If our peer is using the experimental feature bit, we convert our outgoing messages to use the experimental encoding and incoming messages to the official messages. We also change the TLV fields added to `tx_add_input`, `tx_signatures` and `splice_locked` to match the spec version. We always write both the official and experimental TLV to updated nodes (because the experimental one is odd and will be ignored) but we drop the official TLV if our peer is using the experimental feature, because it won't understand the even TLV field. We do the same thing for the `commit_sig` TLV. For peers who support the official splicing version, we insert the `start_batch` message before the batch of `commit_sig` messages. This guarantees backwards-compatibility with peers who only support the experimental feature.
1 parent 71fb4fa commit db70aa6

File tree

24 files changed

+464
-99
lines changed

24 files changed

+464
-99
lines changed

docs/release-notes/eclair-vnext.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,39 @@
66

77
<insert changes>
88

9+
### Channel Splicing
10+
11+
With this release, we add support for the final version of [splicing](https://github.com/lightning/bolts/pull/1160) that was recently added to the BOLTs.
12+
Splicing allows node operators to change the size of their existing channels, which makes it easier and more efficient to allocate liquidity where it is most needed.
13+
Most node operators can now have a single channel with each of their peer, which costs less on-chain fees and resources, and makes path-finding easier.
14+
15+
The size of an existing channel can be increased with the `splicein` API:
16+
17+
```sh
18+
eclair-cli splicein --channelId=<channel_id> --amountIn=<amount_satoshis>
19+
```
20+
21+
Once that transaction confirms, the additional liquidity can be used to send outgoing payments.
22+
If the transaction doesn't confirm, the node operator can speed up confirmation with the `rbfsplice` API:
23+
24+
```sh
25+
eclair-cli rbfsplice --channelId=<channel_id> --targetFeerateSatByte=<feerate_satoshis_per_byte> --fundingFeeBudgetSatoshis=<maximum_on_chain_fee_satoshis>
26+
```
27+
28+
If the node operator wants to reduce the size of a channel, or send some of the channel funds to an on-chain address, they can use the `spliceout` API:
29+
30+
```sh
31+
eclair-cli spliceout --channelId=<channel_id> --amountOut=<amount_satoshis> --scriptPubKey=<on_chain_address>
32+
```
33+
34+
That operation can also be RBF-ed with the `rbfsplice` API to speed up confirmation if necessary.
35+
36+
Note that when 0-conf is used for the channel, it is not possible to RBF splice transactions.
37+
Node operators should instead create a new splice transaction (with `splicein` or `spliceout`) to CPFP the previous transaction.
38+
39+
Note that eclair had already introduced support for a splicing prototype in v0.9.0, which helped improve the BOLT proposal.
40+
We're removing support for the previous splicing prototype feature: users that depended on this protocol must upgrade to create official splice transactions.
41+
942
### Package relay
1043

1144
With Bitcoin Core 28.1, eclair starts relying on the `submitpackage` RPC during channel force-close.

eclair-core/src/main/resources/reference.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ eclair {
8585
option_zeroconf = disabled
8686
keysend = disabled
8787
option_simple_close=optional
88+
option_splice = optional
8889
trampoline_payment_prototype = disabled
8990
async_payment_prototype = disabled
9091
on_the_fly_funding = disabled

eclair-core/src/main/scala/fr/acinq/eclair/Features.scala

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,7 @@ object Features {
264264
val mandatory = 28
265265
}
266266

267-
// TODO: this should also extend NodeFeature once the spec is finalized
268-
case object Quiescence extends Feature with InitFeature {
267+
case object Quiescence extends Feature with InitFeature with NodeFeature {
269268
val rfcName = "option_quiesce"
270269
val mandatory = 34
271270
}
@@ -310,6 +309,11 @@ object Features {
310309
val mandatory = 60
311310
}
312311

312+
case object Splicing extends Feature with InitFeature with NodeFeature {
313+
val rfcName = "option_splice"
314+
val mandatory = 62
315+
}
316+
313317
/** This feature bit indicates that the node is a mobile wallet that can be woken up via push notifications. */
314318
case object WakeUpNotificationClient extends Feature with InitFeature {
315319
val rfcName = "wake_up_notification_client"
@@ -333,12 +337,6 @@ object Features {
333337
val mandatory = 152
334338
}
335339

336-
// TODO: @pm47 custom splices implementation for phoenix, to be replaced once splices is spec-ed (currently reserved here: https://github.com/lightning/bolts/issues/605)
337-
case object SplicePrototype extends Feature with InitFeature {
338-
val rfcName = "splice_prototype"
339-
val mandatory = 154
340-
}
341-
342340
/**
343341
* Activate this feature to provide on-the-fly funding to remote nodes, as specified in bLIP 36: https://github.com/lightning/blips/blob/master/blip-0036.md.
344342
* TODO: add NodeFeature once bLIP is merged.
@@ -381,10 +379,10 @@ object Features {
381379
ZeroConf,
382380
KeySend,
383381
SimpleClose,
382+
Splicing,
384383
WakeUpNotificationClient,
385384
TrampolinePaymentPrototype,
386385
AsyncPaymentPrototype,
387-
SplicePrototype,
388386
OnTheFlyFunding,
389387
FundingFeeCredit
390388
)
@@ -401,7 +399,6 @@ object Features {
401399
KeySend -> (VariableLengthOnion :: Nil),
402400
SimpleClose -> (ShutdownAnySegwit :: Nil),
403401
AsyncPaymentPrototype -> (TrampolinePaymentPrototype :: Nil),
404-
OnTheFlyFunding -> (SplicePrototype :: Nil),
405402
FundingFeeCredit -> (OnTheFlyFunding :: Nil)
406403
)
407404

eclair-core/src/main/scala/fr/acinq/eclair/channel/Commitments.scala

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,8 @@ case class Commitment(fundingTxIndex: Long,
641641
Metrics.recordHtlcsInFlight(spec, remoteCommit.spec)
642642

643643
val tlvs = Set(
644-
if (batchSize > 1) Some(CommitSigTlv.BatchTlv(batchSize)) else None
644+
if (batchSize > 1) Some(CommitSigTlv.FundingTx(fundingTxId)) else None,
645+
if (batchSize > 1) Some(CommitSigTlv.ExperimentalBatchTlv(batchSize)) else None,
645646
).flatten[CommitSigTlv]
646647
val commitSig = params.commitmentFormat match {
647648
case _: SegwitV0CommitmentFormat =>
@@ -1023,8 +1024,10 @@ case class Commitments(params: ChannelParams,
10231024
case commitSig: CommitSig => Seq(commitSig)
10241025
}
10251026
val commitKeys = LocalCommitmentKeys(params, channelKeys, localCommitIndex + 1)
1026-
// Signatures are sent in order (most recent first), calling `zip` will drop trailing sigs that are for deactivated/pruned commitments.
1027-
val active1 = active.zip(sigs).map { case (commitment, commit) =>
1027+
val active1 = active.zipWithIndex.map { case (commitment, idx) =>
1028+
// If the funding_txid isn't provided, we assume that signatures are sent in order (most recent first).
1029+
// This matches the behavior of peers who only support the experimental version of splicing.
1030+
val commit = sigs.find(_.fundingTxId_opt.contains(commitment.fundingTxId)).getOrElse(sigs(idx))
10281031
commitment.receiveCommit(params, channelKeys, commitKeys, changes, commit) match {
10291032
case Left(f) => return Left(f)
10301033
case Right(commitment1) => commitment1

eclair-core/src/main/scala/fr/acinq/eclair/channel/fsm/Channel.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,7 @@ class Channel(val nodeParams: NodeParams, val channelKeys: ChannelKeys, val wall
876876
}
877877

878878
case Event(cmd: CMD_SPLICE, d: DATA_NORMAL) =>
879-
if (!d.commitments.params.remoteParams.initFeatures.hasFeature(Features.SplicePrototype)) {
879+
if (!d.commitments.params.remoteParams.initFeatures.hasFeature(Features.Splicing)) {
880880
log.warning("cannot initiate splice, peer doesn't support splicing")
881881
cmd.replyTo ! RES_FAILURE(cmd, CommandUnavailableInThisState(d.channelId, "splice", NORMAL))
882882
stay()
@@ -2300,7 +2300,8 @@ class Channel(val nodeParams: NodeParams, val channelKeys: ChannelKeys, val wall
23002300
}
23012301
case _ => Set.empty
23022302
}
2303-
val lastFundingLockedTlvs: Set[ChannelReestablishTlv] = if (d.commitments.params.remoteParams.initFeatures.hasFeature(Features.SplicePrototype)) {
2303+
val remoteFeatures = d.commitments.params.remoteParams.initFeatures
2304+
val lastFundingLockedTlvs: Set[ChannelReestablishTlv] = if (remoteFeatures.hasFeature(Features.Splicing) || remoteFeatures.unknown.contains(UnknownFeature(154)) || remoteFeatures.unknown.contains(UnknownFeature(155))) {
23042305
d.commitments.lastLocalLocked_opt.map(c => ChannelReestablishTlv.MyCurrentFundingLockedTlv(c.fundingTxId)).toSet ++
23052306
d.commitments.lastRemoteLocked_opt.map(c => ChannelReestablishTlv.YourLastFundingLockedTlv(c.fundingTxId)).toSet
23062307
} else Set.empty
@@ -2429,7 +2430,8 @@ class Channel(val nodeParams: NodeParams, val channelKeys: ChannelKeys, val wall
24292430
// We only send channel_ready for initial funding transactions.
24302431
case Some(c) if c.fundingTxIndex != 0 => ()
24312432
case Some(c) =>
2432-
val remoteSpliceSupport = d.commitments.params.remoteParams.initFeatures.hasFeature(Features.SplicePrototype)
2433+
val remoteFeatures = d.commitments.params.remoteParams.initFeatures
2434+
val remoteSpliceSupport = remoteFeatures.hasFeature(Features.Splicing) || remoteFeatures.unknown.contains(UnknownFeature(154)) || remoteFeatures.unknown.contains(UnknownFeature(155))
24332435
// If our peer has not received our channel_ready, we retransmit it.
24342436
val notReceivedByRemote = remoteSpliceSupport && channelReestablish.yourLastFundingLocked_opt.isEmpty
24352437
// If next_local_commitment_number is 1 in both the channel_reestablish it sent and received, then the node

eclair-core/src/main/scala/fr/acinq/eclair/io/PeerConnection.scala

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import fr.acinq.eclair.remote.EclairInternalsSerializer.RemoteTypes
2828
import fr.acinq.eclair.router.Router._
2929
import fr.acinq.eclair.wire.protocol
3030
import fr.acinq.eclair.wire.protocol._
31-
import fr.acinq.eclair.{FSMDiagnosticActorLogging, Features, InitFeature, Logs, TimestampMilli, TimestampSecond}
31+
import fr.acinq.eclair.{FSMDiagnosticActorLogging, Features, InitFeature, Logs, TimestampMilli, TimestampSecond, UnknownFeature}
3232
import scodec.Attempt
3333
import scodec.bits.ByteVector
3434

@@ -206,9 +206,19 @@ class PeerConnection(keyPair: KeyPair, conf: PeerConnection.Conf, switchboard: A
206206
stay()
207207

208208
case Event(msg: LightningMessage, d: ConnectedData) if sender() != d.transport => // if the message doesn't originate from the transport, it is an outgoing message
209+
val useExperimentalSplice = d.remoteInit.features.unknown.contains(UnknownFeature(154)) || d.remoteInit.features.unknown.contains(UnknownFeature(155))
209210
msg match {
210-
case batch: CommitSigBatch => batch.messages.foreach(msg => d.transport forward msg)
211-
case msg => d.transport forward msg
211+
// If our peer is using the experimental splice version, we convert splice messages.
212+
case msg: SpliceInit if useExperimentalSplice => d.transport forward ExperimentalSpliceInit.from(msg)
213+
case msg: SpliceAck if useExperimentalSplice => d.transport forward ExperimentalSpliceAck.from(msg)
214+
case msg: SpliceLocked if useExperimentalSplice => d.transport forward ExperimentalSpliceLocked.from(msg)
215+
case msg: TxAddInput if useExperimentalSplice => d.transport forward msg.copy(tlvStream = TlvStream(msg.tlvStream.records.filterNot(_.isInstanceOf[TxAddInputTlv.SharedInputTxId])))
216+
case msg: TxSignatures if useExperimentalSplice => d.transport forward msg.copy(tlvStream = TlvStream(msg.tlvStream.records.filterNot(_.isInstanceOf[TxSignaturesTlv.PreviousFundingTxSig])))
217+
case batch: CommitSigBatch if useExperimentalSplice => batch.messages.foreach(msg => d.transport forward msg.copy(tlvStream = TlvStream(msg.tlvStream.records.filterNot(_.isInstanceOf[CommitSigTlv.FundingTx]))))
218+
case batch: CommitSigBatch =>
219+
d.transport forward StartBatch(batch.channelId, batch.batchSize)
220+
batch.messages.foreach(msg => d.transport forward msg.copy(tlvStream = TlvStream(msg.tlvStream.records.filterNot(_.isInstanceOf[CommitSigTlv.ExperimentalBatchTlv]))))
221+
case _ => d.transport forward msg
212222
}
213223
msg match {
214224
// If we send any channel management message to this peer, the connection should be persistent.
@@ -348,8 +358,41 @@ class PeerConnection(keyPair: KeyPair, conf: PeerConnection.Conf, switchboard: A
348358
// We immediately forward messages to the peer, unless they are part of a batch, in which case we wait to
349359
// receive the whole batch before forwarding.
350360
msg match {
361+
case msg: StartBatch =>
362+
log.debug("starting batch of size {} for channel_id={}", msg.batchSize, msg.channelId)
363+
d.commitSigBatch_opt match {
364+
case Some(pending) if pending.received.nonEmpty =>
365+
log.warning("starting batch with incomplete previous batch ({}/{} received)", pending.received.size, pending.batchSize)
366+
// This is a spec violation from our peer: this will likely lead to a force-close.
367+
d.transport ! Warning(msg.channelId, "invalid start_batch message: the previous batch is not done yet")
368+
d.peer ! CommitSigBatch(pending.received)
369+
case _ => ()
370+
}
371+
stay() using d.copy(commitSigBatch_opt = Some(PendingCommitSigBatch(msg.channelId, msg.batchSize, Nil)))
372+
case msg: HasChannelId if d.commitSigBatch_opt.nonEmpty =>
373+
// We only support batches of commit_sig messages: other messages will simply be relayed individually.
374+
val pending = d.commitSigBatch_opt.get
375+
msg match {
376+
case msg: CommitSig if msg.channelId == pending.channelId =>
377+
val received1 = pending.received :+ msg
378+
if (received1.size == pending.batchSize) {
379+
log.debug("received last commit_sig in batch for channel_id={}", msg.channelId)
380+
d.peer ! CommitSigBatch(received1)
381+
stay() using d.copy(commitSigBatch_opt = None)
382+
} else {
383+
log.debug("received commit_sig {}/{} in batch for channel_id={}", received1.size, pending.batchSize, msg.channelId)
384+
stay() using d.copy(commitSigBatch_opt = Some(pending.copy(received = received1)))
385+
}
386+
case _ =>
387+
log.warning("received {} as part of a batch: we don't support batching that kind of messages", msg.getClass.getSimpleName)
388+
if (pending.received.nonEmpty) d.peer ! CommitSigBatch(pending.received)
389+
d.peer ! msg
390+
stay() using d.copy(commitSigBatch_opt = None)
391+
}
351392
case msg: CommitSig =>
352-
msg.tlvStream.get[CommitSigTlv.BatchTlv].map(_.size) match {
393+
// We keep supporting the experimental version of splicing that older Phoenix wallets use.
394+
// Once we're confident that enough Phoenix users have upgraded, we should remove this branch.
395+
msg.tlvStream.get[CommitSigTlv.ExperimentalBatchTlv].map(_.size) match {
353396
case Some(batchSize) if batchSize > 25 =>
354397
log.warning("received legacy batch of commit_sig exceeding our threshold ({} > 25), processing messages individually", batchSize)
355398
// We don't want peers to be able to exhaust our memory by sending batches of dummy messages that we keep in RAM.
@@ -381,6 +424,16 @@ class PeerConnection(keyPair: KeyPair, conf: PeerConnection.Conf, switchboard: A
381424
d.peer ! msg
382425
stay()
383426
}
427+
// If our peer is using the experimental splice version, we convert splice messages.
428+
case msg: ExperimentalSpliceInit =>
429+
d.peer ! msg.toSpliceInit()
430+
stay()
431+
case msg: ExperimentalSpliceAck =>
432+
d.peer ! msg.toSpliceAck()
433+
stay()
434+
case msg: ExperimentalSpliceLocked =>
435+
d.peer ! msg.toSpliceLocked()
436+
stay()
384437
case _ =>
385438
d.peer ! msg
386439
stay()
@@ -613,6 +666,7 @@ object PeerConnection {
613666
gossipTimestampFilter: Option[GossipTimestampFilter] = None,
614667
behavior: Behavior = Behavior(),
615668
expectedPong_opt: Option[ExpectedPong] = None,
669+
commitSigBatch_opt: Option[PendingCommitSigBatch] = None,
616670
legacyCommitSigBatch_opt: Option[PendingCommitSigBatch] = None,
617671
isPersistent: Boolean) extends Data with HasTransport
618672

eclair-core/src/main/scala/fr/acinq/eclair/wire/protocol/ChannelTlv.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,3 +301,9 @@ object ClosingTlv {
301301
)
302302

303303
}
304+
305+
sealed trait StartBatchTlv extends Tlv
306+
307+
object StartBatchTlv {
308+
val startBatchTlvCodec: Codec[TlvStream[StartBatchTlv]] = tlvStream(discriminated[StartBatchTlv].by(varint))
309+
}

eclair-core/src/main/scala/fr/acinq/eclair/wire/protocol/HtlcTlv.scala

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package fr.acinq.eclair.wire.protocol
1818

1919
import fr.acinq.bitcoin.scalacompat.Crypto.PublicKey
20+
import fr.acinq.bitcoin.scalacompat.TxId
2021
import fr.acinq.eclair.UInt64
2122
import fr.acinq.eclair.wire.protocol.CommonCodecs._
2223
import fr.acinq.eclair.wire.protocol.TlvCodecs.{tlvField, tlvStream, tu16}
@@ -73,16 +74,27 @@ object UpdateFailMalformedHtlcTlv {
7374
sealed trait CommitSigTlv extends Tlv
7475

7576
object CommitSigTlv {
77+
/**
78+
* While a splice is ongoing and not locked, we have multiple valid commitments.
79+
* We send one [[CommitSig]] message for each valid commitment.
80+
*
81+
* @param txId the funding transaction spent by this commitment.
82+
*/
83+
case class FundingTx(txId: TxId) extends CommitSigTlv
7684

77-
/** @param size the number of [[CommitSig]] messages in the batch */
78-
case class BatchTlv(size: Int) extends CommitSigTlv
85+
private val fundingTxTlv: Codec[FundingTx] = tlvField(txIdAsHash)
7986

80-
object BatchTlv {
81-
val codec: Codec[BatchTlv] = tlvField(tu16)
82-
}
87+
/**
88+
* The experimental version of splicing included the number of [[CommitSig]] messages in the batch.
89+
* This TLV can be removed once Phoenix users have upgraded to the official version of splicing.
90+
*/
91+
case class ExperimentalBatchTlv(size: Int) extends CommitSigTlv
92+
93+
private val experimentalBatchTlv: Codec[ExperimentalBatchTlv] = tlvField(tu16)
8394

8495
val commitSigTlvCodec: Codec[TlvStream[CommitSigTlv]] = tlvStream(discriminated[CommitSigTlv].by(varint)
85-
.typecase(UInt64(0x47010005), BatchTlv.codec)
96+
.typecase(UInt64(0), fundingTxTlv)
97+
.typecase(UInt64(0x47010005), experimentalBatchTlv)
8698
)
8799

88100
}

eclair-core/src/main/scala/fr/acinq/eclair/wire/protocol/InteractiveTxTlv.scala

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,13 @@ object TxAddInputTlv {
3333
/** When doing a splice, the initiator must provide the previous funding txId instead of the whole transaction. */
3434
case class SharedInputTxId(txId: TxId) extends TxAddInputTlv
3535

36+
/** Same as [[SharedInputTxId]] for peers who only support the experimental version of splicing. */
37+
case class ExperimentalSharedInputTxId(txId: TxId) extends TxAddInputTlv
38+
3639
val txAddInputTlvCodec: Codec[TlvStream[TxAddInputTlv]] = tlvStream(discriminated[TxAddInputTlv].by(varint)
3740
// Note that we actually encode as a tx_hash to be consistent with other lightning messages.
38-
.typecase(UInt64(1105), tlvField(txIdAsHash.as[SharedInputTxId]))
41+
.typecase(UInt64(0), tlvField(txIdAsHash.as[SharedInputTxId]))
42+
.typecase(UInt64(1105), tlvField(txIdAsHash.as[ExperimentalSharedInputTxId]))
3943
)
4044
}
4145

@@ -69,8 +73,12 @@ object TxSignaturesTlv {
6973
/** When doing a splice, each peer must provide their signature for the previous 2-of-2 funding output. */
7074
case class PreviousFundingTxSig(sig: ByteVector64) extends TxSignaturesTlv
7175

76+
/** Same as [[PreviousFundingTxSig]] for peers who only support the experimental version of splicing. */
77+
case class ExperimentalPreviousFundingTxSig(sig: ByteVector64) extends TxSignaturesTlv
78+
7279
val txSignaturesTlvCodec: Codec[TlvStream[TxSignaturesTlv]] = tlvStream(discriminated[TxSignaturesTlv].by(varint)
73-
.typecase(UInt64(601), tlvField(bytes64.as[PreviousFundingTxSig]))
80+
.typecase(UInt64(0), tlvField(bytes64.as[PreviousFundingTxSig]))
81+
.typecase(UInt64(601), tlvField(bytes64.as[ExperimentalPreviousFundingTxSig]))
7482
)
7583
}
7684

0 commit comments

Comments
 (0)