Search in sources :

Example 26 with StoredBlock

use of co.rsk.bitcoinj.core.StoredBlock in project rskj by rsksmart.

the class RepositoryBtcBlockStoreWithCacheTest method checkDifferentInstancesWithSameRepoHaveSameContentTest.

@Test
public void checkDifferentInstancesWithSameRepoHaveSameContentTest() throws Exception {
    // This Is how I produced RepositoryBlockStore_data.ser. I had a bitcoind in regtest with 613 blocks + genesis block
    // NetworkParameters params = RegTestParams.get();
    // Context context = new Context(params);
    // Wallet wallet = new Wallet(context);
    // BlockStore store = new SPVBlockStore(params, new File("spvBlockstore"));
    // AbstractBlockChain chain = new BlockChain(context, wallet, store);
    // PeerGroup peerGroup = new PeerGroup(context, chain);
    // peerGroup.start();
    // final DownloadProgressTracker listener = new DownloadProgressTracker();
    // peerGroup.startBlockChainDownload(listener);
    // listener.await();
    // peerGroup.stop();
    // StoredBlock storedBlock = chain.getChainHead();
    // FileOutputStream fos = new FileOutputStream("RepositoryBlockStore_data.ser");
    // ObjectOutputStream oos = new ObjectOutputStream(fos);
    // for (int i = 0; i < 614; i++) {
    // Triple<byte[], BigInteger , Integer> tripleStoredBlock = new ImmutableTriple<>(storedBlock.getHeader().bitcoinSerialize(), storedBlock.getChainWork(), storedBlock.getHeight());
    // oos.writeObject(tripleStoredBlock);
    // storedBlock = store.get(storedBlock.getHeader().getPrevBlockHash());
    // }
    // oos.close();
    // Read original store
    InputStream fileInputStream = ClassLoader.getSystemResourceAsStream("peg/RepositoryBlockStore_data.ser");
    ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
    Repository repository = createRepository();
    BtcBlockStoreWithCache.Factory btcBlockStoreFactory = new RepositoryBtcBlockStoreWithCache.Factory(bridgeConstants.getBtcParams());
    BtcBlockStoreWithCache store = btcBlockStoreFactory.newInstance(repository, bridgeConstants, mock(BridgeStorageProvider.class), mock(ActivationConfig.ForBlock.class));
    for (int i = 0; i < 614; i++) {
        Triple<byte[], BigInteger, Integer> tripleStoredBlock = (Triple<byte[], BigInteger, Integer>) objectInputStream.readObject();
        BtcBlock header = RegTestParams.get().getDefaultSerializer().makeBlock(tripleStoredBlock.getLeft());
        StoredBlock storedBlock = new StoredBlock(header, tripleStoredBlock.getMiddle(), tripleStoredBlock.getRight());
        if (i == 0) {
            store.setChainHead(storedBlock);
        }
        store.put(storedBlock);
    }
    // Create a new instance of the store
    BtcBlockStoreWithCache store2 = btcBlockStoreFactory.newInstance(repository, bridgeConstants, mock(BridgeStorageProvider.class), mock(ActivationConfig.ForBlock.class));
    // Check a specific block that used to fail when we had a bug
    assertEquals(store.get(Sha256Hash.wrap("373941fe83961cf70e181e468abc5f9f7cc440c711c3d06948fa66f3912ed27a")), store2.get(Sha256Hash.wrap("373941fe83961cf70e181e468abc5f9f7cc440c711c3d06948fa66f3912ed27a")));
    // Check new instance content is identical to the original one
    StoredBlock storedBlock = store.getChainHead();
    StoredBlock storedBlock2 = store2.getChainHead();
    int headHeight = storedBlock.getHeight();
    for (int i = 0; i < headHeight; i++) {
        assertNotNull(storedBlock);
        assertEquals(storedBlock, storedBlock2);
        Sha256Hash prevBlockHash = storedBlock.getHeader().getPrevBlockHash();
        storedBlock = store.get(prevBlockHash);
        storedBlock2 = store2.get(prevBlockHash);
    }
}
Also used : StoredBlock(co.rsk.bitcoinj.core.StoredBlock) ObjectInputStream(java.io.ObjectInputStream) InputStream(java.io.InputStream) Sha256Hash(co.rsk.bitcoinj.core.Sha256Hash) BigInteger(java.math.BigInteger) Triple(org.apache.commons.lang3.tuple.Triple) Repository(org.ethereum.core.Repository) MutableRepository(org.ethereum.db.MutableRepository) BigInteger(java.math.BigInteger) BtcBlock(co.rsk.bitcoinj.core.BtcBlock) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 27 with StoredBlock

use of co.rsk.bitcoinj.core.StoredBlock in project rskj by rsksmart.

the class RepositoryBtcBlockStoreWithCacheTest method getStoredBlockAtMainChainHeight_postIris_heightLowerThanMaxDepth_limitInBtcHeightWhenBlockIndexActivates.

@Test(expected = BlockStoreException.class)
public void getStoredBlockAtMainChainHeight_postIris_heightLowerThanMaxDepth_limitInBtcHeightWhenBlockIndexActivates() 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 above btcHeightWhenBlockIndexActivates + maxDepthToSearchBlocksBelowIndexActivation
    int maxDepth = btcHeightWhenBlockIndexActivates;
    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)

Example 28 with StoredBlock

use of co.rsk.bitcoinj.core.StoredBlock 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);
    }
}
Also used : StoredBlock(co.rsk.bitcoinj.core.StoredBlock) BlockStoreException(co.rsk.bitcoinj.store.BlockStoreException) BtcBlock(co.rsk.bitcoinj.core.BtcBlock) BigInteger(java.math.BigInteger) Test(org.junit.Test)

Example 29 with StoredBlock

use of co.rsk.bitcoinj.core.StoredBlock in project rskj by rsksmart.

the class BridgeSupport method getPrevBlockAtHeight.

private StoredBlock getPrevBlockAtHeight(StoredBlock cursor, int height) throws BlockStoreException {
    if (cursor.getHeight() == height) {
        return cursor;
    }
    boolean stop = false;
    StoredBlock current = cursor;
    while (!stop) {
        current = current.getPrev(this.btcBlockStore);
        stop = current.getHeight() == height;
    }
    return current;
}
Also used : StoredBlock(co.rsk.bitcoinj.core.StoredBlock)

Example 30 with StoredBlock

use of co.rsk.bitcoinj.core.StoredBlock in project rskj by rsksmart.

the class BridgeSupport method getBtcBlockchainBlockHashAtDepth.

public Sha256Hash getBtcBlockchainBlockHashAtDepth(int depth) throws BlockStoreException, IOException {
    Context.propagate(btcContext);
    this.ensureBtcBlockStore();
    StoredBlock head = btcBlockStore.getChainHead();
    int maxDepth = head.getHeight() - getLowestBlock().getHeight();
    if (depth < 0 || depth > maxDepth) {
        throw new IndexOutOfBoundsException(String.format("Depth must be between 0 and %d", maxDepth));
    }
    StoredBlock blockAtDepth = btcBlockStore.getStoredBlockAtMainChainDepth(depth);
    return blockAtDepth.getHeader().getHash();
}
Also used : StoredBlock(co.rsk.bitcoinj.core.StoredBlock)

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