3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-07 14:54:15 +00:00

Renamed proofOfWorkLimit to maxTarget along with get/set and minor refactorings.

* proofOfWorkLimit renamed to maxTarget to get it more inline with commonly used terminology (wiki, etc.)
* some misleading local variables renamed from 'difficulty' to 'target'
This commit is contained in:
Piotr Włodarek 2014-05-24 20:49:10 +02:00 committed by Mike Hearn
parent c08c68fc5e
commit 9c8d2cc600
9 changed files with 31 additions and 32 deletions

View File

@ -844,26 +844,26 @@ public abstract class AbstractBlockChain {
if (timespan > targetTimespan * 4)
timespan = targetTimespan * 4;
BigInteger newDifficulty = Utils.decodeCompactBits(prev.getDifficultyTarget());
newDifficulty = newDifficulty.multiply(BigInteger.valueOf(timespan));
newDifficulty = newDifficulty.divide(BigInteger.valueOf(targetTimespan));
BigInteger newTarget = Utils.decodeCompactBits(prev.getDifficultyTarget());
newTarget = newTarget.multiply(BigInteger.valueOf(timespan));
newTarget = newTarget.divide(BigInteger.valueOf(targetTimespan));
if (newDifficulty.compareTo(params.getProofOfWorkLimit()) > 0) {
log.info("Difficulty hit proof of work limit: {}", newDifficulty.toString(16));
newDifficulty = params.getProofOfWorkLimit();
if (newTarget.compareTo(params.getMaxTarget()) > 0) {
log.info("Difficulty hit proof of work limit: {}", newTarget.toString(16));
newTarget = params.getMaxTarget();
}
int accuracyBytes = (int) (nextBlock.getDifficultyTarget() >>> 24) - 3;
long receivedDifficultyCompact = nextBlock.getDifficultyTarget();
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);
newDifficulty = newDifficulty.and(mask);
long newDifficultyCompact = Utils.encodeCompactBits(newDifficulty);
newTarget = newTarget.and(mask);
long newTargetCompact = Utils.encodeCompactBits(newTarget);
if (newDifficultyCompact != receivedDifficultyCompact)
if (newTargetCompact != receivedTargetCompact)
throw new VerificationException("Network provided difficulty bits do not match what was calculated: " +
newDifficultyCompact + " vs " + receivedDifficultyCompact);
newTargetCompact + " vs " + receivedTargetCompact);
}
private void checkTestnetDifficulty(StoredBlock storedPrev, Block prev, Block next) throws VerificationException, BlockStoreException {
@ -880,11 +880,11 @@ public abstract class AbstractBlockChain {
StoredBlock cursor = storedPrev;
while (!cursor.getHeader().equals(params.getGenesisBlock()) &&
cursor.getHeight() % params.getInterval() != 0 &&
cursor.getHeader().getDifficultyTargetAsInteger().equals(params.getProofOfWorkLimit()))
cursor.getHeader().getDifficultyTargetAsInteger().equals(params.getMaxTarget()))
cursor = cursor.getPrev(blockStore);
BigInteger cursorDifficulty = cursor.getHeader().getDifficultyTargetAsInteger();
BigInteger newDifficulty = next.getDifficultyTargetAsInteger();
if (!cursorDifficulty.equals(newDifficulty))
BigInteger cursorTarget = cursor.getHeader().getDifficultyTargetAsInteger();
BigInteger newTarget = next.getDifficultyTargetAsInteger();
if (!cursorTarget.equals(newTarget))
throw new VerificationException("Testnet block transition that is not allowed: " +
Long.toHexString(cursor.getHeader().getDifficultyTarget()) + " vs " +
Long.toHexString(next.getDifficultyTarget()));

View File

@ -627,7 +627,7 @@ public class Block extends Message {
public BigInteger getDifficultyTargetAsInteger() throws VerificationException {
maybeParseHeader();
BigInteger target = Utils.decodeCompactBits(difficultyTarget);
if (target.signum() <= 0 || target.compareTo(params.proofOfWorkLimit) > 0)
if (target.signum() <= 0 || target.compareTo(params.maxTarget) > 0)
throw new VerificationException("Difficulty target is bad: " + target.toString());
return target;
}

View File

@ -65,7 +65,7 @@ public abstract class NetworkParameters implements Serializable {
// TODO: Seed nodes should be here as well.
protected Block genesisBlock;
protected BigInteger proofOfWorkLimit;
protected BigInteger maxTarget;
protected int port;
protected long packetMagic;
protected int addressHeader;
@ -324,9 +324,9 @@ public abstract class NetworkParameters implements Serializable {
return interval;
}
/** What the easiest allowable proof of work should be. */
public BigInteger getProofOfWorkLimit() {
return proofOfWorkLimit;
/** Maximum target represents the easiest allowable proof of work. */
public BigInteger getMaxTarget() {
return maxTarget;
}
/**

View File

@ -30,7 +30,7 @@ public class MainNetParams extends NetworkParameters {
super();
interval = INTERVAL;
targetTimespan = TARGET_TIMESPAN;
proofOfWorkLimit = Utils.decodeCompactBits(0x1d00ffffL);
maxTarget = Utils.decodeCompactBits(0x1d00ffffL);
dumpedPrivateKeyHeader = 128;
addressHeader = 0;
p2shHeader = 5;

View File

@ -26,12 +26,12 @@ import static com.google.common.base.Preconditions.checkState;
* Network parameters for the regression test mode of bitcoind in which all blocks are trivially solvable.
*/
public class RegTestParams extends TestNet2Params {
private static final BigInteger PROOF_OF_WORK_LIMIT = new BigInteger("7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16);
private static final BigInteger MAX_TARGET = new BigInteger("7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16);
public RegTestParams() {
super();
interval = 10000;
proofOfWorkLimit = PROOF_OF_WORK_LIMIT;
maxTarget = MAX_TARGET;
subsidyDecreaseBlockCount = 150;
port = 18444;
}

View File

@ -36,7 +36,7 @@ public class TestNet2Params extends NetworkParameters {
acceptableAddressCodes = new int[] { addressHeader, p2shHeader };
interval = INTERVAL;
targetTimespan = TARGET_TIMESPAN;
proofOfWorkLimit = Utils.decodeCompactBits(0x1d0fffffL);
maxTarget = Utils.decodeCompactBits(0x1d0fffffL);
dumpedPrivateKeyHeader = 239;
genesisBlock.setTime(1296688602L);
genesisBlock.setDifficultyTarget(0x1d07fff8L);

View File

@ -34,7 +34,7 @@ public class TestNet3Params extends NetworkParameters {
packetMagic = 0x0b110907;
interval = INTERVAL;
targetTimespan = TARGET_TIMESPAN;
proofOfWorkLimit = Utils.decodeCompactBits(0x1d00ffffL);
maxTarget = Utils.decodeCompactBits(0x1d00ffffL);
port = 18333;
addressHeader = 111;
p2shHeader = 196;

View File

@ -33,7 +33,7 @@ public class UnitTestParams extends NetworkParameters {
addressHeader = 111;
p2shHeader = 196;
acceptableAddressCodes = new int[] { addressHeader, p2shHeader };
proofOfWorkLimit = new BigInteger("00ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16);
maxTarget = new BigInteger("00ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16);
genesisBlock.setTime(System.currentTimeMillis() / 1000);
genesisBlock.setDifficultyTarget(Block.EASIEST_DIFFICULTY_TARGET);
genesisBlock.solve();

View File

@ -51,8 +51,8 @@ public class BlockChainTest {
private Transaction coinbaseTransaction;
private static class TweakableTestNet2Params extends TestNet2Params {
public void setProofOfWorkLimit(BigInteger limit) {
proofOfWorkLimit = limit;
public void setMaxTarget(BigInteger limit) {
maxTarget = limit;
}
}
private static final TweakableTestNet2Params testNet = new TweakableTestNet2Params();
@ -228,9 +228,8 @@ public class BlockChainTest {
}
// Accept any level of difficulty now.
BigInteger oldVal = testNet.getProofOfWorkLimit();
testNet.setProofOfWorkLimit(new BigInteger
("00ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16));
BigInteger oldVal = testNet.getMaxTarget();
testNet.setMaxTarget(new BigInteger("00ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16));
try {
testNetChain.add(bad);
// We should not get here as the difficulty target should not be changing at this point.
@ -238,7 +237,7 @@ public class BlockChainTest {
} catch (VerificationException e) {
assertTrue(e.getMessage(), e.getCause().getMessage().contains("Unexpected change in difficulty"));
}
testNet.setProofOfWorkLimit(oldVal);
testNet.setMaxTarget(oldVal);
// TODO: Test difficulty change is not out of range when a transition period becomes valid.
}