Skip to content

Commit

Permalink
store local/remote aliases in channel data
Browse files Browse the repository at this point in the history
  • Loading branch information
pm47 committed May 3, 2022
1 parent 21997f0 commit 32543a3
Show file tree
Hide file tree
Showing 23 changed files with 184 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ object ShortChannelId {
def outputIndex(shortChannelId: ShortChannelId): Int = (shortChannelId.id & 0xFFFF).toInt

def coordinates(shortChannelId: ShortChannelId): TxCoordinates = TxCoordinates(blockHeight(shortChannelId), txIndex(shortChannelId), outputIndex(shortChannelId))

def generateLocalAlias(): ShortChannelId = ShortChannelId(System.nanoTime()) // TODO: fixme (duplicate, etc.)
}

case class TxCoordinates(blockHeight: BlockHeight, txIndex: Int, outputIndex: Int)
Original file line number Diff line number Diff line change
Expand Up @@ -426,15 +426,22 @@ final case class DATA_WAIT_FOR_FUNDING_CONFIRMED(commitments: Commitments,
waitingSince: BlockHeight, // how long have we been waiting for the funding tx to confirm
deferred: Option[ChannelReady],
lastSent: Either[FundingCreated, FundingSigned]) extends PersistentChannelData
final case class DATA_WAIT_FOR_CHANNEL_READY(commitments: Commitments, shortChannelId: ShortChannelId, lastSent: ChannelReady) extends PersistentChannelData
final case class DATA_WAIT_FOR_CHANNEL_READY(commitments: Commitments,
shortChannelId_opt: Option[ShortChannelId],
localAlias: ShortChannelId,
lastSent: ChannelReady) extends PersistentChannelData
final case class DATA_NORMAL(commitments: Commitments,
shortChannelId: ShortChannelId,
shortChannelId_opt: Option[ShortChannelId],
buried: Boolean,
channelAnnouncement: Option[ChannelAnnouncement],
channelUpdate: ChannelUpdate,
localAliases: List[ShortChannelId],
remoteAlias_opt: Option[ShortChannelId],
localShutdown: Option[Shutdown],
remoteShutdown: Option[Shutdown],
closingFeerates: Option[ClosingFeerates]) extends PersistentChannelData
closingFeerates: Option[ClosingFeerates]) extends PersistentChannelData {
require(localAliases.nonEmpty, "there must be at least one local alias")
}
final case class DATA_SHUTDOWN(commitments: Commitments, localShutdown: Shutdown, remoteShutdown: Shutdown, closingFeerates: Option[ClosingFeerates]) extends PersistentChannelData
final case class DATA_NEGOTIATING(commitments: Commitments,
localShutdown: Shutdown, remoteShutdown: Shutdown,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,15 @@ case class ChannelRestored(channel: ActorRef, channelId: ByteVector32, peer: Act

case class ChannelIdAssigned(channel: ActorRef, remoteNodeId: PublicKey, temporaryChannelId: ByteVector32, channelId: ByteVector32) extends ChannelEvent

case class ShortChannelIdAssigned(channel: ActorRef, channelId: ByteVector32, shortChannelId: ShortChannelId, previousShortChannelId: Option[ShortChannelId]) extends ChannelEvent
/**
* @param shortChannelId_opt the real scid, but it can change in case of a reorg before the channel reaches 6 conf
* @param localAliases we only remember the last alias sent by our peer, we must remember all of them because
* incoming payments to our peer may use any previous alias that we generated in the past
* @param remoteAlias_opt we only remember the last alias sent by our peer, we use this to:
* - resolve incoming [[ChannelUpdate]]
* - generate routing hints in [[fr.acinq.eclair.payment.Bolt11Invoice]]
*/
case class ShortChannelIdAssigned(channel: ActorRef, channelId: ByteVector32, shortChannelId_opt: Option[ShortChannelId], localAliases: Seq[ShortChannelId], remoteAlias_opt: Option[ShortChannelId]) extends ChannelEvent

case class LocalChannelUpdate(channel: ActorRef, channelId: ByteVector32, shortChannelId: ShortChannelId, remoteNodeId: PublicKey, channelAnnouncement_opt: Option[ChannelAnnouncement], channelUpdate: ChannelUpdate, commitments: AbstractCommitments) extends ChannelEvent

Expand All @@ -66,7 +74,7 @@ case class TransactionPublished(channelId: ByteVector32, remoteNodeId: PublicKey
case class TransactionConfirmed(channelId: ByteVector32, remoteNodeId: PublicKey, tx: Transaction) extends ChannelEvent

// NB: this event is only sent when the channel is available.
case class AvailableBalanceChanged(channel: ActorRef, channelId: ByteVector32, shortChannelId: ShortChannelId, commitments: AbstractCommitments) extends ChannelEvent
case class AvailableBalanceChanged(channel: ActorRef, channelId: ByteVector32, localAlias: ShortChannelId, commitments: AbstractCommitments) extends ChannelEvent

case class ChannelPersisted(channel: ActorRef, remoteNodeId: PublicKey, channelId: ByteVector32, data: PersistentChannelData) extends ChannelEvent

Expand Down
11 changes: 11 additions & 0 deletions eclair-core/src/main/scala/fr/acinq/eclair/channel/Helpers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,17 @@ object Helpers {
extractShutdownScript(accept.temporaryChannelId, localFeatures, remoteFeatures, accept.upfrontShutdownScript_opt).map(script_opt => (channelFeatures, script_opt))
}

def scidForChannelUpdate(normal: DATA_NORMAL): ShortChannelId = {
normal.shortChannelId_opt match {
case Some(shortChannelId) if !normal.commitments.channelFeatures.hasFeature(Features.ScidAlias) =>
// funding tx has reached 6 blocks, and the channel does not support scid alias feature, we use the real scid
shortChannelId
case _ =>
// in all other cases (unconfirmed funding tx, scid alias feature, ...) we use an alias
normal.localAliases.last
}
}

/**
* Compute the delay until we need to refresh the channel_update for our channel not to be considered stale by
* other nodes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@ class Register extends Actor with ActorLogging {
case ChannelIdAssigned(channel, remoteNodeId, temporaryChannelId, channelId) =>
context become main(channels + (channelId -> channel) - temporaryChannelId, shortIds, channelsTo + (channelId -> remoteNodeId) - temporaryChannelId)

case ShortChannelIdAssigned(_, channelId, shortChannelId, _) =>
context become main(channels, shortIds + (shortChannelId -> channelId), channelsTo)
case scidAssigned: ShortChannelIdAssigned =>
// we map all known scids (real or aliases) to the channel_id
val m = (scidAssigned.shortChannelId_opt.toSeq ++ scidAssigned.localAliases).map(_ -> scidAssigned.channelId).toMap
context become main(channels, shortIds ++ m, channelsTo)

case Terminated(actor) if channels.values.toSet.contains(actor) =>
val channelId = channels.find(_._2 == actor).get._1
val shortChannelId = shortIds.find(_._2 == channelId).map(_._1).getOrElse(ShortChannelId(0L))
context become main(channels - channelId, shortIds - shortChannelId, channelsTo - channelId)
val shortChannelIds = shortIds.collect { case (key, value) if value == channelId => key }
context become main(channels - channelId, shortIds -- shortChannelIds, channelsTo - channelId)

case Symbol("channels") => sender() ! channels

Expand Down
Loading

0 comments on commit 32543a3

Please sign in to comment.