Skip to content

Commit

Permalink
feat(btcreleaseclient): add unit test for federation member is not pa…
Browse files Browse the repository at this point in the history
…rt of new federationa and fix the rest of exisiting tests to accomdate new condition
  • Loading branch information
apancorb committed Oct 30, 2024
1 parent 9f9a4d1 commit 152218e
Showing 1 changed file with 80 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,31 @@ void setup() {
bridgeConstants = Constants.regtest().bridgeConstants;
}

@Test
void start_whenFederationMemberNotPartOfDesiredFederation_shouldThrowException() {
// Arrange
PowpegNodeSystemProperties powpegNodeSystemProperties = mock(PowpegNodeSystemProperties.class);
when(powpegNodeSystemProperties.getNetworkConstants()).thenReturn(Constants.regtest());
when(powpegNodeSystemProperties.getPegoutSignedCacheTtl())
.thenReturn(PEGOUT_SIGNED_CACHE_TTL);

FederatorSupport federatorSupport = mock(FederatorSupport.class);
Federation federation = TestUtils.createFederation(params, 1);
Federation otherFederation = TestUtils.createFederation(params, 2);
FederationMember federationMember = otherFederation.getMembers().get(1);
doReturn(federationMember).when(federatorSupport).getFederationMember();

BtcReleaseClient btcReleaseClient = new BtcReleaseClient(
mock(Ethereum.class),
federatorSupport,
powpegNodeSystemProperties,
mock(NodeBlockProcessor.class)
);

// Act & Assert
assertThrows(IllegalStateException.class, () -> btcReleaseClient.start(federation));
}

@Test
void if_start_not_called_rsk_blockchain_not_listened() {
Ethereum ethereum = mock(Ethereum.class);
Expand All @@ -136,21 +161,26 @@ void if_start_not_called_rsk_blockchain_not_listened() {
void when_start_called_rsk_blockchain_is_listened() {
Ethereum ethereum = mock(Ethereum.class);
PowpegNodeSystemProperties powpegNodeSystemProperties = mock(PowpegNodeSystemProperties.class);
FederatorSupport federatorSupport = mock(FederatorSupport.class);
Mockito.doReturn(Constants.regtest()).when(powpegNodeSystemProperties).getNetworkConstants();
when(powpegNodeSystemProperties.getPegoutSignedCacheTtl())
.thenReturn(PEGOUT_SIGNED_CACHE_TTL);

BtcReleaseClient btcReleaseClient = new BtcReleaseClient(
ethereum,
mock(FederatorSupport.class),
federatorSupport,
powpegNodeSystemProperties,
mock(NodeBlockProcessor.class)
);

Federation fed1 = TestUtils.createFederation(params, 1);
FederationMember federationMember1 = fed1.getMembers().get(0);
doReturn(federationMember1).when(federatorSupport).getFederationMember();
btcReleaseClient.start(fed1);

Federation fed2 = TestUtils.createFederation(params, 1);
FederationMember federationMember2 = fed2.getMembers().get(0);
doReturn(federationMember2).when(federatorSupport).getFederationMember();
btcReleaseClient.start(fed2);

verify(ethereum, Mockito.times(1)).addListener(ArgumentMatchers.any(EthereumListener.class));
Expand All @@ -160,21 +190,26 @@ void when_start_called_rsk_blockchain_is_listened() {
void if_stop_called_with_just_one_federation_rsk_blockchain_is_still_listened() {
Ethereum ethereum = mock(Ethereum.class);
PowpegNodeSystemProperties powpegNodeSystemProperties = mock(PowpegNodeSystemProperties.class);
FederatorSupport federatorSupport = mock(FederatorSupport.class);
Mockito.doReturn(Constants.regtest()).when(powpegNodeSystemProperties).getNetworkConstants();
when(powpegNodeSystemProperties.getPegoutSignedCacheTtl())
.thenReturn(PEGOUT_SIGNED_CACHE_TTL);

BtcReleaseClient btcReleaseClient = new BtcReleaseClient(
ethereum,
mock(FederatorSupport.class),
federatorSupport,
powpegNodeSystemProperties,
mock(NodeBlockProcessor.class)
);

Federation fed1 = TestUtils.createFederation(params, 1);
FederationMember federationMember1 = fed1.getMembers().get(0);
doReturn(federationMember1).when(federatorSupport).getFederationMember();
btcReleaseClient.start(fed1);

Federation fed2 = TestUtils.createFederation(params, 1);
FederationMember federationMember2 = fed2.getMembers().get(0);
doReturn(federationMember2).when(federatorSupport).getFederationMember();
btcReleaseClient.start(fed2);

Mockito.verify(ethereum, Mockito.times(1)).addListener(ArgumentMatchers.any(EthereumListener.class));
Expand All @@ -186,22 +221,27 @@ void if_stop_called_with_just_one_federation_rsk_blockchain_is_still_listened()
@Test
void if_stop_called_with_federations_rsk_blockchain_is_not_listened() {
Ethereum ethereum = mock(Ethereum.class);
FederatorSupport federatorSupport = mock(FederatorSupport.class);
PowpegNodeSystemProperties powpegNodeSystemProperties = mock(PowpegNodeSystemProperties.class);
Mockito.doReturn(Constants.regtest()).when(powpegNodeSystemProperties).getNetworkConstants();
when(powpegNodeSystemProperties.getPegoutSignedCacheTtl())
.thenReturn(PEGOUT_SIGNED_CACHE_TTL);

BtcReleaseClient btcReleaseClient = new BtcReleaseClient(
ethereum,
mock(FederatorSupport.class),
federatorSupport,
powpegNodeSystemProperties,
mock(NodeBlockProcessor.class)
);

Federation fed1 = TestUtils.createFederation(params, 1);
FederationMember federationMember1 = fed1.getMembers().get(0);
doReturn(federationMember1).when(federatorSupport).getFederationMember();
btcReleaseClient.start(fed1);

Federation fed2 = TestUtils.createFederation(params, 1);
FederationMember federationMember2 = fed2.getMembers().get(0);
doReturn(federationMember2).when(federatorSupport).getFederationMember();
btcReleaseClient.start(fed2);

Mockito.verify(ethereum, Mockito.times(1)).addListener(ArgumentMatchers.any(EthereumListener.class));
Expand All @@ -215,6 +255,7 @@ void if_stop_called_with_federations_rsk_blockchain_is_not_listened() {
void processReleases_ok() throws Exception {
// Arrange
Federation federation = TestUtils.createFederation(params, 1);
FederationMember federationMember = federation.getMembers().get(0);

// Create a tx from the Fed to a random btc address
BtcTransaction releaseTx = new BtcTransaction(params);
Expand Down Expand Up @@ -245,9 +286,12 @@ void processReleases_ok() throws Exception {
.any(ReleaseCreationInformation.class)))
.thenReturn(messageBuilder);

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

BtcReleaseClient client = new BtcReleaseClient(
mock(Ethereum.class),
mock(FederatorSupport.class),
federatorSupport,
powpegNodeSystemProperties,
mock(NodeBlockProcessor.class)
);
Expand Down Expand Up @@ -296,6 +340,7 @@ void processReleases_ok() throws Exception {
void having_two_pegouts_signs_only_one() throws Exception {
// Arrange
Federation federation = TestUtils.createFederation(params, 1);
FederationMember federationMember = federation.getMembers().get(0);
BtcTransaction tx1 = TestUtils.createBtcTransaction(params, federation);
BtcTransaction tx2 = TestUtils.createBtcTransaction(params, federation);

Expand All @@ -322,6 +367,7 @@ void having_two_pegouts_signs_only_one() throws Exception {
any(byte[].class)
);
doReturn(stateForFederator).when(federatorSupport).getStateForFederator();
doReturn(federationMember).when(federatorSupport).getFederationMember();

ECKey ecKey = new ECKey();
ECKey.ECDSASignature ethSig = ecKey.doSign(new byte[]{});
Expand Down Expand Up @@ -405,6 +451,7 @@ void having_two_pegouts_signs_only_one() throws Exception {
@Test
void onBestBlock_whenPegoutTxIsCached_shouldNotSignSamePegoutTxAgain() throws Exception {
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 @@ -420,6 +467,7 @@ void onBestBlock_whenPegoutTxIsCached_shouldNotSignSamePegoutTxAgain() throws Ex

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

ECKey ecKey = new ECKey();
BtcECKey fedKey = new BtcECKey();
Expand Down Expand Up @@ -508,6 +556,7 @@ void onBestBlock_whenPegoutTxIsCached_shouldNotSignSamePegoutTxAgain() throws Ex
@Test
void onBestBlock_whenPegoutTxIsCachedWithInvalidTimestamp_shouldSignSamePegoutTxAgain() throws Exception {
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 @@ -523,6 +572,7 @@ void onBestBlock_whenPegoutTxIsCachedWithInvalidTimestamp_shouldSignSamePegoutTx

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

ECKey ecKey = new ECKey();
BtcECKey fedKey = new BtcECKey();
Expand Down Expand Up @@ -618,6 +668,7 @@ void onBestBlock_whenPegoutTxIsCachedWithInvalidTimestamp_shouldSignSamePegoutTx
void onBestBlock_return_when_node_is_syncing() throws BtcReleaseClientException {
// Arrange
Federation federation = TestUtils.createFederation(params, 1);
FederationMember federationMember = federation.getMembers().get(0);

Ethereum ethereum = mock(Ethereum.class);
AtomicReference<EthereumListener> ethereumListener = new AtomicReference<>();
Expand All @@ -628,6 +679,7 @@ void onBestBlock_return_when_node_is_syncing() throws BtcReleaseClientException
}).when(ethereum).addListener(ArgumentMatchers.any(EthereumListener.class));

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

PowpegNodeSystemProperties powpegNodeSystemProperties = mock(PowpegNodeSystemProperties.class);
doReturn(Constants.regtest()).when(powpegNodeSystemProperties).getNetworkConstants();
Expand Down Expand Up @@ -666,6 +718,7 @@ void onBestBlock_return_when_node_is_syncing() throws BtcReleaseClientException
void onBestBlock_return_when_pegout_is_disabled() throws BtcReleaseClientException {
// Arrange
Federation federation = TestUtils.createFederation(params, 1);
FederationMember federationMember = federation.getMembers().get(0);

Ethereum ethereum = mock(Ethereum.class);
AtomicReference<EthereumListener> ethereumListener = new AtomicReference<>();
Expand All @@ -676,6 +729,7 @@ void onBestBlock_return_when_pegout_is_disabled() throws BtcReleaseClientExcepti
}).when(ethereum).addListener(any(EthereumListener.class));

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

PowpegNodeSystemProperties powpegNodeSystemProperties = mock(PowpegNodeSystemProperties.class);
doReturn(Constants.regtest()).when(powpegNodeSystemProperties).getNetworkConstants();
Expand Down Expand Up @@ -714,6 +768,7 @@ void onBestBlock_return_when_pegout_is_disabled() throws BtcReleaseClientExcepti
void onBlock_return_when_node_is_syncing() {
// Arrange
Federation federation = TestUtils.createFederation(params, 1);
FederationMember federationMember = federation.getMembers().get(0);

Ethereum ethereum = mock(Ethereum.class);
AtomicReference<EthereumListener> ethereumListener = new AtomicReference<>();
Expand All @@ -724,6 +779,7 @@ void onBlock_return_when_node_is_syncing() {
}).when(ethereum).addListener(any(EthereumListener.class));

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

PowpegNodeSystemProperties powpegNodeSystemProperties = mock(PowpegNodeSystemProperties.class);
doReturn(Constants.regtest()).when(powpegNodeSystemProperties).getNetworkConstants();
Expand Down Expand Up @@ -758,6 +814,7 @@ void onBlock_return_when_node_is_syncing() {
void onBlock_return_when_pegout_is_disabled() {
// Arrange
Federation federation = TestUtils.createFederation(params, 1);
FederationMember federationMember = federation.getMembers().get(0);

Ethereum ethereum = mock(Ethereum.class);
AtomicReference<EthereumListener> ethereumListener = new AtomicReference<>();
Expand All @@ -768,6 +825,7 @@ void onBlock_return_when_pegout_is_disabled() {
}).when(ethereum).addListener(any(EthereumListener.class));

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

PowpegNodeSystemProperties powpegNodeSystemProperties = mock(PowpegNodeSystemProperties.class);
doReturn(Constants.regtest()).when(powpegNodeSystemProperties).getNetworkConstants();
Expand Down Expand Up @@ -862,6 +920,7 @@ void validateTxCanBeSigned_federatorAlreadySigned() throws Exception {
FederationArgs federationArgs = new FederationArgs(fedMembers, Instant.now(), 0, params);

Federation federation = FederationFactory.buildStandardMultiSigFederation(federationArgs);
FederationMember federationMember = federation.getMembers().get(0);

// Create a tx from the Fed to a random btc address
BtcTransaction releaseTx = new BtcTransaction(params);
Expand Down Expand Up @@ -891,13 +950,17 @@ void validateTxCanBeSigned_federatorAlreadySigned() throws Exception {
ECPublicKey signerPublicKey = new ECPublicKey(federator1PrivKey.getPubKey());
ECDSASigner signer = mock(ECDSASigner.class);
Mockito.doReturn(signerPublicKey).when(signer).getPublicKey(ArgumentMatchers.any(KeyId.class));

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

BtcReleaseClient client = new BtcReleaseClient(
mock(Ethereum.class),
mock(FederatorSupport.class),
federatorSupport,
powpegNodeSystemProperties,
mock(NodeBlockProcessor.class)
);

client.setup(
signer,
mock(ActivationConfig.class),
Expand All @@ -907,6 +970,7 @@ void validateTxCanBeSigned_federatorAlreadySigned() throws Exception {
mock(BtcReleaseClientStorageAccessor.class),
mock(BtcReleaseClientStorageSynchronizer.class)
);

client.start(federation);

// Act
Expand Down Expand Up @@ -1065,6 +1129,11 @@ private void test_validateTxCanBeSigned(
BtcTransaction releaseTx,
ECPublicKey signerPublicKey
) throws Exception {
FederationMember federationMember = federation.getMembers().get(0);

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

PowpegNodeSystemProperties powpegNodeSystemProperties = mock(PowpegNodeSystemProperties.class);
when(powpegNodeSystemProperties.getNetworkConstants()).thenReturn(Constants.regtest());
when(powpegNodeSystemProperties.getPegoutSignedCacheTtl())
Expand All @@ -1075,10 +1144,11 @@ private void test_validateTxCanBeSigned(

BtcReleaseClient client = new BtcReleaseClient(
mock(Ethereum.class),
mock(FederatorSupport.class),
federatorSupport,
powpegNodeSystemProperties,
mock(NodeBlockProcessor.class)
);

client.setup(
signer,
mock(ActivationConfig.class),
Expand All @@ -1088,6 +1158,7 @@ private void test_validateTxCanBeSigned(
mock(BtcReleaseClientStorageAccessor.class),
mock(BtcReleaseClientStorageSynchronizer.class)
);

client.start(federation);

// Act
Expand Down Expand Up @@ -1185,6 +1256,7 @@ private void testUsageOfStorageWhenSigning(boolean shouldHaveDataInFile)
BtcECKey key3 = new BtcECKey();
List<BtcECKey> keys = Arrays.asList(key1, key2, key3);
Federation federation = createFederation(keys);
FederationMember federationMember = federation.getMembers().get(0);

// Release info
Keccak256 rskTxHash = createHash(0);
Expand All @@ -1207,6 +1279,7 @@ private void testUsageOfStorageWhenSigning(boolean shouldHaveDataInFile)
when(federatorSupport.getStateForFederator()).thenReturn(
new StateForFederator(rskTxsWaitingForSignatures) // Only return the confirmed release
);
when(federatorSupport.getFederationMember()).thenReturn(federationMember);

ECDSASigner signer = mock(ECDSASigner.class);
when(signer.getVersionForKeyId(any())).thenReturn(2);
Expand Down

0 comments on commit 152218e

Please sign in to comment.