Skip to content

Commit

Permalink
Refactor method to be more modular and clean. Add test case for HSM v…
Browse files Browse the repository at this point in the history
…ersion lower than 4
  • Loading branch information
julia-zack committed Feb 19, 2025
1 parent a2f6ce9 commit e257c56
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,30 +94,35 @@ public List<Block> getConfirmedBlocks(Keccak256 startingPoint) {
}

protected BigInteger getBlockDifficultyToConsider(Block block) {
logger.trace(
"[getBlockDifficultyToConsider] Get difficulty for block {} at height {}", block.getHash(), block.getNumber()
);

BigInteger blockDifficulty = block.getDifficulty().asBigInteger();
BigInteger difficultyToConsider = blockDifficulty;
if (hsmVersion < HSMVersion.V4.getNumber()) {
logger.trace("[getBlockDifficultyToConsider] Considering {}", blockDifficulty);
return blockDifficulty;
}

if (hsmVersion >= HSMVersion.V4.getNumber()) {
difficultyToConsider = difficultyCap.min(blockDifficulty);
return getBlockDifficultyConsideringUnclesAndDifficultyCap(block.getUncleList(), blockDifficulty);
}

List<BlockHeader> uncles = block.getUncleList();
for (BlockHeader uncle : uncles) {
BigInteger uncleDifficulty = uncle.getDifficulty().asBigInteger();
blockDifficulty = blockDifficulty.add(uncleDifficulty);
private BigInteger getBlockDifficultyConsideringUnclesAndDifficultyCap(List<BlockHeader> uncles, BigInteger blockTotalDifficulty) {
BigInteger blockDifficultyToConsider = difficultyCap.min(blockTotalDifficulty);

BigInteger uncleDifficultyToConsider = difficultyCap.min(uncleDifficulty);
difficultyToConsider = difficultyToConsider.add(uncleDifficultyToConsider);
}
for (BlockHeader uncle : uncles) {
BigInteger uncleTotalDifficulty = uncle.getDifficulty().asBigInteger();
blockTotalDifficulty = blockTotalDifficulty.add(uncleTotalDifficulty);

BigInteger uncleDifficultyToConsider = difficultyCap.min(uncleTotalDifficulty);
blockDifficultyToConsider = blockDifficultyToConsider.add(uncleDifficultyToConsider);
}

logger.trace(
"[getBlockDifficultyToConsider] Block {} (height {}), total difficulty {}, considering {}",
block.getHash(),
block.getNumber(),
blockDifficulty,
difficultyToConsider
"[getBlockDifficultyConsideringUnclesAndDifficultyCap] Total difficulty {}, considering {}",
blockTotalDifficulty,
blockDifficultyToConsider
);

return difficultyToConsider;
return blockDifficultyToConsider;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ void getConfirmedBlocks_considerBrothersDifficulty_BelowDifficultyCap() {
}

@Test
void getBlockDifficultyToConsider_forBlockWithUncles_capsEachDifficulty() {
void getBlockDifficultyToConsider_forHSMVersionMoreThan4_considersUnclesAndCapDifficulty() {
BigInteger difficultyCapMainnet = MAINNET.getDifficultyCap();
// Block 1 - Brothers: 2, 3
// Block 4 - Parent: 1, Uncles: 2, 3
Expand Down Expand Up @@ -342,4 +342,63 @@ void getBlockDifficultyToConsider_forBlockWithUncles_capsEachDifficulty() {
BigInteger expectedConsideredDifficulty = new BigInteger("15000000000000000000000");
assertEquals(expectedConsideredDifficulty, consideredDifficulty);
}

@Test
void getBlockDifficultyToConsider_forHSMVersionLessThan4_doesNotConsiderUnclesAndCapDifficulty() {
BigInteger difficultyCapMainnet = MAINNET.getDifficultyCap();
// Block 1 - Brothers: 2, 3
// Block 4 - Parent: 1, Uncles: 2, 3

// block 1
BlockHeader block1Header = blockHeaderBuilder
.setNumber(1)
.setParentHashFromKeccak256(TestUtils.createHash(0))
.build();
Keccak256 block1ParentHash = block1Header.getParentHash();

// block 2
BigInteger difficultyBelowCap = new BigInteger("1000000000000000000000");
BlockHeader block2Header = blockHeaderBuilder
.setNumber(2)
.setParentHashFromKeccak256(block1ParentHash)
.setDifficulty(new BlockDifficulty(difficultyBelowCap))
.build();

// block 3
BigInteger difficultyAboveCap = new BigInteger("8000000000000000000000");
BlockHeader block3Header = blockHeaderBuilder
.setNumber(3)
.setParentHashFromKeccak256(block1ParentHash)
.setDifficulty(new BlockDifficulty(difficultyAboveCap))
.build();

// block 4
BigInteger difficultyRightAboveCap = new BigInteger("7000000000000000000001");
BlockHeader block4Header = blockHeaderBuilder
.setNumber(4)
.setParentHashFromKeccak256(block1Header.getHash())
.setDifficulty(new BlockDifficulty(difficultyRightAboveCap))
.build();
// build block 4 with block 2 and block 3 as uncles
List<BlockHeader> block4Uncles = Arrays.asList(block2Header, block3Header);
Block block4 = new Block(block4Header, Collections.emptyList(), block4Uncles, true, true);

// build blocks provider
ConfirmedBlocksProvider confirmedBlocksProvider = new ConfirmedBlocksProvider(
BigInteger.valueOf(160),
100,
mock(BlockStore.class),
difficultyCapMainnet,
HSMVersion.V2.getNumber()
);

// act
BigInteger consideredDifficulty = confirmedBlocksProvider.getBlockDifficultyToConsider(block4);

// assert
// HSM version less than 4 does not consider brothers nor cap difficulty
// = 7000000000000000000001 difficulty from block 4
BigInteger expectedConsideredDifficulty = new BigInteger("7000000000000000000001");
assertEquals(expectedConsideredDifficulty, consideredDifficulty);
}
}

0 comments on commit e257c56

Please sign in to comment.