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..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 @@ -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 @@ -69,6 +77,8 @@ class ApiRpcSyncer( } private fun startTimer() { + if (timer != null) return + timer = Timer().apply { schedule(0, syncInterval * 1000) { onFireTimer() @@ -85,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) } } 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") }