Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
Comment on lines +73 to +76
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change, which uses the longer of the two chains, allowed for syncing to get a bit further when there was anuse with a thread freezing on Android:

  1. headers - only used to sync headers
  2. blocks - used to sync blocks and transactions

This change only delayed when the freeze happened, but this way, there is a consistant approach.


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
Loading