use of co.rsk.bitcoinj.core.StoredBlock in project rskj by rsksmart.
the class RepositoryBtcBlockStoreWithCacheTest method ifCacheNullAlwaysGoToDisk.
@Test
public void ifCacheNullAlwaysGoToDisk() throws BlockStoreException {
Repository repository = createRepository();
BtcBlockStoreWithCache btcBlockStore = new RepositoryBtcBlockStoreWithCache(networkParameters, repository.startTracking(), null, PrecompiledContracts.BRIDGE_ADDR, bridgeConstants, mock(BridgeStorageProvider.class), mock(ActivationConfig.ForBlock.class));
BtcBlock genesis = networkParameters.getGenesisBlock();
StoredBlock firstStoredBlock = createStoredBlock(genesis, 1, 0);
Sha256Hash firstBlockHash = firstStoredBlock.getHeader().getHash();
btcBlockStore.put(firstStoredBlock);
btcBlockStore.setChainHead(firstStoredBlock);
assertEquals(firstStoredBlock, btcBlockStore.get(firstBlockHash));
assertNull(btcBlockStore.getFromCache(firstBlockHash));
assertEquals(firstStoredBlock, btcBlockStore.getChainHead());
assertEquals(genesis, btcBlockStore.getStoredBlockAtMainChainDepth(1).getHeader());
assertEquals(genesis, btcBlockStore.getStoredBlockAtMainChainHeight(0).getHeader());
}
use of co.rsk.bitcoinj.core.StoredBlock in project rskj by rsksmart.
the class RepositoryBtcBlockStoreWithCacheTest method getInMainchain_hashNotFound.
@Test
public void getInMainchain_hashNotFound() {
Repository repository = createRepository();
BtcBlockStoreWithCache.Factory btcBlockStoreFactory = new RepositoryBtcBlockStoreWithCache.Factory(bridgeConstants.getBtcParams());
int blockHeight = 100;
Sha256Hash blockHash = PegTestUtils.createHash(2);
BridgeStorageProvider provider = mock(BridgeStorageProvider.class);
when(provider.getBtcBestBlockHashByHeight(blockHeight)).thenReturn(Optional.of(blockHash));
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
BtcBlockStoreWithCache btcBlockStore = btcBlockStoreFactory.newInstance(repository, bridgeConstants, provider, activations);
Optional<StoredBlock> blockOptional = btcBlockStore.getInMainchain(blockHeight);
Assert.assertFalse(blockOptional.isPresent());
}
use of co.rsk.bitcoinj.core.StoredBlock in project rskj by rsksmart.
the class RepositoryBtcBlockStoreWithCacheTest method put_oldBlockShouldNotGoToCache.
@Test
public void put_oldBlockShouldNotGoToCache() throws BlockStoreException {
BtcBlockStoreWithCache btcBlockStore = createBlockStore();
BtcBlock genesis = networkParameters.getGenesisBlock();
// Set chain head at height 6000
StoredBlock storedBlock1 = createStoredBlock(genesis, 6000, 0);
Sha256Hash firstBlockHash = storedBlock1.getHeader().getHash();
btcBlockStore.put(storedBlock1);
btcBlockStore.setChainHead(storedBlock1);
// Store a block of height 1 which is lesser than chainHead - 5000
StoredBlock storedBlock2 = createStoredBlock(genesis, 1, 1);
Sha256Hash secondBlockHash = storedBlock2.getHeader().getHash();
btcBlockStore.put(storedBlock2);
assertEquals(storedBlock1, btcBlockStore.getFromCache(firstBlockHash));
assertNull(btcBlockStore.getFromCache(secondBlockHash));
assertEquals(storedBlock1, btcBlockStore.get(firstBlockHash));
assertEquals(storedBlock2, btcBlockStore.get(secondBlockHash));
assertEquals(storedBlock1, btcBlockStore.getChainHead());
}
use of co.rsk.bitcoinj.core.StoredBlock in project rskj by rsksmart.
the class RepositoryBtcBlockStoreWithCacheTest method getStoredBlockAtMainChainDepth.
@Test
public void getStoredBlockAtMainChainDepth() throws BlockStoreException {
BtcBlockStoreWithCache btcBlockStore = createBlockStore();
BtcBlock genesis = networkParameters.getGenesisBlock();
StoredBlock storedBlock1 = createStoredBlock(genesis, 1, 0);
btcBlockStore.put(storedBlock1);
StoredBlock storedBlock2 = createStoredBlock(storedBlock1.getHeader(), 2, 0);
btcBlockStore.put(storedBlock2);
StoredBlock storedBlock3 = createStoredBlock(storedBlock2.getHeader(), 3, 0);
btcBlockStore.put(storedBlock3);
StoredBlock storedBlock4 = createStoredBlock(storedBlock3.getHeader(), 4, 0);
btcBlockStore.put(storedBlock4);
btcBlockStore.setChainHead(storedBlock4);
assertEquals(storedBlock4, btcBlockStore.getChainHead());
int maxHeight = storedBlock4.getHeight();
// Check getStoredBlockAtMainChainDepth
assertEquals(storedBlock3, btcBlockStore.getStoredBlockAtMainChainDepth(maxHeight - storedBlock3.getHeight()));
assertEquals(storedBlock2, btcBlockStore.getStoredBlockAtMainChainDepth(maxHeight - storedBlock2.getHeight()));
assertEquals(storedBlock1, btcBlockStore.getStoredBlockAtMainChainDepth(maxHeight - storedBlock1.getHeight()));
assertEquals(genesis, btcBlockStore.getStoredBlockAtMainChainDepth(maxHeight).getHeader());
}
use of co.rsk.bitcoinj.core.StoredBlock in project rskj by rsksmart.
the class RepositoryBtcBlockStoreWithCacheTest method getStoredBlockAtMainChainHeight_postIris_heightLowerThanMaxDepth_limitInChainHeadMinusMaxDepthToSearch.
@Test(expected = BlockStoreException.class)
public void getStoredBlockAtMainChainHeight_postIris_heightLowerThanMaxDepth_limitInChainHeadMinusMaxDepthToSearch() throws BlockStoreException {
Repository repository = createRepository();
BtcBlockStoreWithCache.Factory btcBlockStoreFactory = new RepositoryBtcBlockStoreWithCache.Factory(bridgeConstants.getBtcParams());
BridgeStorageProvider provider = mock(BridgeStorageProvider.class);
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
when(activations.isActive(ConsensusRule.RSKIP199)).thenReturn(true);
BtcBlockStoreWithCache btcBlockStore = btcBlockStoreFactory.newInstance(repository, bridgeConstants, provider, activations);
BtcBlock genesis = networkParameters.getGenesisBlock();
int btcHeightWhenBlockIndexActivates = bridgeConstants.getBtcHeightWhenBlockIndexActivates();
int maxDepthToSearchBlocksBelowIndexActivation = bridgeConstants.getMaxDepthToSearchBlocksBelowIndexActivation();
int blockHeight = btcHeightWhenBlockIndexActivates + maxDepthToSearchBlocksBelowIndexActivation - 1;
StoredBlock storedBlock1 = createStoredBlock(genesis, blockHeight, 0);
btcBlockStore.put(storedBlock1);
btcBlockStore.setChainHead(storedBlock1);
assertEquals(storedBlock1, btcBlockStore.getChainHead());
// Search for a block in a height lower than the max depth, should fail
// Since the chain height is below btcHeightWhenBlockIndexActivates + maxDepthToSearchBlocksBelowIndexActivation
int maxDepth = blockHeight - maxDepthToSearchBlocksBelowIndexActivation;
btcBlockStore.getStoredBlockAtMainChainHeight(maxDepth - 1);
}
Aggregations