Skip to content

Commit

Permalink
Merge pull request #266 from dashpay/bugfix-sync-stuck-33
Browse files Browse the repository at this point in the history
bugfix: sync stuck 33

* this only delayed the freeze on Android.
  • Loading branch information
HashEngineering authored Dec 13, 2024
2 parents c469dbc + 9ecad5c commit 7e0aab3
Show file tree
Hide file tree
Showing 6 changed files with 597 additions and 34 deletions.
39 changes: 21 additions & 18 deletions core/src/main/java/org/bitcoinj/core/DualBlockChain.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ public AbstractBlockChain getHeadersChain() {

public int getBlockHeight(Sha256Hash blockHash) {
try {
if (headersChain != null && headersChain.getBestChainHeight() > blockChain.getBestChainHeight()) {
return headersChain.getBlockStore().get(blockHash).getHeight();
} else return blockChain.getBlockStore().get(blockHash).getHeight();
return getLongestChain().getBlockStore().get(blockHash).getHeight();
} catch (BlockStoreException x) {
return -1;
}
Expand All @@ -60,7 +58,7 @@ public int getBestChainHeight() {
return height;
}

public StoredBlock getBlock(Sha256Hash blockHash) {
public StoredBlock getBlockOld(Sha256Hash blockHash) {
try {
StoredBlock block = blockChain.getBlockStore().get(blockHash);
if (block == null && headersChain != null) {
Expand All @@ -72,34 +70,39 @@ public StoredBlock getBlock(Sha256Hash blockHash) {
}
}

private AbstractBlockChain getLongestChain() {
return headersChain != null && headersChain.getBestChainHeight() > blockChain.getBestChainHeight() ?
headersChain : blockChain;
}

public StoredBlock getBlock(Sha256Hash blockHash) {
try {
AbstractBlockChain chain = getLongestChain();
return chain.getBlockStore().get(blockHash);
} catch (BlockStoreException e) {
throw new RuntimeException(e);
}
}

public StoredBlock getBlockAncestor(StoredBlock block, int height) {
try {
StoredBlock ancestor = block.getAncestor(blockChain.getBlockStore(), height);
if (ancestor == null && headersChain != null) {
ancestor = block.getAncestor(headersChain.getBlockStore(), height);
}
return ancestor;
AbstractBlockChain chain = getLongestChain();
return block.getAncestor(chain.getBlockStore(), height);
} catch (BlockStoreException e) {
throw new RuntimeException(e);
}
}

public StoredBlock getBlock(int height) {
try {
StoredBlock block = blockChain.getBlockStore().get(height);
if (block == null && headersChain != null) {
block = headersChain.getBlockStore().get(height);
}
return block;
AbstractBlockChain chain = getLongestChain();
return chain.getBlockStore().get(height);
} catch (BlockStoreException e) {
throw new RuntimeException(e);
}
}

public StoredBlock getChainHead() {
StoredBlock bestBlock = blockChain.getChainHead();
if (headersChain != null && headersChain.getBestChainHeight() > bestBlock.getHeight())
bestBlock = headersChain.getChainHead();
return bestBlock;
return getLongestChain().chainHead;
}
}
5 changes: 5 additions & 0 deletions core/src/main/java/org/bitcoinj/evolution/QuorumState.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,14 @@ public void applyDiff(Peer peer, DualBlockChain blockChain,

if (peer != null && isSyncingHeadersFirst) peer.queueMasternodeListDownloadedListeners(MasternodeListDownloadedListener.Stage.Processing, mnlistdiff);

Stopwatch mnWatch = Stopwatch.createStarted();
SimplifiedMasternodeList newMNList = mnList.applyDiff(mnlistdiff);
if(context.masternodeSync.hasVerifyFlag(MasternodeSync.VERIFY_FLAGS.MNLISTDIFF_MNLIST))
newMNList.verify(mnlistdiff.coinBaseTx, mnlistdiff, mnList);
mnWatch.stop();
if (peer != null && isSyncingHeadersFirst) peer.queueMasternodeListDownloadedListeners(MasternodeListDownloadedListener.Stage.ProcessedMasternodes, mnlistdiff);

Stopwatch qWatch = Stopwatch.createStarted();
SimplifiedQuorumList newQuorumList = quorumList;
if (mnlistdiff.coinBaseTx.getExtraPayloadObject().getVersion() >= SimplifiedMasternodeListManager.LLMQ_FORMAT_VERSION) {
newQuorumList = quorumList.applyDiff(mnlistdiff, isLoadingBootStrap, blockChain, false, true);
Expand All @@ -136,6 +139,8 @@ public void applyDiff(Peer peer, DualBlockChain blockChain,
} else {
quorumList.syncWithMasternodeList(newMNList);
}
qWatch.stop();
log.info("applyDiff processing times: mn {}; quorums: {}", mnWatch, qWatch);
if (peer != null && isSyncingHeadersFirst) peer.queueMasternodeListDownloadedListeners(MasternodeListDownloadedListener.Stage.ProcessedQuorums, mnlistdiff);

// save the current state, if both mnLists and quorums are both applied
Expand Down
10 changes: 7 additions & 3 deletions core/src/main/java/org/bitcoinj/quorums/ChainLocksHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -592,12 +592,16 @@ public void setBestChainLockBlockMock(Block bestChainLockBlock, int height) {
public void addCoinbaseChainLock(Sha256Hash blockHash, int ancestor, BLSSignature signature) {
try {
int height;
StoredBlock block = blockChain.getBlockStore().get(blockHash);
BlockStore blockStore = blockChain.getBlockStore();
if (block == null) {
StoredBlock block = null;
BlockStore blockStore = null;
if (headerChain != null) {
block = headerChain.getBlockStore().get(blockHash);
blockStore = headerChain.getBlockStore();
}
if (block == null) {
block = blockChain.getBlockStore().get(blockHash);
blockStore = blockChain.getBlockStore();
}
if (block != null) {
block = block.getAncestor(blockStore, block.getHeight() - ancestor);
if (block != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,28 +402,16 @@ public void syncWithMasternodeList(SimplifiedMasternodeList masternodeList) {
blockHash = masternodeList.getBlockHash();
}

private boolean checkCommitment(FinalCommitment commitment, StoredBlock prevBlock, SimplifiedMasternodeListManager manager,
private boolean checkCommitment(FinalCommitment commitment, StoredBlock quorumBlock, SimplifiedMasternodeListManager manager,
DualBlockChain chain, boolean validateQuorums) throws BlockStoreException
{
if (commitment.getVersion() == 0 || commitment.getVersion() > FinalCommitment.MAX_VERSION) {
throw new VerificationException("invalid quorum commitment version: " + commitment.getVersion());
}

StoredBlock quorumBlock = chain.getBlock(commitment.quorumHash);
if(quorumBlock == null)
throw new VerificationException("invalid quorum hash: " + commitment.quorumHash);

StoredBlock cursor = prevBlock;
while(cursor != null) {
if(cursor.getHeader().getHash().equals(quorumBlock.getHeader().getHash()))
break;
cursor = chain.getBlock(cursor.getHeader().getHash());
}

if(cursor == null)
throw new VerificationException("invalid quorum hash: " + commitment.quorumHash);


if (!params.getLlmqs().containsKey(LLMQParameters.LLMQType.fromValue(commitment.llmqType))) {
throw new VerificationException("invalid LLMQType: " + commitment.llmqType);
}
Expand Down
Loading

0 comments on commit 7e0aab3

Please sign in to comment.