-
Notifications
You must be signed in to change notification settings - Fork 266
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
Register spend tx #2851
Merged
marcos-iov
merged 4 commits into
feature/powpeg_validation_protocol-phase4
from
register_spend_tx
Nov 21, 2024
Merged
Register spend tx #2851
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3513c98
Modify public method to remove signatures for just returning the hash…
julia-zack 30dafce
Register spend transaction
julia-zack 007ffad
Create new registerSvpSpendTransaction method. Refactor to make code …
julia-zack 29bc369
Add test for getTxHashWithoutSigs using a real pegout. Minor refactor
julia-zack File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,6 +82,9 @@ public class BridgeSupportSvpTest { | |
private Sha256Hash svpSpendTransactionHashUnsigned; | ||
private BtcTransaction svpSpendTransaction; | ||
|
||
private PartialMerkleTree pmtWithTransactions; | ||
private int btcBlockWithPmtHeight; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
long rskExecutionBlockNumber = 1000L; | ||
|
@@ -467,8 +470,6 @@ private void assertSvpFundTransactionValuesWereNotUpdated() { | |
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
@Tag("Fund transaction registration tests") | ||
class FundTxRegistrationTests { | ||
private PartialMerkleTree pmtWithTransactions; | ||
private int btcBlockWithPmtHeight; | ||
|
||
@Test | ||
void registerBtcTransaction_forSvpFundTransactionChange_whenProposedFederationDoesNotExist_shouldRegisterTransactionButNotUpdateSvpFundTransactionValues() throws Exception { | ||
|
@@ -595,43 +596,6 @@ void registerBtcTransaction_forSvpFundTransactionChange_whenSvpPeriodIsOngoing_s | |
assertSvpFundTransactionValuesWereUpdated(); | ||
} | ||
|
||
private void setUpForTransactionRegistration(BtcTransaction transaction) throws BlockStoreException { | ||
// recreate a valid chain that has the tx, so it passes the previous checks in registerBtcTransaction | ||
BtcBlockStoreWithCache.Factory btcBlockStoreFactory = new RepositoryBtcBlockStoreWithCache.Factory(btcMainnetParams, 100, 100); | ||
BtcBlockStoreWithCache btcBlockStoreWithCache = btcBlockStoreFactory.newInstance(repository, bridgeMainNetConstants, bridgeStorageProvider, allActivations); | ||
|
||
pmtWithTransactions = createValidPmtForTransactions(Collections.singletonList(transaction.getHash()), btcMainnetParams); | ||
btcBlockWithPmtHeight = bridgeMainNetConstants.getBtcHeightWhenPegoutTxIndexActivates() + bridgeMainNetConstants.getPegoutTxIndexGracePeriodInBtcBlocks(); // we want pegout tx index to be activated | ||
|
||
int chainHeight = btcBlockWithPmtHeight + bridgeMainNetConstants.getBtc2RskMinimumAcceptableConfirmations(); | ||
recreateChainFromPmt(btcBlockStoreWithCache, chainHeight, pmtWithTransactions, btcBlockWithPmtHeight, btcMainnetParams); | ||
|
||
bridgeStorageProvider.save(); | ||
|
||
bridgeSupport = bridgeSupportBuilder | ||
.withBridgeConstants(bridgeMainNetConstants) | ||
.withProvider(bridgeStorageProvider) | ||
.withActivations(allActivations) | ||
.withFederationSupport(federationSupport) | ||
.withFeePerKbSupport(feePerKbSupport) | ||
.withExecutionBlock(rskExecutionBlock) | ||
.withBtcBlockStoreFactory(btcBlockStoreFactory) | ||
.withRepository(repository) | ||
.build(); | ||
} | ||
|
||
private void assertActiveFederationUtxosSize(int expectedActiveFederationUtxosSize) { | ||
assertEquals(expectedActiveFederationUtxosSize, federationSupport.getActiveFederationBtcUTXOs().size()); | ||
} | ||
|
||
private void assertTransactionWasProcessed(Sha256Hash transactionHash) throws IOException { | ||
Optional<Long> rskBlockHeightAtWhichBtcTxWasProcessed = bridgeStorageProvider.getHeightIfBtcTxhashIsAlreadyProcessed(transactionHash); | ||
assertTrue(rskBlockHeightAtWhichBtcTxWasProcessed.isPresent()); | ||
|
||
long rskExecutionBlockNumber = rskExecutionBlock.getNumber(); | ||
assertEquals(rskExecutionBlockNumber, rskBlockHeightAtWhichBtcTxWasProcessed.get()); | ||
} | ||
|
||
Comment on lines
-598
to
-634
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this was just moved outside to be reused |
||
private void assertSvpFundTransactionValuesWereUpdated() { | ||
Optional<BtcTransaction> svpFundTransactionSignedOpt = bridgeStorageProvider.getSvpFundTxSigned(); | ||
assertTrue(svpFundTransactionSignedOpt.isPresent()); | ||
|
@@ -666,7 +630,7 @@ void updateCollections_whenSpendTxCanBeCreated_createsExpectedSpendTxAndSavesThe | |
bridgeStorageProvider.save(); | ||
|
||
// assert | ||
svpSpendTransactionHashUnsigned = assertSvpSpendTxHashUnsignedWasSavedInStorage(); | ||
svpSpendTransactionHashUnsigned = assertSvpSpendTxHashUnsignedIsSavedInStorage(); | ||
svpSpendTransaction = assertSvpSpendTxIsWaitingForSignatures(); | ||
assertSvpSpendTxHasExpectedInputsAndOutputs(); | ||
|
||
|
@@ -727,7 +691,7 @@ private void assertInputHasExpectedScriptSig(TransactionInput input, Script rede | |
} | ||
} | ||
|
||
private Sha256Hash assertSvpSpendTxHashUnsignedWasSavedInStorage() { | ||
private Sha256Hash assertSvpSpendTxHashUnsignedIsSavedInStorage() { | ||
Optional<Sha256Hash> svpSpendTransactionHashUnsignedOpt = bridgeStorageProvider.getSvpSpendTxHashUnsigned(); | ||
assertTrue(svpSpendTransactionHashUnsignedOpt.isPresent()); | ||
|
||
|
@@ -982,6 +946,105 @@ private void assertFederatorSignedInputs(List<TransactionInput> inputs, List<Sha | |
} | ||
} | ||
|
||
@Nested | ||
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
@Tag("Spend transaction registration tests") | ||
class SpendTxRegistrationTests { | ||
@Test | ||
void registerBtcTransaction_whenValidationPeriodEnded_shouldNotProcessSpendTx() throws BlockStoreException, BridgeIllegalArgumentException, IOException { | ||
// arrange | ||
arrangeExecutionBlockIsAfterValidationPeriodEnded(); | ||
|
||
arrangeSvpSpendTransaction(); | ||
setUpForTransactionRegistration(svpSpendTransaction); | ||
|
||
// act | ||
int activeFederationUtxosSizeBeforeRegisteringTx = federationSupport.getActiveFederationBtcUTXOs().size(); | ||
bridgeSupport.registerBtcTransaction( | ||
rskTx, | ||
svpSpendTransaction.bitcoinSerialize(), | ||
btcBlockWithPmtHeight, | ||
pmtWithTransactions.bitcoinSerialize() | ||
); | ||
bridgeStorageProvider.save(); | ||
|
||
// assert | ||
assertActiveFederationUtxosSize(activeFederationUtxosSizeBeforeRegisteringTx); | ||
assertTransactionWasNotProcessed(svpSpendTransaction.getHash()); | ||
assertSvpSpendTxHashUnsignedIsSavedInStorage(); | ||
} | ||
|
||
@Test | ||
void registerBtcTransaction_whenIsTheSpendTransaction_shouldProcessSpendTx() throws BlockStoreException, BridgeIllegalArgumentException, IOException { | ||
// arrange | ||
arrangeSvpSpendTransaction(); | ||
setUpForTransactionRegistration(svpSpendTransaction); | ||
|
||
// act | ||
int activeFederationUtxosSizeBeforeRegisteringTx = federationSupport.getActiveFederationBtcUTXOs().size(); | ||
bridgeSupport.registerBtcTransaction( | ||
rskTx, | ||
svpSpendTransaction.bitcoinSerialize(), | ||
btcBlockWithPmtHeight, | ||
pmtWithTransactions.bitcoinSerialize() | ||
); | ||
bridgeStorageProvider.save(); | ||
|
||
// assert | ||
assertActiveFederationUtxosSize(activeFederationUtxosSizeBeforeRegisteringTx + 1); | ||
assertTransactionWasProcessed(svpSpendTransaction.getHash()); | ||
assertNoSvpSpendTxHash(); | ||
} | ||
|
||
@Test | ||
void registerBtcTransaction_whenIsNotTheSpendTransaction_shouldNotProcessSpendTx() throws BlockStoreException, BridgeIllegalArgumentException, IOException { | ||
// arrange | ||
arrangeSvpSpendTransaction(); | ||
setUpForTransactionRegistration(svpSpendTransaction); | ||
|
||
BtcTransaction pegout = createPegout(proposedFederation.getRedeemScript()); | ||
savePegoutIndex(pegout); | ||
signInputs(pegout); | ||
setUpForTransactionRegistration(pegout); | ||
|
||
// act | ||
int activeFederationUtxosSizeBeforeRegisteringTx = federationSupport.getActiveFederationBtcUTXOs().size(); | ||
bridgeSupport.registerBtcTransaction( | ||
rskTx, | ||
pegout.bitcoinSerialize(), | ||
btcBlockWithPmtHeight, | ||
pmtWithTransactions.bitcoinSerialize() | ||
); | ||
bridgeStorageProvider.save(); | ||
|
||
// assert | ||
assertActiveFederationUtxosSize(activeFederationUtxosSizeBeforeRegisteringTx + 1); | ||
assertTransactionWasNotProcessed(svpSpendTransaction.getHash()); | ||
assertSvpSpendTxHashUnsignedIsSavedInStorage(); | ||
} | ||
|
||
@Test | ||
void registerBtcTransaction_whenSpendTransactionHashIsNotSaved_shouldNotProcessSpendTx() throws BlockStoreException, BridgeIllegalArgumentException, IOException { | ||
// arrange | ||
recreateSvpSpendTransactionUnsigned(); | ||
setUpForTransactionRegistration(svpSpendTransaction); | ||
|
||
// act | ||
int activeFederationUtxosSizeBeforeRegisteringTx = federationSupport.getActiveFederationBtcUTXOs().size(); | ||
bridgeSupport.registerBtcTransaction( | ||
rskTx, | ||
svpSpendTransaction.bitcoinSerialize(), | ||
btcBlockWithPmtHeight, | ||
pmtWithTransactions.bitcoinSerialize() | ||
); | ||
bridgeStorageProvider.save(); | ||
|
||
// assert | ||
assertActiveFederationUtxosSize(activeFederationUtxosSizeBeforeRegisteringTx); | ||
assertTransactionWasNotProcessed(svpSpendTransaction.getHash()); | ||
} | ||
} | ||
|
||
private void arrangeExecutionBlockIsAfterValidationPeriodEnded() { | ||
long validationPeriodEndBlock = proposedFederation.getCreationBlockNumber() | ||
+ bridgeMainNetConstants.getFederationConstants().getValidationPeriodDurationInBlocks(); | ||
|
@@ -1081,6 +1144,48 @@ private void saveSvpSpendTransactionValues() { | |
bridgeStorageProvider.save(); | ||
} | ||
|
||
private void setUpForTransactionRegistration(BtcTransaction transaction) throws BlockStoreException { | ||
// recreate a valid chain that has the tx, so it passes the previous checks in registerBtcTransaction | ||
BtcBlockStoreWithCache.Factory btcBlockStoreFactory = new RepositoryBtcBlockStoreWithCache.Factory(btcMainnetParams, 100, 100); | ||
BtcBlockStoreWithCache btcBlockStoreWithCache = btcBlockStoreFactory.newInstance(repository, bridgeMainNetConstants, bridgeStorageProvider, allActivations); | ||
|
||
pmtWithTransactions = createValidPmtForTransactions(Collections.singletonList(transaction.getHash()), btcMainnetParams); | ||
btcBlockWithPmtHeight = bridgeMainNetConstants.getBtcHeightWhenPegoutTxIndexActivates() + bridgeMainNetConstants.getPegoutTxIndexGracePeriodInBtcBlocks(); // we want pegout tx index to be activated | ||
|
||
int chainHeight = btcBlockWithPmtHeight + bridgeMainNetConstants.getBtc2RskMinimumAcceptableConfirmations(); | ||
recreateChainFromPmt(btcBlockStoreWithCache, chainHeight, pmtWithTransactions, btcBlockWithPmtHeight, btcMainnetParams); | ||
|
||
bridgeStorageProvider.save(); | ||
|
||
bridgeSupport = bridgeSupportBuilder | ||
.withBridgeConstants(bridgeMainNetConstants) | ||
.withProvider(bridgeStorageProvider) | ||
.withActivations(allActivations) | ||
.withFederationSupport(federationSupport) | ||
.withFeePerKbSupport(feePerKbSupport) | ||
.withExecutionBlock(rskExecutionBlock) | ||
.withBtcBlockStoreFactory(btcBlockStoreFactory) | ||
.withRepository(repository) | ||
.build(); | ||
} | ||
|
||
private void assertActiveFederationUtxosSize(int expectedActiveFederationUtxosSize) { | ||
assertEquals(expectedActiveFederationUtxosSize, federationSupport.getActiveFederationBtcUTXOs().size()); | ||
} | ||
|
||
private void assertTransactionWasProcessed(Sha256Hash transactionHash) throws IOException { | ||
Optional<Long> rskBlockHeightAtWhichBtcTxWasProcessed = bridgeStorageProvider.getHeightIfBtcTxhashIsAlreadyProcessed(transactionHash); | ||
assertTrue(rskBlockHeightAtWhichBtcTxWasProcessed.isPresent()); | ||
|
||
long rskExecutionBlockNumber = rskExecutionBlock.getNumber(); | ||
assertEquals(rskExecutionBlockNumber, rskBlockHeightAtWhichBtcTxWasProcessed.get()); | ||
} | ||
|
||
private void assertTransactionWasNotProcessed(Sha256Hash transactionHash) throws IOException { | ||
Optional<Long> rskBlockHeightAtWhichBtcTxWasProcessed = bridgeStorageProvider.getHeightIfBtcTxhashIsAlreadyProcessed(transactionHash); | ||
assertFalse(rskBlockHeightAtWhichBtcTxWasProcessed.isPresent()); | ||
} | ||
|
||
private void assertNoSvpFundTxHashUnsigned() { | ||
assertFalse(bridgeStorageProvider.getSvpFundTxHashUnsigned().isPresent()); | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit, just to return early
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i put it this way considering the txs are normally segwit (correct me if im wrong! :) ), so we will have the common behavior outside the check