Skip to content

Commit

Permalink
fix: syncing issues and related crashes (#245)
Browse files Browse the repository at this point in the history
* fix: prevent NPE when processing qrinfo

* fix: prevent NPE in isSynced

* fix: prevent NPE when processing ISLocks after shutdown

* fix: use Sync Stage instead of a boolean flag
  • Loading branch information
HashEngineering authored Feb 9, 2024
1 parent 1ee16c0 commit 76b7d6c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 19 deletions.
19 changes: 10 additions & 9 deletions core/src/main/java/org/bitcoinj/evolution/AbstractQuorumState.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,16 @@ public abstract class AbstractQuorumState<Request extends AbstractQuorumRequest,
Context context;
DualBlockChain blockChain;
protected PeerGroup peerGroup;
//protected BlockStore headerStore;
//protected BlockStore blockStore;

QuorumUpdateRequest<Request> lastRequest;

static final long WAIT_GETMNLISTDIFF = 5;
Peer downloadPeer;
boolean waitingForMNListDiff;
boolean initChainTipSyncComplete = false;
/** is the Sync Stage on past HEADERS and MNLIST */
boolean initChainTipSyncComplete() {
return context.peerGroup != null && context.peerGroup.getSyncStage().value > PeerGroup.SyncStage.MNLIST.value;
}
BlockQueue pendingBlocks = new BlockQueue();

int failedAttempts;
Expand Down Expand Up @@ -169,7 +170,7 @@ public void setBlockChain(PeerGroup peerGroup, DualBlockChain blockChain) {
this.blockChain = blockChain;
if (peerGroup != null) {
this.peerGroup = peerGroup;
peerGroup.addMnListDownloadCompleteListener(() -> initChainTipSyncComplete = true, Threading.SAME_THREAD);
// peerGroup.addMnListDownloadCompleteListener(() -> initChainTipSyncComplete = true, Threading.SAME_THREAD);
}
}

Expand Down Expand Up @@ -407,9 +408,9 @@ void requestNextMNListDiff() {
waitingForMNListDiff = true;
} else {
log.info("there are no pending blocks to process");
if (!initChainTipSyncComplete) {
initChainTipSyncComplete = true;
}
//if (!initChainTipSyncComplete) {
// initChainTipSyncComplete = true;
//}
}
} else {
log.warn("downloadPeer is null, not requesting update");
Expand Down Expand Up @@ -555,7 +556,7 @@ public void removeEventListeners(AbstractBlockChain blockChain, PeerGroup peerGr
public final NewBestBlockListener newBestBlockListener = new NewBestBlockListener() {
@Override
public void notifyNewBestBlock(StoredBlock block) throws VerificationException {
boolean value = initChainTipSyncComplete || !context.masternodeSync.hasSyncFlag(MasternodeSync.SYNC_FLAGS.SYNC_HEADERS_MN_LIST_FIRST);
boolean value = initChainTipSyncComplete() || !context.masternodeSync.hasSyncFlag(MasternodeSync.SYNC_FLAGS.SYNC_HEADERS_MN_LIST_FIRST);
boolean needsUpdate = needsUpdate(block);
if (needsUpdate && value && getMasternodeListAtTip().getHeight() < block.getHeight() && isDeterministicMNsSporkActive() && stateManager.isLoadedFromFile()) {
long timePeriod = syncOptions == MasternodeListSyncOptions.SYNC_SNAPSHOT_PERIOD ? SNAPSHOT_TIME_PERIOD : MAX_CACHE_SIZE * 3 * 600L;
Expand Down Expand Up @@ -859,7 +860,7 @@ Sha256Hash getHashModifier(LLMQParameters llmqParams, StoredBlock quorumBaseBloc

public void close() {
// reset the state of any sync operation
initChainTipSyncComplete = false;
// initChainTipSyncComplete = false;
waitingForMNListDiff = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.bitcoinj.quorums.QuorumSnapshot;
import org.bitcoinj.quorums.SimplifiedQuorumList;
import org.bitcoinj.quorums.SnapshotSkipMode;
import org.bitcoinj.store.BlockStore;
import org.bitcoinj.store.BlockStoreException;
import org.bitcoinj.utils.Pair;
import org.bitcoinj.utils.Threading;
Expand Down Expand Up @@ -306,11 +305,12 @@ public void applyDiff(Peer peer, DualBlockChain blockChain,

// now calculate quorums, but do not validate them since they are all old
SimplifiedQuorumList baseQuorumList;
SimplifiedQuorumList newQuorumListAtHMinus4C = null;
SimplifiedQuorumList newQuorumListAtHMinus4C = new SimplifiedQuorumList(params);

if (quorumRotationInfo.hasExtraShare()) {
baseQuorumList = quorumsCache.get(quorumRotationInfo.getMnListDiffAtHMinus4C().prevBlockHash);
newQuorumListAtHMinus4C = baseQuorumList.applyDiff(quorumRotationInfo.getMnListDiffAtHMinus4C(), isLoadingBootStrap, blockChain, true, false);
if (baseQuorumList != null)
newQuorumListAtHMinus4C = baseQuorumList.applyDiff(quorumRotationInfo.getMnListDiffAtHMinus4C(), isLoadingBootStrap, blockChain, true, false);
}

baseQuorumList = quorumsCache.get(quorumRotationInfo.getMnListDiffAtHMinus3C().prevBlockHash);
Expand Down Expand Up @@ -450,6 +450,9 @@ public boolean isSynced() {
if(mnListAtH.getHeight() == -1)
return false;

if (context.peerGroup == null)
return false;

int mostCommonHeight = context.peerGroup.getMostCommonHeight();

// determine when the last QR height was
Expand Down Expand Up @@ -1301,11 +1304,6 @@ public void processDiff(@Nullable Peer peer, QuorumRotationInfo quorumRotationIn
log.info("processing qrinfo: Total: {} mnlistdiff: {}", watch, quorumRotationInfo.getMnListDiffTip());
log.info(toString());
waitingForMNListDiff = false;
if (!initChainTipSyncComplete) {
log.info("initChainTipSync=false");
initChainTipSyncComplete = true;
log.info("initChainTipSync=true");
}
requestNextMNListDiff();
lock.unlock();
}
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/org/bitcoinj/evolution/QuorumState.java
Original file line number Diff line number Diff line change
Expand Up @@ -329,10 +329,9 @@ public void processDiff(@Nullable Peer peer, SimplifiedMasternodeListDiff mnlist
watch.stop();
log.info("processing mnlistdiff times : Total: " + watch + "mnList: " + watchMNList + " quorums" + watchQuorums + "mnlistdiff" + mnlistdiff);
waitingForMNListDiff = false;
if (!initChainTipSyncComplete) {
if (!initChainTipSyncComplete()) {
log.info("initChainTipSync=false");
context.peerGroup.triggerMnListDownloadComplete();
initChainTipSyncComplete = true;
log.info("initChainTipSync=true");
}
requestNextMNListDiff();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.common.base.Stopwatch;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.bitcoinj.core.*;
import org.bitcoinj.core.listeners.NewBestBlockListener;
import org.bitcoinj.core.listeners.OnTransactionBroadcastListener;
Expand Down Expand Up @@ -541,6 +542,13 @@ HashSet<Sha256Hash> processPendingInstantSendLocks(LLMQParameters.LLMQType llmqT
blockIndex = blockChain.getBlockStore().get(islock.cycleHash);
} catch (BlockStoreException e) {
throw new RuntimeException(e);
} catch (NullPointerException e) {
// blockchain is now null?
if (isInitialized()) {
throw e;
} else {
return Sets.newHashSet();
}
}

if (blockIndex == null) {
Expand Down

0 comments on commit 76b7d6c

Please sign in to comment.