Skip to content

Commit

Permalink
Use clear methods instead of setting null values
Browse files Browse the repository at this point in the history
  • Loading branch information
julia-zack committed Dec 6, 2024
1 parent 71a1c36 commit ce50371
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 23 deletions.
26 changes: 21 additions & 5 deletions rskj-core/src/main/java/co/rsk/peg/BridgeStorageProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,10 @@ public void setSvpFundTxHashUnsigned(Sha256Hash hash) {
this.isSvpFundTxHashUnsignedSet = true;
}

public void clearSvpFundTxHashUnsigned() {
setSvpFundTxHashUnsigned(null);
}

private void saveSvpFundTxHashUnsigned() {
if (!activations.isActive(RSKIP419) || !isSvpFundTxHashUnsignedSet) {
return;
Expand All @@ -630,6 +634,10 @@ public void setSvpFundTxSigned(BtcTransaction svpFundTxSigned) {
this.isSvpFundTxSignedSet = true;
}

public void clearSvpFundTxSigned() {
setSvpFundTxSigned(null);
}

private void saveSvpFundTxSigned() {
if (!activations.isActive(RSKIP419) || !isSvpFundTxSignedSet) {
return;
Expand All @@ -646,6 +654,10 @@ public void setSvpSpendTxHashUnsigned(Sha256Hash hash) {
this.isSvpSpendTxHashUnsignedSet = true;
}

public void clearSvpSpendTxHashUnsigned() {
setSvpSpendTxHashUnsigned(null);
}

private void saveSvpSpendTxHashUnsigned() {
if (!activations.isActive(RSKIP419) || !isSvpSpendTxHashUnsignedSet) {
return;
Expand All @@ -670,6 +682,10 @@ public void setSvpSpendTxWaitingForSignatures(Map.Entry<Keccak256, BtcTransactio
this.isSvpSpendTxWaitingForSignaturesSet = true;
}

public void clearSvpSpendTxWaitingForSignatures() {
setSvpSpendTxWaitingForSignatures(null);
}

private void saveSvpSpendTxWaitingForSignatures() {
if (!activations.isActive(RSKIP419) || !isSvpSpendTxWaitingForSignaturesSet) {
return;
Expand All @@ -685,12 +701,12 @@ private void saveSvpSpendTxWaitingForSignatures() {
public void clearSvpValues() {
if (svpFundTxHashUnsigned != null) {
logger.warn("[clearSvpValues] Clearing fund tx hash unsigned {} value.", svpFundTxHashUnsigned);
setSvpFundTxHashUnsigned(null);
clearSvpFundTxHashUnsigned();
}

if (svpFundTxSigned != null) {
logger.warn("[clearSvpValues] Clearing fund tx signed {} value.", svpFundTxSigned.getHash());
setSvpFundTxSigned(null);
clearSvpFundTxSigned();
}

if (svpSpendTxWaitingForSignatures != null) {
Expand All @@ -700,13 +716,13 @@ public void clearSvpValues() {
"[clearSvpValues] Clearing spend tx waiting for signatures with spend tx {} and rsk creation hash {} value.",
svpSpendTx.getHash(), rskCreationHash
);
setSvpSpendTxWaitingForSignatures(null);
setSvpSpendTxHashUnsigned(null);
clearSvpSpendTxWaitingForSignatures();
clearSvpSpendTxHashUnsigned();
}

if (svpSpendTxHashUnsigned != null) {
logger.warn("[clearSvpValues] Clearing spend tx hash unsigned {} value.", svpSpendTxHashUnsigned);
setSvpSpendTxHashUnsigned(null);
clearSvpSpendTxHashUnsigned();
}
}

Expand Down
6 changes: 3 additions & 3 deletions rskj-core/src/main/java/co/rsk/peg/BridgeSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ private void registerSvpFundTx(BtcTransaction btcTx) throws IOException {

private void registerSvpSpendTx(BtcTransaction btcTx) throws IOException {
registerNewUtxos(btcTx);
provider.setSvpSpendTxHashUnsigned(null);
provider.clearSvpSpendTxHashUnsigned();

logger.info("[registerSvpSpendTx] Going to commit the proposed federation.");
federationSupport.commitProposedFederation();
Expand All @@ -458,7 +458,7 @@ private void updateSvpFundTransactionValues(BtcTransaction transaction) {
);

provider.setSvpFundTxSigned(transaction);
provider.setSvpFundTxHashUnsigned(null);
provider.clearSvpFundTxHashUnsigned();
}

@VisibleForTesting
Expand Down Expand Up @@ -1775,7 +1775,7 @@ private void addSvpSpendTxSignatures(
}

logReleaseBtc(svpSpendTx, svpSpendTxCreationRskTxHash.getBytes());
provider.setSvpSpendTxWaitingForSignatures(null);
provider.clearSvpSpendTxWaitingForSignatures();
}

private boolean areSignaturesEnoughToSignAllTxInputs(BtcTransaction releaseTx, List<byte[]> signatures) {
Expand Down
30 changes: 15 additions & 15 deletions rskj-core/src/test/java/co/rsk/peg/BridgeStorageProviderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ void saveSvpFundTxHashUnsigned_postLovell700AndResettingToNull_shouldSaveNullInS
bridgeStorageProvider.save();

// Act
bridgeStorageProvider.setSvpFundTxHashUnsigned(null);
bridgeStorageProvider.clearSvpFundTxHashUnsigned();
bridgeStorageProvider.save();

// Assert
Expand Down Expand Up @@ -289,7 +289,7 @@ void getSvpFundTxHashUnsigned_whenDifferentHashIsInStorageAndAnotherIsSetButNotS
void getSvpFundTxHashUnsigned_whenStorageIsNotEmptyAndHashSetToNullButNotSaved_shouldReturnEmpty() {
// Arrange
repository.addStorageBytes(bridgeAddress, SVP_FUND_TX_HASH_UNSIGNED.getKey(), BridgeSerializationUtils.serializeSha256Hash(svpFundTxHash));
bridgeStorageProvider.setSvpFundTxHashUnsigned(null);
bridgeStorageProvider.clearSvpFundTxHashUnsigned();

// Act
Optional<Sha256Hash> svpFundTxHashUnsigned = bridgeStorageProvider.getSvpFundTxHashUnsigned();
Expand Down Expand Up @@ -328,7 +328,7 @@ void getSvpFundTxHashUnsigned_whenHashDirectlySavedInStorage_shouldReturnTheHash
@Test
void getSvpFundTxHashUnsigned_whenSetToNull_shouldReturnEmpty() {
// Arrange
bridgeStorageProvider.setSvpFundTxHashUnsigned(null);
bridgeStorageProvider.clearSvpFundTxHashUnsigned();

// Act
Optional<Sha256Hash> svpFundTxHashUnsigned = bridgeStorageProvider.getSvpFundTxHashUnsigned();
Expand All @@ -352,7 +352,7 @@ void getSvpFundTxHashUnsigned_whenHashIsNullInStorage_shouldReturnEmpty() {
@Test
void getSvpFundTxHashUnsigned_whenNullHashIsSetAndSaved_shouldReturnEmpty() {
// Arrange
bridgeStorageProvider.setSvpFundTxHashUnsigned(null);
bridgeStorageProvider.clearSvpFundTxHashUnsigned();
bridgeStorageProvider.save();

// Act
Expand Down Expand Up @@ -438,7 +438,7 @@ void saveSvpFundTxSigned_postLovell700AndResettingToNull_shouldSaveNullInStorage
bridgeStorageProvider.save();

// Act
bridgeStorageProvider.setSvpFundTxSigned(null);
bridgeStorageProvider.clearSvpFundTxSigned();
bridgeStorageProvider.save();

// Assert
Expand Down Expand Up @@ -503,7 +503,7 @@ void getSvpFundTxSigned_whenHashSet_shouldReturnTheHash() {
@Test
void getSvpFundTxSigned_whenHashSetToNull_shouldReturnEmpty() {
// Arrange
bridgeStorageProvider.setSvpFundTxSigned(null);
bridgeStorageProvider.clearSvpFundTxSigned();

// Act
Optional<BtcTransaction> svpFundTxSigned = bridgeStorageProvider.getSvpFundTxSigned();
Expand Down Expand Up @@ -538,7 +538,7 @@ void getSvpFundTxSigned_whenHashSavedAndHashSetToNull_shouldReturnEmpty() {
SVP_FUND_TX_SIGNED.getKey(),
BridgeSerializationUtils.serializeBtcTransaction(svpFundTx)
);
bridgeStorageProvider.setSvpFundTxSigned(null);
bridgeStorageProvider.clearSvpFundTxSigned();

// Act
Optional<BtcTransaction> svpFundTxSigned = bridgeStorageProvider.getSvpFundTxSigned();
Expand Down Expand Up @@ -686,7 +686,7 @@ void saveSvpSpendTxHashUnsigned_postLovell700AndResettingToNull_shouldSaveNullIn
bridgeStorageProvider.save();

// Act
bridgeStorageProvider.setSvpSpendTxHashUnsigned(null);
bridgeStorageProvider.clearSvpSpendTxHashUnsigned();
bridgeStorageProvider.save();

// Assert
Expand Down Expand Up @@ -748,7 +748,7 @@ void getSvpSpendTxHashUnsigned_whenDifferentHashIsInStorageAndAnotherIsSetButNot
void getSvpFundTxHashUnsigned_whenStorageIsNotEmptyAndHashSetToNullButNotSaved_shouldReturnEmpty() {
// Arrange
repository.addStorageBytes(bridgeAddress, SVP_SPEND_TX_HASH_UNSIGNED.getKey(), BridgeSerializationUtils.serializeSha256Hash(svpSpendTxHash));
bridgeStorageProvider.setSvpSpendTxHashUnsigned(null);
bridgeStorageProvider.clearSvpSpendTxHashUnsigned();

// Act
Optional<Sha256Hash> svpSpendTxHashUnsigned = bridgeStorageProvider.getSvpSpendTxHashUnsigned();
Expand Down Expand Up @@ -787,7 +787,7 @@ void getSvpSpendTxHashUnsigned_whenHashDirectlySavedInStorage_shouldReturnTheHas
@Test
void getSvpSpendTxHashUnsigned_whenSetToNull_shouldReturnEmpty() {
// Arrange
bridgeStorageProvider.setSvpSpendTxHashUnsigned(null);
bridgeStorageProvider.clearSvpSpendTxHashUnsigned();

// Act
Optional<Sha256Hash> svpSpendTxHashUnsigned = bridgeStorageProvider.getSvpSpendTxHashUnsigned();
Expand All @@ -811,7 +811,7 @@ void getSvpSpendTxHashUnsigned_whenHashIsNullInStorage_shouldReturnEmpty() {
@Test
void getSvpSpendTxHashUnsigned_whenNullHashIsSetAndSaved_shouldReturnEmpty() {
// Arrange
bridgeStorageProvider.setSvpSpendTxHashUnsigned(null);
bridgeStorageProvider.clearSvpSpendTxHashUnsigned();
bridgeStorageProvider.save();

// Act
Expand Down Expand Up @@ -966,7 +966,7 @@ void saveSvpSpendTxWaitingForSignatures_postLovell700AndNullSvpSpendTxWaitingFor
bridgeStorageProvider.save();

// Act
bridgeStorageProvider.setSvpSpendTxWaitingForSignatures(null);
bridgeStorageProvider.clearSvpSpendTxWaitingForSignatures();
bridgeStorageProvider.save();

// Assert
Expand Down Expand Up @@ -1047,7 +1047,7 @@ void getSvpSpendTxWaitingForSignatures_whenDifferentEntryIsInStorageAndEntrySetT
bridgeAddress,
SVP_SPEND_TX_WAITING_FOR_SIGNATURES.getKey(),
BridgeSerializationUtils.serializeRskTxWaitingForSignatures(svpSpendTxWaitingForSignatures));
bridgeStorageProvider.setSvpSpendTxWaitingForSignatures(null);
bridgeStorageProvider.clearSvpSpendTxWaitingForSignatures();

// Act
Optional<Map.Entry<Keccak256, BtcTransaction>> actualSvpSpendTxWaitingForSignatures =
Expand Down Expand Up @@ -1092,7 +1092,7 @@ void getSvpSpendTxWaitingForSignatures_whenEntryDirectlySavedInStorage_shouldRet
@Test
void getSvpSpendTxWaitingForSignatures_whenSetToNull_shouldReturnEmpty() {
// Arrange
bridgeStorageProvider.setSvpSpendTxWaitingForSignatures(null);
bridgeStorageProvider.clearSvpSpendTxWaitingForSignatures();

// Act
Optional<Map.Entry<Keccak256, BtcTransaction>> actualSvpSpendTxWaitingForSignatures =
Expand All @@ -1105,7 +1105,7 @@ void getSvpSpendTxWaitingForSignatures_whenSetToNull_shouldReturnEmpty() {
@Test
void getSvpSpendTxWaitingForSignatures_whenNullEntryIsSetAndSaved_shouldReturnEmpty() {
// Arrange
bridgeStorageProvider.setSvpSpendTxWaitingForSignatures(null);
bridgeStorageProvider.clearSvpSpendTxWaitingForSignatures();
bridgeStorageProvider.save();

// Act
Expand Down

0 comments on commit ce50371

Please sign in to comment.