Skip to content

Commit

Permalink
feat: changed method getLockWhitelistEntryByIndex so it returns an op…
Browse files Browse the repository at this point in the history
…tional
  • Loading branch information
julianlen authored and marcos-iov committed Jan 14, 2025
1 parent 970a9b6 commit d4e78c6
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 47 deletions.
32 changes: 15 additions & 17 deletions rskj-core/src/main/java/co/rsk/peg/Bridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -1037,14 +1037,11 @@ public String getLockWhitelistAddress(Object[] args) {
logger.trace("getLockWhitelistAddress");

int index = ((BigInteger) args[0]).intValue();
LockWhitelistEntry entry = bridgeSupport.getLockWhitelistEntryByIndex(index);
Optional<LockWhitelistEntry> entry = bridgeSupport.getLockWhitelistEntryByIndex(index);

if (entry == null) {
// Empty string is returned when address is not found
return "";
}
// Empty string is returned when address is not found
return entry.map(lockWhitelistEntry -> lockWhitelistEntry.address().toBase58()).orElse("");

return entry.address().toBase58();
}

public long getLockWhitelistEntryByAddress(Object[] args) {
Expand All @@ -1058,18 +1055,19 @@ public long getLockWhitelistEntryByAddress(Object[] args) {
return WhitelistResponseCode.INVALID_ADDRESS_FORMAT.getCode();
}

Optional<LockWhitelistEntry> entry = bridgeSupport.getLockWhitelistEntryByAddress(addressBase58);

if (entry.isEmpty()) {
// Empty string is returned when address is not found
logger.debug("[getLockWhitelistEntryByAddress] Address not found: {}", addressBase58);
return WhitelistResponseCode.ADDRESS_NOT_EXIST.getCode();
}
return bridgeSupport.getLockWhitelistEntryByAddress(addressBase58)
.map(lockWhitelistEntry -> {
if (lockWhitelistEntry.getClass() == OneOffWhiteListEntry.class) {
return ((OneOffWhiteListEntry) lockWhitelistEntry).maxTransferValue().getValue();
}

LockWhitelistEntry lockWhitelistEntry = entry.get();
return lockWhitelistEntry.getClass() == OneOffWhiteListEntry.class ?
((OneOffWhiteListEntry) lockWhitelistEntry).maxTransferValue().getValue() :
WhitelistResponseCode.UNLIMITED_MODE.getCode();
return WhitelistResponseCode.UNLIMITED_MODE.getCode();
})
.orElseGet(() -> {
// Empty string is returned when address is not found
logger.debug("[getLockWhitelistEntryByAddress] Address not found: {}", addressBase58);
return WhitelistResponseCode.ADDRESS_NOT_EXIST.getCode();
}).longValue();
}

public Integer addOneOffLockWhitelistAddress(Object[] args) {
Expand Down
2 changes: 1 addition & 1 deletion rskj-core/src/main/java/co/rsk/peg/BridgeSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -2121,7 +2121,7 @@ public Integer getLockWhitelistSize() {
return whitelistSupport.getLockWhitelistSize();
}

public LockWhitelistEntry getLockWhitelistEntryByIndex(int index) {
public Optional<LockWhitelistEntry> getLockWhitelistEntryByIndex(int index) {
return whitelistSupport.getLockWhitelistEntryByIndex(index);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ public interface WhitelistSupport {
int getLockWhitelistSize();

/**
* Returns the lock whitelist entry stored at the given index, or null if the index is out of
* Returns the lock whitelist entry stored at the given index, or an empty Optional if the index is out of
* bounds
*
* @param index the index at which to get the entry
* @return the whitelist entry stored at the given index, or null if index is out of
* @return the whitelist entry stored at the given index, or an empty Optional if index is out of
* bounds
*/
LockWhitelistEntry getLockWhitelistEntryByIndex(int index);
Optional<LockWhitelistEntry> getLockWhitelistEntryByIndex(int index);

/**
* Returns the lock whitelist entry for a given address, or null if the address is not whitelisted
* Returns the lock whitelist entry for a given address, or an empty Optional if the address is not whitelisted
*
* @param addressBase58 the address in base58 format to search for
* @return the whitelist entry for the given address, or null if the addrres is not whitelisted
* @return the whitelist entry for the given address, or an empty Optional if the addrres is not whitelisted
*/
Optional<LockWhitelistEntry> getLockWhitelistEntryByAddress(String addressBase58);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ public int getLockWhitelistSize() {
}

@Override
public LockWhitelistEntry getLockWhitelistEntryByIndex(int index) {
public Optional<LockWhitelistEntry> getLockWhitelistEntryByIndex(int index) {
List<LockWhitelistEntry> entries = storageProvider.getLockWhitelist(
activations,
networkParameters
).getAll();

if (index < 0 || index >= entries.size()) {
return null;
return Optional.empty();
}
return entries.get(index);
return Optional.ofNullable(entries.get(index));
}

@Override
Expand Down
11 changes: 7 additions & 4 deletions rskj-core/src/test/java/co/rsk/peg/BridgeSupportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,18 +203,21 @@ void getLockWhitelistSize() {
@Test
void getLockWhitelistEntryByIndex() {
LockWhitelistEntry entry = mock(LockWhitelistEntry.class);
when(whitelistSupport.getLockWhitelistEntryByIndex(0)).thenReturn(entry);
when(whitelistSupport.getLockWhitelistEntryByIndex(0)).thenReturn(Optional.of(entry));

assertEquals(entry, bridgeSupport.getLockWhitelistEntryByIndex(0));
Optional<LockWhitelistEntry> lockWhitelistEntryByIndex = bridgeSupport.getLockWhitelistEntryByIndex(0);
assertTrue(lockWhitelistEntryByIndex.isPresent());
assertEquals(entry, lockWhitelistEntryByIndex.get());
}

@Test
void getLockWhitelistEntryByAddress() {
LockWhitelistEntry entry = mock(LockWhitelistEntry.class);
when(whitelistSupport.getLockWhitelistEntryByAddress("address")).thenReturn(Optional.of(entry));

assertTrue(bridgeSupport.getLockWhitelistEntryByAddress("address").isPresent());
assertEquals(entry, bridgeSupport.getLockWhitelistEntryByAddress("address").get());
Optional<LockWhitelistEntry> lockWhitelistEntry = bridgeSupport.getLockWhitelistEntryByAddress("address");
assertTrue(lockWhitelistEntry.isPresent());
assertEquals(entry, lockWhitelistEntry.get());
}

@Test
Expand Down
8 changes: 4 additions & 4 deletions rskj-core/src/test/java/co/rsk/peg/BridgeTestIntegration.java
Original file line number Diff line number Diff line change
Expand Up @@ -2180,8 +2180,8 @@ void getLockWhitelistAddress() {
OneOffWhiteListEntry mockedEntry10 = new OneOffWhiteListEntry(new BtcECKey().toAddress(networkParameters), Coin.COIN);
OneOffWhiteListEntry mockedEntry20 = new OneOffWhiteListEntry(new BtcECKey().toAddress(networkParameters), Coin.COIN);
TestUtils.setInternalState(bridge, "bridgeSupport", bridgeSupportMock);
when(bridgeSupportMock.getLockWhitelistEntryByIndex(10)).then((InvocationOnMock invocation) -> mockedEntry10);
when(bridgeSupportMock.getLockWhitelistEntryByIndex(20)).then((InvocationOnMock invocation) -> mockedEntry20);
when(bridgeSupportMock.getLockWhitelistEntryByIndex(10)).then((InvocationOnMock invocation) -> Optional.of(mockedEntry10));
when(bridgeSupportMock.getLockWhitelistEntryByIndex(20)).then((InvocationOnMock invocation) -> Optional.of(mockedEntry20));

assertEquals(mockedEntry10.address().toBase58(), bridge.getLockWhitelistAddress(new Object[]{BigInteger.valueOf(10)}));
assertEquals(mockedEntry20.address().toBase58(), bridge.getLockWhitelistAddress(new Object[]{BigInteger.valueOf(20)}));
Expand Down Expand Up @@ -2227,9 +2227,9 @@ void getLockWhitelistEntryByAddressAfterRskip87Fork() throws Exception {

BridgeSupport bridgeSupportMock = mock(BridgeSupport.class);
when(bridgeSupportMock.getLockWhitelistEntryByAddress(mockedAddressForUnlimited.toBase58()))
.then((InvocationOnMock invocation) -> new UnlimitedWhiteListEntry(mockedAddressForUnlimited));
.then((InvocationOnMock invocation) -> Optional.of(new UnlimitedWhiteListEntry(mockedAddressForUnlimited)));
when(bridgeSupportMock.getLockWhitelistEntryByAddress(mockedAddressForOneOff.toBase58()))
.then((InvocationOnMock invocation) -> new OneOffWhiteListEntry(mockedAddressForOneOff, Coin.COIN));
.then((InvocationOnMock invocation) -> Optional.of(new OneOffWhiteListEntry(mockedAddressForOneOff, Coin.COIN)));

mockedTransaction = mock(Transaction.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static co.rsk.peg.whitelist.WhitelistStorageIndexKey.LOCK_ONE_OFF;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import co.rsk.bitcoinj.core.Address;
import co.rsk.bitcoinj.core.Coin;
Expand Down Expand Up @@ -67,32 +66,33 @@ void getLockWhitelistSize_whenLockWhitelistHasEntries_shouldReturnOne() {
}

@Test
void getLockWhitelistEntryByIndex_whenLockWhitelistIsEmpty_shouldReturnNull() {
LockWhitelistEntry actualEntry = whitelistSupport.getLockWhitelistEntryByIndex(0);
void getLockWhitelistEntryByIndex_whenLockWhitelistIsEmpty_shouldReturnAnEmptyOptional() {
Optional<LockWhitelistEntry> actualEntry = whitelistSupport.getLockWhitelistEntryByIndex(0);

assertNull(actualEntry);
assertTrue(actualEntry.isEmpty());
}

@Test
void getLockWhitelistEntryByIndex_whenLockWhitelistHasEntries_shouldReturnOneOffWhiteListEntry() {
saveInMemoryStorageOneOffWhiteListEntry();

LockWhitelistEntry actualLockWhitelistEntry = whitelistSupport.getLockWhitelistEntryByIndex(0);
Optional<LockWhitelistEntry> actualLockWhitelistEntry = whitelistSupport.getLockWhitelistEntryByIndex(0);

assertEquals(btcAddress, actualLockWhitelistEntry.address());
assertTrue(actualLockWhitelistEntry.isPresent());
assertEquals(btcAddress, actualLockWhitelistEntry.get().address());
}

@Test
void getLockWhitelistEntryByIndex_whenIndexIsOutOfBounds_shouldReturnNull() {
void getLockWhitelistEntryByIndex_whenIndexIsOutOfBounds_shouldReturnAnEmptyOptional() {
saveInMemoryStorageOneOffWhiteListEntry();

LockWhitelistEntry actualLockWhitelistEntry = whitelistSupport.getLockWhitelistEntryByIndex(1);
Optional<LockWhitelistEntry> actualLockWhitelistEntry = whitelistSupport.getLockWhitelistEntryByIndex(1);

assertNull(actualLockWhitelistEntry);
assertTrue(actualLockWhitelistEntry.isEmpty());
}

@Test
void getLockWhitelistEntryByAddress_whenLockWhitelistIsEmpty_shouldReturnNull() {
void getLockWhitelistEntryByAddress_whenLockWhitelistIsEmpty_shouldReturnAnEmptyOptional() {
Optional<LockWhitelistEntry> actualEntry = whitelistSupport.getLockWhitelistEntryByAddress(btcAddress.toString());

assertTrue(actualEntry.isEmpty());
Expand Down Expand Up @@ -123,7 +123,7 @@ private void saveInMemoryStorageOneOffWhiteListEntry() {
}

@Test
void getLockWhitelistEntryByAddress_whenAddressIsInvalid_shouldReturnNull() {
void getLockWhitelistEntryByAddress_whenAddressIsInvalid_shouldReturnAnEmptyOptional() {
Optional<LockWhitelistEntry> actualLockWhitelistEntry = whitelistSupport.getLockWhitelistEntryByAddress("invalidAddress");
assertTrue(actualLockWhitelistEntry.isEmpty());
}
Expand Down Expand Up @@ -318,8 +318,8 @@ void save_whenLockWhitelistIsNull_shouldReturnZeroEntries() {

int actualSize = whitelistSupport.getLockWhitelistSize();
assertEquals(0, actualSize);
assertNull(whitelistSupport.getLockWhitelistEntryByIndex(0));
assertNull(whitelistSupport.getLockWhitelistEntryByIndex(1));
assertTrue(whitelistSupport.getLockWhitelistEntryByIndex(0).isEmpty());
assertTrue(whitelistSupport.getLockWhitelistEntryByIndex(1).isEmpty());
}

@Test
Expand All @@ -331,6 +331,8 @@ void save_whenOneOffLockWhitelistAddressIsWhitelisted_shouldSaveOneOffLockWhitel

int actualSize = whitelistSupport.getLockWhitelistSize();
Optional<LockWhitelistEntry> lockWhitelistEntry = whitelistSupport.getLockWhitelistEntryByAddress(btcAddress.toString());
assertTrue(lockWhitelistEntry.isPresent());

Address actualBtcAddress = lockWhitelistEntry.get().address();
assertEquals(1, actualSize);
assertEquals(btcAddress, actualBtcAddress);
Expand All @@ -345,6 +347,8 @@ void save_whenUnlimitedLockWhitelistAddressIsWhitelisted_shouldSaveUnlimitedLock

int actualSize = whitelistSupport.getLockWhitelistSize();
Optional<LockWhitelistEntry> lockWhitelistEntry = whitelistSupport.getLockWhitelistEntryByAddress(btcAddress.toString());
assertTrue(lockWhitelistEntry.isPresent());

Address actualBtcAddress = lockWhitelistEntry.get().address();
assertEquals(1, actualSize);
assertEquals(btcAddress, actualBtcAddress);
Expand All @@ -360,8 +364,11 @@ void save_whenOneOffAndUnlimitedLockWhitelistAddressesAreWhitelisted_shouldSaveB

int actualSize = whitelistSupport.getLockWhitelistSize();
Optional<LockWhitelistEntry> lockWhitelistEntryBtcAddress = whitelistSupport.getLockWhitelistEntryByAddress(btcAddress.toString());
assertTrue(lockWhitelistEntryBtcAddress.isPresent());
Address actualBtcAddress = lockWhitelistEntryBtcAddress.get().address();

Optional<LockWhitelistEntry> lockWhitelistEntrySecondBtcAddress = whitelistSupport.getLockWhitelistEntryByAddress(secondBtcAddress.toString());
assertTrue(lockWhitelistEntrySecondBtcAddress.isPresent());
Address actualSecondBtcAddress = lockWhitelistEntrySecondBtcAddress.get().address();
assertEquals(2, actualSize);
assertEquals(btcAddress, actualBtcAddress);
Expand Down

0 comments on commit d4e78c6

Please sign in to comment.