Skip to content

Commit

Permalink
feat: changed the tracker for a more generic class
Browse files Browse the repository at this point in the history
  • Loading branch information
julianlen committed Jan 14, 2025
1 parent 511db02 commit 1599b5c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
public class FederationStorageProviderImpl implements FederationStorageProvider {
private final StorageAccessor bridgeStorageAccessor;
private final HashMap<DataWord, Optional<Integer>> storageVersionEntries;
private final FederationTracker<Federation> oldFederationTracker = new FederationTracker<>();
private final FederationTracker<PendingFederation> pendingFederationTracker = new FederationTracker<>();
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;
Expand Down Expand Up @@ -139,7 +139,7 @@ public void setNewFederation(Federation federation) {

@Override
public Federation getOldFederation(FederationConstants federationConstants, ActivationConfig.ForBlock activations) {
if (oldFederationTracker.hasBeenSet()) {
if (oldFederationTracker.isPresent()) {
return oldFederationTracker.get();
}

Expand All @@ -159,18 +159,18 @@ public Federation getOldFederation(FederationConstants federationConstants, Acti
}
);

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

@Override
public void setOldFederation(Federation federation) {
oldFederationTracker.set(federation, true);
oldFederationTracker.setNew(federation);
}

@Override
public PendingFederation getPendingFederation() {
if (pendingFederationTracker.hasBeenSet()) {
if (pendingFederationTracker.isPresent()) {
return pendingFederationTracker.get();
}

Expand All @@ -190,13 +190,13 @@ public PendingFederation getPendingFederation() {
}
);

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

@Override
public void setPendingFederation(PendingFederation federation) {
pendingFederationTracker.set(federation, true);
pendingFederationTracker.setNew(federation);
}

@Override
Expand Down Expand Up @@ -338,7 +338,7 @@ private void saveOldFederation(ActivationConfig.ForBlock activations) {
}

private int getOldFederationFormatVersion() {
if (!oldFederationTracker.isPresent()) {
if (oldFederationTracker.isNull()) {
// assume it is a standard federation to keep backwards compatibility
return STANDARD_MULTISIG_FEDERATION.getFormatVersion();
}
Expand All @@ -363,7 +363,7 @@ private void savePendingFederation(ActivationConfig.ForBlock activations) {

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

Expand Down

This file was deleted.

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;
}
}

0 comments on commit 1599b5c

Please sign in to comment.