Skip to content

Commit

Permalink
Upgrade to kotlin 1.5 (#297)
Browse files Browse the repository at this point in the history
* Upgrade to kotlin 1.5

* Use kodein-log 0.11.1 (official version, not our fork)

* Use kotlin 1.5.31, secp256k1-kmp 0.6.0 and bitcoin-kmp 0.8.0

Co-authored-by: dpad85 <[email protected]>
  • Loading branch information
sstone and dpad85 authored Sep 23, 2021
1 parent da7b603 commit d09db3d
Show file tree
Hide file tree
Showing 26 changed files with 93 additions and 102 deletions.
12 changes: 0 additions & 12 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ jobs:
uses: actions/checkout@v2
with:
submodules: 'true'
- name: Checkout kodein-log with UTC fork
uses: actions/checkout@v2
with:
repository: dpad85/Kodein-Log
ref: utc-timezone
path: kodein-log

# 2 - setup cache and tools
- name: Cached Konan
Expand All @@ -63,12 +57,6 @@ jobs:
with:
java-version: 11

# 3 - build dependencies
- name: Build kodein-log and publish it to local maven
run: |
cd kodein-log
./gradlew publishToMavenLocal
# 4 - tests
- name: Check with integration
if: matrix.os == 'ubuntu-latest'
Expand Down
20 changes: 0 additions & 20 deletions BUILD.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,6 @@ brew install libtool
brew install gmp
```

## Required dependency

This project currently depends on a fork of the `kodein-logs` library. This lib must be built manually and deployed to the local maven repository before being able to build `lightning-kmp`. To do so:
1. clone [the GitHub repo](https://github.com/dpad85/Kodein-Log)
```
git clone [email protected]:dpad85/Kodein-Log.git
cd Kodein-Log
```
2. checkout the `0.10.1-utc` tag
```
git checkout 0.10.1-utc
```
4. build and deploy the library:
```
./gradlew build
./gradlew publishToMavenLocal
```

Note: this dependency is a temporary workaround and will be removed as soon as possible.

## Build

You should start by cloning the repository locally:
Expand Down
18 changes: 9 additions & 9 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
import org.jetbrains.kotlin.gradle.targets.native.tasks.KotlinNativeTest

plugins {
kotlin("multiplatform") version "1.4.31"
kotlin("plugin.serialization") version "1.4.30"
kotlin("multiplatform") version "1.5.31"
kotlin("plugin.serialization") version "1.5.31"
`maven-publish`
}

allprojects {
group = "fr.acinq.lightning"
version = "1.0-beta17"
version = "1.1-SNAPSHOT"

repositories {
mavenLocal()
Expand All @@ -23,17 +23,17 @@ allprojects {
val currentOs = org.gradle.internal.os.OperatingSystem.current()

kotlin {
val ktorVersion: String by extra { "1.5.3" }
val ktorVersion: String by extra { "1.6.3" }
fun ktor(module: String) = "io.ktor:ktor-$module:$ktorVersion"
val secp256k1Version = "0.5.1"
val serializationVersion = "1.1.0"
val coroutineVersion = "1.4.3-native-mt"
val secp256k1Version = "0.6.0"
val serializationVersion = "1.2.2"
val coroutineVersion = "1.5.2-native-mt"

val commonMain by sourceSets.getting {
dependencies {
api("fr.acinq.bitcoin:bitcoin-kmp:0.7.0")
api("fr.acinq.bitcoin:bitcoin-kmp:0.8.0")
api("fr.acinq.secp256k1:secp256k1-kmp:$secp256k1Version")
api("org.kodein.log:kodein-log:0.10.1-utc")
api("org.kodein.log:kodein-log:0.11.1")
api("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutineVersion")
api("org.jetbrains.kotlinx:kotlinx-serialization-core:$serializationVersion")
api("org.jetbrains.kotlinx:kotlinx-serialization-cbor:$serializationVersion")
Expand Down
1 change: 0 additions & 1 deletion lightning-kmp-test-fixtures/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ val currentOs = org.gradle.internal.os.OperatingSystem.current()
kotlin {
val ktorVersion: String by rootProject.extra
fun ktor(module: String) = "io.ktor:ktor-$module:$ktorVersion"
val serializationVersion = "1.0.0-RC"

val commonMain by sourceSets.getting {
dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,13 @@ import fr.acinq.lightning.wire.ChannelReestablish
import fr.acinq.lightning.wire.FundingLocked
import fr.acinq.lightning.wire.Init
import fr.acinq.lightning.wire.LightningMessage
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch

@OptIn(ExperimentalCoroutinesApi::class)
@OptIn(ExperimentalCoroutinesApi::class, kotlinx.coroutines.ObsoleteCoroutinesApi::class)
public suspend fun newPeers(
scope: CoroutineScope,
nodeParams: Pair<NodeParams, NodeParams>,
Expand Down Expand Up @@ -114,6 +111,7 @@ public suspend fun newPeers(
return PeerTuple(alice, bob, alice2bob, bob2alice)
}

@OptIn(ObsoleteCoroutinesApi::class)
public suspend fun CoroutineScope.newPeer(
nodeParams: NodeParams,
walletParams: WalletParams,
Expand Down Expand Up @@ -161,7 +159,7 @@ public suspend fun CoroutineScope.newPeer(
return peer
}

@OptIn(ExperimentalCoroutinesApi::class)
@OptIn(ExperimentalCoroutinesApi::class, kotlinx.coroutines.ObsoleteCoroutinesApi::class)
public fun buildPeer(
scope: CoroutineScope,
nodeParams: NodeParams,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import kotlin.time.seconds
expect fun runSuspendBlocking(block: suspend CoroutineScope.() -> Unit)

@OptIn(ExperimentalTime::class)
fun runSuspendTest(timeout: Duration = 30.seconds, test: suspend CoroutineScope.() -> Unit) {
fun runSuspendTest(timeout: Duration = Duration.seconds(30), test: suspend CoroutineScope.() -> Unit) {
runSuspendBlocking {
withTimeout(timeout) {
launch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.filter
import kotlinx.serialization.json.Json
import kotlin.native.concurrent.ThreadLocal
import kotlin.time.Duration
import kotlin.time.ExperimentalTime
import kotlin.time.seconds

Expand Down Expand Up @@ -58,6 +59,7 @@ internal sealed class ClientState {
abstract fun process(event: ClientEvent): Pair<ClientState, List<ElectrumClientAction>>
}

@ObsoleteCoroutinesApi
internal object WaitingForVersion : ClientState() {
override fun process(event: ClientEvent): Pair<ClientState, List<ElectrumClientAction>> = when {
event is ReceivedResponse && event.response is Either.Right -> {
Expand All @@ -77,6 +79,7 @@ internal object WaitingForVersion : ClientState() {
}
}

@ObsoleteCoroutinesApi
internal object WaitingForTip : ClientState() {
override fun process(event: ClientEvent): Pair<ClientState, List<ElectrumClientAction>> =
when (event) {
Expand All @@ -97,6 +100,7 @@ internal object WaitingForTip : ClientState() {
}
}

@ObsoleteCoroutinesApi
internal data class ClientRunning(
val height: Int,
val tip: BlockHeader,
Expand Down Expand Up @@ -138,6 +142,7 @@ internal data class ClientRunning(
}
}

@ObsoleteCoroutinesApi
internal object ClientClosed : ClientState() {
override fun process(event: ClientEvent): Pair<ClientState, List<ElectrumClientAction>> =
when (event) {
Expand All @@ -149,6 +154,7 @@ internal object ClientClosed : ClientState() {
}
}

@ObsoleteCoroutinesApi
private fun ClientState.unhandled(event: ClientEvent): Pair<ClientState, List<ElectrumClientAction>> =
when (event) {
Disconnected -> ClientClosed to emptyList()
Expand All @@ -159,12 +165,14 @@ private fun ClientState.unhandled(event: ClientEvent): Pair<ClientState, List<El
}
}

@ObsoleteCoroutinesApi
private class ClientStateBuilder {
var state: ClientState = ClientClosed
var actions = emptyList<ElectrumClientAction>()
fun build() = state to actions
}

@ObsoleteCoroutinesApi
private fun newState(init: ClientStateBuilder.() -> Unit) = ClientStateBuilder().apply(init).build()

private fun ClientState.returnState(actions: List<ElectrumClientAction> = emptyList()): Pair<ClientState, List<ElectrumClientAction>> = this to actions
Expand All @@ -176,7 +184,7 @@ data class RequestResponseTimestamp(
val lastResponseTimestamp: Long?
)

@OptIn(ExperimentalCoroutinesApi::class, ExperimentalTime::class)
@OptIn(ExperimentalCoroutinesApi::class, ExperimentalTime::class, ObsoleteCoroutinesApi::class)
class ElectrumClient(
socketBuilder: TcpSocket.Builder?,
scope: CoroutineScope
Expand Down Expand Up @@ -284,7 +292,7 @@ class ElectrumClient(

suspend fun ping() {
while (isActive) {
delay(30.seconds)
delay(Duration.seconds(30))
send(Ping.asJsonRPCRequest(-1).encodeToByteArray())
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ internal fun parseJsonResponse(request: ElectrumRequest, rpcResponse: JsonRPCRes
var progress = 0
val inputSize = input.availableBytes
while (progress < inputSize) {
val header = input.readNBytes(headerSize)
val header = input.readNBytes(headerSize)!!
add(BlockHeader.read(header))
progress += headerSize
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ private data class WatcherDisconnected(
val block2tx: Map<Long, List<Transaction>> = mapOf(),
val getTxQueue: List<GetTxWithMeta> = listOf()
) : WatcherState() {
@OptIn(ObsoleteCoroutinesApi::class)
override fun process(event: WatcherEvent): Pair<WatcherState, List<WatcherAction>> =
when (event) {
is ClientStateUpdate -> {
Expand Down Expand Up @@ -353,6 +354,7 @@ private data class WatcherRunning(
}
}

@OptIn(ObsoleteCoroutinesApi::class)
private fun setupWatch(watch: Watch) = when (watch) {
is WatchLost -> returnState() // ignore WatchLost for now
in watches -> returnState()
Expand Down Expand Up @@ -509,7 +511,7 @@ class ElectrumWatcher(
private fun startTimer() {
if (timerJob != null) return

val timeMillis: Long = 2 * 1_000 // fire timer every 2 seconds
val timeMillis: Long = 2L * 1_000 // fire timer every 2 seconds
timerJob = launch {
delay(timeMillis)
while(isActive) {
Expand Down Expand Up @@ -605,6 +607,7 @@ class ElectrumWatcher(
companion object {
val logger by lightningLogger<ElectrumWatcher>()

@ObsoleteCoroutinesApi
internal fun registerToScriptHash(watch: Watch): RegisterToScriptHashNotification? = when (watch) {
is WatchSpent -> {
val (_, txid, outputIndex, publicKeyScript, _) = watch
Expand Down
6 changes: 4 additions & 2 deletions src/commonMain/kotlin/fr/acinq/lightning/io/Peer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import kotlinx.coroutines.channels.Channel.Factory.BUFFERED
import kotlinx.coroutines.channels.ReceiveChannel
import kotlinx.coroutines.channels.consumeEach
import kotlinx.coroutines.flow.*
import kotlin.time.Duration
import kotlin.time.ExperimentalTime
import kotlin.time.seconds

Expand Down Expand Up @@ -60,6 +61,7 @@ data class SwapInResponseEvent(val swapInResponse: SwapInResponse) : PeerListene
data class SwapInPendingEvent(val swapInPending: SwapInPending) : PeerListenerEvent()
data class SwapInConfirmedEvent(val swapInConfirmed: SwapInConfirmed) : PeerListenerEvent()

@ObsoleteCoroutinesApi
@OptIn(ExperimentalStdlibApi::class, ExperimentalCoroutinesApi::class, ExperimentalTime::class)
class Peer(
val nodeParams: NodeParams,
Expand Down Expand Up @@ -275,14 +277,14 @@ class Peer(
suspend fun doPing() {
val ping = Ping(10, ByteVector("deadbeef"))
while (isActive) {
delay(30.seconds)
delay(Duration.seconds(30))
sendToPeer(ping)
}
}

suspend fun checkPaymentsTimeout() {
while (isActive) {
delay(30.seconds)
delay(Duration.seconds(30))
input.send(CheckPaymentsTimeout)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ data class PaymentRequest(
val int5s = ArrayList<Int5>()
while (stream.bitCount() >= 5) int5s.add(read5())

return Bech32.encode(hrp(), int5s.toByteArray())
return Bech32.encode(hrp(), int5s.toByteArray(), Bech32.Encoding.Bech32)
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ object Serialization {
TODO()
}

override fun encodeChar(value: Char) = output.write(value.toInt())
override fun encodeChar(value: Char) = output.write(value.code)
override fun encodeString(value: String) {
val bytes = value.encodeToByteArray()
encodeInt(bytes.size)
Expand All @@ -216,16 +216,16 @@ object Serialization {
override val serializersModule: SerializersModule = serializationModules
override fun decodeBoolean(): Boolean = input.read() != 0
override fun decodeByte(): Byte = input.read().toByte()
override fun decodeShort(): Short = Pack.int16BE(input.readNBytes(2))
override fun decodeInt(): Int = Pack.int32BE(input.readNBytes(4))
override fun decodeLong(): Long = Pack.int64BE(input.readNBytes(8))
override fun decodeShort(): Short = Pack.int16BE(input.readNBytes(2)!!)
override fun decodeInt(): Int = Pack.int32BE(input.readNBytes(4)!!)
override fun decodeLong(): Long = Pack.int64BE(input.readNBytes(8)!!)
override fun decodeFloat(): Float = TODO()
override fun decodeDouble(): Double = TODO()
override fun decodeChar(): Char = input.read().toChar()
override fun decodeString(): String {
val len = decodeInt()
require(len <= input.availableBytes)
val decoded = input.readNBytes(len).decodeToString()
val decoded = input.readNBytes(len)!!.decodeToString()
return decoded
}

Expand Down
2 changes: 1 addition & 1 deletion src/commonMain/kotlin/fr/acinq/lightning/utils/UUID.kt
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class UUID(val mostSignificantBits: Long, val leastSignificantBits: Long) : Comp

do {
--charPos
buf[charPos] = digits[work.toInt() and mask].toByte()
buf[charPos] = digits[work.toInt() and mask].code.toByte()
work = work ushr shift
} while (charPos > offset)
}
Expand Down
Loading

0 comments on commit d09db3d

Please sign in to comment.