ByzantiumConfig 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; } ByzantiumConfig(BlockchainConfig parent); @Override Constants getConstants(); @Override BigInteger calcDifficulty(BlockHeader curBlock, BlockHeader parent); @Override BigInteger getCalcDifficultyMultiplier(BlockHeader curBlock, BlockHeader parent); @Override boolean eip198(); @Override boolean eip206(); @Override boolean eip211(); @Override boolean eip212(); @Override boolean eip213(); @Override boolean eip214(); @Override boolean eip658();  }
@Test public void testDifficultyWithoutExplosion() throws Exception { ByzantiumConfig byzantiumConfig = new ByzantiumConfig(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 = byzantiumConfig.calcDifficulty(current, parent); assertEquals(BigInteger.valueOf(1_000_976), difficulty); }
@Test public void testEtherscanIoBlock4490790() throws Exception { ByzantiumConfig byzantiumConfig = new ByzantiumConfig(new TestBlockchainConfig()); String parentHash = "fd9d7467e933ff2975c33ea3045ddf8773c87c4cec4e7da8de1bcc015361b38b"; BlockHeader parent = new BlockHeaderBuilder(parentHash.getBytes(), 4490788, "1,377,255,445,606,146") .withTimestamp(1509827488) .withUncles(Hex.decode("1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347")) .build(); BlockHeader current = new BlockHeaderBuilder(parent.getHash(), 4490789, BigInteger.ZERO) .withTimestamp(1509827494) .build(); BigInteger minimumDifficulty = byzantiumConfig.calcDifficulty(current, parent); BigInteger actualDifficultyOnEtherscan = BlockHeaderBuilder.parse("1,377,927,933,620,791"); assertTrue(actualDifficultyOnEtherscan.compareTo(minimumDifficulty) > -1); }
@Test public void testDifficultyWithExplosionShouldBeImpactedByBlockTimestamp() throws Exception { ByzantiumConfig byzantiumConfig = new ByzantiumConfig(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, 8_388_608) .withTimestamp(10 * 60) .build(); BigInteger difficulty = byzantiumConfig.calcDifficulty(current, parent); assertEquals(BigInteger.valueOf(8126464), 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, 8_388_608) .withTimestamp(5) .build(); difficulty = byzantiumConfig.calcDifficulty(current, parent); assertEquals(BigInteger.valueOf(8396800), difficulty); }
@Test public void testDifficultyAboveBlock3MShouldTriggerExplosion() throws Exception { ByzantiumConfig byzantiumConfig = new ByzantiumConfig(new TestBlockchainConfig()); int parentDifficulty = 268_435_456; BlockHeader parent = new BlockHeaderBuilder(FAKE_HASH, 4_000_000, parentDifficulty).build(); BlockHeader current = new BlockHeaderBuilder(parent.getHash(), 4_000_001, -1).build(); int actualDifficulty = byzantiumConfig.calcDifficulty(current, parent).intValue(); int differenceWithoutExplosion = actualDifficulty - parentDifficulty; assertEquals(262_400, differenceWithoutExplosion); parent = new BlockHeaderBuilder(FAKE_HASH, 5_000_000, parentDifficulty).build(); current = new BlockHeaderBuilder(parent.getHash(), 5_000_001, -1).build(); actualDifficulty = byzantiumConfig.calcDifficulty(current, parent).intValue(); differenceWithoutExplosion = actualDifficulty - parentDifficulty; assertEquals(524_288, differenceWithoutExplosion); parent = new BlockHeaderBuilder(FAKE_HASH, 6_000_000, parentDifficulty).build(); current = new BlockHeaderBuilder(parent.getHash(), 6_000_001, -1).build(); actualDifficulty = byzantiumConfig.calcDifficulty(current, parent).intValue(); differenceWithoutExplosion = actualDifficulty - parentDifficulty; assertEquals(268_697_600, differenceWithoutExplosion); }