mirror of
https://github.com/ACINQ/eclair.git
synced 2025-03-13 19:37:35 +01:00
Merge remote-tracking branch 'origin/wip-routing-sync' into android
This commit is contained in:
commit
2a9348a7a2
2 changed files with 39 additions and 36 deletions
|
@ -78,44 +78,47 @@ object ChannelRangeQueries {
|
|||
*/
|
||||
def decodeShortChannelIds(data: BinaryData): (Byte, Set[ShortChannelId], Boolean) = {
|
||||
val format = data.head
|
||||
val buffer = new Array[Byte](8)
|
||||
if (data.tail.isEmpty) (format, Set.empty[ShortChannelId], false) else {
|
||||
val buffer = new Array[Byte](8)
|
||||
|
||||
// read 8 bytes from input
|
||||
// zipped input stream often returns less bytes than what you want to read
|
||||
@tailrec
|
||||
def read8(input: InputStream, offset: Int = 0): Int = input.read(buffer, offset, 8 - offset) match {
|
||||
case len if len <= 0 => len
|
||||
case 8 => 8
|
||||
case len if offset + len == 8 => 8
|
||||
case len => read8(input, offset + len)
|
||||
}
|
||||
|
||||
// read until there's nothing left
|
||||
@tailrec
|
||||
def loop(input: InputStream, acc: Set[ShortChannelId]): Set[ShortChannelId] = {
|
||||
if (read8(input) <= 0) acc else loop(input, acc + ShortChannelId(Protocol.uint64(buffer, ByteOrder.BIG_ENDIAN)))
|
||||
}
|
||||
|
||||
def readAll(useGzip: Boolean) = {
|
||||
val bis = new ByteArrayInputStream(data.tail.toArray)
|
||||
val input = format match {
|
||||
case UNCOMPRESSED_FORMAT => bis
|
||||
case ZLIB_FORMAT if useGzip => new GZIPInputStream(bis)
|
||||
case ZLIB_FORMAT => new InflaterInputStream(bis)
|
||||
// read 8 bytes from input
|
||||
// zipped input stream often returns less bytes than what you want to read
|
||||
@tailrec
|
||||
def read8(input: InputStream, offset: Int = 0): Int = input.read(buffer, offset, 8 - offset) match {
|
||||
case len if len <= 0 => len
|
||||
case 8 => 8
|
||||
case len if offset + len == 8 => 8
|
||||
case len => read8(input, offset + len)
|
||||
}
|
||||
|
||||
// read until there's nothing left
|
||||
@tailrec
|
||||
def loop(input: InputStream, acc: Set[ShortChannelId]): Set[ShortChannelId] = {
|
||||
val check = read8(input)
|
||||
if (check <= 0) acc else loop(input, acc + ShortChannelId(Protocol.uint64(buffer, ByteOrder.BIG_ENDIAN)))
|
||||
}
|
||||
|
||||
def readAll(useGzip: Boolean) = {
|
||||
val bis = new ByteArrayInputStream(data.tail.toArray)
|
||||
val input = format match {
|
||||
case UNCOMPRESSED_FORMAT => bis
|
||||
case ZLIB_FORMAT if useGzip => new GZIPInputStream(bis)
|
||||
case ZLIB_FORMAT => new InflaterInputStream(bis)
|
||||
}
|
||||
try {
|
||||
(format, loop(input, Set.empty[ShortChannelId]), useGzip)
|
||||
}
|
||||
finally {
|
||||
input.close()
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
(format, loop(input, Set.empty[ShortChannelId]), useGzip)
|
||||
readAll(useGzip = false)
|
||||
}
|
||||
finally {
|
||||
input.close()
|
||||
catch {
|
||||
case t: Throwable if format == ZLIB_FORMAT => readAll(useGzip = true)
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
readAll(useGzip = false)
|
||||
}
|
||||
catch {
|
||||
case t: Throwable if format == ZLIB_FORMAT => readAll(useGzip = true)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -322,11 +322,11 @@ class Router(nodeParams: NodeParams, watcher: ActorRef) extends FSM[State, Data]
|
|||
if (chainHash != nodeParams.chainHash) {
|
||||
log.warning("received reply_channel_range message for chain {}, we're on {}", chainHash, nodeParams.chainHash)
|
||||
} else {
|
||||
val (_, theirShortChannelIds, useGzip) = ChannelRangeQueries.decodeShortChannelIds(data)
|
||||
val (format, theirShortChannelIds, useGzip) = ChannelRangeQueries.decodeShortChannelIds(data)
|
||||
val ourShortChannelIds = d.channels.keys.filter(keep(firstBlockNum, numberOfBlocks, _, d.channels, d.updates)) // note: order is preserved
|
||||
val missing = theirShortChannelIds -- ourShortChannelIds
|
||||
log.info("we received their reply, we're missing {} channel announcements/updates, useGzip={}", missing.size, useGzip)
|
||||
val blocks = ChannelRangeQueries.encodeShortChannelIds(firstBlockNum, numberOfBlocks, missing, ChannelRangeQueries.ZLIB_FORMAT, useGzip)
|
||||
log.info("we received their reply, we're missing {} channel announcements/updates, format={} useGzip={}", missing.size, format, useGzip)
|
||||
val blocks = ChannelRangeQueries.encodeShortChannelIds(firstBlockNum, numberOfBlocks, missing, format, useGzip)
|
||||
blocks.foreach(block => sender ! QueryShortChannelIds(chainHash, block.shortChannelIds))
|
||||
}
|
||||
stay
|
||||
|
|
Loading…
Add table
Reference in a new issue