Skip to content

Commit

Permalink
Merge pull request #2143 from rsksmart/refactor-federation
Browse files Browse the repository at this point in the history
Refactor federation
  • Loading branch information
kelvinator07 authored Oct 6, 2023
2 parents 514ef9f + c8a0ef3 commit 10e3014
Show file tree
Hide file tree
Showing 41 changed files with 558 additions and 611 deletions.
1 change: 1 addition & 0 deletions rskj-core/src/main/java/co/rsk/config/BridgeConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import co.rsk.peg.AddressBasedAuthorizer;
import co.rsk.peg.Federation;
import java.util.List;

import org.ethereum.config.blockchain.upgrades.ActivationConfig;
import org.ethereum.config.blockchain.upgrades.ConsensusRule;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import co.rsk.bitcoinj.core.Coin;
import co.rsk.bitcoinj.core.NetworkParameters;
import co.rsk.peg.AddressBasedAuthorizer;
import co.rsk.peg.Federation;
import co.rsk.peg.FederationMember;
import co.rsk.peg.StandardMultisigFederation;
import org.bouncycastle.util.encoders.Hex;
import org.ethereum.crypto.ECKey;

Expand Down Expand Up @@ -58,7 +58,7 @@ public BridgeDevNetConstants(List<BtcECKey> federationPublicKeys) {

// Expected federation address is:
// 2NCEo1RdmGDj6MqiipD6DUSerSxKv79FNWX
genesisFederation = new Federation(
genesisFederation = new StandardMultisigFederation(
federationMembers,
genesisFederationAddressCreatedAt,
1L,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import co.rsk.bitcoinj.core.Coin;
import co.rsk.bitcoinj.core.NetworkParameters;
import co.rsk.peg.AddressBasedAuthorizer;
import co.rsk.peg.Federation;
import co.rsk.peg.FederationMember;
import co.rsk.peg.StandardMultisigFederation;
import com.google.common.collect.Lists;
import org.bouncycastle.util.encoders.Hex;
import org.ethereum.crypto.ECKey;
Expand Down Expand Up @@ -53,7 +53,7 @@ public class BridgeMainNetConstants extends BridgeConstants {
// Wednesday, January 3, 2018 12:00:00 AM GMT-03:00
Instant genesisFederationAddressCreatedAt = Instant.ofEpochMilli(1514948400l);

genesisFederation = new Federation(
genesisFederation = new StandardMultisigFederation(
federationMembers,
genesisFederationAddressCreatedAt,
1L,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import co.rsk.bitcoinj.core.Coin;
import co.rsk.bitcoinj.core.NetworkParameters;
import co.rsk.peg.AddressBasedAuthorizer;
import co.rsk.peg.Federation;
import co.rsk.peg.FederationMember;
import co.rsk.peg.StandardMultisigFederation;
import org.bouncycastle.util.encoders.Hex;
import org.ethereum.crypto.ECKey;
import org.ethereum.crypto.HashUtil;
Expand Down Expand Up @@ -56,7 +56,7 @@ public BridgeRegTestConstants(List<BtcECKey> federationPublicKeys) {

Instant genesisFederationCreatedAt = ZonedDateTime.parse("2016-01-01T00:00:00Z").toInstant();

genesisFederation = new Federation(
genesisFederation = new StandardMultisigFederation(
federationMembers,
genesisFederationCreatedAt,
1L,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import co.rsk.bitcoinj.core.Coin;
import co.rsk.bitcoinj.core.NetworkParameters;
import co.rsk.peg.AddressBasedAuthorizer;
import co.rsk.peg.Federation;
import co.rsk.peg.FederationMember;
import co.rsk.peg.StandardMultisigFederation;
import org.bouncycastle.util.encoders.Hex;
import org.ethereum.crypto.ECKey;

Expand Down Expand Up @@ -61,7 +61,7 @@ public class BridgeTestNetConstants extends BridgeConstants {
// Currently set to: Monday, October 8, 2018 12:00:00 AM GMT-03:00
Instant genesisFederationAddressCreatedAt = Instant.ofEpochMilli(1538967600l);

genesisFederation = new Federation(
genesisFederation = new StandardMultisigFederation(
federationMembers,
genesisFederationAddressCreatedAt,
1L,
Expand Down
20 changes: 10 additions & 10 deletions rskj-core/src/main/java/co/rsk/peg/BridgeSerializationUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ private static byte[] serializeFederationWithSerializer(Federation federation, F
}

// For the serialization format, see BridgeSerializationUtils::serializeFederationWithSerializer
private static Federation deserializeFederationWithDeserializer(
private static StandardMultisigFederation deserializeStandardMultisigFederationWithDeserializer(
byte[] data,
NetworkParameters networkParameters,
FederationMemberDesserializer federationMemberDesserializer) {
Expand All @@ -273,7 +273,7 @@ private static Federation deserializeFederationWithDeserializer(
federationMembers.add(member);
}

return new Federation(federationMembers, creationTime, creationBlockNumber, networkParameters);
return new StandardMultisigFederation(federationMembers, creationTime, creationBlockNumber, networkParameters);
}

/**
Expand All @@ -287,8 +287,8 @@ public static byte[] serializeFederationOnlyBtcKeys(Federation federation) {
}

// For the serialization format, see BridgeSerializationUtils::serializeFederationOnlyBtcKeys
public static Federation deserializeFederationOnlyBtcKeys(byte[] data, NetworkParameters networkParameters) {
return deserializeFederationWithDeserializer(data, networkParameters,
public static StandardMultisigFederation deserializeStandardMultisigFederationOnlyBtcKeys(byte[] data, NetworkParameters networkParameters) {
return deserializeStandardMultisigFederationWithDeserializer(data, networkParameters,
(pubKeyBytes -> FederationMember.getFederationMemberFromKey(BtcECKey.fromPublicOnly(pubKeyBytes))));
}

Expand All @@ -304,29 +304,29 @@ public static byte[] serializeFederation(Federation federation) {
}

// For the serialization format, see BridgeSerializationUtils::serializeFederation
public static Federation deserializeFederation(
public static StandardMultisigFederation deserializeStandardMultisigFederation(
byte[] data,
NetworkParameters networkParameters
) {
return deserializeFederationWithDeserializer(
return deserializeStandardMultisigFederationWithDeserializer(
data,
networkParameters,
BridgeSerializationUtils::deserializeFederationMember
);
}

public static ErpFederation deserializeErpFederation(
public static LegacyErpFederation deserializeLegacyErpFederation(
byte[] data,
BridgeConstants bridgeConstants,
ActivationConfig.ForBlock activations
) {
Federation federation = deserializeFederationWithDeserializer(
Federation federation = deserializeStandardMultisigFederationWithDeserializer(
data,
bridgeConstants.getBtcParams(),
BridgeSerializationUtils::deserializeFederationMember
);

return new ErpFederation(
return new LegacyErpFederation(
federation.getMembers(),
federation.creationTime,
federation.getCreationBlockNumber(),
Expand All @@ -342,7 +342,7 @@ public static P2shErpFederation deserializeP2shErpFederation(
BridgeConstants bridgeConstants,
ActivationConfig.ForBlock activations
) {
Federation federation = deserializeFederationWithDeserializer(
Federation federation = deserializeStandardMultisigFederationWithDeserializer(
data,
bridgeConstants.getBtcParams(),
BridgeSerializationUtils::deserializeFederationMember
Expand Down
34 changes: 19 additions & 15 deletions rskj-core/src/main/java/co/rsk/peg/BridgeStorageProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@
* @author Oscar Guindzberg
*/
public class BridgeStorageProvider {
private static final int FEDERATION_FORMAT_VERSION_MULTIKEY = 1000;
private static final int ERP_FEDERATION_FORMAT_VERSION = 2000;
private static final int P2SH_ERP_FEDERATION_FORMAT_VERSION = 3000;
protected static final int STANDARD_MULTISIG_FEDERATION_FORMAT_VERSION = 1000;
protected static final int LEGACY_ERP_FEDERATION_FORMAT_VERSION = 2000;
protected static final int P2SH_ERP_FEDERATION_FORMAT_VERSION = 3000;

// Dummy value to use when saved Fast Bridge Derivation Argument Hash
private static final byte FLYOVER_FEDERATION_DERIVATION_HASH_TRUE_VALUE = (byte) 1;
Expand Down Expand Up @@ -336,7 +336,7 @@ public Federation getNewFederation() {
return deserializeFederationAccordingToVersion(data, storageVersion.get(), bridgeConstants);
}

return BridgeSerializationUtils.deserializeFederationOnlyBtcKeys(data, networkParameters);
return BridgeSerializationUtils.deserializeStandardMultisigFederationOnlyBtcKeys(data, networkParameters);
}
);

Expand Down Expand Up @@ -364,15 +364,15 @@ public void saveNewFederation() {
NEW_FEDERATION_FORMAT_VERSION.getKey(),
P2SH_ERP_FEDERATION_FORMAT_VERSION
);
} else if (activations.isActive(RSKIP201) && newFederation instanceof ErpFederation) {
} else if (activations.isActive(RSKIP201) && newFederation instanceof LegacyErpFederation) {
saveStorageVersion(
NEW_FEDERATION_FORMAT_VERSION.getKey(),
ERP_FEDERATION_FORMAT_VERSION
LEGACY_ERP_FEDERATION_FORMAT_VERSION
);
} else {
saveStorageVersion(
NEW_FEDERATION_FORMAT_VERSION.getKey(),
FEDERATION_FORMAT_VERSION_MULTIKEY
STANDARD_MULTISIG_FEDERATION_FORMAT_VERSION
);
}
serializer = BridgeSerializationUtils::serializeFederation;
Expand All @@ -398,7 +398,7 @@ public Federation getOldFederation() {
return deserializeFederationAccordingToVersion(data, storageVersion.get(), bridgeConstants);
}

return BridgeSerializationUtils.deserializeFederationOnlyBtcKeys(data, networkParameters);
return BridgeSerializationUtils.deserializeStandardMultisigFederationOnlyBtcKeys(data, networkParameters);
}
);

Expand Down Expand Up @@ -428,12 +428,12 @@ protected void saveOldFederation() {
} else if (activations.isActive(RSKIP201) && oldFederation instanceof ErpFederation) {
saveStorageVersion(
OLD_FEDERATION_FORMAT_VERSION.getKey(),
ERP_FEDERATION_FORMAT_VERSION
LEGACY_ERP_FEDERATION_FORMAT_VERSION
);
} else {
saveStorageVersion(
OLD_FEDERATION_FORMAT_VERSION.getKey(),
FEDERATION_FORMAT_VERSION_MULTIKEY
STANDARD_MULTISIG_FEDERATION_FORMAT_VERSION
);
}

Expand Down Expand Up @@ -480,7 +480,7 @@ public void savePendingFederation() {
RepositorySerializer<PendingFederation> serializer = BridgeSerializationUtils::serializePendingFederationOnlyBtcKeys;

if (activations.isActive(RSKIP123)) {
saveStorageVersion(PENDING_FEDERATION_FORMAT_VERSION.getKey(), FEDERATION_FORMAT_VERSION_MULTIKEY);
saveStorageVersion(PENDING_FEDERATION_FORMAT_VERSION.getKey(), STANDARD_MULTISIG_FEDERATION_FORMAT_VERSION);
serializer = BridgeSerializationUtils::serializePendingFederation;
}

Expand Down Expand Up @@ -1028,8 +1028,8 @@ private Federation deserializeFederationAccordingToVersion(
BridgeConstants bridgeConstants
) {
switch (version) {
case ERP_FEDERATION_FORMAT_VERSION:
return BridgeSerializationUtils.deserializeErpFederation(
case LEGACY_ERP_FEDERATION_FORMAT_VERSION:
return BridgeSerializationUtils.deserializeLegacyErpFederation(
data,
bridgeConstants,
activations
Expand All @@ -1040,9 +1040,13 @@ private Federation deserializeFederationAccordingToVersion(
bridgeConstants,
activations
);
case STANDARD_MULTISIG_FEDERATION_FORMAT_VERSION:
return BridgeSerializationUtils.deserializeStandardMultisigFederation(
data,
networkParameters
);
default:
// Assume this is the multi-key version
return BridgeSerializationUtils.deserializeFederation(data, networkParameters);
throw new IllegalArgumentException("Unknown Federation version: " + version);
}
}

Expand Down
2 changes: 1 addition & 1 deletion rskj-core/src/main/java/co/rsk/peg/BridgeSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -2079,7 +2079,7 @@ protected Integer commitFederation(boolean dryRun, Keccak256 hash) throws IOExce
// Preserve federation change info
long nextFederationCreationBlockHeight = rskExecutionBlock.getNumber();
provider.setNextFederationCreationBlockHeight(nextFederationCreationBlockHeight);
Script oldFederationP2SHScript = activations.isActive(RSKIP377)? oldFederation.getStandardP2SHScript(): oldFederation.getP2SHScript();
Script oldFederationP2SHScript = activations.isActive(RSKIP377)? ((ErpFederation) oldFederation).getStandardP2SHScript(): oldFederation.getP2SHScript();
provider.setLastRetiredFederationP2SHScript(oldFederationP2SHScript);
}

Expand Down
13 changes: 10 additions & 3 deletions rskj-core/src/main/java/co/rsk/peg/BridgeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,10 @@ public static boolean isValidPegInTx(
throw new ScriptException(message);
}
Script inputStandardRedeemScript = redeemScriptParser.extractStandardRedeemScript();
if (activeFederations.stream().anyMatch(federation -> federation.getStandardRedeemScript().equals(inputStandardRedeemScript))) {
if (activeFederations.stream().anyMatch(federation ->
(federation instanceof ErpFederation ? ((ErpFederation) federation).getStandardRedeemScript()
: federation.getRedeemScript()
).equals(inputStandardRedeemScript))) {
return false;
}

Expand Down Expand Up @@ -490,7 +493,10 @@ private static boolean isPegOutTx(BtcTransaction tx, Federation federation, Acti
}

public static boolean isPegOutTx(BtcTransaction tx, List<Federation> federations, ActivationConfig.ForBlock activations) {
return isPegOutTx(tx, activations, federations.stream().filter(Objects::nonNull).map(Federation::getStandardP2SHScript).toArray(Script[]::new));
return isPegOutTx(tx, activations, federations.stream().filter(Objects::nonNull).map(fed ->
fed instanceof ErpFederation ? ((ErpFederation) fed).getStandardP2SHScript()
: fed.getP2SHScript()
).toArray(Script[]::new));
}

public static boolean isPegOutTx(BtcTransaction tx, ActivationConfig.ForBlock activations, Script... p2shScript) {
Expand All @@ -515,7 +521,8 @@ public static boolean isPegOutTx(BtcTransaction tx, ActivationConfig.ForBlock ac
}

Script outputScript = ScriptBuilder.createP2SHOutputScript(redeemScript);
if (Stream.of(p2shScript).anyMatch(federationPayScript -> federationPayScript.equals(outputScript))) {
if (Stream.of(p2shScript).anyMatch(federationPayScript ->
federationPayScript.equals(outputScript))) {
return true;
}
}
Expand Down
Loading

0 comments on commit 10e3014

Please sign in to comment.