Search in sources :

Example 11 with StoredBlock

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

Example 12 with StoredBlock

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

Example 13 with StoredBlock

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());
}
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 14 with StoredBlock

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));
}
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 15 with StoredBlock

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