Skip to content

Commit

Permalink
feat(btcreleaseclient): add confirmation diff logic using the receipt…
Browse files Browse the repository at this point in the history
… store for unit tests
  • Loading branch information
apancorb committed Nov 1, 2024
1 parent 3842389 commit ffee1ca
Showing 1 changed file with 48 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
import co.rsk.peg.federation.*;
import co.rsk.peg.StateForFederator;
import co.rsk.peg.StateForProposedFederator;

import java.lang.reflect.Field;
import java.math.BigInteger;
import java.time.Clock;
Expand Down Expand Up @@ -102,6 +101,7 @@ class BtcReleaseClientTest {
private static final Duration PEGOUT_SIGNED_CACHE_TTL = Duration.ofMinutes(30);

private final BlockStore blockStore = mock(BlockStore.class);
private final ReceiptStore receiptStore = mock(ReceiptStore.class);
private final Block bestBlock = mock(Block.class);
private final NetworkParameters params = RegTestParams.get();
private final BridgeConstants bridgeConstants = Constants.regtest().bridgeConstants;
Expand All @@ -115,9 +115,21 @@ class BtcReleaseClientTest {
@BeforeEach
void setup() {
// ensure confirmation difference always passes
Keccak256 rskTxHash = createHash(1);
Keccak256 blockHash = createHash(2);
TransactionInfo transactionInfoForTxWaitingForSignatures = mock(TransactionInfo.class);
Block blockWithTxWaitingForSignatures = mock(Block.class);
when(blockWithTxWaitingForSignatures.getNumber()).thenReturn(0L);
when(blockStore.getBlockByHash(any())).thenReturn(blockWithTxWaitingForSignatures);
when(transactionInfoForTxWaitingForSignatures.getBlockHash())
.thenReturn(blockHash.getBytes());
when(blockWithTxWaitingForSignatures.getHash())
.thenReturn(blockHash);
when(blockWithTxWaitingForSignatures.getNumber())
.thenReturn(0L);

when(receiptStore.getInMainChain(rskTxHash.getBytes(), blockStore))
.thenReturn(Optional.of(transactionInfoForTxWaitingForSignatures));
when(blockStore.getBlockByHash(blockHash.getBytes()))
.thenReturn(blockWithTxWaitingForSignatures);
when(bestBlock.getNumber()).thenReturn(5_000L);
}

Expand All @@ -137,6 +149,8 @@ void start_whenFederationMemberNotPartOfDesiredFederation_shouldThrowException()

BtcReleaseClient btcReleaseClient = new BtcReleaseClient(
mock(Ethereum.class),
blockStore,
receiptStore,
federatorSupport,
powpegNodeSystemProperties,
mock(NodeBlockProcessor.class)
Expand All @@ -157,6 +171,7 @@ void if_start_not_called_rsk_blockchain_not_listened() {
new BtcReleaseClient(
ethereum,
blockStore,
receiptStore,
mock(FederatorSupport.class),
powpegNodeSystemProperties,
mock(NodeBlockProcessor.class)
Expand All @@ -176,9 +191,9 @@ void when_start_called_rsk_blockchain_is_listened() {

BtcReleaseClient btcReleaseClient = new BtcReleaseClient(
ethereum,
federatorSupport,
blockStore,
mock(FederatorSupport.class),
receiptStore,
federatorSupport,
powpegNodeSystemProperties,
mock(NodeBlockProcessor.class)
);
Expand Down Expand Up @@ -207,9 +222,9 @@ void if_stop_called_with_just_one_federation_rsk_blockchain_is_still_listened()

BtcReleaseClient btcReleaseClient = new BtcReleaseClient(
ethereum,
federatorSupport,
blockStore,
mock(FederatorSupport.class),
receiptStore,
federatorSupport,
powpegNodeSystemProperties,
mock(NodeBlockProcessor.class)
);
Expand Down Expand Up @@ -241,9 +256,9 @@ void if_stop_called_with_federations_rsk_blockchain_is_not_listened() {

BtcReleaseClient btcReleaseClient = new BtcReleaseClient(
ethereum,
federatorSupport,
blockStore,
mock(FederatorSupport.class),
receiptStore,
federatorSupport,
powpegNodeSystemProperties,
mock(NodeBlockProcessor.class)
);
Expand Down Expand Up @@ -305,9 +320,9 @@ void processReleases_ok() throws Exception {

BtcReleaseClient client = new BtcReleaseClient(
mock(Ethereum.class),
federatorSupport,
blockStore,
mock(FederatorSupport.class),
receiptStore,
federatorSupport,
powpegNodeSystemProperties,
mock(NodeBlockProcessor.class)
);
Expand Down Expand Up @@ -438,6 +453,7 @@ void having_two_pegouts_signs_only_one() throws Exception {
BtcReleaseClient btcReleaseClient = new BtcReleaseClient(
ethereum,
blockStore,
receiptStore,
federatorSupport,
powpegNodeSystemProperties,
mock(NodeBlockProcessor.class)
Expand Down Expand Up @@ -528,6 +544,7 @@ void onBestBlock_whenPegoutTxIsCached_shouldNotSignSamePegoutTxAgain() throws Ex
BtcReleaseClient btcReleaseClient = new BtcReleaseClient(
ethereum,
blockStore,
receiptStore,
federatorSupport,
powpegNodeSystemProperties,
mock(NodeBlockProcessor.class)
Expand Down Expand Up @@ -633,6 +650,7 @@ void onBestBlock_whenPegoutTxIsCachedWithInvalidTimestamp_shouldSignSamePegoutTx
BtcReleaseClient btcReleaseClient = new BtcReleaseClient(
ethereum,
blockStore,
receiptStore,
federatorSupport,
powpegNodeSystemProperties,
mock(NodeBlockProcessor.class)
Expand Down Expand Up @@ -684,6 +702,7 @@ void onBestBlock_whenPegoutTxIsCachedWithInvalidTimestamp_shouldSignSamePegoutTx
void onBestBlock_whenOnlySvpSpendTxWaitingForSignaturesIsAvailable_shouldAddSignature() throws Exception {
// Arrange
Federation federation = TestUtils.createFederation(params, 9);
FederationMember federationMember = federation.getMembers().get(0);
BtcTransaction svpSpendTx = TestUtils.createBtcTransaction(params, federation);
Keccak256 svpSpendCreationRskTxHash = createHash(0);
Map.Entry<Keccak256, BtcTransaction> entry = new AbstractMap.SimpleEntry<>(svpSpendCreationRskTxHash, svpSpendTx);
Expand All @@ -697,6 +716,7 @@ void onBestBlock_whenOnlySvpSpendTxWaitingForSignaturesIsAvailable_shouldAddSign
}).when(ethereum).addListener(any(EthereumListener.class));

FederatorSupport federatorSupport = mock(FederatorSupport.class);
doReturn(federationMember).when(federatorSupport).getFederationMember();
// return svp spend tx waiting for signatures
doReturn(Optional.of(stateForProposedFederator)).when(federatorSupport).getStateForProposedFederator();
// returns zero pegouts waiting for signatures
Expand Down Expand Up @@ -745,6 +765,7 @@ void onBestBlock_whenOnlySvpSpendTxWaitingForSignaturesIsAvailable_shouldAddSign
BtcReleaseClient btcReleaseClient = new BtcReleaseClient(
ethereum,
blockStore,
receiptStore,
federatorSupport,
powpegNodeSystemProperties,
mock(NodeBlockProcessor.class)
Expand Down Expand Up @@ -776,6 +797,7 @@ void onBestBlock_whenOnlySvpSpendTxWaitingForSignaturesIsAvailable_shouldAddSign
void onBestBlock_whenPegoutTxIsNotReadyToBeSigned_shouldNotAddSignature() throws Exception {
// Arrange
Federation federation = TestUtils.createFederation(params, 9);
FederationMember federationMember = federation.getMembers().get(0);
BtcTransaction pegout = TestUtils.createBtcTransaction(params, federation);
Keccak256 pegoutCreationRskTxHash = createHash(0);
SortedMap<Keccak256, BtcTransaction> rskTxsWaitingForSignatures = new TreeMap<>();
Expand All @@ -790,6 +812,7 @@ void onBestBlock_whenPegoutTxIsNotReadyToBeSigned_shouldNotAddSignature() throws
}).when(ethereum).addListener(any(EthereumListener.class));

FederatorSupport federatorSupport = mock(FederatorSupport.class);
doReturn(federationMember).when(federatorSupport).getFederationMember();
doReturn(stateForFederator).when(federatorSupport).getStateForFederator();

ECKey ecKey = new ECKey();
Expand Down Expand Up @@ -835,6 +858,7 @@ void onBestBlock_whenPegoutTxIsNotReadyToBeSigned_shouldNotAddSignature() throws
BtcReleaseClient btcReleaseClient = new BtcReleaseClient(
ethereum,
blockStore,
receiptStore,
federatorSupport,
powpegNodeSystemProperties,
mock(NodeBlockProcessor.class)
Expand Down Expand Up @@ -898,6 +922,7 @@ void onBestBlock_return_when_node_is_syncing() throws BtcReleaseClientException
BtcReleaseClient btcReleaseClient = new BtcReleaseClient(
ethereum,
blockStore,
receiptStore,
federatorSupport,
powpegNodeSystemProperties,
nodeBlockProcessor
Expand Down Expand Up @@ -949,6 +974,7 @@ void onBestBlock_return_when_pegout_is_disabled() throws BtcReleaseClientExcepti
BtcReleaseClient btcReleaseClient = new BtcReleaseClient(
ethereum,
blockStore,
receiptStore,
federatorSupport,
powpegNodeSystemProperties,
nodeBlockProcessor
Expand Down Expand Up @@ -1000,6 +1026,7 @@ void onBlock_return_when_node_is_syncing() {
BtcReleaseClient btcReleaseClient = new BtcReleaseClient(
ethereum,
blockStore,
receiptStore,
federatorSupport,
powpegNodeSystemProperties,
nodeBlockProcessor
Expand Down Expand Up @@ -1047,6 +1074,7 @@ void onBlock_return_when_pegout_is_disabled() {
BtcReleaseClient btcReleaseClient = new BtcReleaseClient(
ethereum,
blockStore,
receiptStore,
federatorSupport,
powpegNodeSystemProperties,
nodeBlockProcessor
Expand Down Expand Up @@ -1165,9 +1193,9 @@ void validateTxCanBeSigned_federatorAlreadySigned() throws Exception {

BtcReleaseClient client = new BtcReleaseClient(
mock(Ethereum.class),
federatorSupport,
blockStore,
mock(FederatorSupport.class),
receiptStore,
federatorSupport,
powpegNodeSystemProperties,
mock(NodeBlockProcessor.class)
);
Expand Down Expand Up @@ -1211,6 +1239,7 @@ void validateTxCanBeSigned_federationCantSign() throws Exception {
BtcReleaseClient client = new BtcReleaseClient(
mock(Ethereum.class),
blockStore,
receiptStore,
mock(FederatorSupport.class),
powpegNodeSystemProperties,
mock(NodeBlockProcessor.class)
Expand Down Expand Up @@ -1270,6 +1299,7 @@ void removeSignaturesFromTransaction() {
BtcReleaseClient client = new BtcReleaseClient(
mock(Ethereum.class),
blockStore,
receiptStore,
mock(FederatorSupport.class),
powpegNodeSystemProperties,
mock(NodeBlockProcessor.class)
Expand Down Expand Up @@ -1357,9 +1387,9 @@ private void test_validateTxCanBeSigned(

BtcReleaseClient client = new BtcReleaseClient(
mock(Ethereum.class),
federatorSupport,
blockStore,
mock(FederatorSupport.class),
receiptStore,
federatorSupport,
powpegNodeSystemProperties,
mock(NodeBlockProcessor.class)
);
Expand Down Expand Up @@ -1392,6 +1422,7 @@ private void test_extractStandardRedeemScript(
BtcReleaseClient client = new BtcReleaseClient(
mock(Ethereum.class),
blockStore,
receiptStore,
mock(FederatorSupport.class),
powpegNodeSystemProperties,
mock(NodeBlockProcessor.class)
Expand Down Expand Up @@ -1528,6 +1559,7 @@ private void testUsageOfStorageWhenSigning(boolean shouldHaveDataInFile)
BtcReleaseClient btcReleaseClient = new BtcReleaseClient(
ethereumImpl,
blockStore,
receiptStore,
federatorSupport,
powpegNodeSystemProperties,
nodeBlockProcessor
Expand Down Expand Up @@ -1577,6 +1609,7 @@ private BtcReleaseClient createBtcClient() {
return new BtcReleaseClient(
mock(Ethereum.class),
blockStore,
receiptStore,
mock(FederatorSupport.class),
powpegNodeSystemProperties,
mock(NodeBlockProcessor.class)
Expand Down

0 comments on commit ffee1ca

Please sign in to comment.