Skip to content

Commit

Permalink
Merge pull request #2603 from rsksmart/federation-support-refactor-in…
Browse files Browse the repository at this point in the history
…tegration

Federation support refactor integration
  • Loading branch information
josedahlquist authored Aug 9, 2024
2 parents 925fdf6 + c120672 commit a4700c5
Show file tree
Hide file tree
Showing 135 changed files with 12,196 additions and 8,425 deletions.
26 changes: 13 additions & 13 deletions rskj-core/src/main/java/co/rsk/peg/Bridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -771,26 +771,26 @@ public Long getBtcTxHashProcessedHeight(Object[] args) throws VMException {
public String getFederationAddress(Object[] args) {
logger.trace("getFederationAddress");

return bridgeSupport.getFederationAddress().toBase58();
return bridgeSupport.getActiveFederationAddress().toBase58();
}

public Integer getFederationSize(Object[] args) {
logger.trace("getFederationSize");

return bridgeSupport.getFederationSize();
return bridgeSupport.getActiveFederationSize();
}

public Integer getFederationThreshold(Object[] args) {
logger.trace("getFederationThreshold");

return bridgeSupport.getFederationThreshold();
return bridgeSupport.getActiveFederationThreshold();
}

public byte[] getFederatorPublicKey(Object[] args) {
logger.trace("getFederatorPublicKey");

int index = ((BigInteger) args[0]).intValue();
return bridgeSupport.getFederatorPublicKey(index);
return bridgeSupport.getActiveFederatorBtcPublicKey(index);
}

public byte[] getFederatorPublicKeyOfType(Object[] args) throws VMException {
Expand All @@ -806,19 +806,19 @@ public byte[] getFederatorPublicKeyOfType(Object[] args) throws VMException {
throw new VMException("Exception in getFederatorPublicKeyOfType", e);
}

return bridgeSupport.getFederatorPublicKeyOfType(index, keyType);
return bridgeSupport.getActiveFederatorPublicKeyOfType(index, keyType);
}

public Long getFederationCreationTime(Object[] args) {
logger.trace("getFederationCreationTime");

// Return the creation time in milliseconds from the epoch
return bridgeSupport.getFederationCreationTime().toEpochMilli();
return bridgeSupport.getActiveFederationCreationTime().toEpochMilli();
}

public long getFederationCreationBlockNumber(Object[] args) {
logger.trace("getFederationCreationBlockNumber");
return bridgeSupport.getFederationCreationBlockNumber();
return bridgeSupport.getActiveFederationCreationBlockNumber();
}

public String getRetiringFederationAddress(Object[] args) {
Expand Down Expand Up @@ -850,7 +850,7 @@ public byte[] getRetiringFederatorPublicKey(Object[] args) {
logger.trace("getRetiringFederatorPublicKey");

int index = ((BigInteger) args[0]).intValue();
byte[] publicKey = bridgeSupport.getRetiringFederatorPublicKey(index);
byte[] publicKey = bridgeSupport.getRetiringFederatorBtcPublicKey(index);

if (publicKey == null) {
// Empty array is returned when public key is not found or there's no retiring federation
Expand Down Expand Up @@ -970,17 +970,17 @@ public Integer rollbackFederation(Object[] args) throws BridgeIllegalArgumentExc
);
}

public byte[] getPendingFederationHash(Object[] args) {
public byte[] getPendingFederationHashSerialized(Object[] args) {
logger.trace("getPendingFederationHash");

byte[] hash = bridgeSupport.getPendingFederationHash();
Keccak256 hash = bridgeSupport.getPendingFederationHash();

if (hash == null) {
// Empty array is returned when pending federation is not present
return new byte[]{};
}

return hash;
return hash.getBytes();
}

public Integer getPendingFederationSize(Object[] args) {
Expand All @@ -993,7 +993,7 @@ public byte[] getPendingFederatorPublicKey(Object[] args) {
logger.trace("getPendingFederatorPublicKey");

int index = ((BigInteger) args[0]).intValue();
byte[] publicKey = bridgeSupport.getPendingFederatorPublicKey(index);
byte[] publicKey = bridgeSupport.getPendingFederatorBtcPublicKey(index);

if (publicKey == null) {
// Empty array is returned when public key is not found
Expand Down Expand Up @@ -1152,7 +1152,7 @@ public long getLockingCap(Object[] args) {
public byte[] getActivePowpegRedeemScript(Object[] args) {
logger.debug("[getActivePowpegRedeemScript] started");
try {
Optional<Script> redeemScript = bridgeSupport.getActivePowpegRedeemScript();
Optional<Script> redeemScript = bridgeSupport.getActiveFederationRedeemScript();
logger.debug("[getActivePowpegRedeemScript] finished");
return redeemScript.orElse(new Script(new byte[]{})).getProgram();
} catch (Exception ex) {
Expand Down
2 changes: 1 addition & 1 deletion rskj-core/src/main/java/co/rsk/peg/BridgeMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ public enum BridgeMethods {
new String[]{"bytes"}
),
fixedCost(3000L),
(BridgeMethodExecutorTyped) Bridge::getPendingFederationHash,
(BridgeMethodExecutorTyped) Bridge::getPendingFederationHashSerialized,
fixedPermission(true),
CallTypeHelper.ALLOW_STATIC_CALL
),
Expand Down
71 changes: 54 additions & 17 deletions rskj-core/src/main/java/co/rsk/peg/BridgeSerializationUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@

import co.rsk.bitcoinj.core.*;
import co.rsk.bitcoinj.script.Script;
import co.rsk.peg.constants.BridgeConstants;
import co.rsk.core.RskAddress;
import co.rsk.crypto.Keccak256;
import co.rsk.peg.federation.constants.FederationConstants;
import co.rsk.peg.vote.ABICallElection;
import co.rsk.peg.vote.ABICallSpec;
import co.rsk.peg.bitcoin.CoinbaseInformation;
Expand All @@ -39,16 +39,15 @@
import org.ethereum.util.RLPList;

import javax.annotation.Nullable;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.*;
import java.util.stream.Collectors;

import static co.rsk.peg.federation.FederationFormatVersion.*;

/**
* Created by mario on 20/04/17.
*/
Expand Down Expand Up @@ -103,7 +102,7 @@ public static SortedMap<Keccak256, BtcTransaction> deserializeMap(byte[] data, N
return map;
}

public static byte[] serializeUTXOList(List<UTXO> list) throws IOException {
public static byte[] serializeUTXOList(List<UTXO> list) {
int nutxos = list.size();

byte[][] bytes = new byte[nutxos][];
Expand All @@ -113,13 +112,15 @@ public static byte[] serializeUTXOList(List<UTXO> list) throws IOException {
try (ByteArrayOutputStream ostream = new ByteArrayOutputStream()) {
utxo.serializeToStream(ostream);
bytes[n++] = RLP.encodeElement(ostream.toByteArray());
} catch (IOException ioe) {
throw new SerializationException(String.format("Unable to serialize UTXO %s from UTXOs list %s", utxo, list), ioe);
}
}

return RLP.encodeList(bytes);
}

public static List<UTXO> deserializeUTXOList(byte[] data) throws IOException {
public static List<UTXO> deserializeUTXOList(byte[] data) {
List<UTXO> list = new ArrayList<>();

if (data == null || data.length == 0) {
Expand All @@ -133,8 +134,12 @@ public static List<UTXO> deserializeUTXOList(byte[] data) throws IOException {
for (int k = 0; k < nutxos; k++) {
byte[] utxoBytes = rlpList.get(k).getRLPData();
InputStream istream = new ByteArrayInputStream(utxoBytes);
UTXO utxo = new UTXO(istream);
list.add(utxo);
try {
UTXO utxo = new UTXO(istream);
list.add(utxo);
} catch (IOException ioe) {
throw new SerializationException(String.format("Unable to deserialize %d th UTXO %s", k, Arrays.toString(data)), ioe);
}
}

return list;
Expand Down Expand Up @@ -303,6 +308,38 @@ public static byte[] serializeFederation(Federation federation) {
FederationMember::serialize
);
}
public static Federation deserializeFederationAccordingToVersion(
byte[] data,
int version,
FederationConstants federationConstants,
ActivationConfig.ForBlock activations
) {
NetworkParameters networkParameters = federationConstants.getBtcParams();
if (version == STANDARD_MULTISIG_FEDERATION.getFormatVersion()) {
return BridgeSerializationUtils.deserializeStandardMultisigFederation(
data,
networkParameters
);
}
if (version == NON_STANDARD_ERP_FEDERATION.getFormatVersion()) {
return BridgeSerializationUtils.deserializeNonStandardErpFederation(
data,
federationConstants,
activations
);
}
if (version == P2SH_ERP_FEDERATION.getFormatVersion()) {
return BridgeSerializationUtils.deserializeP2shErpFederation(
data,
federationConstants
);
}
// To keep backwards compatibility
return BridgeSerializationUtils.deserializeStandardMultisigFederation(
data,
networkParameters
);
}

// For the serialization format, see BridgeSerializationUtils::serializeFederation
public static StandardMultisigFederation deserializeStandardMultisigFederation(
Expand All @@ -317,35 +354,35 @@ public static StandardMultisigFederation deserializeStandardMultisigFederation(
}
public static ErpFederation deserializeNonStandardErpFederation(
byte[] data,
BridgeConstants bridgeConstants,
FederationConstants federationConstants,
ActivationConfig.ForBlock activations
) {
Federation federation = deserializeStandardMultisigFederationWithDeserializer(
data,
bridgeConstants.getBtcParams(),
federationConstants.getBtcParams(),
FederationMember::deserialize
);

FederationArgs federationArgs = federation.getArgs();
List<BtcECKey> erpPubKeys = bridgeConstants.getErpFedPubKeysList();
long activationDelay = bridgeConstants.getErpFedActivationDelay();
List<BtcECKey> erpPubKeys = federationConstants.getErpFedPubKeysList();
long activationDelay = federationConstants.getErpFedActivationDelay();

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

public static ErpFederation deserializeP2shErpFederation(
byte[] data,
BridgeConstants bridgeConstants
FederationConstants federationConstants
) {
Federation federation = deserializeStandardMultisigFederationWithDeserializer(
data,
bridgeConstants.getBtcParams(),
federationConstants.getBtcParams(),
FederationMember::deserialize
);

FederationArgs federationArgs = federation.getArgs();
List<BtcECKey> erpPubKeys = bridgeConstants.getErpFedPubKeysList();
long activationDelay = bridgeConstants.getErpFedActivationDelay();
List<BtcECKey> erpPubKeys = federationConstants.getErpFedPubKeysList();
long activationDelay = federationConstants.getErpFedActivationDelay();

return FederationFactory.buildP2shErpFederation(federationArgs, erpPubKeys, activationDelay);
}
Expand Down
27 changes: 9 additions & 18 deletions rskj-core/src/main/java/co/rsk/peg/BridgeState.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ public class BridgeState {
private final PegoutsWaitingForConfirmations pegoutsWaitingForConfirmations;
private final ActivationConfig.ForBlock activations;

private BridgeState(int btcBlockchainBestChainHeight,
long nextPegoutCreationBlockNumber,
List<UTXO> activeFederationBtcUTXOs,
SortedMap<Keccak256,
BtcTransaction> rskTxsWaitingForSignatures,
ReleaseRequestQueue releaseRequestQueue,
PegoutsWaitingForConfirmations pegoutsWaitingForConfirmations,
@Nullable ActivationConfig.ForBlock activations) {
protected BridgeState(
int btcBlockchainBestChainHeight,
long nextPegoutCreationBlockNumber,
List<UTXO> activeFederationBtcUTXOs,
SortedMap<Keccak256, BtcTransaction> rskTxsWaitingForSignatures,
ReleaseRequestQueue releaseRequestQueue,
PegoutsWaitingForConfirmations pegoutsWaitingForConfirmations,
@Nullable ActivationConfig.ForBlock activations) {

this.btcBlockchainBestChainHeight = btcBlockchainBestChainHeight;
this.nextPegoutCreationBlockNumber = nextPegoutCreationBlockNumber;
this.activeFederationBtcUTXOs = activeFederationBtcUTXOs;
Expand All @@ -64,16 +65,6 @@ private BridgeState(int btcBlockchainBestChainHeight,
this.activations = activations;
}

public BridgeState(int btcBlockchainBestChainHeight, BridgeStorageProvider provider, ActivationConfig.ForBlock activations) throws IOException {
this(btcBlockchainBestChainHeight,
provider.getNextPegoutHeight().orElse(0L),
provider.getNewFederationBtcUTXOs(),
provider.getPegoutsWaitingForSignatures(),
provider.getReleaseRequestQueue(),
provider.getPegoutsWaitingForConfirmations(),
activations);
}

public int getBtcBlockchainBestChainHeight() {
return this.btcBlockchainBestChainHeight;
}
Expand Down
16 changes: 1 addition & 15 deletions rskj-core/src/main/java/co/rsk/peg/BridgeStorageIndexKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,16 @@
import org.ethereum.vm.DataWord;

public enum BridgeStorageIndexKey {
NEW_FEDERATION_BTC_UTXOS_KEY("newFederationBtcUTXOs"),
NEW_FEDERATION_BTC_UTXOS_KEY_FOR_TESTNET_PRE_HOP("newFederationBtcUTXOsForTestnet"),
NEW_FEDERATION_BTC_UTXOS_KEY_FOR_TESTNET_POST_HOP("newFedBtcUTXOsForTestnetPostHop"),
OLD_FEDERATION_BTC_UTXOS_KEY("oldFederationBtcUTXOs"),

BTC_TX_HASHES_ALREADY_PROCESSED_KEY("btcTxHashesAP"),
RELEASE_REQUEST_QUEUE("releaseRequestQueue"),
PEGOUTS_WAITING_FOR_CONFIRMATIONS("releaseTransactionSet"),
PEGOUTS_WAITING_FOR_SIGNATURES("rskTxsWaitingFS"),
NEW_FEDERATION_KEY("newFederation"),
OLD_FEDERATION_KEY("oldFederation"),
PENDING_FEDERATION_KEY("pendingFederation"),
FEDERATION_ELECTION_KEY("federationElection"),
LOCKING_CAP_KEY("lockingCap"),
RELEASE_REQUEST_QUEUE_WITH_TXHASH("releaseRequestQueueWithTxHash"),
PEGOUTS_WAITING_FOR_CONFIRMATIONS_WITH_TXHASH_KEY("releaseTransactionSetWithTxHash"),
RECEIVE_HEADERS_TIMESTAMP("receiveHeadersLastTimestamp"),
// Federation creation keys
ACTIVE_FEDERATION_CREATION_BLOCK_HEIGHT_KEY("activeFedCreationBlockHeight"),
NEXT_FEDERATION_CREATION_BLOCK_HEIGHT_KEY("nextFedCreationBlockHeight"),
LAST_RETIRED_FEDERATION_P2SH_SCRIPT_KEY("lastRetiredFedP2SHScript"),
// Version keys and versions
NEW_FEDERATION_FORMAT_VERSION("newFederationFormatVersion"),
OLD_FEDERATION_FORMAT_VERSION("oldFederationFormatVersion"),
PENDING_FEDERATION_FORMAT_VERSION("pendingFederationFormatVersion"),
NEXT_PEGOUT_HEIGHT_KEY("nextPegoutHeight"),

// Compound keys
Expand Down
Loading

0 comments on commit a4700c5

Please sign in to comment.