Skip to content

Commit

Permalink
Merge pull request #2840 from rsksmart/update_svp_state_from_uc
Browse files Browse the repository at this point in the history
Make updateSvpState private. Call it from updateCollections
  • Loading branch information
marcos-iov authored Nov 11, 2024
2 parents 271b336 + 99bdde2 commit a84f8a7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
4 changes: 3 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 @@ -1010,14 +1010,16 @@ public void updateCollections(Transaction rskTx) throws IOException {
processConfirmedPegouts(rskTx);

updateFederationCreationBlockHeights();

updateSvpState(rskTx);
}

private void logUpdateCollections(Transaction rskTx) {
RskAddress sender = rskTx.getSender(signatureCache);
eventLogger.logUpdateCollections(sender);
}

protected void updateSvpState(Transaction rskTx) {
private void updateSvpState(Transaction rskTx) {
Optional<Federation> proposedFederationOpt = federationSupport.getProposedFederation();
if (proposedFederationOpt.isEmpty()) {
logger.debug("[updateSvpState] Proposed federation does not exist, so there's no svp going on.");
Expand Down
41 changes: 23 additions & 18 deletions rskj-core/src/test/java/co/rsk/peg/BridgeSupportSvpTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import static co.rsk.peg.bitcoin.BitcoinUtils.addInputFromMatchingOutputScript;
import static co.rsk.peg.bitcoin.UtxoUtils.extractOutpointValues;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -129,6 +130,10 @@ void setUp() {
logs
);

ECKey key = RskTestUtils.getEcKeyFromSeed("key");
RskAddress address = new RskAddress(key.getAddress());
when(rskTx.getSender(any())).thenReturn(address); // to not throw when logging update collections after calling it

bridgeSupport = bridgeSupportBuilder
.withBridgeConstants(bridgeMainNetConstants)
.withProvider(bridgeStorageProvider)
Expand Down Expand Up @@ -173,13 +178,13 @@ void setUp() {
}

@Test
void updateSvpState_whenSvpFundTxHashUnsigned_shouldLogValidationFailureAndClearValue() {
void updateCollections_whenSvpFundTxHashUnsigned_shouldLogValidationFailureAndClearValue() throws IOException {
// arrange
svpFundTransactionHashUnsigned = BitcoinTestUtils.createHash(1);
bridgeStorageProvider.setSvpFundTxHashUnsigned(svpFundTransactionHashUnsigned);

// act
bridgeSupport.updateSvpState(rskTx);
bridgeSupport.updateCollections(rskTx);

// assert
assertLogCommitFederationFailed();
Expand All @@ -188,13 +193,13 @@ void updateSvpState_whenSvpFundTxHashUnsigned_shouldLogValidationFailureAndClear
}

@Test
void updateSvpState_whenSvpFundTxSigned_shouldLogValidationFailureAndClearValue() {
void updateCollections_whenSvpFundTxSigned_shouldLogValidationFailureAndClearValue() throws IOException {
// arrange
svpFundTransaction = new BtcTransaction(btcMainnetParams);
bridgeStorageProvider.setSvpFundTxSigned(svpFundTransaction);

// act
bridgeSupport.updateSvpState(rskTx);
bridgeSupport.updateCollections(rskTx);

// assert
assertLogCommitFederationFailed();
Expand All @@ -203,15 +208,15 @@ void updateSvpState_whenSvpFundTxSigned_shouldLogValidationFailureAndClearValue(
}

@Test
void updateSvpState_whenSvpSpendTxWFS_shouldLogValidationFailureAndClearSpendTxValues() {
void updateCollections_whenSvpSpendTxWFS_shouldLogValidationFailureAndClearSpendTxValues() throws IOException {
// arrange
Keccak256 svpSpendTxCreationHash = RskTestUtils.createHash(1);
svpSpendTransaction = new BtcTransaction(btcMainnetParams);
Map.Entry<Keccak256, BtcTransaction> svpSpendTxWFS = new AbstractMap.SimpleEntry<>(svpSpendTxCreationHash, svpSpendTransaction);
bridgeStorageProvider.setSvpSpendTxWaitingForSignatures(svpSpendTxWFS);

// act
bridgeSupport.updateSvpState(rskTx);
bridgeSupport.updateCollections(rskTx);

// assert
assertLogCommitFederationFailed();
Expand All @@ -220,13 +225,13 @@ void updateSvpState_whenSvpSpendTxWFS_shouldLogValidationFailureAndClearSpendTxV
}

@Test
void updateSvpState_whenSvpSpendTxHashUnsigned_shouldLogValidationFailureAndClearValue() {
void updateCollections_whenSvpSpendTxHashUnsigned_shouldLogValidationFailureAndClearValue() throws IOException {
// arrange
svpSpendTransactionHashUnsigned = BitcoinTestUtils.createHash(2);
bridgeStorageProvider.setSvpSpendTxHashUnsigned(svpSpendTransactionHashUnsigned);

// act
bridgeSupport.updateSvpState(rskTx);
bridgeSupport.updateCollections(rskTx);

// assert
assertLogCommitFederationFailed();
Expand Down Expand Up @@ -261,34 +266,34 @@ private void assertNoSVPValues() {
@Tag("Fund transaction creation and processing tests")
class FundTxCreationAndProcessingTests {
@Test
void updateSvpState_whenProposedFederationDoesNotExist_shouldNotCreateFundTransaction() {
void updateCollections_whenProposedFederationDoesNotExist_shouldNotCreateFundTransaction() throws IOException {
// arrange
when(federationSupport.getProposedFederation()).thenReturn(Optional.empty());

// act
bridgeSupport.updateSvpState(rskTx);
bridgeSupport.updateCollections(rskTx);

// assert
assertNoSvpFundTxHashUnsigned();
}

@Test
void updateSvpState_whenThereAreNoEnoughUTXOs_shouldNotCreateFundTransaction() {
void updateCollections_whenThereAreNoEnoughUTXOs_shouldNotCreateFundTransaction() throws IOException {
// arrange
List<UTXO> insufficientUtxos = new ArrayList<>();
when(federationSupport.getActiveFederationBtcUTXOs()).thenReturn(insufficientUtxos);

// act
bridgeSupport.updateSvpState(rskTx);
bridgeSupport.updateCollections(rskTx);

// assert
assertNoSvpFundTxHashUnsigned();
}

@Test
void updateSvpState_whenFundTxCanBeCreated_createsExpectedFundTxAndSavesTheHashInStorageEntryAndPerformsPegoutActions() throws Exception {
void updateCollections_whenFundTxCanBeCreated_createsExpectedFundTxAndSavesTheHashInStorageEntryAndPerformsPegoutActions() throws Exception {
// act
bridgeSupport.updateSvpState(rskTx);
bridgeSupport.updateCollections(rskTx);
bridgeStorageProvider.save(); // to save the tx sig hash

// assert
Expand Down Expand Up @@ -593,9 +598,9 @@ private void assertSvpFundTransactionValuesWereUpdated() {
class SpendTxCreationAndProcessingTests {

@Test
void updateSvpState_whenThereIsNoFundTxSigned_shouldNotCreateNorProcessSpendTx() {
void updateCollections_whenThereIsNoFundTxSigned_shouldNotCreateNorProcessSpendTx() throws IOException {
// act
bridgeSupport.updateSvpState(rskTx);
bridgeSupport.updateCollections(rskTx);
bridgeStorageProvider.save();

// assert
Expand All @@ -604,12 +609,12 @@ void updateSvpState_whenThereIsNoFundTxSigned_shouldNotCreateNorProcessSpendTx()
}

@Test
void updateSvpState_whenSpendTxCanBeCreated_createsExpectedSpendTxAndSavesTheValuesAndLogsExpectedEvents() {
void updateCollections_whenSpendTxCanBeCreated_createsExpectedSpendTxAndSavesTheValuesAndLogsExpectedEvents() throws IOException {
// arrange
arrangeSvpFundTransactionSigned();

// act
bridgeSupport.updateSvpState(rskTx);
bridgeSupport.updateCollections(rskTx);
bridgeStorageProvider.save();

// assert
Expand Down

0 comments on commit a84f8a7

Please sign in to comment.