From d4e78c624ce0cc06b46c8a130e429b3cafe20633 Mon Sep 17 00:00:00 2001 From: Julian Len Date: Thu, 9 Jan 2025 16:23:17 -0300 Subject: [PATCH] feat: changed method getLockWhitelistEntryByIndex so it returns an optional --- .../src/main/java/co/rsk/peg/Bridge.java | 32 +++++++++--------- .../main/java/co/rsk/peg/BridgeSupport.java | 2 +- .../rsk/peg/whitelist/WhitelistSupport.java | 10 +++--- .../peg/whitelist/WhitelistSupportImpl.java | 6 ++-- .../java/co/rsk/peg/BridgeSupportTest.java | 11 ++++--- .../co/rsk/peg/BridgeTestIntegration.java | 8 ++--- .../whitelist/WhitelistSupportImplTest.java | 33 +++++++++++-------- 7 files changed, 55 insertions(+), 47 deletions(-) diff --git a/rskj-core/src/main/java/co/rsk/peg/Bridge.java b/rskj-core/src/main/java/co/rsk/peg/Bridge.java index 852615eefb4..dc5011df66b 100644 --- a/rskj-core/src/main/java/co/rsk/peg/Bridge.java +++ b/rskj-core/src/main/java/co/rsk/peg/Bridge.java @@ -1037,14 +1037,11 @@ public String getLockWhitelistAddress(Object[] args) { logger.trace("getLockWhitelistAddress"); int index = ((BigInteger) args[0]).intValue(); - LockWhitelistEntry entry = bridgeSupport.getLockWhitelistEntryByIndex(index); + Optional 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) { @@ -1058,18 +1055,19 @@ public long getLockWhitelistEntryByAddress(Object[] args) { return WhitelistResponseCode.INVALID_ADDRESS_FORMAT.getCode(); } - Optional 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) { diff --git a/rskj-core/src/main/java/co/rsk/peg/BridgeSupport.java b/rskj-core/src/main/java/co/rsk/peg/BridgeSupport.java index 33b16264ec8..62b75831b6a 100644 --- a/rskj-core/src/main/java/co/rsk/peg/BridgeSupport.java +++ b/rskj-core/src/main/java/co/rsk/peg/BridgeSupport.java @@ -2121,7 +2121,7 @@ public Integer getLockWhitelistSize() { return whitelistSupport.getLockWhitelistSize(); } - public LockWhitelistEntry getLockWhitelistEntryByIndex(int index) { + public Optional getLockWhitelistEntryByIndex(int index) { return whitelistSupport.getLockWhitelistEntryByIndex(index); } diff --git a/rskj-core/src/main/java/co/rsk/peg/whitelist/WhitelistSupport.java b/rskj-core/src/main/java/co/rsk/peg/whitelist/WhitelistSupport.java index 762d1a7cc1b..841b06f1993 100644 --- a/rskj-core/src/main/java/co/rsk/peg/whitelist/WhitelistSupport.java +++ b/rskj-core/src/main/java/co/rsk/peg/whitelist/WhitelistSupport.java @@ -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 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 getLockWhitelistEntryByAddress(String addressBase58); diff --git a/rskj-core/src/main/java/co/rsk/peg/whitelist/WhitelistSupportImpl.java b/rskj-core/src/main/java/co/rsk/peg/whitelist/WhitelistSupportImpl.java index bdae7d85b51..7c5864ec995 100644 --- a/rskj-core/src/main/java/co/rsk/peg/whitelist/WhitelistSupportImpl.java +++ b/rskj-core/src/main/java/co/rsk/peg/whitelist/WhitelistSupportImpl.java @@ -45,16 +45,16 @@ public int getLockWhitelistSize() { } @Override - public LockWhitelistEntry getLockWhitelistEntryByIndex(int index) { + public Optional getLockWhitelistEntryByIndex(int index) { List 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 diff --git a/rskj-core/src/test/java/co/rsk/peg/BridgeSupportTest.java b/rskj-core/src/test/java/co/rsk/peg/BridgeSupportTest.java index a2767203056..66d9e9ae8d1 100644 --- a/rskj-core/src/test/java/co/rsk/peg/BridgeSupportTest.java +++ b/rskj-core/src/test/java/co/rsk/peg/BridgeSupportTest.java @@ -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 lockWhitelistEntryByIndex = bridgeSupport.getLockWhitelistEntryByIndex(0); + assertTrue(lockWhitelistEntryByIndex.isPresent()); + assertEquals(entry, lockWhitelistEntryByIndex.get()); } @Test @@ -213,8 +215,9 @@ 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 = bridgeSupport.getLockWhitelistEntryByAddress("address"); + assertTrue(lockWhitelistEntry.isPresent()); + assertEquals(entry, lockWhitelistEntry.get()); } @Test diff --git a/rskj-core/src/test/java/co/rsk/peg/BridgeTestIntegration.java b/rskj-core/src/test/java/co/rsk/peg/BridgeTestIntegration.java index d17a537e799..911be80d383 100644 --- a/rskj-core/src/test/java/co/rsk/peg/BridgeTestIntegration.java +++ b/rskj-core/src/test/java/co/rsk/peg/BridgeTestIntegration.java @@ -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)})); @@ -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); diff --git a/rskj-core/src/test/java/co/rsk/peg/whitelist/WhitelistSupportImplTest.java b/rskj-core/src/test/java/co/rsk/peg/whitelist/WhitelistSupportImplTest.java index bd1259f26c5..dfeb547f9a6 100644 --- a/rskj-core/src/test/java/co/rsk/peg/whitelist/WhitelistSupportImplTest.java +++ b/rskj-core/src/test/java/co/rsk/peg/whitelist/WhitelistSupportImplTest.java @@ -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; @@ -67,32 +66,33 @@ void getLockWhitelistSize_whenLockWhitelistHasEntries_shouldReturnOne() { } @Test - void getLockWhitelistEntryByIndex_whenLockWhitelistIsEmpty_shouldReturnNull() { - LockWhitelistEntry actualEntry = whitelistSupport.getLockWhitelistEntryByIndex(0); + void getLockWhitelistEntryByIndex_whenLockWhitelistIsEmpty_shouldReturnAnEmptyOptional() { + Optional actualEntry = whitelistSupport.getLockWhitelistEntryByIndex(0); - assertNull(actualEntry); + assertTrue(actualEntry.isEmpty()); } @Test void getLockWhitelistEntryByIndex_whenLockWhitelistHasEntries_shouldReturnOneOffWhiteListEntry() { saveInMemoryStorageOneOffWhiteListEntry(); - LockWhitelistEntry actualLockWhitelistEntry = whitelistSupport.getLockWhitelistEntryByIndex(0); + Optional 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 actualLockWhitelistEntry = whitelistSupport.getLockWhitelistEntryByIndex(1); - assertNull(actualLockWhitelistEntry); + assertTrue(actualLockWhitelistEntry.isEmpty()); } @Test - void getLockWhitelistEntryByAddress_whenLockWhitelistIsEmpty_shouldReturnNull() { + void getLockWhitelistEntryByAddress_whenLockWhitelistIsEmpty_shouldReturnAnEmptyOptional() { Optional actualEntry = whitelistSupport.getLockWhitelistEntryByAddress(btcAddress.toString()); assertTrue(actualEntry.isEmpty()); @@ -123,7 +123,7 @@ private void saveInMemoryStorageOneOffWhiteListEntry() { } @Test - void getLockWhitelistEntryByAddress_whenAddressIsInvalid_shouldReturnNull() { + void getLockWhitelistEntryByAddress_whenAddressIsInvalid_shouldReturnAnEmptyOptional() { Optional actualLockWhitelistEntry = whitelistSupport.getLockWhitelistEntryByAddress("invalidAddress"); assertTrue(actualLockWhitelistEntry.isEmpty()); } @@ -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 @@ -331,6 +331,8 @@ void save_whenOneOffLockWhitelistAddressIsWhitelisted_shouldSaveOneOffLockWhitel int actualSize = whitelistSupport.getLockWhitelistSize(); Optional lockWhitelistEntry = whitelistSupport.getLockWhitelistEntryByAddress(btcAddress.toString()); + assertTrue(lockWhitelistEntry.isPresent()); + Address actualBtcAddress = lockWhitelistEntry.get().address(); assertEquals(1, actualSize); assertEquals(btcAddress, actualBtcAddress); @@ -345,6 +347,8 @@ void save_whenUnlimitedLockWhitelistAddressIsWhitelisted_shouldSaveUnlimitedLock int actualSize = whitelistSupport.getLockWhitelistSize(); Optional lockWhitelistEntry = whitelistSupport.getLockWhitelistEntryByAddress(btcAddress.toString()); + assertTrue(lockWhitelistEntry.isPresent()); + Address actualBtcAddress = lockWhitelistEntry.get().address(); assertEquals(1, actualSize); assertEquals(btcAddress, actualBtcAddress); @@ -360,8 +364,11 @@ void save_whenOneOffAndUnlimitedLockWhitelistAddressesAreWhitelisted_shouldSaveB int actualSize = whitelistSupport.getLockWhitelistSize(); Optional lockWhitelistEntryBtcAddress = whitelistSupport.getLockWhitelistEntryByAddress(btcAddress.toString()); + assertTrue(lockWhitelistEntryBtcAddress.isPresent()); Address actualBtcAddress = lockWhitelistEntryBtcAddress.get().address(); + Optional lockWhitelistEntrySecondBtcAddress = whitelistSupport.getLockWhitelistEntryByAddress(secondBtcAddress.toString()); + assertTrue(lockWhitelistEntrySecondBtcAddress.isPresent()); Address actualSecondBtcAddress = lockWhitelistEntrySecondBtcAddress.get().address(); assertEquals(2, actualSize); assertEquals(btcAddress, actualBtcAddress);