Skip to content

Conversation

@nathanieliov
Copy link
Contributor

@nathanieliov nathanieliov commented Nov 27, 2025

This pull request refactors the logic for determining and constructing federations in FederationProviderFromFederatorSupport, streamlining the process and improving maintainability. The main changes include consolidating federation construction logic, improving error handling, updating logging, and simplifying the associated tests.

Refactoring and logic simplification:

  • Replaced the getExpectedFederation method with a new buildFederation method that tries to construct the correct federation type (standard multisig or P2SH-P2WSH ERP) and throws a clear exception if neither matches the expected address. Supporting helper methods (tryBuildingStandardMultiSigFederation, tryBuildingP2shP2wshErpFederation) were introduced for clarity and modularity.
  • Updated the logic in getActiveFederation and getRetiringFederation to use the new buildFederation method, and improved debug logging to provide more informative messages.
  • Simplified getProposedFederation to always build a P2SH-P2WSH ERP federation, removing conditional logic and the now-unused buildProposedFederation method.

Remove old and unused logic

Remove the use of local storage for waitingForSignatures pegouts. The changes involve deleting the BtcReleaseClientStorageAccessor and BtcReleaseClientStorageSynchronizer classes and all related code, simplifying the pegout signing and release process. Makes the flow easier to follow by removing file-based storage and synchronization checks, making the pegout process depend solely on WaitingForSignature pegout map from the rsk.

Test cleanup and modernization:

  • Refactored tests in FederationProviderFromFederatorSupportTest to use new helper methods (setupActiveFederation, setupActiveFederationKeys) for setting up mocks, removed redundant code, and improved test readability. Also removed unused federation format version constants.

These changes make the federation provider code more modular, easier to maintain, and clearer in its intent, while also improving the reliability and clarity of the test suite.

How Has This Been Tested?

Unit tests

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • Tests for the changes have been added (for bug fixes / features)
  • Requires Activation Code (Hard Fork)
  • Other information:

@nathanieliov nathanieliov requested a review from a team as a code owner November 27, 2025 20:34
@github-actions
Copy link

github-actions bot commented Nov 27, 2025

Dependency Review

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

Scanned Files

None

@marcos-iov marcos-iov requested a review from Copilot December 2, 2025 19:52
@marcos-iov marcos-iov force-pushed the powpeg-refactors-integration branch from 96d5c5a to 983829c Compare December 2, 2025 19:52
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request refactors the codebase to remove support for legacy HSM versions (V2 and V4), retaining only V1 and the latest V5. The changes consolidate federation construction logic, simplify test setup, and update references throughout the codebase to use a centralized helper for the latest HSM version.

  • Removed HSMVersion V2 and V4 enum values and related version-specific logic
  • Introduced TestUtils.getLatestHsmVersion() helper method to centralize HSM version references in tests
  • Refactored FederationProviderFromFederatorSupport to streamline federation construction with clearer error handling

Reviewed changes

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

Show a summary per file
File Description
TestUtils.java Added getLatestHsmVersion() helper and simplified createBlock() method signature
ReleaseRequirementsEnforcerTest.java Updated to use centralized HSM version helper and consolidated test methods
SignerMessageBuilderFactoryTest.java Simplified tests using the latest HSM version helper
ReleaseCreationInformationGetterTest.java Updated block creation calls and HSM version references
PowHSMSignerMessageTest.java Removed parameterized legacy tests and simplified remaining tests
PowHSMSignerMessageBuilderTest.java Updated test names and removed HSMVersion parameters from method calls
PowHSMSigningClientTest.java Updated to use latest HSM version and simplified test setup
PowHSMSigningClientRskMstTest.java Updated version references to use centralized helper
PowHSMSigningClientBtcTest.java Consolidated parameterized tests and removed version-specific logic
HSMSigningClientV1Test.java Updated method name for clarity
HSMSigningClientProviderTest.java Enhanced unsupported version testing with specific version cases
HSMResponseHandlerBaseTest.java Updated test names to reflect PowHSM instead of version-specific references
HsmBookkeepingClientImplTest.java Centralized setup and removed version-specific parameterization
ConfirmedBlocksProviderTest.java Updated test names and removed legacy version comparison logic
HSMVersionTest.java Removed tests for deleted enum values and their related methods
FederationProviderFromFederatorSupportTest.java Major refactoring with new helper methods and improved test structure
FedNodeRunnerTest.java Updated to use latest HSM version and removed legacy version configuration tests
ReleaseRequirementsEnforcer.java Updated log message to refer to "Pow HSM" instead of "Version 2+"
ReleaseCreationInformationGetter.java Renamed method to reflect PowHSM instead of version-specific naming
PowHSMSignerMessage.java Removed version parameter from getMessageToSign() method
PowHSMSigningClientBtc.java Updated to call getMessageToSign() without version parameter
PowHSMSigningClient.java Simplified getPublicKey() logic and removed commented code
HsmBookkeepingClientImpl.java Removed version-specific conditional logic for uncles and blockchain parameters
ConfirmedBlocksProvider.java Removed version-specific uncle difficulty consideration logic
HSMVersion.java Removed V2 and V4 enum values and their related methods
HSMBlockchainBookkeepingRelatedException.java Updated documentation reference from "HSM 2" to "Pow HSM"
FederationProviderFromFederatorSupport.java Refactored federation building logic with improved error handling and logging

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +46 to +48
public static Block createBlock(List<Transaction> rskTxs) {
int blockNumber = 1;
int parentBlockNumber = 0;
Copy link

Copilot AI Dec 2, 2025

Choose a reason for hiding this comment

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

The createBlock method has been simplified but now hardcodes blockNumber to 1 and parentBlockNumber to 0. This removes flexibility that may be needed in tests requiring different block numbers. Consider keeping the block number as a parameter or document why this hardcoding is acceptable.

Suggested change
public static Block createBlock(List<Transaction> rskTxs) {
int blockNumber = 1;
int parentBlockNumber = 0;
public static Block createBlock(List<Transaction> rskTxs, int blockNumber, int parentBlockNumber) {

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

For now, it is only being used with those values. What if we let this happen when the demand for it appears?

Comment on lines 207 to 209
String.format(
"Cannot determine federation type for federation with address %s. Tried: standard multiSig, and P2SH-P2WSH ERP federations.",
expectedFederationAddress
Copy link

Copilot AI Dec 2, 2025

Choose a reason for hiding this comment

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

Corrected capitalization of 'multiSig' to 'multisig' for consistency with the codebase terminology.

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is also being used as multiSig in other places like co.rsk.peg.federation.FederationFactory#buildStandardMultiSigFederation. I think we can discuss it and agree on which one to use, and do the change as part of another task.

Arguments.of(
activations,
createP2shErpFederation()
createNonStandardErpFederation()
Copy link

Copilot AI Dec 2, 2025

Choose a reason for hiding this comment

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

[nitpick] The method name createNonStandardErpFederation() is ambiguous. Based on the implementation, this appears to create a P2SH ERP federation (non-segwit). Consider renaming to createP2shErpFederationWithNonMatchingAddress() or similar to clarify its purpose in testing invalid federation scenarios.

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe this is also related to this discussion #524 (comment). Let's tackle that there.


@ParameterizedTest
@MethodSource("getMessageToSignLegacyArgProvider")
void getMessageToSign_whenSighashLegacyMode_ok(HSMVersion version, JsonNode expectedMessageToSend) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why remove these test? We still have the logic to sign legacy transactions, don't we?

Copy link
Contributor Author

@nathanieliov nathanieliov Dec 3, 2025

Choose a reason for hiding this comment

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

This is only for PowHSM. It was named legacy because it was only for HSM 5 prior versions. I removed it because we no longer support any of those versions.

public Federation getActiveFederation() {
List<FederationMember> members = new ArrayList<>();
int federationSize = federatorSupport.getFederationSize();
boolean useTypedPublicKeyGetter = federatorSupport.getConfigForBestBlock().isActive(RSKIP123);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Don't think we need this anymore

Copy link
Collaborator

Choose a reason for hiding this comment

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

Or any other activation check in this class I believe

Copy link
Contributor Author

@nathanieliov nathanieliov Dec 3, 2025

Choose a reason for hiding this comment

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

You're right, if we assume all activations are active, we don't need to check activations.

);
}

private static IllegalStateException buildInvalidFederationException(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does this function make sense? Why not trow the exception directly? This is only called in one place

Copy link
Contributor Author

@nathanieliov nathanieliov Dec 3, 2025

Choose a reason for hiding this comment

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

I created it to make the functional flow(where the function is being used) more straightforward to follow. But it could be thrown directly.

private static final FederationConstants federationConstants = FederationMainNetConstants.getInstance();
private static final NetworkParameters networkParameters = federationConstants.getBtcParams();
private static final Instant creationTime = Instant.ofEpochSecond(5);
private static ActivationConfig.ForBlock activations;
Copy link
Collaborator

Choose a reason for hiding this comment

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

If we remove the need for activations, we should just focus on cases where either the genesis federation is built or a p2wsh. And maybe also federation address not match error

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed the activations setup from all tests. Now we are checking only Standard(genesis) and P2wsh federation.

The only place where activations is still being used is to create a non-standard ERP federation, using it as an unknown federation (for the federation address not matching error). Wdyt?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated as discussed. Removed test using old type of federation and refactor border test case when built federatio do not match bridge federation address result

@nathanieliov nathanieliov force-pushed the powpeg-refactors-integration branch from cc0af43 to 28d9905 Compare December 3, 2025 16:39
@sonarqubecloud
Copy link

sonarqubecloud bot commented Dec 5, 2025

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