Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of RSKIP379 #2186

Merged
merged 122 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
122 commits
Select commit Hold shift + click to select a range
0b467a8
add proof of concept tests to understand how sighash works
nathanieliov Jun 7, 2023
19ec15e
Add new test to assert sighash is different when tx is altered
nathanieliov Jun 12, 2023
52cbcda
Added RSKIP379 activation code
nathanieliov Jul 11, 2023
0923048
Fixed compile error after rebased against master
nathanieliov Jul 31, 2023
5e4d3fa
Rename FLYOVER_FEDERATION_DERIVATION_HASH_TRUE_VALUE to TRUE_VALUE
nathanieliov Jul 12, 2023
23d6efb
Added bridge storage provider new methods.
nathanieliov Jul 13, 2023
9aeb4c5
- Changed Activation mock to Activation obtained by using ActivationC…
nathanieliov Jul 18, 2023
b78469c
- Renamed bridge storage provider class variable
nathanieliov Jul 25, 2023
2754a72
Refactored BridgeStorageProvider class to support setting multiple br…
nathanieliov Jul 26, 2023
08f4d68
Removed useless curly braces
nathanieliov Jul 26, 2023
6940a7f
Renamed bridgeBtcTx to pegoutTx
nathanieliov Jul 28, 2023
de432c2
- Deprecated PegTestUtils.CreateHash method.
nathanieliov Jul 31, 2023
26e9d07
Added new getFirstInputSighash and getRedeemScript methods.
nathanieliov Aug 24, 2023
4c1e00a
Added tests for BitcoinUtils.getFirstInputSighash method.
nathanieliov Aug 24, 2023
7829380
Added test for a flyover input
nathanieliov Aug 24, 2023
0ee80b6
Fixed incorrect naming.
nathanieliov Aug 24, 2023
eb48a01
Removed BitcoinUtils.getRedeemScript method.
nathanieliov Aug 24, 2023
e6a33bf
Added new test.
nathanieliov Aug 24, 2023
95b436c
- Created FIRST_INPUT_INDEX constant to get rid of "0" int literal.
nathanieliov Aug 28, 2023
2c0b764
- Made instance variables private in BitcoinUtilsTest.
nathanieliov Aug 29, 2023
965d1d1
Refactored tests to verify using signature.
nathanieliov Aug 31, 2023
a193379
- Fixed failing tests.
nathanieliov Aug 31, 2023
d86c474
- Renamed lastChunk variable to flowOpCodeChunk.
nathanieliov Sep 1, 2023
d11675e
Enhanced isSigHashValid method.
nathanieliov Sep 4, 2023
0449100
Added new tests.
nathanieliov Sep 5, 2023
0d436a3
- Refactored extractSignaturesFromTxInput.
nathanieliov Sep 5, 2023
9a6be6b
Created and implemented savePegoutTxSigHash new method responsible fo…
nathanieliov Aug 25, 2023
a6b0617
Added tests for storing sigHash when a pegout or migration is created.
nathanieliov Aug 25, 2023
e71a4b5
Added tests and assertions to PowpegMigrationTest.
nathanieliov Aug 25, 2023
88355bc
- Fixed comments.
nathanieliov Aug 25, 2023
d3d55ea
- Removed unused import.
nathanieliov Aug 25, 2023
dac8308
- Set new fed activation block number literal into a variable.
nathanieliov Sep 1, 2023
ca786d8
Moved repetitive test code to before junit method.
nathanieliov Sep 1, 2023
024e33b
Reduced lines of code by reformatting some lines.
nathanieliov Sep 6, 2023
b498592
Added getTransactionType method.
nathanieliov Aug 29, 2023
8794a46
Added tests.
nathanieliov Aug 29, 2023
09631b3
Added tests sending funds to multiple outputs
nathanieliov Sep 1, 2023
4e7cfc5
Replaced RegisterBtcTransactionException for IllegalStateException ge…
nathanieliov Sep 6, 2023
0e2987d
- Moved repetitive code to beforeEach method.
nathanieliov Sep 7, 2023
c264449
- Removed activations params from createP2shFederation method.
nathanieliov Sep 8, 2023
890467f
- Moved extractRedeemScriptFromInput to BitcoinUtils class.
nathanieliov Sep 6, 2023
b0daca9
- Added test using inputs different that first input.
nathanieliov Sep 12, 2023
0724856
Moved legacy methods related to identify btc tx types process to PegU…
nathanieliov Sep 7, 2023
eacc1fb
- Moved legacy method tests to PegUtilsLegacyTest.
nathanieliov Sep 7, 2023
a7fc558
Added activation check.
nathanieliov Sep 7, 2023
745226a
- Moved logic for checking old federation peg-out out of PegUtilsLegacy
nathanieliov Oct 2, 2023
69bd0da
- Changed public to protected the visibility of legacy methods.
nathanieliov Oct 2, 2023
9a80bab
- Moved oldFederation logic into PegUtilsLegacy.getTransactionType.
nathanieliov Oct 9, 2023
d37ab8f
- Renamed variable.
nathanieliov Oct 9, 2023
247f702
Avoid null check of old federation address
marcos-iov Oct 9, 2023
91c6a7f
Rename getTransactionType to getTransactionTypeUsingPegoutIndex
nathanieliov Oct 13, 2023
3c73eb3
Refactor PegUtilsLegacy.isValidPegInTx to get rid of BtcContext param…
nathanieliov Oct 13, 2023
9e38b9b
- Removed no needed spy.
nathanieliov Oct 23, 2023
5735b43
Add estimatedPegoutTxIndexBtcActivationHeight and pegoutTxIndexGraceP…
nathanieliov Oct 19, 2023
0bc2a64
Rename estimatedPegoutTxIndexBtcActivationHeight to - btcHeightWhenPe…
nathanieliov Oct 20, 2023
802f1c6
- Change btcHeightWhenPegoutTxIndexActivates type to int type.
nathanieliov Oct 23, 2023
9d9879b
Implement and use PegUtils.getTransactionType method
nathanieliov Oct 20, 2023
73bc8b8
Refactor getTransactionType method to reduce its number of parameters
nathanieliov Oct 20, 2023
db648a5
Consider retired federation when identifying tx type
nathanieliov Oct 23, 2023
fb2eee9
- Add new tests for corner cases
nathanieliov Oct 24, 2023
4b68959
Reduce getTransactionType number of parameters.
nathanieliov Oct 24, 2023
f492412
Fix failing tests due to error getting context instance
nathanieliov Oct 24, 2023
97d0e01
Refactored PegUtilsTest and PegUtilsLegacyGetTransactionTypeTest.
nathanieliov Oct 26, 2023
e6c4b81
Add tests testing peg-in for PegUtils.getTransactionType method.
nathanieliov Oct 27, 2023
f83d2d1
Add tests testing pegouts for PegUtils.getTransactionType method.
nathanieliov Oct 27, 2023
3cc8951
Add tests testing migration for PegUtils.getTransactionType method.
nathanieliov Oct 27, 2023
4e47439
Removed unused imports. Reorganize code.
nathanieliov Oct 31, 2023
c71fe20
Improved and refactored tests by making them into parameterized tests.
nathanieliov Nov 1, 2023
ee17c60
- Refactored PegUtil.getTransactionType tests.
nathanieliov Nov 1, 2023
d716ba5
Rename PegUtilTest class to PegUtilsGetTransactionTypeTest
nathanieliov Nov 2, 2023
54075b1
Add flyover tests
nathanieliov Nov 2, 2023
3815d53
- Add peg-in v1 tests.
nathanieliov Nov 8, 2023
c182364
- Refactor tests to set tx input correctly.
nathanieliov Nov 8, 2023
588bd57
Fix typo
nathanieliov Nov 9, 2023
ee714b7
Renames PegUtils:isAnyUTXOAmountBelowMinimum to PegUtils.allUTXOsToFe…
jeremy-then Nov 5, 2023
25b5f9e
Adds more tests to allUTXOsToFedAreAboveMinimumPeginValue method
jeremy-then Nov 7, 2023
4957935
Moved all allUTXOsToFedAreAboveMinimumPeginValue tests to a new class…
jeremy-then Nov 9, 2023
2acf29c
- Create PeginEvaluationResult class and PeginProcessAction
nathanieliov Nov 13, 2023
60253de
- Fix tests
nathanieliov Nov 14, 2023
a19e207
Add retired federation
nathanieliov Nov 15, 2023
56e4651
Fix tests failing
nathanieliov Nov 15, 2023
c2ba695
Fix bug and code smell.
nathanieliov Nov 16, 2023
d636a97
- Moved RejectedPeginReason to pegin package
nathanieliov Nov 17, 2023
4403a69
Add tests for parsing bech32 tx
nathanieliov Nov 21, 2023
05de68a
- Refactor evaluate and processPegIn method
nathanieliov Nov 21, 2023
7cad2e5
Fix code smells
nathanieliov Nov 21, 2023
741fc26
Refactor and add logs to processPegIn method
marcos-iov Nov 21, 2023
0ab0194
Pass only the rsk tx hash when required
marcos-iov Nov 21, 2023
0318a77
Move evaluatePegin method to PegUtils class
marcos-iov Nov 21, 2023
56cba77
Fix failing tests
nathanieliov Nov 22, 2023
2f29359
Add pegoutTxIndexGracePeriodInBtcBlocks Bridge constant
marcos-iov Nov 21, 2023
6043c6e
Use pegout index grace period from Bridge constants
marcos-iov Nov 21, 2023
cf2ddbf
Emmit event when a peg-in below the minimum is registered in the Bridge
marcos-iov Nov 22, 2023
17eb617
Make rejectedPeginReason Optional in public class PeginEvaluationResult
marcos-iov Nov 22, 2023
e104714
Rename variable
marcos-iov Nov 22, 2023
1e0c986
Create method to get standard redeem and p2sh scripts from a federation
marcos-iov Nov 22, 2023
d20a086
Fix calls to create a new Federation class
marcos-iov Nov 22, 2023
bb78090
Fix failing tests due to the addition of unrefundable invalid amount …
nathanieliov Nov 23, 2023
e5b95f7
Update tests to use new logic to calculate heightAtWhichToStartUsingP…
nathanieliov Nov 23, 2023
315fac7
Put processPegin method name in a constant to use in logs
marcos-iov Nov 23, 2023
79ecbf5
Remove unused imports
marcos-iov Nov 23, 2023
f7a074b
Add logs to scriptCorrectlySpendsTx
marcos-iov Nov 23, 2023
f46d64f
Log exceptions
marcos-iov Nov 23, 2023
0b9c5ae
- Fix wrong rejected pegout reason when peg-out cannot be processed.
nathanieliov Nov 24, 2023
a0e513f
Adds tests to PegUtils::evaluatePegin
jeremy-then Nov 23, 2023
db365a1
Refactor and simplify tests for PegUtils.evaluatePegin
marcos-iov Nov 23, 2023
a084531
Fix wrong rejected pegout reason when peg-out cannot be processed.
nathanieliov Nov 21, 2023
204fd5c
Log exception in isPegoutTx method from PegUtilsLegacy
marcos-iov Nov 24, 2023
3bbfe34
Adds tests to evaluatePegin for pegout and migration
jeremy-then Nov 27, 2023
e0f2b54
Fix tests to reference arrowhead600 instead of tbd600
marcos-iov Dec 6, 2023
e002428
- Changed Activation mock to Activation obtained by using ActivationC…
nathanieliov Jul 18, 2023
a86b24f
Add new peg-in tests for registerBtcTransaction
nathanieliov Nov 28, 2023
9e338e9
Fix logic passing wrong rejected pegin for invalid peg-in v1
nathanieliov Nov 28, 2023
3ed1f83
- Declare minimumPeginTxValue and belowMinimumPeginTxValue as class i…
nathanieliov Nov 29, 2023
d817673
Update evaluate pegin tests
nathanieliov Nov 29, 2023
b9f46c9
Added bech32 tests for peginInformation
nathanieliov Nov 30, 2023
ca7a96b
Fix tests to reference arrowhead600 instead of tbd600
marcos-iov Dec 6, 2023
94fbb8a
Remove unused file
marcos-iov Dec 6, 2023
71d1029
Fix tbd600 reference in ActivationConfigTest
marcos-iov Dec 6, 2023
edda853
Replace tbd600 references for arrowhead600 in tests
marcos-iov Dec 6, 2023
8ee2cbf
Remove unused import
marcos-iov Dec 11, 2023
650cea3
Refactor tests
marcos-iov Dec 11, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion rskj-core/src/main/java/co/rsk/config/BridgeConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public abstract class BridgeConstants {

protected int btcHeightWhenBlockIndexActivates;
protected int maxDepthToSearchBlocksBelowIndexActivation;
protected long minSecondsBetweenCallsReceiveHeader; // (seconds)
protected long minSecondsBetweenCallsReceiveHeader;

protected int maxDepthBlockchainAccepted;

Expand All @@ -87,6 +87,9 @@ public abstract class BridgeConstants {

protected int numberOfBlocksBetweenPegouts;

protected int btcHeightWhenPegoutTxIndexActivates;
protected int pegoutTxIndexGracePeriodInBtcBlocks;

public NetworkParameters getBtcParams() {
return NetworkParameters.fromID(btcParamsString);
}
Expand Down Expand Up @@ -186,4 +189,12 @@ public int getMaxInputsPerPegoutTransaction() {
public int getNumberOfBlocksBetweenPegouts() {
return numberOfBlocksBetweenPegouts;
}

public int getBtcHeightWhenPegoutTxIndexActivates() {
return btcHeightWhenPegoutTxIndexActivates;
}

public int getPegoutTxIndexGracePeriodInBtcBlocks() {
return pegoutTxIndexGracePeriodInBtcBlocks;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@
import co.rsk.peg.AddressBasedAuthorizer;
import co.rsk.peg.FederationMember;
import co.rsk.peg.StandardMultisigFederation;
import org.bouncycastle.util.encoders.Hex;
import org.ethereum.crypto.ECKey;

import java.time.Instant;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.bouncycastle.util.encoders.Hex;
import org.ethereum.crypto.ECKey;

public class BridgeDevNetConstants extends BridgeConstants {
// IMPORTANT: BTC, RSK and MST keys are the same.
Expand Down Expand Up @@ -139,7 +138,7 @@ public BridgeDevNetConstants(List<BtcECKey> federationPublicKeys) {

lockingCapIncrementsMultiplier = 2;

btcHeightWhenBlockIndexActivates = 700_000; //TODO define this value when Iris activation height in RSK is determined
btcHeightWhenBlockIndexActivates = 700_000;
maxDepthToSearchBlocksBelowIndexActivation = 4_320; // 30 days in BTC blocks (considering 1 block every 10 minutes)

erpFedActivationDelay = 52_560; // 1 year in BTC blocks (considering 1 block every 10 minutes)
Expand All @@ -159,7 +158,7 @@ public BridgeDevNetConstants(List<BtcECKey> federationPublicKeys) {
// e1b17fcd0ef1942465eee61b20561b16750191143d365e71de08b33dd84a9788
oldFederationAddress = "2N7ZgQyhFKm17RbaLqygYbS7KLrQfapyZzu";

minSecondsBetweenCallsReceiveHeader = 300; // 5 minutes in seconds
minSecondsBetweenCallsReceiveHeader = 300; // 5 minutes

maxDepthBlockchainAccepted = 25;

Expand All @@ -168,5 +167,8 @@ public BridgeDevNetConstants(List<BtcECKey> federationPublicKeys) {
maxInputsPerPegoutTransaction = 10;

numberOfBlocksBetweenPegouts = 360; // 3 hours of RSK blocks (considering 1 block every 30 seconds)

btcHeightWhenPegoutTxIndexActivates = 1_000_000;
pegoutTxIndexGracePeriodInBtcBlocks = 4_320; // 30 days in BTC blocks (considering 1 block every 10 minutes)
}
}
27 changes: 14 additions & 13 deletions rskj-core/src/main/java/co/rsk/config/BridgeMainNetConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@
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;

import java.time.Instant;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.bouncycastle.util.encoders.Hex;
import org.ethereum.crypto.ECKey;

public class BridgeMainNetConstants extends BridgeConstants {
private static BridgeMainNetConstants instance = new BridgeMainNetConstants();
private static final BridgeMainNetConstants instance = new BridgeMainNetConstants();

BridgeMainNetConstants() {
btcParamsString = NetworkParameters.ID_MAINNET;
Expand All @@ -38,11 +37,11 @@ public class BridgeMainNetConstants extends BridgeConstants {
BtcECKey federator14PublicKey = BtcECKey.fromPublicOnly(Hex.decode("03b65694ccccda83cbb1e56b31308acd08e993114c33f66a456b627c2c1c68bed6"));

List<BtcECKey> genesisFederationPublicKeys = Lists.newArrayList(
federator0PublicKey, federator1PublicKey, federator2PublicKey,
federator3PublicKey, federator4PublicKey, federator5PublicKey,
federator6PublicKey, federator7PublicKey, federator8PublicKey,
federator9PublicKey, federator10PublicKey, federator11PublicKey,
federator12PublicKey, federator13PublicKey, federator14PublicKey
federator0PublicKey, federator1PublicKey, federator2PublicKey,
federator3PublicKey, federator4PublicKey, federator5PublicKey,
federator6PublicKey, federator7PublicKey, federator8PublicKey,
federator9PublicKey, federator10PublicKey, federator11PublicKey,
federator12PublicKey, federator13PublicKey, federator14PublicKey
);

// IMPORTANT: BTC, RSK and MST keys are the same.
Expand All @@ -51,7 +50,7 @@ public class BridgeMainNetConstants extends BridgeConstants {

// Currently set to:
// Wednesday, January 3, 2018 12:00:00 AM GMT-03:00
Instant genesisFederationAddressCreatedAt = Instant.ofEpochMilli(1514948400l);
Instant genesisFederationAddressCreatedAt = Instant.ofEpochMilli(1514948400L);

genesisFederation = new StandardMultisigFederation(
federationMembers,
Expand Down Expand Up @@ -107,8 +106,8 @@ public class BridgeMainNetConstants extends BridgeConstants {
}).map(hex -> ECKey.fromPublicOnly(Hex.decode(hex))).collect(Collectors.toList());

feePerKbChangeAuthorizer = new AddressBasedAuthorizer(
feePerKbAuthorizedKeys,
AddressBasedAuthorizer.MinimumRequiredCalculation.MAJORITY
feePerKbAuthorizedKeys,
AddressBasedAuthorizer.MinimumRequiredCalculation.MAJORITY
);

genesisFeePerKb = Coin.MILLICOIN.multiply(5);
Expand Down Expand Up @@ -151,10 +150,12 @@ public class BridgeMainNetConstants extends BridgeConstants {
maxInputsPerPegoutTransaction = 50;

numberOfBlocksBetweenPegouts = 360; // 3 hours of RSK blocks (considering 1 block every 30 seconds)

btcHeightWhenPegoutTxIndexActivates = 100; // TODO: TBD and change current mock value. This is an estimation of the btc block number once RSKIP379 is activated.
pegoutTxIndexGracePeriodInBtcBlocks = 4_320; // 30 days in BTC blocks (considering 1 block every 10 minutes)
}

public static BridgeMainNetConstants getInstance() {
return instance;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,15 @@
import co.rsk.peg.AddressBasedAuthorizer;
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;

import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.time.ZonedDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.bouncycastle.util.encoders.Hex;
import org.ethereum.crypto.ECKey;
import org.ethereum.crypto.HashUtil;

public class BridgeRegTestConstants extends BridgeConstants {
// IMPORTANT: BTC, RSK and MST keys are the same.
Expand Down Expand Up @@ -67,7 +66,7 @@ public BridgeRegTestConstants(List<BtcECKey> federationPublicKeys) {
btc2RskMinimumAcceptableConfirmationsOnRsk = 5;
rsk2BtcMinimumAcceptableConfirmations = 3;

updateBridgeExecutionPeriod = 1 * 15 * 1000; //15 seconds in millis
updateBridgeExecutionPeriod = 15_000; //15 seconds in millis

maxBtcHeadersPerRskBlock = 500;

Expand Down Expand Up @@ -164,6 +163,9 @@ public BridgeRegTestConstants(List<BtcECKey> federationPublicKeys) {
maxInputsPerPegoutTransaction = 10;

numberOfBlocksBetweenPegouts = 50; // 25 Minutes of RSK blocks (considering 1 block every 30 seconds)

btcHeightWhenPegoutTxIndexActivates = 250;
pegoutTxIndexGracePeriodInBtcBlocks = 100;
}

public static BridgeRegTestConstants getInstance() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,15 @@
import co.rsk.peg.AddressBasedAuthorizer;
import co.rsk.peg.FederationMember;
import co.rsk.peg.StandardMultisigFederation;
import org.bouncycastle.util.encoders.Hex;
import org.ethereum.crypto.ECKey;

import java.time.Instant;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.bouncycastle.util.encoders.Hex;
import org.ethereum.crypto.ECKey;

public class BridgeTestNetConstants extends BridgeConstants {
private static BridgeTestNetConstants instance = new BridgeTestNetConstants();
private static final BridgeTestNetConstants instance = new BridgeTestNetConstants();

BridgeTestNetConstants() {
btcParamsString = NetworkParameters.ID_TESTNET;
Expand All @@ -51,7 +50,12 @@ public class BridgeTestNetConstants extends BridgeConstants {
Hex.decode("034844a99cd7028aa319476674cc381df006628be71bc5593b8b5fdb32bb42ef85")
);

List<BtcECKey> genesisFederationPublicKeys = Arrays.asList(federator0PublicKey, federator1PublicKey, federator2PublicKey, federator3PublicKey);
List<BtcECKey> genesisFederationPublicKeys = Arrays.asList(
federator0PublicKey,
federator1PublicKey,
federator2PublicKey,
federator3PublicKey
);

// IMPORTANT: BTC, RSK and MST keys are the same.
// Change upon implementation of the <INSERT FORK NAME HERE> fork.
Expand Down Expand Up @@ -156,14 +160,17 @@ public class BridgeTestNetConstants extends BridgeConstants {
// e1b17fcd0ef1942465eee61b20561b16750191143d365e71de08b33dd84a9788
oldFederationAddress = "2N7ZgQyhFKm17RbaLqygYbS7KLrQfapyZzu";

minSecondsBetweenCallsReceiveHeader = 300; // 5 minutes in seconds
minSecondsBetweenCallsReceiveHeader = 300; // 5 minutes
maxDepthBlockchainAccepted = 25;

minimumPegoutValuePercentageToReceiveAfterFee = 80;

maxInputsPerPegoutTransaction = 50;

numberOfBlocksBetweenPegouts = 360; // 3 hours of RSK blocks (considering 1 block every 30 seconds)

btcHeightWhenPegoutTxIndexActivates = 150; // TODO: TBD and change current mock value. This is an estimation of the btc block number once RSKIP379 is activated.
pegoutTxIndexGracePeriodInBtcBlocks = 4_320; // 30 days in BTC blocks (considering 1 block every 10 minutes)
}

public static BridgeTestNetConstants getInstance() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ public enum BridgeStorageIndexKey {
BTC_BLOCK_HEIGHT("btcBlockHeight"),
FAST_BRIDGE_HASH_USED_IN_BTC_TX("fastBridgeHashUsedInBtcTx"),
FAST_BRIDGE_FEDERATION_INFORMATION("fastBridgeFederationInformation"),

PEGOUT_TX_SIG_HASH("pegoutTxSigHash"),
;

private String key;
private final String key;

BridgeStorageIndexKey(String key) {
this.key = key;
Expand Down
61 changes: 57 additions & 4 deletions rskj-core/src/main/java/co/rsk/peg/BridgeStorageProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public class BridgeStorageProvider {
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;
// Dummy value to use when saving key only indexes
private static final byte TRUE_VALUE = (byte) 1;

private final Repository repository;
private final RskAddress contractAddress;
Expand Down Expand Up @@ -110,6 +110,8 @@ public class BridgeStorageProvider {

private Long nextPegoutHeight;

private Set<Sha256Hash> pegoutTxSigHashes;

public BridgeStorageProvider(
Repository repository,
RskAddress contractAddress,
Expand Down Expand Up @@ -787,7 +789,7 @@ public boolean isFlyoverDerivationHashUsed(Sha256Hash btcTxHash, Keccak256 flyov

return data != null &&
data.length == 1 &&
data[0] == FLYOVER_FEDERATION_DERIVATION_HASH_TRUE_VALUE;
data[0] == TRUE_VALUE;
}

public void markFlyoverDerivationHashAsUsed(Sha256Hash btcTxHash, Keccak256 flyoverDerivationHash) {
Expand All @@ -808,7 +810,7 @@ private void saveFlyoverDerivationHash() {
flyoverBtcTxHash,
flyoverDerivationHash
),
new byte[]{FLYOVER_FEDERATION_DERIVATION_HASH_TRUE_VALUE}
new byte[]{TRUE_VALUE}
);
}

Expand Down Expand Up @@ -923,6 +925,51 @@ protected int getReleaseRequestQueueSize() throws IOException {
return getReleaseRequestQueue().getEntries().size();
}

public boolean hasPegoutTxSigHash(Sha256Hash sigHash) {
if (!activations.isActive(RSKIP379) || sigHash == null){
return false;
}

byte[] data = repository.getStorageBytes(
contractAddress,
getStorageKeyForPegoutTxSigHash(sigHash)
);

return data != null &&
data.length == 1 &&
data[0] == TRUE_VALUE;
}

public void setPegoutTxSigHash(Sha256Hash sigHash) {
if (!activations.isActive(RSKIP379) || sigHash == null) {
return;
}

if (hasPegoutTxSigHash(sigHash)){
throw new IllegalStateException(String.format("Given pegout tx sigHash %s already exists in the index. Index entries are considered unique.", sigHash));
}

if (pegoutTxSigHashes == null){
pegoutTxSigHashes = new HashSet<>();
}

pegoutTxSigHashes.add(sigHash);
}

private void savePegoutTxSigHashes() {
if (!activations.isActive(RSKIP379) || pegoutTxSigHashes == null) {
return;
}

pegoutTxSigHashes.forEach(pegoutTxSigHash -> repository.addStorageBytes(
contractAddress,
getStorageKeyForPegoutTxSigHash(
pegoutTxSigHash
),
new byte[]{TRUE_VALUE}
));
}

public void save() throws IOException {
saveBtcTxHashesAlreadyProcessed();

Expand Down Expand Up @@ -964,6 +1011,8 @@ public void save() throws IOException {
saveReceiveHeadersLastTimestamp();

saveNextPegoutHeight();

savePegoutTxSigHashes();
}

private DataWord getStorageKeyForBtcTxHashAlreadyProcessed(Sha256Hash btcTxHash) {
Expand Down Expand Up @@ -1000,6 +1049,10 @@ private DataWord getStorageKeyForNewFederationBtcUtxos() {
return key;
}

private DataWord getStorageKeyForPegoutTxSigHash(Sha256Hash sigHash) {
return PEGOUT_TX_SIG_HASH.getCompoundKey("-", sigHash.toString());
}

private Optional<Integer> getStorageVersion(DataWord versionKey) {
if (!storageVersionEntries.containsKey(versionKey)) {
Optional<Integer> version = safeGetFromRepository(versionKey, data -> {
Expand Down
Loading