Skip to content

Commit

Permalink
fix: eliminate race condition when updating BlockchainState that resu…
Browse files Browse the repository at this point in the history
…lts in "Syncing..." for a long time
  • Loading branch information
HashEngineering committed Dec 16, 2023
1 parent 3b0d1ea commit ec08533
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@ public void run() {
protected void progress(double pct, int blocksLeft, Date date) {
super.progress(pct, blocksLeft, date);
syncPercentage = pct > 0.0 ? (int)pct : 0;
log.info("progress {}", syncPercentage);
if (syncPercentage > 100) {
syncPercentage = 100;
}
Expand All @@ -513,9 +514,11 @@ protected void progress(double pct, int blocksLeft, Date date) {
@Override
protected void doneDownload() {
super.doneDownload();
updateBlockchainState();
log.info("DoneDownload {}", syncPercentage);
// if the chain is already synced from a previous session, then syncPercentage = 0
// set to 100% so that observers will see that sync is completed
syncPercentage = 100;
setBlockchainDownloaded();
updateBlockchainState();
}

@Override
Expand Down Expand Up @@ -1169,10 +1172,6 @@ private void updateBlockchainState() {
blockchainStateDataProvider.updateBlockchainState(blockChain, impediments, percentageSync());
}

public void setBlockchainDownloaded() {
blockchainStateDataProvider.setBlockchainDownloaded();
}

@Override
public List<Peer> getConnectedPeers() {
if (peerGroup != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import dagger.hilt.android.qualifiers.ApplicationContext
import de.schildbach.wallet.Constants
import de.schildbach.wallet.database.dao.BlockchainStateDao
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.launch
Expand All @@ -39,10 +39,12 @@ import org.dash.wallet.common.WalletDataProvider
import org.dash.wallet.common.data.entity.BlockchainState
import org.dash.wallet.common.data.entity.BlockchainState.Impediment
import org.dash.wallet.common.services.BlockchainStateProvider
import org.slf4j.LoggerFactory
import java.io.IOException
import java.io.InputStream
import java.math.BigInteger
import java.util.EnumSet
import java.util.concurrent.Executors
import javax.inject.Inject
import kotlin.math.min

Expand Down Expand Up @@ -74,7 +76,8 @@ class BlockchainStateDataProvider @Inject constructor(
const val MASTERNODE_COUNT = 3800
}

private val coroutineScope = CoroutineScope(Dispatchers.IO)
// this coroutineScope should execute all jobs sequentially
private val coroutineScope = CoroutineScope(Executors.newSingleThreadExecutor().asCoroutineDispatcher())

override suspend fun getState(): BlockchainState? {
return blockchainStateDao.getState()
Expand All @@ -95,8 +98,8 @@ class BlockchainStateDataProvider @Inject constructor(
}
}

fun updateBlockchainState(blockChain: BlockChain, impediments: Set<Impediment>, percentageSync: Int) {
coroutineScope.launch {
fun updateBlockchainState(blockChain: BlockChain, impediments: Set<Impediment>, percentageSync: Int) {
coroutineScope.launch {
var blockchainState = blockchainStateDao.getState()
if (blockchainState == null) {
blockchainState = BlockchainState()
Expand All @@ -115,16 +118,6 @@ class BlockchainStateDataProvider @Inject constructor(
}
}

fun setBlockchainDownloaded() {
coroutineScope.launch {
val blockchainState = blockchainStateDao.getState()
if (blockchainState != null && blockchainState.percentageSync != 100) {
blockchainState.percentageSync = 100
blockchainStateDao.saveState(blockchainState)
}
}
}

fun resetBlockchainState() {
coroutineScope.launch {
blockchainStateDao.saveState(
Expand Down

0 comments on commit ec08533

Please sign in to comment.