use of co.rsk.bitcoinj.core.StoredBlock in project rskj by rsksmart.
the class RepositoryBtcBlockStoreWithCacheTest method getInMainchain_notInIndex.
@Test
public void getInMainchain_notInIndex() {
Repository repository = createRepository();
BtcBlockStoreWithCache.Factory btcBlockStoreFactory = new RepositoryBtcBlockStoreWithCache.Factory(bridgeConstants.getBtcParams());
int blockHeight = 100;
BridgeStorageProvider provider = mock(BridgeStorageProvider.class);
when(provider.getBtcBestBlockHashByHeight(blockHeight)).thenReturn(Optional.empty());
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 getStoredBlockAtMainChainHeight_preIris.
@Test
public void getStoredBlockAtMainChainHeight_preIris() 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());
// Check getStoredBlockAtMainChainHeight
assertEquals(storedBlock4, btcBlockStore.getStoredBlockAtMainChainHeight(4));
assertEquals(storedBlock3, btcBlockStore.getStoredBlockAtMainChainHeight(3));
assertEquals(storedBlock2, btcBlockStore.getStoredBlockAtMainChainHeight(2));
assertEquals(storedBlock1, btcBlockStore.getStoredBlockAtMainChainHeight(1));
}
use of co.rsk.bitcoinj.core.StoredBlock in project rskj by rsksmart.
the class RepositoryBtcBlockStoreWithCacheTest method setChainHead.
@Test
public void setChainHead() throws BlockStoreException {
BtcBlockStoreWithCache btcBlockStore = createBlockStore();
BtcBlock genesis = networkParameters.getGenesisBlock();
StoredBlock firstStoredBlock = createStoredBlock(genesis, 1, 0);
Sha256Hash firstBlockHash = firstStoredBlock.getHeader().getHash();
// Check that genesis block is the Head
assertEquals(genesis.getHash(), btcBlockStore.getChainHead().getHeader().getHash());
// There should always be a put before a setChainHead
btcBlockStore.put(firstStoredBlock);
// Test that put worked correctly
assertEquals(firstStoredBlock, btcBlockStore.getFromCache(firstBlockHash));
assertEquals(firstStoredBlock, btcBlockStore.get(firstBlockHash));
assertEquals(genesis, btcBlockStore.getChainHead().getHeader());
btcBlockStore.setChainHead(firstStoredBlock);
// Test that set Chain Head worked correctly
assertEquals(firstStoredBlock, btcBlockStore.getChainHead());
}
use of co.rsk.bitcoinj.core.StoredBlock in project rskj by rsksmart.
the class RepositoryBtcBlockStoreWithCacheTest method cacheLivesAcrossInstances.
@Test
public void cacheLivesAcrossInstances() throws BlockStoreException {
Repository repository = createRepository();
RepositoryBtcBlockStoreWithCache.Factory factory = createBlockStoreFactory();
BtcBlockStoreWithCache btcBlockStore = createBlockStoreWithTrack(factory, repository.startTracking());
BtcBlock genesis = networkParameters.getGenesisBlock();
StoredBlock firstStoredBlock = createStoredBlock(genesis, 1, 0);
Sha256Hash firstBlockHash = firstStoredBlock.getHeader().getHash();
btcBlockStore.put(firstStoredBlock);
// Cache should have the genesis block and the one we just added
assertNotNull(btcBlockStore.getFromCache(genesis.getHash()));
assertEquals(firstStoredBlock, btcBlockStore.getFromCache(firstBlockHash));
BtcBlockStoreWithCache btcBlockStore2 = createBlockStoreWithTrack(factory, repository.startTracking());
StoredBlock secondStoredBlock = createStoredBlock(firstStoredBlock.getHeader(), 2, 0);
Sha256Hash secondBlockHash = secondStoredBlock.getHeader().getHash();
btcBlockStore2.put(secondStoredBlock);
// Trie of btcBlockStore1 should have only te second one
assertEquals(firstStoredBlock, btcBlockStore.get(firstBlockHash));
assertNull(btcBlockStore.get(secondBlockHash));
// Trie of btcBlockStore2 should have only te second one
assertNull(btcBlockStore2.get(firstBlockHash));
assertEquals(secondStoredBlock, btcBlockStore2.get(secondBlockHash));
// Cache has both of them
assertEquals(firstStoredBlock, btcBlockStore2.getFromCache(firstBlockHash));
assertEquals(secondStoredBlock, btcBlockStore2.getFromCache(secondBlockHash));
}
use of co.rsk.bitcoinj.core.StoredBlock in project rskj by rsksmart.
the class RepositoryBtcBlockStoreWithCacheTest method getStoredBlockAtMainChainHeight_heightGreaterThanChainHead.
@Test(expected = BlockStoreException.class)
public void getStoredBlockAtMainChainHeight_heightGreaterThanChainHead() throws BlockStoreException {
BtcBlockStoreWithCache btcBlockStore = createBlockStore();
BtcBlock genesis = networkParameters.getGenesisBlock();
StoredBlock storedBlock1 = createStoredBlock(genesis, 1, 0);
btcBlockStore.put(storedBlock1);
btcBlockStore.setChainHead(storedBlock1);
assertEquals(storedBlock1, btcBlockStore.getChainHead());
// Search for a block in a height higher than current chain head, should fail
btcBlockStore.getStoredBlockAtMainChainHeight(3);
}
Aggregations