Skip to content

Commit

Permalink
Merge pull request #2958 from rsksmart/add_activation_code_for_new_fe…
Browse files Browse the repository at this point in the history
…d_creation_time

Add activation code check for federation being built from pending one creation time
  • Loading branch information
marcos-iov authored Jan 24, 2025
2 parents f68c7ca + 5023294 commit db10a59
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -767,12 +767,20 @@ private FederationChangeResponseCode commitPendingFederation(PendingFederation c
}

private Federation buildFederationFromPendingFederation(PendingFederation pendingFederation) {
Instant federationCreationTime = Instant.ofEpochSecond(rskExecutionBlock.getTimestamp());
Instant federationCreationTime = getFederationCreationTime(rskExecutionBlock.getTimestamp());
long federationCreationBlockNumber = rskExecutionBlock.getNumber();

return pendingFederation.buildFederation(federationCreationTime, federationCreationBlockNumber, constants, activations);
}

private Instant getFederationCreationTime(long rskExecutionBlockTimestamp) {
if (!activations.isActive(RSKIP419)) {
return Instant.ofEpochMilli(rskExecutionBlockTimestamp);
}

return Instant.ofEpochSecond(rskExecutionBlockTimestamp);
}

private 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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,21 +379,21 @@ void voteCommitFederation_preRSKIP186_whenPendingFederationIsSet_shouldPerformLe
voteAndAssertCommitPendingFederation();

// assertions
Federation newFederation = storageProvider.getNewFederation(federationMainnetConstants, activations);
long expectedNewFederationCreationTimeValue = newFederation.getCreationTime().toEpochMilli();
Instant expectedNewFederationCreationTime = Instant.ofEpochMilli(RSK_EXECUTION_BLOCK_TIMESTAMP);
assertIsTheExpectedFederation(newFederation, expectedNewFederationCreationTimeValue, expectedNewFederationCreationTime);

List<UTXO> utxosToMove = new ArrayList<>(storageProvider.getNewFederationBtcUTXOs(federationMainnetConstants.getBtcParams(), activations));
assertUTXOsWereMovedFromNewToOldFederation(utxosToMove);

Federation federationBuiltFromPendingFederation =
pendingFederationToBe.buildFederation(Instant.ofEpochMilli(RSK_EXECUTION_BLOCK_TIMESTAMP), RSK_EXECUTION_BLOCK_NUMBER, federationMainnetConstants, activations);
assertNewAndOldFederationsWereSet(federationBuiltFromPendingFederation);

// pre rskip186 these values are not being set
assertNewActiveFederationCreationBlockHeightWasNotSet();
assertLastRetiredFederationScriptWasNotSet();

assertPendingFederationVotingWasCleaned();

Federation oldFederation = storageProvider.getOldFederation(federationMainnetConstants, activations);
Federation newFederation = storageProvider.getNewFederation(federationMainnetConstants, activations);
assertLogCommitFederation(oldFederation, newFederation);
}

Expand All @@ -417,7 +417,9 @@ void voteCommitFederation_postRSKIP186_preRSKIP419_whenPendingFederationIsSet_sh

// assertions
Federation newFederation = storageProvider.getNewFederation(federationMainnetConstants, activations);
assertIsTheExpectedFederation(newFederation);
long expectedNewFederationCreationTimeValue = newFederation.getCreationTime().toEpochMilli();
Instant expectedNewFederationCreationTime = Instant.ofEpochMilli(RSK_EXECUTION_BLOCK_TIMESTAMP);
assertIsTheExpectedFederation(newFederation, expectedNewFederationCreationTimeValue, expectedNewFederationCreationTime);

List<UTXO> utxosToMove = List.copyOf(storageProvider.getNewFederationBtcUTXOs(federationMainnetConstants.getBtcParams(), activations));
assertHandoverToNewFederation(utxosToMove, newFederation);
Expand All @@ -443,23 +445,26 @@ void voteCommitFederation_postRSKIP419_whenPendingFederationIsSet_shouldPerformC

// assertions
// assert proposed federation was set correctly
Optional<Federation> proposedFederation = storageProvider.getProposedFederation(federationMainnetConstants, activations);
assertTrue(proposedFederation.isPresent());
assertIsTheExpectedFederation(proposedFederation.get());
Optional<Federation> proposedFederationOpt = storageProvider.getProposedFederation(federationMainnetConstants, activations);
assertTrue(proposedFederationOpt.isPresent());
Federation proposedFederation = proposedFederationOpt.get();
long expectedProposedFederationCreationTimeValue = proposedFederation.getCreationTime().getEpochSecond();
Instant expectedProposedFederationCreationTime = Instant.ofEpochSecond(RSK_EXECUTION_BLOCK_TIMESTAMP);
assertIsTheExpectedFederation(proposedFederation, expectedProposedFederationCreationTimeValue, expectedProposedFederationCreationTime);

assertPendingFederationVotingWasCleaned();

assertLogCommitFederation(activeFederation, proposedFederation.get());
assertLogCommitFederation(activeFederation, proposedFederation);

assertNoHandoverToNewFederation();
}

private void assertIsTheExpectedFederation(Federation federation) {
assertEquals(RSK_EXECUTION_BLOCK_TIMESTAMP, federation.getCreationTime().getEpochSecond());
private void assertIsTheExpectedFederation(Federation federation, long expectedCreationTimeValue, Instant expectedCreationTime) {
assertEquals(RSK_EXECUTION_BLOCK_TIMESTAMP, expectedCreationTimeValue);
assertEquals(RSK_EXECUTION_BLOCK_NUMBER, federation.getCreationBlockNumber());

Federation federationBuiltFromPendingFederation = pendingFederationToBe.buildFederation(
Instant.ofEpochSecond(RSK_EXECUTION_BLOCK_TIMESTAMP),
expectedCreationTime,
RSK_EXECUTION_BLOCK_NUMBER,
federationMainnetConstants,
activations
Expand Down

0 comments on commit db10a59

Please sign in to comment.