Skip to content

Commit

Permalink
Patch: Remove Tor library so project can be compiled on Apple Silicon…
Browse files Browse the repository at this point in the history
… machines
  • Loading branch information
robbiehanson committed Oct 25, 2024
1 parent f41b409 commit dbb8bcf
Show file tree
Hide file tree
Showing 8 changed files with 7 additions and 198 deletions.
1 change: 0 additions & 1 deletion buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
object Versions {
const val lightningKmp = "1.8.4"
const val secp256k1 = "0.14.0"
const val torMobile = "0.2.0"

const val kotlin = "1.9.22"

Expand Down
3 changes: 1 addition & 2 deletions phoenix-shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ kotlin {
}
}

listOf(iosX64(), iosArm64()).forEach {
listOf(iosX64(), iosArm64(), iosSimulatorArm64()).forEach {
it.binaries {
framework {
optimized = false
Expand Down Expand Up @@ -89,7 +89,6 @@ kotlin {
dependencies {
// lightning-kmp
api("fr.acinq.lightning:lightning-kmp:${Versions.lightningKmp}")
api("fr.acinq.tor:tor-mobile-kmp:${Versions.torMobile}")
// ktor
implementation("io.ktor:ktor-client-core:${Versions.ktor}")
implementation("io.ktor:ktor-client-json:${Versions.ktor}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import fr.acinq.phoenix.db.createAppDbDriver
import fr.acinq.phoenix.managers.*
import fr.acinq.phoenix.utils.*
import fr.acinq.phoenix.utils.logger.PhoenixLoggerConfig
import fr.acinq.tor.Tor
import io.ktor.client.*
import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.serialization.kotlinx.json.*
Expand All @@ -59,12 +58,7 @@ class PhoenixBusiness(

private val tcpSocketBuilder = TcpSocket.Builder()
internal val tcpSocketBuilderFactory = suspend {
val isTorEnabled = appConfigurationManager.isTorEnabled.filterNotNull().first()
if (isTorEnabled) {
tcpSocketBuilder.torProxy(loggerFactory)
} else {
tcpSocketBuilder
}
tcpSocketBuilder
}

internal val httpClient by lazy {
Expand Down Expand Up @@ -97,7 +91,6 @@ class PhoenixBusiness(
val notificationsManager by lazy { NotificationsManager(this) }
val contactsManager by lazy { ContactsManager(this) }
val blockchainExplorer by lazy { BlockchainExplorer(chain) }
val tor by lazy { Tor(getApplicationCacheDirectoryPath(ctx), TorHelper.torLogger(loggerFactory)) }
val sendManager by lazy { SendManager(this) }

fun start(startupParams: StartupParams) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@ import fr.acinq.lightning.utils.Connection
import fr.acinq.lightning.utils.ServerAddress
import fr.acinq.phoenix.PhoenixBusiness
import fr.acinq.phoenix.data.ElectrumConfig
import fr.acinq.phoenix.utils.TorHelper.connectionState
import fr.acinq.lightning.logging.debug
import fr.acinq.lightning.logging.error
import fr.acinq.lightning.logging.info
import fr.acinq.tor.Tor
import fr.acinq.tor.TorState
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.ReceiveChannel
Expand All @@ -35,7 +32,6 @@ class AppConnectionsDaemon(
private val currencyManager: CurrencyManager,
private val networkMonitor: NetworkMonitor,
private val tcpSocketBuilder: suspend () -> TcpSocket.Builder,
private val tor: Tor,
private val electrumClient: ElectrumClient,
) : CoroutineScope by MainScope() {

Expand All @@ -47,15 +43,13 @@ class AppConnectionsDaemon(
currencyManager = business.currencyManager,
networkMonitor = business.networkMonitor,
tcpSocketBuilder = business.tcpSocketBuilderFactory,
tor = business.tor,
electrumClient = business.electrumClient
)

private val logger = loggerFactory.newLogger(this::class)

private var peerConnectionJob: Job? = null
private var electrumConnectionJob: Job? = null
private var torConnectionJob: Job? = null
private var httpControlFlowEnabled: Boolean = false

private data class TrafficControl(
Expand Down Expand Up @@ -193,51 +187,6 @@ class AppConnectionsDaemon(
}
}

// Tor state monitor
launch {
tor.state.collect {
val newValue = it == TorState.RUNNING
logger.debug { "torIsAvailable = $newValue" }
torControlChanges.send { copy(torIsAvailable = newValue) }
peerControlChanges.send { copy(torIsAvailable = newValue) }
electrumControlChanges.send { copy(torIsAvailable = newValue) }
httpApiControlChanges.send { copy(torIsAvailable = newValue) }
}
}

// Tor
launch {
torControlFlow.collect {
when {
it.internetIsAvailable && it.disconnectCount <= 0 && it.torIsEnabled -> {
if (torConnectionJob == null) {
logger.info { "starting tor" }
torConnectionJob = connectionLoop(
name = "Tor",
statusStateFlow = tor.state.connectionState(this),
) {
try {
tor.startInProperScope(this)
} catch (t: Throwable) {
logger.error(t) { "tor cannot be started: ${t.message}" }
}
}
}
}
else -> {
torConnectionJob?.let {
logger.info { "shutting down tor" }
it.cancel()
tor.stop()
torConnectionJob = null
// Tor runs it's own process, and needs time to shutdown before restarting.
delay(500)
}
}
}
}
}

// Peer
launch {
var configVersion = 0
Expand Down Expand Up @@ -522,6 +471,3 @@ class AppConnectionsDaemon(
}
}
}

/** The start function must run on a different dispatcher depending on the platform. */
expect suspend fun Tor.startInProperScope(scope: CoroutineScope)
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import fr.acinq.lightning.blockchain.electrum.ElectrumClient
import fr.acinq.lightning.logging.LoggerFactory
import fr.acinq.lightning.utils.Connection
import fr.acinq.phoenix.PhoenixBusiness
import fr.acinq.phoenix.utils.TorHelper.connectionState
import fr.acinq.tor.Tor
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import plus
Expand All @@ -30,17 +28,13 @@ class ConnectionsManager(
peerManager: PeerManager,
electrumClient: ElectrumClient,
networkMonitor: NetworkMonitor,
appConfigurationManager: AppConfigurationManager,
tor: Tor
): CoroutineScope {

constructor(business: PhoenixBusiness): this(
loggerFactory = business.loggerFactory,
peerManager = business.peerManager,
electrumClient = business.electrumClient,
networkMonitor = business.networkMonitor,
appConfigurationManager = business.appConfigurationManager,
tor = business.tor
networkMonitor = business.networkMonitor
)

val log = loggerFactory.newLogger(this::class)
Expand All @@ -52,19 +46,17 @@ class ConnectionsManager(
combine(
peer.connectionState,
electrumClient.connectionStatus,
networkMonitor.networkState,
appConfigurationManager.isTorEnabled.filterNotNull(),
tor.state.connectionState(this)
) { peerState, electrumStatus, internetState, torEnabled, torState ->
networkMonitor.networkState
) { peerState, electrumStatus, internetState ->
Connections(
peer = peerState,
electrum = electrumStatus.toConnectionState(),
internet = when (internetState) {
NetworkState.Available -> Connection.ESTABLISHED
NetworkState.NotAvailable -> Connection.CLOSED(reason = null)
},
tor = if (torEnabled) torState else Connection.CLOSED(reason = null),
torEnabled = torEnabled
tor = Connection.CLOSED(reason = null),
torEnabled = false
)
}
}.stateIn(
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit dbb8bcf

Please sign in to comment.