From 53fc3e075bc64b12a4164b5b0daa2b29211d4b40 Mon Sep 17 00:00:00 2001 From: julia zack Date: Wed, 27 Sep 2023 14:18:53 -0300 Subject: [PATCH 01/27] Refactor Federation classes. Remove all unnecessary overridden methods --- .../main/java/co/rsk/peg/ErpFederation.java | 88 ++----------------- .../src/main/java/co/rsk/peg/Federation.java | 47 ++-------- .../java/co/rsk/peg/LegacyErpFederation.java | 86 ++++++++++++++++++ .../java/co/rsk/peg/P2shErpFederation.java | 31 +++++++ .../rsk/peg/StandardMultisigFederation.java | 75 ++++++++++++++++ 5 files changed, 205 insertions(+), 122 deletions(-) create mode 100644 rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java create mode 100644 rskj-core/src/main/java/co/rsk/peg/StandardMultisigFederation.java diff --git a/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java b/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java index b8d303f94a5..f4583a02fef 100644 --- a/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java +++ b/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java @@ -1,9 +1,7 @@ package co.rsk.peg; -import co.rsk.bitcoinj.core.Address; import co.rsk.bitcoinj.core.BtcECKey; import co.rsk.bitcoinj.core.NetworkParameters; -import co.rsk.bitcoinj.script.ErpFederationRedeemScriptParser; import co.rsk.bitcoinj.script.Script; import co.rsk.bitcoinj.script.ScriptBuilder; import co.rsk.peg.utils.EcKeyUtils; @@ -17,16 +15,18 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class ErpFederation extends Federation { - private static final byte[] ERP_TESTNET_REDEEM_SCRIPT_BYTES = Hex.decode("6453210208f40073a9e43b3e9103acec79767a6de9b0409749884e989960fee578012fce210225e892391625854128c5c4ea4340de0c2a70570f33db53426fc9c746597a03f42102afc230c2d355b1a577682b07bc2646041b5d0177af0f98395a46018da699b6da210344a3c38cd59afcba3edcebe143e025574594b001700dec41e59409bdbd0f2a0921039a060badbeb24bee49eb2063f616c0f0f0765d4ca646b20a88ce828f259fcdb955670300cd50b27552210216c23b2ea8e4f11c3f9e22711addb1d16a93964796913830856b568cc3ea21d3210275562901dd8faae20de0a4166362a4f82188db77dbed4ca887422ea1ec185f1421034db69f2112f4fb1bb6141bf6e2bd6631f0484d0bd95b16767902c9fe219d4a6f5368ae"); +public abstract class ErpFederation extends Federation { private static final Logger logger = LoggerFactory.getLogger(ErpFederation.class); + protected static final byte[] ERP_TESTNET_REDEEM_SCRIPT_BYTES = Hex.decode("6453210208f40073a9e43b3e9103acec79767a6de9b0409749884e989960fee578012fce210225e892391625854128c5c4ea4340de0c2a70570f33db53426fc9c746597a03f42102afc230c2d355b1a577682b07bc2646041b5d0177af0f98395a46018da699b6da210344a3c38cd59afcba3edcebe143e025574594b001700dec41e59409bdbd0f2a0921039a060badbeb24bee49eb2063f616c0f0f0765d4ca646b20a88ce828f259fcdb955670300cd50b27552210216c23b2ea8e4f11c3f9e22711addb1d16a93964796913830856b568cc3ea21d3210275562901dd8faae20de0a4166362a4f82188db77dbed4ca887422ea1ec185f1421034db69f2112f4fb1bb6141bf6e2bd6631f0484d0bd95b16767902c9fe219d4a6f5368ae"); + protected final List erpPubKeys; protected final long activationDelay; protected final ActivationConfig.ForBlock activations; - protected Script standardRedeemScript; + // add erpRedeemScript? is it necessary? protected Script standardP2SHScript; + // add erpP2SHScript? public ErpFederation( List members, @@ -57,52 +57,8 @@ public long getActivationDelay() { return activationDelay; } - @Override - public Script getRedeemScript() { - if (!activations.isActive(ConsensusRule.RSKIP284) && - btcParams.getId().equals(NetworkParameters.ID_TESTNET)) { - logger.debug("[getRedeemScript] Returning hardcoded redeem script"); - return new Script(ERP_TESTNET_REDEEM_SCRIPT_BYTES); - } - - if (redeemScript == null) { - logger.debug("[getRedeemScript] Creating the redeem script from the keys"); - redeemScript = activations.isActive(ConsensusRule.RSKIP293) ? - ErpFederationRedeemScriptParser.createErpRedeemScript( - ScriptBuilder.createRedeemScript(getNumberOfSignaturesRequired(), getBtcPublicKeys()), - ScriptBuilder.createRedeemScript(erpPubKeys.size() / 2 + 1, erpPubKeys), - activationDelay - ) : - ErpFederationRedeemScriptParser.createErpRedeemScriptDeprecated( - ScriptBuilder.createRedeemScript(getNumberOfSignaturesRequired(), getBtcPublicKeys()), - ScriptBuilder.createRedeemScript(erpPubKeys.size() / 2 + 1, erpPubKeys), - activationDelay - ); - } - - return redeemScript; - } - - @Override - public Script getStandardRedeemScript() { - if (standardRedeemScript == null) { - standardRedeemScript = ErpFederationRedeemScriptParser.extractStandardRedeemScript( - getRedeemScript().getChunks() - ); - } - return standardRedeemScript; - } - - @Override - public Script getP2SHScript() { - if (p2shScript == null) { - p2shScript = ScriptBuilder.createP2SHOutputScript(getRedeemScript()); - } + public abstract Script getStandardRedeemScript(); - return p2shScript; - } - - @Override public Script getStandardP2SHScript() { if (standardP2SHScript == null) { standardP2SHScript = ScriptBuilder.createP2SHOutputScript(getStandardRedeemScript()); @@ -111,38 +67,6 @@ public Script getStandardP2SHScript() { return standardP2SHScript; } - @Override - public Address getAddress() { - if (address == null) { - address = Address.fromP2SHScript(btcParams, getP2SHScript()); - } - - return address; - } - - @Override - public boolean equals(Object other) { - if (this == other) { - return true; - } - - if (other == null || this.getClass() != other.getClass()) { - return false; - } - - ErpFederation otherErpFederation = (ErpFederation) other; - - return this.getNumberOfSignaturesRequired() == otherErpFederation.getNumberOfSignaturesRequired() && - this.getSize() == otherErpFederation.getSize() && - this.getCreationTime().equals(otherErpFederation.getCreationTime()) && - this.creationBlockNumber == otherErpFederation.creationBlockNumber && - this.btcParams.equals(otherErpFederation.btcParams) && - this.members.equals(otherErpFederation.members) && - this.getRedeemScript().equals(otherErpFederation.getRedeemScript()) && - this.erpPubKeys.equals(otherErpFederation.erpPubKeys) && - this.activationDelay == otherErpFederation.activationDelay; - } - @Override public int hashCode() { // Can use java.util.Objects.hash since all of Instant, int and List have diff --git a/rskj-core/src/main/java/co/rsk/peg/Federation.java b/rskj-core/src/main/java/co/rsk/peg/Federation.java index 0008949397c..045a0ae6a97 100644 --- a/rskj-core/src/main/java/co/rsk/peg/Federation.java +++ b/rskj-core/src/main/java/co/rsk/peg/Federation.java @@ -39,7 +39,7 @@ * @author Ariel Mendelzon */ -public class Federation { +public abstract class Federation { protected final List members; protected final Instant creationTime; protected final long creationBlockNumber; @@ -49,7 +49,7 @@ public class Federation { protected Script p2shScript; protected Address address; - public Federation(List members, Instant creationTime, long creationBlockNumber, NetworkParameters btcParams) { + public Federation(List members, Instant creationTime, long creationBlockNumber, NetworkParameters btcParams) { // Sorting members ensures same order of federation members for same members // Immutability provides protection against unwanted modification, thus making the Federation instance // effectively immutable @@ -91,18 +91,7 @@ public long getCreationBlockNumber() { return creationBlockNumber; } - public Script getRedeemScript() { - if (redeemScript == null) { - redeemScript = ScriptBuilder.createRedeemScript(getNumberOfSignaturesRequired(), getBtcPublicKeys()); - } - - return redeemScript; - } - - public Script getStandardRedeemScript() { - return getRedeemScript(); - } - + public abstract Script getRedeemScript(); public Script getP2SHScript() { if (p2shScript == null) { p2shScript = ScriptBuilder.createP2SHOutputScript(getNumberOfSignaturesRequired(), getBtcPublicKeys()); @@ -111,12 +100,11 @@ public Script getP2SHScript() { return p2shScript; } - public Script getStandardP2SHScript() { - return getP2SHScript(); - } - public Address getAddress() { if (address == null) { + // for now, it is ok to this method to be like this. + // when we add bech32 it should change, because the calculation of the address + // would be different. address = Address.fromP2SHScript(btcParams, getP2SHScript()); } @@ -152,33 +140,12 @@ public boolean isMember(FederationMember federationMember){ return this.members.contains(federationMember); } - @Override public String toString() { return String.format("%d of %d signatures federation", getNumberOfSignaturesRequired(), members.size()); } - @Override - public boolean equals(Object other) { - if (this == other) { - return true; - } - - if (other == null || this.getClass() != other.getClass()) { - return false; - } - - Federation otherFederation = (Federation) other; - - return this.getNumberOfSignaturesRequired() == otherFederation.getNumberOfSignaturesRequired() && - this.getSize() == otherFederation.getSize() && - this.getCreationTime().equals(otherFederation.getCreationTime()) && - this.creationBlockNumber == otherFederation.creationBlockNumber && - this.btcParams.equals(otherFederation.btcParams) && - this.members.equals(otherFederation.members) && - this.getRedeemScript().equals(otherFederation.getRedeemScript()); - } + public abstract boolean equals(Object other); - @Override public int hashCode() { // Can use java.util.Objects.hash since all of Instant, int and List have // well-defined hashCode()s diff --git a/rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java b/rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java new file mode 100644 index 00000000000..5ad206140c1 --- /dev/null +++ b/rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java @@ -0,0 +1,86 @@ +package co.rsk.peg; + +import co.rsk.bitcoinj.core.BtcECKey; +import co.rsk.bitcoinj.core.NetworkParameters; +import co.rsk.bitcoinj.script.ErpFederationRedeemScriptParser; +import co.rsk.bitcoinj.script.Script; +import co.rsk.bitcoinj.script.ScriptBuilder; +import java.time.Instant; +import java.util.List; +import org.ethereum.config.blockchain.upgrades.ActivationConfig; +import org.ethereum.config.blockchain.upgrades.ConsensusRule; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Deprecated +public class LegacyErpFederation extends ErpFederation { + private static final Logger logger = LoggerFactory.getLogger(LegacyErpFederation.class); + + public LegacyErpFederation( + List members, + Instant creationTime, + long creationBlockNumber, + NetworkParameters btcParams, + List erpPubKeys, + long activationDelay, + ActivationConfig.ForBlock activations) { + + super(members, creationTime, creationBlockNumber, btcParams, erpPubKeys, activationDelay, activations); + } + + @Override + public Script getRedeemScript() { + if (!activations.isActive(ConsensusRule.RSKIP284) && + btcParams.getId().equals(NetworkParameters.ID_TESTNET)) { + logger.debug("[getRedeemScript] Returning hardcoded redeem script"); + return new Script(ERP_TESTNET_REDEEM_SCRIPT_BYTES); + } + + if (redeemScript == null) { + logger.debug("[getRedeemScript] Creating the redeem script from the keys"); + redeemScript = ErpFederationRedeemScriptParser.createErpRedeemScriptDeprecated( + ScriptBuilder.createRedeemScript(getNumberOfSignaturesRequired(), getBtcPublicKeys()), + ScriptBuilder.createRedeemScript(erpPubKeys.size() / 2 + 1, erpPubKeys), + activationDelay + ); + } + + return redeemScript; + } + + @Override + public Script getStandardRedeemScript() { + if (standardRedeemScript == null) { + // i think is not quite right to just "fix" the standardRedeemScript in the buggy fed. + // the real case is that the OP_CHECKMULTISIG is not before the OP_ELSE... as the method adds. + standardRedeemScript = ErpFederationRedeemScriptParser.extractStandardRedeemScript( + getRedeemScript().getChunks() + ); + } + return standardRedeemScript; + } + + @Override + public boolean equals(Object other) { + if (this == other) { + return true; + } + + if (other == null || this.getClass() != other.getClass()) { + return false; + } + + LegacyErpFederation otherErpFederation = (LegacyErpFederation) other; + + return this.getNumberOfSignaturesRequired() == otherErpFederation.getNumberOfSignaturesRequired() && + this.getSize() == otherErpFederation.getSize() && + this.getCreationTime().equals(otherErpFederation.getCreationTime()) && + this.creationBlockNumber == otherErpFederation.creationBlockNumber && + this.btcParams.equals(otherErpFederation.btcParams) && + this.members.equals(otherErpFederation.members) && + this.getRedeemScript().equals(otherErpFederation.getRedeemScript()) && + this.erpPubKeys.equals(otherErpFederation.erpPubKeys) && + this.activationDelay == otherErpFederation.activationDelay; + } +} + diff --git a/rskj-core/src/main/java/co/rsk/peg/P2shErpFederation.java b/rskj-core/src/main/java/co/rsk/peg/P2shErpFederation.java index 573fe02f903..85fcdc299ff 100644 --- a/rskj-core/src/main/java/co/rsk/peg/P2shErpFederation.java +++ b/rskj-core/src/main/java/co/rsk/peg/P2shErpFederation.java @@ -8,6 +8,7 @@ import java.time.Instant; import java.util.List; import org.ethereum.config.blockchain.upgrades.ActivationConfig; +import org.ethereum.config.blockchain.upgrades.ConsensusRule; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -28,6 +29,13 @@ public P2shErpFederation( @Override public final Script getRedeemScript() { + // not sure if i should add this here or not. I think not but just in case + if (!activations.isActive(ConsensusRule.RSKIP284) && + btcParams.getId().equals(NetworkParameters.ID_TESTNET)) { + logger.debug("[getRedeemScript] Returning hardcoded redeem script"); + return new Script(ERP_TESTNET_REDEEM_SCRIPT_BYTES); + } + if (redeemScript == null) { logger.debug("[getRedeemScript] Creating the redeem script from the keys"); redeemScript = P2shErpFederationRedeemScriptParser.createP2shErpRedeemScript( @@ -49,4 +57,27 @@ public final Script getStandardRedeemScript() { } return standardRedeemScript; } + + @Override + public boolean equals(Object other) { + if (this == other) { + return true; + } + + if (other == null || this.getClass() != other.getClass()) { + return false; + } + + P2shErpFederation otherP2shErpFederation = (P2shErpFederation) other; + + return this.getNumberOfSignaturesRequired() == otherP2shErpFederation.getNumberOfSignaturesRequired() && + this.getSize() == otherP2shErpFederation.getSize() && + this.getCreationTime().equals(otherP2shErpFederation.getCreationTime()) && + this.creationBlockNumber == otherP2shErpFederation.creationBlockNumber && + this.btcParams.equals(otherP2shErpFederation.btcParams) && + this.members.equals(otherP2shErpFederation.members) && + this.getRedeemScript().equals(otherP2shErpFederation.getRedeemScript()) && + this.erpPubKeys.equals(otherP2shErpFederation.erpPubKeys) && + this.activationDelay == otherP2shErpFederation.activationDelay; + } } diff --git a/rskj-core/src/main/java/co/rsk/peg/StandardMultisigFederation.java b/rskj-core/src/main/java/co/rsk/peg/StandardMultisigFederation.java new file mode 100644 index 00000000000..d2fb83c61ec --- /dev/null +++ b/rskj-core/src/main/java/co/rsk/peg/StandardMultisigFederation.java @@ -0,0 +1,75 @@ +/* + * This file is part of RskJ + * Copyright (C) 2017 RSK Labs Ltd. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package co.rsk.peg; + +import co.rsk.bitcoinj.core.NetworkParameters; +import co.rsk.bitcoinj.script.Script; +import co.rsk.bitcoinj.script.ScriptBuilder; + +import java.time.Instant; +import java.util.List; + +/** + * Immutable representation of an RSK Federation in the context of + * a specific BTC network. + * + * @author Ariel Mendelzon + */ + +public class StandardMultisigFederation extends Federation { + + public StandardMultisigFederation( + List members, + Instant creationTime, + long creationBlockNumber, + NetworkParameters btcParams) { + + super(members, creationTime, creationBlockNumber, btcParams); + } + + @Override + public Script getRedeemScript() { + if (redeemScript == null) { + redeemScript = ScriptBuilder.createRedeemScript(getNumberOfSignaturesRequired(), getBtcPublicKeys()); + } + + return redeemScript; + } + + @Override + public boolean equals(Object other) { + if (this == other) { + return true; + } + + if (other == null || this.getClass() != other.getClass()) { + return false; + } + + StandardMultisigFederation otherFederation = (StandardMultisigFederation) other; + + return this.getNumberOfSignaturesRequired() == otherFederation.getNumberOfSignaturesRequired() && + this.getSize() == otherFederation.getSize() && + this.getCreationTime().equals(otherFederation.getCreationTime()) && + this.creationBlockNumber == otherFederation.creationBlockNumber && + this.btcParams.equals(otherFederation.btcParams) && + this.members.equals(otherFederation.members) && + this.getRedeemScript().equals(otherFederation.getRedeemScript()); + } +} From 293030a995ca237be3fb85f5a1f6ae948013cc1e Mon Sep 17 00:00:00 2001 From: julia zack Date: Wed, 27 Sep 2023 14:38:09 -0300 Subject: [PATCH 02/27] Add P2shP2wshErpFederation class for later --- .../co/rsk/peg/P2shP2wshErpFederation.java | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 rskj-core/src/main/java/co/rsk/peg/P2shP2wshErpFederation.java diff --git a/rskj-core/src/main/java/co/rsk/peg/P2shP2wshErpFederation.java b/rskj-core/src/main/java/co/rsk/peg/P2shP2wshErpFederation.java new file mode 100644 index 00000000000..5d4771ab314 --- /dev/null +++ b/rskj-core/src/main/java/co/rsk/peg/P2shP2wshErpFederation.java @@ -0,0 +1,86 @@ +/* +package co.rsk.peg; + +import co.rsk.bitcoinj.core.BtcECKey; +import co.rsk.bitcoinj.core.NetworkParameters; +import co.rsk.bitcoinj.script.P2shP2wshErpFederationRedeemScriptParser; +import co.rsk.bitcoinj.script.Script; +import co.rsk.bitcoinj.script.ScriptBuilder; +import org.ethereum.config.blockchain.upgrades.ActivationConfig; +import org.ethereum.config.blockchain.upgrades.ConsensusRule; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.time.Instant; +import java.util.List; + +public class P2shP2wshErpFederation extends ErpFederation { + private static final Logger logger = LoggerFactory.getLogger(co.rsk.peg.P2shP2wshErpFederation.class); + + public P2shP2wshErpFederation( + List members, + Instant creationTime, + long creationBlockNumber, + NetworkParameters btcParams, + List erpPubKeys, + long activationDelay, + ActivationConfig.ForBlock activations + ) { + super(members, creationTime, creationBlockNumber, btcParams, erpPubKeys, activationDelay, activations); + } + + @Override + public final Script getRedeemScript() { + // not sure if i should add this here or not. I think not but just in case + if (!activations.isActive(ConsensusRule.RSKIP284) && + btcParams.getId().equals(NetworkParameters.ID_TESTNET)) { + logger.debug("[getRedeemScript] Returning hardcoded redeem script"); + return new Script(ERP_TESTNET_REDEEM_SCRIPT_BYTES); + } + + if (redeemScript == null) { + logger.debug("[getRedeemScript] Creating the redeem script from the keys"); + redeemScript = P2shP2wshErpFederationRedeemScriptParser.createP2shP2wshErpRedeemScript( + ScriptBuilder.createRedeemScript(getNumberOfSignaturesRequired(), getBtcPublicKeys()), + ScriptBuilder.createRedeemScript(erpPubKeys.size() / 2 + 1, erpPubKeys), + activationDelay + ); + } + + return redeemScript; + } + + @Override + public final Script getStandardRedeemScript() { + if (standardRedeemScript == null) { + standardRedeemScript = P2shP2wshErpFederationRedeemScriptParser.extractStandardRedeemScript( + getRedeemScript().getChunks() + ); + } + return standardRedeemScript; + } + + @Override + public boolean equals(Object other) { + if (this == other) { + return true; + } + + if (other == null || this.getClass() != other.getClass()) { + return false; + } + + P2shP2wshErpFederation otherP2shErpFederation = (P2shP2wshErpFederation) other; + + return this.getNumberOfSignaturesRequired() == otherP2shErpFederation.getNumberOfSignaturesRequired() && + this.getSize() == otherP2shErpFederation.getSize() && + this.getCreationTime().equals(otherP2shErpFederation.getCreationTime()) && + this.creationBlockNumber == otherP2shErpFederation.creationBlockNumber && + this.btcParams.equals(otherP2shErpFederation.btcParams) && + this.members.equals(otherP2shErpFederation.members) && + this.getRedeemScript().equals(otherP2shErpFederation.getRedeemScript()) && + this.erpPubKeys.equals(otherP2shErpFederation.erpPubKeys) && + this.activationDelay == otherP2shErpFederation.activationDelay; + } + } +*/ From b7aee4171bbfb9e3877f948e5eca225ccb90c454 Mon Sep 17 00:00:00 2001 From: julia zack Date: Wed, 27 Sep 2023 18:06:45 -0300 Subject: [PATCH 03/27] Add some overrides needed. Fix some stuff --- .../main/java/co/rsk/peg/ErpFederation.java | 22 ----- .../src/main/java/co/rsk/peg/Federation.java | 18 +--- .../java/co/rsk/peg/LegacyErpFederation.java | 17 ++++ .../java/co/rsk/peg/P2shErpFederation.java | 9 +- .../co/rsk/peg/P2shP2wshErpFederation.java | 86 ------------------- .../rsk/peg/StandardMultisigFederation.java | 15 +++- 6 files changed, 37 insertions(+), 130 deletions(-) delete mode 100644 rskj-core/src/main/java/co/rsk/peg/P2shP2wshErpFederation.java diff --git a/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java b/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java index f4583a02fef..e32509e1b0c 100644 --- a/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java +++ b/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java @@ -9,24 +9,14 @@ import java.util.Collections; import java.util.List; import java.util.Objects; -import org.bouncycastle.util.encoders.Hex; import org.ethereum.config.blockchain.upgrades.ActivationConfig; -import org.ethereum.config.blockchain.upgrades.ConsensusRule; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; public abstract class ErpFederation extends Federation { - private static final Logger logger = LoggerFactory.getLogger(ErpFederation.class); - - protected static final byte[] ERP_TESTNET_REDEEM_SCRIPT_BYTES = Hex.decode("6453210208f40073a9e43b3e9103acec79767a6de9b0409749884e989960fee578012fce210225e892391625854128c5c4ea4340de0c2a70570f33db53426fc9c746597a03f42102afc230c2d355b1a577682b07bc2646041b5d0177af0f98395a46018da699b6da210344a3c38cd59afcba3edcebe143e025574594b001700dec41e59409bdbd0f2a0921039a060badbeb24bee49eb2063f616c0f0f0765d4ca646b20a88ce828f259fcdb955670300cd50b27552210216c23b2ea8e4f11c3f9e22711addb1d16a93964796913830856b568cc3ea21d3210275562901dd8faae20de0a4166362a4f82188db77dbed4ca887422ea1ec185f1421034db69f2112f4fb1bb6141bf6e2bd6631f0484d0bd95b16767902c9fe219d4a6f5368ae"); - protected final List erpPubKeys; protected final long activationDelay; protected final ActivationConfig.ForBlock activations; protected Script standardRedeemScript; - // add erpRedeemScript? is it necessary? protected Script standardP2SHScript; - // add erpP2SHScript? public ErpFederation( List members, @@ -45,7 +35,6 @@ public ErpFederation( // Try getting the redeem script in order to validate it can be built // using the given public keys and csv value getRedeemScript(); // NOSONAR - validateRedeemScript(); getStandardRedeemScript(); // NOSONAR } @@ -80,15 +69,4 @@ public int hashCode() { getActivationDelay() ); } - - private void validateRedeemScript() { - if (activations.isActive(ConsensusRule.RSKIP293) && - this.redeemScript.equals(new Script(ERP_TESTNET_REDEEM_SCRIPT_BYTES))) { - - String message = "Unable to create ERP Federation. The obtained redeem script matches the one hardcoded for testnet. " - + "This would cause bitcoinj-thin to identify it as invalid"; - logger.debug("[validateRedeemScript] {}", message); - throw new FederationCreationException(message); - } - } } diff --git a/rskj-core/src/main/java/co/rsk/peg/Federation.java b/rskj-core/src/main/java/co/rsk/peg/Federation.java index 045a0ae6a97..60d8c12fc29 100644 --- a/rskj-core/src/main/java/co/rsk/peg/Federation.java +++ b/rskj-core/src/main/java/co/rsk/peg/Federation.java @@ -36,7 +36,6 @@ * Immutable representation of an RSK Federation in the context of * a specific BTC network. * - * @author Ariel Mendelzon */ public abstract class Federation { @@ -75,6 +74,7 @@ public List getBtcPublicKeys() { .collect(Collectors.toList()); } + // TODO: take a look at this. We should check the threshold at the redeemScript. public int getNumberOfSignaturesRequired() { return members.size() / 2 + 1; } @@ -94,7 +94,7 @@ public long getCreationBlockNumber() { public abstract Script getRedeemScript(); public Script getP2SHScript() { if (p2shScript == null) { - p2shScript = ScriptBuilder.createP2SHOutputScript(getNumberOfSignaturesRequired(), getBtcPublicKeys()); + p2shScript = ScriptBuilder.createP2SHOutputScript(getRedeemScript()); } return p2shScript; @@ -140,20 +140,10 @@ public boolean isMember(FederationMember federationMember){ return this.members.contains(federationMember); } + // TODO: take a look at this + @Override public String toString() { return String.format("%d of %d signatures federation", getNumberOfSignaturesRequired(), members.size()); } - public abstract boolean equals(Object other); - - public int hashCode() { - // Can use java.util.Objects.hash since all of Instant, int and List have - // well-defined hashCode()s - return Objects.hash( - getCreationTime(), - this.creationBlockNumber, - getNumberOfSignaturesRequired(), - getBtcPublicKeys() - ); - } } diff --git a/rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java b/rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java index 5ad206140c1..f58a5ea5155 100644 --- a/rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java +++ b/rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java @@ -7,6 +7,8 @@ import co.rsk.bitcoinj.script.ScriptBuilder; import java.time.Instant; import java.util.List; + +import org.bouncycastle.util.encoders.Hex; import org.ethereum.config.blockchain.upgrades.ActivationConfig; import org.ethereum.config.blockchain.upgrades.ConsensusRule; import org.slf4j.Logger; @@ -15,6 +17,7 @@ @Deprecated public class LegacyErpFederation extends ErpFederation { private static final Logger logger = LoggerFactory.getLogger(LegacyErpFederation.class); + protected static final byte[] ERP_TESTNET_REDEEM_SCRIPT_BYTES = Hex.decode("6453210208f40073a9e43b3e9103acec79767a6de9b0409749884e989960fee578012fce210225e892391625854128c5c4ea4340de0c2a70570f33db53426fc9c746597a03f42102afc230c2d355b1a577682b07bc2646041b5d0177af0f98395a46018da699b6da210344a3c38cd59afcba3edcebe143e025574594b001700dec41e59409bdbd0f2a0921039a060badbeb24bee49eb2063f616c0f0f0765d4ca646b20a88ce828f259fcdb955670300cd50b27552210216c23b2ea8e4f11c3f9e22711addb1d16a93964796913830856b568cc3ea21d3210275562901dd8faae20de0a4166362a4f82188db77dbed4ca887422ea1ec185f1421034db69f2112f4fb1bb6141bf6e2bd6631f0484d0bd95b16767902c9fe219d4a6f5368ae"); public LegacyErpFederation( List members, @@ -26,6 +29,8 @@ public LegacyErpFederation( ActivationConfig.ForBlock activations) { super(members, creationTime, creationBlockNumber, btcParams, erpPubKeys, activationDelay, activations); + + validateRedeemScript(); } @Override @@ -60,6 +65,18 @@ public Script getStandardRedeemScript() { return standardRedeemScript; } + private void validateRedeemScript() { + if (activations.isActive(ConsensusRule.RSKIP293) && + this.redeemScript.equals(new Script(ERP_TESTNET_REDEEM_SCRIPT_BYTES))) { + + String message = "Unable to create ERP Federation. The obtained redeem script matches the one hardcoded for testnet. " + + "This would cause bitcoinj-thin to identify it as invalid"; + logger.debug("[validateRedeemScript] {}", message); + throw new FederationCreationException(message); + } + } + + // TODO: define what it means that two federations are "equal" @Override public boolean equals(Object other) { if (this == other) { diff --git a/rskj-core/src/main/java/co/rsk/peg/P2shErpFederation.java b/rskj-core/src/main/java/co/rsk/peg/P2shErpFederation.java index 85fcdc299ff..bbf3027c2ae 100644 --- a/rskj-core/src/main/java/co/rsk/peg/P2shErpFederation.java +++ b/rskj-core/src/main/java/co/rsk/peg/P2shErpFederation.java @@ -29,13 +29,6 @@ public P2shErpFederation( @Override public final Script getRedeemScript() { - // not sure if i should add this here or not. I think not but just in case - if (!activations.isActive(ConsensusRule.RSKIP284) && - btcParams.getId().equals(NetworkParameters.ID_TESTNET)) { - logger.debug("[getRedeemScript] Returning hardcoded redeem script"); - return new Script(ERP_TESTNET_REDEEM_SCRIPT_BYTES); - } - if (redeemScript == null) { logger.debug("[getRedeemScript] Creating the redeem script from the keys"); redeemScript = P2shErpFederationRedeemScriptParser.createP2shErpRedeemScript( @@ -58,6 +51,8 @@ public final Script getStandardRedeemScript() { return standardRedeemScript; } + + // TODO: define what it means that two federations are "equal" @Override public boolean equals(Object other) { if (this == other) { diff --git a/rskj-core/src/main/java/co/rsk/peg/P2shP2wshErpFederation.java b/rskj-core/src/main/java/co/rsk/peg/P2shP2wshErpFederation.java deleted file mode 100644 index 5d4771ab314..00000000000 --- a/rskj-core/src/main/java/co/rsk/peg/P2shP2wshErpFederation.java +++ /dev/null @@ -1,86 +0,0 @@ -/* -package co.rsk.peg; - -import co.rsk.bitcoinj.core.BtcECKey; -import co.rsk.bitcoinj.core.NetworkParameters; -import co.rsk.bitcoinj.script.P2shP2wshErpFederationRedeemScriptParser; -import co.rsk.bitcoinj.script.Script; -import co.rsk.bitcoinj.script.ScriptBuilder; -import org.ethereum.config.blockchain.upgrades.ActivationConfig; -import org.ethereum.config.blockchain.upgrades.ConsensusRule; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.time.Instant; -import java.util.List; - -public class P2shP2wshErpFederation extends ErpFederation { - private static final Logger logger = LoggerFactory.getLogger(co.rsk.peg.P2shP2wshErpFederation.class); - - public P2shP2wshErpFederation( - List members, - Instant creationTime, - long creationBlockNumber, - NetworkParameters btcParams, - List erpPubKeys, - long activationDelay, - ActivationConfig.ForBlock activations - ) { - super(members, creationTime, creationBlockNumber, btcParams, erpPubKeys, activationDelay, activations); - } - - @Override - public final Script getRedeemScript() { - // not sure if i should add this here or not. I think not but just in case - if (!activations.isActive(ConsensusRule.RSKIP284) && - btcParams.getId().equals(NetworkParameters.ID_TESTNET)) { - logger.debug("[getRedeemScript] Returning hardcoded redeem script"); - return new Script(ERP_TESTNET_REDEEM_SCRIPT_BYTES); - } - - if (redeemScript == null) { - logger.debug("[getRedeemScript] Creating the redeem script from the keys"); - redeemScript = P2shP2wshErpFederationRedeemScriptParser.createP2shP2wshErpRedeemScript( - ScriptBuilder.createRedeemScript(getNumberOfSignaturesRequired(), getBtcPublicKeys()), - ScriptBuilder.createRedeemScript(erpPubKeys.size() / 2 + 1, erpPubKeys), - activationDelay - ); - } - - return redeemScript; - } - - @Override - public final Script getStandardRedeemScript() { - if (standardRedeemScript == null) { - standardRedeemScript = P2shP2wshErpFederationRedeemScriptParser.extractStandardRedeemScript( - getRedeemScript().getChunks() - ); - } - return standardRedeemScript; - } - - @Override - public boolean equals(Object other) { - if (this == other) { - return true; - } - - if (other == null || this.getClass() != other.getClass()) { - return false; - } - - P2shP2wshErpFederation otherP2shErpFederation = (P2shP2wshErpFederation) other; - - return this.getNumberOfSignaturesRequired() == otherP2shErpFederation.getNumberOfSignaturesRequired() && - this.getSize() == otherP2shErpFederation.getSize() && - this.getCreationTime().equals(otherP2shErpFederation.getCreationTime()) && - this.creationBlockNumber == otherP2shErpFederation.creationBlockNumber && - this.btcParams.equals(otherP2shErpFederation.btcParams) && - this.members.equals(otherP2shErpFederation.members) && - this.getRedeemScript().equals(otherP2shErpFederation.getRedeemScript()) && - this.erpPubKeys.equals(otherP2shErpFederation.erpPubKeys) && - this.activationDelay == otherP2shErpFederation.activationDelay; - } - } -*/ diff --git a/rskj-core/src/main/java/co/rsk/peg/StandardMultisigFederation.java b/rskj-core/src/main/java/co/rsk/peg/StandardMultisigFederation.java index d2fb83c61ec..bc210ea9a92 100644 --- a/rskj-core/src/main/java/co/rsk/peg/StandardMultisigFederation.java +++ b/rskj-core/src/main/java/co/rsk/peg/StandardMultisigFederation.java @@ -24,12 +24,12 @@ import java.time.Instant; import java.util.List; +import java.util.Objects; /** * Immutable representation of an RSK Federation in the context of * a specific BTC network. * - * @author Ariel Mendelzon */ public class StandardMultisigFederation extends Federation { @@ -52,6 +52,7 @@ public Script getRedeemScript() { return redeemScript; } + // TODO: define what it means that two federations are "equal" @Override public boolean equals(Object other) { if (this == other) { @@ -72,4 +73,16 @@ public boolean equals(Object other) { this.members.equals(otherFederation.members) && this.getRedeemScript().equals(otherFederation.getRedeemScript()); } + + @Override + public int hashCode() { + // Can use java.util.Objects.hash since all of Instant, int and List have + // well-defined hashCode()s + return Objects.hash( + getCreationTime(), + this.creationBlockNumber, + getNumberOfSignaturesRequired(), + getBtcPublicKeys() + ); + } } From 1e04ebd2256f187e90d2cc535da7c667ac4838d3 Mon Sep 17 00:00:00 2001 From: julia zack Date: Thu, 28 Sep 2023 19:22:21 -0300 Subject: [PATCH 04/27] Modify getNumberOfSignaturesRequired method. Fix some tests --- .../java/co/rsk/config/BridgeDevNetConstants.java | 3 ++- .../java/co/rsk/config/BridgeMainNetConstants.java | 3 ++- .../java/co/rsk/config/BridgeRegTestConstants.java | 3 ++- .../java/co/rsk/config/BridgeTestNetConstants.java | 3 ++- .../src/main/java/co/rsk/peg/ErpFederation.java | 12 ++++++++++++ rskj-core/src/main/java/co/rsk/peg/Federation.java | 9 ++------- .../main/java/co/rsk/peg/LegacyErpFederation.java | 3 ++- .../src/main/java/co/rsk/peg/P2shErpFederation.java | 4 ++-- .../java/co/rsk/peg/StandardMultisigFederation.java | 12 ++++++++++++ .../test/java/co/rsk/peg/P2shErpFederationTest.java | 2 +- 10 files changed, 39 insertions(+), 15 deletions(-) diff --git a/rskj-core/src/main/java/co/rsk/config/BridgeDevNetConstants.java b/rskj-core/src/main/java/co/rsk/config/BridgeDevNetConstants.java index c625c6bed4e..72f1a69372c 100644 --- a/rskj-core/src/main/java/co/rsk/config/BridgeDevNetConstants.java +++ b/rskj-core/src/main/java/co/rsk/config/BridgeDevNetConstants.java @@ -24,6 +24,7 @@ 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; @@ -58,7 +59,7 @@ public BridgeDevNetConstants(List federationPublicKeys) { // Expected federation address is: // 2NCEo1RdmGDj6MqiipD6DUSerSxKv79FNWX - genesisFederation = new Federation( + genesisFederation = new StandardMultisigFederation( federationMembers, genesisFederationAddressCreatedAt, 1L, diff --git a/rskj-core/src/main/java/co/rsk/config/BridgeMainNetConstants.java b/rskj-core/src/main/java/co/rsk/config/BridgeMainNetConstants.java index 0c55020e5e7..b8c117f4e9d 100644 --- a/rskj-core/src/main/java/co/rsk/config/BridgeMainNetConstants.java +++ b/rskj-core/src/main/java/co/rsk/config/BridgeMainNetConstants.java @@ -6,6 +6,7 @@ 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; @@ -53,7 +54,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, diff --git a/rskj-core/src/main/java/co/rsk/config/BridgeRegTestConstants.java b/rskj-core/src/main/java/co/rsk/config/BridgeRegTestConstants.java index feba5514fac..91a8cf44c9c 100644 --- a/rskj-core/src/main/java/co/rsk/config/BridgeRegTestConstants.java +++ b/rskj-core/src/main/java/co/rsk/config/BridgeRegTestConstants.java @@ -24,6 +24,7 @@ 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; @@ -56,7 +57,7 @@ public BridgeRegTestConstants(List federationPublicKeys) { Instant genesisFederationCreatedAt = ZonedDateTime.parse("2016-01-01T00:00:00Z").toInstant(); - genesisFederation = new Federation( + genesisFederation = new StandardMultisigFederation( federationMembers, genesisFederationCreatedAt, 1L, diff --git a/rskj-core/src/main/java/co/rsk/config/BridgeTestNetConstants.java b/rskj-core/src/main/java/co/rsk/config/BridgeTestNetConstants.java index dcb96de245d..f5253cb4769 100644 --- a/rskj-core/src/main/java/co/rsk/config/BridgeTestNetConstants.java +++ b/rskj-core/src/main/java/co/rsk/config/BridgeTestNetConstants.java @@ -24,6 +24,7 @@ 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; @@ -61,7 +62,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, diff --git a/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java b/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java index e32509e1b0c..f584a468612 100644 --- a/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java +++ b/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java @@ -4,6 +4,7 @@ import co.rsk.bitcoinj.core.NetworkParameters; import co.rsk.bitcoinj.script.Script; import co.rsk.bitcoinj.script.ScriptBuilder; +import co.rsk.bitcoinj.script.ScriptChunk; import co.rsk.peg.utils.EcKeyUtils; import java.time.Instant; import java.util.Collections; @@ -48,6 +49,17 @@ public long getActivationDelay() { public abstract Script getStandardRedeemScript(); + @Override + public int getNumberOfSignaturesRequired() { + List standardRedeemScriptChunks = getStandardRedeemScript().getChunks(); + + // the threshold of a multisig is the first chunk of the redeemScript + // and the standardRedeemScript represents a multisig + ScriptChunk thresholdChunk = standardRedeemScriptChunks.get(0); + String thresholdString = thresholdChunk.toString(); + return thresholdString.charAt(thresholdString.length() - 1); + } + public Script getStandardP2SHScript() { if (standardP2SHScript == null) { standardP2SHScript = ScriptBuilder.createP2SHOutputScript(getStandardRedeemScript()); diff --git a/rskj-core/src/main/java/co/rsk/peg/Federation.java b/rskj-core/src/main/java/co/rsk/peg/Federation.java index 60d8c12fc29..644214e1dce 100644 --- a/rskj-core/src/main/java/co/rsk/peg/Federation.java +++ b/rskj-core/src/main/java/co/rsk/peg/Federation.java @@ -75,9 +75,7 @@ public List getBtcPublicKeys() { } // TODO: take a look at this. We should check the threshold at the redeemScript. - public int getNumberOfSignaturesRequired() { - return members.size() / 2 + 1; - } + public abstract int getNumberOfSignaturesRequired(); public Instant getCreationTime() { return creationTime; @@ -102,9 +100,6 @@ public Script getP2SHScript() { public Address getAddress() { if (address == null) { - // for now, it is ok to this method to be like this. - // when we add bech32 it should change, because the calculation of the address - // would be different. address = Address.fromP2SHScript(btcParams, getP2SHScript()); } @@ -143,7 +138,7 @@ public boolean isMember(FederationMember federationMember){ // TODO: take a look at this @Override public String toString() { - return String.format("%d of %d signatures federation", getNumberOfSignaturesRequired(), members.size()); + return String.format("Got federation with address %s", getAddress()); } } diff --git a/rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java b/rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java index f58a5ea5155..afbce018e28 100644 --- a/rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java +++ b/rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java @@ -8,6 +8,7 @@ import java.time.Instant; import java.util.List; +import co.rsk.bitcoinj.script.ScriptChunk; import org.bouncycastle.util.encoders.Hex; import org.ethereum.config.blockchain.upgrades.ActivationConfig; import org.ethereum.config.blockchain.upgrades.ConsensusRule; @@ -76,7 +77,7 @@ private void validateRedeemScript() { } } - // TODO: define what it means that two federations are "equal" + // TODO: define what it means that two federations are "equal". isnt it enough with the address? @Override public boolean equals(Object other) { if (this == other) { diff --git a/rskj-core/src/main/java/co/rsk/peg/P2shErpFederation.java b/rskj-core/src/main/java/co/rsk/peg/P2shErpFederation.java index bbf3027c2ae..ce09fd6d7b3 100644 --- a/rskj-core/src/main/java/co/rsk/peg/P2shErpFederation.java +++ b/rskj-core/src/main/java/co/rsk/peg/P2shErpFederation.java @@ -8,7 +8,6 @@ import java.time.Instant; import java.util.List; import org.ethereum.config.blockchain.upgrades.ActivationConfig; -import org.ethereum.config.blockchain.upgrades.ConsensusRule; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -65,7 +64,8 @@ public boolean equals(Object other) { P2shErpFederation otherP2shErpFederation = (P2shErpFederation) other; - return this.getNumberOfSignaturesRequired() == otherP2shErpFederation.getNumberOfSignaturesRequired() && + return //this.getAddress() == otherP2shErpFederation.getAddress(); + this.getNumberOfSignaturesRequired() == otherP2shErpFederation.getNumberOfSignaturesRequired() && this.getSize() == otherP2shErpFederation.getSize() && this.getCreationTime().equals(otherP2shErpFederation.getCreationTime()) && this.creationBlockNumber == otherP2shErpFederation.creationBlockNumber && diff --git a/rskj-core/src/main/java/co/rsk/peg/StandardMultisigFederation.java b/rskj-core/src/main/java/co/rsk/peg/StandardMultisigFederation.java index bc210ea9a92..82b2a987303 100644 --- a/rskj-core/src/main/java/co/rsk/peg/StandardMultisigFederation.java +++ b/rskj-core/src/main/java/co/rsk/peg/StandardMultisigFederation.java @@ -21,6 +21,7 @@ import co.rsk.bitcoinj.core.NetworkParameters; import co.rsk.bitcoinj.script.Script; import co.rsk.bitcoinj.script.ScriptBuilder; +import co.rsk.bitcoinj.script.ScriptChunk; import java.time.Instant; import java.util.List; @@ -52,6 +53,17 @@ public Script getRedeemScript() { return redeemScript; } + @Override + public int getNumberOfSignaturesRequired() { + List standardRedeemScriptChunks = getRedeemScript().getChunks(); + + // the threshold of a multisig is the first chunk of the redeemScript + // and the standardRedeemScript represents a multisig + ScriptChunk thresholdChunk = standardRedeemScriptChunks.get(0); + String thresholdString = thresholdChunk.toString(); + return (int) thresholdString.charAt(thresholdString.length() - 1); + } + // TODO: define what it means that two federations are "equal" @Override public boolean equals(Object other) { diff --git a/rskj-core/src/test/java/co/rsk/peg/P2shErpFederationTest.java b/rskj-core/src/test/java/co/rsk/peg/P2shErpFederationTest.java index 6d718db995d..01e2b0c640d 100644 --- a/rskj-core/src/test/java/co/rsk/peg/P2shErpFederationTest.java +++ b/rskj-core/src/test/java/co/rsk/peg/P2shErpFederationTest.java @@ -73,7 +73,7 @@ void getStandardRedeemscript() { ActivationConfig.ForBlock activations = ActivationConfigsForTest.all().forBlock(0); // Create a legacy powpeg and then a p2sh valid one. Both of them should produce the same standard redeem script - Federation legacyFed = new Federation( + StandardMultisigFederation legacyFed = new StandardMultisigFederation( members, creationTime, creationBlock, From de46d4205bac3f4fa9a868dafbd904065f8602f7 Mon Sep 17 00:00:00 2001 From: julia zack Date: Thu, 28 Sep 2023 19:38:15 -0300 Subject: [PATCH 05/27] Fix getNumberOfSignaturesRequired --- rskj-core/src/main/java/co/rsk/peg/ErpFederation.java | 3 +-- .../src/main/java/co/rsk/peg/StandardMultisigFederation.java | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java b/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java index f584a468612..9bbd52f14d1 100644 --- a/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java +++ b/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java @@ -56,8 +56,7 @@ public int getNumberOfSignaturesRequired() { // the threshold of a multisig is the first chunk of the redeemScript // and the standardRedeemScript represents a multisig ScriptChunk thresholdChunk = standardRedeemScriptChunks.get(0); - String thresholdString = thresholdChunk.toString(); - return thresholdString.charAt(thresholdString.length() - 1); + return Integer.parseInt(thresholdChunk.toString()); } public Script getStandardP2SHScript() { diff --git a/rskj-core/src/main/java/co/rsk/peg/StandardMultisigFederation.java b/rskj-core/src/main/java/co/rsk/peg/StandardMultisigFederation.java index 82b2a987303..d0bd0e8c917 100644 --- a/rskj-core/src/main/java/co/rsk/peg/StandardMultisigFederation.java +++ b/rskj-core/src/main/java/co/rsk/peg/StandardMultisigFederation.java @@ -60,8 +60,7 @@ public int getNumberOfSignaturesRequired() { // the threshold of a multisig is the first chunk of the redeemScript // and the standardRedeemScript represents a multisig ScriptChunk thresholdChunk = standardRedeemScriptChunks.get(0); - String thresholdString = thresholdChunk.toString(); - return (int) thresholdString.charAt(thresholdString.length() - 1); + return Integer.parseInt(thresholdChunk.toString()); } // TODO: define what it means that two federations are "equal" From daabd8892e09adf3c11437c3d0cd62c48467a9ea Mon Sep 17 00:00:00 2001 From: julia zack Date: Fri, 29 Sep 2023 12:08:39 -0300 Subject: [PATCH 06/27] Refactor following the diagram --- .../main/java/co/rsk/peg/ErpFederation.java | 17 ++++++----- .../src/main/java/co/rsk/peg/Federation.java | 15 +++++++++- .../java/co/rsk/peg/LegacyErpFederation.java | 26 ----------------- .../java/co/rsk/peg/P2shErpFederation.java | 28 ------------------- .../rsk/peg/StandardMultisigFederation.java | 27 +----------------- 5 files changed, 23 insertions(+), 90 deletions(-) diff --git a/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java b/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java index 9bbd52f14d1..6a8ba7a14b1 100644 --- a/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java +++ b/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java @@ -49,7 +49,14 @@ public long getActivationDelay() { public abstract Script getStandardRedeemScript(); - @Override + public Script getStandardP2SHScript() { + if (standardP2SHScript == null) { + standardP2SHScript = ScriptBuilder.createP2SHOutputScript(getStandardRedeemScript()); + } + + return standardP2SHScript; + } + public int getNumberOfSignaturesRequired() { List standardRedeemScriptChunks = getStandardRedeemScript().getChunks(); @@ -59,14 +66,6 @@ public int getNumberOfSignaturesRequired() { return Integer.parseInt(thresholdChunk.toString()); } - public Script getStandardP2SHScript() { - if (standardP2SHScript == null) { - standardP2SHScript = ScriptBuilder.createP2SHOutputScript(getStandardRedeemScript()); - } - - return standardP2SHScript; - } - @Override public int hashCode() { // Can use java.util.Objects.hash since all of Instant, int and List have diff --git a/rskj-core/src/main/java/co/rsk/peg/Federation.java b/rskj-core/src/main/java/co/rsk/peg/Federation.java index 644214e1dce..a74daecdf5e 100644 --- a/rskj-core/src/main/java/co/rsk/peg/Federation.java +++ b/rskj-core/src/main/java/co/rsk/peg/Federation.java @@ -74,7 +74,6 @@ public List getBtcPublicKeys() { .collect(Collectors.toList()); } - // TODO: take a look at this. We should check the threshold at the redeemScript. public abstract int getNumberOfSignaturesRequired(); public Instant getCreationTime() { @@ -141,4 +140,18 @@ public String toString() { return String.format("Got federation with address %s", getAddress()); } + public boolean equals(Object other){ + if (this == other) { + return true; + } + + if (other == null || this.getClass() != other.getClass()) { + return false; + } + + Federation otherFederation = (Federation) other; + return this.getAddress() == otherFederation.getAddress(); + } + + public abstract int hashCode(); } diff --git a/rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java b/rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java index afbce018e28..09b78b5e5bd 100644 --- a/rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java +++ b/rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java @@ -34,7 +34,6 @@ public LegacyErpFederation( validateRedeemScript(); } - @Override public Script getRedeemScript() { if (!activations.isActive(ConsensusRule.RSKIP284) && btcParams.getId().equals(NetworkParameters.ID_TESTNET)) { @@ -54,7 +53,6 @@ public Script getRedeemScript() { return redeemScript; } - @Override public Script getStandardRedeemScript() { if (standardRedeemScript == null) { // i think is not quite right to just "fix" the standardRedeemScript in the buggy fed. @@ -76,29 +74,5 @@ private void validateRedeemScript() { throw new FederationCreationException(message); } } - - // TODO: define what it means that two federations are "equal". isnt it enough with the address? - @Override - public boolean equals(Object other) { - if (this == other) { - return true; - } - - if (other == null || this.getClass() != other.getClass()) { - return false; - } - - LegacyErpFederation otherErpFederation = (LegacyErpFederation) other; - - return this.getNumberOfSignaturesRequired() == otherErpFederation.getNumberOfSignaturesRequired() && - this.getSize() == otherErpFederation.getSize() && - this.getCreationTime().equals(otherErpFederation.getCreationTime()) && - this.creationBlockNumber == otherErpFederation.creationBlockNumber && - this.btcParams.equals(otherErpFederation.btcParams) && - this.members.equals(otherErpFederation.members) && - this.getRedeemScript().equals(otherErpFederation.getRedeemScript()) && - this.erpPubKeys.equals(otherErpFederation.erpPubKeys) && - this.activationDelay == otherErpFederation.activationDelay; - } } diff --git a/rskj-core/src/main/java/co/rsk/peg/P2shErpFederation.java b/rskj-core/src/main/java/co/rsk/peg/P2shErpFederation.java index ce09fd6d7b3..58f97f54a8e 100644 --- a/rskj-core/src/main/java/co/rsk/peg/P2shErpFederation.java +++ b/rskj-core/src/main/java/co/rsk/peg/P2shErpFederation.java @@ -26,7 +26,6 @@ public P2shErpFederation( super(members, creationTime, creationBlockNumber, btcParams, erpPubKeys, activationDelay, activations); } - @Override public final Script getRedeemScript() { if (redeemScript == null) { logger.debug("[getRedeemScript] Creating the redeem script from the keys"); @@ -40,7 +39,6 @@ public final Script getRedeemScript() { return redeemScript; } - @Override public final Script getStandardRedeemScript() { if (standardRedeemScript == null) { standardRedeemScript = P2shErpFederationRedeemScriptParser.extractStandardRedeemScript( @@ -49,30 +47,4 @@ public final Script getStandardRedeemScript() { } return standardRedeemScript; } - - - // TODO: define what it means that two federations are "equal" - @Override - public boolean equals(Object other) { - if (this == other) { - return true; - } - - if (other == null || this.getClass() != other.getClass()) { - return false; - } - - P2shErpFederation otherP2shErpFederation = (P2shErpFederation) other; - - return //this.getAddress() == otherP2shErpFederation.getAddress(); - this.getNumberOfSignaturesRequired() == otherP2shErpFederation.getNumberOfSignaturesRequired() && - this.getSize() == otherP2shErpFederation.getSize() && - this.getCreationTime().equals(otherP2shErpFederation.getCreationTime()) && - this.creationBlockNumber == otherP2shErpFederation.creationBlockNumber && - this.btcParams.equals(otherP2shErpFederation.btcParams) && - this.members.equals(otherP2shErpFederation.members) && - this.getRedeemScript().equals(otherP2shErpFederation.getRedeemScript()) && - this.erpPubKeys.equals(otherP2shErpFederation.erpPubKeys) && - this.activationDelay == otherP2shErpFederation.activationDelay; - } } diff --git a/rskj-core/src/main/java/co/rsk/peg/StandardMultisigFederation.java b/rskj-core/src/main/java/co/rsk/peg/StandardMultisigFederation.java index d0bd0e8c917..1b4291a8c28 100644 --- a/rskj-core/src/main/java/co/rsk/peg/StandardMultisigFederation.java +++ b/rskj-core/src/main/java/co/rsk/peg/StandardMultisigFederation.java @@ -44,7 +44,6 @@ public StandardMultisigFederation( super(members, creationTime, creationBlockNumber, btcParams); } - @Override public Script getRedeemScript() { if (redeemScript == null) { redeemScript = ScriptBuilder.createRedeemScript(getNumberOfSignaturesRequired(), getBtcPublicKeys()); @@ -53,39 +52,15 @@ public Script getRedeemScript() { return redeemScript; } - @Override public int getNumberOfSignaturesRequired() { List standardRedeemScriptChunks = getRedeemScript().getChunks(); // the threshold of a multisig is the first chunk of the redeemScript - // and the standardRedeemScript represents a multisig + // and this fed is a standard multisig ScriptChunk thresholdChunk = standardRedeemScriptChunks.get(0); return Integer.parseInt(thresholdChunk.toString()); } - // TODO: define what it means that two federations are "equal" - @Override - public boolean equals(Object other) { - if (this == other) { - return true; - } - - if (other == null || this.getClass() != other.getClass()) { - return false; - } - - StandardMultisigFederation otherFederation = (StandardMultisigFederation) other; - - return this.getNumberOfSignaturesRequired() == otherFederation.getNumberOfSignaturesRequired() && - this.getSize() == otherFederation.getSize() && - this.getCreationTime().equals(otherFederation.getCreationTime()) && - this.creationBlockNumber == otherFederation.creationBlockNumber && - this.btcParams.equals(otherFederation.btcParams) && - this.members.equals(otherFederation.members) && - this.getRedeemScript().equals(otherFederation.getRedeemScript()); - } - - @Override public int hashCode() { // Can use java.util.Objects.hash since all of Instant, int and List have // well-defined hashCode()s From efcb50b2c95dc1ef00e57b54f3f97cc60fa84bd1 Mon Sep 17 00:00:00 2001 From: julia zack Date: Fri, 29 Sep 2023 12:35:48 -0300 Subject: [PATCH 07/27] Fix building non-tests-related errors --- rskj-core/src/main/java/co/rsk/config/BridgeConstants.java | 6 ++++-- .../src/main/java/co/rsk/peg/BridgeSerializationUtils.java | 4 ++-- rskj-core/src/main/java/co/rsk/peg/BridgeSupport.java | 2 +- rskj-core/src/main/java/co/rsk/peg/BridgeUtils.java | 4 ++-- rskj-core/src/main/java/co/rsk/peg/PendingFederation.java | 4 ++-- 5 files changed, 11 insertions(+), 9 deletions(-) diff --git a/rskj-core/src/main/java/co/rsk/config/BridgeConstants.java b/rskj-core/src/main/java/co/rsk/config/BridgeConstants.java index 11329b1accf..fae35ee98b9 100644 --- a/rskj-core/src/main/java/co/rsk/config/BridgeConstants.java +++ b/rskj-core/src/main/java/co/rsk/config/BridgeConstants.java @@ -24,13 +24,15 @@ import co.rsk.peg.AddressBasedAuthorizer; import co.rsk.peg.Federation; import java.util.List; + +import co.rsk.peg.StandardMultisigFederation; import org.ethereum.config.blockchain.upgrades.ActivationConfig; import org.ethereum.config.blockchain.upgrades.ConsensusRule; public abstract class BridgeConstants { protected String btcParamsString; - protected Federation genesisFederation; + protected StandardMultisigFederation genesisFederation; protected int btc2RskMinimumAcceptableConfirmations; protected int btc2RskMinimumAcceptableConfirmationsOnRsk; @@ -94,7 +96,7 @@ public String getBtcParamsString() { return btcParamsString; } - public Federation getGenesisFederation() { return genesisFederation; } + public StandardMultisigFederation getGenesisFederation() { return genesisFederation; } public int getBtc2RskMinimumAcceptableConfirmations() { return btc2RskMinimumAcceptableConfirmations; diff --git a/rskj-core/src/main/java/co/rsk/peg/BridgeSerializationUtils.java b/rskj-core/src/main/java/co/rsk/peg/BridgeSerializationUtils.java index 852d04602b4..aed12514fe8 100644 --- a/rskj-core/src/main/java/co/rsk/peg/BridgeSerializationUtils.java +++ b/rskj-core/src/main/java/co/rsk/peg/BridgeSerializationUtils.java @@ -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); } /** @@ -326,7 +326,7 @@ public static ErpFederation deserializeErpFederation( BridgeSerializationUtils::deserializeFederationMember ); - return new ErpFederation( + return new LegacyErpFederation( federation.getMembers(), federation.creationTime, federation.getCreationBlockNumber(), diff --git a/rskj-core/src/main/java/co/rsk/peg/BridgeSupport.java b/rskj-core/src/main/java/co/rsk/peg/BridgeSupport.java index ec6d8515c15..b60bb52da9e 100644 --- a/rskj-core/src/main/java/co/rsk/peg/BridgeSupport.java +++ b/rskj-core/src/main/java/co/rsk/peg/BridgeSupport.java @@ -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); } diff --git a/rskj-core/src/main/java/co/rsk/peg/BridgeUtils.java b/rskj-core/src/main/java/co/rsk/peg/BridgeUtils.java index 0a1035365f0..80cadbb1813 100644 --- a/rskj-core/src/main/java/co/rsk/peg/BridgeUtils.java +++ b/rskj-core/src/main/java/co/rsk/peg/BridgeUtils.java @@ -402,7 +402,7 @@ 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; } @@ -490,7 +490,7 @@ private static boolean isPegOutTx(BtcTransaction tx, Federation federation, Acti } public static boolean isPegOutTx(BtcTransaction tx, List 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).getStandardRedeemScript() : fed.getRedeemScript()).toArray(Script[]::new)); } public static boolean isPegOutTx(BtcTransaction tx, ActivationConfig.ForBlock activations, Script... p2shScript) { diff --git a/rskj-core/src/main/java/co/rsk/peg/PendingFederation.java b/rskj-core/src/main/java/co/rsk/peg/PendingFederation.java index 33e388bed43..c0ddd49878e 100644 --- a/rskj-core/src/main/java/co/rsk/peg/PendingFederation.java +++ b/rskj-core/src/main/java/co/rsk/peg/PendingFederation.java @@ -119,7 +119,7 @@ public Federation buildFederation( if (activations.isActive(ConsensusRule.RSKIP201)) { logger.info("[buildFederation] Going to create an ERP Federation"); - return new ErpFederation( + return new LegacyErpFederation( members, creationTime, blockNumber, @@ -130,7 +130,7 @@ public Federation buildFederation( ); } - return new Federation( + return new StandardMultisigFederation( members, creationTime, blockNumber, From d32cd4f0e2e52fbb99a77b0890f749e44872be68 Mon Sep 17 00:00:00 2001 From: julia zack Date: Sat, 30 Sep 2023 10:00:36 -0300 Subject: [PATCH 08/27] Fix tests --- .../main/java/co/rsk/peg/ErpFederation.java | 1 + .../java/co/rsk/peg/LegacyErpFederation.java | 2 + .../java/co/rsk/peg/P2shErpFederation.java | 2 + .../rsk/peg/StandardMultisigFederation.java | 3 + .../rsk/peg/BridgeSerializationUtilsTest.java | 10 +- .../BridgeStorageProviderFederationTests.java | 4 +- .../co/rsk/peg/BridgeStorageProviderTest.java | 10 +- .../peg/BridgeSupportAddSignatureTest.java | 10 +- .../BridgeSupportGetTransactionTypeTest.java | 6 +- ...ridgeSupportProcessFundsMigrationTest.java | 2 +- .../rsk/peg/BridgeSupportReleaseBtcTest.java | 2 +- .../java/co/rsk/peg/BridgeSupportTest.java | 28 ++--- .../rsk/peg/BridgeSupportTestIntegration.java | 58 +++++----- .../src/test/java/co/rsk/peg/BridgeTest.java | 4 +- .../co/rsk/peg/BridgeUtilsLegacyTest.java | 6 +- .../test/java/co/rsk/peg/BridgeUtilsTest.java | 100 +++++++++--------- .../java/co/rsk/peg/ErpFederationTest.java | 46 ++++---- .../co/rsk/peg/FederationSupportTest.java | 6 +- .../test/java/co/rsk/peg/FederationTest.java | 16 +-- .../java/co/rsk/peg/FederationTestUtils.java | 2 +- ...verCompatibleBtcWalletWithStorageTest.java | 4 +- ...patibleBtcWallextWithSingleScriptTest.java | 4 +- .../test/java/co/rsk/peg/PegTestUtils.java | 2 +- .../co/rsk/peg/PendingFederationTest.java | 4 +- .../java/co/rsk/peg/PowpegMigrationTest.java | 12 +-- .../peg/ReleaseTransactionBuilderTest.java | 4 +- .../peg/performance/ActiveFederationTest.java | 7 +- .../performance/RetiringFederationTest.java | 3 +- .../peg/utils/BridgeEventLoggerImplTest.java | 4 +- .../BridgeEventLoggerLegacyImplTest.java | 4 +- 30 files changed, 186 insertions(+), 180 deletions(-) diff --git a/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java b/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java index 6a8ba7a14b1..cced1d6532b 100644 --- a/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java +++ b/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java @@ -57,6 +57,7 @@ public Script getStandardP2SHScript() { return standardP2SHScript; } + @Override public int getNumberOfSignaturesRequired() { List standardRedeemScriptChunks = getStandardRedeemScript().getChunks(); diff --git a/rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java b/rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java index 09b78b5e5bd..cfd104dcdcb 100644 --- a/rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java +++ b/rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java @@ -34,6 +34,7 @@ public LegacyErpFederation( validateRedeemScript(); } + @Override public Script getRedeemScript() { if (!activations.isActive(ConsensusRule.RSKIP284) && btcParams.getId().equals(NetworkParameters.ID_TESTNET)) { @@ -53,6 +54,7 @@ public Script getRedeemScript() { return redeemScript; } + @Override public Script getStandardRedeemScript() { if (standardRedeemScript == null) { // i think is not quite right to just "fix" the standardRedeemScript in the buggy fed. diff --git a/rskj-core/src/main/java/co/rsk/peg/P2shErpFederation.java b/rskj-core/src/main/java/co/rsk/peg/P2shErpFederation.java index 58f97f54a8e..573fe02f903 100644 --- a/rskj-core/src/main/java/co/rsk/peg/P2shErpFederation.java +++ b/rskj-core/src/main/java/co/rsk/peg/P2shErpFederation.java @@ -26,6 +26,7 @@ public P2shErpFederation( super(members, creationTime, creationBlockNumber, btcParams, erpPubKeys, activationDelay, activations); } + @Override public final Script getRedeemScript() { if (redeemScript == null) { logger.debug("[getRedeemScript] Creating the redeem script from the keys"); @@ -39,6 +40,7 @@ public final Script getRedeemScript() { return redeemScript; } + @Override public final Script getStandardRedeemScript() { if (standardRedeemScript == null) { standardRedeemScript = P2shErpFederationRedeemScriptParser.extractStandardRedeemScript( diff --git a/rskj-core/src/main/java/co/rsk/peg/StandardMultisigFederation.java b/rskj-core/src/main/java/co/rsk/peg/StandardMultisigFederation.java index 1b4291a8c28..7adb20b1339 100644 --- a/rskj-core/src/main/java/co/rsk/peg/StandardMultisigFederation.java +++ b/rskj-core/src/main/java/co/rsk/peg/StandardMultisigFederation.java @@ -44,6 +44,7 @@ public StandardMultisigFederation( super(members, creationTime, creationBlockNumber, btcParams); } + @Override public Script getRedeemScript() { if (redeemScript == null) { redeemScript = ScriptBuilder.createRedeemScript(getNumberOfSignaturesRequired(), getBtcPublicKeys()); @@ -52,6 +53,7 @@ public Script getRedeemScript() { return redeemScript; } + @Override public int getNumberOfSignaturesRequired() { List standardRedeemScriptChunks = getRedeemScript().getChunks(); @@ -61,6 +63,7 @@ public int getNumberOfSignaturesRequired() { return Integer.parseInt(thresholdChunk.toString()); } + @Override public int hashCode() { // Can use java.util.Objects.hash since all of Instant, int and List have // well-defined hashCode()s diff --git a/rskj-core/src/test/java/co/rsk/peg/BridgeSerializationUtilsTest.java b/rskj-core/src/test/java/co/rsk/peg/BridgeSerializationUtilsTest.java index efaea5539e4..730cb05ee13 100644 --- a/rskj-core/src/test/java/co/rsk/peg/BridgeSerializationUtilsTest.java +++ b/rskj-core/src/test/java/co/rsk/peg/BridgeSerializationUtilsTest.java @@ -151,7 +151,7 @@ void serializeFederationOnlyBtcKeys() throws Exception { }; // Only actual keys serialized are BTC keys, so we don't really care about RSK or MST keys - Federation federation = new Federation( + Federation federation = new StandardMultisigFederation( FederationTestUtils.getFederationMembersWithBtcKeys(Arrays.asList(new BtcECKey[]{ BtcECKey.fromPublicOnly(publicKeyBytes[0]), BtcECKey.fromPublicOnly(publicKeyBytes[1]), @@ -302,7 +302,7 @@ void serializeFederation_serializedKeysAreCompressedAndThree() { members.add(new FederationMember(new BtcECKey(), new ECKey(), new ECKey())); } - Federation testFederation = new Federation( + Federation testFederation = new StandardMultisigFederation( members, Instant.now(), 123, NetworkParameters.fromID(NetworkParameters.ID_REGTEST) ); @@ -787,7 +787,7 @@ void serializeAndDeserializeFederationOnlyBtcKeysWithRealRLP() { }; // Only actual keys serialized are BTC keys, so deserialization will fill RSK and MST keys with those - Federation federation = new Federation( + Federation federation = new StandardMultisigFederation( FederationTestUtils.getFederationMembersWithKeys(Arrays.asList( BtcECKey.fromPublicOnly(publicKeyBytes[0]), BtcECKey.fromPublicOnly(publicKeyBytes[1]), @@ -1246,7 +1246,7 @@ private void testSerializeAndDeserializeFederation( members.add(new FederationMember(new BtcECKey(), new ECKey(), new ECKey())); } - Federation testFederation = new Federation( + Federation testFederation = new StandardMultisigFederation( members, Instant.now(), 123, @@ -1259,7 +1259,7 @@ private void testSerializeAndDeserializeFederation( bridgeConstants.getBtcParams() ); - Federation testErpFederation = new ErpFederation( + Federation testErpFederation = new LegacyErpFederation( members, Instant.now(), 123, diff --git a/rskj-core/src/test/java/co/rsk/peg/BridgeStorageProviderFederationTests.java b/rskj-core/src/test/java/co/rsk/peg/BridgeStorageProviderFederationTests.java index 5f3e0cbdbd5..767ff666fcf 100644 --- a/rskj-core/src/test/java/co/rsk/peg/BridgeStorageProviderFederationTests.java +++ b/rskj-core/src/test/java/co/rsk/peg/BridgeStorageProviderFederationTests.java @@ -614,7 +614,7 @@ private Federation createFederation(int version) { activations ); case ERP_FEDERATION_FORMAT_VERSION: - return new ErpFederation( + return new LegacyErpFederation( members, Instant.now(), 1L, @@ -624,7 +624,7 @@ private Federation createFederation(int version) { activations ); default: - return new Federation( + return new StandardMultisigFederation( members, Instant.now(), 1L, diff --git a/rskj-core/src/test/java/co/rsk/peg/BridgeStorageProviderTest.java b/rskj-core/src/test/java/co/rsk/peg/BridgeStorageProviderTest.java index 66e84c07618..8d7c0c79d4b 100644 --- a/rskj-core/src/test/java/co/rsk/peg/BridgeStorageProviderTest.java +++ b/rskj-core/src/test/java/co/rsk/peg/BridgeStorageProviderTest.java @@ -402,7 +402,7 @@ void getNewFederation_multiKeyVersion() { @Test void getNewFederation_erp_fed() { Federation newFederation = buildMockFederation(100, 200, 300); - ErpFederation erpFederation = new ErpFederation( + ErpFederation erpFederation = new LegacyErpFederation( newFederation.getMembers(), newFederation.getCreationTime(), newFederation.getCreationBlockNumber(), @@ -546,7 +546,7 @@ void saveNewFederation_postMultiKey_RSKIP_201_active_erp_fed() { BridgeConstants bridgeConstants = config.getNetworkConstants().getBridgeConstants(); Federation newFederation = buildMockFederation(100, 200, 300); - ErpFederation erpFederation = new ErpFederation( + ErpFederation erpFederation = new LegacyErpFederation( newFederation.getMembers(), newFederation.getCreationTime(), newFederation.getCreationBlockNumber(), @@ -680,7 +680,7 @@ void getOldFederation_multiKeyVersion() { void getOldFederation_erp_fed() { BridgeConstants bridgeConstants = config.getNetworkConstants().getBridgeConstants(); Federation oldFederation = buildMockFederation(100, 200, 300); - ErpFederation erpFederation = new ErpFederation( + ErpFederation erpFederation = new LegacyErpFederation( oldFederation.getMembers(), oldFederation.getCreationTime(), oldFederation.getCreationBlockNumber(), @@ -820,7 +820,7 @@ void saveOldFederation_postMultikey_RSKIP_201_active_erp_fed() { BridgeConstants bridgeConstants = config.getNetworkConstants().getBridgeConstants(); Federation oldFederation = buildMockFederation(100, 200, 300); - ErpFederation erpFederation = new ErpFederation( + ErpFederation erpFederation = new LegacyErpFederation( oldFederation.getMembers(), oldFederation.getCreationTime(), oldFederation.getCreationBlockNumber(), @@ -3912,7 +3912,7 @@ private Address getBtcAddress(String addr) { } private Federation buildMockFederation(Integer... pks) { - return new Federation( + return new StandardMultisigFederation( FederationTestUtils.getFederationMembersFromPks(pks), Instant.ofEpochMilli(1000), 0L, NetworkParameters.fromID(NetworkParameters.ID_REGTEST) diff --git a/rskj-core/src/test/java/co/rsk/peg/BridgeSupportAddSignatureTest.java b/rskj-core/src/test/java/co/rsk/peg/BridgeSupportAddSignatureTest.java index 76e1a98cba5..5f4744eef9e 100644 --- a/rskj-core/src/test/java/co/rsk/peg/BridgeSupportAddSignatureTest.java +++ b/rskj-core/src/test/java/co/rsk/peg/BridgeSupportAddSignatureTest.java @@ -75,7 +75,7 @@ void addSignature_fedPubKey_belongs_to_active_federation() throws Exception { ); federation1Keys.sort(BtcECKey.PUBKEY_COMPARATOR); - Federation activeFederation = new Federation( + Federation activeFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembersWithBtcKeys(federation1Keys), Instant.ofEpochMilli(1000L), 0L, @@ -133,7 +133,7 @@ void addSignature_fedPubKey_belongs_to_retiring_federation() throws Exception { BtcECKey.fromPrivate(Hex.decode("fa02"))); federation1Keys.sort(BtcECKey.PUBKEY_COMPARATOR); - Federation retiringFederation = new Federation( + Federation retiringFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembersWithBtcKeys(federation1Keys), Instant.ofEpochMilli(1000L), 0L, @@ -147,7 +147,7 @@ void addSignature_fedPubKey_belongs_to_retiring_federation() throws Exception { ); activeFederationKeys.sort(BtcECKey.PUBKEY_COMPARATOR); - Federation activeFederation = new Federation( + Federation activeFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembersWithBtcKeys(activeFederationKeys), Instant.ofEpochMilli(1000L), 0L, @@ -191,7 +191,7 @@ void addSignature_fedPubKey_no_belong_to_retiring_or_active_federation() throws ); federation1Keys.sort(BtcECKey.PUBKEY_COMPARATOR); - Federation retiringFederation = new Federation( + Federation retiringFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembersWithBtcKeys(federation1Keys), Instant.ofEpochMilli(1000L), 0L, @@ -205,7 +205,7 @@ void addSignature_fedPubKey_no_belong_to_retiring_or_active_federation() throws ); activeFederationKeys.sort(BtcECKey.PUBKEY_COMPARATOR); - Federation activeFederation = new Federation( + Federation activeFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembersWithBtcKeys(activeFederationKeys), Instant.ofEpochMilli(1000L), 0L, diff --git a/rskj-core/src/test/java/co/rsk/peg/BridgeSupportGetTransactionTypeTest.java b/rskj-core/src/test/java/co/rsk/peg/BridgeSupportGetTransactionTypeTest.java index 8b86aaa01dd..e433dbf5eb3 100644 --- a/rskj-core/src/test/java/co/rsk/peg/BridgeSupportGetTransactionTypeTest.java +++ b/rskj-core/src/test/java/co/rsk/peg/BridgeSupportGetTransactionTypeTest.java @@ -128,7 +128,7 @@ void getTransactionType_sentFromOldFed(ActivationConfig.ForBlock activations, Pe .withProvider(provider) .build(); - Federation activeFederation = new Federation( + Federation activeFederation = new StandardMultisigFederation( bridgeRegTestConstants.getGenesisFederation().getMembers(), bridgeRegTestConstants.getGenesisFederation().getCreationTime(), 5L, @@ -258,7 +258,7 @@ void getTransactionType_pegout_tx() { new String[]{"fa01", "fa02", "fa03"}, true ); - Federation activeFederation = new Federation( + Federation activeFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembersWithBtcKeys(fedKeys), Instant.ofEpochMilli(1000L), 0L, @@ -304,7 +304,7 @@ void getTransactionType_migration_tx() { new String[]{"fa01", "fa02", "fa03"}, true );; - Federation retiringFederation = new Federation( + Federation retiringFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembersWithBtcKeys(retiringFedKeys), Instant.ofEpochMilli(1000L), 0L, diff --git a/rskj-core/src/test/java/co/rsk/peg/BridgeSupportProcessFundsMigrationTest.java b/rskj-core/src/test/java/co/rsk/peg/BridgeSupportProcessFundsMigrationTest.java index 10133a00253..6f1a565d7d3 100644 --- a/rskj-core/src/test/java/co/rsk/peg/BridgeSupportProcessFundsMigrationTest.java +++ b/rskj-core/src/test/java/co/rsk/peg/BridgeSupportProcessFundsMigrationTest.java @@ -127,7 +127,7 @@ void test_processFundsMigration( federationActivationAge + bridgeConstants.getFundsMigrationAgeSinceActivationEnd(activations) + 1; - Federation newFederation = new Federation( + Federation newFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembers(1), Instant.EPOCH, federationCreationBlockNumber, diff --git a/rskj-core/src/test/java/co/rsk/peg/BridgeSupportReleaseBtcTest.java b/rskj-core/src/test/java/co/rsk/peg/BridgeSupportReleaseBtcTest.java index 11a4cbaaa28..35cfde10491 100644 --- a/rskj-core/src/test/java/co/rsk/peg/BridgeSupportReleaseBtcTest.java +++ b/rskj-core/src/test/java/co/rsk/peg/BridgeSupportReleaseBtcTest.java @@ -1250,7 +1250,7 @@ private static Repository createRepository() { } private static Federation getFederation() { - return new Federation( + return new StandardMultisigFederation( FederationTestUtils.getFederationMembers(3), Instant.ofEpochMilli(1000), 0L, diff --git a/rskj-core/src/test/java/co/rsk/peg/BridgeSupportTest.java b/rskj-core/src/test/java/co/rsk/peg/BridgeSupportTest.java index 3a4457a91b6..7576c1c34b7 100644 --- a/rskj-core/src/test/java/co/rsk/peg/BridgeSupportTest.java +++ b/rskj-core/src/test/java/co/rsk/peg/BridgeSupportTest.java @@ -970,7 +970,7 @@ void registerBtcTransactionLockTxNotWhitelisted_before_rskip_146_activation() th BtcECKey.fromPrivate(Hex.decode("fa02")) ); - Federation federation1 = new Federation( + Federation federation1 = new StandardMultisigFederation( FederationTestUtils.getFederationMembersWithBtcKeys(federation1Keys), Instant.ofEpochMilli(1000L), 0L, @@ -983,7 +983,7 @@ void registerBtcTransactionLockTxNotWhitelisted_before_rskip_146_activation() th BtcECKey.fromPrivate(Hex.decode("fb02")), BtcECKey.fromPrivate(Hex.decode("fb03"))); - Federation federation2 = new Federation( + Federation federation2 = new StandardMultisigFederation( FederationTestUtils.getFederationMembersWithBtcKeys(federation2Keys), Instant.ofEpochMilli(2000L), 0L, @@ -1140,7 +1140,7 @@ void registerBtcTransactionLockTxNotWhitelisted_after_rskip_146_activation() thr BtcECKey.fromPrivate(Hex.decode("fa02")) ); - Federation federation1 = new Federation( + Federation federation1 = new StandardMultisigFederation( FederationTestUtils.getFederationMembersWithBtcKeys(federation1Keys), Instant.ofEpochMilli(1000L), 0L, @@ -1153,7 +1153,7 @@ void registerBtcTransactionLockTxNotWhitelisted_after_rskip_146_activation() thr BtcECKey.fromPrivate(Hex.decode("fb02")), BtcECKey.fromPrivate(Hex.decode("fb03"))); - Federation federation2 = new Federation( + Federation federation2 = new StandardMultisigFederation( FederationTestUtils.getFederationMembersWithBtcKeys(federation2Keys), Instant.ofEpochMilli(2000L), 0L, @@ -1324,7 +1324,7 @@ void registerBtcTransaction_sending_segwit_tx_twice_locks_just_once() throws Blo BtcECKey.fromPrivate(Hex.decode("fa02")) ); - Federation fed = new Federation( + Federation fed = new StandardMultisigFederation( FederationTestUtils.getFederationMembersWithBtcKeys(fedKeys), Instant.ofEpochMilli(1000L), 0L, @@ -1416,7 +1416,7 @@ void callProcessFundsMigration_is_migrating_before_rskip_146_activation() throws Federation oldFederation = bridgeConstantsRegtest.getGenesisFederation(); - Federation newFederation = new Federation( + Federation newFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembers(1), Instant.EPOCH, 5L, @@ -1479,7 +1479,7 @@ void callProcessFundsMigration_is_migrating_after_rskip_146_activation() throws Federation oldFederation = bridgeConstantsRegtest.getGenesisFederation(); - Federation newFederation = new Federation( + Federation newFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembers(1), Instant.EPOCH, 5L, @@ -1544,7 +1544,7 @@ void callProcessFundsMigration_is_migrated_before_rskip_146_activation() throws Federation oldFederation = bridgeConstantsRegtest.getGenesisFederation(); - Federation newFederation = new Federation( + Federation newFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembers(1), Instant.EPOCH, 5L, @@ -1607,7 +1607,7 @@ void callProcessFundsMigration_is_migrated_after_rskip_146_activation() throws I Federation oldFederation = bridgeConstantsRegtest.getGenesisFederation(); - Federation newFederation = new Federation( + Federation newFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembers(1), Instant.EPOCH, 5L, @@ -1671,7 +1671,7 @@ void updateFederationCreationBlockHeights_before_rskip_186_activation() throws I Federation oldFederation = bridgeConstantsRegtest.getGenesisFederation(); - Federation newFederation = new Federation( + Federation newFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembers(1), Instant.EPOCH, 5L, @@ -1734,7 +1734,7 @@ void updateFederationCreationBlockHeights_after_rskip_186_activation() throws IO Federation oldFederation = bridgeConstantsRegtest.getGenesisFederation(); - Federation newFederation = new Federation( + Federation newFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembers(1), Instant.EPOCH, 5L, @@ -2115,7 +2115,7 @@ void rskTxWaitingForSignature_fail_adding_an_already_existing_key_after_rskip_37 // Set state to make concur a pegout migration tx and pegout batch creation on the same updateCollection Federation oldFederation = bridgeConstants.getGenesisFederation(); - Federation newFederation = new Federation( + Federation newFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembers(1), Instant.EPOCH, 5L, @@ -6410,13 +6410,13 @@ private void test_migrating_many_utxos(boolean isRskip294Active, int utxosToCrea oldFedMembers.add(FederationMember.getFederationMemberFromKey(new BtcECKey())); } - Federation oldFed = new Federation( + Federation oldFed = new StandardMultisigFederation( oldFedMembers, Instant.now(), 0, btcRegTestParams ); - Federation newFed = new Federation( + Federation newFed = new StandardMultisigFederation( Arrays.asList( FederationMember.getFederationMemberFromKey(new BtcECKey()), FederationMember.getFederationMemberFromKey(new BtcECKey()), diff --git a/rskj-core/src/test/java/co/rsk/peg/BridgeSupportTestIntegration.java b/rskj-core/src/test/java/co/rsk/peg/BridgeSupportTestIntegration.java index a7e4d2663a2..c49937c0e45 100644 --- a/rskj-core/src/test/java/co/rsk/peg/BridgeSupportTestIntegration.java +++ b/rskj-core/src/test/java/co/rsk/peg/BridgeSupportTestIntegration.java @@ -584,7 +584,7 @@ void minimumProcessFundsMigrationValue() throws IOException { Federation oldFederation = bridgeConstants.getGenesisFederation(); BtcECKey key = new BtcECKey(new SecureRandom()); FederationMember member = new FederationMember(key, new ECKey(), new ECKey()); - Federation newFederation = new Federation( + Federation newFederation = new StandardMultisigFederation( Collections.singletonList(member), Instant.EPOCH, 5L, @@ -1346,14 +1346,14 @@ void registerBtcTransactionMigrationTx() throws BlockStoreException, AddressForm BtcECKey.fromPrivate(Hex.decode("fa02")) ).sorted(BtcECKey.PUBKEY_COMPARATOR).collect(Collectors.toList()); - Federation activeFederation = new Federation(FederationTestUtils.getFederationMembersWithBtcKeys(activeFederationKeys), Instant.ofEpochMilli(2000L), 2L, parameters); + Federation activeFederation = new StandardMultisigFederation(FederationTestUtils.getFederationMembersWithBtcKeys(activeFederationKeys), Instant.ofEpochMilli(2000L), 2L, parameters); List retiringFederationKeys = Stream.of( BtcECKey.fromPrivate(Hex.decode("fb01")), BtcECKey.fromPrivate(Hex.decode("fb02")) ).sorted(BtcECKey.PUBKEY_COMPARATOR).collect(Collectors.toList()); - Federation retiringFederation = new Federation(FederationTestUtils.getFederationMembersWithBtcKeys(retiringFederationKeys), Instant.ofEpochMilli(1000L), 1L, parameters); + Federation retiringFederation = new StandardMultisigFederation(FederationTestUtils.getFederationMembersWithBtcKeys(retiringFederationKeys), Instant.ofEpochMilli(1000L), 1L, parameters); Repository repository = createRepository(); repository.addBalance(PrecompiledContracts.BRIDGE_ADDR, LIMIT_MONETARY_BASE); @@ -1459,7 +1459,7 @@ void registerBtcTransactionWithCrossFederationsChange() throws Exception { .map(BtcECKey::fromPrivate) .sorted(BtcECKey.PUBKEY_COMPARATOR) .collect(Collectors.toList()); - Federation activeFederation = new Federation( + Federation activeFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembersWithBtcKeys(activeFederationKeys), Instant.ofEpochMilli(1000L), 0L, params ); @@ -1469,7 +1469,7 @@ void registerBtcTransactionWithCrossFederationsChange() throws Exception { .map(BtcECKey::fromPrivate) .sorted(BtcECKey.PUBKEY_COMPARATOR) .collect(Collectors.toList()); - Federation retiringFederation = new Federation( + Federation retiringFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembersWithBtcKeys(retiringFederationKeys), Instant.ofEpochMilli(2000L), 0L, params ); @@ -1566,7 +1566,7 @@ void registerBtcTransactionLockTxWhitelisted() throws Exception { }); federation1Keys.sort(BtcECKey.PUBKEY_COMPARATOR); - Federation federation1 = new Federation(FederationTestUtils.getFederationMembersWithBtcKeys(federation1Keys), Instant.ofEpochMilli(1000L), 0L, btcParams); + Federation federation1 = new StandardMultisigFederation(FederationTestUtils.getFederationMembersWithBtcKeys(federation1Keys), Instant.ofEpochMilli(1000L), 0L, btcParams); List federation2Keys = Arrays.asList(new BtcECKey[]{ BtcECKey.fromPrivate(Hex.decode("fb01")), @@ -1575,7 +1575,7 @@ void registerBtcTransactionLockTxWhitelisted() throws Exception { }); federation2Keys.sort(BtcECKey.PUBKEY_COMPARATOR); - Federation federation2 = new Federation(FederationTestUtils.getFederationMembersWithBtcKeys(federation2Keys), Instant.ofEpochMilli(2000L), 0L, btcParams); + Federation federation2 = new StandardMultisigFederation(FederationTestUtils.getFederationMembersWithBtcKeys(federation2Keys), Instant.ofEpochMilli(2000L), 0L, btcParams); Repository repository = createRepository(); repository.addBalance(PrecompiledContracts.BRIDGE_ADDR, LIMIT_MONETARY_BASE); @@ -1726,13 +1726,13 @@ void getBtcTxHashProcessedHeight() throws IOException, BlockStoreException { @Test void getFederationMethods_genesis() throws IOException { - Federation activeFederation = new Federation( + Federation activeFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembers(3), Instant.ofEpochMilli(1000), 0L, NetworkParameters.fromID(NetworkParameters.ID_REGTEST) ); - Federation genesisFederation = new Federation( + Federation genesisFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembers(6), Instant.ofEpochMilli(1000), 0L, @@ -1754,13 +1754,13 @@ void getFederationMethods_genesis() throws IOException { @Test void getFederationMethods_active() throws IOException { - Federation activeFederation = new Federation( + Federation activeFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembers(3), Instant.ofEpochMilli(1000), 0L, NetworkParameters.fromID(NetworkParameters.ID_REGTEST) ); - Federation genesisFederation = new Federation( + Federation genesisFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembers(6), Instant.ofEpochMilli(1000), 0L, @@ -1790,13 +1790,13 @@ void getFederationMethods_active() throws IOException { @Test void getFederationMethods_newActivated() throws IOException { - Federation newFederation = new Federation( + Federation newFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembers(3), Instant.ofEpochMilli(1000), 15L, NetworkParameters.fromID(NetworkParameters.ID_REGTEST) ); - Federation oldFederation = new Federation( + Federation oldFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembers(6), Instant.ofEpochMilli(1000), 0L, @@ -1830,13 +1830,13 @@ void getFederationMethods_newActivated() throws IOException { @Test void getFederationMethods_newNotActivated() throws IOException { - Federation newFederation = new Federation( + Federation newFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembers(3), Instant.ofEpochMilli(1000), 15L, NetworkParameters.fromID(NetworkParameters.ID_REGTEST) ); - Federation oldFederation = new Federation( + Federation oldFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembers(6), Instant.ofEpochMilli(1000), 0L, @@ -1883,14 +1883,14 @@ void getRetiringFederationMethods_none() throws IOException { @Test void getRetiringFederationMethods_presentNewInactive() throws IOException { - Federation mockedNewFederation = new Federation( + Federation mockedNewFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembers(2), Instant.ofEpochMilli(2000), 10L, NetworkParameters.fromID(NetworkParameters.ID_REGTEST) ); - Federation mockedOldFederation = new Federation( + Federation mockedOldFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembers(4), Instant.ofEpochMilli(1000), 0L, @@ -1920,14 +1920,14 @@ void getRetiringFederationMethods_presentNewInactive() throws IOException { @Test void getRetiringFederationMethods_presentNewActive() throws IOException { - Federation mockedNewFederation = new Federation( + Federation mockedNewFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembers(2), Instant.ofEpochMilli(2000), 10L, NetworkParameters.fromID(NetworkParameters.ID_REGTEST) ); - Federation mockedOldFederation = new Federation( + Federation mockedOldFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembers(4), Instant.ofEpochMilli(1000), 0L, @@ -2137,14 +2137,14 @@ void createFederation_pendingExists() throws IOException, BridgeIllegalArgumentE void createFederation_withPendingActivation() throws IOException, BridgeIllegalArgumentException { VotingMocksProvider mocksProvider = new VotingMocksProvider("create", new byte[][]{}, false); - Federation mockedNewFederation = new Federation( + Federation mockedNewFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembers(2), Instant.ofEpochMilli(2000), 10L, NetworkParameters.fromID(NetworkParameters.ID_REGTEST) ); - Federation mockedOldFederation = new Federation( + Federation mockedOldFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembers(4), Instant.ofEpochMilli(1000), 0L, @@ -2178,14 +2178,14 @@ void createFederation_withPendingActivation() throws IOException, BridgeIllegalA void createFederation_withExistingRetiringFederation() throws IOException, BridgeIllegalArgumentException { VotingMocksProvider mocksProvider = new VotingMocksProvider("create", new byte[][]{}, false); - Federation mockedNewFederation = new Federation( + Federation mockedNewFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembers(2), Instant.ofEpochMilli(2000), 10L, NetworkParameters.fromID(NetworkParameters.ID_REGTEST) ); - Federation mockedOldFederation = new Federation( + Federation mockedOldFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembers(4), Instant.ofEpochMilli(1000), 0L, @@ -2722,14 +2722,14 @@ void commitFederation_ok() throws IOException, BridgeIllegalArgumentException { when(executionBlock.getTimestamp()).thenReturn(15005L); when(executionBlock.getNumber()).thenReturn(15L); - Federation expectedFederation = new Federation(FederationTestUtils.getFederationMembersWithKeys(Arrays.asList( + Federation expectedFederation = new StandardMultisigFederation(FederationTestUtils.getFederationMembersWithKeys(Arrays.asList( BtcECKey.fromPublicOnly(Hex.decode("036bb9eab797eadc8b697f0e82a01d01cabbfaaca37e5bafc06fdc6fdd38af894a")), BtcECKey.fromPublicOnly(Hex.decode("031da807c71c2f303b7f409dd2605b297ac494a563be3b9ca5f52d95a43d183cc5")), BtcECKey.fromPublicOnly(Hex.decode("025eefeeeed5cdc40822880c7db1d0a88b7b986945ed3fc05a0b45fe166fe85e12")), BtcECKey.fromPublicOnly(Hex.decode("03c67ad63527012fd4776ae892b5dc8c56f80f1be002dc65cd520a2efb64e37b49")))), Instant.ofEpochMilli(15005L), 15L, NetworkParameters.fromID(NetworkParameters.ID_REGTEST)); - Federation newFederation = new Federation(FederationTestUtils.getFederationMembersWithKeys(Arrays.asList( + Federation newFederation = new StandardMultisigFederation(FederationTestUtils.getFederationMembersWithKeys(Arrays.asList( BtcECKey.fromPublicOnly(Hex.decode("0346cb6b905e4dee49a862eeb2288217d06afcd4ace4b5ca77ebedfbc6afc1c19d")), BtcECKey.fromPublicOnly(Hex.decode("0269a0dbe7b8f84d1b399103c466fb20531a56b1ad3a7b44fe419e74aad8c46db7")), BtcECKey.fromPublicOnly(Hex.decode("026192d8ab41bd402eb0431457f6756a3f3ce15c955c534d2b87f1e0372d8ba338")))), @@ -2876,7 +2876,7 @@ void commitFederation_hashMismatch() throws IOException, BridgeIllegalArgumentEx @Test void getActiveFederationWallet() throws IOException { - Federation expectedFederation = new Federation(FederationTestUtils.getFederationMembersWithBtcKeys(Arrays.asList(new BtcECKey[]{ + Federation expectedFederation = new StandardMultisigFederation(FederationTestUtils.getFederationMembersWithBtcKeys(Arrays.asList(new BtcECKey[]{ BtcECKey.fromPublicOnly(Hex.decode("036bb9eab797eadc8b697f0e82a01d01cabbfaaca37e5bafc06fdc6fdd38af894a")), BtcECKey.fromPublicOnly(Hex.decode("031da807c71c2f303b7f409dd2605b297ac494a563be3b9ca5f52d95a43d183cc5")) })), Instant.ofEpochMilli(5005L), 0L, NetworkParameters.fromID(NetworkParameters.ID_REGTEST)); @@ -2909,14 +2909,14 @@ void getActiveFederationWallet() throws IOException { @Test void getRetiringFederationWallet_nonEmpty() throws IOException { - Federation mockedNewFederation = new Federation( + Federation mockedNewFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembers(2), Instant.ofEpochMilli(2000), 10L, NetworkParameters.fromID(NetworkParameters.ID_REGTEST) ); - Federation expectedFederation = new Federation(FederationTestUtils.getFederationMembersWithBtcKeys(Arrays.asList(new BtcECKey[]{ + Federation expectedFederation = new StandardMultisigFederation(FederationTestUtils.getFederationMembersWithBtcKeys(Arrays.asList(new BtcECKey[]{ BtcECKey.fromPublicOnly(Hex.decode("036bb9eab797eadc8b697f0e82a01d01cabbfaaca37e5bafc06fdc6fdd38af894a")), BtcECKey.fromPublicOnly(Hex.decode("031da807c71c2f303b7f409dd2605b297ac494a563be3b9ca5f52d95a43d183cc5")) })), Instant.ofEpochMilli(5005L), 0L, NetworkParameters.fromID(NetworkParameters.ID_REGTEST)); @@ -3745,7 +3745,7 @@ private BridgeSupport getBridgeSupportWithMocksForFederationTests( BridgeEventLogger eventLogger) throws IOException { BridgeConstants constantsMock = mock(BridgeConstants.class); - when(constantsMock.getGenesisFederation()).thenReturn(mockedGenesisFederation); + when(constantsMock.getGenesisFederation()).thenReturn((StandardMultisigFederation) mockedGenesisFederation); when(constantsMock.getBtcParams()).thenReturn(NetworkParameters.fromID(NetworkParameters.ID_REGTEST)); when(constantsMock.getFederationChangeAuthorizer()).thenReturn(bridgeConstants.getFederationChangeAuthorizer()); diff --git a/rskj-core/src/test/java/co/rsk/peg/BridgeTest.java b/rskj-core/src/test/java/co/rsk/peg/BridgeTest.java index e073d92caee..2bfee1299fc 100644 --- a/rskj-core/src/test/java/co/rsk/peg/BridgeTest.java +++ b/rskj-core/src/test/java/co/rsk/peg/BridgeTest.java @@ -238,7 +238,7 @@ void registerBtcTransaction_beforeRskip199_rejectsExternalCalls() ActivationConfig activations = spy(ActivationConfigsForTest.genesis()); doReturn(false).when(activations).isActive(eq(RSKIP199), anyLong()); - Federation activeFederation = new Federation( + Federation activeFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembers(3), Instant.ofEpochMilli(1000), 0L, @@ -284,7 +284,7 @@ void registerBtcTransaction_beforeRskip199_acceptsCallFromFederationMember() List federationKeys = Arrays.asList(fed1Key, new BtcECKey(), new BtcECKey()); federationKeys.sort(BtcECKey.PUBKEY_COMPARATOR); - Federation activeFederation = new Federation( + Federation activeFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembersWithKeys(federationKeys), Instant.ofEpochMilli(1000), 0L, diff --git a/rskj-core/src/test/java/co/rsk/peg/BridgeUtilsLegacyTest.java b/rskj-core/src/test/java/co/rsk/peg/BridgeUtilsLegacyTest.java index 61c25b21ae1..5f59ad6f9f3 100644 --- a/rskj-core/src/test/java/co/rsk/peg/BridgeUtilsLegacyTest.java +++ b/rskj-core/src/test/java/co/rsk/peg/BridgeUtilsLegacyTest.java @@ -456,7 +456,7 @@ void calculatePegoutTxSize_before_rskip_271() { when(activations.isActive(ConsensusRule.RSKIP271)).thenReturn(false); List keys = PegTestUtils.createRandomBtcECKeys(13); - Federation federation = new Federation( + Federation federation = new StandardMultisigFederation( FederationMember.getFederationMembersFromKeys(keys), Instant.now(), 0, @@ -478,7 +478,7 @@ void calculatePegoutTxSize_after_rskip_271() { when(activations.isActive(ConsensusRule.RSKIP271)).thenReturn(true); List keys = PegTestUtils.createRandomBtcECKeys(13); - Federation federation = new Federation( + Federation federation = new StandardMultisigFederation( FederationMember.getFederationMembersFromKeys(keys), Instant.now(), 0, @@ -493,7 +493,7 @@ void calculatePegoutTxSize_ZeroInput_ZeroOutput() { when(activations.isActive(ConsensusRule.RSKIP271)).thenReturn(false); List keys = PegTestUtils.createRandomBtcECKeys(13); - Federation federation = new Federation( + Federation federation = new StandardMultisigFederation( FederationMember.getFederationMembersFromKeys(keys), Instant.now(), 0, diff --git a/rskj-core/src/test/java/co/rsk/peg/BridgeUtilsTest.java b/rskj-core/src/test/java/co/rsk/peg/BridgeUtilsTest.java index faed33c78a1..c7ae74957fc 100644 --- a/rskj-core/src/test/java/co/rsk/peg/BridgeUtilsTest.java +++ b/rskj-core/src/test/java/co/rsk/peg/BridgeUtilsTest.java @@ -249,7 +249,7 @@ void testIsValidPegInTxForTwoFederations() { BtcECKey.fromPrivate(Hex.decode("fa02")) ); federation1Keys.sort(BtcECKey.PUBKEY_COMPARATOR); - Federation federation1 = new Federation( + Federation federation1 = new StandardMultisigFederation( FederationTestUtils.getFederationMembersWithBtcKeys(federation1Keys), Instant.ofEpochMilli(1000L), 0L, @@ -262,7 +262,7 @@ void testIsValidPegInTxForTwoFederations() { BtcECKey.fromPrivate(Hex.decode("fb03")) ); federation2Keys.sort(BtcECKey.PUBKEY_COMPARATOR); - Federation federation2 = new Federation( + Federation federation2 = new StandardMultisigFederation( FederationTestUtils.getFederationMembersWithBtcKeys(federation2Keys), Instant.ofEpochMilli(2000L), 0L, @@ -574,7 +574,7 @@ void testIsValidPegInTx_hasChangeUtxoFromFlyoverErpFederation_beforeRskip201_isP BtcECKey.fromPrivate(Hex.decode("fa02")) ); erpFederationKeys.sort(BtcECKey.PUBKEY_COMPARATOR); - Federation erpFederation = new Federation( + Federation erpFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembersWithBtcKeys(erpFederationKeys), Instant.ofEpochMilli(1000L), 0L, @@ -616,7 +616,7 @@ void testIsValidPegInTx_hasChangeUtxoFromFlyoverErpFederation_afterRskip201_notP BtcECKey.fromPrivate(Hex.decode("fa02")) ); erpFederationKeys.sort(BtcECKey.PUBKEY_COMPARATOR); - Federation erpFederation = new Federation( + Federation erpFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembersWithBtcKeys(erpFederationKeys), Instant.ofEpochMilli(1000L), 0L, @@ -658,7 +658,7 @@ void testIsValidPegInTx_hasChangeUtxoFromErpFederation_beforeRskip201_isPegin() BtcECKey.fromPrivate(Hex.decode("fa02")) ); erpFederationKeys.sort(BtcECKey.PUBKEY_COMPARATOR); - Federation erpFederation = new Federation( + Federation erpFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembersWithBtcKeys(erpFederationKeys), Instant.ofEpochMilli(1000L), 0L, @@ -691,7 +691,7 @@ void testIsValidPegInTx_hasChangeUtxoFromErpFederation_afterRskip201_notPegin() BtcECKey.fromPrivate(Hex.decode("fa02")) ); erpFederationKeys.sort(BtcECKey.PUBKEY_COMPARATOR); - Federation erpFederation = new Federation( + Federation erpFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembersWithBtcKeys(erpFederationKeys), Instant.ofEpochMilli(1000L), 0L, @@ -724,7 +724,7 @@ void testIsValidPegInTx_hasChangeUtxoFromFlyoverRetiredFederation_beforeRskip201 BtcECKey.fromPrivate(Hex.decode("fa02")) ); retiredFederationKeys.sort(BtcECKey.PUBKEY_COMPARATOR); - Federation retiredFederation = new Federation( + Federation retiredFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembersWithBtcKeys(retiredFederationKeys), Instant.ofEpochMilli(1000L), 0L, @@ -769,7 +769,7 @@ void testIsValidPegInTx_hasChangeUtxoFromFlyoverRetiredFederation_afterRskip201_ BtcECKey.fromPrivate(Hex.decode("fa02")) ); retiredFederationKeys.sort(BtcECKey.PUBKEY_COMPARATOR); - Federation retiredFederation = new Federation( + Federation retiredFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembersWithBtcKeys(retiredFederationKeys), Instant.ofEpochMilli(1000L), 0L, @@ -814,7 +814,7 @@ void testIsValidPegInTx_hasChangeUtxoFromFlyoverErpRetiredFederation_beforeRskip BtcECKey.fromPrivate(Hex.decode("fa02")) ); retiredFederationKeys.sort(BtcECKey.PUBKEY_COMPARATOR); - Federation retiredFederation = new Federation( + Federation retiredFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembersWithBtcKeys(retiredFederationKeys), Instant.ofEpochMilli(1000L), 0L, @@ -827,7 +827,7 @@ void testIsValidPegInTx_hasChangeUtxoFromFlyoverErpRetiredFederation_beforeRskip ); erpFederationPublicKeys.sort(BtcECKey.PUBKEY_COMPARATOR); - Federation erpFederation = new ErpFederation( + Federation erpFederation = new LegacyErpFederation( FederationTestUtils.getFederationMembersWithBtcKeys(retiredFederationKeys), Instant.ofEpochMilli(1000L), 0L, @@ -875,7 +875,7 @@ void testIsValidPegInTx_hasChangeUtxoFromFlyoverErpRetiredFederation_afterRskip2 BtcECKey.fromPrivate(Hex.decode("fa02")) ); retiredFederationKeys.sort(BtcECKey.PUBKEY_COMPARATOR); - Federation retiredFederation = new Federation( + Federation retiredFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembersWithBtcKeys(retiredFederationKeys), Instant.ofEpochMilli(1000L), 0L, @@ -888,7 +888,7 @@ void testIsValidPegInTx_hasChangeUtxoFromFlyoverErpRetiredFederation_afterRskip2 ); erpFederationPublicKeys.sort(BtcECKey.PUBKEY_COMPARATOR); - Federation erpFederation = new ErpFederation( + Federation erpFederation = new LegacyErpFederation( FederationTestUtils.getFederationMembersWithBtcKeys(retiredFederationKeys), Instant.ofEpochMilli(1000L), 0L, @@ -935,7 +935,7 @@ void testIsValidPegInTx_hasChangeUtxoFromErpRetiredFederation_beforeRskip201_isP BtcECKey.fromPrivate(Hex.decode("fa02")) ); retiredFederationKeys.sort(BtcECKey.PUBKEY_COMPARATOR); - Federation retiredFederation = new Federation( + Federation retiredFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembersWithBtcKeys(retiredFederationKeys), Instant.ofEpochMilli(1000L), 0L, @@ -948,7 +948,7 @@ void testIsValidPegInTx_hasChangeUtxoFromErpRetiredFederation_beforeRskip201_isP ); erpFederationPublicKeys.sort(BtcECKey.PUBKEY_COMPARATOR); - Federation erpFederation = new ErpFederation( + Federation erpFederation = new LegacyErpFederation( FederationTestUtils.getFederationMembersWithBtcKeys(retiredFederationKeys), Instant.ofEpochMilli(1000L), 0L, @@ -991,7 +991,7 @@ void testIsValidPegInTx_hasChangeUtxoFromErpRetiredFederation_afterRskip201_notP BtcECKey.fromPrivate(Hex.decode("fa02")) ); retiredFederationKeys.sort(BtcECKey.PUBKEY_COMPARATOR); - Federation retiredFederation = new Federation( + Federation retiredFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembersWithBtcKeys(retiredFederationKeys), Instant.ofEpochMilli(1000L), 0L, @@ -1004,7 +1004,7 @@ void testIsValidPegInTx_hasChangeUtxoFromErpRetiredFederation_afterRskip201_notP ); erpFederationPublicKeys.sort(BtcECKey.PUBKEY_COMPARATOR); - Federation erpFederation = new ErpFederation( + Federation erpFederation = new LegacyErpFederation( FederationTestUtils.getFederationMembersWithBtcKeys(retiredFederationKeys), Instant.ofEpochMilli(1000L), 0L, @@ -1308,7 +1308,7 @@ void testIsMigrationTx_sending_funds_from_retired_p2sh_fed_to_active_p2sh_fed() BtcECKey.fromPrivate(Hex.decode("fc02")) ).sorted(BtcECKey.PUBKEY_COMPARATOR).collect(Collectors.toList()); - Federation retiredFederation = new P2shErpFederation( + ErpFederation retiredFederation = new P2shErpFederation( FederationTestUtils.getFederationMembersWithBtcKeys(retiredFederationKeys), Instant.ofEpochMilli(1000L), 1L, @@ -1438,7 +1438,7 @@ void testIsMigrationTx_sending_funds_from_retired_standard_fed_to_active_p2sh_fe BtcECKey.fromPrivate(Hex.decode("fc02")) ).sorted(BtcECKey.PUBKEY_COMPARATOR).collect(Collectors.toList()); - Federation retiredFederation = new Federation( + Federation retiredFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembersWithBtcKeys(retiredFederationKeys), Instant.ofEpochMilli(1000L), 1L, @@ -1487,7 +1487,7 @@ void testIsMigrationTx_sending_funds_from_retired_standard_fed_to_active_p2sh_fe migrationTx, activeFederation, null, - retiredFederation.getStandardP2SHScript(), + retiredFederation.getP2SHScript(), btcContext, bridgeConstantsMainnet, activations @@ -1505,7 +1505,7 @@ void testIsMigrationTx_sending_funds_from_retiring_standard_fed_to_active_p2sh_f BtcECKey.fromPrivate(Hex.decode("fc02")) ).sorted(BtcECKey.PUBKEY_COMPARATOR).collect(Collectors.toList()); - Federation retiringFederation = new Federation( + Federation retiringFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembersWithBtcKeys(retiringFederationKeys), Instant.ofEpochMilli(1000L), 1L, @@ -1562,7 +1562,7 @@ void testIsMigrationTx_sending_funds_from_retired_standard_fed_to_active_standar BtcECKey.fromPrivate(Hex.decode("fc02")) ).sorted(BtcECKey.PUBKEY_COMPARATOR).collect(Collectors.toList()); - Federation retiredFederation = new Federation( + Federation retiredFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembersWithBtcKeys(retiredFederationKeys), Instant.ofEpochMilli(1000L), 1L, @@ -1574,7 +1574,7 @@ void testIsMigrationTx_sending_funds_from_retired_standard_fed_to_active_standar BtcECKey.fromPrivate(Hex.decode("fa02")) ).sorted(BtcECKey.PUBKEY_COMPARATOR).collect(Collectors.toList()); - Federation activeFederation = new Federation( + Federation activeFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembersWithBtcKeys(activeFederationKeys), Instant.ofEpochMilli(1000L), 1L, @@ -1598,7 +1598,7 @@ void testIsMigrationTx_sending_funds_from_retired_standard_fed_to_active_standar migrationTx, activeFederation, null, - retiredFederation.getStandardP2SHScript(), + retiredFederation.getP2SHScript(), btcContext, bridgeConstantsMainnet, activations @@ -1626,7 +1626,7 @@ void testIsMigrationTx_sending_funds_from_retiring_standard_fed_to_active_standa BtcECKey.fromPrivate(Hex.decode("fc02")) ).sorted(BtcECKey.PUBKEY_COMPARATOR).collect(Collectors.toList()); - Federation retiringFederation = new Federation( + Federation retiringFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembersWithBtcKeys(retiringFederationKeys), Instant.ofEpochMilli(1000L), 1L, @@ -1638,7 +1638,7 @@ void testIsMigrationTx_sending_funds_from_retiring_standard_fed_to_active_standa BtcECKey.fromPrivate(Hex.decode("fa02")) ).sorted(BtcECKey.PUBKEY_COMPARATOR).collect(Collectors.toList()); - Federation activeFederation = new Federation( + Federation activeFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembersWithBtcKeys(activeFederationKeys), Instant.ofEpochMilli(1000L), 1L, @@ -1677,7 +1677,7 @@ void testIsMigrationTx() { BtcECKey.fromPrivate(Hex.decode("fa01")), BtcECKey.fromPrivate(Hex.decode("fa02")) ).sorted(BtcECKey.PUBKEY_COMPARATOR).collect(Collectors.toList()); - Federation activeFederation = new Federation( + Federation activeFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembersWithBtcKeys(activeFederationKeys), Instant.ofEpochMilli(2000L), 2L, @@ -1689,7 +1689,7 @@ void testIsMigrationTx() { BtcECKey.fromPrivate(Hex.decode("fb02")), BtcECKey.fromPrivate(Hex.decode("fb03")) ).sorted(BtcECKey.PUBKEY_COMPARATOR).collect(Collectors.toList()); - Federation retiringFederation = new Federation( + Federation retiringFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembersWithBtcKeys(retiringFederationKeys), Instant.ofEpochMilli(1000L), 1L, @@ -1700,7 +1700,7 @@ void testIsMigrationTx() { BtcECKey.fromPrivate(Hex.decode("fc01")), BtcECKey.fromPrivate(Hex.decode("fc02")) ).sorted(BtcECKey.PUBKEY_COMPARATOR).collect(Collectors.toList()); - Federation retiredFederation = new Federation( + Federation retiredFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembersWithBtcKeys(retiredFederationKeys), Instant.ofEpochMilli(1000L), 1L, @@ -1831,7 +1831,7 @@ void testIsPegOutTx() { BtcECKey.fromPrivate(Hex.decode("fa02")), BtcECKey.fromPrivate(Hex.decode("fa03")) ).sorted(BtcECKey.PUBKEY_COMPARATOR).collect(Collectors.toList()); - Federation federation2 = new Federation( + Federation federation2 = new StandardMultisigFederation( FederationTestUtils.getFederationMembersWithBtcKeys(activeFederationKeys), Instant.ofEpochMilli(2000L), 2L, @@ -1868,7 +1868,7 @@ void testIsPegOutTx_fromFlyoverFederation() { BtcECKey.fromPrivate(Hex.decode("fa03")) ); flyoverFederationKeys.sort(BtcECKey.PUBKEY_COMPARATOR); - Federation flyoverFederation = new Federation( + Federation flyoverFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembersWithBtcKeys(flyoverFederationKeys), Instant.ofEpochMilli(1000L), 0L, @@ -1926,7 +1926,7 @@ void testIsPegOutTx_fromErpFederation() { BtcECKey.fromPrivate(Hex.decode("fa03")) ); defaultFederationKeys.sort(BtcECKey.PUBKEY_COMPARATOR); - Federation defaultFederation = new Federation( + Federation defaultFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembersWithBtcKeys(defaultFederationKeys), Instant.ofEpochMilli(1000L), 0L, @@ -1940,7 +1940,7 @@ void testIsPegOutTx_fromErpFederation() { ); erpFederationPublicKeys.sort(BtcECKey.PUBKEY_COMPARATOR); - Federation erpFederation = new ErpFederation( + ErpFederation erpFederation = new LegacyErpFederation( FederationTestUtils.getFederationMembersWithBtcKeys(defaultFederationKeys), Instant.ofEpochMilli(1000L), 0L, @@ -2002,7 +2002,7 @@ void testIsPegOutTx_fromFlyoverErpFederation() { BtcECKey.fromPrivate(Hex.decode("fa03")) ); defaultFederationKeys.sort(BtcECKey.PUBKEY_COMPARATOR); - Federation defaultFederation = new Federation( + Federation defaultFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembersWithBtcKeys(defaultFederationKeys), Instant.ofEpochMilli(1000L), 0L, @@ -2016,7 +2016,7 @@ void testIsPegOutTx_fromFlyoverErpFederation() { ); erpFederationPublicKeys.sort(BtcECKey.PUBKEY_COMPARATOR); - Federation erpFederation = new ErpFederation( + Federation erpFederation = new LegacyErpFederation( FederationTestUtils.getFederationMembersWithBtcKeys(defaultFederationKeys), Instant.ofEpochMilli(1000L), 0L, @@ -2116,7 +2116,7 @@ void testChangeBetweenFederations() { .map(BtcECKey::fromPrivate) .sorted(BtcECKey.PUBKEY_COMPARATOR) .collect(Collectors.toList()); - Federation federation1 = new Federation( + Federation federation1 = new StandardMultisigFederation( FederationTestUtils.getFederationMembersWithBtcKeys(federation1Keys), Instant.ofEpochMilli(1000L), 0L, networkParameters ); @@ -2126,7 +2126,7 @@ void testChangeBetweenFederations() { .map(BtcECKey::fromPrivate) .sorted(BtcECKey.PUBKEY_COMPARATOR) .collect(Collectors.toList()); - Federation federation2 = new Federation( + Federation federation2 = new StandardMultisigFederation( FederationTestUtils.getFederationMembersWithBtcKeys(federation2Keys), Instant.ofEpochMilli(2000L), 0L, networkParameters ); @@ -2536,7 +2536,7 @@ void isInputSignedByThisFederator_isSigned() { // Arrange BtcECKey federator1Key = new BtcECKey(); BtcECKey federator2Key = new BtcECKey(); - Federation federation = new Federation( + Federation federation = new StandardMultisigFederation( FederationMember.getFederationMembersFromKeys(Arrays.asList(federator1Key, federator2Key)), Instant.now(), 0, @@ -2584,7 +2584,7 @@ void isInputSignedByThisFederator_isSignedByAnotherFederator() { // Arrange BtcECKey federator1Key = new BtcECKey(); BtcECKey federator2Key = new BtcECKey(); - Federation federation = new Federation( + Federation federation = new StandardMultisigFederation( FederationMember.getFederationMembersFromKeys(Arrays.asList(federator1Key, federator2Key)), Instant.now(), 0, @@ -2632,7 +2632,7 @@ void isInputSignedByThisFederator_notSigned() { // Arrange BtcECKey federator1Key = new BtcECKey(); BtcECKey federator2Key = new BtcECKey(); - Federation federation = new Federation( + Federation federation = new StandardMultisigFederation( FederationMember.getFederationMembersFromKeys(Arrays.asList(federator1Key, federator2Key)), Instant.now(), 0, @@ -2978,7 +2978,7 @@ void testCalculatePegoutTxSize_ZeroInput_ZeroOutput() { when(activations.isActive(ConsensusRule.RSKIP271)).thenReturn(true); List keys = PegTestUtils.createRandomBtcECKeys(13); - Federation federation = new Federation( + Federation federation = new StandardMultisigFederation( FederationMember.getFederationMembersFromKeys(keys), Instant.now(), 0, @@ -2994,7 +2994,7 @@ void testCalculatePegoutTxSize_2Inputs_2Outputs() { when(activations.isActive(ConsensusRule.RSKIP271)).thenReturn(true); List keys = PegTestUtils.createRandomBtcECKeys(13); - Federation federation = new Federation( + Federation federation = new StandardMultisigFederation( FederationMember.getFederationMembersFromKeys(keys), Instant.now(), 0, @@ -3019,7 +3019,7 @@ void testCalculatePegoutTxSize_9Inputs_2Outputs() { when(activations.isActive(ConsensusRule.RSKIP271)).thenReturn(true); List keys = PegTestUtils.createRandomBtcECKeys(13); - Federation federation = new Federation( + Federation federation = new StandardMultisigFederation( FederationMember.getFederationMembersFromKeys(keys), Instant.now(), 0, @@ -3044,7 +3044,7 @@ void testCalculatePegoutTxSize_10Inputs_20Outputs() { when(activations.isActive(ConsensusRule.RSKIP271)).thenReturn(true); List keys = PegTestUtils.createRandomBtcECKeys(13); - Federation federation = new Federation( + Federation federation = new StandardMultisigFederation( FederationMember.getFederationMembersFromKeys(keys), Instant.now(), 0, @@ -3072,7 +3072,7 @@ void testCalculatePegoutTxSize_50Inputs_200Outputs() { when(activations.isActive(ConsensusRule.RSKIP271)).thenReturn(true); List keys = PegTestUtils.createRandomBtcECKeys(13); - Federation federation = new Federation( + Federation federation = new StandardMultisigFederation( FederationMember.getFederationMembersFromKeys(keys), Instant.now(), 0, @@ -3113,7 +3113,7 @@ void testCalculatePegoutTxSize_50Inputs_200Outputs_erpFederation() { ); erpFederationPublicKeys.sort(BtcECKey.PUBKEY_COMPARATOR); - Federation erpFederation = new ErpFederation( + Federation erpFederation = new LegacyErpFederation( FederationTestUtils.getFederationMembersWithBtcKeys(defaultFederationKeys), Instant.ofEpochMilli(1000L), 0L, @@ -3157,7 +3157,7 @@ void testCalculatePegoutTxSize_100Inputs_50Outputs_erpFederation() { ); erpFederationPublicKeys.sort(BtcECKey.PUBKEY_COMPARATOR); - Federation erpFederation = new ErpFederation( + Federation erpFederation = new LegacyErpFederation( FederationTestUtils.getFederationMembersWithBtcKeys(defaultFederationKeys), Instant.ofEpochMilli(1000L), 0L, @@ -3191,7 +3191,7 @@ void getRegularPegoutTxSize_has_proper_calculations() { BtcECKey key2 = new BtcECKey(); BtcECKey key3 = new BtcECKey(); List keys = Arrays.asList(key1, key2, key3); - Federation fed = new Federation( + Federation fed = new StandardMultisigFederation( FederationMember.getFederationMembersFromKeys(keys), Instant.now(), 0, @@ -3274,7 +3274,7 @@ void scriptCorrectlySpends_invalidScript() { } private void test_getSpendWallet(boolean isFlyoverCompatible) throws UTXOProviderException { - Federation federation = new Federation(FederationTestUtils.getFederationMembersWithBtcKeys(Arrays.asList( + Federation federation = new StandardMultisigFederation(FederationTestUtils.getFederationMembersWithBtcKeys(Arrays.asList( BtcECKey.fromPublicOnly(Hex.decode("036bb9eab797eadc8b697f0e82a01d01cabbfaaca37e5bafc06fdc6fdd38af894a")), BtcECKey.fromPublicOnly(Hex.decode("031da807c71c2f303b7f409dd2605b297ac494a563be3b9ca5f52d95a43d183cc5")))), Instant.ofEpochMilli(5005L), @@ -3305,7 +3305,7 @@ private void test_getSpendWallet(boolean isFlyoverCompatible) throws UTXOProvide } private void test_getNoSpendWallet(boolean isFlyoverCompatible) { - Federation federation = new Federation(FederationTestUtils.getFederationMembersWithBtcKeys(Arrays.asList( + Federation federation = new StandardMultisigFederation(FederationTestUtils.getFederationMembersWithBtcKeys(Arrays.asList( BtcECKey.fromPublicOnly(Hex.decode("036bb9eab797eadc8b697f0e82a01d01cabbfaaca37e5bafc06fdc6fdd38af894a")), BtcECKey.fromPublicOnly(Hex.decode("031da807c71c2f303b7f409dd2605b297ac494a563be3b9ca5f52d95a43d183cc5")))), Instant.ofEpochMilli(5005L), @@ -3534,7 +3534,7 @@ private Genesis getGenesisInstance(TrieStore trieStore) { private ErpFederation createErpFederation() { Federation genesisFederation = bridgeConstantsRegtest.getGenesisFederation(); - return new ErpFederation( + return new LegacyErpFederation( genesisFederation.getMembers(), genesisFederation.getCreationTime(), genesisFederation.getCreationBlockNumber(), diff --git a/rskj-core/src/test/java/co/rsk/peg/ErpFederationTest.java b/rskj-core/src/test/java/co/rsk/peg/ErpFederationTest.java index b097cd091bc..7fe20f8a6db 100644 --- a/rskj-core/src/test/java/co/rsk/peg/ErpFederationTest.java +++ b/rskj-core/src/test/java/co/rsk/peg/ErpFederationTest.java @@ -143,7 +143,7 @@ void getErpPubKeys_uncompressed_public_keys() { .collect(Collectors.toList()); // Recreate federation - ErpFederation federationWithUncompressedKeys = new ErpFederation( + ErpFederation federationWithUncompressedKeys = new LegacyErpFederation( FederationTestUtils.getFederationMembersWithBtcKeys(defaultKeys), ZonedDateTime.parse("2017-06-10T02:30:00Z").toInstant(), 0L, @@ -170,7 +170,7 @@ void getErpRedeemScript_compareOtherImplementation() throws IOException { RawGeneratedRedeemScript[] generatedScripts = new ObjectMapper().readValue(rawRedeemScripts, RawGeneratedRedeemScript[].class); for (RawGeneratedRedeemScript generatedScript : generatedScripts) { - Federation erpFederation = new ErpFederation( + Federation erpFederation = new LegacyErpFederation( FederationTestUtils.getFederationMembersWithBtcKeys(generatedScript.mainFed), ZonedDateTime.parse("2017-06-10T02:30:00Z").toInstant(), 0L, @@ -243,7 +243,7 @@ void createInvalidErpFederation_negativeCsvValue() { List federationMembersFromPks = FederationTestUtils.getFederationMembersFromPks(100, 200, 300); Instant creationTime = ZonedDateTime.parse("2017-06-10T02:30:00Z").toInstant(); NetworkParameters btcParams = NetworkParameters.fromID(NetworkParameters.ID_REGTEST); - assertThrows(VerificationException.class, () -> new ErpFederation( + assertThrows(VerificationException.class, () -> new LegacyErpFederation( federationMembersFromPks, creationTime, 0L, @@ -259,7 +259,7 @@ void createInvalidErpFederation_csvValueZero() { List federationMembersFromPks = FederationTestUtils.getFederationMembersFromPks(100, 200, 300); Instant creationTime = ZonedDateTime.parse("2017-06-10T02:30:00Z").toInstant(); NetworkParameters btcParams = NetworkParameters.fromID(NetworkParameters.ID_REGTEST); - assertThrows(VerificationException.class, () -> new ErpFederation( + assertThrows(VerificationException.class, () -> new LegacyErpFederation( federationMembersFromPks, creationTime, 0L, @@ -275,7 +275,7 @@ void createInvalidErpFederation_csvValueAboveMax() { List federationMembersFromPks = FederationTestUtils.getFederationMembersFromPks(100, 200, 300); Instant creationTime = ZonedDateTime.parse("2017-06-10T02:30:00Z").toInstant(); NetworkParameters btcParams = NetworkParameters.fromID(NetworkParameters.ID_REGTEST); - assertThrows(VerificationException.class, () -> new ErpFederation( + assertThrows(VerificationException.class, () -> new LegacyErpFederation( federationMembersFromPks, creationTime, 0L, @@ -291,7 +291,7 @@ void getRedeemScript_before_RSKIP_284_testnet() { ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class); when(activations.isActive(ConsensusRule.RSKIP284)).thenReturn(false); - Federation erpFederation = new ErpFederation( + Federation erpFederation = new LegacyErpFederation( FederationTestUtils.getFederationMembersFromPks(100, 200, 300), ZonedDateTime.parse("2017-06-10T02:30:00Z").toInstant(), 0L, @@ -309,7 +309,7 @@ void getRedeemScript_before_RSKIP_284_mainnet() { ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class); when(activations.isActive(ConsensusRule.RSKIP284)).thenReturn(false); - Federation erpFederation = new ErpFederation( + Federation erpFederation = new LegacyErpFederation( FederationTestUtils.getFederationMembersWithBtcKeys(defaultKeys), ZonedDateTime.parse("2017-06-10T02:30:00Z").toInstant(), 0L, @@ -329,7 +329,7 @@ void getRedeemScript_before_RSKIP_284_mainnet() { @Test void getRedeemScript_after_RSKIP_284_testnet() { - Federation erpFederation = new ErpFederation( + Federation erpFederation = new LegacyErpFederation( FederationTestUtils.getFederationMembersWithBtcKeys(defaultKeys), ZonedDateTime.parse("2017-06-10T02:30:00Z").toInstant(), 0L, @@ -349,7 +349,7 @@ void getRedeemScript_after_RSKIP_284_testnet() { @Test void getRedeemScript_after_RSKIP_284_mainnet() { - Federation erpFederation = new ErpFederation( + Federation erpFederation = new LegacyErpFederation( FederationTestUtils.getFederationMembersWithBtcKeys(defaultKeys), ZonedDateTime.parse("2017-06-10T02:30:00Z").toInstant(), 0L, @@ -378,7 +378,7 @@ void testEquals_basic() { @Test void testEquals_same() { - Federation otherFederation = new ErpFederation( + Federation otherFederation = new LegacyErpFederation( federation.getMembers(), federation.getCreationTime(), federation.getCreationBlockNumber(), @@ -393,7 +393,7 @@ void testEquals_same() { @Test void testEquals_differentNumberOfMembers() { - Federation otherFederation = new ErpFederation( + Federation otherFederation = new LegacyErpFederation( FederationTestUtils.getFederationMembersFromPks(100, 200, 300, 400, 500, 600, 700), federation.getCreationTime(), federation.getCreationBlockNumber(), @@ -408,7 +408,7 @@ void testEquals_differentNumberOfMembers() { @Test void testEquals_differentCreationTime() { - Federation otherFederation = new ErpFederation( + Federation otherFederation = new LegacyErpFederation( federation.getMembers(), Instant.now(), federation.getCreationBlockNumber(), @@ -423,7 +423,7 @@ void testEquals_differentCreationTime() { @Test void testEquals_differentCreationBlockNumber() { - Federation otherFederation = new ErpFederation( + Federation otherFederation = new LegacyErpFederation( federation.getMembers(), federation.getCreationTime(), federation.getCreationBlockNumber() + 1, @@ -438,7 +438,7 @@ void testEquals_differentCreationBlockNumber() { @Test void testEquals_differentNetworkParameters() { - Federation otherFederation = new ErpFederation( + Federation otherFederation = new LegacyErpFederation( federation.getMembers(), federation.getCreationTime(), federation.getCreationBlockNumber(), @@ -453,7 +453,7 @@ void testEquals_differentNetworkParameters() { @Test void testEquals_differentMembers() { - Federation otherFederation = new ErpFederation( + Federation otherFederation = new LegacyErpFederation( FederationTestUtils.getFederationMembersFromPks(101, 201, 301), federation.getCreationTime(), federation.getCreationBlockNumber(), @@ -475,7 +475,7 @@ void testEquals_differentRedeemScript() { when(activationsPost.isActive(ConsensusRule.RSKIP284)).thenReturn(true); // Both federations created before RSKIP284 with the same data, should have the same redeem script - Federation erpFederation = new ErpFederation( + Federation erpFederation = new LegacyErpFederation( FederationTestUtils.getFederationMembersFromPks(100, 200, 300), ZonedDateTime.parse("2017-06-10T02:30:00Z").toInstant(), 0L, @@ -485,7 +485,7 @@ void testEquals_differentRedeemScript() { activationsPre ); - Federation otherErpFederation = new ErpFederation( + Federation otherErpFederation = new LegacyErpFederation( FederationTestUtils.getFederationMembersFromPks(100, 200, 300), ZonedDateTime.parse("2017-06-10T02:30:00Z").toInstant(), 0L, @@ -498,7 +498,7 @@ void testEquals_differentRedeemScript() { assertEquals(erpFederation, otherErpFederation); // One federation created after RSKIP284 with the same data, should have different redeem script - otherErpFederation = new ErpFederation( + otherErpFederation = new LegacyErpFederation( FederationTestUtils.getFederationMembersFromPks(100, 200, 300), ZonedDateTime.parse("2017-06-10T02:30:00Z").toInstant(), 0L, @@ -511,7 +511,7 @@ void testEquals_differentRedeemScript() { Assertions.assertNotEquals(erpFederation, otherErpFederation); // The other federation created after RSKIP284 with the same data, should have same redeem script - erpFederation = new ErpFederation( + erpFederation = new LegacyErpFederation( FederationTestUtils.getFederationMembersFromPks(100, 200, 300), ZonedDateTime.parse("2017-06-10T02:30:00Z").toInstant(), 0L, @@ -551,7 +551,7 @@ void createErpFedWithSameRedeemScriptAsHardcodedOne_after_RSKIP293_fails() { List federationMembersWithBtcKeys = FederationTestUtils.getFederationMembersWithBtcKeys(standardMultisigKeys); Instant creationTime = ZonedDateTime.parse("2017-06-10T02:30:00Z").toInstant(); NetworkParameters btcParams = NetworkParameters.fromID(NetworkParameters.ID_TESTNET); - assertThrows(FederationCreationException.class, () -> new ErpFederation( + assertThrows(FederationCreationException.class, () -> new LegacyErpFederation( federationMembersWithBtcKeys, creationTime, 0L, @@ -704,7 +704,7 @@ void spendFromErpFed_after_RSKIP293_mainnet_using_standard_multisig() { private void createErpFederation(BridgeConstants constants, boolean isRskip293Active) { when(activations.isActive(ConsensusRule.RSKIP293)).thenReturn(isRskip293Active); - Federation erpFederation = new ErpFederation( + Federation erpFederation = new LegacyErpFederation( FederationTestUtils.getFederationMembersWithBtcKeys(defaultKeys), ZonedDateTime.parse("2017-06-10T02:30:00Z").toInstant(), 0L, @@ -744,7 +744,7 @@ private void spendFromErpFed( Collections.singletonList(ConsensusRule.RSKIP293); ActivationConfig activations = ActivationConfigsForTest.hop400(except); - ErpFederation erpFed = new ErpFederation( + ErpFederation erpFed = new LegacyErpFederation( FederationMember.getFederationMembersFromKeys(standardKeys), ZonedDateTime.parse("2017-06-10T02:30:00Z").toInstant(), 0L, @@ -777,7 +777,7 @@ private void spendFromErpFed( } private ErpFederation createDefaultErpFederation() { - return new ErpFederation( + return new LegacyErpFederation( FederationTestUtils.getFederationMembersWithBtcKeys(defaultKeys), ZonedDateTime.parse("2017-06-10T02:30:00Z").toInstant(), 0L, diff --git a/rskj-core/src/test/java/co/rsk/peg/FederationSupportTest.java b/rskj-core/src/test/java/co/rsk/peg/FederationSupportTest.java index 42d49ae32b4..8b2b9aa9c5c 100644 --- a/rskj-core/src/test/java/co/rsk/peg/FederationSupportTest.java +++ b/rskj-core/src/test/java/co/rsk/peg/FederationSupportTest.java @@ -74,7 +74,7 @@ void whenNewFederationIsNullThenActiveFederationIsGenesisFederation() { when(provider.getNewFederation()) .thenReturn(null); when(bridgeConstants.getGenesisFederation()) - .thenReturn(genesisFederation); + .thenReturn((StandardMultisigFederation) genesisFederation); assertThat(federationSupport.getActiveFederation(), is(genesisFederation)); } @@ -165,7 +165,7 @@ void getFederatorPublicKeys() { ECKey rskKey1 = new ECKey(); ECKey mstKey1 = new ECKey(); - Federation theFederation = new Federation( + Federation theFederation = new StandardMultisigFederation( Arrays.asList( new FederationMember(btcKey0, rskKey0, mstKey0), new FederationMember(btcKey1, rskKey1, mstKey1) @@ -235,7 +235,7 @@ void getMemberPublicKeyOfType_OutOfBounds() { } private Federation getNewFakeFederation(long creationBlockNumber) { - return new Federation( + return new StandardMultisigFederation( Collections.emptyList(), Instant.ofEpochMilli(123), creationBlockNumber, NetworkParameters.fromID(NetworkParameters.ID_REGTEST) ); diff --git a/rskj-core/src/test/java/co/rsk/peg/FederationTest.java b/rskj-core/src/test/java/co/rsk/peg/FederationTest.java index 5344712c00b..069c090424a 100644 --- a/rskj-core/src/test/java/co/rsk/peg/FederationTest.java +++ b/rskj-core/src/test/java/co/rsk/peg/FederationTest.java @@ -53,7 +53,7 @@ class FederationTest { @BeforeEach void createFederation() { - federation = new Federation( + federation = new StandardMultisigFederation( FederationTestUtils.getFederationMembersFromPks(100, 200, 300, 400, 500, 600), ZonedDateTime.parse("2017-06-10T02:30:00Z").toInstant(), 0L, @@ -178,7 +178,7 @@ void testEquals_basic() { @Test void testEquals_differentNumberOfMembers() { - Federation otherFederation = new Federation( + Federation otherFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembersFromPks(100, 200, 300, 400, 500, 600, 700), ZonedDateTime.parse("2017-06-10T02:30:00Z").toInstant(), 0L, @@ -189,7 +189,7 @@ void testEquals_differentNumberOfMembers() { @Test void testEquals_differentCreationTime() { - Federation otherFederation = new Federation( + Federation otherFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembersFromPks(100, 200, 300, 400, 500, 600), ZonedDateTime.parse("2017-06-10T02:30:01Z").toInstant(), 0L, @@ -200,7 +200,7 @@ void testEquals_differentCreationTime() { @Test void testEquals_differentCreationBlockNumber() { - Federation otherFederation = new Federation( + Federation otherFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembersFromPks(100, 200, 300, 400, 500, 600), ZonedDateTime.parse("2017-06-10T02:30:01Z").toInstant(), 1L, @@ -211,7 +211,7 @@ void testEquals_differentCreationBlockNumber() { @Test void testEquals_differentNetworkParameters() { - Federation otherFederation = new Federation( + Federation otherFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembersFromPks(100, 200, 300, 400, 500, 600), ZonedDateTime.parse("2017-06-10T02:30:00Z").toInstant(), 0L, @@ -225,7 +225,7 @@ void testEquals_differentMembers() { List members = FederationTestUtils.getFederationMembersFromPks(100, 200, 300, 400, 500); members.add(new FederationMember(BtcECKey.fromPrivate(BigInteger.valueOf(610)), ECKey.fromPrivate(BigInteger.valueOf(600)), ECKey.fromPrivate(BigInteger.valueOf(620)))); - Federation otherFederation = new Federation( + Federation otherFederation = new StandardMultisigFederation( members, ZonedDateTime.parse("2017-06-10T02:30:00Z").toInstant(), 0L, @@ -234,7 +234,7 @@ void testEquals_differentMembers() { members.remove(members.size()-1); members.add(new FederationMember(BtcECKey.fromPrivate(BigInteger.valueOf(600)), ECKey.fromPrivate(BigInteger.valueOf(610)), ECKey.fromPrivate(BigInteger.valueOf(620)))); - Federation yetOtherFederation = new Federation( + Federation yetOtherFederation = new StandardMultisigFederation( members, ZonedDateTime.parse("2017-06-10T02:30:00Z").toInstant(), 0L, @@ -248,7 +248,7 @@ void testEquals_differentMembers() { @Test void testEquals_same() { - Federation otherFederation = new Federation( + Federation otherFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembersFromPks(100, 200, 300, 400, 500, 600), ZonedDateTime.parse("2017-06-10T02:30:00Z").toInstant(), 0L, diff --git a/rskj-core/src/test/java/co/rsk/peg/FederationTestUtils.java b/rskj-core/src/test/java/co/rsk/peg/FederationTestUtils.java index a860b294a8b..bd83f63d2d5 100644 --- a/rskj-core/src/test/java/co/rsk/peg/FederationTestUtils.java +++ b/rskj-core/src/test/java/co/rsk/peg/FederationTestUtils.java @@ -41,7 +41,7 @@ public class FederationTestUtils { public static Federation getFederation(Integer... federationMemberPks) { - return new Federation( + return new StandardMultisigFederation( getFederationMembersFromPks(federationMemberPks), ZonedDateTime.parse("2017-06-10T02:30:01Z").toInstant(), 0L, diff --git a/rskj-core/src/test/java/co/rsk/peg/FlyoverCompatibleBtcWalletWithStorageTest.java b/rskj-core/src/test/java/co/rsk/peg/FlyoverCompatibleBtcWalletWithStorageTest.java index 3fd1d7cbf11..8c2c5567e20 100644 --- a/rskj-core/src/test/java/co/rsk/peg/FlyoverCompatibleBtcWalletWithStorageTest.java +++ b/rskj-core/src/test/java/co/rsk/peg/FlyoverCompatibleBtcWalletWithStorageTest.java @@ -45,14 +45,14 @@ void setup() { ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class); when(activations.isActive(ConsensusRule.RSKIP284)).thenReturn(true); - federation = new Federation( + federation = new StandardMultisigFederation( FederationTestUtils.getFederationMembers(3), Instant.ofEpochMilli(1000), 0L, NetworkParameters.fromID(NetworkParameters.ID_REGTEST) ); - erpFederation = new ErpFederation( + erpFederation = new LegacyErpFederation( FederationTestUtils.getFederationMembers(3), Instant.ofEpochMilli(1000), 0L, diff --git a/rskj-core/src/test/java/co/rsk/peg/FlyoverCompatibleBtcWallextWithSingleScriptTest.java b/rskj-core/src/test/java/co/rsk/peg/FlyoverCompatibleBtcWallextWithSingleScriptTest.java index 0335d6c70e1..afb20abf11d 100644 --- a/rskj-core/src/test/java/co/rsk/peg/FlyoverCompatibleBtcWallextWithSingleScriptTest.java +++ b/rskj-core/src/test/java/co/rsk/peg/FlyoverCompatibleBtcWallextWithSingleScriptTest.java @@ -41,14 +41,14 @@ void setup() { ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class); when(activations.isActive(ConsensusRule.RSKIP284)).thenReturn(true); - federation = new Federation( + federation = new StandardMultisigFederation( FederationTestUtils.getFederationMembers(3), Instant.ofEpochMilli(1000), 0L, NetworkParameters.fromID(NetworkParameters.ID_REGTEST) ); - erpFederation = new ErpFederation( + erpFederation = new LegacyErpFederation( FederationTestUtils.getFederationMembers(3), Instant.ofEpochMilli(1000), 0L, diff --git a/rskj-core/src/test/java/co/rsk/peg/PegTestUtils.java b/rskj-core/src/test/java/co/rsk/peg/PegTestUtils.java index a5181a228df..76e19263bd4 100644 --- a/rskj-core/src/test/java/co/rsk/peg/PegTestUtils.java +++ b/rskj-core/src/test/java/co/rsk/peg/PegTestUtils.java @@ -263,7 +263,7 @@ public static Federation createFederation(BridgeConstants bridgeConstants, Strin public static Federation createFederation(BridgeConstants bridgeConstants, List federationKeys) { federationKeys.sort(BtcECKey.PUBKEY_COMPARATOR); - return new Federation( + return new StandardMultisigFederation( FederationTestUtils.getFederationMembersWithBtcKeys(federationKeys), Instant.ofEpochMilli(1000L), 0L, diff --git a/rskj-core/src/test/java/co/rsk/peg/PendingFederationTest.java b/rskj-core/src/test/java/co/rsk/peg/PendingFederationTest.java index 26c80f27b0d..e0b2d77d73c 100644 --- a/rskj-core/src/test/java/co/rsk/peg/PendingFederationTest.java +++ b/rskj-core/src/test/java/co/rsk/peg/PendingFederationTest.java @@ -298,7 +298,7 @@ private void testBuildFederation( activations ); } else if (isRskip201Active) { - expectedFederation = new ErpFederation( + expectedFederation = new LegacyErpFederation( FederationTestUtils.getFederationMembersFromPks(privateKeys), creationTime, 0L, @@ -308,7 +308,7 @@ private void testBuildFederation( activations ); } else { - expectedFederation = new Federation( + expectedFederation = new StandardMultisigFederation( FederationTestUtils.getFederationMembersFromPks(privateKeys), creationTime, 0L, diff --git a/rskj-core/src/test/java/co/rsk/peg/PowpegMigrationTest.java b/rskj-core/src/test/java/co/rsk/peg/PowpegMigrationTest.java index 4e8f0c19797..0a114606cc1 100644 --- a/rskj-core/src/test/java/co/rsk/peg/PowpegMigrationTest.java +++ b/rskj-core/src/test/java/co/rsk/peg/PowpegMigrationTest.java @@ -135,7 +135,7 @@ private void testChangePowpeg( Federation originalPowpeg; switch (oldPowPegFederationType) { case erp: - originalPowpeg = new ErpFederation( + originalPowpeg = new LegacyErpFederation( originalPowpegMembers, Instant.now(), 0, @@ -591,14 +591,14 @@ private void testChangePowpeg( if (oldPowPegFederationType == FederationType.erp || oldPowPegFederationType == FederationType.p2sh){ assertNotEquals(lastRetiredFederationP2SHScript, originalPowpeg.getP2SHScript()); } - assertEquals(lastRetiredFederationP2SHScript, originalPowpeg.getStandardP2SHScript()); + assertEquals(lastRetiredFederationP2SHScript, originalPowpeg instanceof ErpFederation ? ((ErpFederation) originalPowpeg).getStandardP2SHScript() : originalPowpeg.getP2SHScript()); } else { if (oldPowPegFederationType == FederationType.erp || oldPowPegFederationType == FederationType.p2sh){ assertEquals(lastRetiredFederationP2SHScript, originalPowpeg.getP2SHScript()); - assertNotEquals(lastRetiredFederationP2SHScript, originalPowpeg.getStandardP2SHScript()); + assertNotEquals(lastRetiredFederationP2SHScript, originalPowpeg instanceof ErpFederation ? ((ErpFederation) originalPowpeg).getStandardP2SHScript() : originalPowpeg.getP2SHScript()); } else { assertEquals(lastRetiredFederationP2SHScript, originalPowpeg.getP2SHScript()); - assertEquals(lastRetiredFederationP2SHScript, originalPowpeg.getStandardP2SHScript()); + assertEquals(lastRetiredFederationP2SHScript, originalPowpeg instanceof ErpFederation ? ((ErpFederation) originalPowpeg).getStandardP2SHScript() : originalPowpeg.getP2SHScript()); } } } @@ -620,10 +620,10 @@ private void verifyPegouts(BridgeStorageProvider bridgeStorageProvider) throws I Script inputStandardRedeemScript = RedeemScriptParserFactory.get(result.getChunks()).extractStandardRedeemScript(); Optional spendingFederationOptional = Optional.empty(); - if (inputStandardRedeemScript.equals(activeFederation.getStandardRedeemScript())) { + if (inputStandardRedeemScript.equals(activeFederation instanceof ErpFederation ? ((ErpFederation) activeFederation).getStandardRedeemScript() : activeFederation.getRedeemScript())) { spendingFederationOptional = Optional.of(activeFederation); } else if (retiringFederation != null && - inputStandardRedeemScript.equals(retiringFederation.getStandardRedeemScript())) { + inputStandardRedeemScript.equals(retiringFederation instanceof ErpFederation ? ((ErpFederation) retiringFederation).getStandardRedeemScript() : retiringFederation.getRedeemScript()) ) { spendingFederationOptional = Optional.of(retiringFederation); } else { fail("pegout scriptsig does not match any Federation"); diff --git a/rskj-core/src/test/java/co/rsk/peg/ReleaseTransactionBuilderTest.java b/rskj-core/src/test/java/co/rsk/peg/ReleaseTransactionBuilderTest.java index 481f50c57aa..8d2ef553fe8 100644 --- a/rskj-core/src/test/java/co/rsk/peg/ReleaseTransactionBuilderTest.java +++ b/rskj-core/src/test/java/co/rsk/peg/ReleaseTransactionBuilderTest.java @@ -90,7 +90,7 @@ void setup() { @Test void first_output_pay_fees() { - Federation federation = new Federation( + Federation federation = new StandardMultisigFederation( FederationMember.getFederationMembersFromKeys(Arrays.asList( new BtcECKey(), new BtcECKey(), @@ -164,7 +164,7 @@ void build_pegout_tx_from_erp_federation() { // Use mainnet constants to test a real situation BridgeConstants bridgeConstants = BridgeMainNetConstants.getInstance(); - Federation erpFederation = new ErpFederation( + Federation erpFederation = new LegacyErpFederation( FederationMember.getFederationMembersFromKeys(Arrays.asList( new BtcECKey(), new BtcECKey(), diff --git a/rskj-core/src/test/java/co/rsk/peg/performance/ActiveFederationTest.java b/rskj-core/src/test/java/co/rsk/peg/performance/ActiveFederationTest.java index 5ef18a595bc..96b4a111779 100644 --- a/rskj-core/src/test/java/co/rsk/peg/performance/ActiveFederationTest.java +++ b/rskj-core/src/test/java/co/rsk/peg/performance/ActiveFederationTest.java @@ -20,10 +20,7 @@ import co.rsk.bitcoinj.core.BtcECKey; import co.rsk.bitcoinj.store.BtcBlockStore; -import co.rsk.peg.Bridge; -import co.rsk.peg.BridgeStorageProvider; -import co.rsk.peg.Federation; -import co.rsk.peg.FederationMember; +import co.rsk.peg.*; import org.ethereum.TestUtils; import org.ethereum.core.CallTransaction; import org.ethereum.core.Repository; @@ -117,7 +114,7 @@ private BridgeStorageProviderInitializer buildInitializer(boolean genesis) { int numFederators = Helper.randomInRange(minFederators, maxFederators); List members = getNRandomFederationMembers(numFederators); - federation = new Federation( + federation = new StandardMultisigFederation( members, Instant.ofEpochMilli(TestUtils.generateLong(String.valueOf(executionIndex))), Helper.randomInRange(1, 10), diff --git a/rskj-core/src/test/java/co/rsk/peg/performance/RetiringFederationTest.java b/rskj-core/src/test/java/co/rsk/peg/performance/RetiringFederationTest.java index 669478ce058..227f4d13f5a 100644 --- a/rskj-core/src/test/java/co/rsk/peg/performance/RetiringFederationTest.java +++ b/rskj-core/src/test/java/co/rsk/peg/performance/RetiringFederationTest.java @@ -22,6 +22,7 @@ import co.rsk.peg.Bridge; import co.rsk.peg.BridgeStorageProvider; import co.rsk.peg.Federation; +import co.rsk.peg.StandardMultisigFederation; import org.ethereum.core.CallTransaction; import org.ethereum.core.Repository; import org.ethereum.vm.exception.VMException; @@ -103,7 +104,7 @@ private BridgeStorageProviderInitializer buildInitializer(boolean present) { return (BridgeStorageProvider provider, Repository repository, int executionIndex, BtcBlockStore blockStore) -> { if (present) { int numFederators = Helper.randomInRange(minFederators, maxFederators); - retiringFederation = new Federation( + retiringFederation = new StandardMultisigFederation( ActiveFederationTest.getNRandomFederationMembers(numFederators), Instant.ofEpochMilli(random.nextLong()), Helper.randomInRange(1, 10), diff --git a/rskj-core/src/test/java/co/rsk/peg/utils/BridgeEventLoggerImplTest.java b/rskj-core/src/test/java/co/rsk/peg/utils/BridgeEventLoggerImplTest.java index 7876cc65a4e..a83045a72a9 100644 --- a/rskj-core/src/test/java/co/rsk/peg/utils/BridgeEventLoggerImplTest.java +++ b/rskj-core/src/test/java/co/rsk/peg/utils/BridgeEventLoggerImplTest.java @@ -221,7 +221,7 @@ void logCommitFederation(boolean isRSKIP383Active) { List oldFederationMembers = FederationTestUtils.getFederationMembersWithBtcKeys(oldFederationKeys); - Federation oldFederation = new Federation( + Federation oldFederation = new StandardMultisigFederation( oldFederationMembers, Instant.ofEpochMilli(15005L), 15L, @@ -236,7 +236,7 @@ void logCommitFederation(boolean isRSKIP383Active) { List newFederationMembers = FederationTestUtils.getFederationMembersWithBtcKeys(newFederationKeys); - Federation newFederation = new Federation( + Federation newFederation = new StandardMultisigFederation( newFederationMembers, Instant.ofEpochMilli(5005L), 0L, diff --git a/rskj-core/src/test/java/co/rsk/peg/utils/BridgeEventLoggerLegacyImplTest.java b/rskj-core/src/test/java/co/rsk/peg/utils/BridgeEventLoggerLegacyImplTest.java index ea3473dfe26..dfc52e60c63 100644 --- a/rskj-core/src/test/java/co/rsk/peg/utils/BridgeEventLoggerLegacyImplTest.java +++ b/rskj-core/src/test/java/co/rsk/peg/utils/BridgeEventLoggerLegacyImplTest.java @@ -207,7 +207,7 @@ void testLogCommitFederationBeforeRskip146() { List oldFederationMembers = FederationTestUtils.getFederationMembersWithBtcKeys(oldFederationKeys); - Federation oldFederation = new Federation(oldFederationMembers, + Federation oldFederation = new StandardMultisigFederation(oldFederationMembers, Instant.ofEpochMilli(15005L), 15L, NetworkParameters.fromID(NetworkParameters.ID_REGTEST)); List newFederationKeys = Arrays.asList( @@ -218,7 +218,7 @@ void testLogCommitFederationBeforeRskip146() { List newFederationMembers = FederationTestUtils.getFederationMembersWithBtcKeys(newFederationKeys); - Federation newFederation = new Federation( + Federation newFederation = new StandardMultisigFederation( newFederationMembers, Instant.ofEpochMilli(5005L), 0L, From f880a8d0860ca989218eaa07ab752497e953a797 Mon Sep 17 00:00:00 2001 From: julia zack Date: Sat, 30 Sep 2023 11:13:30 -0300 Subject: [PATCH 09/27] Override Object methods in Federation. Make equals method different in StandardMultisig and ErpFederation to be consistent with hashCode --- .../src/main/java/co/rsk/peg/ErpFederation.java | 14 ++++++++++++++ .../src/main/java/co/rsk/peg/Federation.java | 15 +++------------ .../co/rsk/peg/StandardMultisigFederation.java | 14 ++++++++++++++ 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java b/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java index cced1d6532b..fc37bfecf7c 100644 --- a/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java +++ b/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java @@ -67,6 +67,20 @@ public int getNumberOfSignaturesRequired() { return Integer.parseInt(thresholdChunk.toString()); } + @Override + public boolean equals(Object other){ + if (this == other) { + return true; + } + + if (other == null || this.getClass() != other.getClass()) { + return false; + } + + ErpFederation otherFederation = (ErpFederation) other; + return this.getAddress() == otherFederation.getAddress(); + } + @Override public int hashCode() { // Can use java.util.Objects.hash since all of Instant, int and List have diff --git a/rskj-core/src/main/java/co/rsk/peg/Federation.java b/rskj-core/src/main/java/co/rsk/peg/Federation.java index a74daecdf5e..4ca8cdb1f49 100644 --- a/rskj-core/src/main/java/co/rsk/peg/Federation.java +++ b/rskj-core/src/main/java/co/rsk/peg/Federation.java @@ -140,18 +140,9 @@ public String toString() { return String.format("Got federation with address %s", getAddress()); } - public boolean equals(Object other){ - if (this == other) { - return true; - } - - if (other == null || this.getClass() != other.getClass()) { - return false; - } - - Federation otherFederation = (Federation) other; - return this.getAddress() == otherFederation.getAddress(); - } + @Override + public abstract boolean equals(Object other); + @Override public abstract int hashCode(); } diff --git a/rskj-core/src/main/java/co/rsk/peg/StandardMultisigFederation.java b/rskj-core/src/main/java/co/rsk/peg/StandardMultisigFederation.java index 7adb20b1339..f594041df9e 100644 --- a/rskj-core/src/main/java/co/rsk/peg/StandardMultisigFederation.java +++ b/rskj-core/src/main/java/co/rsk/peg/StandardMultisigFederation.java @@ -63,6 +63,20 @@ public int getNumberOfSignaturesRequired() { return Integer.parseInt(thresholdChunk.toString()); } + @Override + public boolean equals(Object other){ + if (this == other) { + return true; + } + + if (other == null || this.getClass() != other.getClass()) { + return false; + } + + StandardMultisigFederation otherFederation = (StandardMultisigFederation) other; + return this.getAddress() == otherFederation.getAddress(); + } + @Override public int hashCode() { // Can use java.util.Objects.hash since all of Instant, int and List have From 18bab72cf10c69136b5ec4a2936a3bd6fc6ddcce Mon Sep 17 00:00:00 2001 From: julia zack Date: Mon, 2 Oct 2023 19:49:02 -0300 Subject: [PATCH 10/27] Fix almost all tests. --- .../java/co/rsk/config/BridgeConstants.java | 4 +-- .../co/rsk/peg/BridgeSerializationUtils.java | 2 +- .../co/rsk/peg/BridgeStorageProvider.java | 2 +- .../main/java/co/rsk/peg/ErpFederation.java | 28 ++++++--------- .../src/main/java/co/rsk/peg/Federation.java | 10 ++---- .../java/co/rsk/peg/LegacyErpFederation.java | 35 ++++++++++++++++++- .../java/co/rsk/peg/P2shErpFederation.java | 22 ++++++++++++ .../rsk/peg/StandardMultisigFederation.java | 18 ++++------ .../rsk/peg/BridgeSerializationUtilsTest.java | 7 +++- .../java/co/rsk/peg/ErpFederationTest.java | 4 +-- 10 files changed, 89 insertions(+), 43 deletions(-) diff --git a/rskj-core/src/main/java/co/rsk/config/BridgeConstants.java b/rskj-core/src/main/java/co/rsk/config/BridgeConstants.java index fae35ee98b9..6cfda1429d9 100644 --- a/rskj-core/src/main/java/co/rsk/config/BridgeConstants.java +++ b/rskj-core/src/main/java/co/rsk/config/BridgeConstants.java @@ -32,7 +32,7 @@ public abstract class BridgeConstants { protected String btcParamsString; - protected StandardMultisigFederation genesisFederation; + protected Federation genesisFederation; protected int btc2RskMinimumAcceptableConfirmations; protected int btc2RskMinimumAcceptableConfirmationsOnRsk; @@ -96,7 +96,7 @@ public String getBtcParamsString() { return btcParamsString; } - public StandardMultisigFederation getGenesisFederation() { return genesisFederation; } + public StandardMultisigFederation getGenesisFederation() { return (StandardMultisigFederation) genesisFederation; } public int getBtc2RskMinimumAcceptableConfirmations() { return btc2RskMinimumAcceptableConfirmations; diff --git a/rskj-core/src/main/java/co/rsk/peg/BridgeSerializationUtils.java b/rskj-core/src/main/java/co/rsk/peg/BridgeSerializationUtils.java index aed12514fe8..a57b6df06b8 100644 --- a/rskj-core/src/main/java/co/rsk/peg/BridgeSerializationUtils.java +++ b/rskj-core/src/main/java/co/rsk/peg/BridgeSerializationUtils.java @@ -315,7 +315,7 @@ public static Federation deserializeFederation( ); } - public static ErpFederation deserializeErpFederation( + public static LegacyErpFederation deserializeLegacyErpFederation( byte[] data, BridgeConstants bridgeConstants, ActivationConfig.ForBlock activations diff --git a/rskj-core/src/main/java/co/rsk/peg/BridgeStorageProvider.java b/rskj-core/src/main/java/co/rsk/peg/BridgeStorageProvider.java index 9ea7e840400..17781b3c8e3 100644 --- a/rskj-core/src/main/java/co/rsk/peg/BridgeStorageProvider.java +++ b/rskj-core/src/main/java/co/rsk/peg/BridgeStorageProvider.java @@ -1029,7 +1029,7 @@ private Federation deserializeFederationAccordingToVersion( ) { switch (version) { case ERP_FEDERATION_FORMAT_VERSION: - return BridgeSerializationUtils.deserializeErpFederation( + return BridgeSerializationUtils.deserializeLegacyErpFederation( data, bridgeConstants, activations diff --git a/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java b/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java index fc37bfecf7c..40303b48f82 100644 --- a/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java +++ b/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java @@ -32,11 +32,6 @@ public ErpFederation( this.erpPubKeys = EcKeyUtils.getCompressedPubKeysList(erpPubKeys); this.activationDelay = activationDelay; this.activations = activations; - - // Try getting the redeem script in order to validate it can be built - // using the given public keys and csv value - getRedeemScript(); // NOSONAR - getStandardRedeemScript(); // NOSONAR } public List getErpPubKeys() { @@ -57,16 +52,6 @@ public Script getStandardP2SHScript() { return standardP2SHScript; } - @Override - public int getNumberOfSignaturesRequired() { - List standardRedeemScriptChunks = getStandardRedeemScript().getChunks(); - - // the threshold of a multisig is the first chunk of the redeemScript - // and the standardRedeemScript represents a multisig - ScriptChunk thresholdChunk = standardRedeemScriptChunks.get(0); - return Integer.parseInt(thresholdChunk.toString()); - } - @Override public boolean equals(Object other){ if (this == other) { @@ -77,8 +62,17 @@ public boolean equals(Object other){ return false; } - ErpFederation otherFederation = (ErpFederation) other; - return this.getAddress() == otherFederation.getAddress(); + ErpFederation otherErpFederation = (ErpFederation) other; + return this.getNumberOfSignaturesRequired() == otherErpFederation.getNumberOfSignaturesRequired() && + this.getSize() == otherErpFederation.getSize() && + this.getCreationTime().equals(otherErpFederation.getCreationTime()) && + this.creationBlockNumber == otherErpFederation.creationBlockNumber && + this.btcParams.equals(otherErpFederation.btcParams) && + this.members.equals(otherErpFederation.members) && + this.getRedeemScript().equals(otherErpFederation.getRedeemScript()) && + this.erpPubKeys.equals(otherErpFederation.erpPubKeys) && + this.activationDelay == otherErpFederation.activationDelay; + } @Override diff --git a/rskj-core/src/main/java/co/rsk/peg/Federation.java b/rskj-core/src/main/java/co/rsk/peg/Federation.java index 4ca8cdb1f49..02824f4db9f 100644 --- a/rskj-core/src/main/java/co/rsk/peg/Federation.java +++ b/rskj-core/src/main/java/co/rsk/peg/Federation.java @@ -74,7 +74,9 @@ public List getBtcPublicKeys() { .collect(Collectors.toList()); } - public abstract int getNumberOfSignaturesRequired(); + public int getNumberOfSignaturesRequired() { + return getMembers().size() / 2 +1 ; + }; public Instant getCreationTime() { return creationTime; @@ -139,10 +141,4 @@ public boolean isMember(FederationMember federationMember){ public String toString() { return String.format("Got federation with address %s", getAddress()); } - - @Override - public abstract boolean equals(Object other); - - @Override - public abstract int hashCode(); } diff --git a/rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java b/rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java index cfd104dcdcb..c18f260aa87 100644 --- a/rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java +++ b/rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java @@ -31,7 +31,11 @@ public LegacyErpFederation( super(members, creationTime, creationBlockNumber, btcParams, erpPubKeys, activationDelay, activations); + // Try getting the redeem script in order to validate it can be built + // using the given public keys and csv value + getRedeemScript(); validateRedeemScript(); + getStandardRedeemScript(); } @Override @@ -44,7 +48,13 @@ public Script getRedeemScript() { if (redeemScript == null) { logger.debug("[getRedeemScript] Creating the redeem script from the keys"); - redeemScript = ErpFederationRedeemScriptParser.createErpRedeemScriptDeprecated( + redeemScript = activations.isActive(ConsensusRule.RSKIP293) ? + ErpFederationRedeemScriptParser.createErpRedeemScript( + ScriptBuilder.createRedeemScript(getNumberOfSignaturesRequired(), getBtcPublicKeys()), + ScriptBuilder.createRedeemScript(erpPubKeys.size() / 2 + 1, erpPubKeys), + activationDelay + ) : + ErpFederationRedeemScriptParser.createErpRedeemScriptDeprecated( ScriptBuilder.createRedeemScript(getNumberOfSignaturesRequired(), getBtcPublicKeys()), ScriptBuilder.createRedeemScript(erpPubKeys.size() / 2 + 1, erpPubKeys), activationDelay @@ -66,6 +76,29 @@ public Script getStandardRedeemScript() { return standardRedeemScript; } + @Override + public boolean equals(Object other){ + if (this == other) { + return true; + } + + if (other == null || this.getClass() != other.getClass()) { + return false; + } + + LegacyErpFederation otherErpFederation = (LegacyErpFederation) other; + return this.getNumberOfSignaturesRequired() == otherErpFederation.getNumberOfSignaturesRequired() && + this.getSize() == otherErpFederation.getSize() && + this.getCreationTime().equals(otherErpFederation.getCreationTime()) && + this.creationBlockNumber == otherErpFederation.creationBlockNumber && + this.btcParams.equals(otherErpFederation.btcParams) && + this.members.equals(otherErpFederation.members) && + this.getRedeemScript().equals(otherErpFederation.getRedeemScript()) && + this.erpPubKeys.equals(otherErpFederation.erpPubKeys) && + this.activationDelay == otherErpFederation.activationDelay; + + } + private void validateRedeemScript() { if (activations.isActive(ConsensusRule.RSKIP293) && this.redeemScript.equals(new Script(ERP_TESTNET_REDEEM_SCRIPT_BYTES))) { diff --git a/rskj-core/src/main/java/co/rsk/peg/P2shErpFederation.java b/rskj-core/src/main/java/co/rsk/peg/P2shErpFederation.java index 573fe02f903..cd9fd999de7 100644 --- a/rskj-core/src/main/java/co/rsk/peg/P2shErpFederation.java +++ b/rskj-core/src/main/java/co/rsk/peg/P2shErpFederation.java @@ -49,4 +49,26 @@ public final Script getStandardRedeemScript() { } return standardRedeemScript; } + + @Override + public boolean equals(Object other){ + if (this == other) { + return true; + } + + if (other == null || this.getClass() != other.getClass()) { + return false; + } + + P2shErpFederation otherErpFederation = (P2shErpFederation) other; + return this.getNumberOfSignaturesRequired() == otherErpFederation.getNumberOfSignaturesRequired() && + this.getSize() == otherErpFederation.getSize() && + this.getCreationTime().equals(otherErpFederation.getCreationTime()) && + this.creationBlockNumber == otherErpFederation.creationBlockNumber && + this.btcParams.equals(otherErpFederation.btcParams) && + this.members.equals(otherErpFederation.members) && + this.getRedeemScript().equals(otherErpFederation.getRedeemScript()) && + this.erpPubKeys.equals(otherErpFederation.erpPubKeys) && + this.activationDelay == otherErpFederation.activationDelay; + } } diff --git a/rskj-core/src/main/java/co/rsk/peg/StandardMultisigFederation.java b/rskj-core/src/main/java/co/rsk/peg/StandardMultisigFederation.java index f594041df9e..32ce563eb08 100644 --- a/rskj-core/src/main/java/co/rsk/peg/StandardMultisigFederation.java +++ b/rskj-core/src/main/java/co/rsk/peg/StandardMultisigFederation.java @@ -53,16 +53,6 @@ public Script getRedeemScript() { return redeemScript; } - @Override - public int getNumberOfSignaturesRequired() { - List standardRedeemScriptChunks = getRedeemScript().getChunks(); - - // the threshold of a multisig is the first chunk of the redeemScript - // and this fed is a standard multisig - ScriptChunk thresholdChunk = standardRedeemScriptChunks.get(0); - return Integer.parseInt(thresholdChunk.toString()); - } - @Override public boolean equals(Object other){ if (this == other) { @@ -74,7 +64,13 @@ public boolean equals(Object other){ } StandardMultisigFederation otherFederation = (StandardMultisigFederation) other; - return this.getAddress() == otherFederation.getAddress(); + return this.getNumberOfSignaturesRequired() == otherFederation.getNumberOfSignaturesRequired() && + this.getSize() == otherFederation.getSize() && + this.getCreationTime().equals(otherFederation.getCreationTime()) && + this.creationBlockNumber == otherFederation.creationBlockNumber && + this.btcParams.equals(otherFederation.btcParams) && + this.members.equals(otherFederation.members) && + this.getRedeemScript().equals(otherFederation.getRedeemScript()); } @Override diff --git a/rskj-core/src/test/java/co/rsk/peg/BridgeSerializationUtilsTest.java b/rskj-core/src/test/java/co/rsk/peg/BridgeSerializationUtilsTest.java index 730cb05ee13..8379cccfd8c 100644 --- a/rskj-core/src/test/java/co/rsk/peg/BridgeSerializationUtilsTest.java +++ b/rskj-core/src/test/java/co/rsk/peg/BridgeSerializationUtilsTest.java @@ -1253,6 +1253,7 @@ private void testSerializeAndDeserializeFederation( bridgeConstants.getBtcParams() ); byte[] serializedTestFederation = BridgeSerializationUtils.serializeFederation(testFederation); + System.out.println(serializedTestFederation); Federation deserializedTestFederation = BridgeSerializationUtils.deserializeFederation( serializedTestFederation, @@ -1269,14 +1270,18 @@ private void testSerializeAndDeserializeFederation( activations ); byte[] serializedTestErpFederation = BridgeSerializationUtils.serializeFederation(testErpFederation); + System.out.println(serializedTestErpFederation); - Federation deserializedTestErpFederation = BridgeSerializationUtils.deserializeErpFederation( + Federation deserializedTestErpFederation = BridgeSerializationUtils.deserializeLegacyErpFederation( serializedTestErpFederation, bridgeConstants, activations ); + System.out.println(deserializedTestErpFederation); Assertions.assertEquals(testFederation, deserializedTestFederation); + System.out.println(testFederation); + System.out.println(deserializedTestFederation); Assertions.assertEquals(testErpFederation, deserializedTestErpFederation); assertNotEquals(testFederation, deserializedTestErpFederation); assertNotEquals(testErpFederation, deserializedTestFederation); diff --git a/rskj-core/src/test/java/co/rsk/peg/ErpFederationTest.java b/rskj-core/src/test/java/co/rsk/peg/ErpFederationTest.java index 7fe20f8a6db..df1e2a9f910 100644 --- a/rskj-core/src/test/java/co/rsk/peg/ErpFederationTest.java +++ b/rskj-core/src/test/java/co/rsk/peg/ErpFederationTest.java @@ -776,7 +776,7 @@ private void spendFromErpFed( ); } - private ErpFederation createDefaultErpFederation() { + private LegacyErpFederation createDefaultErpFederation() { return new LegacyErpFederation( FederationTestUtils.getFederationMembersWithBtcKeys(defaultKeys), ZonedDateTime.parse("2017-06-10T02:30:00Z").toInstant(), @@ -827,7 +827,7 @@ private void validateErpRedeemScript( assertEquals(ScriptOpCodes.OP_NOTIF, script[index++]); // Next byte should equal M, from an M/N multisig - int m = defaultMultisigKeys.size() / 2 + 1; + int m = defaultMultisigKeys.size() / 2 + 1; // da 8 assertEquals(ScriptOpCodes.getOpCode(String.valueOf(m)), script[index++]); // Assert public keys From 1b79ad964110dcca879405646ce5bc637cbfbb5e Mon Sep 17 00:00:00 2001 From: julia zack Date: Mon, 2 Oct 2023 20:04:39 -0300 Subject: [PATCH 11/27] Refactor some long lines --- rskj-core/src/main/java/co/rsk/peg/BridgeUtils.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/rskj-core/src/main/java/co/rsk/peg/BridgeUtils.java b/rskj-core/src/main/java/co/rsk/peg/BridgeUtils.java index 80cadbb1813..86b34adf13c 100644 --- a/rskj-core/src/main/java/co/rsk/peg/BridgeUtils.java +++ b/rskj-core/src/main/java/co/rsk/peg/BridgeUtils.java @@ -402,7 +402,10 @@ public static boolean isValidPegInTx( throw new ScriptException(message); } Script inputStandardRedeemScript = redeemScriptParser.extractStandardRedeemScript(); - if (activeFederations.stream().anyMatch(federation -> (federation instanceof ErpFederation ? ((ErpFederation) federation).getStandardRedeemScript() : federation.getRedeemScript()).equals(inputStandardRedeemScript))) { + if (activeFederations.stream().anyMatch(federation -> + (federation instanceof ErpFederation ? ((ErpFederation) federation).getStandardRedeemScript() + : federation.getRedeemScript() + ).equals(inputStandardRedeemScript))) { return false; } @@ -490,7 +493,10 @@ private static boolean isPegOutTx(BtcTransaction tx, Federation federation, Acti } public static boolean isPegOutTx(BtcTransaction tx, List federations, ActivationConfig.ForBlock activations) { - return isPegOutTx(tx, activations, federations.stream().filter(Objects::nonNull).map(fed -> fed instanceof ErpFederation ? ((ErpFederation) fed).getStandardRedeemScript() : fed.getRedeemScript()).toArray(Script[]::new)); + return isPegOutTx(tx, activations, federations.stream().filter(Objects::nonNull).map(fed -> + fed instanceof ErpFederation ? ((ErpFederation) fed).getStandardRedeemScript() + : fed.getRedeemScript() + ).toArray(Script[]::new)); } public static boolean isPegOutTx(BtcTransaction tx, ActivationConfig.ForBlock activations, Script... p2shScript) { From 4fdd864e1cac751a09349d5320aa8b0d7269b56d Mon Sep 17 00:00:00 2001 From: julia zack Date: Tue, 3 Oct 2023 10:55:29 -0300 Subject: [PATCH 12/27] Fix isPegOutTx. Rollback toString method from Federation --- rskj-core/src/main/java/co/rsk/peg/BridgeUtils.java | 8 +++++--- rskj-core/src/main/java/co/rsk/peg/Federation.java | 3 +-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/rskj-core/src/main/java/co/rsk/peg/BridgeUtils.java b/rskj-core/src/main/java/co/rsk/peg/BridgeUtils.java index 86b34adf13c..b7f3cc2054c 100644 --- a/rskj-core/src/main/java/co/rsk/peg/BridgeUtils.java +++ b/rskj-core/src/main/java/co/rsk/peg/BridgeUtils.java @@ -494,8 +494,8 @@ private static boolean isPegOutTx(BtcTransaction tx, Federation federation, Acti public static boolean isPegOutTx(BtcTransaction tx, List federations, ActivationConfig.ForBlock activations) { return isPegOutTx(tx, activations, federations.stream().filter(Objects::nonNull).map(fed -> - fed instanceof ErpFederation ? ((ErpFederation) fed).getStandardRedeemScript() - : fed.getRedeemScript() + fed instanceof ErpFederation ? ((ErpFederation) fed).getStandardP2SHScript() + : fed.getP2SHScript() ).toArray(Script[]::new)); } @@ -521,7 +521,9 @@ 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))) { + System.out.println(); return true; } } diff --git a/rskj-core/src/main/java/co/rsk/peg/Federation.java b/rskj-core/src/main/java/co/rsk/peg/Federation.java index 02824f4db9f..2bc08ed87ab 100644 --- a/rskj-core/src/main/java/co/rsk/peg/Federation.java +++ b/rskj-core/src/main/java/co/rsk/peg/Federation.java @@ -136,9 +136,8 @@ public boolean isMember(FederationMember federationMember){ return this.members.contains(federationMember); } - // TODO: take a look at this @Override public String toString() { - return String.format("Got federation with address %s", getAddress()); + return String.format("%d of %d signatures federation", getNumberOfSignaturesRequired(), members.size()); } } From fbb07af41dfb17982b22bbeac0cfea806238ec85 Mon Sep 17 00:00:00 2001 From: julia zack Date: Tue, 3 Oct 2023 14:02:20 -0300 Subject: [PATCH 13/27] Rename FederationTest. Refactor federation types --- .../src/main/java/co/rsk/peg/Federation.java | 1 + .../java/co/rsk/peg/PowpegMigrationTest.java | 40 +++++++++---------- ...va => StandardMultisigFederationTest.java} | 2 +- 3 files changed, 22 insertions(+), 21 deletions(-) rename rskj-core/src/test/java/co/rsk/peg/{FederationTest.java => StandardMultisigFederationTest.java} (99%) diff --git a/rskj-core/src/main/java/co/rsk/peg/Federation.java b/rskj-core/src/main/java/co/rsk/peg/Federation.java index 2bc08ed87ab..918c9b5f790 100644 --- a/rskj-core/src/main/java/co/rsk/peg/Federation.java +++ b/rskj-core/src/main/java/co/rsk/peg/Federation.java @@ -136,6 +136,7 @@ public boolean isMember(FederationMember federationMember){ return this.members.contains(federationMember); } + // TODO: define if we want to change this method or not @Override public String toString() { return String.format("%d of %d signatures federation", getNumberOfSignaturesRequired(), members.size()); diff --git a/rskj-core/src/test/java/co/rsk/peg/PowpegMigrationTest.java b/rskj-core/src/test/java/co/rsk/peg/PowpegMigrationTest.java index 0a114606cc1..4909cc5219e 100644 --- a/rskj-core/src/test/java/co/rsk/peg/PowpegMigrationTest.java +++ b/rskj-core/src/test/java/co/rsk/peg/PowpegMigrationTest.java @@ -134,7 +134,7 @@ private void testChangePowpeg( Federation originalPowpeg; switch (oldPowPegFederationType) { - case erp: + case legacyErp: originalPowpeg = new LegacyErpFederation( originalPowpegMembers, Instant.now(), @@ -145,7 +145,7 @@ private void testChangePowpeg( activations ); break; - case p2sh: + case p2shErp: originalPowpeg = new P2shErpFederation( originalPowpegMembers, Instant.now(), @@ -204,10 +204,10 @@ private void testChangePowpeg( Federation newPowPeg = argumentCaptor.getValue(); assertEquals(newPowPegAddress, newPowPeg.getAddress()); switch (newPowPegFederationType) { - case erp: - assertSame(ErpFederation.class, newPowPeg.getClass()); + case legacyErp: + assertSame(LegacyErpFederation.class, newPowPeg.getClass()); break; - case p2sh: + case p2shErp: assertSame(P2shErpFederation.class, newPowPeg.getClass()); // TODO: CHECK REDEEMSCRIPT break; @@ -588,12 +588,12 @@ private void testChangePowpeg( Script lastRetiredFederationP2SHScript = lastRetiredFederationP2SHScriptOptional.get(); if (activations.isActive(ConsensusRule.RSKIP377)){ - if (oldPowPegFederationType == FederationType.erp || oldPowPegFederationType == FederationType.p2sh){ + if (oldPowPegFederationType == FederationType.legacyErp || oldPowPegFederationType == FederationType.p2shErp){ assertNotEquals(lastRetiredFederationP2SHScript, originalPowpeg.getP2SHScript()); } assertEquals(lastRetiredFederationP2SHScript, originalPowpeg instanceof ErpFederation ? ((ErpFederation) originalPowpeg).getStandardP2SHScript() : originalPowpeg.getP2SHScript()); } else { - if (oldPowPegFederationType == FederationType.erp || oldPowPegFederationType == FederationType.p2sh){ + if (oldPowPegFederationType == FederationType.legacyErp || oldPowPegFederationType == FederationType.p2shErp){ assertEquals(lastRetiredFederationP2SHScript, originalPowpeg.getP2SHScript()); assertNotEquals(lastRetiredFederationP2SHScript, originalPowpeg instanceof ErpFederation ? ((ErpFederation) originalPowpeg).getStandardP2SHScript() : originalPowpeg.getP2SHScript()); } else { @@ -1321,11 +1321,11 @@ void test_change_powpeg_from_erpFederation_with_mainnet_powpeg_pre_RSKIP_353_cre ); testChangePowpeg( - FederationType.erp, + FederationType.legacyErp, getMainnetPowpegKeys(), originalPowpegAddress, utxos, - FederationType.erp, + FederationType.legacyErp, newPowpegKeys, newPowpegAddress, bridgeConstants, @@ -1347,11 +1347,11 @@ void test_change_powpeg_from_erpFederation_with_mainnet_powpeg_post_RSKIP_353_cr ); testChangePowpeg( - FederationType.erp, + FederationType.legacyErp, getMainnetPowpegKeys(), originalPowpegAddress, utxos, - FederationType.p2sh, + FederationType.p2shErp, getMainnetPowpegKeys(), // Using same keys as the original powpeg, should result in a different address since it will create a p2sh erp federation newPowpegAddress, bridgeConstants, @@ -1393,11 +1393,11 @@ void test_change_powpeg_from_p2shErpFederation_with_mainnet_powpeg_post_RSKIP_35 ); testChangePowpeg( - FederationType.p2sh, + FederationType.p2shErp, getMainnetPowpegKeys(), originalPowpegAddress, utxos, - FederationType.p2sh, + FederationType.p2shErp, newPowPegKeys, newPowPegAddress, bridgeConstants, @@ -1458,11 +1458,11 @@ void test_change_powpeg_from_p2shErpFederation_with_mainnet_powpeg(ActivationCon ); testChangePowpeg( - FederationType.p2sh, + FederationType.p2shErp, getMainnetPowpegKeys(), originalPowpegAddress, utxos, - FederationType.p2sh, + FederationType.p2shErp, newPowPegKeys, newPowPegAddress, bridgeConstants, @@ -1506,11 +1506,11 @@ void test_change_powpeg_from_p2shErpFederation_with_mainnet_powpeg_post_RSKIP_37 ); testChangePowpeg( - FederationType.p2sh, + FederationType.p2shErp, getMainnetPowpegKeys(), originalPowpegAddress, utxos, - FederationType.p2sh, + FederationType.p2shErp, newPowPegKeys, newPowPegAddress, bridgeConstants, @@ -1520,8 +1520,8 @@ void test_change_powpeg_from_p2shErpFederation_with_mainnet_powpeg_post_RSKIP_37 } private enum FederationType { - erp, - p2sh, - standard + legacyErp, + p2shErp, + standardMultisig } } diff --git a/rskj-core/src/test/java/co/rsk/peg/FederationTest.java b/rskj-core/src/test/java/co/rsk/peg/StandardMultisigFederationTest.java similarity index 99% rename from rskj-core/src/test/java/co/rsk/peg/FederationTest.java rename to rskj-core/src/test/java/co/rsk/peg/StandardMultisigFederationTest.java index 069c090424a..e0e5a8570c1 100644 --- a/rskj-core/src/test/java/co/rsk/peg/FederationTest.java +++ b/rskj-core/src/test/java/co/rsk/peg/StandardMultisigFederationTest.java @@ -45,7 +45,7 @@ import static org.mockito.Mockito.mockStatic; @ExtendWith(MockitoExtension.class) -class FederationTest { +class StandardMultisigFederationTest { private Federation federation; private List sortedPublicKeys; private List rskPubKeys; From 4daf0187507199738a1e86bd626c49c76fb1a5c2 Mon Sep 17 00:00:00 2001 From: julia zack Date: Tue, 3 Oct 2023 15:53:25 -0300 Subject: [PATCH 14/27] Refactor equals method --- .../main/java/co/rsk/peg/ErpFederation.java | 1 - .../java/co/rsk/peg/LegacyErpFederation.java | 23 ------------------- .../java/co/rsk/peg/P2shErpFederation.java | 22 ------------------ 3 files changed, 46 deletions(-) diff --git a/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java b/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java index 40303b48f82..103a6569e3c 100644 --- a/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java +++ b/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java @@ -72,7 +72,6 @@ public boolean equals(Object other){ this.getRedeemScript().equals(otherErpFederation.getRedeemScript()) && this.erpPubKeys.equals(otherErpFederation.erpPubKeys) && this.activationDelay == otherErpFederation.activationDelay; - } @Override diff --git a/rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java b/rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java index c18f260aa87..180b6fdeeb5 100644 --- a/rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java +++ b/rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java @@ -76,29 +76,6 @@ public Script getStandardRedeemScript() { return standardRedeemScript; } - @Override - public boolean equals(Object other){ - if (this == other) { - return true; - } - - if (other == null || this.getClass() != other.getClass()) { - return false; - } - - LegacyErpFederation otherErpFederation = (LegacyErpFederation) other; - return this.getNumberOfSignaturesRequired() == otherErpFederation.getNumberOfSignaturesRequired() && - this.getSize() == otherErpFederation.getSize() && - this.getCreationTime().equals(otherErpFederation.getCreationTime()) && - this.creationBlockNumber == otherErpFederation.creationBlockNumber && - this.btcParams.equals(otherErpFederation.btcParams) && - this.members.equals(otherErpFederation.members) && - this.getRedeemScript().equals(otherErpFederation.getRedeemScript()) && - this.erpPubKeys.equals(otherErpFederation.erpPubKeys) && - this.activationDelay == otherErpFederation.activationDelay; - - } - private void validateRedeemScript() { if (activations.isActive(ConsensusRule.RSKIP293) && this.redeemScript.equals(new Script(ERP_TESTNET_REDEEM_SCRIPT_BYTES))) { diff --git a/rskj-core/src/main/java/co/rsk/peg/P2shErpFederation.java b/rskj-core/src/main/java/co/rsk/peg/P2shErpFederation.java index cd9fd999de7..573fe02f903 100644 --- a/rskj-core/src/main/java/co/rsk/peg/P2shErpFederation.java +++ b/rskj-core/src/main/java/co/rsk/peg/P2shErpFederation.java @@ -49,26 +49,4 @@ public final Script getStandardRedeemScript() { } return standardRedeemScript; } - - @Override - public boolean equals(Object other){ - if (this == other) { - return true; - } - - if (other == null || this.getClass() != other.getClass()) { - return false; - } - - P2shErpFederation otherErpFederation = (P2shErpFederation) other; - return this.getNumberOfSignaturesRequired() == otherErpFederation.getNumberOfSignaturesRequired() && - this.getSize() == otherErpFederation.getSize() && - this.getCreationTime().equals(otherErpFederation.getCreationTime()) && - this.creationBlockNumber == otherErpFederation.creationBlockNumber && - this.btcParams.equals(otherErpFederation.btcParams) && - this.members.equals(otherErpFederation.members) && - this.getRedeemScript().equals(otherErpFederation.getRedeemScript()) && - this.erpPubKeys.equals(otherErpFederation.erpPubKeys) && - this.activationDelay == otherErpFederation.activationDelay; - } } From fdab85606d5895c75494e3885cb1209a887aa373 Mon Sep 17 00:00:00 2001 From: julia zack Date: Tue, 3 Oct 2023 15:56:53 -0300 Subject: [PATCH 15/27] Add address to toString method from Federation --- rskj-core/src/main/java/co/rsk/peg/Federation.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/rskj-core/src/main/java/co/rsk/peg/Federation.java b/rskj-core/src/main/java/co/rsk/peg/Federation.java index 918c9b5f790..d06cf606ee0 100644 --- a/rskj-core/src/main/java/co/rsk/peg/Federation.java +++ b/rskj-core/src/main/java/co/rsk/peg/Federation.java @@ -136,9 +136,8 @@ public boolean isMember(FederationMember federationMember){ return this.members.contains(federationMember); } - // TODO: define if we want to change this method or not @Override public String toString() { - return String.format("%d of %d signatures federation", getNumberOfSignaturesRequired(), members.size()); + return String.format("Got %d of %d signatures federation with address %s", getNumberOfSignaturesRequired(), members.size(), getAddress()); } } From a4c4399617076a924fde0393ea087ab62f0e730b Mon Sep 17 00:00:00 2001 From: julia zack Date: Tue, 3 Oct 2023 17:58:31 -0300 Subject: [PATCH 16/27] Fix all tests --- .../peg/StandardMultisigFederationTest.java | 105 +++++++----------- 1 file changed, 39 insertions(+), 66 deletions(-) diff --git a/rskj-core/src/test/java/co/rsk/peg/StandardMultisigFederationTest.java b/rskj-core/src/test/java/co/rsk/peg/StandardMultisigFederationTest.java index e0e5a8570c1..d2ce6808d77 100644 --- a/rskj-core/src/test/java/co/rsk/peg/StandardMultisigFederationTest.java +++ b/rskj-core/src/test/java/co/rsk/peg/StandardMultisigFederationTest.java @@ -22,6 +22,7 @@ import co.rsk.bitcoinj.core.NetworkParameters; import co.rsk.bitcoinj.script.Script; import co.rsk.bitcoinj.script.ScriptBuilder; +import co.rsk.bitcoinj.script.ScriptOpCodes; import org.bouncycastle.util.encoders.Hex; import org.ethereum.TestUtils; import org.ethereum.crypto.ECKey; @@ -29,23 +30,21 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.MockedStatic; -import org.mockito.junit.jupiter.MockitoExtension; import java.math.BigInteger; import java.time.ZonedDateTime; import java.util.*; import java.util.stream.Collectors; -import java.util.stream.Stream; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.not; import static org.mockito.Mockito.any; import static org.mockito.Mockito.mockStatic; -@ExtendWith(MockitoExtension.class) class StandardMultisigFederationTest { + + private List btcPublicKeys; private Federation federation; private List sortedPublicKeys; private List rskPubKeys; @@ -53,24 +52,25 @@ class StandardMultisigFederationTest { @BeforeEach void createFederation() { + btcPublicKeys = Arrays.asList( + BtcECKey.fromPublicOnly(Hex.decode("023f0283519167f1603ba92b060146baa054712b938a61f35605ba08773142f4da")), + BtcECKey.fromPublicOnly(Hex.decode("02afc230c2d355b1a577682b07bc2646041b5d0177af0f98395a46018da699b6da")), + BtcECKey.fromPublicOnly(Hex.decode("031174d64db12dc2dcdc8064a53a4981fa60f4ee649a954e01bcae221fc60777a2")), + BtcECKey.fromPublicOnly(Hex.decode("0344a3c38cd59afcba3edcebe143e025574594b001700dec41e59409bdbd0f2a09")), + BtcECKey.fromPublicOnly(Hex.decode("039a060badbeb24bee49eb2063f616c0f0f0765d4ca646b20a88ce828f259fcdb9"))); + federation = new StandardMultisigFederation( - FederationTestUtils.getFederationMembersFromPks(100, 200, 300, 400, 500, 600), + FederationTestUtils.getFederationMembersWithKeys(btcPublicKeys), ZonedDateTime.parse("2017-06-10T02:30:00Z").toInstant(), 0L, NetworkParameters.fromID(NetworkParameters.ID_REGTEST) ); - sortedPublicKeys = Arrays.stream(new BtcECKey[]{ - BtcECKey.fromPrivate(BigInteger.valueOf(100)), - BtcECKey.fromPrivate(BigInteger.valueOf(200)), - BtcECKey.fromPrivate(BigInteger.valueOf(300)), - BtcECKey.fromPrivate(BigInteger.valueOf(400)), - BtcECKey.fromPrivate(BigInteger.valueOf(500)), - BtcECKey.fromPrivate(BigInteger.valueOf(600)), - }).sorted(BtcECKey.PUBKEY_COMPARATOR).collect(Collectors.toList()); - - rskPubKeys = Stream.of(101, 201, 301, 401, 501, 601) - .map(i -> ECKey.fromPrivate(BigInteger.valueOf(i))) + sortedPublicKeys = btcPublicKeys.stream() + .sorted(BtcECKey.PUBKEY_COMPARATOR).collect(Collectors.toList()); + + rskPubKeys = sortedPublicKeys.stream() + .map(btcECKey -> ECKey.fromPublicOnly(btcECKey.getPubKey())) .collect(Collectors.toList()); rskAddresses = rskPubKeys @@ -106,8 +106,8 @@ void redeemScript() { calls.add(1); int numberOfSignaturesRequired = invocationOnMock.getArgument(0); List publicKeys = invocationOnMock.getArgument(1); - Assertions.assertEquals(4, numberOfSignaturesRequired); - Assertions.assertEquals(6, publicKeys.size()); + Assertions.assertEquals(3, numberOfSignaturesRequired); + Assertions.assertEquals(5, publicKeys.size()); for (int i = 0; i < sortedPublicKeys.size(); i++) { Assertions.assertArrayEquals(sortedPublicKeys.get(i).getPubKey(), publicKeys.get(i).getPubKey()); @@ -121,52 +121,20 @@ void redeemScript() { } @Test - void P2SHScript() { - final List calls = new ArrayList<>(); - try (MockedStatic scriptBuilderMocked = mockStatic(ScriptBuilder.class)) { - scriptBuilderMocked.when(() -> ScriptBuilder.createP2SHOutputScript(any(int.class), any(List.class))).thenAnswer((invocationOnMock) -> { - calls.add(0); - int numberOfSignaturesRequired = invocationOnMock.getArgument(0); - List publicKeys = invocationOnMock.getArgument(1); - Assertions.assertEquals(4, numberOfSignaturesRequired); - Assertions.assertEquals(6, publicKeys.size()); - for (int i = 0; i < sortedPublicKeys.size(); i++) { - Assertions.assertArrayEquals(sortedPublicKeys.get(i).getPubKey(), - publicKeys.get(i).getPubKey()); - } - return new Script(new byte[]{(byte) 0xaa}); - }); - Assertions.assertArrayEquals(federation.getP2SHScript().getProgram(), new byte[]{(byte) 0xaa}); - // Make sure the script creation happens only once - Assertions.assertEquals(1, calls.size()); - } + void testArrayEquals_P2SHScript() { + ScriptBuilder p2shScriptBuilder = new ScriptBuilder(); + p2shScriptBuilder.op(ScriptOpCodes.OP_HASH160); + p2shScriptBuilder.data(Hex.decode("57f76bf3ab818811c740929ac7a5e3ef8c7a34b9")); + p2shScriptBuilder.op(ScriptOpCodes.OP_EQUAL); + + Script p2shScript = p2shScriptBuilder.build(); + + Assertions.assertArrayEquals(federation.getP2SHScript().getProgram(), p2shScript.getProgram()); } @Test - void Address() { - // Since we can't mock both Address and ScriptBuilder at the same time (due to PowerMockito limitations) - // we use a well known P2SH and its corresponding address - // and just mock the ScriptBuilder - // a914896ed9f3446d51b5510f7f0b6ef81b2bde55140e87 => 2N5muMepJizJE1gR7FbHJU6CD18V3BpNF9p - final List calls = new ArrayList<>(); - try (MockedStatic scriptBuilderMocked = mockStatic(ScriptBuilder.class)) { - scriptBuilderMocked.when(() -> ScriptBuilder.createP2SHOutputScript(any(int.class), any(List.class))).thenAnswer((invocationOnMock) -> { - calls.add(0); - int numberOfSignaturesRequired = invocationOnMock.getArgument(0); - List publicKeys = invocationOnMock.getArgument(1); - Assertions.assertEquals(4, numberOfSignaturesRequired); - Assertions.assertEquals(6, publicKeys.size()); - for (int i = 0; i < sortedPublicKeys.size(); i++) { - Assertions.assertArrayEquals(sortedPublicKeys.get(i).getPubKey(), - publicKeys.get(i).getPubKey()); - } - return new Script(Hex.decode("a914896ed9f3446d51b5510f7f0b6ef81b2bde55140e87")); - }); - - Assertions.assertEquals("2N5muMepJizJE1gR7FbHJU6CD18V3BpNF9p", federation.getAddress().toBase58()); - // Make sure the address creation happens only once - Assertions.assertEquals(1, calls.size()); - } + void testEquals_Address() { + Assertions.assertEquals("2N1GMB8gxHYR5HLPSRgf9CJ9Lunjb9CTnKB", federation.getAddress().toBase58()); } @Test @@ -249,10 +217,15 @@ void testEquals_differentMembers() { @Test void testEquals_same() { Federation otherFederation = new StandardMultisigFederation( - FederationTestUtils.getFederationMembersFromPks(100, 200, 300, 400, 500, 600), - ZonedDateTime.parse("2017-06-10T02:30:00Z").toInstant(), - 0L, - NetworkParameters.fromID(NetworkParameters.ID_REGTEST) + FederationTestUtils.getFederationMembersWithKeys(Arrays.asList( + BtcECKey.fromPublicOnly(Hex.decode("023f0283519167f1603ba92b060146baa054712b938a61f35605ba08773142f4da")), + BtcECKey.fromPublicOnly(Hex.decode("02afc230c2d355b1a577682b07bc2646041b5d0177af0f98395a46018da699b6da")), + BtcECKey.fromPublicOnly(Hex.decode("031174d64db12dc2dcdc8064a53a4981fa60f4ee649a954e01bcae221fc60777a2")), + BtcECKey.fromPublicOnly(Hex.decode("0344a3c38cd59afcba3edcebe143e025574594b001700dec41e59409bdbd0f2a09")), + BtcECKey.fromPublicOnly(Hex.decode("039a060badbeb24bee49eb2063f616c0f0f0765d4ca646b20a88ce828f259fcdb9")))), + ZonedDateTime.parse("2017-06-10T02:30:00Z").toInstant(), + 0L, + NetworkParameters.fromID(NetworkParameters.ID_REGTEST) ); Assertions.assertEquals(federation, otherFederation); } @@ -285,7 +258,7 @@ void hasMemberWithRskAddress() { @Test void testToString() { - Assertions.assertEquals("4 of 6 signatures federation", federation.toString()); + Assertions.assertEquals("Got 3 of 5 signatures federation with address 2N1GMB8gxHYR5HLPSRgf9CJ9Lunjb9CTnKB", federation.toString()); } @Test From 21327ae9394ab88e6b032a2b5f7e40981998611c Mon Sep 17 00:00:00 2001 From: julia zack Date: Tue, 3 Oct 2023 17:59:45 -0300 Subject: [PATCH 17/27] Remove unused and unnecessary code --- rskj-core/src/main/java/co/rsk/peg/ErpFederation.java | 1 - rskj-core/src/main/java/co/rsk/peg/Federation.java | 3 +-- rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java | 1 - 3 files changed, 1 insertion(+), 4 deletions(-) diff --git a/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java b/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java index 103a6569e3c..e2be4e1fb4e 100644 --- a/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java +++ b/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java @@ -4,7 +4,6 @@ import co.rsk.bitcoinj.core.NetworkParameters; import co.rsk.bitcoinj.script.Script; import co.rsk.bitcoinj.script.ScriptBuilder; -import co.rsk.bitcoinj.script.ScriptChunk; import co.rsk.peg.utils.EcKeyUtils; import java.time.Instant; import java.util.Collections; diff --git a/rskj-core/src/main/java/co/rsk/peg/Federation.java b/rskj-core/src/main/java/co/rsk/peg/Federation.java index d06cf606ee0..604ed71d961 100644 --- a/rskj-core/src/main/java/co/rsk/peg/Federation.java +++ b/rskj-core/src/main/java/co/rsk/peg/Federation.java @@ -29,7 +29,6 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; -import java.util.Objects; import java.util.stream.Collectors; /** @@ -76,7 +75,7 @@ public List getBtcPublicKeys() { public int getNumberOfSignaturesRequired() { return getMembers().size() / 2 +1 ; - }; + } public Instant getCreationTime() { return creationTime; diff --git a/rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java b/rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java index 180b6fdeeb5..67e8b859c24 100644 --- a/rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java +++ b/rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java @@ -8,7 +8,6 @@ import java.time.Instant; import java.util.List; -import co.rsk.bitcoinj.script.ScriptChunk; import org.bouncycastle.util.encoders.Hex; import org.ethereum.config.blockchain.upgrades.ActivationConfig; import org.ethereum.config.blockchain.upgrades.ConsensusRule; From 1e7826a09ff9352050188adae0f5631510b150d5 Mon Sep 17 00:00:00 2001 From: julia zack Date: Wed, 4 Oct 2023 08:58:56 -0300 Subject: [PATCH 18/27] Modify equals method in Federation and remove some tests related. Rename variables. Remove logs. Fix some code smells. --- .../java/co/rsk/config/BridgeConstants.java | 2 +- .../co/rsk/config/BridgeDevNetConstants.java | 1 - .../co/rsk/config/BridgeMainNetConstants.java | 1 - .../co/rsk/config/BridgeRegTestConstants.java | 1 - .../co/rsk/config/BridgeTestNetConstants.java | 1 - .../co/rsk/peg/BridgeSerializationUtils.java | 10 +-- .../co/rsk/peg/BridgeStorageProvider.java | 16 ++-- .../src/main/java/co/rsk/peg/BridgeUtils.java | 1 - .../main/java/co/rsk/peg/ErpFederation.java | 24 +---- .../src/main/java/co/rsk/peg/Federation.java | 18 +++- .../java/co/rsk/peg/LegacyErpFederation.java | 5 ++ .../rsk/peg/StandardMultisigFederation.java | 21 ----- .../rsk/peg/BridgeSerializationUtilsTest.java | 5 -- .../BridgeStorageProviderFederationTests.java | 88 +++++++++---------- .../java/co/rsk/peg/ErpFederationTest.java | 30 ------- 15 files changed, 80 insertions(+), 144 deletions(-) diff --git a/rskj-core/src/main/java/co/rsk/config/BridgeConstants.java b/rskj-core/src/main/java/co/rsk/config/BridgeConstants.java index 6cfda1429d9..4739c6eb6cd 100644 --- a/rskj-core/src/main/java/co/rsk/config/BridgeConstants.java +++ b/rskj-core/src/main/java/co/rsk/config/BridgeConstants.java @@ -96,7 +96,7 @@ public String getBtcParamsString() { return btcParamsString; } - public StandardMultisigFederation getGenesisFederation() { return (StandardMultisigFederation) genesisFederation; } + public Federation getGenesisFederation() { return genesisFederation; } public int getBtc2RskMinimumAcceptableConfirmations() { return btc2RskMinimumAcceptableConfirmations; diff --git a/rskj-core/src/main/java/co/rsk/config/BridgeDevNetConstants.java b/rskj-core/src/main/java/co/rsk/config/BridgeDevNetConstants.java index 72f1a69372c..4b363336571 100644 --- a/rskj-core/src/main/java/co/rsk/config/BridgeDevNetConstants.java +++ b/rskj-core/src/main/java/co/rsk/config/BridgeDevNetConstants.java @@ -22,7 +22,6 @@ 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; diff --git a/rskj-core/src/main/java/co/rsk/config/BridgeMainNetConstants.java b/rskj-core/src/main/java/co/rsk/config/BridgeMainNetConstants.java index b8c117f4e9d..b07ccb57250 100644 --- a/rskj-core/src/main/java/co/rsk/config/BridgeMainNetConstants.java +++ b/rskj-core/src/main/java/co/rsk/config/BridgeMainNetConstants.java @@ -4,7 +4,6 @@ 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; diff --git a/rskj-core/src/main/java/co/rsk/config/BridgeRegTestConstants.java b/rskj-core/src/main/java/co/rsk/config/BridgeRegTestConstants.java index 91a8cf44c9c..db7c4f3520f 100644 --- a/rskj-core/src/main/java/co/rsk/config/BridgeRegTestConstants.java +++ b/rskj-core/src/main/java/co/rsk/config/BridgeRegTestConstants.java @@ -22,7 +22,6 @@ 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; diff --git a/rskj-core/src/main/java/co/rsk/config/BridgeTestNetConstants.java b/rskj-core/src/main/java/co/rsk/config/BridgeTestNetConstants.java index f5253cb4769..540fec54195 100644 --- a/rskj-core/src/main/java/co/rsk/config/BridgeTestNetConstants.java +++ b/rskj-core/src/main/java/co/rsk/config/BridgeTestNetConstants.java @@ -22,7 +22,6 @@ 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; diff --git a/rskj-core/src/main/java/co/rsk/peg/BridgeSerializationUtils.java b/rskj-core/src/main/java/co/rsk/peg/BridgeSerializationUtils.java index a57b6df06b8..a9e92243dae 100644 --- a/rskj-core/src/main/java/co/rsk/peg/BridgeSerializationUtils.java +++ b/rskj-core/src/main/java/co/rsk/peg/BridgeSerializationUtils.java @@ -246,7 +246,7 @@ private static byte[] serializeFederationWithSerializer(Federation federation, F } // For the serialization format, see BridgeSerializationUtils::serializeFederationWithSerializer - private static Federation deserializeFederationWithDeserializer( + private static Federation deserializeStandardMultisigFederationWithDeserializer( byte[] data, NetworkParameters networkParameters, FederationMemberDesserializer federationMemberDesserializer) { @@ -288,7 +288,7 @@ 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, + return deserializeStandardMultisigFederationWithDeserializer(data, networkParameters, (pubKeyBytes -> FederationMember.getFederationMemberFromKey(BtcECKey.fromPublicOnly(pubKeyBytes)))); } @@ -308,7 +308,7 @@ public static Federation deserializeFederation( byte[] data, NetworkParameters networkParameters ) { - return deserializeFederationWithDeserializer( + return deserializeStandardMultisigFederationWithDeserializer( data, networkParameters, BridgeSerializationUtils::deserializeFederationMember @@ -320,7 +320,7 @@ public static LegacyErpFederation deserializeLegacyErpFederation( BridgeConstants bridgeConstants, ActivationConfig.ForBlock activations ) { - Federation federation = deserializeFederationWithDeserializer( + Federation federation = deserializeStandardMultisigFederationWithDeserializer( data, bridgeConstants.getBtcParams(), BridgeSerializationUtils::deserializeFederationMember @@ -342,7 +342,7 @@ public static P2shErpFederation deserializeP2shErpFederation( BridgeConstants bridgeConstants, ActivationConfig.ForBlock activations ) { - Federation federation = deserializeFederationWithDeserializer( + Federation federation = deserializeStandardMultisigFederationWithDeserializer( data, bridgeConstants.getBtcParams(), BridgeSerializationUtils::deserializeFederationMember diff --git a/rskj-core/src/main/java/co/rsk/peg/BridgeStorageProvider.java b/rskj-core/src/main/java/co/rsk/peg/BridgeStorageProvider.java index 17781b3c8e3..42532b4e317 100644 --- a/rskj-core/src/main/java/co/rsk/peg/BridgeStorageProvider.java +++ b/rskj-core/src/main/java/co/rsk/peg/BridgeStorageProvider.java @@ -48,8 +48,8 @@ * @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 STANDARD_MULTISIG_FEDERATION_FORMAT_VERSION_MULTIKEY = 1000; + private static final int LEGACY_ERP_FEDERATION_FORMAT_VERSION = 2000; private static final int P2SH_ERP_FEDERATION_FORMAT_VERSION = 3000; // Dummy value to use when saved Fast Bridge Derivation Argument Hash @@ -367,12 +367,12 @@ public void saveNewFederation() { } else if (activations.isActive(RSKIP201) && newFederation instanceof ErpFederation) { 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_MULTIKEY ); } serializer = BridgeSerializationUtils::serializeFederation; @@ -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_MULTIKEY ); } @@ -480,7 +480,7 @@ public void savePendingFederation() { RepositorySerializer 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_MULTIKEY); serializer = BridgeSerializationUtils::serializePendingFederation; } @@ -1028,7 +1028,7 @@ private Federation deserializeFederationAccordingToVersion( BridgeConstants bridgeConstants ) { switch (version) { - case ERP_FEDERATION_FORMAT_VERSION: + case LEGACY_ERP_FEDERATION_FORMAT_VERSION: return BridgeSerializationUtils.deserializeLegacyErpFederation( data, bridgeConstants, diff --git a/rskj-core/src/main/java/co/rsk/peg/BridgeUtils.java b/rskj-core/src/main/java/co/rsk/peg/BridgeUtils.java index b7f3cc2054c..f4dbcdae97e 100644 --- a/rskj-core/src/main/java/co/rsk/peg/BridgeUtils.java +++ b/rskj-core/src/main/java/co/rsk/peg/BridgeUtils.java @@ -523,7 +523,6 @@ public static boolean isPegOutTx(BtcTransaction tx, ActivationConfig.ForBlock ac Script outputScript = ScriptBuilder.createP2SHOutputScript(redeemScript); if (Stream.of(p2shScript).anyMatch(federationPayScript -> federationPayScript.equals(outputScript))) { - System.out.println(); return true; } } diff --git a/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java b/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java index e2be4e1fb4e..9341103dbd8 100644 --- a/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java +++ b/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java @@ -18,7 +18,7 @@ public abstract class ErpFederation extends Federation { protected Script standardRedeemScript; protected Script standardP2SHScript; - public ErpFederation( + protected ErpFederation( List members, Instant creationTime, long creationBlockNumber, @@ -51,28 +51,6 @@ public Script getStandardP2SHScript() { return standardP2SHScript; } - @Override - public boolean equals(Object other){ - if (this == other) { - return true; - } - - if (other == null || this.getClass() != other.getClass()) { - return false; - } - - ErpFederation otherErpFederation = (ErpFederation) other; - return this.getNumberOfSignaturesRequired() == otherErpFederation.getNumberOfSignaturesRequired() && - this.getSize() == otherErpFederation.getSize() && - this.getCreationTime().equals(otherErpFederation.getCreationTime()) && - this.creationBlockNumber == otherErpFederation.creationBlockNumber && - this.btcParams.equals(otherErpFederation.btcParams) && - this.members.equals(otherErpFederation.members) && - this.getRedeemScript().equals(otherErpFederation.getRedeemScript()) && - this.erpPubKeys.equals(otherErpFederation.erpPubKeys) && - this.activationDelay == otherErpFederation.activationDelay; - } - @Override public int hashCode() { // Can use java.util.Objects.hash since all of Instant, int and List have diff --git a/rskj-core/src/main/java/co/rsk/peg/Federation.java b/rskj-core/src/main/java/co/rsk/peg/Federation.java index 604ed71d961..6746f075aae 100644 --- a/rskj-core/src/main/java/co/rsk/peg/Federation.java +++ b/rskj-core/src/main/java/co/rsk/peg/Federation.java @@ -47,7 +47,7 @@ public abstract class Federation { protected Script p2shScript; protected Address address; - public Federation(List members, Instant creationTime, long creationBlockNumber, NetworkParameters btcParams) { + protected Federation(List members, Instant creationTime, long creationBlockNumber, NetworkParameters btcParams) { // Sorting members ensures same order of federation members for same members // Immutability provides protection against unwanted modification, thus making the Federation instance // effectively immutable @@ -74,7 +74,7 @@ public List getBtcPublicKeys() { } public int getNumberOfSignaturesRequired() { - return getMembers().size() / 2 +1 ; + return members.size() / 2 +1 ; } public Instant getCreationTime() { @@ -139,4 +139,18 @@ public boolean isMember(FederationMember federationMember){ public String toString() { return String.format("Got %d of %d signatures federation with address %s", getNumberOfSignaturesRequired(), members.size(), getAddress()); } + + @Override + public boolean equals(Object other) { + if (this == other) { + return true; + } + + if (other == null || this.getClass() != other.getClass()) { + return false; + } + + Federation otherFederation = (Federation) other; + return this.getAddress().equals(otherFederation.getAddress()); + } } diff --git a/rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java b/rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java index 67e8b859c24..1bc570d50b6 100644 --- a/rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java +++ b/rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java @@ -14,6 +14,11 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +/** + * @deprecated This class represents a Legacy ERP Federation that is non-standard. + * It has been deprecated but it must be kept because of backwards compatibility. + */ + @Deprecated public class LegacyErpFederation extends ErpFederation { private static final Logger logger = LoggerFactory.getLogger(LegacyErpFederation.class); diff --git a/rskj-core/src/main/java/co/rsk/peg/StandardMultisigFederation.java b/rskj-core/src/main/java/co/rsk/peg/StandardMultisigFederation.java index 32ce563eb08..d5f799ecd88 100644 --- a/rskj-core/src/main/java/co/rsk/peg/StandardMultisigFederation.java +++ b/rskj-core/src/main/java/co/rsk/peg/StandardMultisigFederation.java @@ -21,7 +21,6 @@ import co.rsk.bitcoinj.core.NetworkParameters; import co.rsk.bitcoinj.script.Script; import co.rsk.bitcoinj.script.ScriptBuilder; -import co.rsk.bitcoinj.script.ScriptChunk; import java.time.Instant; import java.util.List; @@ -53,26 +52,6 @@ public Script getRedeemScript() { return redeemScript; } - @Override - public boolean equals(Object other){ - if (this == other) { - return true; - } - - if (other == null || this.getClass() != other.getClass()) { - return false; - } - - StandardMultisigFederation otherFederation = (StandardMultisigFederation) other; - return this.getNumberOfSignaturesRequired() == otherFederation.getNumberOfSignaturesRequired() && - this.getSize() == otherFederation.getSize() && - this.getCreationTime().equals(otherFederation.getCreationTime()) && - this.creationBlockNumber == otherFederation.creationBlockNumber && - this.btcParams.equals(otherFederation.btcParams) && - this.members.equals(otherFederation.members) && - this.getRedeemScript().equals(otherFederation.getRedeemScript()); - } - @Override public int hashCode() { // Can use java.util.Objects.hash since all of Instant, int and List have diff --git a/rskj-core/src/test/java/co/rsk/peg/BridgeSerializationUtilsTest.java b/rskj-core/src/test/java/co/rsk/peg/BridgeSerializationUtilsTest.java index 8379cccfd8c..21582e47d3f 100644 --- a/rskj-core/src/test/java/co/rsk/peg/BridgeSerializationUtilsTest.java +++ b/rskj-core/src/test/java/co/rsk/peg/BridgeSerializationUtilsTest.java @@ -1253,7 +1253,6 @@ private void testSerializeAndDeserializeFederation( bridgeConstants.getBtcParams() ); byte[] serializedTestFederation = BridgeSerializationUtils.serializeFederation(testFederation); - System.out.println(serializedTestFederation); Federation deserializedTestFederation = BridgeSerializationUtils.deserializeFederation( serializedTestFederation, @@ -1270,18 +1269,14 @@ private void testSerializeAndDeserializeFederation( activations ); byte[] serializedTestErpFederation = BridgeSerializationUtils.serializeFederation(testErpFederation); - System.out.println(serializedTestErpFederation); Federation deserializedTestErpFederation = BridgeSerializationUtils.deserializeLegacyErpFederation( serializedTestErpFederation, bridgeConstants, activations ); - System.out.println(deserializedTestErpFederation); Assertions.assertEquals(testFederation, deserializedTestFederation); - System.out.println(testFederation); - System.out.println(deserializedTestFederation); Assertions.assertEquals(testErpFederation, deserializedTestErpFederation); assertNotEquals(testFederation, deserializedTestErpFederation); assertNotEquals(testErpFederation, deserializedTestFederation); diff --git a/rskj-core/src/test/java/co/rsk/peg/BridgeStorageProviderFederationTests.java b/rskj-core/src/test/java/co/rsk/peg/BridgeStorageProviderFederationTests.java index 767ff666fcf..3c5b5ecc40d 100644 --- a/rskj-core/src/test/java/co/rsk/peg/BridgeStorageProviderFederationTests.java +++ b/rskj-core/src/test/java/co/rsk/peg/BridgeStorageProviderFederationTests.java @@ -24,8 +24,8 @@ class BridgeStorageProviderFederationTests { - private static final int FEDERATION_FORMAT_VERSION_MULTIKEY = 1000; - private static final int ERP_FEDERATION_FORMAT_VERSION = 2000; + private static final int STANDARD_MULTISIG_FEDERATION_FORMAT_VERSION_MULTIKEY = 1000; + private static final int LEGACY_ERP_FEDERATION_FORMAT_VERSION = 2000; private static final int P2SH_ERP_FEDERATION_FORMAT_VERSION = 3000; private final BridgeConstants bridgeConstantsRegtest = BridgeRegTestConstants.getInstance(); @@ -43,18 +43,18 @@ void getNewFederation_should_return_P2shErpFederation() { @Test void getNewFederation_should_return_erp_federation() { - Federation federation = createFederation(ERP_FEDERATION_FORMAT_VERSION); + Federation federation = createFederation(LEGACY_ERP_FEDERATION_FORMAT_VERSION); testGetNewFederation( - ERP_FEDERATION_FORMAT_VERSION, + LEGACY_ERP_FEDERATION_FORMAT_VERSION, federation ); } @Test void getNewFederation_should_return_legacy_federation() { - Federation federation = createFederation(FEDERATION_FORMAT_VERSION_MULTIKEY); + Federation federation = createFederation(STANDARD_MULTISIG_FEDERATION_FORMAT_VERSION_MULTIKEY); testGetNewFederation( - FEDERATION_FORMAT_VERSION_MULTIKEY, + STANDARD_MULTISIG_FEDERATION_FORMAT_VERSION_MULTIKEY, federation ); } @@ -62,12 +62,12 @@ void getNewFederation_should_return_legacy_federation() { @Test void getNewFederation_should_return_null() { testGetNewFederation( - FEDERATION_FORMAT_VERSION_MULTIKEY, + STANDARD_MULTISIG_FEDERATION_FORMAT_VERSION_MULTIKEY, null ); testGetNewFederation( - ERP_FEDERATION_FORMAT_VERSION, + LEGACY_ERP_FEDERATION_FORMAT_VERSION, null ); @@ -148,18 +148,18 @@ void getOldFederation_should_return_P2shErpFederation() { @Test void getOldFederation_should_return_erp_federation() { - Federation federation = createFederation(ERP_FEDERATION_FORMAT_VERSION); + Federation federation = createFederation(LEGACY_ERP_FEDERATION_FORMAT_VERSION); testGetOldFederation( - ERP_FEDERATION_FORMAT_VERSION, + LEGACY_ERP_FEDERATION_FORMAT_VERSION, federation ); } @Test void getOldFederation_should_return_legacy_fed() { - Federation federation = createFederation(FEDERATION_FORMAT_VERSION_MULTIKEY); + Federation federation = createFederation(STANDARD_MULTISIG_FEDERATION_FORMAT_VERSION_MULTIKEY); testGetOldFederation( - FEDERATION_FORMAT_VERSION_MULTIKEY, + STANDARD_MULTISIG_FEDERATION_FORMAT_VERSION_MULTIKEY, federation ); } @@ -167,12 +167,12 @@ void getOldFederation_should_return_legacy_fed() { @Test void getOldFederation_should_return_null() { testGetOldFederation( - FEDERATION_FORMAT_VERSION_MULTIKEY, + STANDARD_MULTISIG_FEDERATION_FORMAT_VERSION_MULTIKEY, null ); testGetOldFederation( - ERP_FEDERATION_FORMAT_VERSION, + LEGACY_ERP_FEDERATION_FORMAT_VERSION, null ); @@ -244,13 +244,13 @@ private void testGetOldFederation( @Test void saveNewFederation_before_RSKIP123_should_allow_to_save_any_fed_type() throws IOException { testSaveNewFederation( - FEDERATION_FORMAT_VERSION_MULTIKEY, - createFederation(FEDERATION_FORMAT_VERSION_MULTIKEY) + STANDARD_MULTISIG_FEDERATION_FORMAT_VERSION_MULTIKEY, + createFederation(STANDARD_MULTISIG_FEDERATION_FORMAT_VERSION_MULTIKEY) ); testSaveNewFederation( - ERP_FEDERATION_FORMAT_VERSION, - createFederation(ERP_FEDERATION_FORMAT_VERSION) + LEGACY_ERP_FEDERATION_FORMAT_VERSION, + createFederation(LEGACY_ERP_FEDERATION_FORMAT_VERSION) ); testSaveNewFederation( @@ -263,8 +263,8 @@ void saveNewFederation_before_RSKIP123_should_allow_to_save_any_fed_type() throw void saveNewFederation_after_RSKIP123_should_save_legacy_fed_format() throws IOException { activations = ActivationConfigsForTest.only(ConsensusRule.RSKIP123).forBlock(0); testSaveNewFederation( - FEDERATION_FORMAT_VERSION_MULTIKEY, - createFederation(FEDERATION_FORMAT_VERSION_MULTIKEY) + STANDARD_MULTISIG_FEDERATION_FORMAT_VERSION_MULTIKEY, + createFederation(STANDARD_MULTISIG_FEDERATION_FORMAT_VERSION_MULTIKEY) ); } @@ -275,8 +275,8 @@ void saveNewFederation_after_RSKIP201_should_save_legacy_fed_format() throws IOE ConsensusRule.RSKIP201 ).forBlock(0); testSaveNewFederation( - FEDERATION_FORMAT_VERSION_MULTIKEY, - createFederation(FEDERATION_FORMAT_VERSION_MULTIKEY) + STANDARD_MULTISIG_FEDERATION_FORMAT_VERSION_MULTIKEY, + createFederation(STANDARD_MULTISIG_FEDERATION_FORMAT_VERSION_MULTIKEY) ); } @@ -288,8 +288,8 @@ void saveNewFederation_after_RSKIP353_should_save_legacy_fed_format() throws IOE ConsensusRule.RSKIP353 ).forBlock(0); testSaveNewFederation( - FEDERATION_FORMAT_VERSION_MULTIKEY, - createFederation(FEDERATION_FORMAT_VERSION_MULTIKEY) + STANDARD_MULTISIG_FEDERATION_FORMAT_VERSION_MULTIKEY, + createFederation(STANDARD_MULTISIG_FEDERATION_FORMAT_VERSION_MULTIKEY) ); } @@ -300,8 +300,8 @@ void saveNewFederation_after_RSKIP201_should_save_erp_fed_format() throws IOExce ConsensusRule.RSKIP201 ).forBlock(0); testSaveNewFederation( - ERP_FEDERATION_FORMAT_VERSION, - createFederation(ERP_FEDERATION_FORMAT_VERSION) + LEGACY_ERP_FEDERATION_FORMAT_VERSION, + createFederation(LEGACY_ERP_FEDERATION_FORMAT_VERSION) ); } @@ -313,8 +313,8 @@ void saveNewFederation_after_RSKIP353_should_save_erp_fed_format() throws IOExce ConsensusRule.RSKIP353 ).forBlock(0); testSaveNewFederation( - ERP_FEDERATION_FORMAT_VERSION, - createFederation(ERP_FEDERATION_FORMAT_VERSION) + LEGACY_ERP_FEDERATION_FORMAT_VERSION, + createFederation(LEGACY_ERP_FEDERATION_FORMAT_VERSION) ); } @@ -417,13 +417,13 @@ private void testSaveNewFederation( void saveOldFederation_before_RSKIP123_should_allow_to_save_any_fed_type() throws IOException { activations = ActivationConfigsForTest.only().forBlock(0); testSaveOldFederation( - FEDERATION_FORMAT_VERSION_MULTIKEY, - createFederation(FEDERATION_FORMAT_VERSION_MULTIKEY) + STANDARD_MULTISIG_FEDERATION_FORMAT_VERSION_MULTIKEY, + createFederation(STANDARD_MULTISIG_FEDERATION_FORMAT_VERSION_MULTIKEY) ); testSaveOldFederation( - ERP_FEDERATION_FORMAT_VERSION, - createFederation(ERP_FEDERATION_FORMAT_VERSION) + LEGACY_ERP_FEDERATION_FORMAT_VERSION, + createFederation(LEGACY_ERP_FEDERATION_FORMAT_VERSION) ); testSaveOldFederation( @@ -436,8 +436,8 @@ void saveOldFederation_before_RSKIP123_should_allow_to_save_any_fed_type() throw void saveOldFederation_after_RSKIP123_should_save_legacy_fed_format() throws IOException { activations = ActivationConfigsForTest.only(ConsensusRule.RSKIP123).forBlock(0); testSaveOldFederation( - FEDERATION_FORMAT_VERSION_MULTIKEY, - createFederation(FEDERATION_FORMAT_VERSION_MULTIKEY) + STANDARD_MULTISIG_FEDERATION_FORMAT_VERSION_MULTIKEY, + createFederation(STANDARD_MULTISIG_FEDERATION_FORMAT_VERSION_MULTIKEY) ); } @@ -448,8 +448,8 @@ void saveOldFederation_after_RSKIP201_should_save_legacy_fed_format() throws IOE ConsensusRule.RSKIP201 ).forBlock(0); testSaveOldFederation( - FEDERATION_FORMAT_VERSION_MULTIKEY, - createFederation(FEDERATION_FORMAT_VERSION_MULTIKEY) + STANDARD_MULTISIG_FEDERATION_FORMAT_VERSION_MULTIKEY, + createFederation(STANDARD_MULTISIG_FEDERATION_FORMAT_VERSION_MULTIKEY) ); } @@ -461,8 +461,8 @@ void saveOldFederation_after_RSKIP353_should_save_legacy_fed_format() throws IOE ConsensusRule.RSKIP353 ).forBlock(0); testSaveOldFederation( - FEDERATION_FORMAT_VERSION_MULTIKEY, - createFederation(FEDERATION_FORMAT_VERSION_MULTIKEY) + STANDARD_MULTISIG_FEDERATION_FORMAT_VERSION_MULTIKEY, + createFederation(STANDARD_MULTISIG_FEDERATION_FORMAT_VERSION_MULTIKEY) ); } @@ -473,8 +473,8 @@ void saveOldFederation_after_RSKIP201_should_save_erp_fed_format() throws IOExce ConsensusRule.RSKIP201 ).forBlock(0); testSaveOldFederation( - ERP_FEDERATION_FORMAT_VERSION, - createFederation(ERP_FEDERATION_FORMAT_VERSION) + LEGACY_ERP_FEDERATION_FORMAT_VERSION, + createFederation(LEGACY_ERP_FEDERATION_FORMAT_VERSION) ); } @@ -486,8 +486,8 @@ void saveOldFederation_after_RSKIP353_should_save_erp_fed_format() throws IOExce ConsensusRule.RSKIP353 ).forBlock(0); testSaveOldFederation( - ERP_FEDERATION_FORMAT_VERSION, - createFederation(ERP_FEDERATION_FORMAT_VERSION) + LEGACY_ERP_FEDERATION_FORMAT_VERSION, + createFederation(LEGACY_ERP_FEDERATION_FORMAT_VERSION) ); } @@ -551,7 +551,7 @@ void saveOldFederation_after_RSKIP123_should_save_null() throws IOException { verify(repository, times(1)).addStorageBytes( PrecompiledContracts.BRIDGE_ADDR, BridgeStorageIndexKey.OLD_FEDERATION_FORMAT_VERSION.getKey(), - BridgeSerializationUtils.serializeInteger(FEDERATION_FORMAT_VERSION_MULTIKEY) + BridgeSerializationUtils.serializeInteger(STANDARD_MULTISIG_FEDERATION_FORMAT_VERSION_MULTIKEY) ); verify(repository, times(1)).addStorageBytes( PrecompiledContracts.BRIDGE_ADDR, @@ -613,7 +613,7 @@ private Federation createFederation(int version) { bridgeConstantsRegtest.getErpFedActivationDelay(), activations ); - case ERP_FEDERATION_FORMAT_VERSION: + case LEGACY_ERP_FEDERATION_FORMAT_VERSION: return new LegacyErpFederation( members, Instant.now(), diff --git a/rskj-core/src/test/java/co/rsk/peg/ErpFederationTest.java b/rskj-core/src/test/java/co/rsk/peg/ErpFederationTest.java index df1e2a9f910..3574dddc82a 100644 --- a/rskj-core/src/test/java/co/rsk/peg/ErpFederationTest.java +++ b/rskj-core/src/test/java/co/rsk/peg/ErpFederationTest.java @@ -406,36 +406,6 @@ void testEquals_differentNumberOfMembers() { Assertions.assertNotEquals(federation, otherFederation); } - @Test - void testEquals_differentCreationTime() { - Federation otherFederation = new LegacyErpFederation( - federation.getMembers(), - Instant.now(), - federation.getCreationBlockNumber(), - federation.getBtcParams(), - federation.getErpPubKeys(), - federation.getActivationDelay(), - activations - ); - - Assertions.assertNotEquals(federation, otherFederation); - } - - @Test - void testEquals_differentCreationBlockNumber() { - Federation otherFederation = new LegacyErpFederation( - federation.getMembers(), - federation.getCreationTime(), - federation.getCreationBlockNumber() + 1, - federation.getBtcParams(), - federation.getErpPubKeys(), - federation.getActivationDelay(), - activations - ); - - Assertions.assertNotEquals(federation, otherFederation); - } - @Test void testEquals_differentNetworkParameters() { Federation otherFederation = new LegacyErpFederation( From 26435bb48204e625484341273cb146bb9993a2c2 Mon Sep 17 00:00:00 2001 From: julia zack Date: Wed, 4 Oct 2023 11:21:58 -0300 Subject: [PATCH 19/27] Modify hashCode to be consistent with equals --- .../src/main/java/co/rsk/peg/ErpFederation.java | 15 --------------- .../src/main/java/co/rsk/peg/Federation.java | 10 ++++++++++ .../co/rsk/peg/StandardMultisigFederation.java | 13 ------------- 3 files changed, 10 insertions(+), 28 deletions(-) diff --git a/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java b/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java index 9341103dbd8..04135c61bfb 100644 --- a/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java +++ b/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java @@ -8,7 +8,6 @@ import java.time.Instant; import java.util.Collections; import java.util.List; -import java.util.Objects; import org.ethereum.config.blockchain.upgrades.ActivationConfig; public abstract class ErpFederation extends Federation { @@ -50,18 +49,4 @@ public Script getStandardP2SHScript() { return standardP2SHScript; } - - @Override - public int hashCode() { - // Can use java.util.Objects.hash since all of Instant, int and List have - // well-defined hashCode()s - return Objects.hash( - getCreationTime(), - this.creationBlockNumber, - getNumberOfSignaturesRequired(), - getBtcPublicKeys(), - getErpPubKeys(), - getActivationDelay() - ); - } } diff --git a/rskj-core/src/main/java/co/rsk/peg/Federation.java b/rskj-core/src/main/java/co/rsk/peg/Federation.java index 6746f075aae..ba18beb68cc 100644 --- a/rskj-core/src/main/java/co/rsk/peg/Federation.java +++ b/rskj-core/src/main/java/co/rsk/peg/Federation.java @@ -29,6 +29,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.Objects; import java.util.stream.Collectors; /** @@ -153,4 +154,13 @@ public boolean equals(Object other) { Federation otherFederation = (Federation) other; return this.getAddress().equals(otherFederation.getAddress()); } + + @Override + public int hashCode() { + // Can use java.util.Objects.hash since all of Instant, int and List have + // well-defined hashCode()s + return Objects.hash( + getAddress() + ); + } } diff --git a/rskj-core/src/main/java/co/rsk/peg/StandardMultisigFederation.java b/rskj-core/src/main/java/co/rsk/peg/StandardMultisigFederation.java index d5f799ecd88..913e1c491d9 100644 --- a/rskj-core/src/main/java/co/rsk/peg/StandardMultisigFederation.java +++ b/rskj-core/src/main/java/co/rsk/peg/StandardMultisigFederation.java @@ -24,7 +24,6 @@ import java.time.Instant; import java.util.List; -import java.util.Objects; /** * Immutable representation of an RSK Federation in the context of @@ -51,16 +50,4 @@ public Script getRedeemScript() { return redeemScript; } - - @Override - public int hashCode() { - // Can use java.util.Objects.hash since all of Instant, int and List have - // well-defined hashCode()s - return Objects.hash( - getCreationTime(), - this.creationBlockNumber, - getNumberOfSignaturesRequired(), - getBtcPublicKeys() - ); - } } From f6dce43cde5cd1bfe72d90bb93f7de15d678cbb7 Mon Sep 17 00:00:00 2001 From: julia zack Date: Wed, 4 Oct 2023 12:41:24 -0300 Subject: [PATCH 20/27] Fix sonar issues --- rskj-core/src/main/java/co/rsk/config/BridgeConstants.java | 1 - rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java | 2 -- 2 files changed, 3 deletions(-) diff --git a/rskj-core/src/main/java/co/rsk/config/BridgeConstants.java b/rskj-core/src/main/java/co/rsk/config/BridgeConstants.java index 4739c6eb6cd..cda9026acd5 100644 --- a/rskj-core/src/main/java/co/rsk/config/BridgeConstants.java +++ b/rskj-core/src/main/java/co/rsk/config/BridgeConstants.java @@ -25,7 +25,6 @@ import co.rsk.peg.Federation; import java.util.List; -import co.rsk.peg.StandardMultisigFederation; import org.ethereum.config.blockchain.upgrades.ActivationConfig; import org.ethereum.config.blockchain.upgrades.ConsensusRule; diff --git a/rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java b/rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java index 1bc570d50b6..43b2eee4c2d 100644 --- a/rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java +++ b/rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java @@ -37,9 +37,7 @@ public LegacyErpFederation( // Try getting the redeem script in order to validate it can be built // using the given public keys and csv value - getRedeemScript(); validateRedeemScript(); - getStandardRedeemScript(); } @Override From 0ed881c1bbe02595463d6013d365b6944fb0cc7c Mon Sep 17 00:00:00 2001 From: julia zack Date: Wed, 4 Oct 2023 15:18:12 -0300 Subject: [PATCH 21/27] Rollback change --- rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java b/rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java index 43b2eee4c2d..1bc570d50b6 100644 --- a/rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java +++ b/rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java @@ -37,7 +37,9 @@ public LegacyErpFederation( // Try getting the redeem script in order to validate it can be built // using the given public keys and csv value + getRedeemScript(); validateRedeemScript(); + getStandardRedeemScript(); } @Override From d7fdf56488af4861c969be00184107aacbd4226f Mon Sep 17 00:00:00 2001 From: julia zack Date: Thu, 5 Oct 2023 15:18:19 -0300 Subject: [PATCH 22/27] Fix and refactor tests. --- .../co/rsk/peg/BridgeSerializationUtils.java | 6 +- .../co/rsk/peg/BridgeStorageProvider.java | 28 ++++--- .../main/java/co/rsk/peg/ErpFederation.java | 23 ++++++ .../src/main/java/co/rsk/peg/Federation.java | 20 +++-- .../java/co/rsk/peg/LegacyErpFederation.java | 10 +-- .../rsk/peg/BridgeSerializationUtilsTest.java | 12 +-- .../co/rsk/peg/BridgeStorageProviderTest.java | 81 +++++++------------ ...Test.java => LegacyErpFederationTest.java} | 50 ++++++------ 8 files changed, 120 insertions(+), 110 deletions(-) rename rskj-core/src/test/java/co/rsk/peg/{ErpFederationTest.java => LegacyErpFederationTest.java} (98%) diff --git a/rskj-core/src/main/java/co/rsk/peg/BridgeSerializationUtils.java b/rskj-core/src/main/java/co/rsk/peg/BridgeSerializationUtils.java index a9e92243dae..db733975c4c 100644 --- a/rskj-core/src/main/java/co/rsk/peg/BridgeSerializationUtils.java +++ b/rskj-core/src/main/java/co/rsk/peg/BridgeSerializationUtils.java @@ -246,7 +246,7 @@ private static byte[] serializeFederationWithSerializer(Federation federation, F } // For the serialization format, see BridgeSerializationUtils::serializeFederationWithSerializer - private static Federation deserializeStandardMultisigFederationWithDeserializer( + private static StandardMultisigFederation deserializeStandardMultisigFederationWithDeserializer( byte[] data, NetworkParameters networkParameters, FederationMemberDesserializer federationMemberDesserializer) { @@ -287,7 +287,7 @@ public static byte[] serializeFederationOnlyBtcKeys(Federation federation) { } // For the serialization format, see BridgeSerializationUtils::serializeFederationOnlyBtcKeys - public static Federation deserializeFederationOnlyBtcKeys(byte[] data, NetworkParameters networkParameters) { + public static Federation deserializeStandardMultisigFederationOnlyBtcKeys(byte[] data, NetworkParameters networkParameters) { return deserializeStandardMultisigFederationWithDeserializer(data, networkParameters, (pubKeyBytes -> FederationMember.getFederationMemberFromKey(BtcECKey.fromPublicOnly(pubKeyBytes)))); } @@ -304,7 +304,7 @@ 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 ) { diff --git a/rskj-core/src/main/java/co/rsk/peg/BridgeStorageProvider.java b/rskj-core/src/main/java/co/rsk/peg/BridgeStorageProvider.java index 42532b4e317..d3db7f2eadc 100644 --- a/rskj-core/src/main/java/co/rsk/peg/BridgeStorageProvider.java +++ b/rskj-core/src/main/java/co/rsk/peg/BridgeStorageProvider.java @@ -48,9 +48,9 @@ * @author Oscar Guindzberg */ public class BridgeStorageProvider { - private static final int STANDARD_MULTISIG_FEDERATION_FORMAT_VERSION_MULTIKEY = 1000; - private static final int LEGACY_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; @@ -333,10 +333,10 @@ public Federation getNewFederation() { return null; } if (storageVersion.isPresent()) { - return deserializeFederationAccordingToVersion(data, storageVersion.get(), bridgeConstants); + return deserializeFederationAccordingToVersion(data, storageVersion.get(), bridgeConstants); // ACA ES EL ERROR } - return BridgeSerializationUtils.deserializeFederationOnlyBtcKeys(data, networkParameters); + return BridgeSerializationUtils.deserializeStandardMultisigFederationOnlyBtcKeys(data, networkParameters); } ); @@ -364,7 +364,7 @@ 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(), LEGACY_ERP_FEDERATION_FORMAT_VERSION @@ -372,7 +372,7 @@ public void saveNewFederation() { } else { saveStorageVersion( NEW_FEDERATION_FORMAT_VERSION.getKey(), - STANDARD_MULTISIG_FEDERATION_FORMAT_VERSION_MULTIKEY + STANDARD_MULTISIG_FEDERATION_FORMAT_VERSION ); } serializer = BridgeSerializationUtils::serializeFederation; @@ -398,7 +398,7 @@ public Federation getOldFederation() { return deserializeFederationAccordingToVersion(data, storageVersion.get(), bridgeConstants); } - return BridgeSerializationUtils.deserializeFederationOnlyBtcKeys(data, networkParameters); + return BridgeSerializationUtils.deserializeStandardMultisigFederationOnlyBtcKeys(data, networkParameters); } ); @@ -433,7 +433,7 @@ protected void saveOldFederation() { } else { saveStorageVersion( OLD_FEDERATION_FORMAT_VERSION.getKey(), - STANDARD_MULTISIG_FEDERATION_FORMAT_VERSION_MULTIKEY + STANDARD_MULTISIG_FEDERATION_FORMAT_VERSION ); } @@ -480,7 +480,7 @@ public void savePendingFederation() { RepositorySerializer serializer = BridgeSerializationUtils::serializePendingFederationOnlyBtcKeys; if (activations.isActive(RSKIP123)) { - saveStorageVersion(PENDING_FEDERATION_FORMAT_VERSION.getKey(), STANDARD_MULTISIG_FEDERATION_FORMAT_VERSION_MULTIKEY); + saveStorageVersion(PENDING_FEDERATION_FORMAT_VERSION.getKey(), STANDARD_MULTISIG_FEDERATION_FORMAT_VERSION); serializer = BridgeSerializationUtils::serializePendingFederation; } @@ -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); } } diff --git a/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java b/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java index 04135c61bfb..863ade73346 100644 --- a/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java +++ b/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java @@ -2,6 +2,8 @@ import co.rsk.bitcoinj.core.BtcECKey; import co.rsk.bitcoinj.core.NetworkParameters; +import co.rsk.bitcoinj.core.VerificationException; +import co.rsk.bitcoinj.script.ErpFederationRedeemScriptParser; import co.rsk.bitcoinj.script.Script; import co.rsk.bitcoinj.script.ScriptBuilder; import co.rsk.peg.utils.EcKeyUtils; @@ -30,6 +32,8 @@ protected ErpFederation( this.erpPubKeys = EcKeyUtils.getCompressedPubKeysList(erpPubKeys); this.activationDelay = activationDelay; this.activations = activations; + + validateErpFederationValues(); } public List getErpPubKeys() { @@ -49,4 +53,23 @@ public Script getStandardP2SHScript() { return standardP2SHScript; } + + private void validateErpFederationValues() { + if (erpPubKeys == null) { + String message = String.format( + "Provided erpPubKeys is empty" + ); + throw new VerificationException(message); + } + + long max_csv_value = ErpFederationRedeemScriptParser.MAX_CSV_VALUE; + if (activationDelay <= 0 || activationDelay > max_csv_value) { + String message = String.format( + "Provided csv value %d must be larger than 0 and lower than %d", + activationDelay, + max_csv_value + ); + throw new VerificationException(message); + } + } } diff --git a/rskj-core/src/main/java/co/rsk/peg/Federation.java b/rskj-core/src/main/java/co/rsk/peg/Federation.java index ba18beb68cc..e2b86133fb2 100644 --- a/rskj-core/src/main/java/co/rsk/peg/Federation.java +++ b/rskj-core/src/main/java/co/rsk/peg/Federation.java @@ -21,6 +21,7 @@ import co.rsk.bitcoinj.core.Address; import co.rsk.bitcoinj.core.BtcECKey; import co.rsk.bitcoinj.core.NetworkParameters; +import co.rsk.bitcoinj.core.VerificationException; import co.rsk.bitcoinj.script.Script; import co.rsk.bitcoinj.script.ScriptBuilder; @@ -29,7 +30,6 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; -import java.util.Objects; import java.util.stream.Collectors; /** @@ -57,6 +57,8 @@ protected Federation(List members, Instant creationTime, long this.creationTime = creationTime.truncatedTo(ChronoUnit.MILLIS); this.creationBlockNumber = creationBlockNumber; this.btcParams = btcParams; + + //validateFederationValues(); } public List getMembers() { @@ -157,10 +159,16 @@ public boolean equals(Object other) { @Override public int hashCode() { - // Can use java.util.Objects.hash since all of Instant, int and List have - // well-defined hashCode()s - return Objects.hash( - getAddress() - ); + return getAddress().hashCode(); + } + + private void validateFederationValues() { + if (creationBlockNumber <= 0) { + String message = String.format( + "Provided creation block number %d must be larger than 0", + creationBlockNumber + ); + throw new VerificationException(message); + } } } diff --git a/rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java b/rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java index 1bc570d50b6..c326c5d729b 100644 --- a/rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java +++ b/rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java @@ -22,7 +22,7 @@ @Deprecated public class LegacyErpFederation extends ErpFederation { private static final Logger logger = LoggerFactory.getLogger(LegacyErpFederation.class); - protected static final byte[] ERP_TESTNET_REDEEM_SCRIPT_BYTES = Hex.decode("6453210208f40073a9e43b3e9103acec79767a6de9b0409749884e989960fee578012fce210225e892391625854128c5c4ea4340de0c2a70570f33db53426fc9c746597a03f42102afc230c2d355b1a577682b07bc2646041b5d0177af0f98395a46018da699b6da210344a3c38cd59afcba3edcebe143e025574594b001700dec41e59409bdbd0f2a0921039a060badbeb24bee49eb2063f616c0f0f0765d4ca646b20a88ce828f259fcdb955670300cd50b27552210216c23b2ea8e4f11c3f9e22711addb1d16a93964796913830856b568cc3ea21d3210275562901dd8faae20de0a4166362a4f82188db77dbed4ca887422ea1ec185f1421034db69f2112f4fb1bb6141bf6e2bd6631f0484d0bd95b16767902c9fe219d4a6f5368ae"); + private static final byte[] ERP_TESTNET_REDEEM_SCRIPT_BYTES = Hex.decode("6453210208f40073a9e43b3e9103acec79767a6de9b0409749884e989960fee578012fce210225e892391625854128c5c4ea4340de0c2a70570f33db53426fc9c746597a03f42102afc230c2d355b1a577682b07bc2646041b5d0177af0f98395a46018da699b6da210344a3c38cd59afcba3edcebe143e025574594b001700dec41e59409bdbd0f2a0921039a060badbeb24bee49eb2063f616c0f0f0765d4ca646b20a88ce828f259fcdb955670300cd50b27552210216c23b2ea8e4f11c3f9e22711addb1d16a93964796913830856b568cc3ea21d3210275562901dd8faae20de0a4166362a4f82188db77dbed4ca887422ea1ec185f1421034db69f2112f4fb1bb6141bf6e2bd6631f0484d0bd95b16767902c9fe219d4a6f5368ae"); public LegacyErpFederation( List members, @@ -35,11 +35,7 @@ public LegacyErpFederation( super(members, creationTime, creationBlockNumber, btcParams, erpPubKeys, activationDelay, activations); - // Try getting the redeem script in order to validate it can be built - // using the given public keys and csv value - getRedeemScript(); validateRedeemScript(); - getStandardRedeemScript(); } @Override @@ -71,8 +67,6 @@ public Script getRedeemScript() { @Override public Script getStandardRedeemScript() { if (standardRedeemScript == null) { - // i think is not quite right to just "fix" the standardRedeemScript in the buggy fed. - // the real case is that the OP_CHECKMULTISIG is not before the OP_ELSE... as the method adds. standardRedeemScript = ErpFederationRedeemScriptParser.extractStandardRedeemScript( getRedeemScript().getChunks() ); @@ -82,7 +76,7 @@ public Script getStandardRedeemScript() { private void validateRedeemScript() { if (activations.isActive(ConsensusRule.RSKIP293) && - this.redeemScript.equals(new Script(ERP_TESTNET_REDEEM_SCRIPT_BYTES))) { + this.getRedeemScript().equals(new Script(ERP_TESTNET_REDEEM_SCRIPT_BYTES))) { String message = "Unable to create ERP Federation. The obtained redeem script matches the one hardcoded for testnet. " + "This would cause bitcoinj-thin to identify it as invalid"; diff --git a/rskj-core/src/test/java/co/rsk/peg/BridgeSerializationUtilsTest.java b/rskj-core/src/test/java/co/rsk/peg/BridgeSerializationUtilsTest.java index 21582e47d3f..35190f974fc 100644 --- a/rskj-core/src/test/java/co/rsk/peg/BridgeSerializationUtilsTest.java +++ b/rskj-core/src/test/java/co/rsk/peg/BridgeSerializationUtilsTest.java @@ -202,7 +202,7 @@ void deserializeFederationOnlyBtcKeys_ok() throws Exception { byte[] data = RLP.encodeList(rlpFirstElement, rlpSecondElement, rlpKeyList); - Federation deserializedFederation = BridgeSerializationUtils.deserializeFederationOnlyBtcKeys(data, NetworkParameters.fromID(NetworkParameters.ID_REGTEST)); + Federation deserializedFederation = BridgeSerializationUtils.deserializeStandardMultisigFederationOnlyBtcKeys(data, NetworkParameters.fromID(NetworkParameters.ID_REGTEST)); Assertions.assertEquals(5000, deserializedFederation.getCreationTime().toEpochMilli()); Assertions.assertEquals(4, deserializedFederation.getNumberOfSignaturesRequired()); @@ -228,7 +228,7 @@ void deserializeFederationOnlyBtcKeys_wrongListSize() throws Exception { boolean thrown = false; try { - BridgeSerializationUtils.deserializeFederationOnlyBtcKeys(data, NetworkParameters.fromID(NetworkParameters.ID_REGTEST)); + BridgeSerializationUtils.deserializeStandardMultisigFederationOnlyBtcKeys(data, NetworkParameters.fromID(NetworkParameters.ID_REGTEST)); } catch (Exception e) { Assertions.assertTrue(e.getMessage().contains("Expected 3 elements")); thrown = true; @@ -330,7 +330,7 @@ void serializeFederation_serializedKeysAreCompressedAndThree() { void deserializeFederation_wrongListSize() { byte[] serialized = RLP.encodeList(RLP.encodeElement(new byte[0]), RLP.encodeElement(new byte[0])); NetworkParameters networkParameters = NetworkParameters.fromID(NetworkParameters.ID_REGTEST); - Exception ex = Assertions.assertThrows(RuntimeException.class, () -> BridgeSerializationUtils.deserializeFederation(serialized, networkParameters)); + Exception ex = Assertions.assertThrows(RuntimeException.class, () -> BridgeSerializationUtils.deserializeStandardMultisigFederation(serialized, networkParameters)); Assertions.assertTrue(ex.getMessage().contains("Invalid serialized Federation")); } @@ -344,7 +344,7 @@ void deserializeFederation_invalidFederationMember() { NetworkParameters networkParameters = NetworkParameters.fromID(NetworkParameters.ID_REGTEST); - Exception ex = Assertions.assertThrows(RuntimeException.class, () -> BridgeSerializationUtils.deserializeFederation(serialized, networkParameters)); + Exception ex = Assertions.assertThrows(RuntimeException.class, () -> BridgeSerializationUtils.deserializeStandardMultisigFederation(serialized, networkParameters)); Assertions.assertTrue(ex.getMessage().contains("Invalid serialized FederationMember")); } @@ -802,7 +802,7 @@ void serializeAndDeserializeFederationOnlyBtcKeysWithRealRLP() { ); byte[] result = BridgeSerializationUtils.serializeFederationOnlyBtcKeys(federation); - Federation deserializedFederation = BridgeSerializationUtils.deserializeFederationOnlyBtcKeys(result, networkParms); + Federation deserializedFederation = BridgeSerializationUtils.deserializeStandardMultisigFederationOnlyBtcKeys(result, networkParms); MatcherAssert.assertThat(federation, is(deserializedFederation)); } @@ -1254,7 +1254,7 @@ private void testSerializeAndDeserializeFederation( ); byte[] serializedTestFederation = BridgeSerializationUtils.serializeFederation(testFederation); - Federation deserializedTestFederation = BridgeSerializationUtils.deserializeFederation( + Federation deserializedTestFederation = BridgeSerializationUtils.deserializeStandardMultisigFederation( serializedTestFederation, bridgeConstants.getBtcParams() ); diff --git a/rskj-core/src/test/java/co/rsk/peg/BridgeStorageProviderTest.java b/rskj-core/src/test/java/co/rsk/peg/BridgeStorageProviderTest.java index 8d7c0c79d4b..8e8fd5a9301 100644 --- a/rskj-core/src/test/java/co/rsk/peg/BridgeStorageProviderTest.java +++ b/rskj-core/src/test/java/co/rsk/peg/BridgeStorageProviderTest.java @@ -335,7 +335,7 @@ void getNewFederation_initialVersion() { }); try (MockedStatic bridgeSerializationUtilsMocked = mockStatic(BridgeSerializationUtils.class)) { - bridgeSerializationUtilsMocked.when(() -> BridgeSerializationUtils.deserializeFederationOnlyBtcKeys(any(byte[].class), any(NetworkParameters.class))).then((InvocationOnMock invocation) -> { + bridgeSerializationUtilsMocked.when(() -> BridgeSerializationUtils.deserializeStandardMultisigFederationOnlyBtcKeys(any(byte[].class), any(NetworkParameters.class))).then((InvocationOnMock invocation) -> { deserializeCalls.add(0); byte[] data = invocation.getArgument(0); NetworkParameters networkParameters = invocation.getArgument(1); @@ -388,8 +388,8 @@ void getNewFederation_initialVersion_nullBytes() { assertNull(storageProvider.getNewFederation()); Assertions.assertEquals(3, storageCalls.size()); - bridgeSerializationUtilsMocked.verify(() -> BridgeSerializationUtils.deserializeFederationOnlyBtcKeys(any(byte[].class), any(NetworkParameters.class)), never()); - bridgeSerializationUtilsMocked.verify(() -> BridgeSerializationUtils.deserializeFederation(any(byte[].class), any(NetworkParameters.class)), never()); + bridgeSerializationUtilsMocked.verify(() -> BridgeSerializationUtils.deserializeStandardMultisigFederationOnlyBtcKeys(any(byte[].class), any(NetworkParameters.class)), never()); + bridgeSerializationUtilsMocked.verify(() -> BridgeSerializationUtils.deserializeStandardMultisigFederation(any(byte[].class), any(NetworkParameters.class)), never()); } } @@ -469,12 +469,12 @@ void getNewFederation_multiKeyVersion_nullBytes() { assertNull(storageProvider.getNewFederation()); assertEquals(3, storageCalls.size()); - bridgeSerializationUtilsMocked.verify(() -> BridgeSerializationUtils.deserializeFederationOnlyBtcKeys( + bridgeSerializationUtilsMocked.verify(() -> BridgeSerializationUtils.deserializeStandardMultisigFederationOnlyBtcKeys( any(byte[].class), any(NetworkParameters.class)), never() ); - bridgeSerializationUtilsMocked.verify(() -> BridgeSerializationUtils.deserializeFederation( + bridgeSerializationUtilsMocked.verify(() -> BridgeSerializationUtils.deserializeStandardMultisigFederation( any(byte[].class), any(NetworkParameters.class)), never() @@ -614,7 +614,7 @@ void getOldFederation_initialVersion() { }); try (MockedStatic bridgeSerializationUtilsMocked = mockStatic(BridgeSerializationUtils.class)) { - bridgeSerializationUtilsMocked.when(() -> BridgeSerializationUtils.deserializeFederationOnlyBtcKeys(any(byte[].class), any(NetworkParameters.class))).then((InvocationOnMock invocation) -> { + bridgeSerializationUtilsMocked.when(() -> BridgeSerializationUtils.deserializeStandardMultisigFederationOnlyBtcKeys(any(byte[].class), any(NetworkParameters.class))).then((InvocationOnMock invocation) -> { deserializeCalls.add(0); byte[] data = invocation.getArgument(0); NetworkParameters networkParameters = invocation.getArgument(1); @@ -665,8 +665,8 @@ void getOldFederation_initialVersion_nullBytes() { assertNull(storageProvider.getOldFederation()); Assertions.assertEquals(3, storageCalls.size()); - bridgeSerializationUtilsMocked.verify(() -> BridgeSerializationUtils.deserializeFederationOnlyBtcKeys(any(byte[].class), any(NetworkParameters.class)), never()); - bridgeSerializationUtilsMocked.verify(() -> BridgeSerializationUtils.deserializeFederation(any(byte[].class), any(NetworkParameters.class)), never()); + bridgeSerializationUtilsMocked.verify(() -> BridgeSerializationUtils.deserializeStandardMultisigFederationOnlyBtcKeys(any(byte[].class), any(NetworkParameters.class)), never()); + bridgeSerializationUtilsMocked.verify(() -> BridgeSerializationUtils.deserializeStandardMultisigFederation(any(byte[].class), any(NetworkParameters.class)), never()); } } @@ -745,12 +745,12 @@ void getOldFederation_multiKeyVersion_nullBytes() { assertNull(storageProvider.getOldFederation()); assertEquals(3, storageCalls.size()); - bridgeSerializationUtilsMocked.verify(() -> BridgeSerializationUtils.deserializeFederationOnlyBtcKeys( + bridgeSerializationUtilsMocked.verify(() -> BridgeSerializationUtils.deserializeStandardMultisigFederationOnlyBtcKeys( any(byte[].class), any(NetworkParameters.class)), never() ); - bridgeSerializationUtilsMocked.verify(() -> BridgeSerializationUtils.deserializeFederation( + bridgeSerializationUtilsMocked.verify(() -> BridgeSerializationUtils.deserializeStandardMultisigFederation( any(byte[].class), any(NetworkParameters.class)), never() @@ -3564,7 +3564,6 @@ void getReleaseRequestQueueSize_when_releaseRequestQueue_is_not_null() throws IO private void testGetOldFederation(Federation oldFederation) { BridgeConstants bridgeConstants = config.getNetworkConstants().getBridgeConstants(); List storageCalls = new ArrayList<>(); - List deserializeCalls = new ArrayList<>(); Repository repositoryMock = mock(Repository.class); BridgeStorageProvider storageProvider = new BridgeStorageProvider( repositoryMock, @@ -3583,34 +3582,18 @@ private void testGetOldFederation(Federation oldFederation) { if (storageCalls.size() == 1) { // First call is storage version getter Assertions.assertEquals(OLD_FEDERATION_FORMAT_VERSION.getKey(), address); - return RLP.encodeBigInteger(BigInteger.valueOf(1234)); + int federationVersion = getFederationVersion(oldFederation); + return RLP.encodeBigInteger(BigInteger.valueOf(federationVersion)); } else { // Second call is the actual storage getter Assertions.assertEquals(2, storageCalls.size()); Assertions.assertEquals(OLD_FEDERATION_KEY.getKey(), address); - return new byte[]{(byte) 0xaa}; + return BridgeSerializationUtils.serializeFederation(oldFederation); } }); - try (MockedStatic bridgeSerializationUtilsMocked = mockStatic(BridgeSerializationUtils.class)) { - bridgeSerializationUtilsMocked.when(() -> BridgeSerializationUtils.deserializeFederation( - any(byte[].class), - any(NetworkParameters.class) - )).then((InvocationOnMock invocation) -> { - deserializeCalls.add(0); - byte[] data = invocation.getArgument(0); - NetworkParameters networkParameters = invocation.getArgument(1); - // Make sure we're deserializing what just came from the repo with the correct BTC context - assertArrayEquals(new byte[]{(byte) 0xaa}, data); - assertEquals(networkParameters, bridgeConstants.getBtcParams()); - Assertions.assertEquals(networkParameters, bridgeConstants.getBtcParams()); - return oldFederation; - }); - assertEquals(oldFederation, storageProvider.getOldFederation()); assertEquals(2, storageCalls.size()); - assertEquals(1, deserializeCalls.size()); - } } private void testSaveOldFederation(Federation oldFederation, int version, ActivationConfig.ForBlock activations) { @@ -3668,7 +3651,6 @@ private void testSaveOldFederation(Federation oldFederation, int version, Activa private void testGetNewFederationPostMultiKey(Federation federation) { List storageCalls = new ArrayList<>(); - List deserializeCalls = new ArrayList<>(); Repository repositoryMock = mock(Repository.class); BridgeConstants bridgeConstants = config.getNetworkConstants().getBridgeConstants(); BridgeStorageProvider storageProvider = new BridgeStorageProvider( @@ -3691,33 +3673,18 @@ private void testGetNewFederationPostMultiKey(Federation federation) { if (storageCalls.size() == 1) { // First call is storage version getter Assertions.assertEquals(NEW_FEDERATION_FORMAT_VERSION.getKey(), address); - return RLP.encodeBigInteger(BigInteger.valueOf(1234)); + int federationVersion = getFederationVersion(federation); + return RLP.encodeBigInteger(BigInteger.valueOf(federationVersion)); } else { // Second call is the actual storage getter Assertions.assertEquals(2, storageCalls.size()); Assertions.assertEquals(NEW_FEDERATION_KEY.getKey(), address); - return new byte[]{(byte) 0xaa}; + return BridgeSerializationUtils.serializeFederation(federation); } }); - try (MockedStatic bridgeSerializationUtilsMocked = mockStatic(BridgeSerializationUtils.class)) { - bridgeSerializationUtilsMocked.when(() -> BridgeSerializationUtils.deserializeFederation( - any(byte[].class), - any(NetworkParameters.class)) - ).then((InvocationOnMock invocation) -> { - deserializeCalls.add(0); - byte[] data = invocation.getArgument(0); - NetworkParameters networkParameters = invocation.getArgument(1); - // Make sure we're deserializing what just came from the repo with the correct BTC context - assertArrayEquals(new byte[]{(byte) 0xaa}, data); - assertEquals(networkParameters, bridgeConstants.getBtcParams()); - return federation; - }); - assertEquals(federation, storageProvider.getNewFederation()); assertEquals(2, storageCalls.size()); - assertEquals(1, deserializeCalls.size()); - } } private void testSaveNewFederationPostMultiKey(Federation newFederation, int version, ActivationConfig.ForBlock activations) { @@ -3915,7 +3882,8 @@ private Federation buildMockFederation(Integer... pks) { return new StandardMultisigFederation( FederationTestUtils.getFederationMembersFromPks(pks), Instant.ofEpochMilli(1000), - 0L, NetworkParameters.fromID(NetworkParameters.ID_REGTEST) + 1, + NetworkParameters.fromID(NetworkParameters.ID_REGTEST) ); } @@ -3932,4 +3900,17 @@ private static Repository createRepository() { TrieStore trieStore = new TrieStoreImpl(new HashMapDB()); return new MutableRepository(new MutableTrieCache(new MutableTrieImpl(trieStore, new Trie(trieStore)))); } + + private int getFederationVersion(Federation federation) { + if (federation instanceof StandardMultisigFederation) { + return BridgeStorageProvider.STANDARD_MULTISIG_FEDERATION_FORMAT_VERSION; + } + if (federation instanceof LegacyErpFederation) { + return BridgeStorageProvider.LEGACY_ERP_FEDERATION_FORMAT_VERSION; + } + if (federation instanceof P2shErpFederation) { + return BridgeStorageProvider.P2SH_ERP_FEDERATION_FORMAT_VERSION; + } + throw new IllegalArgumentException("Unknown Federation type: " + federation.getClass()); + } } diff --git a/rskj-core/src/test/java/co/rsk/peg/ErpFederationTest.java b/rskj-core/src/test/java/co/rsk/peg/LegacyErpFederationTest.java similarity index 98% rename from rskj-core/src/test/java/co/rsk/peg/ErpFederationTest.java rename to rskj-core/src/test/java/co/rsk/peg/LegacyErpFederationTest.java index 3574dddc82a..65be368df1f 100644 --- a/rskj-core/src/test/java/co/rsk/peg/ErpFederationTest.java +++ b/rskj-core/src/test/java/co/rsk/peg/LegacyErpFederationTest.java @@ -43,8 +43,8 @@ import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; -class ErpFederationTest { - private ErpFederation federation; +class LegacyErpFederationTest { + private LegacyErpFederation federation; private List defaultKeys; private List emergencyKeys; private long activationDelayValue; @@ -61,7 +61,7 @@ void setup() { activations = mock(ActivationConfig.ForBlock.class); when(activations.isActive(ConsensusRule.RSKIP284)).thenReturn(true); - federation = createDefaultErpFederation(); + federation = createDefaultLegacyErpFederation(); } @Test @@ -87,7 +87,7 @@ void getRedeemScript_before_RSKIP293() { @Test void getRedeemScript_after_RSKIP293() { when(activations.isActive(ConsensusRule.RSKIP293)).thenReturn(true); - federation = createDefaultErpFederation(); + federation = createDefaultLegacyErpFederation(); Script redeemScript = federation.getRedeemScript(); validateErpRedeemScript( @@ -102,7 +102,7 @@ void getRedeemScript_changes_after_RSKIP293() { Script preRskip293RedeemScript = federation.getRedeemScript(); when(activations.isActive(ConsensusRule.RSKIP293)).thenReturn(true); - federation = createDefaultErpFederation(); + federation = createDefaultLegacyErpFederation(); Script postRskip293RedeemScript = federation.getRedeemScript(); Assertions.assertNotEquals(preRskip293RedeemScript, postRskip293RedeemScript); @@ -146,7 +146,7 @@ void getErpPubKeys_uncompressed_public_keys() { ErpFederation federationWithUncompressedKeys = new LegacyErpFederation( FederationTestUtils.getFederationMembersWithBtcKeys(defaultKeys), ZonedDateTime.parse("2017-06-10T02:30:00Z").toInstant(), - 0L, + 1, NetworkParameters.fromID(NetworkParameters.ID_REGTEST), uncompressedErpKeys, activationDelayValue, @@ -173,7 +173,7 @@ void getErpRedeemScript_compareOtherImplementation() throws IOException { Federation erpFederation = new LegacyErpFederation( FederationTestUtils.getFederationMembersWithBtcKeys(generatedScript.mainFed), ZonedDateTime.parse("2017-06-10T02:30:00Z").toInstant(), - 0L, + 1, NetworkParameters.fromID(NetworkParameters.ID_TESTNET), generatedScript.emergencyFed, generatedScript.timelock, @@ -205,7 +205,7 @@ void getErpRedeemScript_compareOtherImplementation_P2SHERPFederation() throws IO Federation erpFederation = new P2shErpFederation( FederationTestUtils.getFederationMembersWithBtcKeys(generatedScript.mainFed), ZonedDateTime.parse("2017-06-10T02:30:00Z").toInstant(), - 0L, + 1, NetworkParameters.fromID(NetworkParameters.ID_TESTNET), generatedScript.emergencyFed, generatedScript.timelock, @@ -246,7 +246,7 @@ void createInvalidErpFederation_negativeCsvValue() { assertThrows(VerificationException.class, () -> new LegacyErpFederation( federationMembersFromPks, creationTime, - 0L, + 1, btcParams, emergencyKeys, -100L, @@ -262,7 +262,7 @@ void createInvalidErpFederation_csvValueZero() { assertThrows(VerificationException.class, () -> new LegacyErpFederation( federationMembersFromPks, creationTime, - 0L, + 1, btcParams, emergencyKeys, 0, @@ -278,7 +278,7 @@ void createInvalidErpFederation_csvValueAboveMax() { assertThrows(VerificationException.class, () -> new LegacyErpFederation( federationMembersFromPks, creationTime, - 0L, + 1, btcParams, emergencyKeys, ErpFederationRedeemScriptParser.MAX_CSV_VALUE + 1, @@ -294,7 +294,7 @@ void getRedeemScript_before_RSKIP_284_testnet() { Federation erpFederation = new LegacyErpFederation( FederationTestUtils.getFederationMembersFromPks(100, 200, 300), ZonedDateTime.parse("2017-06-10T02:30:00Z").toInstant(), - 0L, + 1, NetworkParameters.fromID(NetworkParameters.ID_TESTNET), emergencyKeys, activationDelayValue, @@ -312,7 +312,7 @@ void getRedeemScript_before_RSKIP_284_mainnet() { Federation erpFederation = new LegacyErpFederation( FederationTestUtils.getFederationMembersWithBtcKeys(defaultKeys), ZonedDateTime.parse("2017-06-10T02:30:00Z").toInstant(), - 0L, + 1, NetworkParameters.fromID(NetworkParameters.ID_MAINNET), emergencyKeys, activationDelayValue, @@ -332,7 +332,7 @@ void getRedeemScript_after_RSKIP_284_testnet() { Federation erpFederation = new LegacyErpFederation( FederationTestUtils.getFederationMembersWithBtcKeys(defaultKeys), ZonedDateTime.parse("2017-06-10T02:30:00Z").toInstant(), - 0L, + 1, NetworkParameters.fromID(NetworkParameters.ID_TESTNET), emergencyKeys, activationDelayValue, @@ -352,7 +352,7 @@ void getRedeemScript_after_RSKIP_284_mainnet() { Federation erpFederation = new LegacyErpFederation( FederationTestUtils.getFederationMembersWithBtcKeys(defaultKeys), ZonedDateTime.parse("2017-06-10T02:30:00Z").toInstant(), - 0L, + 1, NetworkParameters.fromID(NetworkParameters.ID_MAINNET), emergencyKeys, activationDelayValue, @@ -448,7 +448,7 @@ void testEquals_differentRedeemScript() { Federation erpFederation = new LegacyErpFederation( FederationTestUtils.getFederationMembersFromPks(100, 200, 300), ZonedDateTime.parse("2017-06-10T02:30:00Z").toInstant(), - 0L, + 1, NetworkParameters.fromID(NetworkParameters.ID_TESTNET), emergencyKeys, activationDelayValue, @@ -458,7 +458,7 @@ void testEquals_differentRedeemScript() { Federation otherErpFederation = new LegacyErpFederation( FederationTestUtils.getFederationMembersFromPks(100, 200, 300), ZonedDateTime.parse("2017-06-10T02:30:00Z").toInstant(), - 0L, + 1, NetworkParameters.fromID(NetworkParameters.ID_TESTNET), emergencyKeys, activationDelayValue, @@ -471,7 +471,7 @@ void testEquals_differentRedeemScript() { otherErpFederation = new LegacyErpFederation( FederationTestUtils.getFederationMembersFromPks(100, 200, 300), ZonedDateTime.parse("2017-06-10T02:30:00Z").toInstant(), - 0L, + 1, NetworkParameters.fromID(NetworkParameters.ID_TESTNET), emergencyKeys, activationDelayValue, @@ -484,7 +484,7 @@ void testEquals_differentRedeemScript() { erpFederation = new LegacyErpFederation( FederationTestUtils.getFederationMembersFromPks(100, 200, 300), ZonedDateTime.parse("2017-06-10T02:30:00Z").toInstant(), - 0L, + 1, NetworkParameters.fromID(NetworkParameters.ID_TESTNET), emergencyKeys, activationDelayValue, @@ -524,7 +524,7 @@ void createErpFedWithSameRedeemScriptAsHardcodedOne_after_RSKIP293_fails() { assertThrows(FederationCreationException.class, () -> new LegacyErpFederation( federationMembersWithBtcKeys, creationTime, - 0L, + 1, btcParams, emergencyMultisigKeys, activationDelay, @@ -677,7 +677,7 @@ private void createErpFederation(BridgeConstants constants, boolean isRskip293Ac Federation erpFederation = new LegacyErpFederation( FederationTestUtils.getFederationMembersWithBtcKeys(defaultKeys), ZonedDateTime.parse("2017-06-10T02:30:00Z").toInstant(), - 0L, + 1, constants.getBtcParams(), constants.getErpFedPubKeysList(), constants.getErpFedActivationDelay(), @@ -717,7 +717,7 @@ private void spendFromErpFed( ErpFederation erpFed = new LegacyErpFederation( FederationMember.getFederationMembersFromKeys(standardKeys), ZonedDateTime.parse("2017-06-10T02:30:00Z").toInstant(), - 0L, + 1, networkParameters, emergencyKeys, activationDelay, @@ -746,11 +746,11 @@ private void spendFromErpFed( ); } - private LegacyErpFederation createDefaultErpFederation() { + private LegacyErpFederation createDefaultLegacyErpFederation() { return new LegacyErpFederation( FederationTestUtils.getFederationMembersWithBtcKeys(defaultKeys), ZonedDateTime.parse("2017-06-10T02:30:00Z").toInstant(), - 0L, + 1, NetworkParameters.fromID(NetworkParameters.ID_REGTEST), emergencyKeys, activationDelayValue, @@ -797,7 +797,7 @@ private void validateErpRedeemScript( assertEquals(ScriptOpCodes.OP_NOTIF, script[index++]); // Next byte should equal M, from an M/N multisig - int m = defaultMultisigKeys.size() / 2 + 1; // da 8 + int m = defaultMultisigKeys.size() / 2 + 1; assertEquals(ScriptOpCodes.getOpCode(String.valueOf(m)), script[index++]); // Assert public keys From def70701ad0c126743ba97e535988710b22bb724 Mon Sep 17 00:00:00 2001 From: julia zack Date: Thu, 5 Oct 2023 15:20:53 -0300 Subject: [PATCH 23/27] Comment unused-for-now code. Remove unnecessary string formatting --- rskj-core/src/main/java/co/rsk/peg/ErpFederation.java | 4 +--- rskj-core/src/main/java/co/rsk/peg/Federation.java | 4 ++-- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java b/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java index 863ade73346..667a7c4cc08 100644 --- a/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java +++ b/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java @@ -56,9 +56,7 @@ public Script getStandardP2SHScript() { private void validateErpFederationValues() { if (erpPubKeys == null) { - String message = String.format( - "Provided erpPubKeys is empty" - ); + String message = "Provided erpPubKeys is empty"; throw new VerificationException(message); } diff --git a/rskj-core/src/main/java/co/rsk/peg/Federation.java b/rskj-core/src/main/java/co/rsk/peg/Federation.java index e2b86133fb2..04aae335101 100644 --- a/rskj-core/src/main/java/co/rsk/peg/Federation.java +++ b/rskj-core/src/main/java/co/rsk/peg/Federation.java @@ -162,7 +162,7 @@ public int hashCode() { return getAddress().hashCode(); } - private void validateFederationValues() { +/* private void validateFederationValues() { if (creationBlockNumber <= 0) { String message = String.format( "Provided creation block number %d must be larger than 0", @@ -170,5 +170,5 @@ private void validateFederationValues() { ); throw new VerificationException(message); } - } + }*/ } From a6ef2f76f11855f1aaabec797549c194b9de12d8 Mon Sep 17 00:00:00 2001 From: julia zack Date: Thu, 5 Oct 2023 22:12:50 -0300 Subject: [PATCH 24/27] Fix code smells --- rskj-core/src/main/java/co/rsk/peg/ErpFederation.java | 6 +++--- rskj-core/src/main/java/co/rsk/peg/Federation.java | 7 ++++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java b/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java index 667a7c4cc08..a7aeaa43370 100644 --- a/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java +++ b/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java @@ -60,12 +60,12 @@ private void validateErpFederationValues() { throw new VerificationException(message); } - long max_csv_value = ErpFederationRedeemScriptParser.MAX_CSV_VALUE; - if (activationDelay <= 0 || activationDelay > max_csv_value) { + long maxCsvValue = ErpFederationRedeemScriptParser.MAX_CSV_VALUE; + if (activationDelay <= 0 || activationDelay > maxCsvValue) { String message = String.format( "Provided csv value %d must be larger than 0 and lower than %d", activationDelay, - max_csv_value + maxCsvValue ); throw new VerificationException(message); } diff --git a/rskj-core/src/main/java/co/rsk/peg/Federation.java b/rskj-core/src/main/java/co/rsk/peg/Federation.java index 04aae335101..e2c899ed4b0 100644 --- a/rskj-core/src/main/java/co/rsk/peg/Federation.java +++ b/rskj-core/src/main/java/co/rsk/peg/Federation.java @@ -21,7 +21,6 @@ import co.rsk.bitcoinj.core.Address; import co.rsk.bitcoinj.core.BtcECKey; import co.rsk.bitcoinj.core.NetworkParameters; -import co.rsk.bitcoinj.core.VerificationException; import co.rsk.bitcoinj.script.Script; import co.rsk.bitcoinj.script.ScriptBuilder; @@ -58,7 +57,8 @@ protected Federation(List members, Instant creationTime, long this.creationBlockNumber = creationBlockNumber; this.btcParams = btcParams; - //validateFederationValues(); + // TODO + // validateFederationValues(); } public List getMembers() { @@ -162,7 +162,8 @@ public int hashCode() { return getAddress().hashCode(); } -/* private void validateFederationValues() { +/* TODO implement this check + private void validateFederationValues() { if (creationBlockNumber <= 0) { String message = String.format( "Provided creation block number %d must be larger than 0", From 0ad9ded9d580f3c131f1ad66d0535aab2bab859c Mon Sep 17 00:00:00 2001 From: julia zack Date: Fri, 6 Oct 2023 10:17:06 -0300 Subject: [PATCH 25/27] Add LEGACY_ to LEGACY_ERP variable --- rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java b/rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java index c326c5d729b..1db5ad0c8a1 100644 --- a/rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java +++ b/rskj-core/src/main/java/co/rsk/peg/LegacyErpFederation.java @@ -22,7 +22,7 @@ @Deprecated public class LegacyErpFederation extends ErpFederation { private static final Logger logger = LoggerFactory.getLogger(LegacyErpFederation.class); - private static final byte[] ERP_TESTNET_REDEEM_SCRIPT_BYTES = Hex.decode("6453210208f40073a9e43b3e9103acec79767a6de9b0409749884e989960fee578012fce210225e892391625854128c5c4ea4340de0c2a70570f33db53426fc9c746597a03f42102afc230c2d355b1a577682b07bc2646041b5d0177af0f98395a46018da699b6da210344a3c38cd59afcba3edcebe143e025574594b001700dec41e59409bdbd0f2a0921039a060badbeb24bee49eb2063f616c0f0f0765d4ca646b20a88ce828f259fcdb955670300cd50b27552210216c23b2ea8e4f11c3f9e22711addb1d16a93964796913830856b568cc3ea21d3210275562901dd8faae20de0a4166362a4f82188db77dbed4ca887422ea1ec185f1421034db69f2112f4fb1bb6141bf6e2bd6631f0484d0bd95b16767902c9fe219d4a6f5368ae"); + private static final byte[] LEGACY_ERP_TESTNET_REDEEM_SCRIPT_BYTES = Hex.decode("6453210208f40073a9e43b3e9103acec79767a6de9b0409749884e989960fee578012fce210225e892391625854128c5c4ea4340de0c2a70570f33db53426fc9c746597a03f42102afc230c2d355b1a577682b07bc2646041b5d0177af0f98395a46018da699b6da210344a3c38cd59afcba3edcebe143e025574594b001700dec41e59409bdbd0f2a0921039a060badbeb24bee49eb2063f616c0f0f0765d4ca646b20a88ce828f259fcdb955670300cd50b27552210216c23b2ea8e4f11c3f9e22711addb1d16a93964796913830856b568cc3ea21d3210275562901dd8faae20de0a4166362a4f82188db77dbed4ca887422ea1ec185f1421034db69f2112f4fb1bb6141bf6e2bd6631f0484d0bd95b16767902c9fe219d4a6f5368ae"); public LegacyErpFederation( List members, @@ -43,7 +43,7 @@ public Script getRedeemScript() { if (!activations.isActive(ConsensusRule.RSKIP284) && btcParams.getId().equals(NetworkParameters.ID_TESTNET)) { logger.debug("[getRedeemScript] Returning hardcoded redeem script"); - return new Script(ERP_TESTNET_REDEEM_SCRIPT_BYTES); + return new Script(LEGACY_ERP_TESTNET_REDEEM_SCRIPT_BYTES); } if (redeemScript == null) { @@ -76,7 +76,7 @@ public Script getStandardRedeemScript() { private void validateRedeemScript() { if (activations.isActive(ConsensusRule.RSKIP293) && - this.getRedeemScript().equals(new Script(ERP_TESTNET_REDEEM_SCRIPT_BYTES))) { + this.getRedeemScript().equals(new Script(LEGACY_ERP_TESTNET_REDEEM_SCRIPT_BYTES))) { String message = "Unable to create ERP Federation. The obtained redeem script matches the one hardcoded for testnet. " + "This would cause bitcoinj-thin to identify it as invalid"; From 05cabdff8acb1ae3d52e78741337215c5cc56b68 Mon Sep 17 00:00:00 2001 From: julia zack Date: Fri, 6 Oct 2023 11:16:27 -0300 Subject: [PATCH 26/27] Correct exception class. Remove TODOs. Remove comments --- .../java/co/rsk/peg/BridgeSerializationUtils.java | 2 +- .../java/co/rsk/peg/BridgeStorageProvider.java | 2 +- .../src/main/java/co/rsk/peg/ErpFederation.java | 2 +- rskj-core/src/main/java/co/rsk/peg/Federation.java | 14 -------------- .../java/co/rsk/peg/LegacyErpFederationTest.java | 6 +++--- 5 files changed, 6 insertions(+), 20 deletions(-) diff --git a/rskj-core/src/main/java/co/rsk/peg/BridgeSerializationUtils.java b/rskj-core/src/main/java/co/rsk/peg/BridgeSerializationUtils.java index db733975c4c..b9cdac52c4c 100644 --- a/rskj-core/src/main/java/co/rsk/peg/BridgeSerializationUtils.java +++ b/rskj-core/src/main/java/co/rsk/peg/BridgeSerializationUtils.java @@ -287,7 +287,7 @@ public static byte[] serializeFederationOnlyBtcKeys(Federation federation) { } // For the serialization format, see BridgeSerializationUtils::serializeFederationOnlyBtcKeys - public static Federation deserializeStandardMultisigFederationOnlyBtcKeys(byte[] data, NetworkParameters networkParameters) { + public static StandardMultisigFederation deserializeStandardMultisigFederationOnlyBtcKeys(byte[] data, NetworkParameters networkParameters) { return deserializeStandardMultisigFederationWithDeserializer(data, networkParameters, (pubKeyBytes -> FederationMember.getFederationMemberFromKey(BtcECKey.fromPublicOnly(pubKeyBytes)))); } diff --git a/rskj-core/src/main/java/co/rsk/peg/BridgeStorageProvider.java b/rskj-core/src/main/java/co/rsk/peg/BridgeStorageProvider.java index d3db7f2eadc..03683b37dd2 100644 --- a/rskj-core/src/main/java/co/rsk/peg/BridgeStorageProvider.java +++ b/rskj-core/src/main/java/co/rsk/peg/BridgeStorageProvider.java @@ -333,7 +333,7 @@ public Federation getNewFederation() { return null; } if (storageVersion.isPresent()) { - return deserializeFederationAccordingToVersion(data, storageVersion.get(), bridgeConstants); // ACA ES EL ERROR + return deserializeFederationAccordingToVersion(data, storageVersion.get(), bridgeConstants); } return BridgeSerializationUtils.deserializeStandardMultisigFederationOnlyBtcKeys(data, networkParameters); diff --git a/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java b/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java index a7aeaa43370..b5d92e771c9 100644 --- a/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java +++ b/rskj-core/src/main/java/co/rsk/peg/ErpFederation.java @@ -67,7 +67,7 @@ private void validateErpFederationValues() { activationDelay, maxCsvValue ); - throw new VerificationException(message); + throw new FederationCreationException(message); } } } diff --git a/rskj-core/src/main/java/co/rsk/peg/Federation.java b/rskj-core/src/main/java/co/rsk/peg/Federation.java index e2c899ed4b0..e14dc02df93 100644 --- a/rskj-core/src/main/java/co/rsk/peg/Federation.java +++ b/rskj-core/src/main/java/co/rsk/peg/Federation.java @@ -56,9 +56,6 @@ protected Federation(List members, Instant creationTime, long this.creationTime = creationTime.truncatedTo(ChronoUnit.MILLIS); this.creationBlockNumber = creationBlockNumber; this.btcParams = btcParams; - - // TODO - // validateFederationValues(); } public List getMembers() { @@ -161,15 +158,4 @@ public boolean equals(Object other) { public int hashCode() { return getAddress().hashCode(); } - -/* TODO implement this check - private void validateFederationValues() { - if (creationBlockNumber <= 0) { - String message = String.format( - "Provided creation block number %d must be larger than 0", - creationBlockNumber - ); - throw new VerificationException(message); - } - }*/ } diff --git a/rskj-core/src/test/java/co/rsk/peg/LegacyErpFederationTest.java b/rskj-core/src/test/java/co/rsk/peg/LegacyErpFederationTest.java index 65be368df1f..0cd0a4a877c 100644 --- a/rskj-core/src/test/java/co/rsk/peg/LegacyErpFederationTest.java +++ b/rskj-core/src/test/java/co/rsk/peg/LegacyErpFederationTest.java @@ -243,7 +243,7 @@ void createInvalidErpFederation_negativeCsvValue() { List federationMembersFromPks = FederationTestUtils.getFederationMembersFromPks(100, 200, 300); Instant creationTime = ZonedDateTime.parse("2017-06-10T02:30:00Z").toInstant(); NetworkParameters btcParams = NetworkParameters.fromID(NetworkParameters.ID_REGTEST); - assertThrows(VerificationException.class, () -> new LegacyErpFederation( + assertThrows(FederationCreationException.class, () -> new LegacyErpFederation( federationMembersFromPks, creationTime, 1, @@ -259,7 +259,7 @@ void createInvalidErpFederation_csvValueZero() { List federationMembersFromPks = FederationTestUtils.getFederationMembersFromPks(100, 200, 300); Instant creationTime = ZonedDateTime.parse("2017-06-10T02:30:00Z").toInstant(); NetworkParameters btcParams = NetworkParameters.fromID(NetworkParameters.ID_REGTEST); - assertThrows(VerificationException.class, () -> new LegacyErpFederation( + assertThrows(FederationCreationException.class, () -> new LegacyErpFederation( federationMembersFromPks, creationTime, 1, @@ -275,7 +275,7 @@ void createInvalidErpFederation_csvValueAboveMax() { List federationMembersFromPks = FederationTestUtils.getFederationMembersFromPks(100, 200, 300); Instant creationTime = ZonedDateTime.parse("2017-06-10T02:30:00Z").toInstant(); NetworkParameters btcParams = NetworkParameters.fromID(NetworkParameters.ID_REGTEST); - assertThrows(VerificationException.class, () -> new LegacyErpFederation( + assertThrows(FederationCreationException.class, () -> new LegacyErpFederation( federationMembersFromPks, creationTime, 1, From c8a0ef3e0e6e8591d4c07c46f508d6f9641d62d2 Mon Sep 17 00:00:00 2001 From: julia zack Date: Fri, 6 Oct 2023 11:26:46 -0300 Subject: [PATCH 27/27] Refactor test names. Remove unnecessary casting --- .../src/test/java/co/rsk/peg/FederationSupportTest.java | 2 +- .../java/co/rsk/peg/StandardMultisigFederationTest.java | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/rskj-core/src/test/java/co/rsk/peg/FederationSupportTest.java b/rskj-core/src/test/java/co/rsk/peg/FederationSupportTest.java index 8b2b9aa9c5c..46f550c1662 100644 --- a/rskj-core/src/test/java/co/rsk/peg/FederationSupportTest.java +++ b/rskj-core/src/test/java/co/rsk/peg/FederationSupportTest.java @@ -74,7 +74,7 @@ void whenNewFederationIsNullThenActiveFederationIsGenesisFederation() { when(provider.getNewFederation()) .thenReturn(null); when(bridgeConstants.getGenesisFederation()) - .thenReturn((StandardMultisigFederation) genesisFederation); + .thenReturn(genesisFederation); assertThat(federationSupport.getActiveFederation(), is(genesisFederation)); } diff --git a/rskj-core/src/test/java/co/rsk/peg/StandardMultisigFederationTest.java b/rskj-core/src/test/java/co/rsk/peg/StandardMultisigFederationTest.java index d2ce6808d77..8cca1a85abe 100644 --- a/rskj-core/src/test/java/co/rsk/peg/StandardMultisigFederationTest.java +++ b/rskj-core/src/test/java/co/rsk/peg/StandardMultisigFederationTest.java @@ -121,7 +121,7 @@ void redeemScript() { } @Test - void testArrayEquals_P2SHScript() { + void getP2SHScript() { ScriptBuilder p2shScriptBuilder = new ScriptBuilder(); p2shScriptBuilder.op(ScriptOpCodes.OP_HASH160); p2shScriptBuilder.data(Hex.decode("57f76bf3ab818811c740929ac7a5e3ef8c7a34b9")); @@ -129,11 +129,11 @@ void testArrayEquals_P2SHScript() { Script p2shScript = p2shScriptBuilder.build(); - Assertions.assertArrayEquals(federation.getP2SHScript().getProgram(), p2shScript.getProgram()); + Assertions.assertEquals(federation.getP2SHScript(), p2shScript); } @Test - void testEquals_Address() { + void getAddress() { Assertions.assertEquals("2N1GMB8gxHYR5HLPSRgf9CJ9Lunjb9CTnKB", federation.getAddress().toBase58()); }