-
Notifications
You must be signed in to change notification settings - Fork 266
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate internal channel config from features
Our current ChannelVersion field mixes two unrelated concepts: channel features (as defined in Bolt 9) and channel internals (such as custom key derivation). It is more future-proof to separate these two unrelated concepts and will make it easier to implement channel types (see lightning/bolts#880).
- Loading branch information
Showing
16 changed files
with
461 additions
and
220 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 33 additions & 33 deletions
66
eclair-core/src/main/scala/fr/acinq/eclair/channel/Channel.scala
Large diffs are not rendered by default.
Oops, something went wrong.
77 changes: 77 additions & 0 deletions
77
eclair-core/src/main/scala/fr/acinq/eclair/channel/ChannelConfigOptions.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright 2021 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. | ||
*/ | ||
|
||
package fr.acinq.eclair.channel | ||
|
||
import scodec.bits.{BitVector, ByteVector} | ||
|
||
/** | ||
* Created by t-bast on 24/06/2021. | ||
*/ | ||
|
||
/** | ||
* Internal configuration option impacting the channel's structure or behavior. | ||
* This must be set when creating the channel and cannot be changed afterwards. | ||
*/ | ||
trait ChannelConfigOption { | ||
def supportBit: Int | ||
} | ||
|
||
case class ChannelConfigOptions(activated: Set[ChannelConfigOption]) { | ||
|
||
def hasOption(option: ChannelConfigOption): Boolean = activated.contains(option) | ||
|
||
def bytes: ByteVector = toByteVector | ||
|
||
def toByteVector: ByteVector = { | ||
val indices = activated.map(_.supportBit) | ||
if (indices.isEmpty) { | ||
ByteVector.empty | ||
} else { | ||
// NB: when converting from BitVector to ByteVector, scodec pads right instead of left, so we make sure we pad to bytes *before* setting bits. | ||
var buffer = BitVector.fill(indices.max + 1)(high = false).bytes.bits | ||
indices.foreach(i => buffer = buffer.set(i)) | ||
buffer.reverse.bytes | ||
} | ||
} | ||
|
||
} | ||
|
||
object ChannelConfigOptions { | ||
|
||
def standard: ChannelConfigOptions = ChannelConfigOptions(activated = Set(FundingPubKeyBasedChannelKeyPath)) | ||
|
||
def apply(options: ChannelConfigOption*): ChannelConfigOptions = ChannelConfigOptions(Set.from(options)) | ||
|
||
def apply(bytes: ByteVector): ChannelConfigOptions = { | ||
val activated: Set[ChannelConfigOption] = bytes.bits.toIndexedSeq.reverse.zipWithIndex.collect { | ||
case (true, 0) => FundingPubKeyBasedChannelKeyPath | ||
}.toSet | ||
ChannelConfigOptions(activated) | ||
} | ||
|
||
/** | ||
* If set, the channel's BIP32 key path will be deterministically derived from the funding public key. | ||
* It makes it very easy to retrieve funds when channel data has been lost: | ||
* - connect to your peer and use option_data_loss_protect to get them to publish their remote commit tx | ||
* - retrieve the commit tx from the bitcoin network, extract your funding pubkey from its witness data | ||
* - recompute your channel keys and spend your output | ||
*/ | ||
case object FundingPubKeyBasedChannelKeyPath extends ChannelConfigOption { | ||
override def supportBit = 0 | ||
} | ||
|
||
} |
69 changes: 69 additions & 0 deletions
69
eclair-core/src/main/scala/fr/acinq/eclair/channel/ChannelType.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright 2021 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. | ||
*/ | ||
|
||
package fr.acinq.eclair.channel | ||
|
||
import fr.acinq.eclair.FeatureSupport.Optional | ||
import fr.acinq.eclair.Features | ||
import fr.acinq.eclair.Features.{AnchorOutputs, StaticRemoteKey} | ||
import fr.acinq.eclair.transactions.Transactions.{AnchorOutputsCommitmentFormat, CommitmentFormat, DefaultCommitmentFormat} | ||
|
||
/** | ||
* Created by t-bast on 24/06/2021. | ||
*/ | ||
|
||
/** | ||
* A channel type is a specific combination of Bolt 9 features, defined in the RFC (Bolt 2). | ||
*/ | ||
case class ChannelType(features: Features) { | ||
|
||
val commitmentFormat: CommitmentFormat = { | ||
if (features.hasFeature(AnchorOutputs)) { | ||
AnchorOutputsCommitmentFormat | ||
} else { | ||
DefaultCommitmentFormat | ||
} | ||
} | ||
|
||
/** | ||
* True if our main output in the remote commitment is directly sent (without any delay) to one of our wallet addresses. | ||
*/ | ||
def paysDirectlyToWallet: Boolean = { | ||
features.hasFeature(Features.StaticRemoteKey) && !features.hasFeature(Features.AnchorOutputs) | ||
} | ||
|
||
} | ||
|
||
object ChannelTypes { | ||
|
||
val standard = ChannelType(Features.empty) | ||
val staticRemoteKey = ChannelType(Features(StaticRemoteKey -> Optional)) | ||
val anchorOutputs = ChannelType(Features(StaticRemoteKey -> Optional, AnchorOutputs -> Optional)) | ||
|
||
/** | ||
* Pick the channel type that should be applied based on features alone (in case our peer doesn't support explicit channel type negotiation). | ||
*/ | ||
def pickChannelType(localFeatures: Features, remoteFeatures: Features): ChannelType = { | ||
if (Features.canUseFeature(localFeatures, remoteFeatures, AnchorOutputs)) { | ||
anchorOutputs | ||
} else if (Features.canUseFeature(localFeatures, remoteFeatures, StaticRemoteKey)) { | ||
staticRemoteKey | ||
} else { | ||
standard | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.