Skip to content

Commit

Permalink
fix: deleted the spy usage and replaced by an actual assert that cert…
Browse files Browse the repository at this point in the history
…ain event was emitted, besides I moved the method to assert an event to BridgeSupportTestUtil
  • Loading branch information
julianlen committed Dec 17, 2024
1 parent 05a7c51 commit 97f2977
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 43 deletions.
47 changes: 12 additions & 35 deletions rskj-core/src/test/java/co/rsk/peg/BridgeSupportSvpTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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, BridgeSupportSvpTest.this.logs);
assertEventWasEmittedWithExpectedData(encodedData, BridgeSupportSvpTest.this.logs);
}

private void assertNoSVPValues() {
Expand Down Expand Up @@ -387,11 +387,11 @@ private void assertJustUpdateCollectionsWasLogged() {

CallTransaction.Function updateCollectionsEvent = BridgeEvents.UPDATE_COLLECTIONS.getEvent();
List<DataWord> encodedTopics = getEncodedTopics(updateCollectionsEvent);
assertEventWasEmittedWithExpectedTopics(encodedTopics);
assertEventWasEmittedWithExpectedTopics(encodedTopics, BridgeSupportSvpTest.this.logs);

String senderData = rskTx.getSender(mock(SignatureCache.class)).toHexString();
byte[] encodedData = getEncodedData(updateCollectionsEvent, senderData);
assertEventWasEmittedWithExpectedData(encodedData);
assertEventWasEmittedWithExpectedData(encodedData, BridgeSupportSvpTest.this.logs);
}
}

Expand Down Expand Up @@ -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, BridgeSupportSvpTest.this.logs);
assertEventWasEmittedWithExpectedData(encodedData, BridgeSupportSvpTest.this.logs);
}

private void assertLogReleaseBtc(Keccak256 rskTxHash, BtcTransaction btcTx) {
Expand All @@ -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, BridgeSupportSvpTest.this.logs);
assertEventWasEmittedWithExpectedData(encodedData, BridgeSupportSvpTest.this.logs);
}

private void assertFederatorSignedInputs(List<TransactionInput> inputs, List<Sha256Hash> sigHashes, BtcECKey key) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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<DataWord> 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<DataWord> expectedTopics) {
Optional<LogInfo> topicOpt = logs.stream()
.filter(log -> log.getTopics().equals(expectedTopics))
.findFirst();
assertTrue(topicOpt.isPresent());
}

private void assertEventWasEmittedWithExpectedData(byte[] expectedData) {
Optional<LogInfo> data = logs.stream()
.filter(log -> Arrays.equals(log.getData(), expectedData))
.findFirst();
assertTrue(data.isPresent());
assertEventWasEmittedWithExpectedTopics(encodedTopics, logs);
assertEventWasEmittedWithExpectedData(encodedData, logs);
}
}
32 changes: 27 additions & 5 deletions rskj-core/src/test/java/co/rsk/peg/BridgeSupportTestUtil.java
Original file line number Diff line number Diff line change
@@ -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.*;
Expand All @@ -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() {
Expand Down Expand Up @@ -105,4 +104,27 @@ public static Block getRskExecutionBlock() {
.build();
return Block.createBlockFromHeader(blockHeader, true);
}

public static List<DataWord> 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<DataWord> expectedTopics, List<LogInfo> logs) {
Optional<LogInfo> topicOpt = logs.stream()
.filter(log -> log.getTopics().equals(expectedTopics))
.findFirst();
assertTrue(topicOpt.isPresent());
}

public static void assertEventWasEmittedWithExpectedData(byte[] expectedData, List<LogInfo> logs) {
Optional<LogInfo> data = logs.stream()
.filter(log -> Arrays.equals(log.getData(), expectedData))
.findFirst();
assertTrue(data.isPresent());
}
}
19 changes: 16 additions & 3 deletions rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -49,6 +50,7 @@ class RegisterBtcTransactionIT {
private RskAddress rskReceiver;
private BridgeSupport bridgeSupport;
private BridgeEventLoggerImpl bridgeEventLogger;
private ArrayList<LogInfo> logs;


@BeforeEach
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -125,7 +128,8 @@ void whenRegisterALegacyBtcTransaction_shouldRegisterTheNewUtxoAndTransferTheRbt
assertEquals(expectedFederationUtxos, federationSupport.getActiveFederationBtcUTXOs());
assertEquals(expectedReceiverBalance, repository.getBalance(rskReceiver));

verify(bridgeEventLogger, times(1)).logPeginBtc(rskReceiver, bitcoinTransaction, minimumPeginValue, 0);
// verify(bridgeEventLogger, times(1)).logPeginBtc(rskReceiver, bitcoinTransaction, minimumPeginValue, 0);
assertLogPegInBtc();

}

Expand Down Expand Up @@ -165,4 +169,13 @@ private BtcTransaction createPegInTransaction(Address federationAddress, Coin co

return btcTx;
}

private void assertLogPegInBtc() {
Sha256Hash peginTransactionHash = bitcoinTransaction.getHash();
List<DataWord> 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);
}
}

0 comments on commit 97f2977

Please sign in to comment.