Skip to content

Commit

Permalink
Merge pull request #2702 from rsksmart/feat/set_save_spend_unsigned_t…
Browse files Browse the repository at this point in the history
…x_hash

Create set and save SvpFundTransactionHashUnsigned methods
  • Loading branch information
marcos-iov authored Aug 29, 2024
2 parents 2928e52 + cb8df0a commit ebd5a13
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
20 changes: 20 additions & 0 deletions rskj-core/src/main/java/co/rsk/peg/BridgeStorageProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ public class BridgeStorageProvider {
private boolean isSvpFundTxHashUnsignedSet = false;
private Sha256Hash svpFundTxHashSigned;
private boolean isSvpFundTxHashSignedSet = false;
private Sha256Hash svpSpendTxHashUnsigned;
private boolean isSvpSpendTxHashUnsignedSet = false;

public BridgeStorageProvider(
Repository repository,
Expand Down Expand Up @@ -594,6 +596,23 @@ private void saveSvpFundTxHashSigned() {
repository.addStorageBytes(contractAddress, SVP_FUND_TX_HASH_SIGNED.getKey(), data);
}

public void setSvpSpendTxHashUnsigned(Sha256Hash hash) {
this.svpSpendTxHashUnsigned = hash;
this.isSvpSpendTxHashUnsignedSet = true;
}

private void saveSvpSpendTxHashUnsigned() {
if (!activations.isActive(RSKIP419) || !isSvpSpendTxHashUnsignedSet) {
return;
}

byte[] data = Optional.ofNullable(svpSpendTxHashUnsigned)
.map(BridgeSerializationUtils::serializeSha256Hash)
.orElse(null);

repository.addStorageBytes(contractAddress, SVP_SPEND_TX_HASH_UNSIGNED.getKey(), data);
}

public void save() {
saveBtcTxHashesAlreadyProcessed();

Expand All @@ -619,6 +638,7 @@ public void save() {

saveSvpFundTxHashUnsigned();
saveSvpFundTxHashSigned();
saveSvpSpendTxHashUnsigned();
}

private DataWord getStorageKeyForBtcTxHashAlreadyProcessed(Sha256Hash btcTxHash) {
Expand Down
57 changes: 57 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 @@ -633,6 +633,63 @@ void getSvpFundTxHashSigned_whenNullHashIsCached_shouldReturnNewSavedHash() {
}
}

@Nested
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@Tag("save, set and get svp spend transaction hash unsigned tests")
class SvpSpendTxHashUnsignedTests {
private final Sha256Hash svpSpendTxHash = BitcoinTestUtils.createHash(123_456_789);
private Repository repository;
private BridgeStorageProvider bridgeStorageProvider;

@BeforeEach
void setup() {
repository = createRepository();
bridgeStorageProvider = createBridgeStorageProvider(repository, mainnetBtcParams, activationsAllForks);
}

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

// Act
bridgeStorageProvider.setSvpSpendTxHashUnsigned(svpSpendTxHash);
bridgeStorageProvider.save();

// Assert
byte[] actualSvpSpendTxHashSerialized = repository.getStorageBytes(bridgeAddress, SVP_SPEND_TX_HASH_UNSIGNED.getKey());
assertNull(actualSvpSpendTxHashSerialized);
}

@Test
void saveSvpSpendTxHashUnsigned_postLovell700_shouldSaveInStorage() throws IOException {
// Act
bridgeStorageProvider.setSvpSpendTxHashUnsigned(svpSpendTxHash);
bridgeStorageProvider.save();

// Assert
byte[] svpSpendTxHashSerialized = BridgeSerializationUtils.serializeSha256Hash(svpSpendTxHash);
byte[] actualSvpSpendTxHashSerialized = repository.getStorageBytes(bridgeAddress, SVP_SPEND_TX_HASH_UNSIGNED.getKey());
assertArrayEquals(svpSpendTxHashSerialized, actualSvpSpendTxHashSerialized);
}

@Test
void saveSvpSpendTxHashUnsigned_postLovell700AndResettingToNull_shouldSaveNullInStorage() throws IOException {
// Initially setting a valid hash in storage
bridgeStorageProvider.setSvpSpendTxHashUnsigned(svpSpendTxHash);
bridgeStorageProvider.save();

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

// Assert
byte[] actualSvpSpendTxHashSerialized = repository.getStorageBytes(bridgeAddress, SVP_SPEND_TX_HASH_UNSIGNED.getKey());
assertNull(actualSvpSpendTxHashSerialized);
}
}

@Test
void getReleaseRequestQueue_before_rskip_146_activation() throws IOException {
Repository repositoryMock = mock(Repository.class);
Expand Down

0 comments on commit ebd5a13

Please sign in to comment.