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 8, 2025
1 parent 509fcbb commit ef766e1
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 18 deletions.
6 changes: 3 additions & 3 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,14 @@ 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) {
if (entry.isEmpty()) {
// Empty string is returned when address is not found
return "";
}

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

public long getLockWhitelistEntryByAddress(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 @@ -28,7 +28,7 @@ public interface WhitelistSupport {
* @return the whitelist entry stored at the given index, or null 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
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
6 changes: 4 additions & 2 deletions rskj-core/src/test/java/co/rsk/peg/BridgeSupportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,11 @@ 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,27 +68,27 @@ void getLockWhitelistSize_whenLockWhitelistHasEntries_shouldReturnOne() {

@Test
void getLockWhitelistEntryByIndex_whenLockWhitelistIsEmpty_shouldReturnNull() {
LockWhitelistEntry actualEntry = whitelistSupport.getLockWhitelistEntryByIndex(0);
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());
assertEquals(btcAddress, actualLockWhitelistEntry.get().address());
}

@Test
void getLockWhitelistEntryByIndex_whenIndexIsOutOfBounds_shouldReturnNull() {
saveInMemoryStorageOneOffWhiteListEntry();

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

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

@Test
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 Down

0 comments on commit ef766e1

Please sign in to comment.