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

Refactor Bridge related tests #2706

Merged
merged 17 commits into from
Sep 12, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@

public class FederationStorageProviderImpl implements FederationStorageProvider {
private final StorageAccessor bridgeStorageAccessor;
private final HashMap<DataWord, Optional<Integer>> storageVersionEntries;

private List<UTXO> newFederationBtcUTXOs;
private List<UTXO> oldFederationBtcUTXOs;
private Federation newFederation;
private HashMap<DataWord, Optional<Integer>> storageVersionEntries;
private Federation oldFederation;
private boolean shouldSaveOldFederation = false;

Expand Down
10 changes: 10 additions & 0 deletions rskj-core/src/test/java/co/rsk/RskTestUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package co.rsk;

import co.rsk.crypto.Keccak256;
import org.ethereum.crypto.ECKey;
import org.ethereum.crypto.HashUtil;
import java.nio.charset.StandardCharsets;
Expand All @@ -8,6 +9,15 @@
import java.util.stream.Collectors;

public class RskTestUtils {

public static Keccak256 createHash(int nHash) {
byte[] bytes = new byte[32];
bytes[0] = (byte) (nHash & 0xFF);
bytes[1] = (byte) (nHash >>8 & 0xFF);

return new Keccak256(bytes);
}

public static List<ECKey> getEcKeysFromSeeds(String[] seeds) {
return Arrays.stream(seeds)
.map(seed -> seed.getBytes(StandardCharsets.UTF_8))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@
import co.rsk.crypto.Keccak256;
import co.rsk.db.MutableTrieCache;
import co.rsk.db.MutableTrieImpl;
import co.rsk.peg.bitcoin.BitcoinTestUtils;
import co.rsk.peg.bitcoin.CoinbaseInformation;
import co.rsk.peg.bitcoin.SimpleBtcTransaction;
import co.rsk.peg.bitcoin.*;
import co.rsk.peg.constants.*;
import co.rsk.peg.flyover.FlyoverFederationInformation;
import co.rsk.trie.Trie;
Expand All @@ -43,9 +41,7 @@
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.config.blockchain.upgrades.ConsensusRule;
import org.ethereum.config.blockchain.upgrades.*;
import org.ethereum.core.Repository;
import org.ethereum.datasource.HashMapDB;
import org.ethereum.db.MutableRepository;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package co.rsk.peg;

import co.rsk.RskTestUtils;
import co.rsk.peg.bitcoin.BitcoinTestUtils;
import co.rsk.peg.federation.constants.FederationConstants;
import co.rsk.peg.feeperkb.FeePerKbSupport;
import co.rsk.peg.lockingcap.LockingCapSupport;
Expand Down Expand Up @@ -88,7 +90,7 @@ class BridgeSupportAddSignatureTest {
void setUpOnEachTest() {
activationsBeforeForks = ActivationConfigsForTest.genesis().forBlock(0);
activationsAfterForks = ActivationConfigsForTest.all().forBlock(0);
bridgeSupportBuilder = new BridgeSupportBuilder();
bridgeSupportBuilder = BridgeSupportBuilder.builder();
whitelistSupport = mock(WhitelistSupport.class);
lockingCapSupport = mock(LockingCapSupport.class);
}
Expand Down Expand Up @@ -327,7 +329,7 @@ void addSignatureToMissingTransaction() throws Exception {
bridgeSupport.addSignature(
genesisFederation.getBtcPublicKeys().get(0),
null,
createHash().getBytes()
BitcoinTestUtils.createHash(1).getBytes()
);
bridgeSupport.save();

Expand Down Expand Up @@ -355,7 +357,11 @@ void addSignatureFromInvalidFederator() throws Exception {
.withRepository(repository)
.build();

bridgeSupport.addSignature(new BtcECKey(), null, createHash().getBytes());
bridgeSupport.addSignature(
new BtcECKey(),
null,
BitcoinTestUtils.createHash(1).getBytes()
);
bridgeSupport.save();

BridgeStorageProvider provider = new BridgeStorageProvider(repository, bridgeAddress,
Expand Down Expand Up @@ -436,7 +442,7 @@ private void test_addSignature_EventEmitted(boolean rskip326Active, boolean useV

BtcECKey privateKeyToSignWith = REGTEST_FEDERATION_PRIVATE_KEYS.get(indexOfKeyToSignWith);

List derEncodedSigs;
List<byte[]> derEncodedSigs;

if (useValidSignature) {
Script inputScript = btcTx.getInputs().get(0).getScriptSig();
Expand Down Expand Up @@ -719,7 +725,7 @@ void addSignature(ActivationConfig.ForBlock activations, FederationMember federa
Script redeemScript = new Script(program);
Sha256Hash sigHash = btcTx.hashForSignature(0, redeemScript, BtcTransaction.SigHash.ALL, false);
BtcECKey.ECDSASignature sig = privateKeyToSignWith.sign(sigHash);
List derEncodedSigs = Collections.singletonList(sig.encodeToDER());
List<byte[]> derEncodedSigs = Collections.singletonList(sig.encodeToDER());

// Act
bridgeSupport.addSignature(federationMemberToSignWith.getBtcPublicKey(), derEncodedSigs, rskTxHash.getBytes());
Expand All @@ -728,7 +734,13 @@ void addSignature(ActivationConfig.ForBlock activations, FederationMember federa
commonAssertLogs(eventLogs);
assertTopics(3, eventLogs);

assertEvent(eventLogs, 0, BridgeEvents.ADD_SIGNATURE.getEvent(), new Object[]{rskTxHash.getBytes(), expectedRskAddress}, new Object[]{federatorBtcPubKey.getPubKey()});
assertEvent(
eventLogs,
0,
BridgeEvents.ADD_SIGNATURE.getEvent(),
new Object[]{rskTxHash.getBytes(), expectedRskAddress},
new Object[]{federatorBtcPubKey.getPubKey()}
);
}

private static void assertEvent(List<LogInfo> logs, int index, CallTransaction.Function event, Object[] topics, Object[] params) {
Expand Down Expand Up @@ -764,7 +776,7 @@ private void addSignatureFromValidFederator(List<BtcECKey> privateKeysToSignWith
Federation genesisFederation = FederationTestUtils.getGenesisFederation(bridgeRegTestConstants.getFederationConstants());
Repository repository = createRepository();

final Keccak256 keccak256 = PegTestUtils.createHash3();
final Keccak256 keccak256 = RskTestUtils.createHash(1);

Repository track = repository.startTracking();
BridgeStorageProvider provider = new BridgeStorageProvider(track, bridgeAddress, btcRegTestParams, activationsBeforeForks);
Expand Down Expand Up @@ -815,17 +827,17 @@ private void addSignatureFromValidFederator(List<BtcECKey> privateKeysToSignWith
}
byte[] derEncodedSig = sig.encodeToDER();

List derEncodedSigs = new ArrayList();
List<byte[]> derEncodedSigs = new ArrayList<>();
for (int i = 0; i < numberOfInputsToSign; i++) {
derEncodedSigs.add(derEncodedSig);
}
bridgeSupport.addSignature(findPublicKeySignedBy(genesisFederation.getBtcPublicKeys(), privateKeysToSignWith.get(0)), derEncodedSigs, keccak256.getBytes());
if (signTwice) {
// Create another valid signature with the same private key
ECDSASigner signer = new ECDSASigner();
X9ECParameters CURVE_PARAMS = CustomNamedCurves.getByName("secp256k1");
ECDomainParameters CURVE = new ECDomainParameters(CURVE_PARAMS.getCurve(), CURVE_PARAMS.getG(), CURVE_PARAMS.getN(), CURVE_PARAMS.getH());
ECPrivateKeyParameters privKey = new ECPrivateKeyParameters(privateKeysToSignWith.get(0).getPrivKey(), CURVE);
X9ECParameters curveParams = CustomNamedCurves.getByName("secp256k1");
ECDomainParameters curve = new ECDomainParameters(curveParams.getCurve(), curveParams.getG(), curveParams.getN(), curveParams.getH());
ECPrivateKeyParameters privKey = new ECPrivateKeyParameters(privateKeysToSignWith.get(0).getPrivKey(), curve);
signer.init(true, privKey);
BigInteger[] components = signer.generateSignature(sighash.getBytes());
BtcECKey.ECDSASignature sig2 = new BtcECKey.ECDSASignature(components[0], components[1]).toCanonicalised();
Expand All @@ -836,11 +848,15 @@ private void addSignatureFromValidFederator(List<BtcECKey> privateKeysToSignWith
if (privateKeysToSignWith.size() > 1) {
BtcECKey.ECDSASignature sig2 = privateKeysToSignWith.get(1).sign(sighash);
byte[] derEncodedSig2 = sig2.encodeToDER();
List derEncodedSigs2 = new ArrayList();
List<byte[]> derEncodedSigs2 = new ArrayList<>();
for (int i = 0; i < numberOfInputsToSign; i++) {
derEncodedSigs2.add(derEncodedSig2);
}
bridgeSupport.addSignature(findPublicKeySignedBy(genesisFederation.getBtcPublicKeys(), privateKeysToSignWith.get(1)), derEncodedSigs2, keccak256.getBytes());
bridgeSupport.addSignature(
findPublicKeySignedBy(genesisFederation.getBtcPublicKeys(), privateKeysToSignWith.get(1)),
derEncodedSigs2,
keccak256.getBytes()
);
}
bridgeSupport.save();
track.commit();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ void setUpOnEachTest() {
signatureCache = new BlockTxSignatureCache(new ReceivedTxSignatureCache());
when(activations.isActive(ConsensusRule.RSKIP176)).thenReturn(true);
when(activations.isActive(ConsensusRule.RSKIP219)).thenReturn(true);
bridgeSupportBuilder = new BridgeSupportBuilder();
bridgeSupportBuilder = BridgeSupportBuilder.builder();
lockingCapSupport = mock(LockingCapSupport.class);
whitelistSupport = mock(WhitelistSupport.class);
federationSupportBuilder = new FederationSupportBuilder();
federationSupportBuilder = FederationSupportBuilder.builder();
}

private BtcTransaction createBtcTransactionWithOutputToAddress(Coin amount, Address btcAddress) {
Expand Down
Loading
Loading