Skip to content

Commit c1872b1

Browse files
committed
feat: changed method getLockWhitelistEntryByIndex so it returns an optional
1 parent 4220bfb commit c1872b1

File tree

7 files changed

+55
-47
lines changed

7 files changed

+55
-47
lines changed

rskj-core/src/main/java/co/rsk/peg/Bridge.java

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,14 +1037,11 @@ public String getLockWhitelistAddress(Object[] args) {
10371037
logger.trace("getLockWhitelistAddress");
10381038

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

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

1047-
return entry.address().toBase58();
10481045
}
10491046

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

1061-
Optional<LockWhitelistEntry> entry = bridgeSupport.getLockWhitelistEntryByAddress(addressBase58);
1062-
1063-
if (entry.isEmpty()) {
1064-
// Empty string is returned when address is not found
1065-
logger.debug("[getLockWhitelistEntryByAddress] Address not found: {}", addressBase58);
1066-
return WhitelistResponseCode.ADDRESS_NOT_EXIST.getCode();
1067-
}
1058+
return bridgeSupport.getLockWhitelistEntryByAddress(addressBase58)
1059+
.map(lockWhitelistEntry -> {
1060+
if (lockWhitelistEntry.getClass() == OneOffWhiteListEntry.class) {
1061+
return ((OneOffWhiteListEntry) lockWhitelistEntry).maxTransferValue().getValue();
1062+
}
10681063

1069-
LockWhitelistEntry lockWhitelistEntry = entry.get();
1070-
return lockWhitelistEntry.getClass() == OneOffWhiteListEntry.class ?
1071-
((OneOffWhiteListEntry) lockWhitelistEntry).maxTransferValue().getValue() :
1072-
WhitelistResponseCode.UNLIMITED_MODE.getCode();
1064+
return WhitelistResponseCode.UNLIMITED_MODE.getCode();
1065+
})
1066+
.orElseGet(() -> {
1067+
// Empty string is returned when address is not found
1068+
logger.debug("[getLockWhitelistEntryByAddress] Address not found: {}", addressBase58);
1069+
return WhitelistResponseCode.ADDRESS_NOT_EXIST.getCode();
1070+
}).longValue();
10731071
}
10741072

10751073
public Integer addOneOffLockWhitelistAddress(Object[] args) {

rskj-core/src/main/java/co/rsk/peg/BridgeSupport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2121,7 +2121,7 @@ public Integer getLockWhitelistSize() {
21212121
return whitelistSupport.getLockWhitelistSize();
21222122
}
21232123

2124-
public LockWhitelistEntry getLockWhitelistEntryByIndex(int index) {
2124+
public Optional<LockWhitelistEntry> getLockWhitelistEntryByIndex(int index) {
21252125
return whitelistSupport.getLockWhitelistEntryByIndex(index);
21262126
}
21272127

rskj-core/src/main/java/co/rsk/peg/whitelist/WhitelistSupport.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,20 @@ public interface WhitelistSupport {
2121
int getLockWhitelistSize();
2222

2323
/**
24-
* Returns the lock whitelist entry stored at the given index, or null if the index is out of
24+
* Returns the lock whitelist entry stored at the given index, or an empty Optional if the index is out of
2525
* bounds
2626
*
2727
* @param index the index at which to get the entry
28-
* @return the whitelist entry stored at the given index, or null if index is out of
28+
* @return the whitelist entry stored at the given index, or an empty Optional if index is out of
2929
* bounds
3030
*/
31-
LockWhitelistEntry getLockWhitelistEntryByIndex(int index);
31+
Optional<LockWhitelistEntry> getLockWhitelistEntryByIndex(int index);
3232

3333
/**
34-
* Returns the lock whitelist entry for a given address, or null if the address is not whitelisted
34+
* Returns the lock whitelist entry for a given address, or an empty Optional if the address is not whitelisted
3535
*
3636
* @param addressBase58 the address in base58 format to search for
37-
* @return the whitelist entry for the given address, or null if the addrres is not whitelisted
37+
* @return the whitelist entry for the given address, or an empty Optional if the addrres is not whitelisted
3838
*/
3939
Optional<LockWhitelistEntry> getLockWhitelistEntryByAddress(String addressBase58);
4040

rskj-core/src/main/java/co/rsk/peg/whitelist/WhitelistSupportImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,16 @@ public int getLockWhitelistSize() {
4545
}
4646

4747
@Override
48-
public LockWhitelistEntry getLockWhitelistEntryByIndex(int index) {
48+
public Optional<LockWhitelistEntry> getLockWhitelistEntryByIndex(int index) {
4949
List<LockWhitelistEntry> entries = storageProvider.getLockWhitelist(
5050
activations,
5151
networkParameters
5252
).getAll();
5353

5454
if (index < 0 || index >= entries.size()) {
55-
return null;
55+
return Optional.empty();
5656
}
57-
return entries.get(index);
57+
return Optional.ofNullable(entries.get(index));
5858
}
5959

6060
@Override

rskj-core/src/test/java/co/rsk/peg/BridgeSupportTest.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,18 +203,21 @@ void getLockWhitelistSize() {
203203
@Test
204204
void getLockWhitelistEntryByIndex() {
205205
LockWhitelistEntry entry = mock(LockWhitelistEntry.class);
206-
when(whitelistSupport.getLockWhitelistEntryByIndex(0)).thenReturn(entry);
206+
when(whitelistSupport.getLockWhitelistEntryByIndex(0)).thenReturn(Optional.of(entry));
207207

208-
assertEquals(entry, bridgeSupport.getLockWhitelistEntryByIndex(0));
208+
Optional<LockWhitelistEntry> lockWhitelistEntryByIndex = bridgeSupport.getLockWhitelistEntryByIndex(0);
209+
assertTrue(lockWhitelistEntryByIndex.isPresent());
210+
assertEquals(entry, lockWhitelistEntryByIndex.get());
209211
}
210212

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

216-
assertTrue(bridgeSupport.getLockWhitelistEntryByAddress("address").isPresent());
217-
assertEquals(entry, bridgeSupport.getLockWhitelistEntryByAddress("address").get());
218+
Optional<LockWhitelistEntry> lockWhitelistEntry = bridgeSupport.getLockWhitelistEntryByAddress("address");
219+
assertTrue(lockWhitelistEntry.isPresent());
220+
assertEquals(entry, lockWhitelistEntry.get());
218221
}
219222

220223
@Test

rskj-core/src/test/java/co/rsk/peg/BridgeTestIntegration.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2180,8 +2180,8 @@ void getLockWhitelistAddress() {
21802180
OneOffWhiteListEntry mockedEntry10 = new OneOffWhiteListEntry(new BtcECKey().toAddress(networkParameters), Coin.COIN);
21812181
OneOffWhiteListEntry mockedEntry20 = new OneOffWhiteListEntry(new BtcECKey().toAddress(networkParameters), Coin.COIN);
21822182
TestUtils.setInternalState(bridge, "bridgeSupport", bridgeSupportMock);
2183-
when(bridgeSupportMock.getLockWhitelistEntryByIndex(10)).then((InvocationOnMock invocation) -> mockedEntry10);
2184-
when(bridgeSupportMock.getLockWhitelistEntryByIndex(20)).then((InvocationOnMock invocation) -> mockedEntry20);
2183+
when(bridgeSupportMock.getLockWhitelistEntryByIndex(10)).then((InvocationOnMock invocation) -> Optional.of(mockedEntry10));
2184+
when(bridgeSupportMock.getLockWhitelistEntryByIndex(20)).then((InvocationOnMock invocation) -> Optional.of(mockedEntry20));
21852185

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

22282228
BridgeSupport bridgeSupportMock = mock(BridgeSupport.class);
22292229
when(bridgeSupportMock.getLockWhitelistEntryByAddress(mockedAddressForUnlimited.toBase58()))
2230-
.then((InvocationOnMock invocation) -> new UnlimitedWhiteListEntry(mockedAddressForUnlimited));
2230+
.then((InvocationOnMock invocation) -> Optional.of(new UnlimitedWhiteListEntry(mockedAddressForUnlimited)));
22312231
when(bridgeSupportMock.getLockWhitelistEntryByAddress(mockedAddressForOneOff.toBase58()))
2232-
.then((InvocationOnMock invocation) -> new OneOffWhiteListEntry(mockedAddressForOneOff, Coin.COIN));
2232+
.then((InvocationOnMock invocation) -> Optional.of(new OneOffWhiteListEntry(mockedAddressForOneOff, Coin.COIN)));
22332233

22342234
mockedTransaction = mock(Transaction.class);
22352235

rskj-core/src/test/java/co/rsk/peg/whitelist/WhitelistSupportImplTest.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import static co.rsk.peg.whitelist.WhitelistStorageIndexKey.LOCK_ONE_OFF;
44
import static org.junit.jupiter.api.Assertions.assertEquals;
55
import static org.junit.jupiter.api.Assertions.assertFalse;
6-
import static org.junit.jupiter.api.Assertions.assertNull;
76
import static org.junit.jupiter.api.Assertions.assertTrue;
87
import co.rsk.bitcoinj.core.Address;
98
import co.rsk.bitcoinj.core.Coin;
@@ -67,32 +66,33 @@ void getLockWhitelistSize_whenLockWhitelistHasEntries_shouldReturnOne() {
6766
}
6867

6968
@Test
70-
void getLockWhitelistEntryByIndex_whenLockWhitelistIsEmpty_shouldReturnNull() {
71-
LockWhitelistEntry actualEntry = whitelistSupport.getLockWhitelistEntryByIndex(0);
69+
void getLockWhitelistEntryByIndex_whenLockWhitelistIsEmpty_shouldReturnAnEmptyOptional() {
70+
Optional<LockWhitelistEntry> actualEntry = whitelistSupport.getLockWhitelistEntryByIndex(0);
7271

73-
assertNull(actualEntry);
72+
assertTrue(actualEntry.isEmpty());
7473
}
7574

7675
@Test
7776
void getLockWhitelistEntryByIndex_whenLockWhitelistHasEntries_shouldReturnOneOffWhiteListEntry() {
7877
saveInMemoryStorageOneOffWhiteListEntry();
7978

80-
LockWhitelistEntry actualLockWhitelistEntry = whitelistSupport.getLockWhitelistEntryByIndex(0);
79+
Optional<LockWhitelistEntry> actualLockWhitelistEntry = whitelistSupport.getLockWhitelistEntryByIndex(0);
8180

82-
assertEquals(btcAddress, actualLockWhitelistEntry.address());
81+
assertTrue(actualLockWhitelistEntry.isPresent());
82+
assertEquals(btcAddress, actualLockWhitelistEntry.get().address());
8383
}
8484

8585
@Test
86-
void getLockWhitelistEntryByIndex_whenIndexIsOutOfBounds_shouldReturnNull() {
86+
void getLockWhitelistEntryByIndex_whenIndexIsOutOfBounds_shouldReturnAnEmptyOptional() {
8787
saveInMemoryStorageOneOffWhiteListEntry();
8888

89-
LockWhitelistEntry actualLockWhitelistEntry = whitelistSupport.getLockWhitelistEntryByIndex(1);
89+
Optional<LockWhitelistEntry> actualLockWhitelistEntry = whitelistSupport.getLockWhitelistEntryByIndex(1);
9090

91-
assertNull(actualLockWhitelistEntry);
91+
assertTrue(actualLockWhitelistEntry.isEmpty());
9292
}
9393

9494
@Test
95-
void getLockWhitelistEntryByAddress_whenLockWhitelistIsEmpty_shouldReturnNull() {
95+
void getLockWhitelistEntryByAddress_whenLockWhitelistIsEmpty_shouldReturnAnEmptyOptional() {
9696
Optional<LockWhitelistEntry> actualEntry = whitelistSupport.getLockWhitelistEntryByAddress(btcAddress.toString());
9797

9898
assertTrue(actualEntry.isEmpty());
@@ -123,7 +123,7 @@ private void saveInMemoryStorageOneOffWhiteListEntry() {
123123
}
124124

125125
@Test
126-
void getLockWhitelistEntryByAddress_whenAddressIsInvalid_shouldReturnNull() {
126+
void getLockWhitelistEntryByAddress_whenAddressIsInvalid_shouldReturnAnEmptyOptional() {
127127
Optional<LockWhitelistEntry> actualLockWhitelistEntry = whitelistSupport.getLockWhitelistEntryByAddress("invalidAddress");
128128
assertTrue(actualLockWhitelistEntry.isEmpty());
129129
}
@@ -318,8 +318,8 @@ void save_whenLockWhitelistIsNull_shouldReturnZeroEntries() {
318318

319319
int actualSize = whitelistSupport.getLockWhitelistSize();
320320
assertEquals(0, actualSize);
321-
assertNull(whitelistSupport.getLockWhitelistEntryByIndex(0));
322-
assertNull(whitelistSupport.getLockWhitelistEntryByIndex(1));
321+
assertTrue(whitelistSupport.getLockWhitelistEntryByIndex(0).isEmpty());
322+
assertTrue(whitelistSupport.getLockWhitelistEntryByIndex(1).isEmpty());
323323
}
324324

325325
@Test
@@ -331,6 +331,8 @@ void save_whenOneOffLockWhitelistAddressIsWhitelisted_shouldSaveOneOffLockWhitel
331331

332332
int actualSize = whitelistSupport.getLockWhitelistSize();
333333
Optional<LockWhitelistEntry> lockWhitelistEntry = whitelistSupport.getLockWhitelistEntryByAddress(btcAddress.toString());
334+
assertTrue(lockWhitelistEntry.isPresent());
335+
334336
Address actualBtcAddress = lockWhitelistEntry.get().address();
335337
assertEquals(1, actualSize);
336338
assertEquals(btcAddress, actualBtcAddress);
@@ -345,6 +347,8 @@ void save_whenUnlimitedLockWhitelistAddressIsWhitelisted_shouldSaveUnlimitedLock
345347

346348
int actualSize = whitelistSupport.getLockWhitelistSize();
347349
Optional<LockWhitelistEntry> lockWhitelistEntry = whitelistSupport.getLockWhitelistEntryByAddress(btcAddress.toString());
350+
assertTrue(lockWhitelistEntry.isPresent());
351+
348352
Address actualBtcAddress = lockWhitelistEntry.get().address();
349353
assertEquals(1, actualSize);
350354
assertEquals(btcAddress, actualBtcAddress);
@@ -360,8 +364,11 @@ void save_whenOneOffAndUnlimitedLockWhitelistAddressesAreWhitelisted_shouldSaveB
360364

361365
int actualSize = whitelistSupport.getLockWhitelistSize();
362366
Optional<LockWhitelistEntry> lockWhitelistEntryBtcAddress = whitelistSupport.getLockWhitelistEntryByAddress(btcAddress.toString());
367+
assertTrue(lockWhitelistEntryBtcAddress.isPresent());
363368
Address actualBtcAddress = lockWhitelistEntryBtcAddress.get().address();
369+
364370
Optional<LockWhitelistEntry> lockWhitelistEntrySecondBtcAddress = whitelistSupport.getLockWhitelistEntryByAddress(secondBtcAddress.toString());
371+
assertTrue(lockWhitelistEntrySecondBtcAddress.isPresent());
365372
Address actualSecondBtcAddress = lockWhitelistEntrySecondBtcAddress.get().address();
366373
assertEquals(2, actualSize);
367374
assertEquals(btcAddress, actualBtcAddress);

0 commit comments

Comments
 (0)