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

Federation support refactor integration #296

Merged
merged 2 commits into from
Aug 9, 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
2 changes: 1 addition & 1 deletion src/main/java/co/rsk/federate/FedNodeRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ private void startFederate() throws Exception {
// btc to rsk client upon federation changes
FederationProvider federationProvider = new FederationProviderFromFederatorSupport(
federatorSupport,
bridgeConstants
bridgeConstants.getFederationConstants()
);

BtcLockSenderProvider btcLockSenderProvider = new BtcLockSenderProvider();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import co.rsk.bitcoinj.core.Address;
import co.rsk.bitcoinj.core.BtcECKey;
import co.rsk.bitcoinj.core.NetworkParameters;
import co.rsk.peg.constants.BridgeConstants;
import co.rsk.peg.federation.*;
import co.rsk.peg.federation.constants.FederationConstants;
import org.ethereum.config.blockchain.upgrades.ActivationConfig;
import org.ethereum.crypto.ECKey;

Expand All @@ -41,11 +41,14 @@
*/
public class FederationProviderFromFederatorSupport implements FederationProvider {
private final FederatorSupport federatorSupport;
private final BridgeConstants bridgeConstants;
private final FederationConstants federationConstants;

public FederationProviderFromFederatorSupport(
FederatorSupport federatorSupport,
FederationConstants federationConstants) {

public FederationProviderFromFederatorSupport(FederatorSupport federatorSupport, BridgeConstants bridgeConstants) {
this.federatorSupport = federatorSupport;
this.bridgeConstants = bridgeConstants;
this.federationConstants = federationConstants;
}

@Override
Expand Down Expand Up @@ -153,8 +156,8 @@ private Federation getExpectedFederation(Federation initialFederation, Address e
Instant creationTime = initialFederation.getCreationTime();
long creationBlockNumber = initialFederation.getCreationBlockNumber();
NetworkParameters btcParams = federatorSupport.getBtcParams();
List<BtcECKey> erpPubKeys = bridgeConstants.getErpFedPubKeysList();
long activationDelay = bridgeConstants.getErpFedActivationDelay();
List<BtcECKey> erpPubKeys = federationConstants.getErpFedPubKeysList();
long activationDelay = federationConstants.getErpFedActivationDelay();
ActivationConfig.ForBlock activations = federatorSupport.getConfigForBestBlock();

FederationArgs federationArgs =
Expand Down
10 changes: 4 additions & 6 deletions src/test/java/co/rsk/federate/BtcToRskClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,29 +95,27 @@
* Created by ajlopez on 6/1/2016.
*/
class BtcToRskClientTest {
private final NetworkParameters networkParameters = ThinConverter.toOriginalInstance(BridgeRegTestConstants.getInstance().getBtcParamsString());

private int nhash = 0;
private ActivationConfig activationConfig;
private BridgeConstants bridgeRegTestConstants;
private Federation activeFederation;
private FederationMember fakeMember;
private BtcToRskClientBuilder btcToRskClientBuilder;
private List<BtcECKey> federationPrivateKeys;
private NetworkParameters networkParameters;

@BeforeEach
void setup() throws PeginInstructionsException, IOException {
activationConfig = mock(ActivationConfig.class);
when(activationConfig.forBlock(anyLong())).thenReturn(mock(ActivationConfig.ForBlock.class));
when(activationConfig.isActive(eq(ConsensusRule.RSKIP89), anyLong())).thenReturn(true);

bridgeRegTestConstants = BridgeRegTestConstants.getInstance();
bridgeRegTestConstants = new BridgeRegTestConstants();
networkParameters = ThinConverter.toOriginalInstance(bridgeRegTestConstants.getBtcParamsString());
federationPrivateKeys = TestUtils.getFederationPrivateKeys(9);
activeFederation = TestUtils.createFederation(bridgeRegTestConstants.getBtcParams(), federationPrivateKeys);
fakeMember = FederationMember.getFederationMemberFromKey(
BtcECKey.fromPrivate(
HashUtil.keccak256("00".getBytes(StandardCharsets.UTF_8))
)
BtcECKey.fromPrivate(HashUtil.keccak256("00".getBytes(StandardCharsets.UTF_8)))
);
btcToRskClientBuilder = new BtcToRskClientBuilder();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
import co.rsk.bitcoinj.core.BtcECKey;
import co.rsk.bitcoinj.core.NetworkParameters;
import co.rsk.bitcoinj.script.Script;
import co.rsk.peg.constants.BridgeConstants;
import co.rsk.peg.constants.BridgeTestNetConstants;
import co.rsk.peg.federation.*;

import co.rsk.peg.federation.constants.FederationConstants;
import co.rsk.peg.federation.constants.FederationTestNetConstants;
import java.math.BigInteger;
import java.time.Instant;
import java.util.Arrays;
Expand All @@ -32,7 +32,7 @@
class FederationProviderFromFederatorSupportTest {
private FederatorSupport federatorSupportMock;
private FederationProvider federationProvider;
private BridgeConstants bridgeConstants;
private FederationConstants federationConstants;
private NetworkParameters testnetParams;
private Instant creationTime;

Expand All @@ -50,11 +50,11 @@ class FederationProviderFromFederatorSupportTest {

@BeforeEach
void createProvider() {
bridgeConstants = BridgeTestNetConstants.getInstance();
federationConstants = FederationTestNetConstants.getInstance();
federatorSupportMock = mock(FederatorSupport.class);
federationProvider = new FederationProviderFromFederatorSupport(
federatorSupportMock,
bridgeConstants
federationConstants
);
testnetParams = NetworkParameters.fromID(NetworkParameters.ID_TESTNET);
creationTime = Instant.ofEpochMilli(5005L);
Expand Down Expand Up @@ -808,19 +808,23 @@ private Federation createFederation(List<FederationMember> members) {
}

private ErpFederation createNonStandardErpFederation(List<FederationMember> members, ActivationConfig.ForBlock activations) {
List<BtcECKey> erpPubKeys = bridgeConstants.getErpFedPubKeysList();
long activationDelay = bridgeConstants.getErpFedActivationDelay();
List<BtcECKey> erpPubKeys = federationConstants.getErpFedPubKeysList();
long activationDelay = federationConstants.getErpFedActivationDelay();
FederationArgs federationArgs =
new FederationArgs(members, creationTime, 0L, testnetParams);

return FederationFactory.buildNonStandardErpFederation(federationArgs, erpPubKeys, activationDelay, activations);
}

private ErpFederation createP2shErpFederation(List<FederationMember> members) {
List<BtcECKey> erpPubKeys = bridgeConstants.getErpFedPubKeysList();
long activationDelay = bridgeConstants.getErpFedActivationDelay();
FederationArgs federationArgs =
new FederationArgs(members, creationTime, 0L, testnetParams);
List<BtcECKey> erpPubKeys = federationConstants.getErpFedPubKeysList();
long activationDelay = federationConstants.getErpFedActivationDelay();
FederationArgs federationArgs = new FederationArgs(
members,
creationTime,
0L,
testnetParams
);

return FederationFactory.buildP2shErpFederation(federationArgs, erpPubKeys, activationDelay);
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/co/rsk/federate/FederatorSupportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

class FederatorSupportTest {

private final NetworkParameters networkParameters = ThinConverter.toOriginalInstance(BridgeRegTestConstants.getInstance().getBtcParamsString());
private final NetworkParameters networkParameters = ThinConverter.toOriginalInstance(new BridgeRegTestConstants().getBtcParamsString());

@Test
void sendReceiveHeadersSendsBlockHeaders() {
Expand Down
18 changes: 8 additions & 10 deletions src/test/java/co/rsk/federate/adapter/ThinConverterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,16 @@ class ThinConverterTest {

private static int nhash = 0;

public static org.bitcoinj.core.Sha256Hash createOriginalHash() {
private org.bitcoinj.core.Sha256Hash createOriginalHash() {
byte[] bytes = new byte[32];
bytes[0] = (byte) nhash++;
org.bitcoinj.core.Sha256Hash hash = org.bitcoinj.core.Sha256Hash.wrap(bytes);
return hash;
return org.bitcoinj.core.Sha256Hash.wrap(bytes);
}

public static co.rsk.bitcoinj.core.Sha256Hash createThinHash() {
private co.rsk.bitcoinj.core.Sha256Hash createThinHash() {
byte[] bytes = new byte[32];
bytes[0] = (byte) nhash++;
co.rsk.bitcoinj.core.Sha256Hash hash = co.rsk.bitcoinj.core.Sha256Hash.wrap(bytes);
return hash;
return co.rsk.bitcoinj.core.Sha256Hash.wrap(bytes);
}

@Test
Expand All @@ -34,12 +32,12 @@ void toThinInstanceStoredBlock() {
int height = 200;
org.bitcoinj.core.Block originalBlock = new org.bitcoinj.core.Block(params, 1, createOriginalHash(), createOriginalHash(), 1000, 2000, 3000, new ArrayList<>());
org.bitcoinj.core.StoredBlock originalStoredBlock = new org.bitcoinj.core.StoredBlock(originalBlock, chainWork, height);
co.rsk.bitcoinj.core.StoredBlock thinStoredBlock = ThinConverter.toThinInstance(originalStoredBlock, BridgeRegTestConstants.getInstance());
co.rsk.bitcoinj.core.StoredBlock thinStoredBlock = ThinConverter.toThinInstance(originalStoredBlock, new BridgeRegTestConstants());
assertEquals(chainWork, thinStoredBlock.getChainWork());
assertEquals(height, thinStoredBlock.getHeight());
assertArrayEquals(originalBlock.bitcoinSerialize(), thinStoredBlock.getHeader().bitcoinSerialize());

assertNull(ThinConverter.toThinInstance(null, BridgeRegTestConstants.getInstance()));
assertNull(ThinConverter.toThinInstance(null, new BridgeRegTestConstants()));
}

@Test
Expand All @@ -49,12 +47,12 @@ void toOriginalInstanceStoredBlock() {
int height = 200;
co.rsk.bitcoinj.core.BtcBlock thinBlock = new co.rsk.bitcoinj.core.BtcBlock(params, 1, createThinHash(), createThinHash(), 1000, 2000, 3000, new ArrayList<>());
co.rsk.bitcoinj.core.StoredBlock thinStoredBlock = new co.rsk.bitcoinj.core.StoredBlock(thinBlock, chainWork, height);
org.bitcoinj.core.StoredBlock originalStoredBlock = ThinConverter.toOriginalInstance(thinStoredBlock, BridgeRegTestConstants.getInstance());
org.bitcoinj.core.StoredBlock originalStoredBlock = ThinConverter.toOriginalInstance(thinStoredBlock, new BridgeRegTestConstants());
assertEquals(chainWork, originalStoredBlock.getChainWork());
assertEquals(height, originalStoredBlock.getHeight());
assertArrayEquals(thinBlock.bitcoinSerialize(), originalStoredBlock.getHeader().bitcoinSerialize());

assertNull(ThinConverter.toOriginalInstance(null, BridgeRegTestConstants.getInstance()));
assertNull(ThinConverter.toOriginalInstance(null, new BridgeRegTestConstants()));
}

@Test
Expand Down
5 changes: 2 additions & 3 deletions src/test/java/co/rsk/federate/bitcoin/KitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,12 @@

class KitTest {
private static BridgeConstants bridgeConstants;
private static NetworkParameters networkParameters;
private static Context btcContext;

@BeforeEach
void setUpBeforeClass() {
bridgeConstants = BridgeRegTestConstants.getInstance();
networkParameters = ThinConverter.toOriginalInstance(bridgeConstants.getBtcParamsString());
bridgeConstants = new BridgeRegTestConstants();
NetworkParameters networkParameters = ThinConverter.toOriginalInstance(bridgeConstants.getBtcParamsString());
btcContext = new Context(networkParameters);
}

Expand Down
Loading
Loading