Search in sources :

Example 36 with Sha256Hash

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

the class RepositoryBtcBlockStoreWithCache method populateCache.

private synchronized void populateCache(StoredBlock chainHead) {
    logger.trace("Populating BTC Block Store Cache.");
    if (this.btcNetworkParams.getGenesisBlock().equals(chainHead.getHeader())) {
        return;
    }
    cacheBlocks.put(chainHead.getHeader().getHash(), chainHead);
    Sha256Hash blockHash = chainHead.getHeader().getPrevBlockHash();
    int depth = this.maxDepthBlockCache - 1;
    while (blockHash != null && depth > 0) {
        if (cacheBlocks.get(blockHash) != null) {
            break;
        }
        StoredBlock currentBlock = get(blockHash);
        if (currentBlock == null) {
            break;
        }
        cacheBlocks.put(currentBlock.getHeader().getHash(), currentBlock);
        depth--;
        blockHash = currentBlock.getHeader().getPrevBlockHash();
    }
    logger.trace("END Populating BTC Block Store Cache.");
}
Also used : StoredBlock(co.rsk.bitcoinj.core.StoredBlock) Sha256Hash(co.rsk.bitcoinj.core.Sha256Hash)

Example 37 with Sha256Hash

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

the class RepositoryBtcBlockStoreWithCache method getStoredBlockAtMainChainDepth.

@Override
@Deprecated
public StoredBlock getStoredBlockAtMainChainDepth(int depth) throws BlockStoreException {
    logger.trace("[getStoredBlockAtMainChainDepth] Looking for block at depth {}", depth);
    StoredBlock chainHead = getChainHead();
    Sha256Hash blockHash = chainHead.getHeader().getHash();
    for (int i = 0; i < depth && blockHash != null; i++) {
        // If its older than cache go to disk
        StoredBlock currentBlock = getFromCache(blockHash);
        if (currentBlock == null) {
            logger.trace("[getStoredBlockAtMainChainDepth] Block with hash {} not in cache, getting from disk", blockHash);
            currentBlock = get(blockHash);
            if (currentBlock == null) {
                return null;
            }
        }
        blockHash = currentBlock.getHeader().getPrevBlockHash();
    }
    if (blockHash == null) {
        logger.trace("[getStoredBlockAtMainChainDepth] Block not found");
        return null;
    }
    StoredBlock block = getFromCache(blockHash);
    if (block == null) {
        block = get(blockHash);
    }
    int expectedHeight = chainHead.getHeight() - depth;
    if (block != null && block.getHeight() != expectedHeight) {
        String message = String.format("Block %s at depth %d Height is %d but should be %d", block.getHeader().getHash(), depth, block.getHeight(), expectedHeight);
        logger.trace("[getStoredBlockAtMainChainDepth] {}", message);
        throw new BlockStoreException(message);
    }
    return block;
}
Also used : StoredBlock(co.rsk.bitcoinj.core.StoredBlock) BlockStoreException(co.rsk.bitcoinj.store.BlockStoreException) Sha256Hash(co.rsk.bitcoinj.core.Sha256Hash)

Example 38 with Sha256Hash

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

the class Rskip92MerkleProofValidatorTests method isValid_PassInvalidRootHash_ShouldReturnFalse.

@Test
public void isValid_PassInvalidRootHash_ShouldReturnFalse() {
    List<byte[]> hashList = makeHashList();
    byte[] pmtSerialized = join(hashList);
    Sha256Hash coinbaseHash = Sha256Hash.wrap(hashList.get(0));
    Rskip92MerkleProofValidator rskip92MerkleProofValidator = new Rskip92MerkleProofValidator(pmtSerialized, true);
    Sha256Hash rootHash = Sha256Hash.wrap(new byte[Sha256Hash.LENGTH]);
    boolean actualResult = rskip92MerkleProofValidator.isValid(rootHash, coinbaseHash);
    assertFalse(actualResult);
}
Also used : Rskip92MerkleProofValidator(co.rsk.validators.Rskip92MerkleProofValidator) Sha256Hash(co.rsk.bitcoinj.core.Sha256Hash) Test(org.junit.Test)

Example 39 with Sha256Hash

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

the class RepositoryBlockStoreTest method test.

@Test
public void test() throws Exception {
    // This Is how I produced RepositoryBlockStore_data.ser. I had a bitcoind in regtest with 613 blocks + genesis block
    // NetworkParameters params = RegTestParams.get();
    // Context context = new Context(params);
    // Wallet wallet = new Wallet(context);
    // BlockStore store = new SPVBlockStore(params, new File("spvBlockstore"));
    // AbstractBlockChain chain = new BlockChain(context, wallet, store);
    // PeerGroup peerGroup = new PeerGroup(context, chain);
    // peerGroup.start();
    // final DownloadProgressTracker listener = new DownloadProgressTracker();
    // peerGroup.startBlockChainDownload(listener);
    // listener.await();
    // peerGroup.stop();
    // StoredBlock storedBlock = chain.getChainHead();
    // FileOutputStream fos = new FileOutputStream("RepositoryBlockStore_data.ser");
    // ObjectOutputStream oos = new ObjectOutputStream(fos);
    // for (int i = 0; i < 614; i++) {
    // Triple<byte[], BigInteger , Integer> tripleStoredBlock = new ImmutableTriple<>(storedBlock.getHeader().bitcoinSerialize(), storedBlock.getChainWork(), storedBlock.getHeight());
    // oos.writeObject(tripleStoredBlock);
    // storedBlock = store.get(storedBlock.getHeader().getPrevBlockHash());
    // }
    // oos.close();
    // Read original store
    InputStream fileInputStream = ClassLoader.getSystemResourceAsStream("peg/RepositoryBlockStore_data.ser");
    ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
    Repository repository = new RepositoryImplForTesting();
    RskSystemProperties config = new RskSystemProperties();
    RepositoryBlockStore store = new RepositoryBlockStore(config, repository, PrecompiledContracts.BRIDGE_ADDR);
    for (int i = 0; i < 614; i++) {
        Triple<byte[], BigInteger, Integer> tripleStoredBlock = (Triple<byte[], BigInteger, Integer>) objectInputStream.readObject();
        BtcBlock header = RegTestParams.get().getDefaultSerializer().makeBlock(tripleStoredBlock.getLeft());
        StoredBlock storedBlock = new StoredBlock(header, tripleStoredBlock.getMiddle(), tripleStoredBlock.getRight());
        if (i == 0) {
            store.setChainHead(storedBlock);
        }
        store.put(storedBlock);
    }
    // Create a new instance of the store
    RepositoryBlockStore store2 = new RepositoryBlockStore(config, repository, PrecompiledContracts.BRIDGE_ADDR);
    // Check a specific block that used to fail when we had a bug
    assertEquals(store.get(Sha256Hash.wrap("373941fe83961cf70e181e468abc5f9f7cc440c711c3d06948fa66f3912ed27a")), store2.get(Sha256Hash.wrap("373941fe83961cf70e181e468abc5f9f7cc440c711c3d06948fa66f3912ed27a")));
    // Check new instance content is identical to the original one
    StoredBlock storedBlock = store.getChainHead();
    StoredBlock storedBlock2 = store2.getChainHead();
    int headHeight = storedBlock.getHeight();
    for (int i = 0; i < headHeight; i++) {
        assertNotNull(storedBlock);
        assertEquals(storedBlock, storedBlock2);
        Sha256Hash prevBlockHash = storedBlock.getHeader().getPrevBlockHash();
        storedBlock = store.get(prevBlockHash);
        storedBlock2 = store2.get(prevBlockHash);
    }
}
Also used : StoredBlock(co.rsk.bitcoinj.core.StoredBlock) ObjectInputStream(java.io.ObjectInputStream) InputStream(java.io.InputStream) Sha256Hash(co.rsk.bitcoinj.core.Sha256Hash) RepositoryImplForTesting(co.rsk.db.RepositoryImplForTesting) BigInteger(java.math.BigInteger) Triple(org.apache.commons.lang3.tuple.Triple) Repository(org.ethereum.core.Repository) BigInteger(java.math.BigInteger) BtcBlock(co.rsk.bitcoinj.core.BtcBlock) RskSystemProperties(co.rsk.config.RskSystemProperties) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 40 with Sha256Hash

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

the class BridgeState method create.

public static BridgeState create(BridgeConstants bridgeConstants, byte[] data) throws IOException {
    RLPList rlpList = (RLPList) RLP.decode2(data).get(0);
    byte[] btcBlockchainBestChainHeightBytes = rlpList.get(0).getRLPData();
    int btcBlockchainBestChainHeight = btcBlockchainBestChainHeightBytes == null ? 0 : (new BigInteger(1, btcBlockchainBestChainHeightBytes)).intValue();
    byte[] btcTxHashesAlreadyProcessedBytes = rlpList.get(1).getRLPData();
    Map<Sha256Hash, Long> btcTxHashesAlreadyProcessed = BridgeSerializationUtils.deserializeMapOfHashesToLong(btcTxHashesAlreadyProcessedBytes);
    byte[] btcUTXOsBytes = rlpList.get(2).getRLPData();
    List<UTXO> btcUTXOs = BridgeSerializationUtils.deserializeUTXOList(btcUTXOsBytes);
    byte[] rskTxsWaitingForSignaturesBytes = rlpList.get(3).getRLPData();
    SortedMap<Keccak256, BtcTransaction> rskTxsWaitingForSignatures = BridgeSerializationUtils.deserializeMap(rskTxsWaitingForSignaturesBytes, bridgeConstants.getBtcParams(), false);
    byte[] releaseRequestQueueBytes = rlpList.get(4).getRLPData();
    ReleaseRequestQueue releaseRequestQueue = BridgeSerializationUtils.deserializeReleaseRequestQueue(releaseRequestQueueBytes, bridgeConstants.getBtcParams());
    byte[] releaseTransactionSetBytes = rlpList.get(5).getRLPData();
    ReleaseTransactionSet releaseTransactionSet = BridgeSerializationUtils.deserializeReleaseTransactionSet(releaseTransactionSetBytes, bridgeConstants.getBtcParams());
    return new BridgeState(btcBlockchainBestChainHeight, btcTxHashesAlreadyProcessed, btcUTXOs, rskTxsWaitingForSignatures, releaseRequestQueue, releaseTransactionSet);
}
Also used : Sha256Hash(co.rsk.bitcoinj.core.Sha256Hash) BtcTransaction(co.rsk.bitcoinj.core.BtcTransaction) Keccak256(co.rsk.crypto.Keccak256) RLPList(org.ethereum.util.RLPList) UTXO(co.rsk.bitcoinj.core.UTXO) BigInteger(java.math.BigInteger)

Aggregations

Sha256Hash (co.rsk.bitcoinj.core.Sha256Hash)40 Test (org.junit.Test)17 StoredBlock (co.rsk.bitcoinj.core.StoredBlock)15 BtcTransaction (co.rsk.bitcoinj.core.BtcTransaction)11 BtcBlock (co.rsk.bitcoinj.core.BtcBlock)9 Script (co.rsk.bitcoinj.script.Script)7 BlockStoreException (co.rsk.bitcoinj.store.BlockStoreException)7 Repository (org.ethereum.core.Repository)7 BtcECKey (co.rsk.bitcoinj.core.BtcECKey)6 TransactionOutPoint (co.rsk.bitcoinj.core.TransactionOutPoint)6 ArrayList (java.util.ArrayList)6 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)6 IOException (java.io.IOException)5 PartialMerkleTree (co.rsk.bitcoinj.core.PartialMerkleTree)4 TransactionInput (co.rsk.bitcoinj.core.TransactionInput)4 TransactionSignature (co.rsk.bitcoinj.crypto.TransactionSignature)4 ScriptChunk (co.rsk.bitcoinj.script.ScriptChunk)4 BigInteger (java.math.BigInteger)4 MutableRepository (org.ethereum.db.MutableRepository)4 VerificationException (co.rsk.bitcoinj.core.VerificationException)3