Search in sources :

Example 21 with StoredBlock

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());
}
Also used : Repository(org.ethereum.core.Repository) MutableRepository(org.ethereum.db.MutableRepository) StoredBlock(co.rsk.bitcoinj.core.StoredBlock) Sha256Hash(co.rsk.bitcoinj.core.Sha256Hash) BtcBlock(co.rsk.bitcoinj.core.BtcBlock) Test(org.junit.Test)

Example 22 with StoredBlock

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());
}
Also used : Repository(org.ethereum.core.Repository) MutableRepository(org.ethereum.db.MutableRepository) StoredBlock(co.rsk.bitcoinj.core.StoredBlock) Sha256Hash(co.rsk.bitcoinj.core.Sha256Hash) ActivationConfig(org.ethereum.config.blockchain.upgrades.ActivationConfig) Test(org.junit.Test)

Example 23 with StoredBlock

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());
}
Also used : StoredBlock(co.rsk.bitcoinj.core.StoredBlock) Sha256Hash(co.rsk.bitcoinj.core.Sha256Hash) BtcBlock(co.rsk.bitcoinj.core.BtcBlock) Test(org.junit.Test)

Example 24 with StoredBlock

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());
}
Also used : StoredBlock(co.rsk.bitcoinj.core.StoredBlock) BtcBlock(co.rsk.bitcoinj.core.BtcBlock) Test(org.junit.Test)

Example 25 with StoredBlock

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);
}
Also used : Repository(org.ethereum.core.Repository) MutableRepository(org.ethereum.db.MutableRepository) StoredBlock(co.rsk.bitcoinj.core.StoredBlock) BtcBlock(co.rsk.bitcoinj.core.BtcBlock) ActivationConfig(org.ethereum.config.blockchain.upgrades.ActivationConfig) Test(org.junit.Test)

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