From 62961df994b65f5cfdfeeaa0aa58c26b7ced7575 Mon Sep 17 00:00:00 2001 From: Marcos Date: Wed, 28 Aug 2024 13:01:22 -0300 Subject: [PATCH] Rename variables in tests --- .../java/co/rsk/peg/BridgeSupportTest.java | 40 +-- .../FederationStorageProviderImplTests.java | 40 ++- .../peg/utils/BridgeEventLoggerImplTest.java | 233 +++++++++--------- 3 files changed, 160 insertions(+), 153 deletions(-) diff --git a/rskj-core/src/test/java/co/rsk/peg/BridgeSupportTest.java b/rskj-core/src/test/java/co/rsk/peg/BridgeSupportTest.java index 6080cffc2aa..f0645739160 100644 --- a/rskj-core/src/test/java/co/rsk/peg/BridgeSupportTest.java +++ b/rskj-core/src/test/java/co/rsk/peg/BridgeSupportTest.java @@ -140,7 +140,12 @@ void setUpOnEachTest() { signatureCache = new BlockTxSignatureCache(new ReceivedTxSignatureCache()); Repository repository = createRepository(); - bridgeStorageProvider = new BridgeStorageProvider(repository, contractAddress, btcMainnetParams, activationsAfterForks); + bridgeStorageProvider = new BridgeStorageProvider( + repository, + contractAddress, + btcMainnetParams, + activationsAfterForks + ); bridgeSupportBuilder = new BridgeSupportBuilder(); StorageAccessor bridgeStorageAccessor = new InMemoryStorage(); @@ -155,14 +160,23 @@ void setUpOnEachTest() { .withFederationStorageProvider(federationStorageProvider) .withActivations(activationsAfterForks) .build(); - whitelistSupport = new WhitelistSupportImpl(WhitelistMainNetConstants.getInstance(), whitelistStorageProvider, mock(ActivationConfig.ForBlock.class), signatureCache); - lockingCapSupport = new LockingCapSupportImpl(lockingCapStorageProvider, mock(ActivationConfig.ForBlock.class), LockingCapMainNetConstants.getInstance(), signatureCache); + whitelistSupport = new WhitelistSupportImpl( + WhitelistMainNetConstants.getInstance(), + whitelistStorageProvider, + activationsAfterForks, + signatureCache + ); + lockingCapSupport = new LockingCapSupportImpl( + lockingCapStorageProvider, + activationsAfterForks, + LockingCapMainNetConstants.getInstance(), + signatureCache + ); } @Test void getFeePerKb() { Coin feePerKb = Coin.valueOf(10_000L); - FeePerKbSupport feePerKbSupport = mock(FeePerKbSupport.class); when(feePerKbSupport.getFeePerKb()).thenReturn(feePerKb); BridgeSupport bridgeSupport = bridgeSupportBuilder @@ -176,7 +190,6 @@ void getFeePerKb() { @Test void voteFeePerKbChange_success() { - FeePerKbSupport feePerKbSupport = mock(FeePerKbSupport.class); when(feePerKbSupport.voteFeePerKbChange(any(), any(), any())).thenReturn(1); BridgeSupport bridgeSupport = bridgeSupportBuilder @@ -274,9 +287,6 @@ void setLockWhitelistDisableBlockDelay() throws BlockStoreException, IOException BridgeConstants bridgeConstantsMainNet = BridgeMainNetConstants.getInstance(); BtcBlockStoreWithCache.Factory btcBlockStoreFactory = mock(BtcBlockStoreWithCache.Factory.class); ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class); - FederationSupport federationSupport = federationSupportBuilder - .withFederationConstants(bridgeConstantsMainNet.getFederationConstants()) - .build(); bridgeSupport = bridgeSupportBuilder .withWhitelistSupport(whitelistSupport) @@ -529,7 +539,7 @@ void getPendingFederatorPublicKeyOfType() { } @Test - void voteFederationChange() throws BridgeIllegalArgumentException { + void voteFederationChange() { Transaction tx = mock(Transaction.class); ABICallSpec callSpec = mock(ABICallSpec.class); int result = 1; @@ -936,12 +946,12 @@ void isBtcTxHashAlreadyProcessed() throws IOException { Sha256Hash hash1 = Sha256Hash.ZERO_HASH; Sha256Hash hash2 = Sha256Hash.wrap("0000000000000000000000000000000000000000000000000000000000000001"); - BridgeStorageProvider bridgeStorageProvider = mock(BridgeStorageProvider.class); - when(bridgeStorageProvider.getHeightIfBtcTxhashIsAlreadyProcessed(hash1)).thenReturn(Optional.of(1L)); + BridgeStorageProvider bridgeStorageProviderMock = mock(BridgeStorageProvider.class); + when(bridgeStorageProviderMock.getHeightIfBtcTxhashIsAlreadyProcessed(hash1)).thenReturn(Optional.of(1L)); BridgeSupport bridgeSupport = bridgeSupportBuilder .withBridgeConstants(bridgeConstants) - .withProvider(bridgeStorageProvider) + .withProvider(bridgeStorageProviderMock) .withActivations(activations) .build(); @@ -957,12 +967,12 @@ void getBtcTxHashProcessedHeight() throws IOException { Sha256Hash hash1 = Sha256Hash.ZERO_HASH; Sha256Hash hash2 = Sha256Hash.wrap("0000000000000000000000000000000000000000000000000000000000000001"); - BridgeStorageProvider bridgeStorageProvider = mock(BridgeStorageProvider.class); - when(bridgeStorageProvider.getHeightIfBtcTxhashIsAlreadyProcessed(hash1)).thenReturn(Optional.of(1L)); + BridgeStorageProvider bridgeStorageProviderMock = mock(BridgeStorageProvider.class); + when(bridgeStorageProviderMock.getHeightIfBtcTxhashIsAlreadyProcessed(hash1)).thenReturn(Optional.of(1L)); BridgeSupport bridgeSupport = bridgeSupportBuilder .withBridgeConstants(bridgeConstants) - .withProvider(bridgeStorageProvider) + .withProvider(bridgeStorageProviderMock) .withActivations(activations) .build(); diff --git a/rskj-core/src/test/java/co/rsk/peg/federation/FederationStorageProviderImplTests.java b/rskj-core/src/test/java/co/rsk/peg/federation/FederationStorageProviderImplTests.java index 660b6aac66e..cc5165b0134 100644 --- a/rskj-core/src/test/java/co/rsk/peg/federation/FederationStorageProviderImplTests.java +++ b/rskj-core/src/test/java/co/rsk/peg/federation/FederationStorageProviderImplTests.java @@ -47,7 +47,6 @@ import co.rsk.bitcoinj.core.UTXO; class FederationStorageProviderImplTests { - private static final int STANDARD_MULTISIG_FEDERATION_FORMAT_VERSION = STANDARD_MULTISIG_FEDERATION.getFormatVersion(); private static final int NON_STANDARD_ERP_FEDERATION_FORMAT_VERSION = NON_STANDARD_ERP_FEDERATION.getFormatVersion(); private static final int P2SH_ERP_FEDERATION_FORMAT_VERSION = P2SH_ERP_FEDERATION.getFormatVersion(); @@ -56,11 +55,12 @@ class FederationStorageProviderImplTests { private static final BridgeConstants bridgeConstantsRegtest = new BridgeRegTestConstants(); private static final NetworkParameters regtestBtcParams = bridgeConstantsRegtest.getBtcParams(); - private static ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class); private static final BridgeConstants bridgeConstants = BridgeMainNetConstants.getInstance(); private static final FederationConstants federationConstants = bridgeConstants.getFederationConstants(); private static final NetworkParameters networkParameters = federationConstants.getBtcParams(); + private static ActivationConfig.ForBlock activations; + private static Stream provideFederationAndFormatArguments() { return Stream.of( Arguments.of(P2SH_ERP_FEDERATION_FORMAT_VERSION, new P2shErpFederationBuilder().build()), @@ -76,6 +76,11 @@ private static Stream provideFederationAndFormatArguments() { ); } + @BeforeEach + void setup() { + activations = mock(ActivationConfig.ForBlock.class); + } + @ParameterizedTest @MethodSource("provideFederationAndFormatArguments") void testGetNewFederation( @@ -158,8 +163,6 @@ void testGetPendingFederation( PendingFederation expectedFederation ) { // Arrange - ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class); - when(activations.isActive(ConsensusRule.RSKIP123)).thenReturn(true); StorageAccessor storageAccessor = new InMemoryStorage(); @@ -184,14 +187,10 @@ void testGetPendingFederation( } @Test - void getPendingFederation_whenStorageVersionIsNotAvailable_deserializeFromBtcKeysOnly( - ) { - + void getPendingFederation_whenStorageVersionIsNotAvailable_deserializeFromBtcKeysOnly() { PendingFederation expectedFederation = new PendingFederationBuilder().build(); // Arrange - ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class); - when(activations.isActive(ConsensusRule.RSKIP123)).thenReturn(false); StorageAccessor storageAccessor = new InMemoryStorage(); @@ -248,8 +247,6 @@ void getPendingFederation_previouslySetToNull_returnsNull() { @Test void getPendingFederation_nullPendingFederationInStorage_returnsNull() { // Arrange - ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class); - when(activations.isActive(ConsensusRule.RSKIP123)).thenReturn(false); StorageAccessor storageAccessor = new InMemoryStorage(); @@ -289,7 +286,6 @@ private static Stream provideSaveFederationTestArguments() { @Test void saveNewFederation_before_RSKIP123_should_not_save_null() { Repository repository = mock(Repository.class); - activations = mock(ActivationConfig.ForBlock.class); when(activations.isActive(ConsensusRule.RSKIP123)).thenReturn(false); // Act @@ -454,7 +450,7 @@ void getNewFederationBtcUTXOs( @Test void getNewFederationBtcUTXOs_calledTwice_returnCachedUtxos() { - ActivationConfig.ForBlock activations = ActivationConfigsForTest.only(ConsensusRule.RSKIP123).forBlock(0); + ActivationConfig.ForBlock wasabiActivations = ActivationConfigsForTest.wasabi100().forBlock(0); StorageAccessor storageAccessor = new InMemoryStorage(); FederationStorageProvider federationStorageProvider = new FederationStorageProviderImpl(storageAccessor); @@ -467,7 +463,7 @@ void getNewFederationBtcUTXOs_calledTwice_returnCachedUtxos() { storageAccessor.saveToRepository(newFederationBtcUtxosKey, expectedUtxos, BridgeSerializationUtils::serializeUTXOList); // Get utxos from method and check they are as expected - List actualUtxos = federationStorageProvider.getNewFederationBtcUTXOs(networkParameters, activations); + List actualUtxos = federationStorageProvider.getNewFederationBtcUTXOs(networkParameters, wasabiActivations); assertEquals(1, actualUtxos.size()); assertEquals(expectedUtxos, actualUtxos); @@ -476,7 +472,7 @@ void getNewFederationBtcUTXOs_calledTwice_returnCachedUtxos() { storageAccessor.saveToRepository(newFederationBtcUtxosKey, extraUtxos, BridgeSerializationUtils::serializeUTXOList); // Get utxos from method and check they are still the same as the original expected utxos since it is returning the cached utxos - List actualUtxosAfterSecondGet = federationStorageProvider.getNewFederationBtcUTXOs(null, activations); + List actualUtxosAfterSecondGet = federationStorageProvider.getNewFederationBtcUTXOs(null, wasabiActivations); assertEquals(1, actualUtxosAfterSecondGet.size()); assertEquals(expectedUtxos, actualUtxosAfterSecondGet); @@ -616,7 +612,7 @@ void getFederationElection_whenElectionIsNotInStorage_shouldReturnDefault() { @Test void getNextFederationCreationBlockHeight_preIris300_storageIsNotAccessedAndReturnsEmpty() { // Arrange - ActivationConfig.ForBlock activations = ActivationConfigsForTest.papyrus200().forBlock(0L); + ActivationConfig.ForBlock papyrusActivations = ActivationConfigsForTest.papyrus200().forBlock(0L); StorageAccessor storageAccessor = new InMemoryStorage(); // Putting some value in the storage just to then assert that before fork, the storage won't be accessed. @@ -629,13 +625,13 @@ void getNextFederationCreationBlockHeight_preIris300_storageIsNotAccessedAndRetu FederationStorageProvider federationStorageProvider = new FederationStorageProviderImpl(storageAccessor); // Assert - assertEquals(Optional.empty(), federationStorageProvider.getNextFederationCreationBlockHeight(activations)); + assertEquals(Optional.empty(), federationStorageProvider.getNextFederationCreationBlockHeight(papyrusActivations)); } @Test void getNextFederationCreationBlockHeight_postIris300_getsValueFromStorage() { // Arrange - ActivationConfig.ForBlock activations = ActivationConfigsForTest.iris300().forBlock(0L); + ActivationConfig.ForBlock irisActivations = ActivationConfigsForTest.iris300().forBlock(0L); StorageAccessor storageAccessor = new InMemoryStorage(); long expectedValue = 1_000_000L; @@ -644,7 +640,7 @@ void getNextFederationCreationBlockHeight_postIris300_getsValueFromStorage() { // Act FederationStorageProvider federationStorageProvider = new FederationStorageProviderImpl(storageAccessor); - Optional actualValue = federationStorageProvider.getNextFederationCreationBlockHeight(activations); + Optional actualValue = federationStorageProvider.getNextFederationCreationBlockHeight(irisActivations); // Assert assertTrue(actualValue.isPresent()); @@ -653,7 +649,7 @@ void getNextFederationCreationBlockHeight_postIris300_getsValueFromStorage() { // Setting in storage a different value to assert that calling the method again should return cached value storageAccessor.saveToRepository(NEXT_FEDERATION_CREATION_BLOCK_HEIGHT_KEY.getKey(), RLP.encodeBigInteger(BigInteger.valueOf(2_000_000L))); - Optional actualCachedValue = federationStorageProvider.getNextFederationCreationBlockHeight(activations); + Optional actualCachedValue = federationStorageProvider.getNextFederationCreationBlockHeight(irisActivations); assertTrue(actualCachedValue.isPresent()); assertEquals(expectedValue, actualCachedValue.get()); @@ -662,14 +658,14 @@ void getNextFederationCreationBlockHeight_postIris300_getsValueFromStorage() { @Test void getNextFederationCreationBlockHeight_postIris300AndNoValueInStorage_returnsEmpty() { // Arrange - ActivationConfig.ForBlock activations = ActivationConfigsForTest.iris300().forBlock(0L); + ActivationConfig.ForBlock irisActivations = ActivationConfigsForTest.iris300().forBlock(0L); StorageAccessor storageAccessor = new InMemoryStorage(); storageAccessor.saveToRepository(NEXT_FEDERATION_CREATION_BLOCK_HEIGHT_KEY.getKey(), null); // Act FederationStorageProvider federationStorageProvider = new FederationStorageProviderImpl(storageAccessor); - Optional actualValue = federationStorageProvider.getNextFederationCreationBlockHeight(activations); + Optional actualValue = federationStorageProvider.getNextFederationCreationBlockHeight(irisActivations); // Assert assertFalse(actualValue.isPresent()); diff --git a/rskj-core/src/test/java/co/rsk/peg/utils/BridgeEventLoggerImplTest.java b/rskj-core/src/test/java/co/rsk/peg/utils/BridgeEventLoggerImplTest.java index c8ba335c0dd..3931e6f3edc 100644 --- a/rskj-core/src/test/java/co/rsk/peg/utils/BridgeEventLoggerImplTest.java +++ b/rskj-core/src/test/java/co/rsk/peg/utils/BridgeEventLoggerImplTest.java @@ -17,68 +17,51 @@ */ package co.rsk.peg.utils; +import static co.rsk.peg.bitcoin.BitcoinTestUtils.coinListOf; +import static co.rsk.peg.bitcoin.BitcoinTestUtils.flatKeysAsByteArray; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + import co.rsk.bitcoinj.core.*; import co.rsk.bitcoinj.script.ScriptBuilder; -import co.rsk.peg.constants.BridgeMainNetConstants; -import co.rsk.peg.constants.BridgeRegTestConstants; import co.rsk.core.RskAddress; import co.rsk.crypto.Keccak256; -import co.rsk.peg.*; -import co.rsk.peg.bitcoin.BitcoinTestUtils; -import co.rsk.peg.bitcoin.InvalidOutpointValueException; -import co.rsk.peg.bitcoin.UtxoUtils; -import co.rsk.peg.federation.*; +import co.rsk.peg.BridgeEvents; import co.rsk.peg.PegTestUtils; +import co.rsk.peg.bitcoin.*; +import co.rsk.peg.constants.BridgeConstants; +import co.rsk.peg.constants.BridgeMainNetConstants; +import co.rsk.peg.federation.*; import co.rsk.peg.federation.constants.FederationConstants; import co.rsk.peg.pegin.RejectedPeginReason; -import java.util.ArrayList; -import java.util.Collections; +import java.math.BigInteger; +import java.time.Instant; +import java.util.*; +import java.util.stream.Collectors; +import java.util.stream.Stream; import org.bouncycastle.util.encoders.Hex; -import org.ethereum.config.blockchain.upgrades.ActivationConfig; -import org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest; -import org.ethereum.config.blockchain.upgrades.ConsensusRule; +import org.ethereum.config.blockchain.upgrades.*; import org.ethereum.core.*; import org.ethereum.core.CallTransaction.Function; import org.ethereum.crypto.ECKey; import org.ethereum.vm.LogInfo; import org.ethereum.vm.PrecompiledContracts; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.*; import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.Arguments; -import org.junit.jupiter.params.provider.MethodSource; -import org.junit.jupiter.params.provider.ValueSource; - -import java.math.BigInteger; -import java.time.Instant; -import java.util.Arrays; -import java.util.LinkedList; -import java.util.List; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -import static co.rsk.peg.bitcoin.BitcoinTestUtils.coinListOf; -import static co.rsk.peg.bitcoin.BitcoinTestUtils.flatKeysAsByteArray; -import static org.junit.jupiter.api.Assertions.assertArrayEquals; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; +import org.junit.jupiter.params.provider.*; /** * Test class for BridgeEventLoggerImpl. * * @author martin.medina */ - class BridgeEventLoggerImplTest { - - private static final BridgeRegTestConstants bridgeRegtestConstants = new BridgeRegTestConstants(); - private static final FederationConstants federationRegtestConstants = bridgeRegtestConstants.getFederationConstants(); - private static final NetworkParameters regtestParameters = bridgeRegtestConstants.getBtcParams(); + private static final BridgeConstants BRIDGE_CONSTANTS = BridgeMainNetConstants.getInstance(); + private static final FederationConstants FEDERATION_CONSTANTS = BRIDGE_CONSTANTS.getFederationConstants(); + private static final NetworkParameters NETWORK_PARAMETERS = BRIDGE_CONSTANTS.getBtcParams(); + private static final RskAddress RSK_ADDRESS = new RskAddress("0x0000000000000000000000000000000000000000"); private final ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class); private List eventLogs; @@ -91,9 +74,9 @@ class BridgeEventLoggerImplTest { void setup() { signatureCache = new BlockTxSignatureCache(new ReceivedTxSignatureCache()); eventLogs = new LinkedList<>(); - eventLogger = new BridgeEventLoggerImpl(bridgeRegtestConstants, activations, eventLogs, signatureCache); + eventLogger = new BridgeEventLoggerImpl(BRIDGE_CONSTANTS, activations, eventLogs, signatureCache); btcTxMock = mock(BtcTransaction.class); - btcTx = new BtcTransaction(regtestParameters); + btcTx = new BtcTransaction(NETWORK_PARAMETERS); } @Test @@ -101,96 +84,103 @@ void logLockBtc() { Address senderAddress = mock(Address.class); when(senderAddress.toString()).thenReturn("mixzLp4xx5bUsHuYUEyPpL42BzEDp8kSTv"); - RskAddress rskAddress = mock(RskAddress.class); - when(rskAddress.toString()).thenReturn("0x00000000000000000000000000000000000000"); - // Mock btc transaction - when(btcTxMock.getHash()).thenReturn(PegTestUtils.createHash(0)); + when(btcTxMock.getHash()).thenReturn(BitcoinTestUtils.createHash(0)); Coin amount = Coin.SATOSHI; // Act - eventLogger.logLockBtc(rskAddress, btcTxMock, senderAddress, amount); + eventLogger.logLockBtc(RSK_ADDRESS, btcTxMock, senderAddress, amount); commonAssertLogs(eventLogs); assertTopics(2, eventLogs); - assertEvent(eventLogs, 0, BridgeEvents.LOCK_BTC.getEvent(), new Object[]{rskAddress.toString()}, new Object[]{btcTxMock.getHash().getBytes(), senderAddress.toString(), amount.getValue()}); + assertEvent( + eventLogs, + 0, + BridgeEvents.LOCK_BTC.getEvent(), + new Object[]{RSK_ADDRESS.toString()}, + new Object[]{btcTxMock.getHash().getBytes(), senderAddress.toString(), amount.getValue()} + ); } @Test void logLockBtc_with_segwit_address() { - Address senderAddress = new Address(regtestParameters, regtestParameters.getP2SHHeader(), Hex.decode("c99a8f22127007255b4a9d8d57b0892ae2103f2d")); - - RskAddress rskAddress = mock(RskAddress.class); - when(rskAddress.toString()).thenReturn("0x00000000000000000000000000000000000000"); + Address senderAddress = new Address( + NETWORK_PARAMETERS, + NETWORK_PARAMETERS.getP2SHHeader(), + Hex.decode("c99a8f22127007255b4a9d8d57b0892ae2103f2d") + ); // Mock btc transaction - when(btcTxMock.getHash()).thenReturn(PegTestUtils.createHash(0)); + when(btcTxMock.getHash()).thenReturn(BitcoinTestUtils.createHash(0)); Coin amount = Coin.SATOSHI; // Act - eventLogger.logLockBtc(rskAddress, btcTxMock, senderAddress, amount); + eventLogger.logLockBtc(RSK_ADDRESS, btcTxMock, senderAddress, amount); commonAssertLogs(eventLogs); assertTopics(2, eventLogs); - assertEvent(eventLogs, 0, BridgeEvents.LOCK_BTC.getEvent(), new Object[]{rskAddress.toString()}, new Object[]{btcTxMock.getHash().getBytes(), "2NBdCxoCY6wx1NHpwGWfJThHk9K2tVdNx1A", amount.getValue()}); + assertEvent( + eventLogs, + 0, + BridgeEvents.LOCK_BTC.getEvent(), + new Object[]{RSK_ADDRESS.toString()}, + new Object[]{btcTxMock.getHash().getBytes(), "3L4zu4GWVVSfAWCPbP3RqkJUvxpiiQebPX", amount.getValue()} + ); } @Test void logPeginBtc() { - // Setup event logger - List eventLogs = new LinkedList<>(); - BridgeEventLogger eventLogger = new BridgeEventLoggerImpl(null, activations, eventLogs, signatureCache); - - RskAddress rskAddress = mock(RskAddress.class); - when(rskAddress.toString()).thenReturn("0x00000000000000000000000000000000000000"); - // Mock btc transaction BtcTransaction mockedTx = mock(BtcTransaction.class); - when(mockedTx.getHash()).thenReturn(PegTestUtils.createHash(0)); + when(mockedTx.getHash()).thenReturn(BitcoinTestUtils.createHash(0)); Coin amount = Coin.SATOSHI; int protocolVersion = 1; // Act - eventLogger.logPeginBtc(rskAddress, mockedTx, amount, protocolVersion); + eventLogger.logPeginBtc(RSK_ADDRESS, mockedTx, amount, protocolVersion); // Assert log size - Assertions.assertEquals(1, eventLogs.size()); + assertEquals(1, eventLogs.size()); LogInfo logResult = eventLogs.get(0); CallTransaction.Function event = BridgeEvents.PEGIN_BTC.getEvent(); // Assert address that made the log - Assertions.assertEquals(PrecompiledContracts.BRIDGE_ADDR, new RskAddress(logResult.getAddress())); + assertEquals(PrecompiledContracts.BRIDGE_ADDR, new RskAddress(logResult.getAddress())); // Assert log topics - Assertions.assertEquals(3, logResult.getTopics().size()); - byte[][] topics = event.encodeEventTopics(rskAddress.toString(), mockedTx.getHash().getBytes()); + assertEquals(3, logResult.getTopics().size()); + byte[][] topics = event.encodeEventTopics(RSK_ADDRESS.toString(), mockedTx.getHash().getBytes()); for (int i=0; i logAddSignatureArgProvider() { @@ -228,21 +218,15 @@ private static Stream logAddSignatureArgProvider() { @MethodSource("logAddSignatureArgProvider") void logAddSignature(ActivationConfig.ForBlock activations, FederationMember federationMember, String expectedRskAddress) { // Arrange - BridgeMainNetConstants bridgeMainNetConstants = BridgeMainNetConstants.getInstance(); - BlockTxSignatureCache signatureCache = new BlockTxSignatureCache(new ReceivedTxSignatureCache()); - LinkedList eventLogs = new LinkedList<>(); - BridgeEventLogger eventLogger = new BridgeEventLoggerImpl(bridgeMainNetConstants, activations, eventLogs, signatureCache); - + BridgeEventLogger bridgeEventLogger = new BridgeEventLoggerImpl(BRIDGE_CONSTANTS, activations, eventLogs, signatureCache); BtcECKey federatorBtcPubKey = federationMember.getBtcPublicKey(); - Keccak256 rskTxHash = PegTestUtils.createHash3(1); - BtcTransaction btcTxMock = mock(BtcTransaction.class); - when(btcTxMock.getHash()).thenReturn(BitcoinTestUtils.createHash(1)); - when(btcTxMock.getHashAsString()).thenReturn(rskTxHash.toHexString()); + BtcTransaction pegoutTx = mock(BtcTransaction.class); + when(pegoutTx.getHash()).thenReturn(BitcoinTestUtils.createHash(1)); // Act - eventLogger.logAddSignature(federationMember, btcTxMock, rskTxHash.getBytes()); + bridgeEventLogger.logAddSignature(federationMember, pegoutTx, rskTxHash.getBytes()); // Assert commonAssertLogs(eventLogs); @@ -272,7 +256,9 @@ void logReleaseBtc() { @ValueSource(booleans = {false, true}) void logCommitFederation(boolean isRSKIP383Active) { when(activations.isActive(ConsensusRule.RSKIP383)).thenReturn(isRSKIP383Active); + // Setup parameters for test method call + Instant creationTime = Instant.ofEpochMilli(15005L); Block executionBlock = mock(Block.class); when(executionBlock.getTimestamp()).thenReturn(15005L); when(executionBlock.getNumber()).thenReturn(15L); @@ -285,8 +271,8 @@ void logCommitFederation(boolean isRSKIP383Active) { ); List oldFederationMembers = FederationTestUtils.getFederationMembersWithBtcKeys(oldFederationKeys); - NetworkParameters btcParams = NetworkParameters.fromID(NetworkParameters.ID_REGTEST); - FederationArgs oldFedArgs = new FederationArgs(oldFederationMembers, Instant.ofEpochMilli(15005L), 15L, btcParams); + NetworkParameters btcParams = BRIDGE_CONSTANTS.getBtcParams(); + FederationArgs oldFedArgs = new FederationArgs(oldFederationMembers, creationTime, 15L, btcParams); Federation oldFederation = FederationFactory.buildStandardMultiSigFederation(oldFedArgs); @@ -296,7 +282,7 @@ void logCommitFederation(boolean isRSKIP383Active) { BtcECKey.fromPublicOnly(Hex.decode("026192d8ab41bd402eb0431457f6756a3f3ce15c955c534d2b87f1e0372d8ba338")) ); List newFederationMembers = FederationTestUtils.getFederationMembersWithBtcKeys(newFederationKeys); - FederationArgs newFedArgs = new FederationArgs(newFederationMembers, Instant.ofEpochMilli(15005L), 15L, btcParams); + FederationArgs newFedArgs = new FederationArgs(newFederationMembers, creationTime, 15L, btcParams); Federation newFederation = FederationFactory.buildStandardMultiSigFederation(newFedArgs); @@ -311,7 +297,7 @@ void logCommitFederation(boolean isRSKIP383Active) { String oldFederationBtcAddress = oldFederation.getAddress().toBase58(); byte[] newFederationFlatPubKeys = flatKeysAsByteArray(newFederation.getBtcPublicKeys()); String newFederationBtcAddress = newFederation.getAddress().toBase58(); - long newFedActivationBlockNumber = executionBlock.getNumber() + federationRegtestConstants.getFederationActivationAge(activations); + long newFedActivationBlockNumber = executionBlock.getNumber() + FEDERATION_CONSTANTS.getFederationActivationAge(activations); Object[] data = new Object[]{ oldFederationFlatPubKeys, @@ -336,14 +322,14 @@ void logCommitFederation(boolean isRSKIP383Active) { ActivationConfig.ForBlock activationsForLegacyFedActivationAge = mock(ActivationConfig.ForBlock.class); when(activationsForLegacyFedActivationAge.isActive(ConsensusRule.RSKIP383)).thenReturn(false); - long legacyFedActivationBlockNumber = executionBlock.getNumber() + federationRegtestConstants.getFederationActivationAge(activationsForLegacyFedActivationAge); + long legacyFedActivationBlockNumber = executionBlock.getNumber() + FEDERATION_CONSTANTS.getFederationActivationAge(activationsForLegacyFedActivationAge); assertNotEquals(loggedFedActivationBlockNumber, legacyFedActivationBlockNumber); } else { ActivationConfig.ForBlock activationsForDefaultFedActivationAge = mock(ActivationConfig.ForBlock.class); when(activationsForDefaultFedActivationAge.isActive(ConsensusRule.RSKIP383)).thenReturn(true); - long defaultFedActivationBlockNumber = executionBlock.getNumber() + federationRegtestConstants.getFederationActivationAge(activationsForDefaultFedActivationAge); + long defaultFedActivationBlockNumber = executionBlock.getNumber() + FEDERATION_CONSTANTS.getFederationActivationAge(activationsForDefaultFedActivationAge); assertNotEquals(loggedFedActivationBlockNumber, defaultFedActivationBlockNumber); } } @@ -363,13 +349,6 @@ void logReleaseBtcRequested() { @Test void logRejectedPegin() { - // Setup event logger - List eventLogs = new LinkedList<>(); - - BridgeEventLogger eventLogger = new BridgeEventLoggerImpl(null, activations, eventLogs, signatureCache); - - BtcTransaction btcTx = new BtcTransaction(regtestParameters); - eventLogger.logRejectedPegin(btcTx, RejectedPeginReason.PEGIN_CAP_SURPASSED); Assertions.assertEquals(1, eventLogs.size()); @@ -398,35 +377,32 @@ void logRejectedPegin() { @Test void logUnrefundablePegin() { // Setup event logger - List eventLogs = new LinkedList<>(); - - BridgeEventLogger eventLogger = new BridgeEventLoggerImpl(null, activations, eventLogs, signatureCache); - - BtcTransaction btcTx = new BtcTransaction(regtestParameters); - eventLogger.logUnrefundablePegin(btcTx, UnrefundablePeginReason.LEGACY_PEGIN_UNDETERMINED_SENDER); - Assertions.assertEquals(1, eventLogs.size()); + assertEquals(1, eventLogs.size()); LogInfo entry = eventLogs.get(0); - Assertions.assertEquals(PrecompiledContracts.BRIDGE_ADDR, new RskAddress(entry.getAddress())); + assertEquals(PrecompiledContracts.BRIDGE_ADDR, new RskAddress(entry.getAddress())); // Assert address that made the log LogInfo result = eventLogs.get(0); - Assertions.assertArrayEquals(PrecompiledContracts.BRIDGE_ADDR.getBytes(), result.getAddress()); + assertArrayEquals(PrecompiledContracts.BRIDGE_ADDR.getBytes(), result.getAddress()); // Assert log topics - Assertions.assertEquals(2, result.getTopics().size()); + assertEquals(2, result.getTopics().size()); CallTransaction.Function event = BridgeEvents.UNREFUNDABLE_PEGIN.getEvent(); byte[][] topics = event.encodeEventTopics(btcTx.getHash().getBytes()); for (int i=0; i