Add unit tests for the retarget at block 720.

This commit is contained in:
Ross Nicoll
2015-10-16 21:04:38 +01:00
parent 4cf2dec918
commit 4951fb7241
3 changed files with 40 additions and 6 deletions

View File

@@ -20,7 +20,6 @@ import java.io.ByteArrayOutputStream;
import java.math.BigInteger;
import org.bitcoinj.core.AltcoinBlock;
import org.libdohj.core.AltcoinNetworkParameters;
import org.bitcoinj.core.Block;
import org.bitcoinj.core.Coin;
import static org.bitcoinj.core.Coin.COIN;
@@ -92,6 +91,8 @@ public abstract class AbstractDogecoinParams extends NetworkParameters implement
protected final int diffChangeTarget;
protected Logger log = LoggerFactory.getLogger(AbstractDogecoinParams.class);
public static final int DOGECOIN_PROTOCOL_VERSION_AUXPOW = 70003;
public static final int DOGECOIN_PROTOCOL_VERSION_CURRENT = 70004;
public AbstractDogecoinParams(final int setDiffChangeTarget) {
super();
@@ -320,6 +321,20 @@ public abstract class AbstractDogecoinParams extends NetworkParameters implement
return new AltcoinSerializer(this, parseRetain);
}
@Override
public int getProtocolVersionNum(final ProtocolVersion version) {
switch (version) {
case PONG:
case BLOOM_FILTER:
return version.getBitcoinProtocolVersion();
case CURRENT:
return DOGECOIN_PROTOCOL_VERSION_CURRENT;
case MINIMUM:
default:
return DOGECOIN_PROTOCOL_VERSION_AUXPOW;
}
}
@Override
public boolean isAuxPoWBlockVersion(long version) {
return version >= BLOCK_MIN_VERSION_AUXPOW

View File

@@ -43,14 +43,28 @@ public class AbstractDogecoinParamsTest {
long lastRetargetDifficulty = 0x1e0ffff0;
long lastRetargetTime = 1386474927; // Block 1
long newDifficulty = params.getNewDifficultyTarget(previousHeight, previousBlockTime, lastRetargetDifficulty, lastRetargetTime);
assertEquals(newDifficulty, 0x1e00ffff);
assertEquals(0x1e00ffff, newDifficulty);
previousHeight = 9599;
previousBlockTime = 1386954113;
lastRetargetDifficulty = 0x1c1a1206;
lastRetargetTime = 1386942008; // Block 9359
newDifficulty = params.getNewDifficultyTarget(previousHeight, previousBlockTime, lastRetargetDifficulty, lastRetargetTime);
assertEquals(newDifficulty, 0x1c15ea59);
assertEquals(0x1c15ea59, newDifficulty);
}
/**
* Test block 720, where the time interval is below the minimum time interval
* (900 seconds).
*/
@Test
public void shouldConstrainActualTime() {
int previousHeight = 719;
long previousBlockTime = 1386476362; // Block 719
long lastRetargetDifficulty = 0x1e00ffff;
long lastRetargetTime = 1386475840; // Block 439
long newDifficulty = params.getNewDifficultyTarget(previousHeight, previousBlockTime, lastRetargetDifficulty, lastRetargetTime);
assertEquals(0x1d0ffff0, newDifficulty);
}
@Test
@@ -60,7 +74,7 @@ public class AbstractDogecoinParamsTest {
long lastRetargetDifficulty = 0x1b499dfd;
long lastRetargetTime = 1395094427;
long newDifficulty = params.getNewDifficultyTarget(previousHeight, previousBlockTime, lastRetargetDifficulty, lastRetargetTime);
assertEquals(newDifficulty, 0x1b671062);
assertEquals(0x1b671062, newDifficulty);
}
@Test
@@ -71,20 +85,25 @@ public class AbstractDogecoinParamsTest {
long lastRetargetDifficulty = 0x1b671062;
long lastRetargetTime = 1395094679;
long newDifficulty = params.getNewDifficultyTarget(previousHeight, previousBlockTime, lastRetargetDifficulty, lastRetargetTime);
assertEquals(newDifficulty, 0x1b6558a4);
assertEquals(0x1b6558a4, newDifficulty);
}
@Test
public void shouldCalculateFirstRetarget() throws IOException {
public void shouldCalculateRetarget() throws IOException {
// Do a more in-depth test for the first retarget
byte[] payload = Util.getBytes(getClass().getResourceAsStream("dogecoin_block239.bin"));
AltcoinSerializer serializer = (AltcoinSerializer)params.getDefaultSerializer();
final AltcoinBlock block239 = (AltcoinBlock)serializer.makeBlock(payload);
final AltcoinBlock block479;
final AltcoinBlock block719;
payload = Util.getBytes(getClass().getResourceAsStream("dogecoin_block479.bin"));
block479 = (AltcoinBlock)serializer.makeBlock(payload);
payload = Util.getBytes(getClass().getResourceAsStream("dogecoin_block719.bin"));
block719 = (AltcoinBlock)serializer.makeBlock(payload);
assertEquals(0x1e00ffff, params.getNewDifficultyTarget(479, block239, block479));
assertEquals(0x1d0ffff0, params.getNewDifficultyTarget(719, block479, block719));
}
}