Skip to content

Commit

Permalink
Refactor validateCoinbaseInformation
Browse files Browse the repository at this point in the history
  • Loading branch information
marcos-iov committed Dec 5, 2024
1 parent 931308f commit 810f04d
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/main/java/co/rsk/federate/BtcToRskClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -329,22 +329,22 @@ private void validateCoinbaseInformation(CoinbaseInformation coinbaseInformation
throw new IllegalArgumentException(message);
}

Optional<co.rsk.bitcoinj.core.Sha256Hash> expectedWitnessCommitment = BitcoinUtils.findWitnessCommitment(coinbaseTransaction);
co.rsk.bitcoinj.core.Sha256Hash calculatedWitnessCommitment = co.rsk.bitcoinj.core.Sha256Hash.twiceOf(
witnessMerkleRoot.getReversedBytes(),
witnessReservedValue
);

BitcoinUtils.findWitnessCommitment(coinbaseTransaction)
.filter(commitment -> commitment.equals(calculatedWitnessCommitment))
.orElseThrow(() -> {
String message = String.format(
"Block %s with segwit peg-in tx %s generated an invalid witness merkle root",
coinbaseInformation.getBlockHash(),
coinbaseTransaction.getHash()
);
logger.error("[validateCoinbaseInformation] {}", message);
return new IllegalArgumentException(message);
});
if (expectedWitnessCommitment.isEmpty() || !expectedWitnessCommitment.get().equals(calculatedWitnessCommitment)) {
String message = String.format(
"Block %s with segwit peg-in tx %s generated an invalid witness commitment",
coinbaseInformation.getBlockHash(),
coinbaseTransaction.getHash()
);
logger.error("[validateCoinbaseInformation] {}", message);
throw new IllegalArgumentException(message);
}

logger.debug(
"[validateCoinbaseInformation] Block {} with segwit peg-in tx {} has a valid witness merkle root",
coinbaseInformation.getBlockHash(),
Expand Down

0 comments on commit 810f04d

Please sign in to comment.