ETCFork3M extends Eip160HFConfig { @Override public BigInteger calcDifficulty(BlockHeader curBlock, BlockHeader parent) { BigInteger pd = parent.getDifficultyBI(); BigInteger quotient = pd.divide(getConstants().getDIFFICULTY_BOUND_DIVISOR()); BigInteger sign = getCalcDifficultyMultiplier(curBlock, parent); BigInteger fromParent = pd.add(quotient.multiply(sign)); BigInteger difficulty = max(getConstants().getMINIMUM_DIFFICULTY(), fromParent); int explosion = getExplosion(curBlock, parent); if (explosion >= 0) { difficulty = max(getConstants().getMINIMUM_DIFFICULTY(), difficulty.add(BigInteger.ONE.shiftLeft(explosion))); } return difficulty; } ETCFork3M(BlockchainConfig parent); @Override Integer getChainId(); @Override boolean eip161(); @Override BigInteger calcDifficulty(BlockHeader curBlock, BlockHeader parent); @Override BigInteger getCalcDifficultyMultiplier(BlockHeader curBlock, BlockHeader parent);  }
@Test public void testDifficultyWithoutExplosion() throws Exception { ETCFork3M etcFork3M = new ETCFork3M(new TestBlockchainConfig()); BlockHeader parent = new BlockHeaderBuilder(new byte[]{11, 12}, 0L, 1_000_000).build(); BlockHeader current = new BlockHeaderBuilder(parent.getHash(), 1L, -1).build(); BigInteger difficulty = etcFork3M.calcDifficulty(current, parent); assertEquals(BigInteger.valueOf(269435944), difficulty); }
@Test public void testDifficultyWithExplosionShouldBeImpactedByBlockTimestamp() throws Exception { ETCFork3M etcFork3M = new ETCFork3M(new TestBlockchainConfig()); BlockHeader parent = new BlockHeaderBuilder(new byte[]{11, 12}, 2_500_000, 8_388_608) .withTimestamp(0) .build(); BlockHeader current = new BlockHeaderBuilder(parent.getHash(), 2_500_001, -1) .withTimestamp(10 * 60) .build(); BigInteger difficulty = etcFork3M.calcDifficulty(current, parent); assertEquals(BigInteger.valueOf(276582400), difficulty); parent = new BlockHeaderBuilder(new byte[]{11, 12}, 2_500_000, 8_388_608) .withTimestamp(0) .build(); current = new BlockHeaderBuilder(parent.getHash(), 2_500_001, -1) .withTimestamp(5) .build(); difficulty = etcFork3M.calcDifficulty(current, parent); assertEquals(BigInteger.valueOf(276828160), difficulty); }
@Test public void testDifficultyAboveBlock5MShouldTriggerExplosion() throws Exception { ETCFork3M etcFork3M = new ETCFork3M(new TestBlockchainConfig()); BlockHeader parent = new BlockHeaderBuilder(new byte[]{11, 12}, 5_000_000, 268_435_456).build(); BlockHeader current = new BlockHeaderBuilder(parent.getHash(), 5_000_001, -1).build(); assertEquals(BigInteger.valueOf(537_001_984), etcFork3M.calcDifficulty(current, parent)); parent = new BlockHeaderBuilder(new byte[]{11, 12}, 5_199_999, 1_073_872_896).build(); current = new BlockHeaderBuilder(parent.getHash(), 5_200_000, 1_073_872_896).build(); assertEquals(BlockHeaderBuilder.parse("2,148,139,072"), etcFork3M.calcDifficulty(current, parent)); }