Search in sources :

Example 11 with BlockStoreException

use of co.rsk.bitcoinj.store.BlockStoreException in project rskj by rsksmart.

the class SimpleBlockChain method add.

@Override
public boolean add(BtcBlock block) {
    StoredBlock sblock = new StoredBlock(block, BigInteger.ONE, 1);
    try {
        this.blockStore.put(sblock);
        this.block = sblock;
        this.setChainHead(sblock);
    } catch (BlockStoreException e) {
        e.printStackTrace();
        return false;
    }
    return true;
}
Also used : StoredBlock(co.rsk.bitcoinj.core.StoredBlock) BlockStoreException(co.rsk.bitcoinj.store.BlockStoreException)

Example 12 with BlockStoreException

use of co.rsk.bitcoinj.store.BlockStoreException in project rskj by rsksmart.

the class RepositoryBtcBlockStoreWithCache method getStoredBlockAtMainChainDepth.

@Override
@Deprecated
public StoredBlock getStoredBlockAtMainChainDepth(int depth) throws BlockStoreException {
    logger.trace("[getStoredBlockAtMainChainDepth] Looking for block at depth {}", depth);
    StoredBlock chainHead = getChainHead();
    Sha256Hash blockHash = chainHead.getHeader().getHash();
    for (int i = 0; i < depth && blockHash != null; i++) {
        // If its older than cache go to disk
        StoredBlock currentBlock = getFromCache(blockHash);
        if (currentBlock == null) {
            logger.trace("[getStoredBlockAtMainChainDepth] Block with hash {} not in cache, getting from disk", blockHash);
            currentBlock = get(blockHash);
            if (currentBlock == null) {
                return null;
            }
        }
        blockHash = currentBlock.getHeader().getPrevBlockHash();
    }
    if (blockHash == null) {
        logger.trace("[getStoredBlockAtMainChainDepth] Block not found");
        return null;
    }
    StoredBlock block = getFromCache(blockHash);
    if (block == null) {
        block = get(blockHash);
    }
    int expectedHeight = chainHead.getHeight() - depth;
    if (block != null && block.getHeight() != expectedHeight) {
        String message = String.format("Block %s at depth %d Height is %d but should be %d", block.getHeader().getHash(), depth, block.getHeight(), expectedHeight);
        logger.trace("[getStoredBlockAtMainChainDepth] {}", message);
        throw new BlockStoreException(message);
    }
    return block;
}
Also used : StoredBlock(co.rsk.bitcoinj.core.StoredBlock) BlockStoreException(co.rsk.bitcoinj.store.BlockStoreException) Sha256Hash(co.rsk.bitcoinj.core.Sha256Hash)

Example 13 with BlockStoreException

use of co.rsk.bitcoinj.store.BlockStoreException 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 14 with BlockStoreException

use of co.rsk.bitcoinj.store.BlockStoreException in project rskj by rsksmart.

the class ReceiveHeaderTest method receiveHeader_success.

private void receiveHeader_success(int times, ExecutionStats stats) throws VMException {
    int minBlocks = 10;
    int maxBlocks = 100;
    BridgeStorageProviderInitializer storageInitializer = generateInitializer(minBlocks, maxBlocks);
    executeAndAverage("receiveHeader_success", times, // false to use an existed block (create new block)
    getABIEncoder(false), storageInitializer, Helper.getZeroValueRandomSenderTxBuilder(), Helper.getRandomHeightProvider(10), stats, (environment, executionResult) -> {
        // Working fine.
        int result = new BigInteger(executionResult).intValue();
        Assert.assertEquals(0, result);
        BridgeStorageProvider bridgeStorageProvider = new BridgeStorageProvider((Repository) environment.getBenchmarkedRepository(), PrecompiledContracts.BRIDGE_ADDR, constants.getBridgeConstants(), activationConfig.forBlock(0));
        // Best Block is the new block added.
        BtcBlockStore btcBlockStore = new RepositoryBtcBlockStoreWithCache.Factory(networkParameters).newInstance((Repository) environment.getBenchmarkedRepository(), bridgeConstants, bridgeStorageProvider, null);
        Sha256Hash bestBlockHash = null;
        try {
            bestBlockHash = btcBlockStore.getChainHead().getHeader().getHash();
        } catch (BlockStoreException e) {
            Assert.fail(e.getMessage());
        }
        Assert.assertEquals(expectedBlock.getHash(), bestBlockHash);
    });
}
Also used : RepositoryBtcBlockStoreWithCache(co.rsk.peg.RepositoryBtcBlockStoreWithCache) BlockStoreException(co.rsk.bitcoinj.store.BlockStoreException) BridgeStorageProvider(co.rsk.peg.BridgeStorageProvider) BigInteger(java.math.BigInteger) BtcBlockStore(co.rsk.bitcoinj.store.BtcBlockStore)

Example 15 with BlockStoreException

use of co.rsk.bitcoinj.store.BlockStoreException in project rskj by rsksmart.

the class BridgeSupportTestPowerMock method getBtcTransactionConfirmations_heightInconsistency.

@Test
public void getBtcTransactionConfirmations_heightInconsistency() throws BlockStoreException, IOException {
    ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
    Repository repository = createRepository();
    Repository track = repository.startTracking();
    Sha256Hash blockHash = Sha256Hash.of(Hex.decode("aabbcc"));
    BtcBlock blockHeader = mock(BtcBlock.class);
    when(blockHeader.getHash()).thenReturn(blockHash);
    int height = 50;
    StoredBlock block = new StoredBlock(blockHeader, new BigInteger("0"), height);
    BtcBlockStoreWithCache btcBlockStore = mock(BtcBlockStoreWithCache.class);
    when(btcBlockStore.getFromCache(blockHash)).thenReturn(block);
    StoredBlock chainHead = new StoredBlock(blockHeader, new BigInteger("0"), 132);
    when(btcBlockStore.getChainHead()).thenReturn(chainHead);
    when(btcBlockStore.getStoredBlockAtMainChainHeight(block.getHeight())).thenThrow(new BlockStoreException("blah"));
    BridgeStorageProvider provider = new BridgeStorageProvider(track, PrecompiledContracts.BRIDGE_ADDR, bridgeConstants, activationsBeforeForks);
    BtcBlockStoreWithCache.Factory mockFactory = mock(BtcBlockStoreWithCache.Factory.class);
    when(mockFactory.newInstance(track, bridgeConstants, provider, activations)).thenReturn(btcBlockStore);
    Sha256Hash btcTransactionHash = Sha256Hash.of(Hex.decode("112233"));
    BridgeSupport bridgeSupport = getBridgeSupport(provider, track, mockFactory, activations);
    int confirmations = bridgeSupport.getBtcTransactionConfirmations(btcTransactionHash, blockHash, null);
    Assert.assertEquals(BridgeSupport.BTC_TRANSACTION_CONFIRMATION_INCONSISTENT_BLOCK_ERROR_CODE.intValue(), confirmations);
}
Also used : BlockStoreException(co.rsk.bitcoinj.store.BlockStoreException) ActivationConfig(org.ethereum.config.blockchain.upgrades.ActivationConfig) MutableRepository(org.ethereum.db.MutableRepository) BigInteger(java.math.BigInteger) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

BlockStoreException (co.rsk.bitcoinj.store.BlockStoreException)19 StoredBlock (co.rsk.bitcoinj.core.StoredBlock)9 Sha256Hash (co.rsk.bitcoinj.core.Sha256Hash)7 IOException (java.io.IOException)7 BigInteger (java.math.BigInteger)6 BtcBlockStore (co.rsk.bitcoinj.store.BtcBlockStore)5 BridgeStorageProvider (co.rsk.peg.BridgeStorageProvider)5 Repository (org.ethereum.core.Repository)5 VMException (org.ethereum.vm.exception.VMException)5 BtcBlock (co.rsk.bitcoinj.core.BtcBlock)4 VerificationException (co.rsk.bitcoinj.core.VerificationException)4 RskSystemProperties (co.rsk.config.RskSystemProperties)4 Test (org.junit.Test)4 AddressFormatException (co.rsk.bitcoinj.core.AddressFormatException)3 InsufficientMoneyException (co.rsk.bitcoinj.core.InsufficientMoneyException)3 UTXOProviderException (co.rsk.bitcoinj.core.UTXOProviderException)3 RepositoryBlockStore (co.rsk.peg.RepositoryBlockStore)3 PeginInstructionsException (co.rsk.peg.pegininstructions.PeginInstructionsException)3 ArrayList (java.util.ArrayList)3 BtcBlockChain (co.rsk.bitcoinj.core.BtcBlockChain)2