From c196b1d75476fb0c24ba8158963bd4cd11ff987f Mon Sep 17 00:00:00 2001 From: Julian Len Date: Fri, 13 Dec 2024 12:57:22 -0300 Subject: [PATCH 01/14] added a test when a tx is already processed do not modify the state --- .../co/rsk/peg/RegisterBtcTransactionIT.java | 100 ++++++++++++------ 1 file changed, 69 insertions(+), 31 deletions(-) diff --git a/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java b/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java index 0bbf9231ec..f1a724043a 100644 --- a/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java +++ b/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java @@ -26,7 +26,9 @@ import org.ethereum.config.blockchain.upgrades.ActivationConfig; import org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest; import org.ethereum.core.*; +import org.ethereum.crypto.ECKey; import org.ethereum.vm.PrecompiledContracts; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.util.*; @@ -35,66 +37,102 @@ class RegisterBtcTransactionIT { private final BridgeConstants bridgeConstants = BridgeMainNetConstants.getInstance(); private final NetworkParameters btcParams = bridgeConstants.getBtcParams(); private final BridgeSupportBuilder bridgeSupportBuilder = BridgeSupportBuilder.builder(); + private Repository track; + private Repository repository; + private Block rskExecutionBlock; + private FederationSupport federationSupport; + private BridgeStorageProvider bridgeStorageProvider; + private BtcTransaction bitcoinTransaction; + private PartialMerkleTree pmtWithTransactions; + private int btcBlockWithPmtHeight; + private Transaction rskTx; + private ECKey ecKey; + private RskAddress rskReceiver; + private BridgeSupport bridgeSupport; + private BridgeEventLoggerImpl bridgeEventLogger; + private Coin btcTransferred; + + @BeforeEach + void setUp() throws Exception{ + rskTx = TransactionUtils.createTransaction(); + repository = BridgeSupportTestUtil.createRepository(); + track = repository.startTracking(); - @Test - void whenRegisterALegacyBtcTransaction_shouldRegisterTheNewUtxoAndTransferTheRbtcBalance() throws Exception { - // Arrange ActivationConfig.ForBlock activations = ActivationConfigsForTest.all().forBlock(0); - Repository repository = BridgeSupportTestUtil.createRepository(); - Repository track = repository.startTracking(); - Block rskExecutionBlock = getRskExecutionBlock(); + rskExecutionBlock = getRskExecutionBlock(); BtcLockSenderProvider btcLockSenderProvider = new BtcLockSenderProvider(); FeePerKbSupport feePerKbSupport = getFeePerKbSupport(repository, bridgeConstants); Federation federation = P2shErpFederationBuilder.builder().build(); FederationStorageProvider federationStorageProvider = getFederationStorageProvider(track, federation); - FederationSupport federationSupport = getFederationSupport(federationStorageProvider, activations, bridgeConstants.getFederationConstants()); - - BtcECKey btcPublicKey = BitcoinTestUtils.getBtcEcKeyFromSeed("seed"); - Coin btcTransferred = bridgeConstants.getMinimumPeginTxValue(activations); - BtcTransaction bitcoinTransaction = createPegInTransaction(federationSupport.getActiveFederation().getAddress(), btcTransferred, btcPublicKey); - TransactionOutput output = bitcoinTransaction.getOutput(0); - List expectedFederationUtxos = Collections.singletonList(utxoOf(bitcoinTransaction, output)); + federationSupport = getFederationSupport(federationStorageProvider, activations, bridgeConstants.getFederationConstants()); - BridgeStorageProvider bridgeStorageProvider = new BridgeStorageProvider(track, PrecompiledContracts.BRIDGE_ADDR, bridgeConstants.getBtcParams(), activations); + bridgeStorageProvider = new BridgeStorageProvider(track, PrecompiledContracts.BRIDGE_ADDR, bridgeConstants.getBtcParams(), activations); BtcBlockStoreWithCache.Factory btcBlockStoreFactory = new RepositoryBtcBlockStoreWithCache.Factory(bridgeConstants.getBtcParams(), 100, 100); BtcBlockStoreWithCache btcBlockStoreWithCache = btcBlockStoreFactory.newInstance(track, bridgeConstants, bridgeStorageProvider, activations); - PartialMerkleTree pmtWithTransactions = createValidPmtForTransactions(Collections.singletonList(bitcoinTransaction.getHash()), bridgeConstants.getBtcParams()); - int btcBlockWithPmtHeight = bridgeConstants.getBtcHeightWhenPegoutTxIndexActivates() + bridgeConstants.getPegoutTxIndexGracePeriodInBtcBlocks(); + BtcECKey btcPublicKey = BitcoinTestUtils.getBtcEcKeyFromSeed("seed"); + ecKey = ECKey.fromPublicOnly(btcPublicKey.getPubKey()); + rskReceiver = new RskAddress(ecKey.getAddress()); + btcTransferred = bridgeConstants.getMinimumPeginTxValue(activations); + bitcoinTransaction = createPegInTransaction(federationSupport.getActiveFederation().getAddress(), btcTransferred, btcPublicKey); + + pmtWithTransactions = createValidPmtForTransactions(Collections.singletonList(bitcoinTransaction.getHash()), bridgeConstants.getBtcParams()); + btcBlockWithPmtHeight = bridgeConstants.getBtcHeightWhenPegoutTxIndexActivates() + bridgeConstants.getPegoutTxIndexGracePeriodInBtcBlocks(); int chainHeight = btcBlockWithPmtHeight + bridgeConstants.getBtc2RskMinimumAcceptableConfirmations(); - BridgeEventLoggerImpl bridgeEventLogger = spy(new BridgeEventLoggerImpl(bridgeConstants, activations, new ArrayList<>())); + bridgeEventLogger = spy(new BridgeEventLoggerImpl(bridgeConstants, activations, new ArrayList<>())); recreateChainFromPmt(btcBlockStoreWithCache, chainHeight, pmtWithTransactions, btcBlockWithPmtHeight, bridgeConstants.getBtcParams()); - bridgeStorageProvider.save(); + bridgeSupport = getBridgeSupport(bridgeEventLogger, bridgeStorageProvider, activations, federationSupport, feePerKbSupport, rskExecutionBlock, btcBlockStoreFactory, track, btcLockSenderProvider); + } - BridgeSupport bridgeSupport = getBridgeSupport(bridgeEventLogger, bridgeStorageProvider, activations, federationSupport, feePerKbSupport, rskExecutionBlock, btcBlockStoreFactory, track, btcLockSenderProvider); - - Transaction rskTx = TransactionUtils.createTransaction(); - org.ethereum.crypto.ECKey key = org.ethereum.crypto.ECKey.fromPublicOnly(btcPublicKey.getPubKey()); + @Test + void whenRegisterALegacyBtcTransaction_shouldRegisterTheNewUtxoAndTransferTheRbtcBalance() throws Exception { + // Arrange + TransactionOutput output = bitcoinTransaction.getOutput(0); + List expectedFederationUtxos = Collections.singletonList(utxoOf(bitcoinTransaction, output)); - RskAddress receiver = new RskAddress(key.getAddress()); - co.rsk.core.Coin receiverBalance = track.getBalance(receiver); + co.rsk.core.Coin receiverBalance = track.getBalance(rskReceiver); co.rsk.core.Coin expectedReceiverBalance = receiverBalance.add(co.rsk.core.Coin.fromBitcoin(btcTransferred)); // Act - bridgeSupport.registerBtcTransaction(rskTx, bitcoinTransaction.bitcoinSerialize(), btcBlockWithPmtHeight, pmtWithTransactions.bitcoinSerialize()); - - bridgeSupport.save(); - track.commit(); + registerBtcTransactionAndCommit(); // Assert Optional heightIfBtcTxHashIsAlreadyProcessed = bridgeStorageProvider.getHeightIfBtcTxhashIsAlreadyProcessed(bitcoinTransaction.getHash()); - assertTrue(heightIfBtcTxHashIsAlreadyProcessed.isPresent()); assertEquals(rskExecutionBlock.getNumber(), heightIfBtcTxHashIsAlreadyProcessed.get()); assertEquals(expectedFederationUtxos, federationSupport.getActiveFederationBtcUTXOs()); - assertEquals(expectedReceiverBalance, repository.getBalance(receiver)); + assertEquals(expectedReceiverBalance, repository.getBalance(rskReceiver)); + + verify(bridgeEventLogger, times(1)).logPeginBtc(rskReceiver, bitcoinTransaction, btcTransferred, 0); + + } + + @Test + void whenRegisterARepeatedLegacyBtcTransaction_shouldNotPerformAnyChange() throws Exception { + // Arrange + registerBtcTransactionAndCommit(); - verify(bridgeEventLogger, times(1)).logPeginBtc(receiver, bitcoinTransaction, btcTransferred, 0); + RskAddress receiverBalance = new RskAddress(ecKey.getAddress()); + co.rsk.core.Coin expectedReceiverBalance = track.getBalance(receiverBalance); + List expectedFederationUTXOs = federationSupport.getActiveFederationBtcUTXOs(); + + // Act + registerBtcTransactionAndCommit(); + + // Assert + assertEquals(expectedFederationUTXOs, federationSupport.getActiveFederationBtcUTXOs()); + assertEquals(expectedReceiverBalance, repository.getBalance(receiverBalance)); + } + + private void registerBtcTransactionAndCommit() throws Exception { + bridgeSupport.registerBtcTransaction(rskTx, bitcoinTransaction.bitcoinSerialize(), btcBlockWithPmtHeight, pmtWithTransactions.bitcoinSerialize()); + bridgeSupport.save(); + track.commit(); } private static UTXO utxoOf(BtcTransaction bitcoinTransaction, TransactionOutput output) { From f304f4b041fa26780a151728610a1068c34fce83 Mon Sep 17 00:00:00 2001 From: Julian Len Date: Fri, 13 Dec 2024 13:09:04 -0300 Subject: [PATCH 02/14] fix: added space to separate Constants from the rest --- rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java | 1 - 1 file changed, 1 deletion(-) diff --git a/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java b/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java index f1a724043a..1ef22b2536 100644 --- a/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java +++ b/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java @@ -33,7 +33,6 @@ import java.util.*; class RegisterBtcTransactionIT { - private final BridgeConstants bridgeConstants = BridgeMainNetConstants.getInstance(); private final NetworkParameters btcParams = bridgeConstants.getBtcParams(); private final BridgeSupportBuilder bridgeSupportBuilder = BridgeSupportBuilder.builder(); From 574bc8898a0d4ac71acae6dfe3402f201cc045ad Mon Sep 17 00:00:00 2001 From: Julian Len Date: Fri, 13 Dec 2024 13:11:46 -0300 Subject: [PATCH 03/14] fix: no private methods for builders --- .../co/rsk/peg/RegisterBtcTransactionIT.java | 43 ++++++++----------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java b/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java index 1ef22b2536..81998ab39c 100644 --- a/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java +++ b/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java @@ -65,7 +65,12 @@ void setUp() throws Exception{ Federation federation = P2shErpFederationBuilder.builder().build(); FederationStorageProvider federationStorageProvider = getFederationStorageProvider(track, federation); - federationSupport = getFederationSupport(federationStorageProvider, activations, bridgeConstants.getFederationConstants()); + FederationConstants federationConstants = bridgeConstants.getFederationConstants(); + federationSupport = FederationSupportBuilder.builder() + .withFederationConstants(federationConstants) + .withFederationStorageProvider(federationStorageProvider) + .withActivations(activations) + .build(); bridgeStorageProvider = new BridgeStorageProvider(track, PrecompiledContracts.BRIDGE_ADDR, bridgeConstants.getBtcParams(), activations); BtcBlockStoreWithCache.Factory btcBlockStoreFactory = new RepositoryBtcBlockStoreWithCache.Factory(bridgeConstants.getBtcParams(), 100, 100); @@ -85,7 +90,18 @@ void setUp() throws Exception{ recreateChainFromPmt(btcBlockStoreWithCache, chainHeight, pmtWithTransactions, btcBlockWithPmtHeight, bridgeConstants.getBtcParams()); bridgeStorageProvider.save(); - bridgeSupport = getBridgeSupport(bridgeEventLogger, bridgeStorageProvider, activations, federationSupport, feePerKbSupport, rskExecutionBlock, btcBlockStoreFactory, track, btcLockSenderProvider); + bridgeSupport = bridgeSupportBuilder + .withBridgeConstants(bridgeConstants) + .withProvider(bridgeStorageProvider) + .withActivations(activations) + .withEventLogger(bridgeEventLogger) + .withFederationSupport(federationSupport) + .withFeePerKbSupport(feePerKbSupport) + .withExecutionBlock(rskExecutionBlock) + .withBtcBlockStoreFactory(btcBlockStoreFactory) + .withRepository(track) + .withBtcLockSenderProvider(btcLockSenderProvider) + .build(); } @Test @@ -145,14 +161,6 @@ private static UTXO utxoOf(BtcTransaction bitcoinTransaction, TransactionOutput ); } - private static FederationSupport getFederationSupport(FederationStorageProvider federationStorageProvider, ActivationConfig.ForBlock activationConfig, FederationConstants federationConstants) { - return FederationSupportBuilder.builder() - .withFederationConstants(federationConstants) - .withFederationStorageProvider(federationStorageProvider) - .withActivations(activationConfig) - .build(); - } - private FederationStorageProvider getFederationStorageProvider(Repository track, Federation federation) { FederationStorageProvider federationStorageProvider = createFederationStorageProvider(track); federationStorageProvider.setNewFederation(federation); @@ -168,21 +176,6 @@ private static FeePerKbSupport getFeePerKbSupport(Repository repository, BridgeC ); } - private BridgeSupport getBridgeSupport(BridgeEventLoggerImpl bridgeEventLogger, BridgeStorageProvider bridgeStorageProvider, ActivationConfig.ForBlock activationsBeforeForks, FederationSupport federationSupport, FeePerKbSupport feePerKbSupport, Block rskExecutionBlock, BtcBlockStoreWithCache.Factory btcBlockStoreFactory, Repository repository, BtcLockSenderProvider btcLockSenderProvider) { - return bridgeSupportBuilder - .withBridgeConstants(bridgeConstants) - .withProvider(bridgeStorageProvider) - .withEventLogger(bridgeEventLogger) - .withActivations(activationsBeforeForks) - .withFederationSupport(federationSupport) - .withFeePerKbSupport(feePerKbSupport) - .withExecutionBlock(rskExecutionBlock) - .withBtcBlockStoreFactory(btcBlockStoreFactory) - .withRepository(repository) - .withBtcLockSenderProvider(btcLockSenderProvider) - .build(); - } - private BtcTransaction createPegInTransaction(Address federationAddress, Coin coin, BtcECKey pubKey) { BtcTransaction btcTx = new BtcTransaction(btcParams); btcTx.addInput(BitcoinTestUtils.createHash(0), 0, ScriptBuilder.createInputScript(null, pubKey)); From 3d5d7e9b0b4d6ba2d2db992ace1b0240d77f4c0e Mon Sep 17 00:00:00 2001 From: Julian Len Date: Fri, 13 Dec 2024 13:22:14 -0300 Subject: [PATCH 04/14] fix: replaced Collections.singletonList for List.of --- .../src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java b/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java index 81998ab39c..23862e53c0 100644 --- a/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java +++ b/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java @@ -82,7 +82,7 @@ void setUp() throws Exception{ btcTransferred = bridgeConstants.getMinimumPeginTxValue(activations); bitcoinTransaction = createPegInTransaction(federationSupport.getActiveFederation().getAddress(), btcTransferred, btcPublicKey); - pmtWithTransactions = createValidPmtForTransactions(Collections.singletonList(bitcoinTransaction.getHash()), bridgeConstants.getBtcParams()); + pmtWithTransactions = createValidPmtForTransactions(List.of(bitcoinTransaction.getHash()), bridgeConstants.getBtcParams()); btcBlockWithPmtHeight = bridgeConstants.getBtcHeightWhenPegoutTxIndexActivates() + bridgeConstants.getPegoutTxIndexGracePeriodInBtcBlocks(); int chainHeight = btcBlockWithPmtHeight + bridgeConstants.getBtc2RskMinimumAcceptableConfirmations(); From d7a00917377bd0cc68aad62051103762a5eb9b5c Mon Sep 17 00:00:00 2001 From: Julian Len Date: Tue, 17 Dec 2024 10:20:53 -0300 Subject: [PATCH 05/14] fix: rsk receiver address is obtained in the setUp instead of in each test --- .../java/co/rsk/peg/RegisterBtcTransactionIT.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java b/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java index 23862e53c0..39b8fc17b2 100644 --- a/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java +++ b/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java @@ -45,7 +45,6 @@ class RegisterBtcTransactionIT { private PartialMerkleTree pmtWithTransactions; private int btcBlockWithPmtHeight; private Transaction rskTx; - private ECKey ecKey; private RskAddress rskReceiver; private BridgeSupport bridgeSupport; private BridgeEventLoggerImpl bridgeEventLogger; @@ -77,8 +76,7 @@ void setUp() throws Exception{ BtcBlockStoreWithCache btcBlockStoreWithCache = btcBlockStoreFactory.newInstance(track, bridgeConstants, bridgeStorageProvider, activations); BtcECKey btcPublicKey = BitcoinTestUtils.getBtcEcKeyFromSeed("seed"); - ecKey = ECKey.fromPublicOnly(btcPublicKey.getPubKey()); - rskReceiver = new RskAddress(ecKey.getAddress()); + rskReceiver = getRskReceiver(btcPublicKey); btcTransferred = bridgeConstants.getMinimumPeginTxValue(activations); bitcoinTransaction = createPegInTransaction(federationSupport.getActiveFederation().getAddress(), btcTransferred, btcPublicKey); @@ -132,8 +130,7 @@ void whenRegisterARepeatedLegacyBtcTransaction_shouldNotPerformAnyChange() throw // Arrange registerBtcTransactionAndCommit(); - RskAddress receiverBalance = new RskAddress(ecKey.getAddress()); - co.rsk.core.Coin expectedReceiverBalance = track.getBalance(receiverBalance); + co.rsk.core.Coin expectedReceiverBalance = track.getBalance(rskReceiver); List expectedFederationUTXOs = federationSupport.getActiveFederationBtcUTXOs(); // Act @@ -141,7 +138,7 @@ void whenRegisterARepeatedLegacyBtcTransaction_shouldNotPerformAnyChange() throw // Assert assertEquals(expectedFederationUTXOs, federationSupport.getActiveFederationBtcUTXOs()); - assertEquals(expectedReceiverBalance, repository.getBalance(receiverBalance)); + assertEquals(expectedReceiverBalance, repository.getBalance(rskReceiver)); } private void registerBtcTransactionAndCommit() throws Exception { @@ -161,6 +158,11 @@ private static UTXO utxoOf(BtcTransaction bitcoinTransaction, TransactionOutput ); } + private RskAddress getRskReceiver(BtcECKey btcPublicKey) { + ECKey ecKey = ECKey.fromPublicOnly(btcPublicKey.getPubKey()); + return new RskAddress(ecKey.getAddress()); + } + private FederationStorageProvider getFederationStorageProvider(Repository track, Federation federation) { FederationStorageProvider federationStorageProvider = createFederationStorageProvider(track); federationStorageProvider.setNewFederation(federation); From 710857d6a7ceaf948e35645e79a875fa07e457e9 Mon Sep 17 00:00:00 2001 From: Julian Len Date: Tue, 17 Dec 2024 14:43:31 -0300 Subject: [PATCH 06/14] fix: moved variables as final and renamed btcTransferred to minimumPegintxValue --- .../co/rsk/peg/BridgeSupportTestUtil.java | 3 ++- .../co/rsk/peg/RegisterBtcTransactionIT.java | 20 +++++++++---------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/rskj-core/src/test/java/co/rsk/peg/BridgeSupportTestUtil.java b/rskj-core/src/test/java/co/rsk/peg/BridgeSupportTestUtil.java index 12f19b9f56..41c49090f2 100644 --- a/rskj-core/src/test/java/co/rsk/peg/BridgeSupportTestUtil.java +++ b/rskj-core/src/test/java/co/rsk/peg/BridgeSupportTestUtil.java @@ -16,6 +16,7 @@ import java.util.*; import org.bouncycastle.util.encoders.Hex; import org.ethereum.config.blockchain.upgrades.ActivationConfig; +import org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest; import org.ethereum.core.Block; import org.ethereum.core.BlockHeader; import org.ethereum.core.BlockHeaderBuilder; @@ -98,7 +99,7 @@ public static FederationStorageProvider createFederationStorageProvider(Reposito public static Block getRskExecutionBlock() { long rskExecutionBlockNumber = 1000L; long rskExecutionBlockTimestamp = 10L; - BlockHeader blockHeader = new BlockHeaderBuilder(mock(ActivationConfig.class)) + BlockHeader blockHeader = new BlockHeaderBuilder(ActivationConfigsForTest.all()) .setNumber(rskExecutionBlockNumber) .setTimestamp(rskExecutionBlockTimestamp) .build(); diff --git a/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java b/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java index 39b8fc17b2..778e3d1f6a 100644 --- a/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java +++ b/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java @@ -36,29 +36,28 @@ class RegisterBtcTransactionIT { private final BridgeConstants bridgeConstants = BridgeMainNetConstants.getInstance(); private final NetworkParameters btcParams = bridgeConstants.getBtcParams(); private final BridgeSupportBuilder bridgeSupportBuilder = BridgeSupportBuilder.builder(); + private final ActivationConfig.ForBlock activations = ActivationConfigsForTest.all().forBlock(0); + private final Transaction rskTx = TransactionUtils.createTransaction(); + private final Coin minimumPeginValue = bridgeConstants.getMinimumPeginTxValue(activations); + private final Block rskExecutionBlock = getRskExecutionBlock(); private Repository track; private Repository repository; - private Block rskExecutionBlock; private FederationSupport federationSupport; private BridgeStorageProvider bridgeStorageProvider; private BtcTransaction bitcoinTransaction; private PartialMerkleTree pmtWithTransactions; private int btcBlockWithPmtHeight; - private Transaction rskTx; private RskAddress rskReceiver; private BridgeSupport bridgeSupport; private BridgeEventLoggerImpl bridgeEventLogger; - private Coin btcTransferred; + + @BeforeEach void setUp() throws Exception{ - rskTx = TransactionUtils.createTransaction(); repository = BridgeSupportTestUtil.createRepository(); track = repository.startTracking(); - ActivationConfig.ForBlock activations = ActivationConfigsForTest.all().forBlock(0); - rskExecutionBlock = getRskExecutionBlock(); - BtcLockSenderProvider btcLockSenderProvider = new BtcLockSenderProvider(); FeePerKbSupport feePerKbSupport = getFeePerKbSupport(repository, bridgeConstants); @@ -77,8 +76,7 @@ void setUp() throws Exception{ BtcECKey btcPublicKey = BitcoinTestUtils.getBtcEcKeyFromSeed("seed"); rskReceiver = getRskReceiver(btcPublicKey); - btcTransferred = bridgeConstants.getMinimumPeginTxValue(activations); - bitcoinTransaction = createPegInTransaction(federationSupport.getActiveFederation().getAddress(), btcTransferred, btcPublicKey); + bitcoinTransaction = createPegInTransaction(federationSupport.getActiveFederation().getAddress(), minimumPeginValue, btcPublicKey); pmtWithTransactions = createValidPmtForTransactions(List.of(bitcoinTransaction.getHash()), bridgeConstants.getBtcParams()); btcBlockWithPmtHeight = bridgeConstants.getBtcHeightWhenPegoutTxIndexActivates() + bridgeConstants.getPegoutTxIndexGracePeriodInBtcBlocks(); @@ -109,7 +107,7 @@ void whenRegisterALegacyBtcTransaction_shouldRegisterTheNewUtxoAndTransferTheRbt List expectedFederationUtxos = Collections.singletonList(utxoOf(bitcoinTransaction, output)); co.rsk.core.Coin receiverBalance = track.getBalance(rskReceiver); - co.rsk.core.Coin expectedReceiverBalance = receiverBalance.add(co.rsk.core.Coin.fromBitcoin(btcTransferred)); + co.rsk.core.Coin expectedReceiverBalance = receiverBalance.add(co.rsk.core.Coin.fromBitcoin(minimumPeginValue)); // Act registerBtcTransactionAndCommit(); @@ -121,7 +119,7 @@ void whenRegisterALegacyBtcTransaction_shouldRegisterTheNewUtxoAndTransferTheRbt assertEquals(expectedFederationUtxos, federationSupport.getActiveFederationBtcUTXOs()); assertEquals(expectedReceiverBalance, repository.getBalance(rskReceiver)); - verify(bridgeEventLogger, times(1)).logPeginBtc(rskReceiver, bitcoinTransaction, btcTransferred, 0); + verify(bridgeEventLogger, times(1)).logPeginBtc(rskReceiver, bitcoinTransaction, minimumPeginValue, 0); } From b751f1c7435735f27443f7e23cd1a0dfadafdb46 Mon Sep 17 00:00:00 2001 From: Julian Len Date: Tue, 17 Dec 2024 15:13:58 -0300 Subject: [PATCH 07/14] fix: deleted boilerplate code like getters --- .../co/rsk/peg/RegisterBtcTransactionIT.java | 52 +++++++------------ 1 file changed, 20 insertions(+), 32 deletions(-) diff --git a/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java b/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java index 778e3d1f6a..0017b760bb 100644 --- a/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java +++ b/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java @@ -59,10 +59,17 @@ void setUp() throws Exception{ track = repository.startTracking(); BtcLockSenderProvider btcLockSenderProvider = new BtcLockSenderProvider(); - FeePerKbSupport feePerKbSupport = getFeePerKbSupport(repository, bridgeConstants); + StorageAccessor bridgeStorageAccessor = new BridgeStorageAccessorImpl(repository); + + FeePerKbStorageProvider feePerKbStorageProvider = new FeePerKbStorageProviderImpl(bridgeStorageAccessor); + FeePerKbSupport feePerKbSupport = new FeePerKbSupportImpl( + bridgeConstants.getFeePerKbConstants(), + feePerKbStorageProvider + ); Federation federation = P2shErpFederationBuilder.builder().build(); - FederationStorageProvider federationStorageProvider = getFederationStorageProvider(track, federation); + FederationStorageProvider federationStorageProvider = createFederationStorageProvider(track); + federationStorageProvider.setNewFederation(federation); FederationConstants federationConstants = bridgeConstants.getFederationConstants(); federationSupport = FederationSupportBuilder.builder() .withFederationConstants(federationConstants) @@ -75,7 +82,8 @@ void setUp() throws Exception{ BtcBlockStoreWithCache btcBlockStoreWithCache = btcBlockStoreFactory.newInstance(track, bridgeConstants, bridgeStorageProvider, activations); BtcECKey btcPublicKey = BitcoinTestUtils.getBtcEcKeyFromSeed("seed"); - rskReceiver = getRskReceiver(btcPublicKey); + ECKey ecKey = ECKey.fromPublicOnly(btcPublicKey.getPubKey()); + rskReceiver = new RskAddress(ecKey.getAddress()); bitcoinTransaction = createPegInTransaction(federationSupport.getActiveFederation().getAddress(), minimumPeginValue, btcPublicKey); pmtWithTransactions = createValidPmtForTransactions(List.of(bitcoinTransaction.getHash()), bridgeConstants.getBtcParams()); @@ -110,7 +118,9 @@ void whenRegisterALegacyBtcTransaction_shouldRegisterTheNewUtxoAndTransferTheRbt co.rsk.core.Coin expectedReceiverBalance = receiverBalance.add(co.rsk.core.Coin.fromBitcoin(minimumPeginValue)); // Act - registerBtcTransactionAndCommit(); + bridgeSupport.registerBtcTransaction(rskTx, bitcoinTransaction.bitcoinSerialize(), btcBlockWithPmtHeight, pmtWithTransactions.bitcoinSerialize()); + bridgeSupport.save(); + track.commit(); // Assert Optional heightIfBtcTxHashIsAlreadyProcessed = bridgeStorageProvider.getHeightIfBtcTxhashIsAlreadyProcessed(bitcoinTransaction.getHash()); @@ -126,25 +136,23 @@ void whenRegisterALegacyBtcTransaction_shouldRegisterTheNewUtxoAndTransferTheRbt @Test void whenRegisterARepeatedLegacyBtcTransaction_shouldNotPerformAnyChange() throws Exception { // Arrange - registerBtcTransactionAndCommit(); + bridgeSupport.registerBtcTransaction(rskTx, bitcoinTransaction.bitcoinSerialize(), btcBlockWithPmtHeight, pmtWithTransactions.bitcoinSerialize()); + bridgeSupport.save(); + track.commit(); co.rsk.core.Coin expectedReceiverBalance = track.getBalance(rskReceiver); List expectedFederationUTXOs = federationSupport.getActiveFederationBtcUTXOs(); // Act - registerBtcTransactionAndCommit(); + bridgeSupport.registerBtcTransaction(rskTx, bitcoinTransaction.bitcoinSerialize(), btcBlockWithPmtHeight, pmtWithTransactions.bitcoinSerialize()); + bridgeSupport.save(); + track.commit(); // Assert assertEquals(expectedFederationUTXOs, federationSupport.getActiveFederationBtcUTXOs()); assertEquals(expectedReceiverBalance, repository.getBalance(rskReceiver)); } - private void registerBtcTransactionAndCommit() throws Exception { - bridgeSupport.registerBtcTransaction(rskTx, bitcoinTransaction.bitcoinSerialize(), btcBlockWithPmtHeight, pmtWithTransactions.bitcoinSerialize()); - bridgeSupport.save(); - track.commit(); - } - private static UTXO utxoOf(BtcTransaction bitcoinTransaction, TransactionOutput output) { return new UTXO( bitcoinTransaction.getHash(), @@ -156,26 +164,6 @@ private static UTXO utxoOf(BtcTransaction bitcoinTransaction, TransactionOutput ); } - private RskAddress getRskReceiver(BtcECKey btcPublicKey) { - ECKey ecKey = ECKey.fromPublicOnly(btcPublicKey.getPubKey()); - return new RskAddress(ecKey.getAddress()); - } - - private FederationStorageProvider getFederationStorageProvider(Repository track, Federation federation) { - FederationStorageProvider federationStorageProvider = createFederationStorageProvider(track); - federationStorageProvider.setNewFederation(federation); - return federationStorageProvider; - } - - private static FeePerKbSupport getFeePerKbSupport(Repository repository, BridgeConstants bridgeConstants) { - StorageAccessor bridgeStorageAccessor = new BridgeStorageAccessorImpl(repository); - FeePerKbStorageProvider feePerKbStorageProvider = new FeePerKbStorageProviderImpl(bridgeStorageAccessor); - return new FeePerKbSupportImpl( - bridgeConstants.getFeePerKbConstants(), - feePerKbStorageProvider - ); - } - private BtcTransaction createPegInTransaction(Address federationAddress, Coin coin, BtcECKey pubKey) { BtcTransaction btcTx = new BtcTransaction(btcParams); btcTx.addInput(BitcoinTestUtils.createHash(0), 0, ScriptBuilder.createInputScript(null, pubKey)); From c782a033914dd5313548d4fe5dfad9028af2e626 Mon Sep 17 00:00:00 2001 From: Julian Len Date: Tue, 17 Dec 2024 16:05:30 -0300 Subject: [PATCH 08/14] fix: unified repository and track variables into one --- .../co/rsk/peg/RegisterBtcTransactionIT.java | 20 +++++++------------ 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java b/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java index 0017b760bb..8faa955952 100644 --- a/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java +++ b/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java @@ -40,7 +40,6 @@ class RegisterBtcTransactionIT { private final Transaction rskTx = TransactionUtils.createTransaction(); private final Coin minimumPeginValue = bridgeConstants.getMinimumPeginTxValue(activations); private final Block rskExecutionBlock = getRskExecutionBlock(); - private Repository track; private Repository repository; private FederationSupport federationSupport; private BridgeStorageProvider bridgeStorageProvider; @@ -52,11 +51,9 @@ class RegisterBtcTransactionIT { private BridgeEventLoggerImpl bridgeEventLogger; - @BeforeEach void setUp() throws Exception{ - repository = BridgeSupportTestUtil.createRepository(); - track = repository.startTracking(); + repository = BridgeSupportTestUtil.createRepository().startTracking(); BtcLockSenderProvider btcLockSenderProvider = new BtcLockSenderProvider(); StorageAccessor bridgeStorageAccessor = new BridgeStorageAccessorImpl(repository); @@ -68,7 +65,7 @@ void setUp() throws Exception{ ); Federation federation = P2shErpFederationBuilder.builder().build(); - FederationStorageProvider federationStorageProvider = createFederationStorageProvider(track); + FederationStorageProvider federationStorageProvider = createFederationStorageProvider(repository); federationStorageProvider.setNewFederation(federation); FederationConstants federationConstants = bridgeConstants.getFederationConstants(); federationSupport = FederationSupportBuilder.builder() @@ -77,9 +74,9 @@ void setUp() throws Exception{ .withActivations(activations) .build(); - bridgeStorageProvider = new BridgeStorageProvider(track, PrecompiledContracts.BRIDGE_ADDR, bridgeConstants.getBtcParams(), activations); + bridgeStorageProvider = new BridgeStorageProvider(repository, PrecompiledContracts.BRIDGE_ADDR, bridgeConstants.getBtcParams(), activations); BtcBlockStoreWithCache.Factory btcBlockStoreFactory = new RepositoryBtcBlockStoreWithCache.Factory(bridgeConstants.getBtcParams(), 100, 100); - BtcBlockStoreWithCache btcBlockStoreWithCache = btcBlockStoreFactory.newInstance(track, bridgeConstants, bridgeStorageProvider, activations); + BtcBlockStoreWithCache btcBlockStoreWithCache = btcBlockStoreFactory.newInstance(repository, bridgeConstants, bridgeStorageProvider, activations); BtcECKey btcPublicKey = BitcoinTestUtils.getBtcEcKeyFromSeed("seed"); ECKey ecKey = ECKey.fromPublicOnly(btcPublicKey.getPubKey()); @@ -103,7 +100,7 @@ void setUp() throws Exception{ .withFeePerKbSupport(feePerKbSupport) .withExecutionBlock(rskExecutionBlock) .withBtcBlockStoreFactory(btcBlockStoreFactory) - .withRepository(track) + .withRepository(repository) .withBtcLockSenderProvider(btcLockSenderProvider) .build(); } @@ -114,13 +111,12 @@ void whenRegisterALegacyBtcTransaction_shouldRegisterTheNewUtxoAndTransferTheRbt TransactionOutput output = bitcoinTransaction.getOutput(0); List expectedFederationUtxos = Collections.singletonList(utxoOf(bitcoinTransaction, output)); - co.rsk.core.Coin receiverBalance = track.getBalance(rskReceiver); + co.rsk.core.Coin receiverBalance = repository.getBalance(rskReceiver); co.rsk.core.Coin expectedReceiverBalance = receiverBalance.add(co.rsk.core.Coin.fromBitcoin(minimumPeginValue)); // Act bridgeSupport.registerBtcTransaction(rskTx, bitcoinTransaction.bitcoinSerialize(), btcBlockWithPmtHeight, pmtWithTransactions.bitcoinSerialize()); bridgeSupport.save(); - track.commit(); // Assert Optional heightIfBtcTxHashIsAlreadyProcessed = bridgeStorageProvider.getHeightIfBtcTxhashIsAlreadyProcessed(bitcoinTransaction.getHash()); @@ -138,15 +134,13 @@ void whenRegisterARepeatedLegacyBtcTransaction_shouldNotPerformAnyChange() throw // Arrange bridgeSupport.registerBtcTransaction(rskTx, bitcoinTransaction.bitcoinSerialize(), btcBlockWithPmtHeight, pmtWithTransactions.bitcoinSerialize()); bridgeSupport.save(); - track.commit(); - co.rsk.core.Coin expectedReceiverBalance = track.getBalance(rskReceiver); + co.rsk.core.Coin expectedReceiverBalance = repository.getBalance(rskReceiver); List expectedFederationUTXOs = federationSupport.getActiveFederationBtcUTXOs(); // Act bridgeSupport.registerBtcTransaction(rskTx, bitcoinTransaction.bitcoinSerialize(), btcBlockWithPmtHeight, pmtWithTransactions.bitcoinSerialize()); bridgeSupport.save(); - track.commit(); // Assert assertEquals(expectedFederationUTXOs, federationSupport.getActiveFederationBtcUTXOs()); From 05a7c51eeaab00a97707c6f9eeff70e34edffb27 Mon Sep 17 00:00:00 2001 From: Julian Len Date: Tue, 17 Dec 2024 16:11:59 -0300 Subject: [PATCH 09/14] fix: replaced bridgeConstants.getBtcParams() for btcNetworkParams --- .../java/co/rsk/peg/RegisterBtcTransactionIT.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java b/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java index 8faa955952..7038d55957 100644 --- a/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java +++ b/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java @@ -34,7 +34,7 @@ class RegisterBtcTransactionIT { private final BridgeConstants bridgeConstants = BridgeMainNetConstants.getInstance(); - private final NetworkParameters btcParams = bridgeConstants.getBtcParams(); + private final NetworkParameters btcNetworkParams = bridgeConstants.getBtcParams(); private final BridgeSupportBuilder bridgeSupportBuilder = BridgeSupportBuilder.builder(); private final ActivationConfig.ForBlock activations = ActivationConfigsForTest.all().forBlock(0); private final Transaction rskTx = TransactionUtils.createTransaction(); @@ -74,8 +74,8 @@ void setUp() throws Exception{ .withActivations(activations) .build(); - bridgeStorageProvider = new BridgeStorageProvider(repository, PrecompiledContracts.BRIDGE_ADDR, bridgeConstants.getBtcParams(), activations); - BtcBlockStoreWithCache.Factory btcBlockStoreFactory = new RepositoryBtcBlockStoreWithCache.Factory(bridgeConstants.getBtcParams(), 100, 100); + bridgeStorageProvider = new BridgeStorageProvider(repository, PrecompiledContracts.BRIDGE_ADDR, btcNetworkParams, activations); + BtcBlockStoreWithCache.Factory btcBlockStoreFactory = new RepositoryBtcBlockStoreWithCache.Factory(btcNetworkParams, 100, 100); BtcBlockStoreWithCache btcBlockStoreWithCache = btcBlockStoreFactory.newInstance(repository, bridgeConstants, bridgeStorageProvider, activations); BtcECKey btcPublicKey = BitcoinTestUtils.getBtcEcKeyFromSeed("seed"); @@ -83,13 +83,13 @@ void setUp() throws Exception{ rskReceiver = new RskAddress(ecKey.getAddress()); bitcoinTransaction = createPegInTransaction(federationSupport.getActiveFederation().getAddress(), minimumPeginValue, btcPublicKey); - pmtWithTransactions = createValidPmtForTransactions(List.of(bitcoinTransaction.getHash()), bridgeConstants.getBtcParams()); + pmtWithTransactions = createValidPmtForTransactions(List.of(bitcoinTransaction.getHash()), btcNetworkParams); btcBlockWithPmtHeight = bridgeConstants.getBtcHeightWhenPegoutTxIndexActivates() + bridgeConstants.getPegoutTxIndexGracePeriodInBtcBlocks(); int chainHeight = btcBlockWithPmtHeight + bridgeConstants.getBtc2RskMinimumAcceptableConfirmations(); bridgeEventLogger = spy(new BridgeEventLoggerImpl(bridgeConstants, activations, new ArrayList<>())); - recreateChainFromPmt(btcBlockStoreWithCache, chainHeight, pmtWithTransactions, btcBlockWithPmtHeight, bridgeConstants.getBtcParams()); + recreateChainFromPmt(btcBlockStoreWithCache, chainHeight, pmtWithTransactions, btcBlockWithPmtHeight, btcNetworkParams); bridgeStorageProvider.save(); bridgeSupport = bridgeSupportBuilder .withBridgeConstants(bridgeConstants) @@ -159,9 +159,9 @@ private static UTXO utxoOf(BtcTransaction bitcoinTransaction, TransactionOutput } private BtcTransaction createPegInTransaction(Address federationAddress, Coin coin, BtcECKey pubKey) { - BtcTransaction btcTx = new BtcTransaction(btcParams); + BtcTransaction btcTx = new BtcTransaction(btcNetworkParams); btcTx.addInput(BitcoinTestUtils.createHash(0), 0, ScriptBuilder.createInputScript(null, pubKey)); - btcTx.addOutput(new TransactionOutput(btcParams, btcTx, coin, federationAddress)); + btcTx.addOutput(new TransactionOutput(btcNetworkParams, btcTx, coin, federationAddress)); return btcTx; } From fcedd0d47ebcaabf081c0d65c5174749c46d27ca Mon Sep 17 00:00:00 2001 From: Julian Len Date: Tue, 17 Dec 2024 18:10:20 -0300 Subject: [PATCH 10/14] fix: deleted the spy usage and replaced by an actual assert that certain event was emitted, besides I moved the method to assert an event to BridgeSupportTestUtil --- .../java/co/rsk/peg/BridgeSupportSvpTest.java | 47 +++++-------------- .../co/rsk/peg/BridgeSupportTestUtil.java | 32 +++++++++++-- .../co/rsk/peg/RegisterBtcTransactionIT.java | 18 +++++-- 3 files changed, 54 insertions(+), 43 deletions(-) diff --git a/rskj-core/src/test/java/co/rsk/peg/BridgeSupportSvpTest.java b/rskj-core/src/test/java/co/rsk/peg/BridgeSupportSvpTest.java index c116f6ed9b..6acd217b8f 100644 --- a/rskj-core/src/test/java/co/rsk/peg/BridgeSupportSvpTest.java +++ b/rskj-core/src/test/java/co/rsk/peg/BridgeSupportSvpTest.java @@ -247,8 +247,8 @@ private void assertLogCommitFederationFailed() { byte[] proposedFederationRedeemScriptSerialized = proposedFederation.getRedeemScript().getProgram(); byte[] encodedData = getEncodedData(commitFederationFailedEvent, proposedFederationRedeemScriptSerialized, rskExecutionBlock.getNumber()); - assertEventWasEmittedWithExpectedTopics(encodedTopics); - assertEventWasEmittedWithExpectedData(encodedData); + assertEventWasEmittedWithExpectedTopics(encodedTopics, logs); + assertEventWasEmittedWithExpectedData(encodedData, logs); } private void assertNoSVPValues() { @@ -387,11 +387,11 @@ private void assertJustUpdateCollectionsWasLogged() { CallTransaction.Function updateCollectionsEvent = BridgeEvents.UPDATE_COLLECTIONS.getEvent(); List encodedTopics = getEncodedTopics(updateCollectionsEvent); - assertEventWasEmittedWithExpectedTopics(encodedTopics); + assertEventWasEmittedWithExpectedTopics(encodedTopics, logs); String senderData = rskTx.getSender(mock(SignatureCache.class)).toHexString(); byte[] encodedData = getEncodedData(updateCollectionsEvent, senderData); - assertEventWasEmittedWithExpectedData(encodedData); + assertEventWasEmittedWithExpectedData(encodedData, logs); } } @@ -924,8 +924,8 @@ private void assertLogAddSignature(FederationMember federationMember, byte[] rsk BtcECKey federatorBtcPublicKey = federationMember.getBtcPublicKey(); byte[] encodedData = getEncodedData(addSignatureEvent, federatorBtcPublicKey.getPubKey()); - assertEventWasEmittedWithExpectedTopics(encodedTopics); - assertEventWasEmittedWithExpectedData(encodedData); + assertEventWasEmittedWithExpectedTopics(encodedTopics, logs); + assertEventWasEmittedWithExpectedData(encodedData, logs); } private void assertLogReleaseBtc(Keccak256 rskTxHash, BtcTransaction btcTx) { @@ -935,8 +935,8 @@ private void assertLogReleaseBtc(Keccak256 rskTxHash, BtcTransaction btcTx) { byte[] btcTxSerialized = btcTx.bitcoinSerialize(); byte[] encodedData = getEncodedData(releaseBtcEvent, btcTxSerialized); - assertEventWasEmittedWithExpectedTopics(encodedTopics); - assertEventWasEmittedWithExpectedData(encodedData); + assertEventWasEmittedWithExpectedTopics(encodedTopics, logs); + assertEventWasEmittedWithExpectedData(encodedData, logs); } private void assertFederatorSignedInputs(List inputs, List sigHashes, BtcECKey key) { @@ -1402,8 +1402,8 @@ private void assertLogReleaseRequested(Keccak256 releaseCreationTxHash, Sha256Ha byte[] encodedData = getEncodedData(releaseRequestedEvent, requestedAmount.getValue()); - assertEventWasEmittedWithExpectedTopics(encodedTopics); - assertEventWasEmittedWithExpectedData(encodedData); + assertEventWasEmittedWithExpectedTopics(encodedTopics, logs); + assertEventWasEmittedWithExpectedData(encodedData, logs); } private void assertLogPegoutTransactionCreated(BtcTransaction pegoutTransaction) { @@ -1415,30 +1415,7 @@ private void assertLogPegoutTransactionCreated(BtcTransaction pegoutTransaction) byte[] serializedOutpointValues = UtxoUtils.encodeOutpointValues(outpointValues); byte[] encodedData = getEncodedData(pegoutTransactionCreatedEvent, serializedOutpointValues); - assertEventWasEmittedWithExpectedTopics(encodedTopics); - assertEventWasEmittedWithExpectedData(encodedData); - } - - private List getEncodedTopics(CallTransaction.Function bridgeEvent, Object... args) { - byte[][] encodedTopicsInBytes = bridgeEvent.encodeEventTopics(args); - return LogInfo.byteArrayToList(encodedTopicsInBytes); - } - - private byte[] getEncodedData(CallTransaction.Function bridgeEvent, Object... args) { - return bridgeEvent.encodeEventData(args); - } - - private void assertEventWasEmittedWithExpectedTopics(List expectedTopics) { - Optional topicOpt = logs.stream() - .filter(log -> log.getTopics().equals(expectedTopics)) - .findFirst(); - assertTrue(topicOpt.isPresent()); - } - - private void assertEventWasEmittedWithExpectedData(byte[] expectedData) { - Optional data = logs.stream() - .filter(log -> Arrays.equals(log.getData(), expectedData)) - .findFirst(); - assertTrue(data.isPresent()); + assertEventWasEmittedWithExpectedTopics(encodedTopics, logs); + assertEventWasEmittedWithExpectedData(encodedData, logs); } } diff --git a/rskj-core/src/test/java/co/rsk/peg/BridgeSupportTestUtil.java b/rskj-core/src/test/java/co/rsk/peg/BridgeSupportTestUtil.java index 41c49090f2..d501cdbbe8 100644 --- a/rskj-core/src/test/java/co/rsk/peg/BridgeSupportTestUtil.java +++ b/rskj-core/src/test/java/co/rsk/peg/BridgeSupportTestUtil.java @@ -1,5 +1,6 @@ package co.rsk.peg; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.*; import co.rsk.bitcoinj.core.*; @@ -15,13 +16,11 @@ import java.math.BigInteger; import java.util.*; import org.bouncycastle.util.encoders.Hex; -import org.ethereum.config.blockchain.upgrades.ActivationConfig; import org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest; -import org.ethereum.core.Block; -import org.ethereum.core.BlockHeader; -import org.ethereum.core.BlockHeaderBuilder; -import org.ethereum.core.Repository; +import org.ethereum.core.*; import org.ethereum.db.MutableRepository; +import org.ethereum.vm.DataWord; +import org.ethereum.vm.LogInfo; public final class BridgeSupportTestUtil { public static Repository createRepository() { @@ -105,4 +104,27 @@ public static Block getRskExecutionBlock() { .build(); return Block.createBlockFromHeader(blockHeader, true); } + + public static List getEncodedTopics(CallTransaction.Function bridgeEvent, Object... args) { + byte[][] encodedTopicsInBytes = bridgeEvent.encodeEventTopics(args); + return LogInfo.byteArrayToList(encodedTopicsInBytes); + } + + public static byte[] getEncodedData(CallTransaction.Function bridgeEvent, Object... args) { + return bridgeEvent.encodeEventData(args); + } + + public static void assertEventWasEmittedWithExpectedTopics(List expectedTopics, List logs) { + Optional topicOpt = logs.stream() + .filter(log -> log.getTopics().equals(expectedTopics)) + .findFirst(); + assertTrue(topicOpt.isPresent()); + } + + public static void assertEventWasEmittedWithExpectedData(byte[] expectedData, List logs) { + Optional data = logs.stream() + .filter(log -> Arrays.equals(log.getData(), expectedData)) + .findFirst(); + assertTrue(data.isPresent()); + } } diff --git a/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java b/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java index 7038d55957..71da673a17 100644 --- a/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java +++ b/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java @@ -2,7 +2,6 @@ import static co.rsk.peg.BridgeSupportTestUtil.*; import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.Mockito.*; import co.rsk.bitcoinj.core.*; import co.rsk.bitcoinj.script.ScriptBuilder; @@ -27,6 +26,8 @@ import org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest; import org.ethereum.core.*; import org.ethereum.crypto.ECKey; +import org.ethereum.vm.DataWord; +import org.ethereum.vm.LogInfo; import org.ethereum.vm.PrecompiledContracts; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -49,6 +50,7 @@ class RegisterBtcTransactionIT { private RskAddress rskReceiver; private BridgeSupport bridgeSupport; private BridgeEventLoggerImpl bridgeEventLogger; + private ArrayList logs; @BeforeEach @@ -87,7 +89,8 @@ void setUp() throws Exception{ btcBlockWithPmtHeight = bridgeConstants.getBtcHeightWhenPegoutTxIndexActivates() + bridgeConstants.getPegoutTxIndexGracePeriodInBtcBlocks(); int chainHeight = btcBlockWithPmtHeight + bridgeConstants.getBtc2RskMinimumAcceptableConfirmations(); - bridgeEventLogger = spy(new BridgeEventLoggerImpl(bridgeConstants, activations, new ArrayList<>())); + logs = new ArrayList<>(); + bridgeEventLogger = new BridgeEventLoggerImpl(bridgeConstants, activations, logs); recreateChainFromPmt(btcBlockStoreWithCache, chainHeight, pmtWithTransactions, btcBlockWithPmtHeight, btcNetworkParams); bridgeStorageProvider.save(); @@ -125,7 +128,7 @@ void whenRegisterALegacyBtcTransaction_shouldRegisterTheNewUtxoAndTransferTheRbt assertEquals(expectedFederationUtxos, federationSupport.getActiveFederationBtcUTXOs()); assertEquals(expectedReceiverBalance, repository.getBalance(rskReceiver)); - verify(bridgeEventLogger, times(1)).logPeginBtc(rskReceiver, bitcoinTransaction, minimumPeginValue, 0); + assertLogPegInBtc(); } @@ -165,4 +168,13 @@ private BtcTransaction createPegInTransaction(Address federationAddress, Coin co return btcTx; } + + private void assertLogPegInBtc() { + Sha256Hash peginTransactionHash = bitcoinTransaction.getHash(); + List encodedTopics = getEncodedTopics(BridgeEvents.PEGIN_BTC.getEvent(), rskReceiver.toString(), peginTransactionHash.getBytes()); + byte[] encodedData = getEncodedData(BridgeEvents.PEGIN_BTC.getEvent(), minimumPeginValue.getValue(), 0); + + assertEventWasEmittedWithExpectedTopics(encodedTopics, logs); + assertEventWasEmittedWithExpectedData(encodedData, logs); + } } From 68a0f155120f80c71aa2802694aa7abe5fa0d9b5 Mon Sep 17 00:00:00 2001 From: Julian Len Date: Tue, 17 Dec 2024 18:12:20 -0300 Subject: [PATCH 11/14] fix: renamed the test according to the standard --- .../src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java b/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java index 71da673a17..725ab83fbb 100644 --- a/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java +++ b/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java @@ -109,7 +109,7 @@ void setUp() throws Exception{ } @Test - void whenRegisterALegacyBtcTransaction_shouldRegisterTheNewUtxoAndTransferTheRbtcBalance() throws Exception { + void registerBtcTransaction_forALegacyBtcTransaction_shouldRegisterTheNewUtxoAndTransferTheRbtcBalance() throws Exception { // Arrange TransactionOutput output = bitcoinTransaction.getOutput(0); List expectedFederationUtxos = Collections.singletonList(utxoOf(bitcoinTransaction, output)); @@ -133,7 +133,7 @@ void whenRegisterALegacyBtcTransaction_shouldRegisterTheNewUtxoAndTransferTheRbt } @Test - void whenRegisterARepeatedLegacyBtcTransaction_shouldNotPerformAnyChange() throws Exception { + void registerBtc_forARepeatedLegacyBtcTransaction_shouldNotPerformAnyChange() throws Exception { // Arrange bridgeSupport.registerBtcTransaction(rskTx, bitcoinTransaction.bitcoinSerialize(), btcBlockWithPmtHeight, pmtWithTransactions.bitcoinSerialize()); bridgeSupport.save(); From c3770fe1cdf7247b5806b2f77a29df26738e46fe Mon Sep 17 00:00:00 2001 From: Julian Len Date: Tue, 17 Dec 2024 18:26:10 -0300 Subject: [PATCH 12/14] fix: bridgeEventLogger as a local variable --- .../src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java b/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java index 725ab83fbb..857b99a3cf 100644 --- a/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java +++ b/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java @@ -49,7 +49,6 @@ class RegisterBtcTransactionIT { private int btcBlockWithPmtHeight; private RskAddress rskReceiver; private BridgeSupport bridgeSupport; - private BridgeEventLoggerImpl bridgeEventLogger; private ArrayList logs; @@ -90,7 +89,7 @@ void setUp() throws Exception{ int chainHeight = btcBlockWithPmtHeight + bridgeConstants.getBtc2RskMinimumAcceptableConfirmations(); logs = new ArrayList<>(); - bridgeEventLogger = new BridgeEventLoggerImpl(bridgeConstants, activations, logs); + BridgeEventLoggerImpl bridgeEventLogger = new BridgeEventLoggerImpl(bridgeConstants, activations, logs); recreateChainFromPmt(btcBlockStoreWithCache, chainHeight, pmtWithTransactions, btcBlockWithPmtHeight, btcNetworkParams); bridgeStorageProvider.save(); From 568762cc78ea3b91cfec39927f4f34f782aadda2 Mon Sep 17 00:00:00 2001 From: Julian Len Date: Wed, 18 Dec 2024 12:07:29 -0300 Subject: [PATCH 13/14] fix: style code changes --- .../java/co/rsk/peg/BridgeSupportSvpTest.java | 6 +- .../co/rsk/peg/BridgeSupportTestUtil.java | 4 +- .../co/rsk/peg/RegisterBtcTransactionIT.java | 70 +++++++++---------- 3 files changed, 36 insertions(+), 44 deletions(-) diff --git a/rskj-core/src/test/java/co/rsk/peg/BridgeSupportSvpTest.java b/rskj-core/src/test/java/co/rsk/peg/BridgeSupportSvpTest.java index 6acd217b8f..e273991e5a 100644 --- a/rskj-core/src/test/java/co/rsk/peg/BridgeSupportSvpTest.java +++ b/rskj-core/src/test/java/co/rsk/peg/BridgeSupportSvpTest.java @@ -93,11 +93,7 @@ void setUp() { // rsk execution block long rskExecutionBlockNumber = 1000L; long rskExecutionBlockTimestamp = 10L; - BlockHeader blockHeader = new BlockHeaderBuilder(mock(ActivationConfig.class)) - .setNumber(rskExecutionBlockNumber) - .setTimestamp(rskExecutionBlockTimestamp) - .build(); - rskExecutionBlock = Block.createBlockFromHeader(blockHeader, true); + rskExecutionBlock = getRskExecutionBlock(rskExecutionBlockNumber, rskExecutionBlockTimestamp); Keccak256 rskTxHash = PegTestUtils.createHash3(1); rskTx = mock(Transaction.class); diff --git a/rskj-core/src/test/java/co/rsk/peg/BridgeSupportTestUtil.java b/rskj-core/src/test/java/co/rsk/peg/BridgeSupportTestUtil.java index d501cdbbe8..f4c04238db 100644 --- a/rskj-core/src/test/java/co/rsk/peg/BridgeSupportTestUtil.java +++ b/rskj-core/src/test/java/co/rsk/peg/BridgeSupportTestUtil.java @@ -95,9 +95,7 @@ public static FederationStorageProvider createFederationStorageProvider(Reposito return new FederationStorageProviderImpl(bridgeStorageAccessor); } - public static Block getRskExecutionBlock() { - long rskExecutionBlockNumber = 1000L; - long rskExecutionBlockTimestamp = 10L; + public static Block getRskExecutionBlock(long rskExecutionBlockNumber, long rskExecutionBlockTimestamp) { BlockHeader blockHeader = new BlockHeaderBuilder(ActivationConfigsForTest.all()) .setNumber(rskExecutionBlockNumber) .setTimestamp(rskExecutionBlockTimestamp) diff --git a/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java b/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java index 857b99a3cf..dab40ce610 100644 --- a/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java +++ b/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java @@ -34,13 +34,15 @@ import java.util.*; class RegisterBtcTransactionIT { + public static final long RSK_EXECUTION_BLOCK_NUMBER = 1000L; + public static final long RSK_EXECUTION_BLOCK_TIMESTAMP = 10L; private final BridgeConstants bridgeConstants = BridgeMainNetConstants.getInstance(); private final NetworkParameters btcNetworkParams = bridgeConstants.getBtcParams(); private final BridgeSupportBuilder bridgeSupportBuilder = BridgeSupportBuilder.builder(); private final ActivationConfig.ForBlock activations = ActivationConfigsForTest.all().forBlock(0); private final Transaction rskTx = TransactionUtils.createTransaction(); private final Coin minimumPeginValue = bridgeConstants.getMinimumPeginTxValue(activations); - private final Block rskExecutionBlock = getRskExecutionBlock(); + private final Block rskExecutionBlock = getRskExecutionBlock(RSK_EXECUTION_BLOCK_NUMBER, RSK_EXECUTION_BLOCK_TIMESTAMP); private Repository repository; private FederationSupport federationSupport; private BridgeStorageProvider bridgeStorageProvider; @@ -60,20 +62,17 @@ void setUp() throws Exception{ StorageAccessor bridgeStorageAccessor = new BridgeStorageAccessorImpl(repository); FeePerKbStorageProvider feePerKbStorageProvider = new FeePerKbStorageProviderImpl(bridgeStorageAccessor); - FeePerKbSupport feePerKbSupport = new FeePerKbSupportImpl( - bridgeConstants.getFeePerKbConstants(), - feePerKbStorageProvider - ); + FeePerKbSupport feePerKbSupport = new FeePerKbSupportImpl(bridgeConstants.getFeePerKbConstants(), feePerKbStorageProvider); + FederationStorageProvider federationStorageProvider = new FederationStorageProviderImpl(bridgeStorageAccessor); Federation federation = P2shErpFederationBuilder.builder().build(); - FederationStorageProvider federationStorageProvider = createFederationStorageProvider(repository); federationStorageProvider.setNewFederation(federation); FederationConstants federationConstants = bridgeConstants.getFederationConstants(); federationSupport = FederationSupportBuilder.builder() - .withFederationConstants(federationConstants) - .withFederationStorageProvider(federationStorageProvider) - .withActivations(activations) - .build(); + .withFederationConstants(federationConstants) + .withFederationStorageProvider(federationStorageProvider) + .withActivations(activations) + .build(); bridgeStorageProvider = new BridgeStorageProvider(repository, PrecompiledContracts.BRIDGE_ADDR, btcNetworkParams, activations); BtcBlockStoreWithCache.Factory btcBlockStoreFactory = new RepositoryBtcBlockStoreWithCache.Factory(btcNetworkParams, 100, 100); @@ -93,29 +92,23 @@ void setUp() throws Exception{ recreateChainFromPmt(btcBlockStoreWithCache, chainHeight, pmtWithTransactions, btcBlockWithPmtHeight, btcNetworkParams); bridgeStorageProvider.save(); + bridgeSupport = bridgeSupportBuilder - .withBridgeConstants(bridgeConstants) - .withProvider(bridgeStorageProvider) - .withActivations(activations) - .withEventLogger(bridgeEventLogger) - .withFederationSupport(federationSupport) - .withFeePerKbSupport(feePerKbSupport) - .withExecutionBlock(rskExecutionBlock) - .withBtcBlockStoreFactory(btcBlockStoreFactory) - .withRepository(repository) - .withBtcLockSenderProvider(btcLockSenderProvider) - .build(); + .withBridgeConstants(bridgeConstants) + .withProvider(bridgeStorageProvider) + .withActivations(activations) + .withEventLogger(bridgeEventLogger) + .withFederationSupport(federationSupport) + .withFeePerKbSupport(feePerKbSupport) + .withExecutionBlock(rskExecutionBlock) + .withBtcBlockStoreFactory(btcBlockStoreFactory) + .withRepository(repository) + .withBtcLockSenderProvider(btcLockSenderProvider) + .build(); } @Test void registerBtcTransaction_forALegacyBtcTransaction_shouldRegisterTheNewUtxoAndTransferTheRbtcBalance() throws Exception { - // Arrange - TransactionOutput output = bitcoinTransaction.getOutput(0); - List expectedFederationUtxos = Collections.singletonList(utxoOf(bitcoinTransaction, output)); - - co.rsk.core.Coin receiverBalance = repository.getBalance(rskReceiver); - co.rsk.core.Coin expectedReceiverBalance = receiverBalance.add(co.rsk.core.Coin.fromBitcoin(minimumPeginValue)); - // Act bridgeSupport.registerBtcTransaction(rskTx, bitcoinTransaction.bitcoinSerialize(), btcBlockWithPmtHeight, pmtWithTransactions.bitcoinSerialize()); bridgeSupport.save(); @@ -123,12 +116,17 @@ void registerBtcTransaction_forALegacyBtcTransaction_shouldRegisterTheNewUtxoAnd // Assert Optional heightIfBtcTxHashIsAlreadyProcessed = bridgeStorageProvider.getHeightIfBtcTxhashIsAlreadyProcessed(bitcoinTransaction.getHash()); assertTrue(heightIfBtcTxHashIsAlreadyProcessed.isPresent()); - assertEquals(rskExecutionBlock.getNumber(), heightIfBtcTxHashIsAlreadyProcessed.get()); + assertEquals(RSK_EXECUTION_BLOCK_NUMBER, heightIfBtcTxHashIsAlreadyProcessed.get()); + + int outputIndex = 0; + TransactionOutput output = bitcoinTransaction.getOutput(outputIndex); + List expectedFederationUtxos = Collections.singletonList(utxoOf(bitcoinTransaction, output)); assertEquals(expectedFederationUtxos, federationSupport.getActiveFederationBtcUTXOs()); + + co.rsk.core.Coin expectedReceiverBalance = co.rsk.core.Coin.fromBitcoin(output.getValue()); assertEquals(expectedReceiverBalance, repository.getBalance(rskReceiver)); assertLogPegInBtc(); - } @Test @@ -151,12 +149,12 @@ void registerBtc_forARepeatedLegacyBtcTransaction_shouldNotPerformAnyChange() th private static UTXO utxoOf(BtcTransaction bitcoinTransaction, TransactionOutput output) { return new UTXO( - bitcoinTransaction.getHash(), - output.getIndex(), - output.getValue(), - 0, - bitcoinTransaction.isCoinBase(), - output.getScriptPubKey() + bitcoinTransaction.getHash(), + output.getIndex(), + output.getValue(), + 0, + bitcoinTransaction.isCoinBase(), + output.getScriptPubKey() ); } From d956d400bed8199a81cfdc2d1c9c2625f3088ffd Mon Sep 17 00:00:00 2001 From: Julian Len Date: Wed, 18 Dec 2024 12:32:48 -0300 Subject: [PATCH 14/14] fix: fixed the test's name --- .../src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java b/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java index dab40ce610..1f7ed3484c 100644 --- a/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java +++ b/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java @@ -130,7 +130,7 @@ void registerBtcTransaction_forALegacyBtcTransaction_shouldRegisterTheNewUtxoAnd } @Test - void registerBtc_forARepeatedLegacyBtcTransaction_shouldNotPerformAnyChange() throws Exception { + void registerBtcTransaction_forARepeatedLegacyBtcTransaction_shouldNotPerformAnyChange() throws Exception { // Arrange bridgeSupport.registerBtcTransaction(rskTx, bitcoinTransaction.bitcoinSerialize(), btcBlockWithPmtHeight, pmtWithTransactions.bitcoinSerialize()); bridgeSupport.save();