Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix wrong casting to long #2841

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions rskj-core/src/main/java/co/rsk/peg/Bridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -1085,22 +1085,22 @@ public int getProposedFederationSize(Object[] args) {
}

/**
* Retrieves the creation time of the proposed federation in milliseconds since the epoch.
* Retrieves the creation time of the proposed federation in seconds since the epoch.
*
* <p>
* This method checks if a proposed federation exists and returns its creation time in
* milliseconds since the Unix epoch. If no proposed federation exists, it returns -1.
* seconds since the Unix epoch. If no proposed federation exists, it returns -1.
* </p>
*
* @param args unused arguments for this method (can be null or empty).
* @return the creation time of the proposed federation in milliseconds since the epoch,
* @return the creation time of the proposed federation in seconds since the epoch,
* or -1 if no proposed federation exists.
*/
public Long getProposedFederationCreationTime(Object[] args) {
logger.trace("getProposedFederationCreationTime");

return bridgeSupport.getProposedFederationCreationTime()
.map(Instant::toEpochMilli)
.map(Instant::getEpochSecond)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now I wonder if we should also apply this change to active and retiring feds

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about that. I can create a task in the backlog so we dont forget

.orElse(-1L);
}

Expand Down
16 changes: 10 additions & 6 deletions rskj-core/src/main/java/co/rsk/peg/BridgeSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -1019,13 +1019,17 @@ private void logUpdateCollections(Transaction rskTx) {

private boolean svpIsOngoing() {
return federationSupport.getProposedFederation()
.map(Federation::getCreationBlockNumber)
.map(proposedFederationCreationBlockNumber ->
proposedFederationCreationBlockNumber + bridgeConstants.getFederationConstants().getValidationPeriodDurationInBlocks())
.filter(validationPeriodEndBlock -> rskExecutionBlock.getNumber() <= validationPeriodEndBlock)
.filter(this::validationPeriodIsOngoing)
.isPresent();
}

private boolean validationPeriodIsOngoing(Federation proposedFederation) {
long validationPeriodEndBlock = proposedFederation.getCreationBlockNumber() +
bridgeConstants.getFederationConstants().getValidationPeriodDurationInBlocks();

return rskExecutionBlock.getNumber() < validationPeriodEndBlock;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also replaced the <= for a < to follow the same logic as in migration period (i.e. not considering the ending block as part of the period)

}

protected void processSvpFundTransactionUnsigned(Transaction rskTx) throws IOException, InsufficientMoneyException {
Optional<Federation> proposedFederationOpt = federationSupport.getProposedFederation();
if (proposedFederationOpt.isEmpty()) {
Expand Down Expand Up @@ -1119,10 +1123,10 @@ private void addSvpSpendTransactionInputs(BtcTransaction svpSpendTransaction, Bt

private Coin calculateSvpSpendTxAmount(Federation proposedFederation) {
int svpSpendTransactionSize = calculatePegoutTxSize(activations, proposedFederation, 2, 1);
long backupSizePercentage = (long) 1.2; // just to be sure the amount sent will be enough
long svpSpendTransactionBackedUpSize = svpSpendTransactionSize * 12L / 10L; // just to be sure the amount sent will be enough

return feePerKbSupport.getFeePerKb()
.multiply(svpSpendTransactionSize * backupSizePercentage)
.multiply(svpSpendTransactionBackedUpSize)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.multiply(svpSpendTransactionBackedUpSize)
.multiply(svpSpendTransactionBackedUpSize)
.multiply(1200) // Add 20% just to be sure the amount sent will be enough
.divide(1000)

Just an idea, maybe we encapsulate the increase here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i found the other way clearer. We would end up having two .divide(1000) in a row, kind of confusing. Wdyt?

.divide(1000);
}

Expand Down
19 changes: 12 additions & 7 deletions rskj-core/src/test/java/co/rsk/peg/BridgeSupportSvpTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public class BridgeSupportSvpTest {
private static final NetworkParameters btcMainnetParams = bridgeMainNetConstants.getBtcParams();
private static final FederationConstants federationMainNetConstants = bridgeMainNetConstants.getFederationConstants();

private static final Coin feePerKb = Coin.valueOf(1000L);

private static final Coin spendableValueFromProposedFederation = bridgeMainNetConstants.getSpendableValueFromProposedFederation();

private final BridgeSupportBuilder bridgeSupportBuilder = BridgeSupportBuilder.builder();
Expand Down Expand Up @@ -100,7 +102,6 @@ void setUp() {
when(federationSupport.getProposedFederation()).thenReturn(Optional.of(proposedFederation));

feePerKbSupport = mock(FeePerKbSupport.class);
Coin feePerKb = Coin.valueOf(1000);
when(feePerKbSupport.getFeePerKb()).thenReturn(feePerKb);

repository = createRepository();
Expand Down Expand Up @@ -567,8 +568,9 @@ void processSvpSpendTransaction_createsExpectedTransactionAndSavesTheValuesAndLo
assertSvpFundTxSignedWasRemovedFromStorage();

assertLogPegoutTransactionCreated(logs, svpSpendTransactionUnsigned);
Coin valueSentToActiveFed = Coin.valueOf(1762);
assertLogReleaseRequested(logs, rskTx.getHash(), svpSpendTransactionHashUnsigned, valueSentToActiveFed);

TransactionOutput outputToActiveFed = svpSpendTransactionUnsigned.getOutput(0);
assertLogReleaseRequested(logs, rskTx.getHash(), svpSpendTransactionHashUnsigned, outputToActiveFed.getValue());
}

private void assertSvpSpendTxHashUnsignedWasSavedInStorage() {
Expand Down Expand Up @@ -605,7 +607,10 @@ private void assertSvpSpendTxHasExpectedInputsAndOutputs() {
List<TransactionOutput> outputs = svpSpendTransactionUnsigned.getOutputs();
assertEquals(1, outputs.size());

Coin expectedAmount = Coin.valueOf(1_762L);
long calculatedTransactionSize = 1762L; // using calculatePegoutTxSize method
Coin expectedAmount = feePerKb
.multiply(calculatedTransactionSize * 12L / 10L) // back up calculation
.divide(1000);
assertOutputWasSentToExpectedScriptWithExpectedAmount(outputs, activeFederation.getP2SHScript(), expectedAmount);
}

Expand Down Expand Up @@ -912,7 +917,8 @@ private void recreateSvpSpendTransaction() {
.setScriptSig(createBaseP2SHInputScriptThatSpendsFromRedeemScript(flyoverRedeemScript));

// add output
svpSpendTx.addOutput(Coin.valueOf(1762), federationSupport.getActiveFederationAddress());
Coin amount = Coin.valueOf(2114L); // previously calculated amount
svpSpendTx.addOutput(amount, federationSupport.getActiveFederationAddress());
}

private void saveSvpSpendTransactionWFSValues() {
Expand All @@ -925,10 +931,9 @@ private void saveSvpSpendTransactionWFSValues() {
private void arrangeExecutionBlockIsAfterValidationPeriodEnded() {
long validationPeriodEndBlock = proposedFederation.getCreationBlockNumber()
+ bridgeMainNetConstants.getFederationConstants().getValidationPeriodDurationInBlocks();
long rskExecutionBlockNumber = validationPeriodEndBlock + 1; // adding one more block to ensure validation period is ended
long rskExecutionBlockTimestamp = 10L;

rskExecutionBlock = createRskBlock(rskExecutionBlockNumber, rskExecutionBlockTimestamp);
rskExecutionBlock = createRskBlock(validationPeriodEndBlock, rskExecutionBlockTimestamp);
}

private BtcTransaction arrangeSvpFundTransactionSigned() {
Expand Down