Skip to content

Align blocks difficulty - #647

Open
julia-zack wants to merge 5 commits into
masterfrom
align-blocks-difficulty
Open

Align blocks difficulty#647
julia-zack wants to merge 5 commits into
masterfrom
align-blocks-difficulty

Conversation

@julia-zack

Copy link
Copy Markdown
Contributor

No description provided.

@github-actions

Copy link
Copy Markdown

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

Scanned Files

None

@julia-zack
julia-zack force-pushed the dont-sign-when-advancing-blockchain branch 2 times, most recently from 8a6b781 to d6ffdc2 Compare July 22, 2026 22:14
@julia-zack
julia-zack force-pushed the align-blocks-difficulty branch 2 times, most recently from ccdce64 to ef63db9 Compare July 23, 2026 14:54
@julia-zack
julia-zack changed the base branch from dont-sign-when-advancing-blockchain to check-context-nonnull July 23, 2026 14:56
@julia-zack
julia-zack force-pushed the check-context-nonnull branch from 90a1cc7 to d15fc2b Compare July 23, 2026 14:58
Base automatically changed from check-context-nonnull to fix/checkout-sonarqube July 23, 2026 15:06
@julia-zack
julia-zack force-pushed the align-blocks-difficulty branch from ef63db9 to 6d0f650 Compare July 23, 2026 15:10
Base automatically changed from fix/checkout-sonarqube to master July 23, 2026 15:18

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates PowHSM advance-blockchain bookkeeping so block difficulty accounting matches what can actually be delivered to the HSM (i.e., only counting uncles that can be sent as “brothers”), and modernizes a small piece of stream collection code.

Changes:

  • Update confirmed-block selection to use a new getBlockTotalDifficulty(block, hsmBestBlockHeight) that ignores uncles at/below the HSM best block height.
  • Refactor getConfirmedBlocks internals to track “blocks in window” + proof blocks more explicitly.
  • Replace Collectors.toList() with Stream.toList() in AdvanceBlockchainMessage.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/main/java/co/rsk/federate/signing/hsm/advanceblockchain/ConfirmedBlocksProvider.java Computes per-block “total difficulty” (block + eligible uncles) and uses it to choose confirmed blocks and proof blocks to send to the HSM.
src/test/java/co/rsk/federate/signing/hsm/advanceblockchain/ConfirmedBlocksProviderTest.java Adjusts existing difficulty test to call the new API and adds coverage for ignoring uncles at/below the HSM best block height.
src/main/java/co/rsk/federate/signing/hsm/message/AdvanceBlockchainMessage.java Uses Stream.toList() for parsed headers/brothers lists.

@sonarqubecloud

Copy link
Copy Markdown

@marcos-iov
marcos-iov marked this pull request as ready for review July 28, 2026 18:57
@marcos-iov
marcos-iov requested a review from a team as a code owner July 28, 2026 18:57
Copilot AI review requested due to automatic review settings July 28, 2026 18:57

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (3)

src/main/java/co/rsk/federate/signing/hsm/advanceblockchain/ConfirmedBlocksProvider.java:75

  • In getConfirmedBlocks(), you already select the first element with blocksInWindow.get(0) and then remove it via remove(confirmedBlock). Removing by index is clearer and avoids relying on Block.equals() semantics.
                Block confirmedBlock = blocksInWindow.get(0);
                confirmedBlocks.add(confirmedBlock);
                BigInteger confirmedBlockTotalDifficulty = getBlockTotalDifficulty(confirmedBlock, initialBlockNumber);
                accumulatedDifficulty = accumulatedDifficulty.subtract(confirmedBlockTotalDifficulty);
                blocksInWindow.remove(confirmedBlock);

src/main/java/co/rsk/federate/signing/hsm/message/AdvanceBlockchainMessage.java:50

  • groupBrothersByParentHash() uses confirmedBlocks.stream().skip(1) to drop the oldest block, but this relies on the caller providing the list in chronological order. Since parseHeadersAndBrothers() sorts internally, it’s safer to also sort here before skipping so the “oldest” block is unambiguous and brothers grouping doesn’t depend on input ordering.
    private Map<Keccak256, List<BlockHeader>> groupBrothersByParentHash(List<Block> confirmedBlocks) {
        return confirmedBlocks.stream()
            .skip(1) // Skip the oldest block (index 0) because its uncles doesn't belong to this set of blocks
            .flatMap(block -> block.getUncleList().stream())
            .collect(Collectors.groupingBy(BlockHeader::getParentHash));

src/main/java/co/rsk/federate/signing/hsm/advanceblockchain/ConfirmedBlocksProvider.java:106

  • getBlockTotalDifficulty() is a misleading name: it returns the capped difficulty of the block plus the capped difficulty of eligible uncles, not the chain/ledger “total difficulty” concept. This can lead to incorrect reuse elsewhere; consider renaming to something like getDifficultyToConsider/getCappedDifficultyIncludingEligibleUncles and update the trace message accordingly.
    protected BigInteger getBlockTotalDifficulty(Block block, long uncleHeightThreshold) {
        logger.trace(
            "[getBlockTotalDifficulty] Get total difficulty for block {} at height {}",
            block.getHash(),
            block.getNumber()
        );

        BigInteger blockDifficulty = difficultyCap.min(block.getDifficulty().asBigInteger());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants