From 2b37fbd576a7ce8dfd1ece54d99313cb029af160 Mon Sep 17 00:00:00 2001 From: kubel Date: Fri, 9 Aug 2024 09:20:43 +0200 Subject: [PATCH] Add logs for subscribe request --- .../foundation/network/BaseRelayClient.kt | 132 +++++++++--------- .../com/walletconnect/foundation/RelayTest.kt | 7 +- 2 files changed, 75 insertions(+), 64 deletions(-) diff --git a/foundation/src/main/kotlin/com/walletconnect/foundation/network/BaseRelayClient.kt b/foundation/src/main/kotlin/com/walletconnect/foundation/network/BaseRelayClient.kt index d1d0e9f70..4181e8e92 100644 --- a/foundation/src/main/kotlin/com/walletconnect/foundation/network/BaseRelayClient.kt +++ b/foundation/src/main/kotlin/com/walletconnect/foundation/network/BaseRelayClient.kt @@ -36,15 +36,16 @@ import org.koin.core.KoinApplication @OptIn(ExperimentalCoroutinesApi::class) abstract class BaseRelayClient : RelayInterface { - private var foundationKoinApp: KoinApplication = KoinApplication.init() - lateinit var relayService: RelayService - protected var logger: Logger - private val resultState: MutableSharedFlow = MutableSharedFlow() - - init { - foundationKoinApp.run { modules(foundationCommonModule()) } - logger = foundationKoinApp.koin.get() - } + private var foundationKoinApp: KoinApplication = KoinApplication.init() + lateinit var relayService: RelayService + protected var logger: Logger + private val resultState: MutableSharedFlow = MutableSharedFlow() + var isLoggingEnabled: Boolean = false + + init { + foundationKoinApp.run { modules(foundationCommonModule()) } + logger = foundationKoinApp.koin.get() + } fun observeResults() { scope.launch { @@ -63,26 +64,26 @@ abstract class BaseRelayClient : RelayInterface { } } - override val eventsFlow: SharedFlow by lazy { - relayService - .observeWebSocketEvent() - .map { event -> event.toRelayEvent() } - .shareIn(scope, SharingStarted.Lazily, REPLAY) - } + override val eventsFlow: SharedFlow by lazy { + relayService + .observeWebSocketEvent() + .map { event -> event.toRelayEvent() } + .shareIn(scope, SharingStarted.Lazily, REPLAY) + } - override val subscriptionRequest: Flow by lazy { - relayService.observeSubscriptionRequest() - .map { request -> request.toRelay() } - .onEach { relayRequest -> supervisorScope { publishSubscriptionAcknowledgement(relayRequest.id) } } - } + override val subscriptionRequest: Flow by lazy { + relayService.observeSubscriptionRequest() + .map { request -> request.toRelay() } + .onEach { relayRequest -> supervisorScope { publishSubscriptionAcknowledgement(relayRequest.id) } } + } @ExperimentalCoroutinesApi override fun publish( - topic: String, - message: String, - params: Relay.Model.IrnParams, - id: Long?, - onResult: (Result) -> Unit, + topic: String, + message: String, + params: Relay.Model.IrnParams, + id: Long?, + onResult: (Result) -> Unit, ) { val (tag, ttl, prompt) = params val publishParams = RelayDTO.Publish.Request.Params(Topic(topic), message, Ttl(ttl), tag, prompt) @@ -97,15 +98,15 @@ abstract class BaseRelayClient : RelayInterface { try { withTimeout(RESULT_TIMEOUT) { resultState - .filterIsInstance() - .filter { relayResult -> relayResult.id == id } - .first { publishResult -> - when (publishResult) { - is RelayDTO.Publish.Result.Acknowledgement -> onResult(Result.success(publishResult.toRelay())) - is RelayDTO.Publish.Result.JsonRpcError -> onResult(Result.failure(Throwable(publishResult.error.errorMessage))) - } - true + .filterIsInstance() + .filter { relayResult -> relayResult.id == id } + .first { publishResult -> + when (publishResult) { + is RelayDTO.Publish.Result.Acknowledgement -> onResult(Result.success(publishResult.toRelay())) + is RelayDTO.Publish.Result.JsonRpcError -> onResult(Result.failure(Throwable(publishResult.error.errorMessage))) } + true + } } } catch (e: TimeoutCancellationException) { onResult(Result.failure(e)) @@ -121,6 +122,10 @@ abstract class BaseRelayClient : RelayInterface { override fun subscribe(topic: String, id: Long?, onResult: (Result) -> Unit) { val subscribeRequest = RelayDTO.Subscribe.Request(id = id ?: generateClientToServerId(), params = RelayDTO.Subscribe.Request.Params(Topic(topic))) + if (isLoggingEnabled) { + logger.log("Sending SubscribeRequest: $subscribeRequest") + } + observeSubscribeResult(subscribeRequest.id, onResult) relayService.subscribeRequest(subscribeRequest) } @@ -130,15 +135,16 @@ abstract class BaseRelayClient : RelayInterface { try { withTimeout(RESULT_TIMEOUT) { resultState - .filterIsInstance() - .filter { relayResult -> relayResult.id == id } - .first { subscribeResult -> - when (subscribeResult) { - is RelayDTO.Subscribe.Result.Acknowledgement -> onResult(Result.success(subscribeResult.toRelay())) - is RelayDTO.Subscribe.Result.JsonRpcError -> onResult(Result.failure(Throwable(subscribeResult.error.errorMessage))) - } - true + .filterIsInstance() + .onEach {relayResult -> if (isLoggingEnabled) logger.log("SubscribeResult: $relayResult") } + .filter { relayResult -> relayResult.id == id } + .first { subscribeResult -> + when (subscribeResult) { + is RelayDTO.Subscribe.Result.Acknowledgement -> onResult(Result.success(subscribeResult.toRelay())) + is RelayDTO.Subscribe.Result.JsonRpcError -> onResult(Result.failure(Throwable(subscribeResult.error.errorMessage))) } + true + } } } catch (e: TimeoutCancellationException) { onResult(Result.failure(e)) @@ -163,15 +169,15 @@ abstract class BaseRelayClient : RelayInterface { try { withTimeout(RESULT_TIMEOUT) { resultState - .filterIsInstance() - .filter { relayResult -> relayResult.id == id } - .first { batchSubscribeResult -> - when (batchSubscribeResult) { - is RelayDTO.BatchSubscribe.Result.Acknowledgement -> onResult(Result.success(batchSubscribeResult.toRelay())) - is RelayDTO.BatchSubscribe.Result.JsonRpcError -> onResult(Result.failure(Throwable(batchSubscribeResult.error.errorMessage))) - } - true + .filterIsInstance() + .filter { relayResult -> relayResult.id == id } + .first { batchSubscribeResult -> + when (batchSubscribeResult) { + is RelayDTO.BatchSubscribe.Result.Acknowledgement -> onResult(Result.success(batchSubscribeResult.toRelay())) + is RelayDTO.BatchSubscribe.Result.JsonRpcError -> onResult(Result.failure(Throwable(batchSubscribeResult.error.errorMessage))) } + true + } } } catch (e: TimeoutCancellationException) { onResult(Result.failure(e)) @@ -185,14 +191,14 @@ abstract class BaseRelayClient : RelayInterface { @ExperimentalCoroutinesApi override fun unsubscribe( - topic: String, - subscriptionId: String, - id: Long?, - onResult: (Result) -> Unit, + topic: String, + subscriptionId: String, + id: Long?, + onResult: (Result) -> Unit, ) { val unsubscribeRequest = RelayDTO.Unsubscribe.Request( - id = id ?: generateClientToServerId(), - params = RelayDTO.Unsubscribe.Request.Params(Topic(topic), SubscriptionId(subscriptionId)) + id = id ?: generateClientToServerId(), + params = RelayDTO.Unsubscribe.Request.Params(Topic(topic), SubscriptionId(subscriptionId)) ) observeUnsubscribeResult(unsubscribeRequest.id, onResult) @@ -204,15 +210,15 @@ abstract class BaseRelayClient : RelayInterface { try { withTimeout(RESULT_TIMEOUT) { resultState - .filterIsInstance() - .filter { relayResult -> relayResult.id == id } - .first { unsubscribeResult -> - when (unsubscribeResult) { - is RelayDTO.Unsubscribe.Result.Acknowledgement -> onResult(Result.success(unsubscribeResult.toRelay())) - is RelayDTO.Unsubscribe.Result.JsonRpcError -> onResult(Result.failure(Throwable(unsubscribeResult.error.errorMessage))) - } - true + .filterIsInstance() + .filter { relayResult -> relayResult.id == id } + .first { unsubscribeResult -> + when (unsubscribeResult) { + is RelayDTO.Unsubscribe.Result.Acknowledgement -> onResult(Result.success(unsubscribeResult.toRelay())) + is RelayDTO.Unsubscribe.Result.JsonRpcError -> onResult(Result.failure(Throwable(unsubscribeResult.error.errorMessage))) } + true + } } } catch (e: TimeoutCancellationException) { onResult(Result.failure(e)) diff --git a/foundation/src/test/kotlin/com/walletconnect/foundation/RelayTest.kt b/foundation/src/test/kotlin/com/walletconnect/foundation/RelayTest.kt index 65fd13cf1..ac9050b67 100644 --- a/foundation/src/test/kotlin/com/walletconnect/foundation/RelayTest.kt +++ b/foundation/src/test/kotlin/com/walletconnect/foundation/RelayTest.kt @@ -188,6 +188,9 @@ class RelayTest { startLoggingClientEventsFlow(clientA, "ClientA") startLoggingClientEventsFlow(clientB, "ClientB") + clientA.isLoggingEnabled = true + clientB.isLoggingEnabled = true + return (clientA to clientB) } @@ -220,7 +223,9 @@ class RelayTest { delay(10) } - if (didTimeout(start, 50000L)) { throw Exception("Unable to establish socket connection") } + if (didTimeout(start, 50000L)) { + throw Exception("Unable to establish socket connection") + } clientAJob.cancel() clientBJob.cancel()