Skip to content

Commit

Permalink
Merge pull request #2678 from rsksmart/feature/locking-cap-integratio…
Browse files Browse the repository at this point in the history
…n-test

LockingCap Integration Test
  • Loading branch information
marcos-iov authored Aug 12, 2024
2 parents 2dd40f7 + 9bf6802 commit 84a7135
Show file tree
Hide file tree
Showing 4 changed files with 285 additions and 14 deletions.
2 changes: 1 addition & 1 deletion rskj-core/src/test/java/co/rsk/peg/BridgeSupportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ void getLockingCap_whenLockingCapIsEmpty_shouldReturnNull() {
void increaseLockingCap() throws VMException {
// Arrange
Coin newLockingCap = constants.getInitialValue().add(Coin.SATOSHI);
Transaction tx = TransactionUtils.getTransactionFromCaller(signatureCache, LockingCapCaller.AUTHORIZED.getRskAddress());
Transaction tx = TransactionUtils.getTransactionFromCaller(signatureCache, LockingCapCaller.FIRST_AUTHORIZED.getRskAddress());
when(lockingCapSupport.increaseLockingCap(tx, newLockingCap)).thenReturn(true);
when(lockingCapSupport.getLockingCap()).thenReturn(Optional.of(newLockingCap));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import co.rsk.core.RskAddress;

public enum LockingCapCaller {
AUTHORIZED("a02db0ed94a5894bc6f9079bb9a2d93ada1917f3"),
FIRST_AUTHORIZED("a02db0ed94a5894bc6f9079bb9a2d93ada1917f3"),
SECOND_AUTHORIZED("180a7edda4e640ea5a3e495e17a1efad260c39e9"),
THIRD_AUTHORIZED("8418edc8fea47183116b4c8cd6a12e51a7e169c1"),
UNAUTHORIZED("e2a5070b4e2cb77fe22dff05d9dcdc4d3eaa6ead");

private final String rskAddress;
Expand Down
269 changes: 269 additions & 0 deletions rskj-core/src/test/java/co/rsk/peg/lockingcap/LockingCapIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
package co.rsk.peg.lockingcap;

import static org.junit.jupiter.api.Assertions.*;

import co.rsk.bitcoinj.core.Coin;
import co.rsk.net.utils.TransactionUtils;
import co.rsk.peg.lockingcap.constants.LockingCapConstants;
import co.rsk.peg.lockingcap.constants.LockingCapMainNetConstants;
import co.rsk.peg.storage.InMemoryStorage;
import co.rsk.peg.storage.StorageAccessor;
import java.util.Optional;
import org.ethereum.config.blockchain.upgrades.ActivationConfig;
import org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest;
import org.ethereum.core.*;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.TestInstance.Lifecycle;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@TestInstance(Lifecycle.PER_CLASS)
class LockingCapIT {
private final LockingCapConstants constants = LockingCapMainNetConstants.getInstance();
private LockingCapSupport lockingCapSupport;
private LockingCapStorageProvider lockingCapStorageProvider;
private SignatureCache signatureCache;
private StorageAccessor bridgeStorageAccessor;
private Coin currentLockingCap; // it is used to guarantee the value from getLockingCap() contains the value expected

@BeforeAll
void setUp() {
bridgeStorageAccessor = new InMemoryStorage();
lockingCapStorageProvider = new LockingCapStorageProviderImpl(bridgeStorageAccessor);
signatureCache = new BlockTxSignatureCache(new ReceivedTxSignatureCache());

ActivationConfig.ForBlock activations = ActivationConfigsForTest.all().forBlock(0);
lockingCapSupport = new LockingCapSupportImpl(
lockingCapStorageProvider,
activations,
constants,
signatureCache
);
}

@Test
@Order(-1)
void increaseLockingCap_prePapyrus200_whenLockingCapIsNotSet_shouldNotSavedNewLockingCapValue() throws LockingCapIllegalArgumentException {
// Arrange
// Previously to the genesis of the Locking Cap
ActivationConfig.ForBlock wasabiActivations = ActivationConfigsForTest.wasabi100().forBlock(0);
LockingCapSupport wasabiLockingCapSupport = new LockingCapSupportImpl(
lockingCapStorageProvider,
wasabiActivations,
constants,
signatureCache
);

// Setting up new Locking Cap value
Coin newLockingCap = constants.getInitialValue();
Transaction tx = TransactionUtils.getTransactionFromCaller(
signatureCache,
LockingCapCaller.FIRST_AUTHORIZED.getRskAddress()
);

// Act
boolean isIncreased = wasabiLockingCapSupport.increaseLockingCap(tx, newLockingCap);

// Assert
assertFalse(isIncreased);
}

@Test
@Order(0)
void getInitialValue_whenFirstTimeGettingLockingCap_shouldReturnInitialValue() {
// The first time the Locking Cap is requested, it should return the initial value
Coin expectedLockingCap = constants.getInitialValue();

// Actual / Assert
assertLockingCapValue(expectedLockingCap);
}

@Test
@Order(1)
void increaseLockingCap_whenNewValueIsGreaterThanCurrentLockingCap_shouldSaveNewLockingCapValue() throws LockingCapIllegalArgumentException {
// Arrange
Coin newLockingCap = currentLockingCap.add(Coin.COIN);

// Act
boolean isIncreased = voteToIncreaseLockingCap(newLockingCap);

// Assert
assertTrue(isIncreased);
assertLockingCapValue(newLockingCap);
}

@Test
@Order(2)
void increaseLockingCap_whenPreviousValueExistsInStorageAndNewLockingCapIsLessThanPreviousValue_shouldNotSaveNewLockingCapValue() throws LockingCapIllegalArgumentException {
// Arrange
Coin newLockingCap = currentLockingCap.subtract(Coin.COIN);

// Act
boolean isIncreased = voteToIncreaseLockingCap(newLockingCap);

// Assert
assertFalse(isIncreased);
assertLockingCapValue(currentLockingCap);
}

@Test
@Order(3)
void increaseLockingCap_whenPreviousValueExistsInStorageAndNewLockingCapIsGreaterThanPreviousValue_shouldSaveNewLockingCapValue() throws LockingCapIllegalArgumentException {
// Arrange
Coin newLockingCap = currentLockingCap.add(Coin.COIN);

// Act
boolean isIncreased = voteToIncreaseLockingCap(newLockingCap);

// Assert
assertTrue(isIncreased);
assertLockingCapValue(newLockingCap);
}

@Test
@Order(4)
void increaseLockingCap_whenAnUnauthorizedCallerRequestToIncreaseLockingCapValue_shouldNotSaveNewLockingCapValue() throws LockingCapIllegalArgumentException {
// Arrange
Coin newLockingCap = currentLockingCap.add(Coin.COIN);
Transaction tx = TransactionUtils.getTransactionFromCaller(
signatureCache,
LockingCapCaller.UNAUTHORIZED.getRskAddress()
);

// Act
boolean isIncreased = lockingCapSupport.increaseLockingCap(tx, newLockingCap);

// Assert
assertFalse(isIncreased);
assertLockingCapValue(currentLockingCap);
}

@Test
@Order(5)
void increaseLockingCap_whenSecondAuthorizedCallerRequestToIncreaseLockingCapValue_shouldSaveNewLockingCapValue() throws LockingCapIllegalArgumentException {
// Arrange
Coin newLockingCap = currentLockingCap.add(Coin.COIN);

// Act
boolean isIncreased = voteToIncreaseLockingCap(newLockingCap, LockingCapCaller.SECOND_AUTHORIZED);

// Assert
assertTrue(isIncreased);
assertLockingCapValue(newLockingCap);
}

@Test
@Order(6)
void increaseLockingCap_whenThirdAuthorizedCallerRequestToIncreaseLockingCapValue_shouldSaveNewLockingCapValue() throws LockingCapIllegalArgumentException {
// Arrange
Coin newLockingCap = currentLockingCap.add(Coin.COIN);

// Act
boolean isIncreased = voteToIncreaseLockingCap(newLockingCap, LockingCapCaller.THIRD_AUTHORIZED);

// Assert
assertTrue(isIncreased);
assertLockingCapValue(newLockingCap);
}

@Test
@Order(7)
void increaseLockingCap_whenNewLockingCapIsGreaterThanMaxLockingCap_shouldNotSaveNewLockingCapValue() throws LockingCapIllegalArgumentException {
// Arrange
Coin maxLockingCapVoteValueAllowed = currentLockingCap.multiply(constants.getIncrementsMultiplier());
Coin newLockingCap = maxLockingCapVoteValueAllowed.add(Coin.SATOSHI);

// Act
boolean isIncreased = voteToIncreaseLockingCap(newLockingCap);

// Assert
assertFalse(isIncreased);
assertLockingCapValue(currentLockingCap);
}

@Test
@Order(8)
void increaseLockingCap_whenNewLockingCapIsEqualToMaxLockingCap_shouldSaveNewLockingCapValue() throws LockingCapIllegalArgumentException {
// Arrange
Coin newLockingCap = currentLockingCap.multiply(constants.getIncrementsMultiplier());

// Act
boolean isIncreased = voteToIncreaseLockingCap(newLockingCap);

// Assert
assertTrue(isIncreased);
assertLockingCapValue(newLockingCap);
}

@Test
@Order(9)
void increaseLockingCap_whenNewLockingCapIsZero_shouldThrowLockingCapIllegalArgumentException() {
// Arrange
Coin newLockingCap = Coin.ZERO;

// Actual / Assert
assertThrows(
LockingCapIllegalArgumentException.class,
() -> voteToIncreaseLockingCap(newLockingCap)
);
assertLockingCapValue(currentLockingCap);
}

@Test
@Order(10)
void increaseLockingCap_whenNewLockingCapIsNegative_shouldThrowLockingCapIllegalArgumentException() {
// Arrange
Coin newLockingCap = Coin.NEGATIVE_SATOSHI;

// Actual / Assert
assertThrows(
LockingCapIllegalArgumentException.class,
() -> voteToIncreaseLockingCap(newLockingCap)
);
assertLockingCapValue(currentLockingCap);
}

@Test
@Order(11)
void increaseLockingCap_whenNewValueIsEqualToCurrentValue_shouldSaveNewLockingCapValue() throws LockingCapIllegalArgumentException {
// Arrange
Coin newLockingCap = currentLockingCap;

// Act
boolean isIncreased = voteToIncreaseLockingCap(newLockingCap);

// Assert
assertTrue(isIncreased);
assertLockingCapValue(newLockingCap);
}

private boolean voteToIncreaseLockingCap(Coin valueToVote) throws LockingCapIllegalArgumentException {
return voteToIncreaseLockingCap(valueToVote, LockingCapCaller.FIRST_AUTHORIZED);
}

private boolean voteToIncreaseLockingCap(Coin valueToVote, LockingCapCaller caller) throws LockingCapIllegalArgumentException {
Transaction tx = TransactionUtils.getTransactionFromCaller(
signatureCache,
caller.getRskAddress()
);

boolean result = lockingCapSupport.increaseLockingCap(tx, valueToVote);
lockingCapSupport.save();

return result;
}

private void assertLockingCapValue(Coin expectedLockingCap) {
// Recreate LockingCapStorageProvider to clear cached value and make sure it is fetched from the storage
lockingCapStorageProvider = new LockingCapStorageProviderImpl(bridgeStorageAccessor);

// Act
Optional<Coin> actualLockingCap = lockingCapSupport.getLockingCap();

// Assert
assertTrue(actualLockingCap.isPresent());
assertEquals(expectedLockingCap, actualLockingCap.get());

// Save the current Locking Cap value to be used in the next test
currentLockingCap = actualLockingCap.get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ void increaseLockingCap_whenNewValueIsGreaterThanCurrentLockingCap_shouldReturnT
throws LockingCapIllegalArgumentException {
// Arrange
Coin newLockingCap = constants.getInitialValue().add(Coin.SATOSHI);
Transaction tx = TransactionUtils.getTransactionFromCaller(signatureCache, LockingCapCaller.AUTHORIZED.getRskAddress());
Transaction tx = TransactionUtils.getTransactionFromCaller(signatureCache, LockingCapCaller.FIRST_AUTHORIZED.getRskAddress());

// Act
boolean actualResult = lockingCapSupport.increaseLockingCap(tx, newLockingCap);
Expand All @@ -99,7 +99,7 @@ void increaseLockingCap_whenNewValueIsLessThanInitialValue_shouldReturnFalse()
throws LockingCapIllegalArgumentException {
// Arrange
Coin newLockingCap = constants.getInitialValue().subtract(Coin.SATOSHI);
Transaction tx = TransactionUtils.getTransactionFromCaller(signatureCache, LockingCapCaller.AUTHORIZED.getRskAddress());
Transaction tx = TransactionUtils.getTransactionFromCaller(signatureCache, LockingCapCaller.FIRST_AUTHORIZED.getRskAddress());

// Act
boolean actualResult = lockingCapSupport.increaseLockingCap(tx, newLockingCap);
Expand All @@ -121,7 +121,7 @@ void increaseLockingCap_whenPreviousValueExistsInStorageAndNewLockingCapIsGreate
lockingCapSupport = new LockingCapSupportImpl(lockingCapStorageProvider, activations, constants, signatureCache);

Coin newLockingCap = previousLockingCap.add(Coin.SATOSHI);
Transaction tx = TransactionUtils.getTransactionFromCaller(signatureCache, LockingCapCaller.AUTHORIZED.getRskAddress());
Transaction tx = TransactionUtils.getTransactionFromCaller(signatureCache, LockingCapCaller.FIRST_AUTHORIZED.getRskAddress());

// Act
boolean actualResult = lockingCapSupport.increaseLockingCap(tx, newLockingCap);
Expand All @@ -143,7 +143,7 @@ void increaseLockingCap_whenPreviousValueExistsInStorageAndNewLockingCapIsLessTh
lockingCapSupport = new LockingCapSupportImpl(lockingCapStorageProvider, activations, constants, signatureCache);

Coin newLockingCap = expectedLockingCap.subtract(Coin.SATOSHI);
Transaction tx = TransactionUtils.getTransactionFromCaller(signatureCache, LockingCapCaller.AUTHORIZED.getRskAddress());
Transaction tx = TransactionUtils.getTransactionFromCaller(signatureCache, LockingCapCaller.FIRST_AUTHORIZED.getRskAddress());

// Act
boolean actualResult = lockingCapSupport.increaseLockingCap(tx, newLockingCap);
Expand Down Expand Up @@ -181,7 +181,7 @@ void increaseLockingCap_whenNewLockingCapIsGreaterThanMaxLockingCap_shouldReturn

Coin maxLockingCapVoteValueAllowed = expectedLockingCap.multiply(constants.getIncrementsMultiplier());
Coin newLockingCap = maxLockingCapVoteValueAllowed.add(Coin.SATOSHI);
Transaction tx = TransactionUtils.getTransactionFromCaller(signatureCache, LockingCapCaller.AUTHORIZED.getRskAddress());
Transaction tx = TransactionUtils.getTransactionFromCaller(signatureCache, LockingCapCaller.FIRST_AUTHORIZED.getRskAddress());

// Act
boolean actualResult = lockingCapSupport.increaseLockingCap(tx, newLockingCap);
Expand All @@ -204,7 +204,7 @@ void increaseLockingCap_whenNewLockingCapIsEqualToMaxLockingCap_shouldReturnTrue

// The new locking cap is the maximum value that can be set
Coin newLockingCap = previousLockingCap.multiply(constants.getIncrementsMultiplier());
Transaction tx = TransactionUtils.getTransactionFromCaller(signatureCache, LockingCapCaller.AUTHORIZED.getRskAddress());
Transaction tx = TransactionUtils.getTransactionFromCaller(signatureCache, LockingCapCaller.FIRST_AUTHORIZED.getRskAddress());

// Act
boolean actualResult = lockingCapSupport.increaseLockingCap(tx, newLockingCap);
Expand All @@ -219,7 +219,7 @@ void increaseLockingCap_whenNewValueIsEqualToCurrentValue_shouldReturnTrue()
throws LockingCapIllegalArgumentException {
// Arrange
Coin newLockingCap = constants.getInitialValue();
Transaction tx = TransactionUtils.getTransactionFromCaller(signatureCache, LockingCapCaller.AUTHORIZED.getRskAddress());
Transaction tx = TransactionUtils.getTransactionFromCaller(signatureCache, LockingCapCaller.FIRST_AUTHORIZED.getRskAddress());

// Act
boolean actualResult = lockingCapSupport.increaseLockingCap(tx, newLockingCap);
Expand All @@ -233,7 +233,7 @@ void increaseLockingCap_whenNewValueIsEqualToCurrentValue_shouldReturnTrue()
void increaseLockingCap_whenNewLockingCapIsZero_shouldReturnFalse() {
// Arrange
Coin newLockingCap = Coin.ZERO;
Transaction tx = TransactionUtils.getTransactionFromCaller(signatureCache, LockingCapCaller.AUTHORIZED.getRskAddress());
Transaction tx = TransactionUtils.getTransactionFromCaller(signatureCache, LockingCapCaller.FIRST_AUTHORIZED.getRskAddress());

// Act / Assert
assertThrows(LockingCapIllegalArgumentException.class, () -> lockingCapSupport.increaseLockingCap(tx, newLockingCap));
Expand All @@ -244,7 +244,7 @@ void increaseLockingCap_whenNewLockingCapIsZero_shouldReturnFalse() {
void increaseLockingCap_whenNewLockingCapIsNegative_shouldReturnFalse() {
// Arrange
Coin newLockingCap = Coin.NEGATIVE_SATOSHI;
Transaction tx = TransactionUtils.getTransactionFromCaller(signatureCache, LockingCapCaller.AUTHORIZED.getRskAddress());
Transaction tx = TransactionUtils.getTransactionFromCaller(signatureCache, LockingCapCaller.FIRST_AUTHORIZED.getRskAddress());

// Act / Assert
assertThrows(LockingCapIllegalArgumentException.class, () -> lockingCapSupport.increaseLockingCap(tx, newLockingCap));
Expand All @@ -258,7 +258,7 @@ void increaseLockingCap_prePapyrus200_whenLockingCapIsNotSet_shouldReturnFalse()
activations = ActivationConfigsForTest.wasabi100().forBlock(0);
lockingCapSupport = new LockingCapSupportImpl(lockingCapStorageProvider, activations, constants, signatureCache);
Coin newLockingCap = constants.getInitialValue().add(Coin.SATOSHI);
Transaction tx = TransactionUtils.getTransactionFromCaller(signatureCache, LockingCapCaller.AUTHORIZED.getRskAddress());
Transaction tx = TransactionUtils.getTransactionFromCaller(signatureCache, LockingCapCaller.FIRST_AUTHORIZED.getRskAddress());

// Act
boolean actualResult = lockingCapSupport.increaseLockingCap(tx, newLockingCap);
Expand All @@ -273,7 +273,7 @@ void save_whenIsIncreasedLockingCapValue_shouldSaveLockingCap()
throws LockingCapIllegalArgumentException {
// Arrange
Coin newLockingCap = constants.getInitialValue().add(Coin.SATOSHI);
Transaction tx = TransactionUtils.getTransactionFromCaller(signatureCache, LockingCapCaller.AUTHORIZED.getRskAddress());
Transaction tx = TransactionUtils.getTransactionFromCaller(signatureCache, LockingCapCaller.FIRST_AUTHORIZED.getRskAddress());
lockingCapSupport.increaseLockingCap(tx, newLockingCap);

// Act
Expand All @@ -293,7 +293,7 @@ void save_prePapyrus200_whenIsAttemptedIncreaseLockingCapValue_shouldNotSaveLock
activations = ActivationConfigsForTest.wasabi100().forBlock(0);
lockingCapSupport = new LockingCapSupportImpl(lockingCapStorageProvider, activations, constants, signatureCache);
Coin newLockingCap = constants.getInitialValue().add(Coin.SATOSHI);
Transaction tx = TransactionUtils.getTransactionFromCaller(signatureCache, LockingCapCaller.AUTHORIZED.getRskAddress());
Transaction tx = TransactionUtils.getTransactionFromCaller(signatureCache, LockingCapCaller.FIRST_AUTHORIZED.getRskAddress());
lockingCapSupport.increaseLockingCap(tx, newLockingCap);

// Act
Expand Down

0 comments on commit 84a7135

Please sign in to comment.