Skip to content

Commit

Permalink
Merge pull request #2545 from rsksmart/wip/whitelist-refactor-integra…
Browse files Browse the repository at this point in the history
…tion

Whitelist Refactor Integration
  • Loading branch information
josedahlquist authored Aug 6, 2024
2 parents 859b52a + 9b37dc4 commit 925fdf6
Show file tree
Hide file tree
Showing 40 changed files with 2,629 additions and 1,839 deletions.
25 changes: 12 additions & 13 deletions rskj-core/src/main/java/co/rsk/peg/Bridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import co.rsk.peg.utils.BtcTransactionFormatUtils;
import co.rsk.peg.whitelist.LockWhitelistEntry;
import co.rsk.peg.whitelist.OneOffWhiteListEntry;
import co.rsk.peg.whitelist.WhitelistResponseCode;
import co.rsk.rpc.modules.trace.ProgramSubtrace;
import com.google.common.annotations.VisibleForTesting;
import org.ethereum.config.Constants;
Expand Down Expand Up @@ -70,7 +71,7 @@
*/
public class Bridge extends PrecompiledContracts.PrecompiledContract {

private static final Logger logger = LoggerFactory.getLogger("bridge");
private static final Logger logger = LoggerFactory.getLogger(Bridge.class);
private static final PanicProcessor panicProcessor = new PanicProcessor();

// No parameters
Expand Down Expand Up @@ -207,10 +208,6 @@ public class Bridge extends PrecompiledContracts.PrecompiledContract {

public static final CallTransaction.Function GET_ACTIVE_POWPEG_REDEEM_SCRIPT = BridgeMethods.GET_ACTIVE_POWPEG_REDEEM_SCRIPT.getFunction();

public static final int LOCK_WHITELIST_UNLIMITED_MODE_CODE = 0;
public static final int LOCK_WHITELIST_ENTRY_NOT_FOUND_CODE = -1;
public static final int LOCK_WHITELIST_INVALID_ADDRESS_FORMAT_ERROR_CODE = -2;

// Log topics used by Bridge Contract pre RSKIP146
public static final DataWord RELEASE_BTC_TOPIC = DataWord.fromString("release_btc_topic");
public static final DataWord UPDATE_COLLECTIONS_TOPIC = DataWord.fromString("update_collections_topic");
Expand Down Expand Up @@ -1056,20 +1053,21 @@ public long getLockWhitelistEntryByAddress(Object[] args) {
try {
addressBase58 = (String) args[0];
} catch (Exception e) {
logger.warn("Exception in getLockWhitelistEntryByAddress", e);
return LOCK_WHITELIST_INVALID_ADDRESS_FORMAT_ERROR_CODE;
logger.warn("[getLockWhitelistEntryByAddress] Error while parsing the provided address. {}", e.getMessage());
return WhitelistResponseCode.INVALID_ADDRESS_FORMAT.getCode();
}

LockWhitelistEntry entry = bridgeSupport.getLockWhitelistEntryByAddress(addressBase58);

if (entry == null) {
// Empty string is returned when address is not found
return LOCK_WHITELIST_ENTRY_NOT_FOUND_CODE;
logger.debug("[getLockWhitelistEntryByAddress] Address not found: {}", addressBase58);
return WhitelistResponseCode.ADDRESS_NOT_EXIST.getCode();
}

return entry.getClass() == OneOffWhiteListEntry.class ?
((OneOffWhiteListEntry)entry).maxTransferValue().getValue() :
LOCK_WHITELIST_UNLIMITED_MODE_CODE;
((OneOffWhiteListEntry)entry).maxTransferValue().getValue() :
WhitelistResponseCode.UNLIMITED_MODE.getCode();
}

public Integer addOneOffLockWhitelistAddress(Object[] args) {
Expand All @@ -1081,7 +1079,7 @@ public Integer addOneOffLockWhitelistAddress(Object[] args) {
addressBase58 = (String) args[0];
maxTransferValue = (BigInteger) args[1];
} catch (Exception e) {
logger.warn("Exception in addOneOffLockWhitelistAddress", e);
logger.warn("[addOneOffLockWhitelistAddress] Error while parsing the provided address and max value. {}", e.getMessage());
return 0;
}

Expand All @@ -1095,7 +1093,7 @@ public Integer addUnlimitedLockWhitelistAddress(Object[] args) {
try {
addressBase58 = (String) args[0];
} catch (Exception e) {
logger.warn("Exception in addUnlimitedLockWhitelistAddress", e);
logger.warn("[addUnlimitedLockWhitelistAddress] Exception in addUnlimitedLockWhitelistAddress", e);
return 0;
}

Expand All @@ -1109,7 +1107,7 @@ public Integer removeLockWhitelistAddress(Object[] args) {
try {
addressBase58 = (String) args[0];
} catch (Exception e) {
logger.warn("Exception in removeLockWhitelistAddress", e);
logger.warn("[removeLockWhitelistAddress] Error while parsing the provided address. {}", e.getMessage());
return 0;
}

Expand All @@ -1119,6 +1117,7 @@ public Integer removeLockWhitelistAddress(Object[] args) {
public Integer setLockWhitelistDisableBlockDelay(Object[] args) throws IOException, BlockStoreException {
logger.trace("setLockWhitelistDisableBlockDelay");
BigInteger lockWhitelistDisableBlockDelay = (BigInteger) args[0];

return bridgeSupport.setLockWhitelistDisableBlockDelay(rskTx, lockWhitelistDisableBlockDelay);
}

Expand Down
2 changes: 0 additions & 2 deletions rskj-core/src/main/java/co/rsk/peg/BridgeStorageIndexKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ public enum BridgeStorageIndexKey {
OLD_FEDERATION_KEY("oldFederation"),
PENDING_FEDERATION_KEY("pendingFederation"),
FEDERATION_ELECTION_KEY("federationElection"),
LOCK_ONE_OFF_WHITELIST_KEY("lockWhitelist"),
LOCK_UNLIMITED_WHITELIST_KEY("unlimitedLockWhitelist"),
LOCKING_CAP_KEY("lockingCap"),
RELEASE_REQUEST_QUEUE_WITH_TXHASH("releaseRequestQueueWithTxHash"),
PEGOUTS_WAITING_FOR_CONFIRMATIONS_WITH_TXHASH_KEY("releaseTransactionSetWithTxHash"),
Expand Down
55 changes: 1 addition & 54 deletions rskj-core/src/main/java/co/rsk/peg/BridgeStorageProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@
import co.rsk.peg.federation.PendingFederation;
import co.rsk.peg.flyover.FlyoverFederationInformation;
import co.rsk.peg.vote.AddressBasedAuthorizer;
import co.rsk.peg.whitelist.LockWhitelist;
import co.rsk.peg.whitelist.LockWhitelistEntry;
import co.rsk.peg.whitelist.OneOffWhiteListEntry;
import co.rsk.peg.whitelist.UnlimitedWhiteListEntry;
import org.apache.commons.lang3.tuple.Pair;
import org.ethereum.config.blockchain.upgrades.ActivationConfig;
import org.ethereum.core.Repository;
import org.ethereum.vm.DataWord;
Expand Down Expand Up @@ -86,8 +81,6 @@ public class BridgeStorageProvider {

private ABICallElection federationElection;

private LockWhitelist lockWhitelist;

private Coin lockingCap;

private HashMap<DataWord, Optional<Integer>> storageVersionEntries;
Expand Down Expand Up @@ -500,50 +493,6 @@ public ABICallElection getFederationElection(AddressBasedAuthorizer authorizer)
return federationElection;
}

/**
* Save the lock whitelist
*/
public void saveLockWhitelist() {
if (lockWhitelist == null) {
return;
}

List<OneOffWhiteListEntry> oneOffEntries = lockWhitelist.getAll(OneOffWhiteListEntry.class);
safeSaveToRepository(LOCK_ONE_OFF_WHITELIST_KEY, Pair.of(oneOffEntries, lockWhitelist.getDisableBlockHeight()), BridgeSerializationUtils::serializeOneOffLockWhitelist);

if (activations.isActive(RSKIP87)) {
List<UnlimitedWhiteListEntry> unlimitedEntries = lockWhitelist.getAll(UnlimitedWhiteListEntry.class);
safeSaveToRepository(LOCK_UNLIMITED_WHITELIST_KEY, unlimitedEntries, BridgeSerializationUtils::serializeUnlimitedLockWhitelist);
}
}

public LockWhitelist getLockWhitelist() {
if (lockWhitelist != null) {
return lockWhitelist;
}

Pair<HashMap<Address, OneOffWhiteListEntry>, Integer> oneOffWhitelistAndDisableBlockHeightData =
safeGetFromRepository(LOCK_ONE_OFF_WHITELIST_KEY,
data -> BridgeSerializationUtils.deserializeOneOffLockWhitelistAndDisableBlockHeight(data, networkParameters));
if (oneOffWhitelistAndDisableBlockHeightData == null) {
lockWhitelist = new LockWhitelist(new HashMap<>());
return lockWhitelist;
}

Map<Address, LockWhitelistEntry> whitelistedAddresses = new HashMap<>();

whitelistedAddresses.putAll(oneOffWhitelistAndDisableBlockHeightData.getLeft());

if (activations.isActive(RSKIP87)) {
whitelistedAddresses.putAll(safeGetFromRepository(LOCK_UNLIMITED_WHITELIST_KEY,
data -> BridgeSerializationUtils.deserializeUnlimitedLockWhitelistEntries(data, networkParameters)));
}

lockWhitelist = new LockWhitelist(whitelistedAddresses, oneOffWhitelistAndDisableBlockHeightData.getRight());

return lockWhitelist;
}

public void saveLockingCap() {
if (activations.isActive(RSKIP134)) {
safeSaveToRepository(LOCKING_CAP_KEY, this.getLockingCap(), BridgeSerializationUtils::serializeCoin);
Expand Down Expand Up @@ -636,7 +585,7 @@ private void saveCoinbaseInformations() {
return;
}

if (coinbaseInformationMap == null || coinbaseInformationMap.size() == 0) {
if (coinbaseInformationMap == null || coinbaseInformationMap.isEmpty()) {
return;
}
coinbaseInformationMap.forEach((Sha256Hash blockHash, CoinbaseInformation data) ->
Expand Down Expand Up @@ -936,8 +885,6 @@ public void save() throws IOException {

saveFederationElection();

saveLockWhitelist();

saveLockingCap();

saveHeightBtcTxHashAlreadyProcessed();
Expand Down
Loading

0 comments on commit 925fdf6

Please sign in to comment.