use of co.rsk.bitcoinj.core.VerificationException in project rskj by rsksmart.
the class BridgeSupport method validationsForRegisterBtcTransaction.
@VisibleForTesting
protected boolean validationsForRegisterBtcTransaction(Sha256Hash btcTxHash, int height, byte[] pmtSerialized, byte[] btcTxSerialized) throws BlockStoreException, VerificationException.EmptyInputsOrOutputs, BridgeIllegalArgumentException {
// Validates height and confirmations for tx
try {
int acceptableConfirmationsAmount = bridgeConstants.getBtc2RskMinimumAcceptableConfirmations();
if (!BridgeUtils.validateHeightAndConfirmations(height, getBtcBlockchainBestChainHeight(), acceptableConfirmationsAmount, btcTxHash)) {
return false;
}
} catch (Exception e) {
String panicMessage = String.format("Btc Tx %s Supplied Height is %d but should be greater than 0", btcTxHash, height);
logger.warn(panicMessage);
panicProcessor.panic("btclock", panicMessage);
return false;
}
// Validates pmt size
if (!PartialMerkleTreeFormatUtils.hasExpectedSize(pmtSerialized)) {
throw new BridgeIllegalArgumentException("PartialMerkleTree doesn't have expected size");
}
// Calculates merkleRoot
Sha256Hash merkleRoot;
try {
NetworkParameters networkParameters = bridgeConstants.getBtcParams();
merkleRoot = BridgeUtils.calculateMerkleRoot(networkParameters, pmtSerialized, btcTxHash);
if (merkleRoot == null) {
return false;
}
} catch (VerificationException e) {
throw new BridgeIllegalArgumentException(e.getMessage(), e);
}
// Validates inputs count
logger.info("Going to validate inputs for btc tx {}", btcTxHash);
BridgeUtils.validateInputsCount(btcTxSerialized, activations.isActive(ConsensusRule.RSKIP143));
// Check the the merkle root equals merkle root of btc block at specified height in the btc best chain
// BTC blockstore is available since we've already queried the best chain height
logger.trace("Getting btc block at height: {}", height);
BtcBlock blockHeader = btcBlockStore.getStoredBlockAtMainChainHeight(height).getHeader();
logger.trace("Validating block merkle root at height: {}", height);
if (!isBlockMerkleRootValid(merkleRoot, blockHeader)) {
String panicMessage = String.format("Btc Tx %s Supplied merkle root %s does not match block's merkle root %s", btcTxHash.toString(), merkleRoot, blockHeader.getMerkleRoot());
logger.warn(panicMessage);
panicProcessor.panic("btclock", panicMessage);
return false;
}
return true;
}
use of co.rsk.bitcoinj.core.VerificationException in project rskj by rsksmart.
the class BridgeSupport method registerBtcCoinbaseTransaction.
public void registerBtcCoinbaseTransaction(byte[] btcTxSerialized, Sha256Hash blockHash, byte[] pmtSerialized, Sha256Hash witnessMerkleRoot, byte[] witnessReservedValue) throws VMException {
Context.propagate(btcContext);
try {
this.ensureBtcBlockStore();
} catch (BlockStoreException | IOException e) {
logger.warn("Exception in registerBtcCoinbaseTransaction", e);
throw new VMException("Exception in registerBtcCoinbaseTransaction", e);
}
Sha256Hash btcTxHash = BtcTransactionFormatUtils.calculateBtcTxHash(btcTxSerialized);
if (witnessReservedValue.length != 32) {
logger.warn("[btcTx:{}] WitnessResevedValue length can't be different than 32 bytes", btcTxHash);
throw new BridgeIllegalArgumentException("WitnessResevedValue length can't be different than 32 bytes");
}
if (!PartialMerkleTreeFormatUtils.hasExpectedSize(pmtSerialized)) {
logger.warn("[btcTx:{}] PartialMerkleTree doesn't have expected size", btcTxHash);
throw new BridgeIllegalArgumentException("PartialMerkleTree doesn't have expected size");
}
Sha256Hash merkleRoot;
try {
PartialMerkleTree pmt = new PartialMerkleTree(bridgeConstants.getBtcParams(), pmtSerialized, 0);
List<Sha256Hash> hashesInPmt = new ArrayList<>();
merkleRoot = pmt.getTxnHashAndMerkleRoot(hashesInPmt);
if (!hashesInPmt.contains(btcTxHash)) {
logger.warn("Supplied Btc Tx {} is not in the supplied partial merkle tree", btcTxHash);
return;
}
} catch (VerificationException e) {
logger.warn("[btcTx:{}] PartialMerkleTree could not be parsed", btcTxHash);
throw new BridgeIllegalArgumentException(String.format("PartialMerkleTree could not be parsed %s", ByteUtil.toHexString(pmtSerialized)), e);
}
// Check merkle root equals btc block merkle root at the specified height in the btc best chain
// Btc blockstore is available since we've already queried the best chain height
StoredBlock storedBlock = btcBlockStore.getFromCache(blockHash);
if (storedBlock == null) {
logger.warn("[btcTx:{}] Block not registered", btcTxHash);
throw new BridgeIllegalArgumentException(String.format("Block not registered %s", blockHash.toString()));
}
BtcBlock blockHeader = storedBlock.getHeader();
if (!blockHeader.getMerkleRoot().equals(merkleRoot)) {
String panicMessage = String.format("Btc Tx %s Supplied merkle root %s does not match block's merkle root %s", btcTxHash.toString(), merkleRoot, blockHeader.getMerkleRoot());
logger.warn(panicMessage);
panicProcessor.panic("btclock", panicMessage);
return;
}
BtcTransaction btcTx = new BtcTransaction(bridgeConstants.getBtcParams(), btcTxSerialized);
btcTx.verify();
Sha256Hash witnessCommitment = Sha256Hash.twiceOf(witnessMerkleRoot.getReversedBytes(), witnessReservedValue);
if (!witnessCommitment.equals(btcTx.findWitnessCommitment())) {
logger.warn("[btcTx:{}] WitnessCommitment does not match", btcTxHash);
throw new BridgeIllegalArgumentException("WitnessCommitment does not match");
}
CoinbaseInformation coinbaseInformation = new CoinbaseInformation(witnessMerkleRoot);
provider.setCoinbaseInformation(blockHeader.getHash(), coinbaseInformation);
logger.warn("[btcTx:{}] Registered coinbase information", btcTxHash);
}
Aggregations