Skip to content

Commit

Permalink
Create new public method in BridgeUtils to be reused
Browse files Browse the repository at this point in the history
  • Loading branch information
julia-zack authored and marcos-iov committed Jan 15, 2025
1 parent 1650378 commit 58e5b0b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 22 deletions.
19 changes: 17 additions & 2 deletions rskj-core/src/main/java/co/rsk/peg/BridgeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import co.rsk.core.RskAddress;
import co.rsk.peg.bitcoin.RskAllowUnconfirmedCoinSelector;
import co.rsk.peg.btcLockSender.BtcLockSender.TxSenderAddressType;
import co.rsk.peg.federation.ErpFederation;
import co.rsk.peg.federation.Federation;
import co.rsk.peg.federation.constants.FederationConstants;
import co.rsk.peg.feeperkb.constants.FeePerKbConstants;
Expand All @@ -49,8 +50,7 @@
import java.util.*;
import java.util.stream.Collectors;

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

/**
* @author Oscar Guindzberg
Expand Down Expand Up @@ -616,4 +616,19 @@ public static int calculatePegoutTxSize(ActivationConfig.ForBlock activations, F

return baseSize + signingSize;
}

public 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();
}
}
45 changes: 25 additions & 20 deletions rskj-core/src/test/java/co/rsk/peg/BridgeUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
import java.util.Collections;
import java.util.List;

import static co.rsk.peg.BridgeSerializationUtils.deserializeRskTxHash;
import static co.rsk.peg.BridgeUtils.getFederationMembersP2SHScript;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

Expand Down Expand Up @@ -924,7 +924,6 @@ void deserializeBtcAddressWithVersion_invalid_address_hash() {

@Test
void testCalculatePegoutTxSize_ZeroInput_ZeroOutput() {
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
when(activations.isActive(ConsensusRule.RSKIP271)).thenReturn(true);

List<BtcECKey> keys = PegTestUtils.createRandomBtcECKeys(13);
Expand All @@ -943,7 +942,6 @@ void testCalculatePegoutTxSize_ZeroInput_ZeroOutput() {

@Test
void testCalculatePegoutTxSize_2Inputs_2Outputs() {
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
when(activations.isActive(ConsensusRule.RSKIP271)).thenReturn(true);

List<BtcECKey> keys = PegTestUtils.createRandomBtcECKeys(13);
Expand Down Expand Up @@ -971,7 +969,6 @@ void testCalculatePegoutTxSize_2Inputs_2Outputs() {

@Test
void testCalculatePegoutTxSize_9Inputs_2Outputs() {
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
when(activations.isActive(ConsensusRule.RSKIP271)).thenReturn(true);

List<BtcECKey> keys = PegTestUtils.createRandomBtcECKeys(13);
Expand Down Expand Up @@ -999,7 +996,6 @@ void testCalculatePegoutTxSize_9Inputs_2Outputs() {

@Test
void testCalculatePegoutTxSize_10Inputs_20Outputs() {
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
when(activations.isActive(ConsensusRule.RSKIP271)).thenReturn(true);

List<BtcECKey> keys = PegTestUtils.createRandomBtcECKeys(13);
Expand Down Expand Up @@ -1030,7 +1026,6 @@ void testCalculatePegoutTxSize_10Inputs_20Outputs() {

@Test
void testCalculatePegoutTxSize_50Inputs_200Outputs() {
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
when(activations.isActive(ConsensusRule.RSKIP271)).thenReturn(true);

List<BtcECKey> keys = PegTestUtils.createRandomBtcECKeys(13);
Expand Down Expand Up @@ -1061,7 +1056,6 @@ void testCalculatePegoutTxSize_50Inputs_200Outputs() {

@Test
void testCalculatePegoutTxSize_50Inputs_200Outputs_nonStandardErpFederation() {
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
when(activations.isActive(ConsensusRule.RSKIP271)).thenReturn(true);

List<BtcECKey> defaultFederationKeys = Arrays.asList(
Expand Down Expand Up @@ -1104,7 +1098,6 @@ void testCalculatePegoutTxSize_50Inputs_200Outputs_nonStandardErpFederation() {

@Test
void testCalculatePegoutTxSize_100Inputs_50Outputs_nonStandardErpFederation() {
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
when(activations.isActive(ConsensusRule.RSKIP271)).thenReturn(true);

List<BtcECKey> defaultFederationKeys = Arrays.asList(
Expand Down Expand Up @@ -1147,7 +1140,6 @@ void testCalculatePegoutTxSize_100Inputs_50Outputs_nonStandardErpFederation() {

@Test
void getRegularPegoutTxSize_has_proper_calculations() {
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
when(activations.isActive(ConsensusRule.RSKIP271)).thenReturn(true);

BtcECKey key1 = new BtcECKey();
Expand Down Expand Up @@ -1679,7 +1671,6 @@ private Script signWithOneKey(

@Test
void testValidateFlyoverPeginValue_sent_zero_amount_before_RSKIP293() {
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
when(activations.isActive(ConsensusRule.RSKIP293)).thenReturn(false);

Address btcAddressReceivingFunds = PegTestUtils.createRandomP2PKHBtcAddress(networkParameters);
Expand All @@ -1706,7 +1697,6 @@ were not sent to the given address(normally the federation address */

@Test
void testValidateFlyoverPeginValue_sent_one_utxo_with_amount_below_minimum_before_RSKIP293() {
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
when(activations.isActive(ConsensusRule.RSKIP293)).thenReturn(false);

Address addressReceivingFundsBelowMinimum = PegTestUtils.createRandomP2PKHBtcAddress(networkParameters);
Expand Down Expand Up @@ -1734,7 +1724,6 @@ void testValidateFlyoverPeginValue_sent_one_utxo_with_amount_below_minimum_befor

@Test
void testValidateFlyoverPeginValue_sent_one_utxo_with_amount_below_minimum_after_RSKIP293() {
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
when(activations.isActive(ConsensusRule.RSKIP293)).thenReturn(true);

Context btcContext = new Context(networkParameters);
Expand All @@ -1759,7 +1748,6 @@ void testValidateFlyoverPeginValue_sent_one_utxo_with_amount_below_minimum_after

@Test
void testValidateFlyoverPeginValue_funds_sent_equal_to_minimum_after_RSKIP293() {
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
when(activations.isActive(ConsensusRule.RSKIP293)).thenReturn(true);

Context btcContext = new Context(bridgeConstantsRegtest.getBtcParams());
Expand All @@ -1786,7 +1774,6 @@ void testValidateFlyoverPeginValue_funds_sent_equal_to_minimum_after_RSKIP293()

@Test
void testValidateFlyoverPeginValue_funds_sent_above_minimum_after_RSKIP293() {
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
when(activations.isActive(ConsensusRule.RSKIP293)).thenReturn(true);

Address btcAddressReceivingFundsEqualToMin = PegTestUtils.createRandomP2PKHBtcAddress(networkParameters);
Expand Down Expand Up @@ -1814,7 +1801,6 @@ void testValidateFlyoverPeginValue_funds_sent_above_minimum_after_RSKIP293() {

@Test
void testGetUTXOsSentToAddresses_multiple_utxos_sent_to_random_address_and_one_utxo_sent_to_bech32_address_before_RSKIP293() {
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
when(activations.isActive(ConsensusRule.RSKIP293)).thenReturn(false);

Context btcContext = new Context(bridgeConstantsRegtest.getBtcParams());
Expand All @@ -1839,7 +1825,6 @@ void testGetUTXOsSentToAddresses_multiple_utxos_sent_to_random_address_and_one_u

@Test
void testGetUTXOsSentToAddresses_multiple_utxo_sent_to_multiple_addresses_before_RSKIP293() {
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
when(activations.isActive(ConsensusRule.RSKIP293)).thenReturn(false);

Context btcContext = new Context(bridgeConstantsRegtest.getBtcParams());
Expand Down Expand Up @@ -1879,7 +1864,6 @@ void testGetUTXOsSentToAddresses_multiple_utxo_sent_to_multiple_addresses_before

@Test
void testGetUTXOsSentToAddresses_no_utxo_sent_to_given_address_before_RSKIP293() {
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
when(activations.isActive(ConsensusRule.RSKIP293)).thenReturn(false);

Context btcContext = new Context(bridgeConstantsRegtest.getBtcParams());
Expand Down Expand Up @@ -1908,7 +1892,6 @@ void testGetUTXOsSentToAddresses_no_utxo_sent_to_given_address_before_RSKIP293()

@Test
void testGetUTXOsSentToAddresses_multiple_utxos_sent_to_random_address_and_one_utxo_sent_to_bech32_address_after_RSKIP293() {
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
when(activations.isActive(ConsensusRule.RSKIP293)).thenReturn(true);

Context btcContext = new Context(networkParameters);
Expand Down Expand Up @@ -1942,7 +1925,6 @@ void testGetUTXOsSentToAddresses_multiple_utxos_sent_to_random_address_and_one_u

@Test
void testGetUTXOsSentToAddresses_multiple_utxo_sent_to_multiple_addresses_after_RSKIP293() {
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
when(activations.isActive(ConsensusRule.RSKIP293)).thenReturn(true);

Context btcContext = new Context(networkParameters);
Expand Down Expand Up @@ -1989,7 +1971,6 @@ void testGetUTXOsSentToAddresses_multiple_utxo_sent_to_multiple_addresses_after_

@Test
void testGetUTXOsSentToAddresses_no_utxo_sent_to_given_address_after_RSKIP293() {
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
when(activations.isActive(ConsensusRule.RSKIP293)).thenReturn(false);

Context btcContext = new Context(networkParameters);
Expand All @@ -2016,4 +1997,28 @@ void testGetUTXOsSentToAddresses_no_utxo_sent_to_given_address_after_RSKIP293()

Assertions.assertTrue(foundUTXOs.isEmpty());
}

@Test
void getFederationMembersP2SHScript_beforeRSKIP377_shouldReturnP2SHScript() {
when(activations.isActive(ConsensusRule.RSKIP377)).thenReturn(false);
Federation standardMultisigFederation = StandardMultiSigFederationBuilder.builder().build();

assertEquals(standardMultisigFederation.getP2SHScript(), getFederationMembersP2SHScript(activations, standardMultisigFederation));
}

@Test
void getFederationMembersP2SHScript_whenIsNotErpFederation_shouldReturnP2SHScript() {
when(activations.isActive(ConsensusRule.RSKIP377)).thenReturn(true);
Federation standardMultisigFederation = StandardMultiSigFederationBuilder.builder().build();

assertEquals(standardMultisigFederation.getP2SHScript(), getFederationMembersP2SHScript(activations, standardMultisigFederation));
}

@Test
void getFederationMembersP2SHScript_whenIsErpFederation_shouldReturnDefaultP2SHScript() {
when(activations.isActive(ConsensusRule.RSKIP377)).thenReturn(true);
ErpFederation p2shErpFederation = P2shErpFederationBuilder.builder().build();

assertEquals(p2shErpFederation.getDefaultP2SHScript(), getFederationMembersP2SHScript(activations, p2shErpFederation));
}
}

0 comments on commit 58e5b0b

Please sign in to comment.