Skip to content

Commit

Permalink
Merge pull request #2703 from rsksmart/feat/get_svp_spend_tx_hash
Browse files Browse the repository at this point in the history
Create getSpendTransactionHashUnsigned method
  • Loading branch information
marcos-iov authored Aug 30, 2024
2 parents ebd5a13 + 9a45d44 commit 35cb096
Show file tree
Hide file tree
Showing 2 changed files with 171 additions and 2 deletions.
25 changes: 23 additions & 2 deletions rskj-core/src/main/java/co/rsk/peg/BridgeStorageProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,8 @@ public Optional<Sha256Hash> getSvpFundTxHashUnsigned() {
return Optional.empty();
}

svpFundTxHashUnsigned = safeGetFromRepository(SVP_FUND_TX_HASH_UNSIGNED.getKey(), BridgeSerializationUtils::deserializeSha256Hash);
svpFundTxHashUnsigned = safeGetFromRepository(
SVP_FUND_TX_HASH_UNSIGNED.getKey(), BridgeSerializationUtils::deserializeSha256Hash);
return Optional.ofNullable(svpFundTxHashUnsigned);
}

Expand All @@ -558,10 +559,30 @@ public Optional<Sha256Hash> getSvpFundTxHashSigned() {
return Optional.empty();
}

svpFundTxHashSigned = safeGetFromRepository(SVP_FUND_TX_HASH_SIGNED.getKey(), BridgeSerializationUtils::deserializeSha256Hash);
svpFundTxHashSigned = safeGetFromRepository(
SVP_FUND_TX_HASH_SIGNED.getKey(), BridgeSerializationUtils::deserializeSha256Hash);
return Optional.ofNullable(svpFundTxHashSigned);
}

public Optional<Sha256Hash> getSvpSpendTxHashUnsigned() {
if (!activations.isActive(RSKIP419)) {
return Optional.empty();
}

if (svpSpendTxHashUnsigned != null) {
return Optional.of(svpSpendTxHashUnsigned);
}

// Return empty if the svp spend tx hash unsigned was explicitly set to null
if (isSvpSpendTxHashUnsignedSet) {
return Optional.empty();
}

svpSpendTxHashUnsigned = safeGetFromRepository(
SVP_SPEND_TX_HASH_UNSIGNED.getKey(), BridgeSerializationUtils::deserializeSha256Hash);
return Optional.ofNullable(svpSpendTxHashUnsigned);
}

public void setSvpFundTxHashUnsigned(Sha256Hash hash) {
this.svpFundTxHashUnsigned = hash;
this.isSvpFundTxHashUnsignedSet = true;
Expand Down
148 changes: 148 additions & 0 deletions rskj-core/src/test/java/co/rsk/peg/BridgeStorageProviderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,154 @@ void saveSvpSpendTxHashUnsigned_postLovell700AndResettingToNull_shouldSaveNullIn
byte[] actualSvpSpendTxHashSerialized = repository.getStorageBytes(bridgeAddress, SVP_SPEND_TX_HASH_UNSIGNED.getKey());
assertNull(actualSvpSpendTxHashSerialized);
}

@Test
void getSvpSpendTxHashUnsigned_preLovell700_shouldReturnEmpty() {
// Arrange
ActivationConfig.ForBlock arrowheadActivations = ActivationConfigsForTest.arrowhead631().forBlock(0L);
bridgeStorageProvider = createBridgeStorageProvider(repository, mainnetBtcParams, arrowheadActivations);

// Manually setting the value in storage to then assert that pre fork the method doesn't access the storage
repository.addStorageBytes(bridgeAddress, SVP_SPEND_TX_HASH_UNSIGNED.getKey(), BridgeSerializationUtils.serializeSha256Hash(svpSpendTxHash));

// Act
Optional<Sha256Hash> svpSpendTxHashUnsigned = bridgeStorageProvider.getSvpSpendTxHashUnsigned();

// Assert
assertEquals(Optional.empty(), svpSpendTxHashUnsigned);
}

@Test
void getSvpSpendTxHashUnsigned_whenThereIsNoSvpSpendTxHashUnsignedSavedNorSet_shouldReturnEmpty() {
Optional<Sha256Hash> svpSpendTxHashUnsigned = bridgeStorageProvider.getSvpSpendTxHashUnsigned();
assertEquals(Optional.empty(), svpSpendTxHashUnsigned);
}

@Test
void getSvpSpendTxHashUnsigned_whenHashSetButNotSavedToStorage_shouldReturnTheHash() {
// Arrange
bridgeStorageProvider.setSvpSpendTxHashUnsigned(svpSpendTxHash);

// Act
Optional<Sha256Hash> svpSpendTxHashUnsigned = bridgeStorageProvider.getSvpSpendTxHashUnsigned();

// Assert
assertTrue(svpSpendTxHashUnsigned.isPresent());
assertEquals(svpSpendTxHash, svpSpendTxHashUnsigned.get());
}

@Test
void getSvpSpendTxHashUnsigned_whenDifferentHashIsInStorageAndAnotherIsSetButNotSaved_shouldReturnTheSetHash() {
// Arrange
Sha256Hash anotherSvpSpendTxHash = BitcoinTestUtils.createHash(987_654_321);
repository.addStorageBytes(bridgeAddress, SVP_SPEND_TX_HASH_UNSIGNED.getKey(), BridgeSerializationUtils.serializeSha256Hash(anotherSvpSpendTxHash));
bridgeStorageProvider.setSvpSpendTxHashUnsigned(svpSpendTxHash);

// Act
Optional<Sha256Hash> svpSpendTxHashUnsigned = bridgeStorageProvider.getSvpSpendTxHashUnsigned();

// Assert
assertTrue(svpSpendTxHashUnsigned.isPresent());
assertEquals(svpSpendTxHash, svpSpendTxHashUnsigned.get());
}

@Test
void getSvpFundTxHashUnsigned_whenStorageIsNotEmptyAndHashSetToNullButNotSaved_shouldReturnEmpty() {
// Arrange
repository.addStorageBytes(bridgeAddress, SVP_SPEND_TX_HASH_UNSIGNED.getKey(), BridgeSerializationUtils.serializeSha256Hash(svpSpendTxHash));
bridgeStorageProvider.setSvpSpendTxHashUnsigned(null);

// Act
Optional<Sha256Hash> svpSpendTxHashUnsigned = bridgeStorageProvider.getSvpSpendTxHashUnsigned();

// Assert
assertEquals(Optional.empty(), svpSpendTxHashUnsigned);
}

@Test
void getSvpSpendTxHashUnsigned_whenHashSetAndSaved_shouldReturnTheHash() {
// Arrange
bridgeStorageProvider.setSvpSpendTxHashUnsigned(svpSpendTxHash);
bridgeStorageProvider.save();

// Act
Optional<Sha256Hash> svpSpendTxHashUnsigned = bridgeStorageProvider.getSvpSpendTxHashUnsigned();

// Assert
assertTrue(svpSpendTxHashUnsigned.isPresent());
assertEquals(svpSpendTxHash, svpSpendTxHashUnsigned.get());
}

@Test
void getSvpSpendTxHashUnsigned_whenHashDirectlySavedInStorage_shouldReturnTheHash() {
// Arrange
repository.addStorageBytes(bridgeAddress, SVP_SPEND_TX_HASH_UNSIGNED.getKey(), BridgeSerializationUtils.serializeSha256Hash(svpSpendTxHash));

// Act
Optional<Sha256Hash> svpSpendTxHashUnsigned = bridgeStorageProvider.getSvpSpendTxHashUnsigned();

// Assert
assertTrue(svpSpendTxHashUnsigned.isPresent());
assertEquals(svpSpendTxHash, svpSpendTxHashUnsigned.get());
}

@Test
void getSvpSpendTxHashUnsigned_whenSetToNull_shouldReturnEmpty() {
// Arrange
bridgeStorageProvider.setSvpSpendTxHashUnsigned(null);

// Act
Optional<Sha256Hash> svpSpendTxHashUnsigned = bridgeStorageProvider.getSvpSpendTxHashUnsigned();

// Assert
assertEquals(Optional.empty(), svpSpendTxHashUnsigned);
}

@Test
void getSvpSpendTxHashUnsigned_whenHashIsNullInStorage_shouldReturnEmpty() {
// Arrange
repository.addStorageBytes(bridgeAddress, SVP_SPEND_TX_HASH_UNSIGNED.getKey(), null);

// Act
Optional<Sha256Hash> svpSpendTxHashUnsigned = bridgeStorageProvider.getSvpSpendTxHashUnsigned();

// Assert
assertEquals(Optional.empty(), svpSpendTxHashUnsigned);
}

@Test
void getSvpSpendTxHashUnsigned_whenNullHashIsSetAndSaved_shouldReturnEmpty() {
// Arrange
bridgeStorageProvider.setSvpSpendTxHashUnsigned(null);
bridgeStorageProvider.save();

// Act
Optional<Sha256Hash> svpSpendTxHashUnsigned = bridgeStorageProvider.getSvpSpendTxHashUnsigned();

// Assert
assertEquals(Optional.empty(), svpSpendTxHashUnsigned);
}

@Test
void getSvpSpendTxHashUnsigned_whenHashIsCached_shouldReturnTheCachedHash() {
// Arrange
// Manually saving a hash in storage to then cache it
repository.addStorageBytes(bridgeAddress, SVP_SPEND_TX_HASH_UNSIGNED.getKey(), BridgeSerializationUtils.serializeSha256Hash(svpSpendTxHash));

// Calling method, so it retrieves the hash from storage and caches it
bridgeStorageProvider.getSvpSpendTxHashUnsigned();

// Setting a different hash in storage to make sure that when calling the method again it returns the cached one, not this one
Sha256Hash anotherSvpSpendTxHash = BitcoinTestUtils.createHash(987_654_321);
repository.addStorageBytes(bridgeAddress, SVP_SPEND_TX_HASH_UNSIGNED.getKey(), BridgeSerializationUtils.serializeSha256Hash(anotherSvpSpendTxHash));

// Act
Optional<Sha256Hash> svpSpendTxHashUnsigned = bridgeStorageProvider.getSvpSpendTxHashUnsigned();

// Assert
assertTrue(svpSpendTxHashUnsigned.isPresent());
assertEquals(svpSpendTxHash, svpSpendTxHashUnsigned.get());
}
}

@Test
Expand Down

0 comments on commit 35cb096

Please sign in to comment.