-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Binary serialization for payments data (#739)
This is symmetrical with what we already have for channel data. The goal is to offer ready-made serializers for library integrators, taking care of backward compatibility when the model is updated, instead of pushing the work to integrators. Initially I explored a json-based approach in branch [db-type-module](https://github.com/ACINQ/lightning-kmp/tree/db-type-module) (see module `lightning-kmp-db-types`). But it turned out to be tedious and verbose, with a lot of class definitions and boiler plate code to handle model migrations. These binary codecs are much lighter to write and maintain, and probably faster (although it was not the main objective). The main drawbacks is that the serialized data is not human readable.
- Loading branch information
Showing
32 changed files
with
1,120 additions
and
184 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
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
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
55 changes: 55 additions & 0 deletions
55
modules/core/src/commonMain/kotlin/fr/acinq/lightning/serialization/InputExtensions.kt
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,55 @@ | ||
package fr.acinq.lightning.serialization | ||
|
||
import fr.acinq.bitcoin.* | ||
import fr.acinq.bitcoin.io.Input | ||
import fr.acinq.bitcoin.utils.Either | ||
import fr.acinq.lightning.utils.UUID | ||
import fr.acinq.lightning.wire.LightningCodecs | ||
import fr.acinq.lightning.wire.LightningMessage | ||
|
||
object InputExtensions { | ||
|
||
fun Input.readNumber(): Long = LightningCodecs.bigSize(this) | ||
|
||
fun Input.readBoolean(): Boolean = read() == 1 | ||
|
||
fun Input.readString(): String = readDelimitedByteArray().decodeToString() | ||
|
||
fun Input.readByteVector32(): ByteVector32 = ByteVector32(ByteArray(32).also { read(it, 0, it.size) }) | ||
|
||
fun Input.readByteVector64(): ByteVector64 = ByteVector64(ByteArray(64).also { read(it, 0, it.size) }) | ||
|
||
fun Input.readPublicKey() = PublicKey(ByteArray(33).also { read(it, 0, it.size) }) | ||
|
||
fun Input.readPrivateKey() = PrivateKey(ByteArray(32).also { read(it, 0, it.size) }) | ||
|
||
fun Input.readTxId(): TxId = TxId(readByteVector32()) | ||
|
||
fun Input.readUuid(): UUID = UUID.fromBytes(ByteArray(16).also { read(it, 0, it.size) }) | ||
|
||
fun Input.readDelimitedByteArray(): ByteArray { | ||
val size = readNumber().toInt() | ||
return ByteArray(size).also { read(it, 0, size) } | ||
} | ||
|
||
fun Input.readLightningMessage() = LightningMessage.decode(readDelimitedByteArray()) | ||
|
||
fun <T> Input.readCollection(readElem: () -> T): Collection<T> { | ||
val size = readNumber() | ||
return buildList { | ||
repeat(size.toInt()) { | ||
add(readElem()) | ||
} | ||
} | ||
} | ||
|
||
fun <L, R> Input.readEither(readLeft: () -> L, readRight: () -> R): Either<L, R> = when (read()) { | ||
0 -> Either.Left(readLeft()) | ||
else -> Either.Right(readRight()) | ||
} | ||
|
||
fun <T : Any> Input.readNullable(readNotNull: () -> T): T? = when (read()) { | ||
1 -> readNotNull() | ||
else -> null | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
modules/core/src/commonMain/kotlin/fr/acinq/lightning/serialization/OutputExtensions.kt
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,71 @@ | ||
package fr.acinq.lightning.serialization | ||
|
||
import fr.acinq.bitcoin.* | ||
import fr.acinq.bitcoin.io.Output | ||
import fr.acinq.bitcoin.utils.Either | ||
import fr.acinq.lightning.utils.UUID | ||
import fr.acinq.lightning.wire.LightningCodecs | ||
import fr.acinq.lightning.wire.LightningMessage | ||
|
||
object OutputExtensions { | ||
|
||
fun Output.writeNumber(o: Number): Unit = LightningCodecs.writeBigSize(o.toLong(), this) | ||
|
||
fun Output.writeBoolean(o: Boolean): Unit = if (o) write(1) else write(0) | ||
|
||
fun Output.writeString(o: String): Unit = writeDelimited(o.encodeToByteArray()) | ||
|
||
fun Output.writeByteVector32(o: ByteVector32) = write(o.toByteArray()) | ||
|
||
fun Output.writeByteVector64(o: ByteVector64) = write(o.toByteArray()) | ||
|
||
fun Output.writePublicKey(o: PublicKey) = write(o.value.toByteArray()) | ||
|
||
fun Output.writePrivateKey(o: PrivateKey) = write(o.value.toByteArray()) | ||
|
||
fun Output.writeTxId(o: TxId) = write(o.value.toByteArray()) | ||
|
||
fun Output.writeUuid(o: UUID) = o.run { | ||
// NB: copied from kotlin source code (https://github.com/JetBrains/kotlin/blob/v2.1.0/libraries/stdlib/src/kotlin/uuid/Uuid.kt) in order to be forward compatible | ||
fun Long.toByteArray(dst: ByteArray, dstOffset: Int) { | ||
for (index in 0 until 8) { | ||
val shift = 8 * (7 - index) | ||
dst[dstOffset + index] = (this ushr shift).toByte() | ||
} | ||
} | ||
val bytes = ByteArray(16) | ||
mostSignificantBits.toByteArray(bytes, 0) | ||
leastSignificantBits.toByteArray(bytes, 8) | ||
write(bytes) | ||
} | ||
|
||
fun Output.writeDelimited(o: ByteArray) { | ||
writeNumber(o.size) | ||
write(o) | ||
} | ||
|
||
fun <T : BtcSerializable<T>> Output.writeBtcObject(o: T): Unit = writeDelimited(o.serializer().write(o)) | ||
|
||
fun Output.writeLightningMessage(o: LightningMessage) = writeDelimited(LightningMessage.encode(o)) | ||
|
||
fun <T> Output.writeCollection(o: Collection<T>, writeElem: (T) -> Unit) { | ||
writeNumber(o.size) | ||
o.forEach { writeElem(it) } | ||
} | ||
|
||
fun <L, R> Output.writeEither(o: Either<L, R>, writeLeft: (L) -> Unit, writeRight: (R) -> Unit) = when (o) { | ||
is Either.Left -> { | ||
write(0); writeLeft(o.value) | ||
} | ||
is Either.Right -> { | ||
write(1); writeRight(o.value) | ||
} | ||
} | ||
|
||
fun <T : Any> Output.writeNullable(o: T?, writeNotNull: (T) -> Unit) = when (o) { | ||
is T -> { | ||
write(1); writeNotNull(o) | ||
} | ||
else -> write(0) | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...inq/lightning/serialization/Encryption.kt → ...tning/serialization/channel/Encryption.kt
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
6 changes: 3 additions & 3 deletions
6
.../lightning/serialization/Serialization.kt → ...ng/serialization/channel/Serialization.kt
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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.