Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replacing shouldSave federation by a tracker class #2911

Open
wants to merge 4 commits into
base: bridge-refactors-integration
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,14 @@
public class FederationStorageProviderImpl implements FederationStorageProvider {
private final StorageAccessor bridgeStorageAccessor;
private final HashMap<DataWord, Optional<Integer>> storageVersionEntries;

private final ValueTracker<Federation> oldFederationTracker = new ValueTracker<>();
private final ValueTracker<PendingFederation> pendingFederationTracker = new ValueTracker<>();
private List<UTXO> newFederationBtcUTXOs;
private List<UTXO> oldFederationBtcUTXOs;
private Federation newFederation;
private Federation oldFederation;
private boolean shouldSaveOldFederation = false;

private PendingFederation pendingFederation;
private boolean shouldSavePendingFederation = false;

private ABICallElection federationElection;

private Long activeFederationCreationBlockHeight;
private Long nextFederationCreationBlockHeight; // if -1, then clear value

private Script lastRetiredFederationP2SHScript;

public FederationStorageProviderImpl(StorageAccessor bridgeStorageAccessor) {
Expand Down Expand Up @@ -146,13 +139,13 @@ public void setNewFederation(Federation federation) {

@Override
public Federation getOldFederation(FederationConstants federationConstants, ActivationConfig.ForBlock activations) {
if (oldFederation != null || shouldSaveOldFederation) {
return oldFederation;
if (oldFederationTracker.isPresent()) {
return oldFederationTracker.get();
}

Optional<Integer> storageVersion = getStorageVersion(OLD_FEDERATION_FORMAT_VERSION.getKey());

oldFederation = bridgeStorageAccessor.getFromRepository(
Federation oldFederation = bridgeStorageAccessor.getFromRepository(
OLD_FEDERATION_KEY.getKey(),
data -> {
if (data == null) {
Expand All @@ -166,24 +159,24 @@ public Federation getOldFederation(FederationConstants federationConstants, Acti
}
);

return oldFederation;
oldFederationTracker.set(oldFederation);
return oldFederationTracker.get();
}

@Override
public void setOldFederation(Federation federation) {
shouldSaveOldFederation = true;
oldFederation = federation;
oldFederationTracker.setNew(federation);
}

@Override
public PendingFederation getPendingFederation() {
if (pendingFederation != null || shouldSavePendingFederation) {
return pendingFederation;
if (pendingFederationTracker.isPresent()) {
return pendingFederationTracker.get();
}

Optional<Integer> storageVersion = getStorageVersion(PENDING_FEDERATION_FORMAT_VERSION.getKey());

pendingFederation =
PendingFederation pendingFederation =
bridgeStorageAccessor.getFromRepository(PENDING_FEDERATION_KEY.getKey(),
data -> {
if (data == null) {
Expand All @@ -197,13 +190,13 @@ public PendingFederation getPendingFederation() {
}
);

return pendingFederation;
pendingFederationTracker.set(pendingFederation);
return pendingFederationTracker.get();
}

@Override
public void setPendingFederation(PendingFederation federation) {
shouldSavePendingFederation = true;
pendingFederation = federation;
pendingFederationTracker.setNew(federation);
}

@Override
Expand Down Expand Up @@ -329,10 +322,11 @@ private void saveNewFederation(ActivationConfig.ForBlock activations) {
}

private void saveOldFederation(ActivationConfig.ForBlock activations) {
if (!shouldSaveOldFederation) {
if (!oldFederationTracker.isModified()) {
return;
}

Federation oldFederation = oldFederationTracker.get();
if (!activations.isActive(RSKIP123)) {
bridgeStorageAccessor.saveToRepository(OLD_FEDERATION_KEY.getKey(), oldFederation, BridgeSerializationUtils::serializeFederationOnlyBtcKeys);
return;
Expand All @@ -344,16 +338,16 @@ private void saveOldFederation(ActivationConfig.ForBlock activations) {
}

private int getOldFederationFormatVersion() {
if (oldFederation == null) {
if (oldFederationTracker.isNull()) {
// assume it is a standard federation to keep backwards compatibility
return STANDARD_MULTISIG_FEDERATION.getFormatVersion();
}

return oldFederation.getFormatVersion();
return oldFederationTracker.get().getFormatVersion();
}

private void savePendingFederation(ActivationConfig.ForBlock activations) {
if (!shouldSavePendingFederation) {
if (!pendingFederationTracker.isModified()) {
return;
}

Expand All @@ -369,11 +363,11 @@ private void savePendingFederation(ActivationConfig.ForBlock activations) {

@Nullable
private byte[] serializePendingFederation(ActivationConfig.ForBlock activations) {
if (pendingFederation == null) {
if (pendingFederationTracker.isNull()) {
return null;
}

return pendingFederation.serialize(activations);
return pendingFederationTracker.get().serialize(activations);
}

private void saveFederationFormatVersion(DataWord versionKey, Integer version) {
Expand Down
31 changes: 31 additions & 0 deletions rskj-core/src/main/java/co/rsk/peg/federation/ValueTracker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package co.rsk.peg.federation;

public class ValueTracker<T> {
private T value;
private boolean modified = false;

public boolean isModified() {
return this.modified;
}

public T get() {
return this.value;
}

public boolean isPresent() {
return !isNull() || this.isModified();
}

public boolean isNull() {
return this.value == null;
}

public void setNew(T aValue) {
this.value = aValue;
this.modified = true;
}

public void set(T aValue) {
this.value = aValue;
}
}
Loading