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);
}
}
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);
}
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;
}
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);
}
}
}
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()));
}
Aggregations