Search in sources :

Example 1 with StoredBlock

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

the class RepositoryBtcBlockStoreWithCache method checkIfInitialized.

private void checkIfInitialized() {
    if (getChainHead() == null) {
        BtcBlock genesisHeader = this.btcNetworkParams.getGenesisBlock().cloneAsHeader();
        StoredBlock storedGenesis = new StoredBlock(genesisHeader, genesisHeader.getWork(), 0);
        put(storedGenesis);
        setChainHead(storedGenesis);
    }
}
Also used : StoredBlock(co.rsk.bitcoinj.core.StoredBlock) BtcBlock(co.rsk.bitcoinj.core.BtcBlock)

Example 2 with StoredBlock

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

the class RepositoryBtcBlockStoreWithCache method getInMainchain.

@Override
public Optional<StoredBlock> getInMainchain(int height) {
    Optional<Sha256Hash> bestBlockHash = bridgeStorageProvider.getBtcBestBlockHashByHeight(height);
    if (!bestBlockHash.isPresent()) {
        logger.trace("[getInMainchain] Block at height {} not present in storage", height);
        return Optional.empty();
    }
    StoredBlock block = get(bestBlockHash.get());
    if (block == null) {
        logger.trace("[getInMainchain] Block with hash {} not found in storage", bestBlockHash.get());
        return Optional.empty();
    }
    logger.trace("[getInMainchain] Found block with hash {} at height {}", bestBlockHash.get(), height);
    return Optional.of(block);
}
Also used : StoredBlock(co.rsk.bitcoinj.core.StoredBlock) Sha256Hash(co.rsk.bitcoinj.core.Sha256Hash)

Example 3 with StoredBlock

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

the class RepositoryBtcBlockStoreWithCache method getStoredBlockAtMainChainHeight.

@Override
public StoredBlock getStoredBlockAtMainChainHeight(int height) throws BlockStoreException {
    StoredBlock chainHead = getChainHead();
    int depth = chainHead.getHeight() - height;
    logger.trace("Getting btc block at depth: {}", depth);
    if (depth < 0) {
        String message = String.format("Height provided is higher than chain head. provided: %n. chain head: %n", height, chainHead.getHeight());
        logger.trace("[getStoredBlockAtMainChainHeight] {}", message);
        throw new BlockStoreException(message);
    }
    if (activations.isActive(ConsensusRule.RSKIP199)) {
        int btcHeightWhenBlockIndexActivates = this.bridgeConstants.getBtcHeightWhenBlockIndexActivates();
        int maxDepthToSearch = this.bridgeConstants.getMaxDepthToSearchBlocksBelowIndexActivation();
        int limit;
        if (chainHead.getHeight() - btcHeightWhenBlockIndexActivates > maxDepthToSearch) {
            limit = btcHeightWhenBlockIndexActivates;
        } else {
            limit = chainHead.getHeight() - maxDepthToSearch;
        }
        logger.trace("[getStoredBlockAtMainChainHeight] Chain head height is {} and the depth limit {}", chainHead.getHeight(), limit);
        if (height < limit) {
            String message = String.format("Height provided is lower than the depth limit defined to search for blocks. Provided: %n, limit: %n", height, limit);
            logger.trace("[getStoredBlockAtMainChainHeight] {}", message);
            throw new BlockStoreException(message);
        }
    }
    StoredBlock block;
    Optional<StoredBlock> blockOptional = getInMainchain(height);
    if (blockOptional.isPresent()) {
        block = blockOptional.get();
    } else {
        block = getStoredBlockAtMainChainDepth(depth);
    }
    return block;
}
Also used : StoredBlock(co.rsk.bitcoinj.core.StoredBlock) BlockStoreException(co.rsk.bitcoinj.store.BlockStoreException)

Example 4 with StoredBlock

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

the class RepositoryBtcBlockStoreWithCache method put.

@Override
public synchronized void put(StoredBlock storedBlock) {
    Sha256Hash hash = storedBlock.getHeader().getHash();
    byte[] ba = storedBlockToByteArray(storedBlock);
    repository.addStorageBytes(contractAddress, DataWord.valueFromHex(hash.toString()), ba);
    if (cacheBlocks != null) {
        StoredBlock chainHead = getChainHead();
        if (chainHead == null || chainHead.getHeight() - storedBlock.getHeight() < this.maxDepthBlockCache) {
            cacheBlocks.put(storedBlock.getHeader().getHash(), storedBlock);
        }
    }
}
Also used : StoredBlock(co.rsk.bitcoinj.core.StoredBlock) Sha256Hash(co.rsk.bitcoinj.core.Sha256Hash)

Example 5 with StoredBlock

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

the class BridgeSupport method getBtcBlockchainParentBlockHeaderByHash.

public byte[] getBtcBlockchainParentBlockHeaderByHash(Sha256Hash hash) throws IOException, BlockStoreException {
    this.ensureBtcBlockStore();
    StoredBlock block = btcBlockStore.get(hash);
    if (block == null) {
        return ByteUtil.EMPTY_BYTE_ARRAY;
    }
    return serializeBlockHeader(btcBlockStore.get(block.getHeader().getPrevBlockHash()));
}
Also used : StoredBlock(co.rsk.bitcoinj.core.StoredBlock)

Aggregations

StoredBlock (co.rsk.bitcoinj.core.StoredBlock)33 BtcBlock (co.rsk.bitcoinj.core.BtcBlock)16 Sha256Hash (co.rsk.bitcoinj.core.Sha256Hash)15 Test (org.junit.Test)15 BlockStoreException (co.rsk.bitcoinj.store.BlockStoreException)9 Repository (org.ethereum.core.Repository)9 MutableRepository (org.ethereum.db.MutableRepository)8 ActivationConfig (org.ethereum.config.blockchain.upgrades.ActivationConfig)5 IOException (java.io.IOException)4 VerificationException (co.rsk.bitcoinj.core.VerificationException)3 InputStream (java.io.InputStream)3 BigInteger (java.math.BigInteger)3 VMException (org.ethereum.vm.exception.VMException)3 AddressFormatException (co.rsk.bitcoinj.core.AddressFormatException)2 InsufficientMoneyException (co.rsk.bitcoinj.core.InsufficientMoneyException)2 UTXOProviderException (co.rsk.bitcoinj.core.UTXOProviderException)2 PeginInstructionsException (co.rsk.peg.pegininstructions.PeginInstructionsException)2 ObjectInputStream (java.io.ObjectInputStream)2 ArrayList (java.util.ArrayList)2 Triple (org.apache.commons.lang3.tuple.Triple)2