mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-07-31 20:11:23 +00:00
Add unit tests to verify how current and previous block time and difficulty are extracted from blocks
This commit is contained in:
@@ -210,14 +210,25 @@ public abstract class AbstractDogecoinParams extends NetworkParameters implement
|
|||||||
|
|
||||||
Block blockIntervalAgo = cursor.getHeader();
|
Block blockIntervalAgo = cursor.getHeader();
|
||||||
long receivedTargetCompact = nextBlock.getDifficultyTarget();
|
long receivedTargetCompact = nextBlock.getDifficultyTarget();
|
||||||
long newTargetCompact = this.getNewDifficultyTarget(previousHeight, prev.getTimeSeconds(),
|
long newTargetCompact = this.getNewDifficultyTarget(previousHeight, prev, blockIntervalAgo);
|
||||||
prev.getDifficultyTarget(), blockIntervalAgo.getTimeSeconds());
|
|
||||||
|
|
||||||
if (newTargetCompact != receivedTargetCompact)
|
if (newTargetCompact != receivedTargetCompact)
|
||||||
throw new VerificationException("Network provided difficulty bits do not match what was calculated: " +
|
throw new VerificationException("Network provided difficulty bits do not match what was calculated: " +
|
||||||
newTargetCompact + " vs " + receivedTargetCompact);
|
newTargetCompact + " vs " + receivedTargetCompact);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param prev the block immediately before the retarget block.
|
||||||
|
* @param blockIntervalAgo The last retarget block.
|
||||||
|
* @return New difficulty target as compact bytes.
|
||||||
|
*/
|
||||||
|
public long getNewDifficultyTarget(int previousHeight, final Block prev,
|
||||||
|
final Block blockIntervalAgo) {
|
||||||
|
return this.getNewDifficultyTarget(previousHeight, prev.getTimeSeconds(),
|
||||||
|
blockIntervalAgo.getDifficultyTarget(), blockIntervalAgo.getTimeSeconds());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param previousHeight Height of the block immediately previous to the one we're calculating difficulty of.
|
* @param previousHeight Height of the block immediately previous to the one we're calculating difficulty of.
|
||||||
|
@@ -15,10 +15,14 @@
|
|||||||
*/
|
*/
|
||||||
package org.libdohj.params;
|
package org.libdohj.params;
|
||||||
|
|
||||||
import org.libdohj.params.AbstractDogecoinParams;
|
import java.io.IOException;
|
||||||
import org.libdohj.params.DogecoinMainNetParams;
|
import org.bitcoinj.core.AltcoinBlock;
|
||||||
|
import org.bitcoinj.core.Context;
|
||||||
|
import org.bitcoinj.core.Util;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.libdohj.core.AltcoinSerializer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@@ -27,6 +31,11 @@ import org.junit.Test;
|
|||||||
public class AbstractDogecoinParamsTest {
|
public class AbstractDogecoinParamsTest {
|
||||||
private static final AbstractDogecoinParams params = DogecoinMainNetParams.get();
|
private static final AbstractDogecoinParams params = DogecoinMainNetParams.get();
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() throws Exception {
|
||||||
|
Context context = new Context(params);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldCalculateBitcoinLikeDifficulty() {
|
public void shouldCalculateBitcoinLikeDifficulty() {
|
||||||
int previousHeight = 239;
|
int previousHeight = 239;
|
||||||
@@ -36,13 +45,6 @@ public class AbstractDogecoinParamsTest {
|
|||||||
long newDifficulty = params.getNewDifficultyTarget(previousHeight, previousBlockTime, lastRetargetDifficulty, lastRetargetTime);
|
long newDifficulty = params.getNewDifficultyTarget(previousHeight, previousBlockTime, lastRetargetDifficulty, lastRetargetTime);
|
||||||
assertEquals(newDifficulty, 0x1e00ffff);
|
assertEquals(newDifficulty, 0x1e00ffff);
|
||||||
|
|
||||||
previousHeight = 479;
|
|
||||||
previousBlockTime = 1386475840;
|
|
||||||
lastRetargetDifficulty = 0x1e0fffff;
|
|
||||||
lastRetargetTime = 1386475638; // Block 239
|
|
||||||
newDifficulty = params.getNewDifficultyTarget(previousHeight, previousBlockTime, lastRetargetDifficulty, lastRetargetTime);
|
|
||||||
assertEquals(newDifficulty, 0x1e00ffff);
|
|
||||||
|
|
||||||
previousHeight = 9599;
|
previousHeight = 9599;
|
||||||
previousBlockTime = 1386954113;
|
previousBlockTime = 1386954113;
|
||||||
lastRetargetDifficulty = 0x1c1a1206;
|
lastRetargetDifficulty = 0x1c1a1206;
|
||||||
@@ -71,4 +73,18 @@ public class AbstractDogecoinParamsTest {
|
|||||||
long newDifficulty = params.getNewDifficultyTarget(previousHeight, previousBlockTime, lastRetargetDifficulty, lastRetargetTime);
|
long newDifficulty = params.getNewDifficultyTarget(previousHeight, previousBlockTime, lastRetargetDifficulty, lastRetargetTime);
|
||||||
assertEquals(newDifficulty, 0x1b6558a4);
|
assertEquals(newDifficulty, 0x1b6558a4);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldCalculateFirstRetarget() 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;
|
||||||
|
|
||||||
|
payload = Util.getBytes(getClass().getResourceAsStream("dogecoin_block479.bin"));
|
||||||
|
block479 = (AltcoinBlock)serializer.makeBlock(payload);
|
||||||
|
|
||||||
|
assertEquals(0x1e00ffff, params.getNewDifficultyTarget(479, block239, block479));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
BIN
src/test/resources/org/libdohj/params/dogecoin_block239.bin
Normal file
BIN
src/test/resources/org/libdohj/params/dogecoin_block239.bin
Normal file
Binary file not shown.
BIN
src/test/resources/org/libdohj/params/dogecoin_block479.bin
Normal file
BIN
src/test/resources/org/libdohj/params/dogecoin_block479.bin
Normal file
Binary file not shown.
Reference in New Issue
Block a user