Search in sources :

Example 21 with VMException

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);
}
Also used : BlockStoreException(co.rsk.bitcoinj.store.BlockStoreException) StoredBlock(co.rsk.bitcoinj.core.StoredBlock) CoinbaseInformation(co.rsk.peg.bitcoin.CoinbaseInformation) Sha256Hash(co.rsk.bitcoinj.core.Sha256Hash) BtcTransaction(co.rsk.bitcoinj.core.BtcTransaction) ArrayList(java.util.ArrayList) IOException(java.io.IOException) VMException(org.ethereum.vm.exception.VMException) PartialMerkleTree(co.rsk.bitcoinj.core.PartialMerkleTree) VerificationException(co.rsk.bitcoinj.core.VerificationException) BtcBlock(co.rsk.bitcoinj.core.BtcBlock)

Example 22 with VMException

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;
}
Also used : VMException(org.ethereum.vm.exception.VMException) BigInteger(java.math.BigInteger) VMException(org.ethereum.vm.exception.VMException) IOException(java.io.IOException) BlockStoreException(co.rsk.bitcoinj.store.BlockStoreException)

Example 23 with VMException

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);
    }
}
Also used : VMException(org.ethereum.vm.exception.VMException) VMException(org.ethereum.vm.exception.VMException) IOException(java.io.IOException) BlockStoreException(co.rsk.bitcoinj.store.BlockStoreException)

Example 24 with VMException

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();
}
Also used : VMException(org.ethereum.vm.exception.VMException) BigInteger(java.math.BigInteger) VMException(org.ethereum.vm.exception.VMException) IOException(java.io.IOException) BlockStoreException(co.rsk.bitcoinj.store.BlockStoreException)

Example 25 with VMException

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;
}
Also used : VMException(org.ethereum.vm.exception.VMException) BigInteger(java.math.BigInteger) VMException(org.ethereum.vm.exception.VMException) IOException(java.io.IOException) BlockStoreException(co.rsk.bitcoinj.store.BlockStoreException)

Aggregations

VMException (org.ethereum.vm.exception.VMException)29 IOException (java.io.IOException)16 BlockStoreException (co.rsk.bitcoinj.store.BlockStoreException)14 Test (org.junit.Test)11 BigInteger (java.math.BigInteger)9 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)9 SimpleBtcTransaction (co.rsk.peg.bitcoin.SimpleBtcTransaction)7 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)7 RskAddress (co.rsk.core.RskAddress)6 PrecompiledContracts (org.ethereum.vm.PrecompiledContracts)5 Keccak256 (co.rsk.crypto.Keccak256)4 co.rsk.bitcoinj.core (co.rsk.bitcoinj.core)3 BtcBlockStore (co.rsk.bitcoinj.store.BtcBlockStore)3 BridgeConstants (co.rsk.config.BridgeConstants)3 BridgeRegTestConstants (co.rsk.config.BridgeRegTestConstants)3 TestSystemProperties (co.rsk.config.TestSystemProperties)3 MerkleBranch (co.rsk.peg.bitcoin.MerkleBranch)3 OneOffWhiteListEntry (co.rsk.peg.whitelist.OneOffWhiteListEntry)3 StandardCharsets (java.nio.charset.StandardCharsets)3 Instant (java.time.Instant)3