Skip to content

Commit

Permalink
Add test for svp transaction without output change
Browse files Browse the repository at this point in the history
  • Loading branch information
julia-zack committed Sep 16, 2024
1 parent 744f3f7 commit 6a3fd9f
Showing 1 changed file with 40 additions and 6 deletions.
46 changes: 40 additions & 6 deletions rskj-core/src/test/java/co/rsk/peg/BridgeSupportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,7 @@ void registerBtcTransaction_forSvpFundTransaction_whenProposedFederationDoesNotE
bridgeStorageProvider.save();

// assert
assertNewUtxoWasSaved(activeFederationUtxosSizeBeforeRegisteringTx);
assertActiveFederationUtxosSize(activeFederationUtxosSizeBeforeRegisteringTx + 1);
assertTransactionWasProcessed(svpFundTransaction.getHash());
assertSvpFundTransactionValuesWereNotUpdated();
}
Expand Down Expand Up @@ -851,7 +851,7 @@ void registerBtcTransaction_forSvpFundTransaction_whenValidationPeriodEnded_shou
bridgeStorageProvider.save();

// assert
assertNewUtxoWasSaved(activeFederationUtxosSizeBeforeRegisteringTx);
assertActiveFederationUtxosSize(activeFederationUtxosSizeBeforeRegisteringTx + 1);
assertTransactionWasProcessed(svpFundTransaction.getHash());
assertSvpFundTransactionValuesWereNotUpdated();
}
Expand All @@ -870,11 +870,29 @@ void registerBtcTransaction_forNormalPegout_whenSvpPeriodIsOngoing_shouldRegiste
bridgeStorageProvider.save();

// assert
assertNewUtxoWasSaved(activeFederationUtxosSizeBeforeRegisteringTx);
assertActiveFederationUtxosSize(activeFederationUtxosSizeBeforeRegisteringTx + 1);
assertTransactionWasProcessed(pegout.getHash());
assertSvpFundTransactionValuesWereNotUpdated();
}

@Test
void registerBtcTransaction_forSvpFundTransactionWithoutChangeOutput_whenSvpPeriodIsOngoing_shouldRegisterTransactionAndUpdateSvpFundTransactionValuesButNotSaveNewUtxos() throws Exception {
// Arrange
BtcTransaction svpFundTransaction = createAndSaveSvpFundTransactionUnsignedWithoutChangeOutput();
signInputs(svpFundTransaction); // a transaction trying to be registered should be signed
setUpForTransactionRegistration(svpFundTransaction);

// Act
int activeFederationUtxosSizeBeforeRegisteringTx = federationSupport.getActiveFederationBtcUTXOs().size();
bridgeSupport.registerBtcTransaction(rskTx, svpFundTransaction.bitcoinSerialize(), btcBlockWithPmtHeight, pmtWithTransactions.bitcoinSerialize());
bridgeStorageProvider.save();

// Assert
assertActiveFederationUtxosSize(activeFederationUtxosSizeBeforeRegisteringTx);
assertTransactionWasProcessed(svpFundTransaction.getHash());
assertSvpFundTransactionValuesWereUpdated();
}

@Test
void registerBtcTransaction_forSvpFundTransaction_whenSvpPeriodIsOngoing_shouldRegisterTransactionAndUpdateSvpFundTransactionValues() throws Exception {
// Arrange
Expand All @@ -888,13 +906,17 @@ void registerBtcTransaction_forSvpFundTransaction_whenSvpPeriodIsOngoing_shouldR
bridgeStorageProvider.save();

// Assert
assertNewUtxoWasSaved(activeFederationUtxosSizeBeforeRegisteringTx);
assertActiveFederationUtxosSize(activeFederationUtxosSizeBeforeRegisteringTx + 1);
assertTransactionWasProcessed(svpFundTransaction.getHash());
assertSvpFundTransactionValuesWereUpdated();
}

private void assertNewUtxoWasSaved(int activeFederationUtxosSizeBeforeRegisteringTx) {
assertEquals(activeFederationUtxosSizeBeforeRegisteringTx + 1, federationSupport.getActiveFederationBtcUTXOs().size());
private void addOutputToProposedFederation(BtcTransaction svpFundTransaction) {
svpFundTransaction.addOutput(spendableValueFromProposedFederation, proposedFederation.getAddress());
}

private void assertActiveFederationUtxosSize(int expectedActiveFederationUtxosSize) {
assertEquals(expectedActiveFederationUtxosSize, federationSupport.getActiveFederationBtcUTXOs().size());
}

private void assertTransactionWasProcessed(Sha256Hash transactionHash) throws IOException {
Expand All @@ -917,10 +939,22 @@ private void assertSvpFundTransactionValuesWereUpdated() {
assertFalse(bridgeStorageProvider.getSvpFundTxHashUnsigned().isPresent());
}

private BtcTransaction createAndSaveSvpFundTransactionUnsignedWithoutChangeOutput() {
BtcTransaction svpFundTransaction = new BtcTransaction(btcMainnetParams);
Sha256Hash parentTxHash = BitcoinTestUtils.createHash(1);
addInput(svpFundTransaction, parentTxHash);
addOutputToProposedFederation(svpFundTransaction);
savePegoutIndex(svpFundTransaction);
saveSvpFundTransactionHashUnsigned(svpFundTransaction.getHash());

return svpFundTransaction;
}

private BtcTransaction createAndSaveSvpFundTransactionUnsigned() {
BtcTransaction svpFundTransaction = new BtcTransaction(btcMainnetParams);
Sha256Hash parentTxHash = BitcoinTestUtils.createHash(1);
addInput(svpFundTransaction, parentTxHash);
addOutputToProposedFederation(svpFundTransaction);
addOutputChange(svpFundTransaction);
savePegoutIndex(svpFundTransaction);
saveSvpFundTransactionHashUnsigned(svpFundTransaction.getHash());
Expand Down

0 comments on commit 6a3fd9f

Please sign in to comment.