Skip to content

Commit

Permalink
Set network parameters at bridge support class level to avoid duplica…
Browse files Browse the repository at this point in the history
…tion code
  • Loading branch information
julia-zack committed Sep 10, 2024
1 parent 5cf3355 commit 34ba1e4
Showing 1 changed file with 22 additions and 24 deletions.
46 changes: 22 additions & 24 deletions rskj-core/src/main/java/co/rsk/peg/BridgeSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public class BridgeSupport {
private static final PanicProcessor panicProcessor = new PanicProcessor();

private final BridgeConstants bridgeConstants;
private final NetworkParameters networkParameters;
private final BridgeStorageProvider provider;
private final Repository rskRepository;
private final BridgeEventLogger eventLogger;
Expand Down Expand Up @@ -145,6 +146,7 @@ public BridgeSupport(
this.provider = provider;
this.rskExecutionBlock = executionBlock;
this.bridgeConstants = bridgeConstants;
this.networkParameters = bridgeConstants.getBtcParams();
this.eventLogger = eventLogger;
this.btcLockSenderProvider = btcLockSenderProvider;
this.peginInstructionsProvider = peginInstructionsProvider;
Expand All @@ -164,12 +166,12 @@ public List<ProgramSubtrace> getSubtraces() {

@VisibleForTesting
InputStream getCheckPoints() {
String resourceName = "/rskbitcoincheckpoints/" + bridgeConstants.getBtcParams().getId() + ".checkpoints";
String resourceName = "/rskbitcoincheckpoints/" + networkParameters.getId() + ".checkpoints";
InputStream checkpoints = BridgeSupport.class.getResourceAsStream(resourceName);
logger.debug("[getCheckPoints] Looking for checkpoint {}. Found? {}", resourceName, checkpoints != null);
if (checkpoints == null) {
// If we don't have a custom checkpoints file, try to use bitcoinj's default checkpoints for that network
checkpoints = BridgeSupport.class.getResourceAsStream("/" + bridgeConstants.getBtcParams().getId() + ".checkpoints");
checkpoints = BridgeSupport.class.getResourceAsStream("/" + networkParameters.getId() + ".checkpoints");
}
return checkpoints;
}
Expand Down Expand Up @@ -375,7 +377,7 @@ public void registerBtcTransaction(
throw new RegisterBtcTransactionException("Could not validate transaction");
}

BtcTransaction btcTx = new BtcTransaction(bridgeConstants.getBtcParams(), btcTxSerialized);
BtcTransaction btcTx = new BtcTransaction(networkParameters, btcTxSerialized);
btcTx.verify();
logger.debug("[registerBtcTransaction][rsk tx {}] Btc tx hash without witness {}", rskTxHash, btcTx.getHash(false));

Expand Down Expand Up @@ -833,8 +835,7 @@ public void releaseBtc(Transaction rskTx) throws IOException {
}

Context.propagate(btcContext);
NetworkParameters btcParams = bridgeConstants.getBtcParams();
Address btcDestinationAddress = BridgeUtils.recoverBtcAddressFromEthTransaction(rskTx, btcParams);
Address btcDestinationAddress = BridgeUtils.recoverBtcAddressFromEthTransaction(rskTx, networkParameters);
logger.debug("[releaseBtc] BTC destination address: {}", btcDestinationAddress);

requestRelease(btcDestinationAddress, pegoutValueInSatoshis, rskTx);
Expand Down Expand Up @@ -976,7 +977,7 @@ protected void processSvpFundTransactionUnsigned(Transaction rskTx) throws IOExc
private BtcTransaction createSvpFundTransaction(Federation proposedFederation, Coin spendableValueFromProposedFederation) throws InsufficientMoneyException {
Wallet activeFederationWallet = getActiveFederationWallet(true);

BtcTransaction svpFundTransaction = new BtcTransaction(bridgeConstants.getBtcParams());
BtcTransaction svpFundTransaction = new BtcTransaction(networkParameters);
svpFundTransaction.setVersion(BTC_TX_VERSION_2);

// add outputs to proposed fed and proposed fed with flyover prefix
Expand All @@ -997,7 +998,7 @@ private Address getProposedFederationWithFlyoverPrefixAddress(Script federationR
Script federationWithFlyoverPrefixRedeemScript = flyoverRedeemScriptBuilder.addFlyoverDerivationHashToRedeemScript(bridgeConstants.getProposedFederationFlyoverPrefix(), federationRedeemScript);
Script federationWithFlyoverPrefixP2SHScript = ScriptBuilder.createP2SHOutputScript(federationWithFlyoverPrefixRedeemScript);

return Address.fromP2SHScript(bridgeConstants.getBtcParams(), federationWithFlyoverPrefixP2SHScript);
return Address.fromP2SHScript(networkParameters, federationWithFlyoverPrefixP2SHScript);
}

private SendRequest createSvpFundTransactionSendRequest(BtcTransaction transaction) {
Expand Down Expand Up @@ -2202,7 +2203,7 @@ public void registerBtcCoinbaseTransaction(byte[] btcTxSerialized, Sha256Hash bl
Sha256Hash merkleRoot;

try {
PartialMerkleTree pmt = new PartialMerkleTree(bridgeConstants.getBtcParams(), pmtSerialized, 0);
PartialMerkleTree pmt = new PartialMerkleTree(networkParameters, pmtSerialized, 0);
List<Sha256Hash> hashesInPmt = new ArrayList<>();
merkleRoot = pmt.getTxnHashAndMerkleRoot(hashesInPmt);
if (!hashesInPmt.contains(btcTxHash)) {
Expand Down Expand Up @@ -2234,7 +2235,7 @@ public void registerBtcCoinbaseTransaction(byte[] btcTxSerialized, Sha256Hash bl
return;
}

BtcTransaction btcTx = new BtcTransaction(bridgeConstants.getBtcParams(), btcTxSerialized);
BtcTransaction btcTx = new BtcTransaction(networkParameters, btcTxSerialized);
btcTx.verify();

Sha256Hash witnessCommitment = Sha256Hash.twiceOf(witnessMerkleRoot.getReversedBytes(), witnessReservedValue);
Expand Down Expand Up @@ -2350,7 +2351,7 @@ public BigInteger registerFlyoverBtcTransaction(
return BigInteger.valueOf(FlyoverTxResponseCodes.UNPROCESSABLE_TX_VALIDATIONS_ERROR.value());
}

BtcTransaction btcTx = new BtcTransaction(bridgeConstants.getBtcParams(), btcTxSerialized);
BtcTransaction btcTx = new BtcTransaction(networkParameters, btcTxSerialized);
btcTx.verify();

Sha256Hash btcTxHashWithoutWitness = btcTx.getHash(false);
Expand All @@ -2368,7 +2369,7 @@ public BigInteger registerFlyoverBtcTransaction(
}

FlyoverFederationInformation flyoverActiveFederationInformation = createFlyoverFederationInformation(flyoverDerivationHash);
Address flyoverActiveFederationAddress = flyoverActiveFederationInformation.getFlyoverFederationAddress(bridgeConstants.getBtcParams());
Address flyoverActiveFederationAddress = flyoverActiveFederationInformation.getFlyoverFederationAddress(networkParameters);
Federation retiringFederation = getRetiringFederation();
Optional<FlyoverFederationInformation> flyoverRetiringFederationInformation = Optional.empty();

Expand All @@ -2378,7 +2379,7 @@ public BigInteger registerFlyoverBtcTransaction(
if (activations.isActive(RSKIP293) && retiringFederation != null) {
flyoverRetiringFederationInformation = Optional.of(createFlyoverFederationInformation(flyoverDerivationHash, retiringFederation));
Address flyoverRetiringFederationAddress = flyoverRetiringFederationInformation.get().getFlyoverFederationAddress(
bridgeConstants.getBtcParams()
networkParameters
);
addresses.add(flyoverRetiringFederationAddress);
logger.debug("[registerFlyoverBtcTransaction] flyover retiring federation address: {}", flyoverRetiringFederationAddress);
Expand All @@ -2399,7 +2400,7 @@ public BigInteger registerFlyoverBtcTransaction(

Coin totalAmount = BridgeUtils.getAmountSentToAddresses(
activations,
bridgeConstants.getBtcParams(),
networkParameters,
btcContext,
btcTx,
addresses
Expand Down Expand Up @@ -2431,7 +2432,7 @@ public BigInteger registerFlyoverBtcTransaction(

List<UTXO> utxosForFlyoverActiveFed = BridgeUtils.getUTXOsSentToAddresses(
activations,
bridgeConstants.getBtcParams(),
networkParameters,
btcContext,
btcTx,
Collections.singletonList(flyoverActiveFederationAddress)
Expand All @@ -2451,11 +2452,11 @@ public BigInteger registerFlyoverBtcTransaction(
if (activations.isActive(RSKIP293) && flyoverRetiringFederationInformation.isPresent()) {
List<UTXO> utxosForRetiringFed = BridgeUtils.getUTXOsSentToAddresses(
activations,
bridgeConstants.getBtcParams(),
networkParameters,
btcContext,
btcTx,
Collections.singletonList(
flyoverRetiringFederationInformation.get().getFlyoverFederationAddress(bridgeConstants.getBtcParams())
flyoverRetiringFederationInformation.get().getFlyoverFederationAddress(networkParameters)
)
);

Expand Down Expand Up @@ -2505,7 +2506,7 @@ private WalletProvider createFlyoverWalletProvider(
return (BtcTransaction btcTx, List<Address> addresses) -> {
List<UTXO> utxosList = BridgeUtils.getUTXOsSentToAddresses(
activations,
bridgeConstants.getBtcParams(),
networkParameters,
btcContext,
btcTx,
addresses
Expand Down Expand Up @@ -2617,10 +2618,10 @@ private StoredBlock getBtcBlockchainChainHead() throws IOException, BlockStoreEx
private StoredBlock getLowestBlock() throws IOException {
InputStream checkpoints = this.getCheckPoints();
if (checkpoints == null) {
BtcBlock genesis = bridgeConstants.getBtcParams().getGenesisBlock();
BtcBlock genesis = networkParameters.getGenesisBlock();
return new StoredBlock(genesis, genesis.getWork(), 0);
}
CheckpointManager manager = new CheckpointManager(bridgeConstants.getBtcParams(), checkpoints);
CheckpointManager manager = new CheckpointManager(networkParameters, checkpoints);
long time = getActiveFederation().getCreationTime().toEpochMilli();
// Go back 1 week to match CheckpointManager.checkpoint() behaviour
time -= 86400 * 7;
Expand Down Expand Up @@ -2691,14 +2692,12 @@ private void ensureBtcBlockStore() throws IOException, BlockStoreException {
provider,
activations
);
NetworkParameters btcParams = this.bridgeConstants.getBtcParams();

if (this.btcBlockStore.getChainHead().getHeader().getHash().equals(btcParams.getGenesisBlock().getHash())) {
if (this.btcBlockStore.getChainHead().getHeader().getHash().equals(networkParameters.getGenesisBlock().getHash())) {
// We are building the blockstore for the first time, so we have not set the checkpoints yet.
long time = federationSupport.getActiveFederation().getCreationTime().toEpochMilli();
InputStream checkpoints = this.getCheckPoints();
if (time > 0 && checkpoints != null) {
CheckpointManager.checkpoint(btcParams, checkpoints, this.btcBlockStore, time);
CheckpointManager.checkpoint(networkParameters, checkpoints, this.btcBlockStore, time);
}
}
}
Expand Down Expand Up @@ -2863,7 +2862,6 @@ protected boolean validationsForRegisterBtcTransaction(Sha256Hash btcTxHash, int
// Calculates merkleRoot
Sha256Hash merkleRoot;
try {
NetworkParameters networkParameters = bridgeConstants.getBtcParams();
merkleRoot = BridgeUtils.calculateMerkleRoot(networkParameters, pmtSerialized, btcTxHash);
if (merkleRoot == null) {
return false;
Expand Down

0 comments on commit 34ba1e4

Please sign in to comment.