Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Register twice the same transaction, the second time should not modify any state #2893

Open
wants to merge 14 commits into
base: pegin-tests-integration
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 13 additions & 40 deletions rskj-core/src/test/java/co/rsk/peg/BridgeSupportSvpTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -247,8 +243,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() {
Expand Down Expand Up @@ -387,11 +383,11 @@ private void assertJustUpdateCollectionsWasLogged() {

CallTransaction.Function updateCollectionsEvent = BridgeEvents.UPDATE_COLLECTIONS.getEvent();
List<DataWord> 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);
}
}

Expand Down Expand Up @@ -924,8 +920,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) {
Expand All @@ -935,8 +931,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<TransactionInput> inputs, List<Sha256Hash> sigHashes, BtcECKey key) {
Expand Down Expand Up @@ -1402,8 +1398,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 +1411,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);
}
}
39 changes: 30 additions & 9 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,12 +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.core.Block;
import org.ethereum.core.BlockHeader;
import org.ethereum.core.BlockHeaderBuilder;
import org.ethereum.core.Repository;
import org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest;
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 @@ -95,13 +95,34 @@ public static FederationStorageProvider createFederationStorageProvider(Reposito
return new FederationStorageProviderImpl(bridgeStorageAccessor);
}

public static Block getRskExecutionBlock() {
long rskExecutionBlockNumber = 1000L;
long rskExecutionBlockTimestamp = 10L;
BlockHeader blockHeader = new BlockHeaderBuilder(mock(ActivationConfig.class))
public static Block getRskExecutionBlock(long rskExecutionBlockNumber, long rskExecutionBlockTimestamp) {
BlockHeader blockHeader = new BlockHeaderBuilder(ActivationConfigsForTest.all())
.setNumber(rskExecutionBlockNumber)
.setTimestamp(rskExecutionBlockTimestamp)
.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());
}
}
Loading
Loading