Search in sources :

Example 1 with PartialMerkleTree

use of co.rsk.bitcoinj.core.PartialMerkleTree in project rskj by rsksmart.

the class GenesisMerkleProofValidator method isValid.

@Override
public boolean isValid(Sha256Hash expectedRoot, Sha256Hash coinbaseHash) {
    PartialMerkleTree merkleTree = new PartialMerkleTree(bitcoinNetworkParameters, pmtSerialized, 0);
    List<Sha256Hash> txHashes = new ArrayList<>();
    Sha256Hash root = merkleTree.getTxnHashAndMerkleRoot(txHashes);
    return root.equals(expectedRoot) && txHashes.contains(coinbaseHash);
}
Also used : Sha256Hash(co.rsk.bitcoinj.core.Sha256Hash) PartialMerkleTree(co.rsk.bitcoinj.core.PartialMerkleTree) ArrayList(java.util.ArrayList)

Example 2 with PartialMerkleTree

use of co.rsk.bitcoinj.core.PartialMerkleTree in project rskj by rsksmart.

the class BridgeUtilsTest method calculateMerkleRoot_hashes_in_pmt.

@Test
public void calculateMerkleRoot_hashes_in_pmt() {
    BtcTransaction tx = new BtcTransaction(networkParameters);
    byte[] bits = new byte[1];
    bits[0] = 0x5;
    List<Sha256Hash> hashes = new ArrayList<>();
    hashes.add(Sha256Hash.ZERO_HASH);
    hashes.add(tx.getHash());
    PartialMerkleTree pmt = new PartialMerkleTree(networkParameters, bits, hashes, 2);
    Sha256Hash merkleRoot = BridgeUtils.calculateMerkleRoot(networkParameters, pmt.bitcoinSerialize(), tx.getHash());
    Assert.assertNotNull(merkleRoot);
}
Also used : BtcTransaction(co.rsk.bitcoinj.core.BtcTransaction) Sha256Hash(co.rsk.bitcoinj.core.Sha256Hash) ArrayList(java.util.ArrayList) PartialMerkleTree(co.rsk.bitcoinj.core.PartialMerkleTree) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) Test(org.junit.Test)

Example 3 with PartialMerkleTree

use of co.rsk.bitcoinj.core.PartialMerkleTree in project rskj by rsksmart.

the class BridgeUtilsTest method calculateMerkleRoot_hashes_not_in_pmt.

@Test
public void calculateMerkleRoot_hashes_not_in_pmt() {
    byte[] bits = new byte[1];
    bits[0] = 0x01;
    List<Sha256Hash> hashes = new ArrayList<>();
    hashes.add(PegTestUtils.createHash(2));
    BtcTransaction tx = new BtcTransaction(networkParameters);
    PartialMerkleTree pmt = new PartialMerkleTree(networkParameters, bits, hashes, 1);
    Assert.assertNull(BridgeUtils.calculateMerkleRoot(networkParameters, pmt.bitcoinSerialize(), tx.getHash()));
}
Also used : Sha256Hash(co.rsk.bitcoinj.core.Sha256Hash) BtcTransaction(co.rsk.bitcoinj.core.BtcTransaction) ArrayList(java.util.ArrayList) PartialMerkleTree(co.rsk.bitcoinj.core.PartialMerkleTree) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) Test(org.junit.Test)

Example 4 with PartialMerkleTree

use of co.rsk.bitcoinj.core.PartialMerkleTree 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)

Aggregations

PartialMerkleTree (co.rsk.bitcoinj.core.PartialMerkleTree)4 Sha256Hash (co.rsk.bitcoinj.core.Sha256Hash)4 ArrayList (java.util.ArrayList)4 BtcTransaction (co.rsk.bitcoinj.core.BtcTransaction)3 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)2 Test (org.junit.Test)2 BtcBlock (co.rsk.bitcoinj.core.BtcBlock)1 StoredBlock (co.rsk.bitcoinj.core.StoredBlock)1 VerificationException (co.rsk.bitcoinj.core.VerificationException)1 BlockStoreException (co.rsk.bitcoinj.store.BlockStoreException)1 CoinbaseInformation (co.rsk.peg.bitcoin.CoinbaseInformation)1 IOException (java.io.IOException)1 VMException (org.ethereum.vm.exception.VMException)1