Commit 08fe07b6 authored by bitcoinj-sv's avatar bitcoinj-sv

able to compile

parent 34521106
......@@ -538,6 +538,25 @@ public abstract class NetworkParameters {
return verifyFlags;
}
public void verifyDifficulty(BigInteger newTarget, Block nextBlock)
{
if (newTarget.compareTo(this.getMaxTarget()) > 0) {
newTarget = this.getMaxTarget();
}
int accuracyBytes = (int) (nextBlock.getDifficultyTarget() >>> 24) - 3;
long receivedTargetCompact = nextBlock.getDifficultyTarget();
// The calculated difficulty is to a higher precision than received, so reduce here.
BigInteger mask = BigInteger.valueOf(0xFFFFFFL).shiftLeft(accuracyBytes * 8);
newTarget = newTarget.and(mask);
long newTargetCompact = Utils.encodeCompactBits(newTarget);
if (newTargetCompact != receivedTargetCompact)
throw new VerificationException("Network provided difficulty bits do not match what was calculated: " +
Long.toHexString(newTargetCompact) + " vs " + Long.toHexString(receivedTargetCompact));
}
public abstract int getProtocolVersionNum(final ProtocolVersion version);
public static enum ProtocolVersion {
......
......@@ -20,6 +20,7 @@ package org.bitcoinj.params;
import java.math.BigInteger;
import java.util.concurrent.TimeUnit;
import com.google.common.base.Preconditions;
import org.bitcoinj.core.*;
import org.bitcoinj.utils.MonetaryFormat;
import org.bitcoinj.store.BlockStore;
......@@ -33,6 +34,11 @@ import com.google.common.base.Stopwatch;
* Parameters for Bitcoin-like networks.
*/
public abstract class AbstractBitcoinNetParams extends NetworkParameters {
/**
* The number that is one greater than the largest representable SHA-256
* hash.
*/
private static BigInteger LARGEST_HASH = BigInteger.ONE.shiftLeft(256);
/**
* Checks if we are at a difficulty transition point.
* @param storedPrev The previous stored block
......@@ -91,6 +97,43 @@ public abstract class AbstractBitcoinNetParams extends NetworkParameters {
return ((storedPrev.getHeight() + 1) % this.getInterval()) == 0;
}
/**
* Compute the a target based on the work done between 2 blocks and the time
* required to produce that work.
*/
public static BigInteger ComputeTarget(StoredBlock pindexFirst,
StoredBlock pindexLast) {
Preconditions.checkState(pindexLast.getHeight() > pindexFirst.getHeight());
/*
* From the total work done and the time it took to produce that much work,
* we can deduce how much work we expect to be produced in the targeted time
* between blocks.
*/
BigInteger work = pindexLast.getChainWork().subtract(pindexFirst.getChainWork());
work = work.multiply(BigInteger.valueOf(TARGET_SPACING));
// In order to avoid difficulty cliffs, we bound the amplitude of the
// adjustement we are going to do.
//assert(pindexLast->nTime > pindexFirst->nTime);
long nActualTimespan = pindexLast.getHeader().getTimeSeconds() - pindexFirst.getHeader().getTimeSeconds();
if (nActualTimespan > 288 * TARGET_SPACING) {
nActualTimespan = 288 * TARGET_SPACING;
} else if (nActualTimespan < 72 * TARGET_SPACING) {
nActualTimespan = 72 * TARGET_SPACING;
}
work = work.divide(BigInteger.valueOf(nActualTimespan));
/*
* We need to compute T = (2^256 / W) - 1 but 2^256 doesn't fit in 256 bits.
* By expressing 1 as W / W, we get (2^256 - W) / W, and we can compute
* 2^256 - W as the complement of W.
*/
return LARGEST_HASH.divide(work).subtract(BigInteger.ONE);//target.add(BigInteger.ONE))
}
@Override
public Coin getMaxMoney() {
return MAX_MONEY;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment