Skip to content

Commit

Permalink
group all short ids in a class
Browse files Browse the repository at this point in the history
  • Loading branch information
pm47 committed Jun 9, 2022
1 parent 896c4e5 commit 31799b7
Show file tree
Hide file tree
Showing 32 changed files with 326 additions and 282 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -427,16 +427,32 @@ final case class DATA_WAIT_FOR_FUNDING_CONFIRMED(commitments: Commitments,
deferred: Option[ChannelReady],
lastSent: Either[FundingCreated, FundingSigned]) extends PersistentChannelData
final case class DATA_WAIT_FOR_CHANNEL_READY(commitments: Commitments,
realShortChannelId_opt: Option[RealShortChannelId],
localAlias: LocalAlias,
shortIds: ShortIds,
lastSent: ChannelReady) extends PersistentChannelData
sealed trait RealScidStatus { def toOption: Option[RealShortChannelId] }
object RealScidStatus {
case class Temporary(realScid: RealShortChannelId) extends RealScidStatus { override def toOption: Option[RealShortChannelId] = Some(realScid) }
case class Final(realScid: RealShortChannelId) extends RealScidStatus { override def toOption: Option[RealShortChannelId] = Some(realScid) }
case object Unknown extends RealScidStatus { override def toOption: Option[RealShortChannelId] = None }
}

/**
* Short identifiers for the channel
*
* @param real the real scid, it may change if a reorg happens before the channel reaches 6 conf
* @param localAlias we must remember the alias that we sent to our peer because we use it to:
* - identify incoming [[ChannelUpdate]]
* - route outgoing payments to that channel
* @param remoteAlias_opt we only remember the last alias received from our peer, we use this to generate
* routing hints in [[fr.acinq.eclair.payment.Bolt11Invoice]]
*/
case class ShortIds(real: RealScidStatus,
localAlias: LocalAlias,
remoteAlias_opt: Option[ShortChannelId])
final case class DATA_NORMAL(commitments: Commitments,
realShortChannelId_opt: Option[RealShortChannelId],
buried: Boolean,
shortIds: ShortIds,
channelAnnouncement: Option[ChannelAnnouncement],
channelUpdate: ChannelUpdate,
localAlias: LocalAlias,
remoteAlias_opt: Option[ShortChannelId],
localShutdown: Option[Shutdown],
remoteShutdown: Option[Shutdown],
closingFeerates: Option[ClosingFeerates]) extends PersistentChannelData
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,34 +46,27 @@ case class ChannelIdAssigned(channel: ActorRef, remoteNodeId: PublicKey, tempora

/**
* This event will be sent whenever a new scid is assigned to the channel, be it a real, local alias or remote alias.
*
* @param realShortChannelId_opt the real scid, it can change in case of a reorg before the channel reaches 6 conf
* @param localAlias we must remember the alias that we sent to our peer because we use it to:
* - identify incoming [[ChannelUpdate]]
* - route outgoing payments to that channel
* @param remoteAlias_opt we only remember the last alias received from our peer, we use this to generate
* routing hints in [[fr.acinq.eclair.payment.Bolt11Invoice]]
*/
case class ShortChannelIdAssigned(channel: ActorRef, channelId: ByteVector32, realShortChannelId_opt: Option[RealShortChannelId], localAlias: LocalAlias, remoteAlias_opt: Option[ShortChannelId], remoteNodeId: PublicKey) extends ChannelEvent
case class ShortChannelIdAssigned(channel: ActorRef, channelId: ByteVector32, shortIds: ShortIds, remoteNodeId: PublicKey) extends ChannelEvent

case class LocalChannelUpdate(channel: ActorRef, channelId: ByteVector32, realShortChannelId_opt: Option[RealShortChannelId], localAlias: LocalAlias, remoteNodeId: PublicKey, channelAnnouncement_opt: Option[ChannelAnnouncement], channelUpdate: ChannelUpdate, commitments: AbstractCommitments) extends ChannelEvent {
case class LocalChannelUpdate(channel: ActorRef, channelId: ByteVector32, shortIds: ShortIds, remoteNodeId: PublicKey, channelAnnouncement_opt: Option[ChannelAnnouncement], channelUpdate: ChannelUpdate, commitments: AbstractCommitments) extends ChannelEvent {
/**
* We always map the local alias because we must always be able to route based on it
* However we only map the real scid if option_scid_alias (TODO: rename to option_scid_privacy) is disabled
*/
def scidsForRouting: Seq[ShortChannelId] = {
commitments match {
case c: Commitments =>
val realScid_opt = if (c.channelFeatures.hasFeature(Features.ScidAlias)) None else realShortChannelId_opt
realScid_opt.toSeq :+ localAlias
case _ => Seq(localAlias) // TODO: ugly
val realScid_opt = if (c.channelFeatures.hasFeature(Features.ScidAlias)) None else shortIds.real.toOption
realScid_opt.toSeq :+ shortIds.localAlias
case _ => Seq(shortIds.localAlias) // TODO: ugly
}
}
}

case class ChannelUpdateParametersChanged(channel: ActorRef, channelId: ByteVector32, remoteNodeId: PublicKey, channelUpdate: ChannelUpdate) extends ChannelEvent

case class LocalChannelDown(channel: ActorRef, channelId: ByteVector32, realShortChannelId_opt: Option[RealShortChannelId], localAlias: LocalAlias, remoteNodeId: PublicKey) extends ChannelEvent
case class LocalChannelDown(channel: ActorRef, channelId: ByteVector32, shortIds: ShortIds, remoteNodeId: PublicKey) extends ChannelEvent

case class ChannelStateChanged(channel: ActorRef, channelId: ByteVector32, peer: ActorRef, remoteNodeId: PublicKey, previousState: ChannelState, currentState: ChannelState, commitments_opt: Option[AbstractCommitments]) extends ChannelEvent

Expand All @@ -89,7 +82,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, realShortChannelId_opt: Option[RealShortChannelId], localAlias: LocalAlias, commitments: AbstractCommitments) extends ChannelEvent
case class AvailableBalanceChanged(channel: ActorRef, channelId: ByteVector32, shortIds: ShortIds, commitments: AbstractCommitments) extends ChannelEvent

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,14 @@ object Helpers {
* - min_depth > 0 : use real scid (may change if reorg between min_depth and 6 conf)
* - min_depth = 0 (zero-conf) : unsupported
*/
def scidForChannelUpdate(channelAnnouncement_opt: Option[ChannelAnnouncement], realShortChannelId_opt: Option[ShortChannelId], remoteAlias_opt: Option[ShortChannelId])(implicit log: DiagnosticLoggingAdapter): ShortChannelId = {
def scidForChannelUpdate(channelAnnouncement_opt: Option[ChannelAnnouncement], shortIds: ShortIds)(implicit log: DiagnosticLoggingAdapter): ShortChannelId = {
channelAnnouncement_opt.map(_.shortChannelId) // we use the real "final" scid when it is publicly announced
.orElse(remoteAlias_opt) // otherwise the remote alias
.orElse(realShortChannelId_opt) // if we don't have a remote alias, we use the real scid (which could change because the funding tx possibly has less than 6 confs here)
.orElse(shortIds.remoteAlias_opt) // otherwise the remote alias
.orElse(shortIds.real.toOption) // if we don't have a remote alias, we use the real scid (which could change because the funding tx possibly has less than 6 confs here)
.getOrElse(throw new RuntimeException("this is a zero-conf channel and no alias was provided in channel_ready")) // if we don't have a real scid, it means this is a zero-conf channel and our peer must have sent an alias
}

def scidForChannelUpdate(d: DATA_NORMAL)(implicit log: DiagnosticLoggingAdapter): ShortChannelId = scidForChannelUpdate(d.channelAnnouncement, realShortChannelId_opt = d.realShortChannelId_opt, remoteAlias_opt = d.remoteAlias_opt)
def scidForChannelUpdate(d: DATA_NORMAL)(implicit log: DiagnosticLoggingAdapter): ShortChannelId = scidForChannelUpdate(d.channelAnnouncement, d.shortIds)

/**
* Compute the delay until we need to refresh the channel_update for our channel not to be considered stale by
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Register extends Actor with ActorLogging {
case scidAssigned: ShortChannelIdAssigned =>
// We map all known scids (real or alias) to the channel_id. The relayer is in charge of deciding whether a real
// scid can be used or not for routing (see option_scid_privacy), but the register is neutral.
val m = (scidAssigned.realShortChannelId_opt.toSeq :+ scidAssigned.localAlias).map(_ -> scidAssigned.channelId).toMap
val m = (scidAssigned.shortIds.real.toOption.toSeq :+ scidAssigned.shortIds.localAlias).map(_ -> scidAssigned.channelId).toMap
context become main(channels, shortIds ++ m, channelsTo)

case Terminated(actor) if channels.values.toSet.contains(actor) =>
Expand Down
Loading

0 comments on commit 31799b7

Please sign in to comment.