use of org.ethereum.vm.exception.VMException 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);
}
use of org.ethereum.vm.exception.VMException in project rskj by rsksmart.
the class Bridge method getRetiringFederatorPublicKeyOfType.
public byte[] getRetiringFederatorPublicKeyOfType(Object[] args) throws VMException {
logger.trace("getRetiringFederatorPublicKeyOfType");
int index = ((BigInteger) args[0]).intValue();
FederationMember.KeyType keyType;
try {
keyType = FederationMember.KeyType.byValue((String) args[1]);
} catch (Exception e) {
logger.warn("Exception in getRetiringFederatorPublicKeyOfType", e);
throw new VMException("Exception in getRetiringFederatorPublicKeyOfType", e);
}
byte[] publicKey = bridgeSupport.getRetiringFederatorPublicKeyOfType(index, keyType);
if (publicKey == null) {
// Empty array is returned when public key is not found or there's no retiring federation
return new byte[] {};
}
return publicKey;
}
use of org.ethereum.vm.exception.VMException in project rskj by rsksmart.
the class Bridge method execute.
@Override
public byte[] execute(byte[] data) throws VMException {
try {
// Preliminary validation: the transaction on which we execute cannot be null
if (rskTx == null) {
throw new VMException("Rsk Transaction is null");
}
BridgeParsedData bridgeParsedData = parseData(data);
// Function parsing from data returned null => invalid function selected, halt!
if (bridgeParsedData == null) {
String errorMessage = String.format("Invalid data given: %s.", ByteUtil.toHexString(data));
logger.info(errorMessage);
if (activations.isActive(ConsensusRule.RSKIP88)) {
throw new BridgeIllegalArgumentException(errorMessage);
}
return null;
}
// allows for non-local calls
if (activations.isActive(ConsensusRule.RSKIP88) && !isLocalCall() && bridgeParsedData.bridgeMethod.onlyAllowsLocalCalls(this, bridgeParsedData.args)) {
String errorMessage = String.format("Non-local-call to %s. Returning without execution.", bridgeParsedData.bridgeMethod.getFunction().name);
logger.info(errorMessage);
throw new BridgeIllegalArgumentException(errorMessage);
}
Optional<?> result;
try {
// bridgeParsedData.function should be one of the CallTransaction.Function declared above.
// If the user tries to call an non-existent function, parseData() will return null.
result = bridgeParsedData.bridgeMethod.getExecutor().execute(this, bridgeParsedData.args);
} catch (BridgeIllegalArgumentException ex) {
String errorMessage = String.format("Error executing: %s", bridgeParsedData.bridgeMethod);
logger.warn(errorMessage, ex);
if (activations.isActive(ConsensusRule.RSKIP88)) {
throw new BridgeIllegalArgumentException(errorMessage);
}
return null;
}
teardown();
return result.map(bridgeParsedData.bridgeMethod.getFunction()::encodeOutputs).orElse(null);
} catch (Exception ex) {
logger.error(ex.getMessage(), ex);
panicProcessor.panic("bridgeexecute", ex.getMessage());
throw new VMException(String.format("Exception executing bridge: %s", ex.getMessage()), ex);
}
}
use of org.ethereum.vm.exception.VMException in project rskj by rsksmart.
the class Bridge method getBtcBlockchainBlockHashAtDepth.
public byte[] getBtcBlockchainBlockHashAtDepth(Object[] args) throws VMException {
logger.trace("getBtcBlockchainBlockHashAtDepth");
int depth = ((BigInteger) args[0]).intValue();
Sha256Hash blockHash = null;
try {
blockHash = bridgeSupport.getBtcBlockchainBlockHashAtDepth(depth);
} catch (Exception e) {
logger.warn("Exception in getBtcBlockchainBlockHashAtDepth", e);
throw new VMException("Exception in getBtcBlockchainBlockHashAtDepth", e);
}
return blockHash.getBytes();
}
use of org.ethereum.vm.exception.VMException in project rskj by rsksmart.
the class Bridge method getPendingFederatorPublicKeyOfType.
public byte[] getPendingFederatorPublicKeyOfType(Object[] args) throws VMException {
logger.trace("getPendingFederatorPublicKeyOfType");
int index = ((BigInteger) args[0]).intValue();
FederationMember.KeyType keyType;
try {
keyType = FederationMember.KeyType.byValue((String) args[1]);
} catch (Exception e) {
logger.warn("Exception in getPendingFederatorPublicKeyOfType", e);
throw new VMException("Exception in getPendingFederatorPublicKeyOfType", e);
}
byte[] publicKey = bridgeSupport.getPendingFederatorPublicKeyOfType(index, keyType);
if (publicKey == null) {
// Empty array is returned when public key is not found
return new byte[] {};
}
return publicKey;
}
Aggregations