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); + } }