From 3759e53272d918000ed37c38fe814272ee505ed1 Mon Sep 17 00:00:00 2001 From: abdrasulov Date: Tue, 3 Feb 2026 18:31:17 +0600 Subject: [PATCH 1/3] Add pause/resume methods --- .../ethereumkit/api/core/ApiRpcSyncer.kt | 10 ++++++++- .../ethereumkit/api/core/Interfaces.kt | 2 ++ .../ethereumkit/api/core/RpcBlockchain.kt | 8 +++++++ .../api/core/WebSocketRpcSyncer.kt | 4 ++++ .../ethereumkit/core/EthereumKit.kt | 8 +++++++ .../ethereumkit/core/Interfaces.kt | 2 ++ .../ethereumkit/spv/core/SpvBlockchain.kt | 22 +++++++++++++++++-- 7 files changed, 53 insertions(+), 3 deletions(-) diff --git a/ethereumkit/src/main/java/io/horizontalsystems/ethereumkit/api/core/ApiRpcSyncer.kt b/ethereumkit/src/main/java/io/horizontalsystems/ethereumkit/api/core/ApiRpcSyncer.kt index f092deba..1be202d8 100644 --- a/ethereumkit/src/main/java/io/horizontalsystems/ethereumkit/api/core/ApiRpcSyncer.kt +++ b/ethereumkit/src/main/java/io/horizontalsystems/ethereumkit/api/core/ApiRpcSyncer.kt @@ -7,7 +7,7 @@ import io.horizontalsystems.ethereumkit.network.ConnectionManager import io.reactivex.Single import io.reactivex.disposables.CompositeDisposable import io.reactivex.schedulers.Schedulers -import java.util.* +import java.util.Timer import kotlin.concurrent.schedule class ApiRpcSyncer( @@ -52,6 +52,14 @@ class ApiRpcSyncer( stopTimer() } + override fun pause() { + stopTimer() + } + + override fun resume() { + startTimer() + } + override fun single(rpc: JsonRpc): Single = rpcApiProvider.single(rpc) //endregion diff --git a/ethereumkit/src/main/java/io/horizontalsystems/ethereumkit/api/core/Interfaces.kt b/ethereumkit/src/main/java/io/horizontalsystems/ethereumkit/api/core/Interfaces.kt index fba99fde..6c1946b2 100644 --- a/ethereumkit/src/main/java/io/horizontalsystems/ethereumkit/api/core/Interfaces.kt +++ b/ethereumkit/src/main/java/io/horizontalsystems/ethereumkit/api/core/Interfaces.kt @@ -58,6 +58,8 @@ interface IRpcSyncer { fun start() fun stop() + fun pause() + fun resume() fun single(rpc: JsonRpc): Single } diff --git a/ethereumkit/src/main/java/io/horizontalsystems/ethereumkit/api/core/RpcBlockchain.kt b/ethereumkit/src/main/java/io/horizontalsystems/ethereumkit/api/core/RpcBlockchain.kt index dad3cc26..23b20296 100644 --- a/ethereumkit/src/main/java/io/horizontalsystems/ethereumkit/api/core/RpcBlockchain.kt +++ b/ethereumkit/src/main/java/io/horizontalsystems/ethereumkit/api/core/RpcBlockchain.kt @@ -133,6 +133,14 @@ class RpcBlockchain( syncer.stop() } + override fun pause() { + syncer.pause() + } + + override fun resume() { + syncer.resume() + } + override fun send(rawTransaction: RawTransaction, signature: Signature): Single { val transaction = transactionBuilder.transaction(rawTransaction, signature) val encoded = transactionBuilder.encode(rawTransaction, signature) diff --git a/ethereumkit/src/main/java/io/horizontalsystems/ethereumkit/api/core/WebSocketRpcSyncer.kt b/ethereumkit/src/main/java/io/horizontalsystems/ethereumkit/api/core/WebSocketRpcSyncer.kt index 5d8ec1d7..7f1af4fa 100644 --- a/ethereumkit/src/main/java/io/horizontalsystems/ethereumkit/api/core/WebSocketRpcSyncer.kt +++ b/ethereumkit/src/main/java/io/horizontalsystems/ethereumkit/api/core/WebSocketRpcSyncer.kt @@ -46,6 +46,10 @@ class WebSocketRpcSyncer( rpcSocket.stop() } + override fun pause() = Unit + + override fun resume() = Unit + override fun single(rpc: JsonRpc): Single { return Single.create { emitter -> send( diff --git a/ethereumkit/src/main/java/io/horizontalsystems/ethereumkit/core/EthereumKit.kt b/ethereumkit/src/main/java/io/horizontalsystems/ethereumkit/core/EthereumKit.kt index a5a58ca4..8ec0833f 100644 --- a/ethereumkit/src/main/java/io/horizontalsystems/ethereumkit/core/EthereumKit.kt +++ b/ethereumkit/src/main/java/io/horizontalsystems/ethereumkit/core/EthereumKit.kt @@ -148,6 +148,14 @@ class EthereumKit( connectionManager.stop() } + fun onEnterForeground() { + blockchain.resume() + } + + fun onEnterBackground() { + blockchain.pause() + } + fun refresh() { blockchain.refresh() transactionSyncManager.sync() diff --git a/ethereumkit/src/main/java/io/horizontalsystems/ethereumkit/core/Interfaces.kt b/ethereumkit/src/main/java/io/horizontalsystems/ethereumkit/core/Interfaces.kt index 04718d1c..43878ef4 100644 --- a/ethereumkit/src/main/java/io/horizontalsystems/ethereumkit/core/Interfaces.kt +++ b/ethereumkit/src/main/java/io/horizontalsystems/ethereumkit/core/Interfaces.kt @@ -53,6 +53,8 @@ interface IBlockchain { fun start() fun refresh() fun stop() + fun pause() + fun resume() fun syncAccountState() val syncState: EthereumKit.SyncState diff --git a/ethereumkit/src/main/java/io/horizontalsystems/ethereumkit/spv/core/SpvBlockchain.kt b/ethereumkit/src/main/java/io/horizontalsystems/ethereumkit/spv/core/SpvBlockchain.kt index 4a539516..e2a33a18 100644 --- a/ethereumkit/src/main/java/io/horizontalsystems/ethereumkit/spv/core/SpvBlockchain.kt +++ b/ethereumkit/src/main/java/io/horizontalsystems/ethereumkit/spv/core/SpvBlockchain.kt @@ -13,7 +13,13 @@ import io.horizontalsystems.ethereumkit.core.IBlockchainListener import io.horizontalsystems.ethereumkit.core.ISpvStorage import io.horizontalsystems.ethereumkit.core.TransactionBuilder import io.horizontalsystems.ethereumkit.crypto.ECKey -import io.horizontalsystems.ethereumkit.models.* +import io.horizontalsystems.ethereumkit.models.Address +import io.horizontalsystems.ethereumkit.models.DefaultBlockParameter +import io.horizontalsystems.ethereumkit.models.GasPrice +import io.horizontalsystems.ethereumkit.models.RawTransaction +import io.horizontalsystems.ethereumkit.models.Signature +import io.horizontalsystems.ethereumkit.models.Transaction +import io.horizontalsystems.ethereumkit.models.TransactionLog import io.horizontalsystems.ethereumkit.network.INetwork import io.horizontalsystems.ethereumkit.spv.helpers.RandomHelper import io.horizontalsystems.ethereumkit.spv.models.AccountStateSpv @@ -22,7 +28,11 @@ import io.horizontalsystems.ethereumkit.spv.net.BlockHelper import io.horizontalsystems.ethereumkit.spv.net.BlockValidator import io.horizontalsystems.ethereumkit.spv.net.PeerGroup import io.horizontalsystems.ethereumkit.spv.net.PeerProvider -import io.horizontalsystems.ethereumkit.spv.net.handlers.* +import io.horizontalsystems.ethereumkit.spv.net.handlers.AccountStateTaskHandler +import io.horizontalsystems.ethereumkit.spv.net.handlers.AnnouncedBlockHandler +import io.horizontalsystems.ethereumkit.spv.net.handlers.BlockHeadersTaskHandler +import io.horizontalsystems.ethereumkit.spv.net.handlers.HandshakeTaskHandler +import io.horizontalsystems.ethereumkit.spv.net.handlers.SendTransactionTaskHandler import io.horizontalsystems.ethereumkit.spv.net.tasks.HandshakeTask import io.reactivex.Single import io.reactivex.subjects.PublishSubject @@ -61,6 +71,14 @@ class SpvBlockchain( TODO("not implemented") } + override fun pause() { + TODO("Not yet implemented") + } + + override fun resume() { + TODO("Not yet implemented") + } + override fun refresh() { TODO("not implemented") } From 07d2f61364be518ecf4c8d3d8e2c71d106aed047 Mon Sep 17 00:00:00 2001 From: abdrasulov Date: Wed, 4 Feb 2026 11:22:24 +0600 Subject: [PATCH 2/3] Keep only one timer running --- .../io/horizontalsystems/ethereumkit/api/core/ApiRpcSyncer.kt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ethereumkit/src/main/java/io/horizontalsystems/ethereumkit/api/core/ApiRpcSyncer.kt b/ethereumkit/src/main/java/io/horizontalsystems/ethereumkit/api/core/ApiRpcSyncer.kt index 1be202d8..be93c8a8 100644 --- a/ethereumkit/src/main/java/io/horizontalsystems/ethereumkit/api/core/ApiRpcSyncer.kt +++ b/ethereumkit/src/main/java/io/horizontalsystems/ethereumkit/api/core/ApiRpcSyncer.kt @@ -77,6 +77,8 @@ class ApiRpcSyncer( } private fun startTimer() { + if (timer != null) return + timer = Timer().apply { schedule(0, syncInterval * 1000) { onFireTimer() From 980c207891628c3724bbaaf8591d157c086e035c Mon Sep 17 00:00:00 2001 From: abdrasulov Date: Wed, 4 Feb 2026 12:38:58 +0600 Subject: [PATCH 3/3] Leave state as ready on error Getting block is a periodical task. Even if it faces error it will run after. --- .../horizontalsystems/ethereumkit/api/core/ApiRpcSyncer.kt | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/ethereumkit/src/main/java/io/horizontalsystems/ethereumkit/api/core/ApiRpcSyncer.kt b/ethereumkit/src/main/java/io/horizontalsystems/ethereumkit/api/core/ApiRpcSyncer.kt index be93c8a8..51dec12c 100644 --- a/ethereumkit/src/main/java/io/horizontalsystems/ethereumkit/api/core/ApiRpcSyncer.kt +++ b/ethereumkit/src/main/java/io/horizontalsystems/ethereumkit/api/core/ApiRpcSyncer.kt @@ -95,11 +95,9 @@ class ApiRpcSyncer( rpcApiProvider.single(BlockNumberJsonRpc()) .subscribeOn(Schedulers.io()) .observeOn(Schedulers.io()) - .subscribe({ lastBlockNumber -> + .subscribe { lastBlockNumber -> listener?.didUpdateLastBlockHeight(lastBlockNumber) - }, { - state = SyncerState.NotReady(it) - }).let { + }.let { disposables.add(it) } }