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

Remove spendableValueFromProposedFed constant and use minPegoutTxValue directly #2918

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions rskj-core/src/main/java/co/rsk/peg/BridgeSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -1088,14 +1088,15 @@ private boolean isSvpOngoing() {
}

private void processSvpFundTransactionUnsigned(Keccak256 rskTxHash, Federation proposedFederation) {
Coin spendableValueFromProposedFederation = bridgeConstants.getSpendableValueFromProposedFederation();
try {
BtcTransaction svpFundTransactionUnsigned = createSvpFundTransaction(proposedFederation, spendableValueFromProposedFederation);
BtcTransaction svpFundTransactionUnsigned = createSvpFundTransaction(proposedFederation);
provider.setSvpFundTxHashUnsigned(svpFundTransactionUnsigned.getHash());
PegoutsWaitingForConfirmations pegoutsWaitingForConfirmations = provider.getPegoutsWaitingForConfirmations();

List<UTXO> utxosToUse = federationSupport.getActiveFederationBtcUTXOs();
settleReleaseRequest(utxosToUse, pegoutsWaitingForConfirmations, svpFundTransactionUnsigned, rskTxHash, spendableValueFromProposedFederation);
// minPegoutValue to proposed fed, minPegoutValue to flyover proposed fed
Coin totalValueSentToProposedFederation = bridgeConstants.getMinimumPegoutTxValue().multiply(2);
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we get this value from the transaction inputs? Feel like it will be more robust that way, a change in the transaction will automatically be reflected here

settleReleaseRequest(utxosToUse, pegoutsWaitingForConfirmations, svpFundTransactionUnsigned, rskTxHash, totalValueSentToProposedFederation);
} catch (InsufficientMoneyException e) {
logger.error(
"[processSvpFundTransactionUnsigned] Insufficient funds for creating the fund transaction. Error message: {}",
Expand All @@ -1109,18 +1110,18 @@ private void processSvpFundTransactionUnsigned(Keccak256 rskTxHash, Federation p
}
}

private BtcTransaction createSvpFundTransaction(Federation proposedFederation, Coin spendableValueFromProposedFederation) throws InsufficientMoneyException {
private BtcTransaction createSvpFundTransaction(Federation proposedFederation) throws InsufficientMoneyException {
Wallet activeFederationWallet = getActiveFederationWallet(true);

BtcTransaction svpFundTransaction = new BtcTransaction(networkParameters);
svpFundTransaction.setVersion(BTC_TX_VERSION_2);

Coin minPegoutTxValue = bridgeConstants.getMinimumPegoutTxValue();
// add outputs to proposed fed and proposed fed with flyover prefix
svpFundTransaction.addOutput(spendableValueFromProposedFederation, proposedFederation.getAddress());

svpFundTransaction.addOutput(minPegoutTxValue, proposedFederation.getAddress());
Address proposedFederationWithFlyoverPrefixAddress =
getFlyoverAddress(networkParameters, bridgeConstants.getProposedFederationFlyoverPrefix(), proposedFederation.getRedeemScript());
svpFundTransaction.addOutput(spendableValueFromProposedFederation, proposedFederationWithFlyoverPrefixAddress);
svpFundTransaction.addOutput(minPegoutTxValue, proposedFederationWithFlyoverPrefixAddress);

// complete tx with input and change output
SendRequest sendRequest = createSvpFundTransactionSendRequest(svpFundTransaction);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,6 @@ public Coin getMinimumPeginTxValue(ActivationConfig.ForBlock activations) {

public Coin getMinimumPegoutTxValue() { return minimumPegoutTxValue; }

public Coin getSpendableValueFromProposedFederation() { return minimumPegoutTxValue.multiply(2); }

public Keccak256 getProposedFederationFlyoverPrefix() { return new Keccak256("0000000000000000000000000000000000000000000000000000000000000001"); }

public Coin getMaxRbtc() { return Coin.valueOf(21_000_000, 0); }
Expand Down
18 changes: 9 additions & 9 deletions rskj-core/src/test/java/co/rsk/peg/BridgeSupportSvpTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public class BridgeSupportSvpTest {
private static final NetworkParameters btcMainnetParams = bridgeMainNetConstants.getBtcParams();
private static final FederationConstants federationMainNetConstants = bridgeMainNetConstants.getFederationConstants();

private static final Coin spendableValueFromProposedFederation = bridgeMainNetConstants.getSpendableValueFromProposedFederation();
private static final Coin minPegoutTxValue = bridgeMainNetConstants.getMinimumPegoutTxValue();
private static final Coin totalValueSentToProposedFederation = bridgeMainNetConstants.getMinimumPegoutTxValue().multiply(2);
private static final Coin feePerKb = Coin.valueOf(1000L);
private static final Keccak256 svpSpendTxCreationHash = RskTestUtils.createHash(1);

Expand Down Expand Up @@ -411,14 +412,14 @@ private void assertOneOutputIsChange(List<TransactionOutput> transactionOutputs)
private void assertOneOutputIsToProposedFederationWithExpectedAmount(List<TransactionOutput> svpFundTransactionUnsignedOutputs) {
Script proposedFederationScriptPubKey = proposedFederation.getP2SHScript();

assertOutputWasSentToExpectedScriptWithExpectedAmount(svpFundTransactionUnsignedOutputs, proposedFederationScriptPubKey, spendableValueFromProposedFederation);
assertOutputWasSentToExpectedScriptWithExpectedAmount(svpFundTransactionUnsignedOutputs, proposedFederationScriptPubKey, minPegoutTxValue);
}

private void assertOneOutputIsToProposedFederationWithFlyoverPrefixWithExpectedAmount(List<TransactionOutput> svpFundTransactionUnsignedOutputs) {
Script proposedFederationWithFlyoverPrefixScriptPubKey =
PegUtils.getFlyoverScriptPubKey(bridgeMainNetConstants.getProposedFederationFlyoverPrefix(), proposedFederation.getRedeemScript());

assertOutputWasSentToExpectedScriptWithExpectedAmount(svpFundTransactionUnsignedOutputs, proposedFederationWithFlyoverPrefixScriptPubKey, spendableValueFromProposedFederation);
assertOutputWasSentToExpectedScriptWithExpectedAmount(svpFundTransactionUnsignedOutputs, proposedFederationWithFlyoverPrefixScriptPubKey, minPegoutTxValue);
}

@Test
Expand Down Expand Up @@ -510,7 +511,7 @@ private void assertSvpFundTxReleaseWasSettled(Sha256Hash svpFundTransactionHashU
svpFundTransaction = getSvpFundTransactionFromPegoutsMap(pegoutsWaitingForConfirmations);

assertPegoutTxSigHashWasSaved(svpFundTransaction);
assertLogReleaseRequested(rskTx.getHash(), svpFundTransactionHashUnsigned, spendableValueFromProposedFederation);
assertLogReleaseRequested(rskTx.getHash(), svpFundTransactionHashUnsigned, totalValueSentToProposedFederation);
assertLogPegoutTransactionCreated(svpFundTransaction);
}

Expand Down Expand Up @@ -741,10 +742,9 @@ private void assertSvpSpendTxHasExpectedInputsAndOutputs() {
.multiply(calculatedTransactionSize * 12L / 10L) // back up calculation
.divide(1000);

Coin valueToSend = spendableValueFromProposedFederation
.multiply(2)
Coin valueToSendBackToActiveFed = totalValueSentToProposedFederation
.minus(fees);
assertOutputWasSentToExpectedScriptWithExpectedAmount(outputs, activeFederation.getP2SHScript(), valueToSend);
assertOutputWasSentToExpectedScriptWithExpectedAmount(outputs, activeFederation.getP2SHScript(), valueToSendBackToActiveFed);
}

private void assertInputsHaveExpectedScriptSig(List<TransactionInput> inputs) {
Expand Down Expand Up @@ -1319,13 +1319,13 @@ private void recreateSvpFundTransactionUnsigned() {
Sha256Hash parentTxHash = BitcoinTestUtils.createHash(1);
addInput(svpFundTransaction, parentTxHash, proposedFederation.getRedeemScript());

svpFundTransaction.addOutput(spendableValueFromProposedFederation, proposedFederation.getAddress());
svpFundTransaction.addOutput(minPegoutTxValue, proposedFederation.getAddress());
Address flyoverProposedFederationAddress = PegUtils.getFlyoverAddress(
btcMainnetParams,
bridgeMainNetConstants.getProposedFederationFlyoverPrefix(),
proposedFederation.getRedeemScript()
);
svpFundTransaction.addOutput(spendableValueFromProposedFederation, flyoverProposedFederationAddress);
svpFundTransaction.addOutput(minPegoutTxValue, flyoverProposedFederationAddress);
}

private BtcTransaction createPegout(Script redeemScript) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,6 @@ void getMinimumPegoutTxValue(BridgeConstants bridgeConstants, Coin expectedMinim
assertEquals(expectedMinimumPegoutTxValue, minimumPegoutTxValue);
}

private static Stream<Arguments> spendableValueFromProposedFederationArgProvider() {
BridgeConstants bridgeMainnetConstants = BridgeMainNetConstants.getInstance();
BridgeConstants bridgeTestnetConstants = BridgeTestNetConstants.getInstance();
BridgeConstants bridgeRegtestConstants = new BridgeRegTestConstants();

return Stream.of(
Arguments.of(bridgeMainnetConstants, bridgeMainnetConstants.getMinimumPegoutTxValue().multiply(2)),
Arguments.of(bridgeTestnetConstants, bridgeTestnetConstants.getMinimumPegoutTxValue().multiply(2)),
Arguments.of(bridgeRegtestConstants, bridgeRegtestConstants.getMinimumPegoutTxValue().multiply(2))
);
}

@ParameterizedTest()
@MethodSource("spendableValueFromProposedFederationArgProvider")
void getSpendableValueFromProposedFederation(BridgeConstants bridgeConstants, Coin expectedSpendableValueFromProposedFederation) {
Coin spendableValueFromProposedFederation = bridgeConstants.getSpendableValueFromProposedFederation();
assertEquals(expectedSpendableValueFromProposedFederation, spendableValueFromProposedFederation);
}

private static Stream<BridgeConstants> bridgeConstantsArgProvider() {
BridgeConstants bridgeMainnetConstants = BridgeMainNetConstants.getInstance();
BridgeConstants bridgeTestnetConstants = BridgeTestNetConstants.getInstance();
Expand Down
Loading