Skip to content

Commit

Permalink
feat: changed method getLockWhitelistEntryByAddress so it returns an …
Browse files Browse the repository at this point in the history
…optional
  • Loading branch information
julianlen committed Jan 2, 2025
1 parent 87bde23 commit 3fc4c1f
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 23 deletions.
9 changes: 5 additions & 4 deletions rskj-core/src/main/java/co/rsk/peg/Bridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -1058,16 +1058,17 @@ public long getLockWhitelistEntryByAddress(Object[] args) {
return WhitelistResponseCode.INVALID_ADDRESS_FORMAT.getCode();
}

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

if (entry == null) {
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 entry.getClass() == OneOffWhiteListEntry.class ?
((OneOffWhiteListEntry)entry).maxTransferValue().getValue() :
LockWhitelistEntry lockWhitelistEntry = entry.get();
return lockWhitelistEntry.getClass() == OneOffWhiteListEntry.class ?
((OneOffWhiteListEntry) lockWhitelistEntry).maxTransferValue().getValue() :
WhitelistResponseCode.UNLIMITED_MODE.getCode();
}

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 @@ -2125,7 +2125,7 @@ public LockWhitelistEntry getLockWhitelistEntryByIndex(int index) {
return whitelistSupport.getLockWhitelistEntryByIndex(index);
}

public LockWhitelistEntry getLockWhitelistEntryByAddress(String addressBase58) {
public Optional<LockWhitelistEntry> getLockWhitelistEntryByAddress(String addressBase58) {
return whitelistSupport.getLockWhitelistEntryByAddress(addressBase58);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import co.rsk.bitcoinj.core.Address;
import co.rsk.bitcoinj.core.Coin;
import java.math.BigInteger;
import java.util.Optional;

import org.ethereum.core.Transaction;

/**
Expand Down Expand Up @@ -34,7 +36,7 @@ public interface WhitelistSupport {
* @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
*/
LockWhitelistEntry getLockWhitelistEntryByAddress(String addressBase58);
Optional<LockWhitelistEntry> getLockWhitelistEntryByAddress(String addressBase58);

/**
* Adds the given address to the lock whitelist, allowing peg-ins up to certain max value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import co.rsk.peg.whitelist.constants.WhitelistConstants;
import java.math.BigInteger;
import java.util.List;
import java.util.Optional;

import org.ethereum.config.blockchain.upgrades.ActivationConfig;
import org.ethereum.core.SignatureCache;
import org.ethereum.core.Transaction;
Expand Down Expand Up @@ -56,14 +58,14 @@ public LockWhitelistEntry getLockWhitelistEntryByIndex(int index) {
}

@Override
public LockWhitelistEntry getLockWhitelistEntryByAddress(String addressBase58) {
public Optional<LockWhitelistEntry> getLockWhitelistEntryByAddress(String addressBase58) {
try {
Address address = Address.fromBase58(networkParameters, addressBase58);

return storageProvider.getLockWhitelist(activations, networkParameters).get(address);
return Optional.of(storageProvider.getLockWhitelist(activations, networkParameters).get(address));
} catch (AddressFormatException e) {
logger.warn("[getLockWhitelistEntryByAddress] {}", INVALID_ADDRESS_FORMAT_MESSAGE, e);
return null;
return Optional.empty();
}
}

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 @@ -80,6 +80,7 @@
import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.hamcrest.core.Is.is;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.*;
Expand Down Expand Up @@ -210,9 +211,10 @@ void getLockWhitelistEntryByIndex() {
@Test
void getLockWhitelistEntryByAddress() {
LockWhitelistEntry entry = mock(LockWhitelistEntry.class);
when(whitelistSupport.getLockWhitelistEntryByAddress("address")).thenReturn(entry);
when(whitelistSupport.getLockWhitelistEntryByAddress("address")).thenReturn(Optional.of(entry));

assertEquals(entry, bridgeSupport.getLockWhitelistEntryByAddress("address"));
assertTrue(bridgeSupport.getLockWhitelistEntryByAddress("address").isPresent());
assertEquals(entry, bridgeSupport.getLockWhitelistEntryByAddress("address").get());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import java.math.BigInteger;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

import org.apache.commons.lang3.tuple.Pair;
import org.ethereum.config.blockchain.upgrades.ActivationConfig;
import org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest;
Expand Down Expand Up @@ -91,18 +93,19 @@ void getLockWhitelistEntryByIndex_whenIndexIsOutOfBounds_shouldReturnNull() {

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

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

@Test
void getLockWhitelistEntryByAddress_whenLockWhitelistHasEntries_shouldReturnOneOffWhiteListEntry() {
saveInMemoryStorageOneOffWhiteListEntry();

LockWhitelistEntry actualLockWhitelistEntry = whitelistSupport.getLockWhitelistEntryByAddress(btcAddress.toString());
Optional<LockWhitelistEntry> actualLockWhitelistEntry = whitelistSupport.getLockWhitelistEntryByAddress(btcAddress.toString());

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

private void saveInMemoryStorageOneOffWhiteListEntry() {
Expand All @@ -121,8 +124,8 @@ private void saveInMemoryStorageOneOffWhiteListEntry() {

@Test
void getLockWhitelistEntryByAddress_whenAddressIsInvalid_shouldReturnNull() {
LockWhitelistEntry actualLockWhitelistEntry = whitelistSupport.getLockWhitelistEntryByAddress("invalidAddress");
assertNull(actualLockWhitelistEntry);
Optional<LockWhitelistEntry> actualLockWhitelistEntry = whitelistSupport.getLockWhitelistEntryByAddress("invalidAddress");
assertTrue(actualLockWhitelistEntry.isEmpty());
}

@Test
Expand Down Expand Up @@ -283,11 +286,12 @@ void setLockWhitelistDisableBlockDelay_whenDisableBlockDelayPlusBtcBlockchainBes
void verifyLockSenderIsWhitelisted_whenOneOffLockWhitelistAddressIsWhitelisted_shouldReturnTrue() {
Transaction tx = TransactionUtils.getTransactionFromCaller(signatureCache, WhitelistCaller.AUTHORIZED.getRskAddress());
whitelistSupport.addOneOffLockWhitelistAddress(tx, btcAddress.toString(), BigInteger.TEN);
LockWhitelistEntry lockWhitelistEntry = whitelistSupport.getLockWhitelistEntryByAddress(btcAddress.toString());
Optional<LockWhitelistEntry> lockWhitelistEntry = whitelistSupport.getLockWhitelistEntryByAddress(btcAddress.toString());

boolean actualResult = whitelistSupport.verifyLockSenderIsWhitelisted(btcAddress, Coin.SATOSHI, 0);

assertTrue(lockWhitelistEntry.isConsumed());
assertTrue(lockWhitelistEntry.isPresent());
assertTrue(lockWhitelistEntry.get().isConsumed());
assertTrue(actualResult);
}

Expand Down Expand Up @@ -326,7 +330,8 @@ void save_whenOneOffLockWhitelistAddressIsWhitelisted_shouldSaveOneOffLockWhitel
whitelistSupport.save();

int actualSize = whitelistSupport.getLockWhitelistSize();
Address actualBtcAddress = whitelistSupport.getLockWhitelistEntryByAddress(btcAddress.toString()).address();
Optional<LockWhitelistEntry> lockWhitelistEntry = whitelistSupport.getLockWhitelistEntryByAddress(btcAddress.toString());
Address actualBtcAddress = lockWhitelistEntry.get().address();
assertEquals(1, actualSize);
assertEquals(btcAddress, actualBtcAddress);
}
Expand All @@ -339,7 +344,8 @@ void save_whenUnlimitedLockWhitelistAddressIsWhitelisted_shouldSaveUnlimitedLock
whitelistSupport.save();

int actualSize = whitelistSupport.getLockWhitelistSize();
Address actualBtcAddress = whitelistSupport.getLockWhitelistEntryByAddress(btcAddress.toString()).address();
Optional<LockWhitelistEntry> lockWhitelistEntry = whitelistSupport.getLockWhitelistEntryByAddress(btcAddress.toString());
Address actualBtcAddress = lockWhitelistEntry.get().address();
assertEquals(1, actualSize);
assertEquals(btcAddress, actualBtcAddress);
}
Expand All @@ -353,8 +359,10 @@ void save_whenOneOffAndUnlimitedLockWhitelistAddressesAreWhitelisted_shouldSaveB
whitelistSupport.save();

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

0 comments on commit 3fc4c1f

Please sign in to comment.