Skip to content

Commit

Permalink
Refactor validateWitnessInformation
Browse files Browse the repository at this point in the history
  • Loading branch information
marcos-iov committed Dec 5, 2024
1 parent 5e87536 commit 6c82660
Showing 1 changed file with 12 additions and 16 deletions.
28 changes: 12 additions & 16 deletions rskj-core/src/main/java/co/rsk/peg/BridgeSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -2283,24 +2283,20 @@ private void validateWitnessInformation(
Sha256Hash witnessMerkleRoot,
byte[] witnessReservedValue
) throws BridgeIllegalArgumentException {
Optional<Sha256Hash> expectedWitnessCommitment = findWitnessCommitment(coinbaseTransaction);
Sha256Hash calculatedWitnessCommitment = Sha256Hash.twiceOf(witnessMerkleRoot.getReversedBytes(), witnessReservedValue);

findWitnessCommitment(coinbaseTransaction)
.filter(commitment -> commitment.equals(calculatedWitnessCommitment))
.orElseThrow(() -> {
String message = String.format(
"[btcTx: %s] Calculated witness commitment %s does not match the expected value",
coinbaseTransaction.getHash(),
calculatedWitnessCommitment
);
logger.warn("[validateWitnessInformation] {}", message);
return new BridgeIllegalArgumentException(message);
});
logger.debug(
"[validateWitnessInformation] Witness commitment {} validated for btc tx {}",
calculatedWitnessCommitment,
coinbaseTransaction.getHash()
);
if (expectedWitnessCommitment.isEmpty() || !expectedWitnessCommitment.get().equals(calculatedWitnessCommitment)) {
String message = String.format(
"[btcTx: %s] Witness commitment does not match. Expected: %s, Calculated: %s",
coinbaseTransaction.getHash(),
expectedWitnessCommitment.orElse(null),
calculatedWitnessCommitment
);
logger.warn("[validateWitnessInformation] {}", message);
throw new BridgeIllegalArgumentException(message);
}
logger.debug("[validateWitnessInformation] Witness commitment {} validated for btc tx {}", calculatedWitnessCommitment, coinbaseTransaction.getHash());
}

public boolean hasBtcBlockCoinbaseTransactionInformation(Sha256Hash blockHash) {
Expand Down

0 comments on commit 6c82660

Please sign in to comment.