Skip to content

Commit

Permalink
Merge pull request #2854 from rsksmart/svp_success
Browse files Browse the repository at this point in the history
Svp success
  • Loading branch information
julia-zack authored Nov 25, 2024
2 parents 0d4c61c + f1dad07 commit 2ae9834
Show file tree
Hide file tree
Showing 7 changed files with 399 additions and 179 deletions.
6 changes: 5 additions & 1 deletion rskj-core/src/main/java/co/rsk/peg/BridgeSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ public void registerBtcTransaction(

if (isSvpOngoing() && isTheSvpSpendTransaction(btcTx)) {
registerSvpSpendTransaction(btcTx);
processSvpSuccess();
return;
}

Expand Down Expand Up @@ -462,8 +463,11 @@ private boolean isTheSvpSpendTransaction(BtcTransaction transaction) {
private void registerSvpSpendTransaction(BtcTransaction svpSpendTx) throws IOException {
markTxAsProcessed(svpSpendTx);
saveNewUTXOs(svpSpendTx);
}

private void processSvpSuccess() {
provider.setSvpSpendTxHashUnsigned(null);
// proceed with svp success
federationSupport.commitProposedFederation();
}

private Script getLastRetiredFederationP2SHScript() {
Expand Down
5 changes: 2 additions & 3 deletions rskj-core/src/main/java/co/rsk/peg/BridgeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
package co.rsk.peg;

import static org.ethereum.config.blockchain.upgrades.ConsensusRule.*;

import co.rsk.bitcoinj.core.*;
import co.rsk.bitcoinj.crypto.TransactionSignature;
import co.rsk.bitcoinj.script.*;
Expand Down Expand Up @@ -52,9 +54,6 @@
import java.util.List;
import java.util.stream.Collectors;

import static org.ethereum.config.blockchain.upgrades.ConsensusRule.RSKIP284;
import static org.ethereum.config.blockchain.upgrades.ConsensusRule.RSKIP293;

/**
* @author Oscar Guindzberg
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ public interface FederationSupport {

void clearProposedFederation();

void commitProposedFederation();

int voteFederationChange(
Transaction tx,
ABICallSpec callSpec,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -691,39 +691,54 @@ private FederationChangeResponseCode commitPendingFederationAccordingToActivatio
* The federation change info is preserved, and the commitment with the voted federation is logged.
*/
private FederationChangeResponseCode legacyCommitPendingFederation(PendingFederation currentPendingFederation, BridgeEventLogger eventLogger) {
moveUTXOsFromNewToOldFederation();

// set old and new federations
Federation activeFederation = getActiveFederation();
provider.setOldFederation(activeFederation);
Federation newFederation = buildFederationFromPendingFederation(currentPendingFederation);
provider.setNewFederation(newFederation);
handoverToNewFederation(newFederation);

clearPendingFederationVoting();

if (activations.isActive(RSKIP186)) {
// since we are creating the to-be-active-fed in this block,
// its creation block height is this block number
saveFederationChangeInfo(rskExecutionBlock.getNumber());
}

Federation currentOldFederation = provider.getOldFederation(constants, activations);
Federation currentNewFederation = provider.getNewFederation(constants, activations);
logCommitmentWithVotedFederation(eventLogger, currentOldFederation, currentNewFederation);

return FederationChangeResponseCode.SUCCESSFUL;
}

public void commitProposedFederation() {
Federation proposedFederation = provider.getProposedFederation(constants, activations)
.orElseThrow(IllegalStateException::new);

handoverToNewFederation(proposedFederation);
clearProposedFederation();
}

private void handoverToNewFederation(Federation newFederation) {
moveUTXOsFromNewToOldFederation();

setOldAndNewFederations(getActiveFederation(), newFederation);

if (activations.isActive(RSKIP186)) {
saveLastRetiredFederationScript();
provider.setNextFederationCreationBlockHeight(newFederation.getCreationBlockNumber());
}
}

private void setOldAndNewFederations(Federation oldFederation, Federation newFederation) {
provider.setOldFederation(oldFederation);
provider.setNewFederation(newFederation);
}

private void moveUTXOsFromNewToOldFederation() {
List<UTXO> utxosToMove = new ArrayList<>(provider.getNewFederationBtcUTXOs(constants.getBtcParams(), activations));
// since the current active fed reference will change from being 'new' to 'old',
// we have to change the UTXOs reference to match it
List<UTXO> activeFederationUTXOs = List.copyOf(provider.getNewFederationBtcUTXOs(constants.getBtcParams(), activations));

// Clear new and old federation's UTXOs
provider.getNewFederationBtcUTXOs(constants.getBtcParams(), activations).clear();
List<UTXO> oldFederationUTXOs = provider.getOldFederationBtcUTXOs();
oldFederationUTXOs.clear();

// Move UTXOs from the new federation into the old federation
oldFederationUTXOs.addAll(utxosToMove);
// Move UTXOs reference to the old federation
oldFederationUTXOs.addAll(activeFederationUTXOs);
}

/**
Expand All @@ -750,38 +765,31 @@ private Federation buildFederationFromPendingFederation(PendingFederation pendin
return pendingFederation.buildFederation(federationCreationTime, federationCreationBlockNumber, constants, activations);
}

private static Script getFederationMembersP2SHScript(ActivationConfig.ForBlock activations, Federation federation) {
// when the federation is a standard multisig, the members p2sh script is the p2sh script
if (!activations.isActive(RSKIP377)) {
return federation.getP2SHScript();
}
if (!(federation instanceof ErpFederation)) {
return federation.getP2SHScript();
}

// when the federation also has erp keys, the members p2sh script is the default p2sh script
return ((ErpFederation) federation).getDefaultP2SHScript();
}

private void clearPendingFederationVoting() {
// Clear pending federation and votes on election
provider.setPendingFederation(null);
provider.getFederationElection(constants.getFederationChangeAuthorizer()).clear();
}

private void saveFederationChangeInfo(long newActiveFederationCreationBlockHeight) {
saveLastRetiredFederationScript();
provider.setNextFederationCreationBlockHeight(newActiveFederationCreationBlockHeight);
}

private void saveLastRetiredFederationScript() {
Federation activeFederation = getActiveFederation();
Script activeFederationMembersP2SHScript = getFederationMembersP2SHScript(activeFederation);
Script activeFederationMembersP2SHScript = getFederationMembersP2SHScript(activations, activeFederation);
provider.setLastRetiredFederationP2SHScript(activeFederationMembersP2SHScript);
}

private Script getFederationMembersP2SHScript(Federation federation) {
// when the federation is a standard multisig,
// the members p2sh script is the p2sh script
if (!activations.isActive(RSKIP377)) {
return federation.getP2SHScript();
}
if (!(federation instanceof ErpFederation)) {
return federation.getP2SHScript();
}

// when the federation also has erp keys,
// the members p2sh script is the default p2sh script
return ((ErpFederation) federation).getDefaultP2SHScript();
}

private void logCommitmentWithVotedFederation(BridgeEventLogger eventLogger, Federation federationToBeRetired, Federation votedFederation) {
eventLogger.logCommitFederation(rskExecutionBlock, federationToBeRetired, votedFederation);
logger.debug("[logCommitmentWithVotedFederation] Voted federation committed: {}", votedFederation.getAddress());
Expand Down
Loading

0 comments on commit 2ae9834

Please sign in to comment.