Skip to content

Commit

Permalink
fix(federate): add RSKIP419 check for proposed federation methods
Browse files Browse the repository at this point in the history
  • Loading branch information
apancorb committed Nov 19, 2024
1 parent 7dec119 commit 6abadbf
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static co.rsk.peg.federation.FederationChangeResponseCode.FEDERATION_NON_EXISTENT;
import static co.rsk.peg.federation.FederationMember.KeyType;
import static org.ethereum.config.blockchain.upgrades.ConsensusRule.RSKIP123;
import static org.ethereum.config.blockchain.upgrades.ConsensusRule.RSKIP419;

import co.rsk.bitcoinj.core.Address;
import co.rsk.bitcoinj.core.BtcECKey;
Expand Down Expand Up @@ -143,6 +144,10 @@ public Optional<Address> getRetiringFederationAddress() {

@Override
public Optional<Federation> getProposedFederation() {
if (!federatorSupport.getConfigForBestBlock().isActive(RSKIP419)) {
return Optional.empty();
}

Integer federationSize = federatorSupport.getProposedFederationSize()
.orElse(FEDERATION_NON_EXISTENT.getCode());
if (federationSize == FEDERATION_NON_EXISTENT.getCode()) {
Expand Down Expand Up @@ -181,7 +186,9 @@ public Optional<Federation> getProposedFederation() {

@Override
public Optional<Address> getProposedFederationAddress() {
return federatorSupport.getProposedFederationAddress();
return Optional.of(federatorSupport)
.filter(fedSupport -> fedSupport.getConfigForBestBlock().isActive(RSKIP419))
.flatMap(FederatorSupport::getProposedFederationAddress);
}

private Federation getExpectedFederation(Federation initialFederation, Address expectedFederationAddress) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static co.rsk.peg.federation.FederationChangeResponseCode.FEDERATION_NON_EXISTENT;
import static org.ethereum.config.blockchain.upgrades.ConsensusRule.RSKIP123;
import static org.ethereum.config.blockchain.upgrades.ConsensusRule.RSKIP284;
import static org.ethereum.config.blockchain.upgrades.ConsensusRule.RSKIP419;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand Down Expand Up @@ -374,6 +375,9 @@ void getRetiringFederation_present_p2sh_erp_federation() {
@Test
void getProposedFederation_whenProposedFederationSizeIsNonExistent_shouldReturnEmptyOptional() {
// Arrange
ActivationConfig.ForBlock configMock = mock(ActivationConfig.ForBlock.class);
when(configMock.isActive(RSKIP419)).thenReturn(true);
when(federatorSupportMock.getConfigForBestBlock()).thenReturn(configMock);
when(federatorSupportMock.getProposedFederationSize())
.thenReturn(Optional.of(FEDERATION_NON_EXISTENT.getCode()));

Expand All @@ -385,6 +389,9 @@ void getProposedFederation_whenProposedFederationSizeIsNonExistent_shouldReturnE
@Test
void getProposedFederation_whenSomeDataDoesNotExists_shouldThrowIllegalStateException() {
// Arrange
ActivationConfig.ForBlock configMock = mock(ActivationConfig.ForBlock.class);
when(configMock.isActive(RSKIP419)).thenReturn(true);
when(federatorSupportMock.getConfigForBestBlock()).thenReturn(configMock);
Federation expectedFederation = createP2shErpFederation(
getFederationMembersFromPks(1, 1000, 2000, 3000, 4000, 5000));
Address expectedFederationAddress = expectedFederation.getAddress();
Expand All @@ -404,6 +411,9 @@ void getProposedFederation_whenSomeDataDoesNotExists_shouldThrowIllegalStateExce
@Test
void getProposedFederation_whenExistsAndIsP2shErpFederation_shouldReturnProposedFederation() {
// Arrange
ActivationConfig.ForBlock configMock = mock(ActivationConfig.ForBlock.class);
when(configMock.isActive(RSKIP419)).thenReturn(true);
when(federatorSupportMock.getConfigForBestBlock()).thenReturn(configMock);
Federation expectedFederation = createP2shErpFederation(
getFederationMembersFromPks(1, 1000, 2000, 3000, 4000, 5000));
Address expectedFederationAddress = expectedFederation.getAddress();
Expand Down Expand Up @@ -432,9 +442,26 @@ void getProposedFederation_whenExistsAndIsP2shErpFederation_shouldReturnProposed
assertEquals(expectedFederationAddress, proposedFederation.get().getAddress());
}

@Test
void getProposedFederation_whenRSKIP419IsNotActivated_shouldReturnEmptyOptional() {
// Arrange
ActivationConfig.ForBlock configMock = mock(ActivationConfig.ForBlock.class);
when(configMock.isActive(RSKIP419)).thenReturn(false);
when(federatorSupportMock.getConfigForBestBlock()).thenReturn(configMock);

// Act
Optional<Federation> result = federationProvider.getProposedFederation();

// Assert
assertFalse(result.isPresent());
}

@Test
void getProposedFederationAddress_whenAddressExists_shouldReturnAddress() {
// Arrange
ActivationConfig.ForBlock configMock = mock(ActivationConfig.ForBlock.class);
when(configMock.isActive(RSKIP419)).thenReturn(true);
when(federatorSupportMock.getConfigForBestBlock()).thenReturn(configMock);
when(federatorSupportMock.getProposedFederationAddress()).thenReturn(Optional.of(DEFAULT_ADDRESS));

// Act
Expand All @@ -448,6 +475,9 @@ void getProposedFederationAddress_whenAddressExists_shouldReturnAddress() {
@Test
void getProposedFederationAddress_whenNoAddressExists_shouldReturnEmptyOptional() {
// Arrange
ActivationConfig.ForBlock configMock = mock(ActivationConfig.ForBlock.class);
when(configMock.isActive(RSKIP419)).thenReturn(true);
when(federatorSupportMock.getConfigForBestBlock()).thenReturn(configMock);
when(federatorSupportMock.getProposedFederationAddress()).thenReturn(Optional.empty());

// Act
Expand All @@ -456,6 +486,20 @@ void getProposedFederationAddress_whenNoAddressExists_shouldReturnEmptyOptional(
// Assert
assertFalse(result.isPresent());
}

@Test
void getProposedFederationAddress_whenRSKIP419IsNotActivated_shouldReturnEmptyOptional() {
// Arrange
ActivationConfig.ForBlock configMock = mock(ActivationConfig.ForBlock.class);
when(configMock.isActive(RSKIP419)).thenReturn(false);
when(federatorSupportMock.getConfigForBestBlock()).thenReturn(configMock);

// Act
Optional<Address> result = federationProvider.getProposedFederationAddress();

// Assert
assertFalse(result.isPresent());
}

private Federation createFederation(List<FederationMember> members) {
FederationArgs federationArgs = new FederationArgs(members, creationTime, 0L, testnetParams);
Expand Down

0 comments on commit 6abadbf

Please sign in to comment.