Skip to content

Commit

Permalink
Merge pull request #2231 from rsksmart/rebase-federations-new-refacto…
Browse files Browse the repository at this point in the history
…r-integration

Rebase federations new refactor integration
  • Loading branch information
josedahlquist authored Jan 31, 2024
2 parents 5395060 + 376b627 commit 471dc02
Show file tree
Hide file tree
Showing 97 changed files with 4,527 additions and 3,436 deletions.
2 changes: 1 addition & 1 deletion rskj-core/src/main/java/co/rsk/config/BridgeConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
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.federation.Federation;
import java.util.List;

import org.ethereum.config.blockchain.upgrades.ActivationConfig;
Expand Down
13 changes: 5 additions & 8 deletions rskj-core/src/main/java/co/rsk/config/BridgeDevNetConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
import co.rsk.bitcoinj.core.Coin;
import co.rsk.bitcoinj.core.NetworkParameters;
import co.rsk.peg.AddressBasedAuthorizer;
import co.rsk.peg.FederationMember;
import co.rsk.peg.StandardMultisigFederation;
import co.rsk.peg.federation.FederationArgs;
import co.rsk.peg.federation.FederationMember;
import co.rsk.peg.federation.FederationFactory;
import java.time.Instant;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -57,12 +58,8 @@ public BridgeDevNetConstants(List<BtcECKey> federationPublicKeys) {

// Expected federation address is:
// 2NCEo1RdmGDj6MqiipD6DUSerSxKv79FNWX
genesisFederation = new StandardMultisigFederation(
federationMembers,
genesisFederationAddressCreatedAt,
1L,
getBtcParams()
);
FederationArgs federationArgs = new FederationArgs(federationMembers, genesisFederationAddressCreatedAt, 1L, getBtcParams());
genesisFederation = FederationFactory.buildStandardMultiSigFederation(federationArgs);

btc2RskMinimumAcceptableConfirmations = 1;
btc2RskMinimumAcceptableConfirmationsOnRsk = 10;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import co.rsk.bitcoinj.core.Coin;
import co.rsk.bitcoinj.core.NetworkParameters;
import co.rsk.peg.AddressBasedAuthorizer;
import co.rsk.peg.FederationMember;
import co.rsk.peg.StandardMultisigFederation;
import co.rsk.peg.federation.FederationArgs;
import co.rsk.peg.federation.FederationMember;
import co.rsk.peg.federation.FederationFactory;
import com.google.common.collect.Lists;
import java.time.Instant;
import java.util.Arrays;
Expand Down Expand Up @@ -52,12 +53,8 @@ public class BridgeMainNetConstants extends BridgeConstants {
// Wednesday, January 3, 2018 12:00:00 AM GMT-03:00
Instant genesisFederationAddressCreatedAt = Instant.ofEpochMilli(1514948400L);

genesisFederation = new StandardMultisigFederation(
federationMembers,
genesisFederationAddressCreatedAt,
1L,
getBtcParams()
);
FederationArgs federationArgs = new FederationArgs(federationMembers, genesisFederationAddressCreatedAt, 1L, getBtcParams());
genesisFederation = FederationFactory.buildStandardMultiSigFederation(federationArgs);

btc2RskMinimumAcceptableConfirmations = 100;
btc2RskMinimumAcceptableConfirmationsOnRsk = 1000;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
import co.rsk.bitcoinj.core.Coin;
import co.rsk.bitcoinj.core.NetworkParameters;
import co.rsk.peg.AddressBasedAuthorizer;
import co.rsk.peg.FederationMember;
import co.rsk.peg.StandardMultisigFederation;
import co.rsk.peg.federation.FederationArgs;
import co.rsk.peg.federation.FederationMember;
import co.rsk.peg.federation.FederationFactory;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.time.ZonedDateTime;
Expand Down Expand Up @@ -55,12 +56,8 @@ public BridgeRegTestConstants(List<BtcECKey> federationPublicKeys) {

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

genesisFederation = new StandardMultisigFederation(
federationMembers,
genesisFederationCreatedAt,
1L,
getBtcParams()
);
FederationArgs federationArgs = new FederationArgs(federationMembers, genesisFederationCreatedAt, 1L, getBtcParams());
genesisFederation = FederationFactory.buildStandardMultiSigFederation(federationArgs);

btc2RskMinimumAcceptableConfirmations = 3;
btc2RskMinimumAcceptableConfirmationsOnRsk = 5;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
import co.rsk.bitcoinj.core.Coin;
import co.rsk.bitcoinj.core.NetworkParameters;
import co.rsk.peg.AddressBasedAuthorizer;
import co.rsk.peg.FederationMember;
import co.rsk.peg.StandardMultisigFederation;
import co.rsk.peg.federation.FederationArgs;
import co.rsk.peg.federation.FederationMember;
import co.rsk.peg.federation.FederationFactory;
import java.time.Instant;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -65,12 +66,8 @@ 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 StandardMultisigFederation(
federationMembers,
genesisFederationAddressCreatedAt,
1L,
getBtcParams()
);
FederationArgs federationArgs = new FederationArgs(federationMembers, genesisFederationAddressCreatedAt, 1L, getBtcParams());
genesisFederation = FederationFactory.buildStandardMultiSigFederation(federationArgs);

btc2RskMinimumAcceptableConfirmations = 10;
btc2RskMinimumAcceptableConfirmationsOnRsk = 10;
Expand Down
2 changes: 2 additions & 0 deletions rskj-core/src/main/java/co/rsk/peg/Bridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import co.rsk.crypto.Keccak256;
import co.rsk.panic.PanicProcessor;
import co.rsk.peg.bitcoin.MerkleBranch;
import co.rsk.peg.federation.Federation;
import co.rsk.peg.federation.FederationMember;
import co.rsk.peg.flyover.FlyoverTxResponseCodes;
import co.rsk.peg.utils.BtcTransactionFormatUtils;
import co.rsk.peg.whitelist.LockWhitelistEntry;
Expand Down
1 change: 1 addition & 0 deletions rskj-core/src/main/java/co/rsk/peg/BridgeBtcWallet.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import co.rsk.bitcoinj.core.Context;
import co.rsk.bitcoinj.wallet.RedeemData;
import co.rsk.bitcoinj.wallet.Wallet;
import co.rsk.peg.federation.Federation;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
Expand Down
163 changes: 21 additions & 142 deletions rskj-core/src/main/java/co/rsk/peg/BridgeSerializationUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
import co.rsk.core.RskAddress;
import co.rsk.crypto.Keccak256;
import co.rsk.peg.bitcoin.CoinbaseInformation;
import co.rsk.peg.federation.*;
import co.rsk.peg.flyover.FlyoverFederationInformation;
import co.rsk.peg.whitelist.OneOffWhiteListEntry;
import co.rsk.peg.whitelist.UnlimitedWhiteListEntry;
import org.apache.commons.lang3.tuple.Pair;
import org.bouncycastle.util.BigIntegers;
import org.ethereum.config.blockchain.upgrades.ActivationConfig;
import org.ethereum.crypto.ECKey;
import org.ethereum.util.RLP;
import org.ethereum.util.RLPElement;
import org.ethereum.util.RLPList;
Expand All @@ -55,11 +55,6 @@ public class BridgeSerializationUtils {
private static final int FEDERATION_CREATION_BLOCK_NUMBER_INDEX = 1;
private static final int FEDERATION_MEMBERS_INDEX = 2;

private static final int FEDERATION_MEMBER_LIST_SIZE = 3;
private static final int FEDERATION_MEMBER_BTC_KEY_INDEX = 0;
private static final int FEDERATION_MEMBER_RSK_KEY_INDEX = 1;
private static final int FEDERATION_MEMBER_MST_KEY_INDEX = 2;

private BridgeSerializationUtils() {
throw new IllegalAccessError("Utility class, do not instantiate it");
}
Expand Down Expand Up @@ -273,7 +268,10 @@ private static StandardMultisigFederation deserializeStandardMultisigFederationW
federationMembers.add(member);
}

return new StandardMultisigFederation(federationMembers, creationTime, creationBlockNumber, networkParameters);
FederationArgs federationArgs = new FederationArgs(federationMembers, creationTime, creationBlockNumber, networkParameters);
return FederationFactory.buildStandardMultiSigFederation(
federationArgs
);
}

/**
Expand All @@ -299,7 +297,7 @@ public static StandardMultisigFederation deserializeStandardMultisigFederationOn
public static byte[] serializeFederation(Federation federation) {
return serializeFederationWithSerializer(
federation,
BridgeSerializationUtils::serializeFederationMember
FederationMember::serialize
);
}

Expand All @@ -311,134 +309,42 @@ public static StandardMultisigFederation deserializeStandardMultisigFederation(
return deserializeStandardMultisigFederationWithDeserializer(
data,
networkParameters,
BridgeSerializationUtils::deserializeFederationMember
FederationMember::deserialize
);
}

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

return new LegacyErpFederation(
federation.getMembers(),
federation.creationTime,
federation.getCreationBlockNumber(),
federation.getBtcParams(),
bridgeConstants.getErpFedPubKeysList(),
bridgeConstants.getErpFedActivationDelay(),
activations
);
FederationArgs federationArgs = federation.getArgs();
List<BtcECKey> erpPubKeys = bridgeConstants.getErpFedPubKeysList();
long activationDelay = bridgeConstants.getErpFedActivationDelay();

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

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

return new P2shErpFederation(
federation.getMembers(),
federation.creationTime,
federation.getCreationBlockNumber(),
federation.getBtcParams(),
bridgeConstants.getErpFedPubKeysList(),
bridgeConstants.getErpFedActivationDelay(),
activations
);
}

/**
* A FederationMember is serialized as a list in the following order:
* - BTC public key
* - RSK public key
* - MST public key
* All keys are stored in their COMPRESSED versions.
*/
public static byte[] serializeFederationMember(FederationMember federationMember) {
byte[][] rlpElements = new byte[FEDERATION_MEMBER_LIST_SIZE][];
rlpElements[FEDERATION_MEMBER_BTC_KEY_INDEX] = RLP.encodeElement(
federationMember.getBtcPublicKey().getPubKeyPoint().getEncoded(true)
FederationMember::deserialize
);
rlpElements[FEDERATION_MEMBER_RSK_KEY_INDEX] = RLP.encodeElement(federationMember.getRskPublicKey().getPubKey(true));
rlpElements[FEDERATION_MEMBER_MST_KEY_INDEX] = RLP.encodeElement(federationMember.getMstPublicKey().getPubKey(true));
return RLP.encodeList(rlpElements);
}

// For the serialization format, see BridgeSerializationUtils::serializeFederationMember
private static FederationMember deserializeFederationMember(byte[] data) {
RLPList rlpList = (RLPList)RLP.decode2(data).get(0);

if (rlpList.size() != FEDERATION_RLP_LIST_SIZE) {
throw new RuntimeException(String.format("Invalid serialized FederationMember. Expected %d elements but got %d", FEDERATION_MEMBER_LIST_SIZE, rlpList.size()));
}

BtcECKey btcKey = BtcECKey.fromPublicOnly(rlpList.get(FEDERATION_MEMBER_BTC_KEY_INDEX).getRLPData());
ECKey rskKey = ECKey.fromPublicOnly(rlpList.get(FEDERATION_MEMBER_RSK_KEY_INDEX).getRLPData());
ECKey mstKey = ECKey.fromPublicOnly(rlpList.get(FEDERATION_MEMBER_MST_KEY_INDEX).getRLPData());

return new FederationMember(btcKey, rskKey, mstKey);
}

/**
* A pending federation is serialized as the
* public keys conforming it.
* This is a legacy format for blocks before the Wasabi
* network upgrade.
* See BridgeSerializationUtils::serializeBtcPublicKeys
*/
public static byte[] serializePendingFederationOnlyBtcKeys(PendingFederation pendingFederation) {
return serializeBtcPublicKeys(pendingFederation.getBtcPublicKeys());
}

// For the serialization format, see BridgeSerializationUtils::serializePendingFederationOnlyBtcKeys
// and serializePublicKeys::deserializeBtcPublicKeys
public static PendingFederation deserializePendingFederationOnlyBtcKeys(byte[] data) {
// BTC, RSK and MST keys are the same
List<FederationMember> members = deserializeBtcPublicKeys(data).stream().map(pk ->
FederationMember.getFederationMemberFromKey(pk)
).collect(Collectors.toList());
FederationArgs federationArgs = federation.getArgs();
List<BtcECKey> erpPubKeys = bridgeConstants.getErpFedPubKeysList();
long activationDelay = bridgeConstants.getErpFedActivationDelay();

return new PendingFederation(members);
}

/**
* A pending federation is serialized as the
* list of its sorted members serialized.
* For the member serialization format, see BridgeSerializationUtils::serializeFederationMember
*/
public static byte[] serializePendingFederation(PendingFederation pendingFederation) {
List<byte[]> encodedMembers = pendingFederation.getMembers().stream()
.sorted(FederationMember.BTC_RSK_MST_PUBKEYS_COMPARATOR)
.map(BridgeSerializationUtils::serializeFederationMember)
.collect(Collectors.toList());
return RLP.encodeList(encodedMembers.toArray(new byte[0][]));
}

// For the serialization format, see BridgeSerializationUtils::serializePendingFederation
public static PendingFederation deserializePendingFederation(byte[] data) {
RLPList rlpList = (RLPList)RLP.decode2(data).get(0);

List<FederationMember> members = new ArrayList<>();

for (int k = 0; k < rlpList.size(); k++) {
RLPElement element = rlpList.get(k);
FederationMember member = deserializeFederationMember(element.getRLPData());
members.add(member);
}

return new PendingFederation(members);
return FederationFactory.buildP2shErpFederation(federationArgs, erpPubKeys, activationDelay);
}

// An ABI call election is serialized as a list of the votes, like so:
Expand Down Expand Up @@ -889,33 +795,6 @@ private static ABICallSpec deserializeABICallSpec(byte[] data) {
return new ABICallSpec(function, arguments);
}

// A list of btc public keys is serialized as
// [pubkey1, pubkey2, ..., pubkeyn], sorted
// using the lexicographical order of the public keys
// (see BtcECKey.PUBKEY_COMPARATOR).
private static byte[] serializeBtcPublicKeys(List<BtcECKey> keys) {
List<byte[]> encodedKeys = keys.stream()
.sorted(BtcECKey.PUBKEY_COMPARATOR)
.map(key -> RLP.encodeElement(key.getPubKey()))
.collect(Collectors.toList());
return RLP.encodeList(encodedKeys.toArray(new byte[0][]));
}

// For the serialization format, see BridgeSerializationUtils::serializePublicKeys
private static List<BtcECKey> deserializeBtcPublicKeys(byte[] data) {
RLPList rlpList = (RLPList)RLP.decode2(data).get(0);

List<BtcECKey> keys = new ArrayList<>();

for (int k = 0; k < rlpList.size(); k++) {
RLPElement element = rlpList.get(k);
BtcECKey key = BtcECKey.fromPublicOnly(element.getRLPData());
keys.add(key);
}

return keys;
}

// A list of voters is serialized as
// [voterBytes1, voterBytes2, ..., voterBytesn], sorted
// using the lexicographical order of the voters' unsigned bytes
Expand Down
Loading

0 comments on commit 471dc02

Please sign in to comment.