-
Notifications
You must be signed in to change notification settings - Fork 282
Generate local alias before spawning channel #2316
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,8 @@ package fr.acinq.eclair | |
|
|
||
| import fr.acinq.eclair.ShortChannelId.toShortId | ||
|
|
||
| import scala.util.Random | ||
|
|
||
| // @formatter:off | ||
| sealed trait ShortChannelId extends Ordered[ShortChannelId] { | ||
| def toLong: Long | ||
|
|
@@ -61,7 +63,11 @@ object ShortChannelId { | |
|
|
||
| def toShortId(blockHeight: Int, txIndex: Int, outputIndex: Int): Long = ((blockHeight & 0xFFFFFFL) << 40) | ((txIndex & 0xFFFFFFL) << 16) | (outputIndex & 0xFFFFL) | ||
|
|
||
| def generateLocalAlias(): Alias = Alias(System.nanoTime()) // TODO: fixme (duplicate, etc.) | ||
| def generateLocalAlias(): Alias = { | ||
| // at block height 1000 LN didn't exist, so all real scids less than that will never be used | ||
| val upperBound = RealShortChannelId.apply(BlockHeight(1000),1,0).toLong | ||
| Alias(Random.nextLong(upperBound)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should use the |
||
| } | ||
|
|
||
| @inline | ||
| def blockHeight(shortChannelId: ShortChannelId): BlockHeight = BlockHeight((shortChannelId.toLong >> 40) & 0xFFFFFF) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,9 +19,11 @@ package fr.acinq.eclair.channel | |
| import akka.actor.{Actor, ActorLogging, ActorRef, Terminated} | ||
| import fr.acinq.bitcoin.scalacompat.ByteVector32 | ||
| import fr.acinq.bitcoin.scalacompat.Crypto.PublicKey | ||
| import fr.acinq.eclair.ShortChannelId | ||
| import fr.acinq.eclair.{Alias, ShortChannelId} | ||
| import fr.acinq.eclair.channel.Register._ | ||
|
|
||
| import scala.concurrent.Promise | ||
|
|
||
| /** | ||
| * Created by PM on 26/01/2016. | ||
| */ | ||
|
|
@@ -32,6 +34,7 @@ class Register extends Actor with ActorLogging { | |
| context.system.eventStream.subscribe(self, classOf[AbstractChannelRestored]) | ||
| context.system.eventStream.subscribe(self, classOf[ChannelIdAssigned]) | ||
| context.system.eventStream.subscribe(self, classOf[ShortChannelIdAssigned]) | ||
| context.system.eventStream.subscribe(self, classOf[GenerateLocalAlias]) | ||
|
|
||
| override def receive: Receive = main(Map.empty, Map.empty, Map.empty) | ||
|
|
||
|
|
@@ -79,6 +82,13 @@ class Register extends Actor with ActorLogging { | |
| case Some(channel) => channel.tell(msg, compatReplyTo) | ||
| case None => compatReplyTo ! ForwardShortIdFailure(fwd) | ||
| } | ||
|
|
||
| case GenerateLocalAlias(promise) => | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we re-populate the I believe it won't be an issue in practice as long as the range we use for |
||
| var alias: Alias = null | ||
| do { | ||
| alias = ShortChannelId.generateLocalAlias() | ||
| } while (shortIds.contains(alias)) // we make sure that there is no collision with an existing scid | ||
| promise.trySuccess(alias) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -90,5 +100,7 @@ object Register { | |
|
|
||
| case class ForwardFailure[T](fwd: Forward[T]) | ||
| case class ForwardShortIdFailure[T](fwd: ForwardShortId[T]) | ||
|
|
||
| case class GenerateLocalAlias(promise: Promise[Alias]) | ||
| // @formatter:on | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we use such a small range? We can go up to at least block 350 000, can't we?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The space is already huge : 1_099_511_627_841_536 (*) and we are checking for duplicates. I figure leaving some values available could prove handy later?
(*) So huge in fact that I believe our previous computation was invalid. If we indeed go up to block height 350 000 it gives us a ceiling of 384_829_069_721_665_536 which is about 2^59. The probability of a collision for 250 000 channels would be 0.000011 %, not 0.8 % ?