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

Include SVP transactions as possible transaction types to be used by the Bridge #2857

Draft
wants to merge 9 commits into
base: feature/powpeg_validation_protocol-phase4
Choose a base branch
from
185 changes: 38 additions & 147 deletions rskj-core/src/main/java/co/rsk/peg/BridgeSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,13 @@ private Wallet getRetiringFederationWallet(boolean shouldConsiderFlyoverUTXOs, i
*
*/
public Wallet getUTXOBasedWalletForLiveFederations(List<UTXO> utxos, boolean isFlyoverCompatible) {
return BridgeUtils.getFederationsSpendWallet(btcContext, getLiveFederations(), utxos, isFlyoverCompatible, provider);
return BridgeUtils.getFederationsSpendWallet(
btcContext,
federationSupport.getLiveFederations(),
utxos,
isFlyoverCompatible,
provider
);
}

/**
Expand All @@ -347,7 +353,12 @@ public Wallet getUTXOBasedWalletForLiveFederations(List<UTXO> utxos, boolean isF
*
*/
public Wallet getNoSpendWalletForLiveFederations(boolean isFlyoverCompatible) {
return BridgeUtils.getFederationsNoSpendWallet(btcContext, getLiveFederations(), isFlyoverCompatible, provider);
return BridgeUtils.getFederationsNoSpendWallet(
btcContext,
federationSupport.getLiveFederations(),
isFlyoverCompatible,
provider
);
}

/**
Expand Down Expand Up @@ -391,38 +402,39 @@ public void registerBtcTransaction(
throw new RegisterBtcTransactionException("Transaction already processed");
}

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

FederationContext federationContext = federationSupport.getFederationContext();
PegTxType pegTxType = PegUtils.getTransactionType(
activations,
provider,
bridgeConstants,
getActiveFederation(),
getRetiringFederation(),
getLastRetiredFederationP2SHScript(),
federationContext,
btcTx,
height
);

switch (pegTxType) {
case PEGIN:
case PEGIN -> {
logger.debug("[registerBtcTransaction] This is a peg-in tx {}", btcTx.getHash());
processPegIn(btcTx, rskTxHash, height);
break;
case PEGOUT_OR_MIGRATION:
}
case PEGOUT_OR_MIGRATION -> {
logger.debug("[registerBtcTransaction] This is a peg-out or migration tx {}", btcTx.getHash());
processPegoutOrMigration(btcTx);
if (isSvpOngoing() && isTheSvpFundTransaction(btcTx)) {
updateSvpFundTransactionValues(btcTx);
}
break;
default:
}
case SVP_FUND_TX -> {
logger.debug("[registerBtcTransaction] This is an svp fund tx {}", btcTx.getHash());
processPegoutOrMigration(btcTx); // Need to register the change UTXO
updateSvpFundTransactionValues(btcTx);
}
case SVP_SPEND_TX -> {
logger.debug("[registerBtcTransaction] This is an svp spend tx {}", btcTx.getHash());
registerSvpSpendTransaction(btcTx);
}
default -> {
String message = String.format("This is not a peg-in, a peg-out nor a migration tx %s", btcTx.getHash());
Comment on lines +433 to 434
Copy link
Contributor

Choose a reason for hiding this comment

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

We need to update this message. Maybe make sense to just say "This is not a bridge transaction".

logger.warn("[registerBtcTransaction][rsk tx {}] {}", rskTxHash, message);
panicProcessor.panic("btclock", message);
}
}
} catch (RegisterBtcTransactionException e) {
logger.warn(
Expand All @@ -434,14 +446,6 @@ public void registerBtcTransaction(
}
}

private boolean isTheSvpFundTransaction(BtcTransaction transaction) {
return provider.getSvpFundTxHashUnsigned()
.map(svpFundTransactionHashUnsigned ->
getMultiSigTransactionHashWithoutSignatures(networkParameters, transaction).equals(svpFundTransactionHashUnsigned)
)
.orElse(false);
}

private void updateSvpFundTransactionValues(BtcTransaction transaction) {
logger.debug(
"[updateSvpFundTransactionValues] Transaction {} is the svp fund transaction. Going to update its values.", transaction
Expand All @@ -451,25 +455,13 @@ private void updateSvpFundTransactionValues(BtcTransaction transaction) {
provider.setSvpFundTxHashUnsigned(null);
}

private boolean isTheSvpSpendTransaction(BtcTransaction transaction) {
return provider.getSvpSpendTxHashUnsigned()
.filter(svpSpendTransactionHashUnsigned ->
getMultiSigTransactionHashWithoutSignatures(networkParameters, transaction).equals(svpSpendTransactionHashUnsigned)
)
.isPresent();
}

private void registerSvpSpendTransaction(BtcTransaction svpSpendTx) throws IOException {
markTxAsProcessed(svpSpendTx);
saveNewUTXOs(svpSpendTx);
provider.setSvpSpendTxHashUnsigned(null);
// proceed with svp success
}

private Script getLastRetiredFederationP2SHScript() {
return federationSupport.getLastRetiredFederationP2SHScript().orElse(null);
}

@VisibleForTesting
BtcBlockStoreWithCache getBtcBlockStore() {
return btcBlockStore;
Expand Down Expand Up @@ -2252,186 +2244,85 @@ public Federation getActiveFederation() {
return federationSupport.getActiveFederation();
}

/**
* Returns the currently retiring federation.
* See getRetiringFederationReference() for details.
* @return the retiring federation.
*/

@Nullable
public Federation getRetiringFederation() {
return federationSupport.getRetiringFederation();
}

/**
* Returns the active federation bitcoin address.
* @return the active federation bitcoin address.
*/
public Address getActiveFederationAddress() {
return federationSupport.getActiveFederationAddress();
}

/**
* Returns the active federation's size
* @return the active federation size
*/
public Integer getActiveFederationSize() {
return federationSupport.getActiveFederationSize();
}

/**
* Returns the active federation's minimum required signatures
* @return the active federation minimum required signatures
*/
public Integer getActiveFederationThreshold() {
return federationSupport.getActiveFederationThreshold();
}

/**
* Returns the public key of the active federation's federator at the given index
* @param index the federator's index (zero-based)
* @return the federator's public key
*/
public byte[] getActiveFederatorBtcPublicKey(int index) {
return federationSupport.getActiveFederatorBtcPublicKey(index);
}

/**
* Returns the public key of given type of the active federation's federator at the given index
* @param index the federator's index (zero-based)
* @param keyType the key type
* @return the federator's public key
*/
public byte[] getActiveFederatorPublicKeyOfType(int index, FederationMember.KeyType keyType) {
return federationSupport.getActiveFederatorPublicKeyOfType(index, keyType);
}

/**
* Returns the active federation's creation time
* @return the active federation creation time
*/
public Instant getActiveFederationCreationTime() {
return federationSupport.getActiveFederationCreationTime();
}

/**
* Returns the active federation's creation block number
* @return the active federation creation block number
*/
public long getActiveFederationCreationBlockNumber() {
return federationSupport.getActiveFederationCreationBlockNumber();
}

/**
* Returns the retiring federation bitcoin address.
* @return the retiring federation bitcoin address, null if no retiring federation exists
*/
public Address getRetiringFederationAddress() {
return federationSupport.getRetiringFederationAddress();
}

/**
* Returns the retiring federation's size
* @return the retiring federation size, -1 if no retiring federation exists
*/

public Integer getRetiringFederationSize() {
return federationSupport.getRetiringFederationSize();
}

/**
* Returns the retiring federation's minimum required signatures
* @return the retiring federation minimum required signatures, -1 if no retiring federation exists
*/
public Integer getRetiringFederationThreshold() {
return federationSupport.getRetiringFederationThreshold();
}

/**
* Returns the public key of the retiring federation's federator at the given index
* @param index the retiring federator's index (zero-based)
* @return the retiring federator's public key, null if no retiring federation exists
*/
public byte[] getRetiringFederatorBtcPublicKey(int index) {
return federationSupport.getRetiringFederatorBtcPublicKey(index);
}

/**
* Returns the public key of the given type of the retiring federation's federator at the given index
* @param index the retiring federator's index (zero-based)
* @param keyType the key type
* @return the retiring federator's public key of the given type, null if no retiring federation exists
*/
public byte[] getRetiringFederatorPublicKeyOfType(int index, FederationMember.KeyType keyType) {
return federationSupport.getRetiringFederatorPublicKeyOfType(index, keyType);
}

/**
* Returns the retiring federation's creation time
* @return the retiring federation creation time, null if no retiring federation exists
*/
public Instant getRetiringFederationCreationTime() {
return federationSupport.getRetiringFederationCreationTime();
}

/**
* Returns the retiring federation's creation block number
* @return the retiring federation creation block number,
* -1 if no retiring federation exists
*/
public long getRetiringFederationCreationBlockNumber() {
return federationSupport.getRetiringFederationCreationBlockNumber();
}

/**
* Returns the currently live federations
* This would be the active federation plus
* potentially the retiring federation
* @return a list of live federations
*/
private List<Federation> getLiveFederations() {
List<Federation> liveFederations = new ArrayList<>();
liveFederations.add(getActiveFederation());
Federation retiringFederation = getRetiringFederation();
if (retiringFederation != null) {
liveFederations.add(retiringFederation);
}
return liveFederations;
}

public Integer voteFederationChange(Transaction tx, ABICallSpec callSpec) {
return federationSupport.voteFederationChange(tx, callSpec, signatureCache, eventLogger);
}

/**
* Returns the currently pending federation hash, or null if none exists
* @return the currently pending federation hash, or null if none exists
*/
public Keccak256 getPendingFederationHash() {
return federationSupport.getPendingFederationHash();
}

/**
* Returns the currently pending federation size, or -1 if none exists
* @return the currently pending federation size, or -1 if none exists
*/
public Integer getPendingFederationSize() {
return federationSupport.getPendingFederationSize();
}

/**
* Returns the currently pending federation federator's public key at the given index, or null if none exists
* @param index the federator's index (zero-based)
* @return the pending federation's federator public key
*/
public byte[] getPendingFederatorBtcPublicKey(int index) {
return federationSupport.getPendingFederatorBtcPublicKey(index);
}

/**
* Returns the public key of the given type of the pending federation's federator at the given index
* @param index the federator's index (zero-based)
* @param keyType the key type
* @return the pending federation's federator public key of given type
*/
public byte[] getPendingFederatorPublicKeyOfType(int index, FederationMember.KeyType keyType) {
return federationSupport.getPendingFederatorPublicKeyOfType(index, keyType);
}
Expand Down Expand Up @@ -2498,11 +2389,6 @@ public Coin getLockingCap() {
return lockingCapSupport.getLockingCap().orElse(null);
}

/**
* Returns the redeemScript of the current active federation
*
* @return Returns the redeemScript of the current active federation
*/
public Optional<Script> getActiveFederationRedeemScript() {
return federationSupport.getActiveFederationRedeemScript();
}
Expand Down Expand Up @@ -2849,10 +2735,15 @@ private WalletProvider createFlyoverWalletProvider(
}

protected Wallet getFlyoverWallet(Context btcContext, List<UTXO> utxos, List<FlyoverFederationInformation> fbFederations) {
Wallet wallet = new FlyoverCompatibleBtcWalletWithMultipleScripts(btcContext, getLiveFederations(), fbFederations);
Wallet wallet = new FlyoverCompatibleBtcWalletWithMultipleScripts(
btcContext,
federationSupport.getLiveFederations(),
fbFederations
);
RskUTXOProvider utxoProvider = new RskUTXOProvider(btcContext.getParams(), utxos);
wallet.setUTXOProvider(utxoProvider);
wallet.setCoinSelector(new RskAllowUnconfirmedCoinSelector());

return wallet;
}

Expand Down
2 changes: 2 additions & 0 deletions rskj-core/src/main/java/co/rsk/peg/PegTxType.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
enum PegTxType {
PEGIN,
PEGOUT_OR_MIGRATION,
SVP_FUND_TX,
SVP_SPEND_TX,
UNKNOWN
}
Loading
Loading