use of co.rsk.bitcoinj.store.BlockStoreException in project rskj by rsksmart.
the class SimpleBlockChain method add.
@Override
public boolean add(BtcBlock block) {
StoredBlock sblock = new StoredBlock(block, BigInteger.ONE, 1);
try {
this.blockStore.put(sblock);
this.block = sblock;
this.setChainHead(sblock);
} catch (BlockStoreException e) {
e.printStackTrace();
return false;
}
return true;
}
use of co.rsk.bitcoinj.store.BlockStoreException 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;
}
use of co.rsk.bitcoinj.store.BlockStoreException in project rskj by rsksmart.
the class RepositoryBtcBlockStoreWithCacheTest method getStoredBlockAtMainChainDepth_Error.
@Test
public void getStoredBlockAtMainChainDepth_Error() throws BlockStoreException {
BtcBlockStoreWithCache btcBlockStore = createBlockStore();
BtcBlock parent = networkParameters.getGenesisBlock();
BtcBlock blockHeader1 = new BtcBlock(networkParameters, 2L, parent.getHash(), Sha256Hash.ZERO_HASH, parent.getTimeSeconds() + 1, parent.getDifficultyTarget(), 0, new ArrayList<>());
StoredBlock storedBlock1 = new StoredBlock(blockHeader1, new BigInteger("0"), 2);
btcBlockStore.put(storedBlock1);
parent = blockHeader1;
BtcBlock blockHeader2 = new BtcBlock(networkParameters, 2L, parent.getHash(), Sha256Hash.ZERO_HASH, parent.getTimeSeconds() + 1, parent.getDifficultyTarget(), 0, new ArrayList<>());
StoredBlock storedBlock2 = new StoredBlock(blockHeader2, new BigInteger("0"), 2);
btcBlockStore.put(storedBlock2);
btcBlockStore.setChainHead(storedBlock2);
// getStoredBlockAtMainChainDepth should fail as the block is at a inconsistent height
try {
btcBlockStore.getStoredBlockAtMainChainDepth(1);
fail();
} catch (BlockStoreException e) {
assertTrue(true);
}
// getStoredBlockAtMainChainHeight should fail as the block is at a inconsistent height
try {
btcBlockStore.getStoredBlockAtMainChainHeight(1);
fail();
} catch (BlockStoreException e) {
assertTrue(true);
}
}
use of co.rsk.bitcoinj.store.BlockStoreException in project rskj by rsksmart.
the class ReceiveHeaderTest method receiveHeader_success.
private void receiveHeader_success(int times, ExecutionStats stats) throws VMException {
int minBlocks = 10;
int maxBlocks = 100;
BridgeStorageProviderInitializer storageInitializer = generateInitializer(minBlocks, maxBlocks);
executeAndAverage("receiveHeader_success", times, // false to use an existed block (create new block)
getABIEncoder(false), storageInitializer, Helper.getZeroValueRandomSenderTxBuilder(), Helper.getRandomHeightProvider(10), stats, (environment, executionResult) -> {
// Working fine.
int result = new BigInteger(executionResult).intValue();
Assert.assertEquals(0, result);
BridgeStorageProvider bridgeStorageProvider = new BridgeStorageProvider((Repository) environment.getBenchmarkedRepository(), PrecompiledContracts.BRIDGE_ADDR, constants.getBridgeConstants(), activationConfig.forBlock(0));
// Best Block is the new block added.
BtcBlockStore btcBlockStore = new RepositoryBtcBlockStoreWithCache.Factory(networkParameters).newInstance((Repository) environment.getBenchmarkedRepository(), bridgeConstants, bridgeStorageProvider, null);
Sha256Hash bestBlockHash = null;
try {
bestBlockHash = btcBlockStore.getChainHead().getHeader().getHash();
} catch (BlockStoreException e) {
Assert.fail(e.getMessage());
}
Assert.assertEquals(expectedBlock.getHash(), bestBlockHash);
});
}
use of co.rsk.bitcoinj.store.BlockStoreException in project rskj by rsksmart.
the class BridgeSupportTestPowerMock method getBtcTransactionConfirmations_heightInconsistency.
@Test
public void getBtcTransactionConfirmations_heightInconsistency() throws BlockStoreException, IOException {
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
Repository repository = createRepository();
Repository track = repository.startTracking();
Sha256Hash blockHash = Sha256Hash.of(Hex.decode("aabbcc"));
BtcBlock blockHeader = mock(BtcBlock.class);
when(blockHeader.getHash()).thenReturn(blockHash);
int height = 50;
StoredBlock block = new StoredBlock(blockHeader, new BigInteger("0"), height);
BtcBlockStoreWithCache btcBlockStore = mock(BtcBlockStoreWithCache.class);
when(btcBlockStore.getFromCache(blockHash)).thenReturn(block);
StoredBlock chainHead = new StoredBlock(blockHeader, new BigInteger("0"), 132);
when(btcBlockStore.getChainHead()).thenReturn(chainHead);
when(btcBlockStore.getStoredBlockAtMainChainHeight(block.getHeight())).thenThrow(new BlockStoreException("blah"));
BridgeStorageProvider provider = new BridgeStorageProvider(track, PrecompiledContracts.BRIDGE_ADDR, bridgeConstants, activationsBeforeForks);
BtcBlockStoreWithCache.Factory mockFactory = mock(BtcBlockStoreWithCache.Factory.class);
when(mockFactory.newInstance(track, bridgeConstants, provider, activations)).thenReturn(btcBlockStore);
Sha256Hash btcTransactionHash = Sha256Hash.of(Hex.decode("112233"));
BridgeSupport bridgeSupport = getBridgeSupport(provider, track, mockFactory, activations);
int confirmations = bridgeSupport.getBtcTransactionConfirmations(btcTransactionHash, blockHash, null);
Assert.assertEquals(BridgeSupport.BTC_TRANSACTION_CONFIRMATION_INCONSISTENT_BLOCK_ERROR_CODE.intValue(), confirmations);
}
Aggregations