3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-15 11:45:51 +00:00

AbstractBitcoinNetParams.checkDifficultyTransitions: More useful exception message when a way back to the last transition point cannot be found.

This commit is contained in:
Andreas Schildbach 2016-08-24 13:48:43 +02:00 committed by Andreas Schildbach
parent 7b0641e271
commit eccf14adeb

View File

@ -23,6 +23,7 @@ import java.util.concurrent.TimeUnit;
import org.bitcoinj.core.Block; import org.bitcoinj.core.Block;
import org.bitcoinj.core.Coin; import org.bitcoinj.core.Coin;
import org.bitcoinj.core.NetworkParameters; import org.bitcoinj.core.NetworkParameters;
import org.bitcoinj.core.Sha256Hash;
import org.bitcoinj.core.StoredBlock; import org.bitcoinj.core.StoredBlock;
import org.bitcoinj.core.Transaction; import org.bitcoinj.core.Transaction;
import org.bitcoinj.core.Utils; import org.bitcoinj.core.Utils;
@ -75,7 +76,7 @@ public abstract class AbstractBitcoinNetParams extends NetworkParameters {
@Override @Override
public void checkDifficultyTransitions(final StoredBlock storedPrev, final Block nextBlock, public void checkDifficultyTransitions(final StoredBlock storedPrev, final Block nextBlock,
final BlockStore blockStore) throws VerificationException, BlockStoreException { final BlockStore blockStore) throws VerificationException, BlockStoreException {
Block prev = storedPrev.getHeader(); final Block prev = storedPrev.getHeader();
// Is this supposed to be a difficulty transition point? // Is this supposed to be a difficulty transition point?
if (!isDifficultyTransitionPoint(storedPrev.getHeight())) { if (!isDifficultyTransitionPoint(storedPrev.getHeight())) {
@ -91,14 +92,17 @@ public abstract class AbstractBitcoinNetParams extends NetworkParameters {
// We need to find a block far back in the chain. It's OK that this is expensive because it only occurs every // We need to find a block far back in the chain. It's OK that this is expensive because it only occurs every
// two weeks after the initial block chain download. // two weeks after the initial block chain download.
final Stopwatch watch = Stopwatch.createStarted(); final Stopwatch watch = Stopwatch.createStarted();
StoredBlock cursor = blockStore.get(prev.getHash()); Sha256Hash hash = prev.getHash();
for (int i = 0; i < this.getInterval() - 1; i++) { StoredBlock cursor = null;
final int interval = this.getInterval();
for (int i = 0; i < interval; i++) {
cursor = blockStore.get(hash);
if (cursor == null) { if (cursor == null) {
// This should never happen. If it does, it means we are following an incorrect or busted chain. // This should never happen. If it does, it means we are following an incorrect or busted chain.
throw new VerificationException( throw new VerificationException(
"Difficulty transition point but we did not find a way back to the genesis block."); "Difficulty transition point but we did not find a way back to the last transition point. Not found: " + hash);
} }
cursor = blockStore.get(cursor.getHeader().getPrevBlockHash()); hash = cursor.getHeader().getPrevBlockHash();
} }
watch.stop(); watch.stop();
if (watch.elapsed(TimeUnit.MILLISECONDS) > 50) if (watch.elapsed(TimeUnit.MILLISECONDS) > 50)