Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create updateSvpState method #2838

Merged

Conversation

julia-zack
Copy link
Contributor

@julia-zack julia-zack commented Oct 31, 2024

Background
In order to keep going with the validation process, in every updateCollections we will call a new validation-related method, updateSvpState, that will update the state of the svp process to see if the validation failed, or if conditions are met for creating the fund or the spend transaction.

IN THIS PR
Create a new updateSvpState method that will:

  • If a proposed federation does not exist, do nothing.

  • If a proposed federation exists and the validation period is ended, we can conclude that the validation failed.

  • If a proposed federation exists, the validation period is not ended and the svpFundTxHashUnigned entry is empty, we have to proceed with the fund transaction creation and processing.

  • If a proposed federation exists, the validation period is not ended and the svpFundTxSigned has a value stored, we will proceed with the spend tx creation.

Copy link

github-actions bot commented Oct 31, 2024

Dependency Review

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

OpenSSF Scorecard

PackageVersionScoreDetails

Scanned Manifest Files

@julia-zack julia-zack marked this pull request as ready for review October 31, 2024 20:53
@julia-zack julia-zack requested a review from a team as a code owner October 31, 2024 20:53
rskj-core/src/main/java/co/rsk/peg/BridgeSupport.java Outdated Show resolved Hide resolved
rskj-core/src/main/java/co/rsk/peg/BridgeSupport.java Outdated Show resolved Hide resolved
rskj-core/src/main/java/co/rsk/peg/BridgeSupport.java Outdated Show resolved Hide resolved
rskj-core/src/main/java/co/rsk/peg/BridgeSupport.java Outdated Show resolved Hide resolved
rskj-core/src/main/java/co/rsk/peg/BridgeSupport.java Outdated Show resolved Hide resolved
Comment on lines 1035 to 1060
// if the fund tx signed is present, then the fund transaction change was registered,
// meaning we can create the spend tx.
Optional<BtcTransaction> svpFundTxSigned = provider.getSvpFundTxSigned();
if (svpFundTxSigned.isPresent()) {
processSvpSpendTransactionUnsigned(rskTxHash, proposedFederation, svpFundTxSigned.get());
return;
}

// if the unsigned fund tx hash is not present, meaning we can proceed with the fund tx creation
Optional<Sha256Hash> svpFundTxHashUnsigned = provider.getSvpFundTxHashUnsigned();
if (svpFundTxHashUnsigned.isEmpty()) {
try {
processSvpFundTransactionUnsigned(rskTxHash, proposedFederation);
} catch (Exception e) {
logger.error("[updateSvpState] Error processing svp fund transaction unsigned.");
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I was thinking on how we can change the order of these two logic blocks get executed. I think it would be easier to understand if the logic related to processing svpFundTxHashUnsigned happens first than the svpFundTxSigned. Maybe we can do something like this:

// Check if the unsigned fund transaction hash is not present, and create the fund transaction if needed
Optional<Sha256Hash> svpFundTxHashUnsigned = provider.getSvpFundTxHashUnsigned();
if (svpFundTxHashUnsigned.isEmpty()) {
    try {
        processSvpFundTransactionUnsigned(rskTxHash, proposedFederation);
    } catch (Exception e) {
       ...
    }
   return;
}

// Check if the signed fund transaction is present, and create the spend transaction if available
Optional<BtcTransaction> svpFundTxSigned = provider.getSvpFundTxSigned();
if (svpFundTxSigned.isPresent()) {
    processSvpSpendTransactionUnsigned(rskTxHash, proposedFederation, svpFundTxSigned.get());
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

remember that when we save the svpFundTxSigned we remove the svpFundTxHashUnsigned. So this order won't have the expected behavior

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

@julia-zack julia-zack force-pushed the update_state_svp branch 5 times, most recently from 45b45d3 to d9cf8e0 Compare November 4, 2024 20:34
Copy link

sonarcloud bot commented Nov 4, 2024

rskj-core/src/main/java/co/rsk/peg/BridgeSupport.java Outdated Show resolved Hide resolved
Comment on lines +1098 to +1103
logger.error(
"[processSvpFundTransactionUnsigned] IOException getting the pegouts waiting for confirmations. Error message: {}",
e.getMessage()
Copy link
Contributor

Choose a reason for hiding this comment

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

same here

Copy link
Contributor

@apancorb apancorb left a comment

Choose a reason for hiding this comment

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

LGTM, nice job

Copy link
Contributor

@nathanieliov nathanieliov left a comment

Choose a reason for hiding this comment

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

LGTM

// if the fund tx signed is present, then the fund transaction change was registered,
// meaning we can create the spend tx.
Optional<BtcTransaction> svpFundTxSigned = provider.getSvpFundTxSigned();
if (svpFundTxSigned.isPresent()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we also be checking that there is no spend tx created? Just in case.

I know there should only be one at a time so if both are found perhaps we have a problem

@@ -1017,13 +1017,51 @@ private void logUpdateCollections(Transaction rskTx) {
eventLogger.logUpdateCollections(sender);
}

protected void processSVPFailure(Federation proposedFederation) {
protected void updateSvpState(Transaction rskTx) {
Copy link
Contributor

Choose a reason for hiding this comment

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

To be honest, I'd find it easier to follow if the logic was written in chronological order. I know it requires more validations but we can probably make use of auxiliary methods. The way it's written now, for every new condition that appears in the code, you need to keep in mind the previous condition that was already validated a few lines above and the protocol logic about one having one SVP related value in storage at once.

I'd go with a very straight forward approach

if (!proposedFederationExists) return;

if (validationPeriodIsOngoing) {
     if (shouldCreateSvpFundTx()) {
        createFundTx()
        return;
      }
     if (shouldCreateSvpSpendTx()) {
        createSpendTx()
        return;
      }

      validationFailed();
}

What do you think? I think that's the only 3 cases we need to cover

Copy link
Contributor Author

Choose a reason for hiding this comment

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

makes sense, will update

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

provider.setSvpFundTxHashUnsigned(svpFundTransactionUnsigned.getHash());
PegoutsWaitingForConfirmations pegoutsWaitingForConfirmations = provider.getPegoutsWaitingForConfirmations();
settleReleaseRequest(pegoutsWaitingForConfirmations, svpFundTransactionUnsigned, rskTx.getHash(), spendableValueFromProposedFederation);
private void processSvpFundTransactionUnsigned(Keccak256 rskTxHash, Federation proposedFederation) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe call this create rather than process?

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 named it process to be consistent with processPegoutOrMigration method. But no big deal, I can change it :)

@marcos-iov marcos-iov force-pushed the feature/powpeg_validation_protocol-phase4 branch from 38c0b3f to 7a04c05 Compare November 12, 2024 19:58
Minor refactors to avoid duplication code

Rewrite comment

Fix sonar complains

Handle exception when processing fund transaction. Add logs for when updating svp state

Add debug level log for when proposed fed does not exist

Not allow validation period end block to be part of validation ongoing period

Make updateSvpState private. Call it from updateCollections

Add missing import

Fix test
Copy link

sonarcloud bot commented Nov 13, 2024

@marcos-iov marcos-iov merged commit 3858201 into feature/powpeg_validation_protocol-phase4 Nov 13, 2024
8 checks passed
@marcos-iov marcos-iov deleted the update_state_svp branch November 13, 2024 20:32
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.

4 participants