Search in sources :

Example 1 with BlockStoreException

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

the class LockWhitelistTest method buildInitializer.

private BridgeStorageProviderInitializer buildInitializer() {
    final int minSize = 10;
    final int maxSize = 100;
    final int minBtcBlocks = 500;
    final int maxBtcBlocks = 1000;
    return (BridgeStorageProvider provider, Repository repository, int executionIndex) -> {
        BtcBlockStore btcBlockStore = new RepositoryBlockStore(new RskSystemProperties(), repository, PrecompiledContracts.BRIDGE_ADDR);
        Context btcContext = new Context(networkParameters);
        BtcBlockChain btcBlockChain;
        try {
            btcBlockChain = new BtcBlockChain(btcContext, btcBlockStore);
        } catch (BlockStoreException e) {
            throw new RuntimeException("Error initializing btc blockchain for tests");
        }
        int blocksToGenerate = Helper.randomInRange(minBtcBlocks, maxBtcBlocks);
        Helper.generateAndAddBlocks(btcBlockChain, blocksToGenerate);
        lockWhitelist = provider.getLockWhitelist();
        int size = Helper.randomInRange(minSize, maxSize);
        for (int i = 0; i < size; i++) {
            Address address = new BtcECKey().toAddress(networkParameters);
            Coin value = Helper.randomCoin(Coin.COIN, 1, 30);
            lockWhitelist.put(address, value);
        }
    };
}
Also used : BlockStoreException(co.rsk.bitcoinj.store.BlockStoreException) BridgeStorageProvider(co.rsk.peg.BridgeStorageProvider) BtcBlockStore(co.rsk.bitcoinj.store.BtcBlockStore) Repository(org.ethereum.core.Repository) RepositoryBlockStore(co.rsk.peg.RepositoryBlockStore) RskSystemProperties(co.rsk.config.RskSystemProperties)

Example 2 with BlockStoreException

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

the class ReceiveHeadersTest method doReceiveHeaders.

private ExecutionStats doReceiveHeaders(String caseName, int times, int numHeaders, int forkDepth) throws VMException {
    String name = String.format("%s-forkdepth-%d-headers-%d", caseName, forkDepth, numHeaders);
    ExecutionStats stats = new ExecutionStats(name);
    int totalHeaders = numHeaders + forkDepth;
    return executeAndAverage(name, times, generateABIEncoder(totalHeaders, totalHeaders, forkDepth), buildInitializer(1000, 2000), Helper.getZeroValueTxBuilder(Helper.getRandomFederatorECKey()), Helper.getRandomHeightProvider(10), stats, (EnvironmentBuilder.Environment environment, byte[] result) -> {
        BridgeStorageProvider bridgeStorageProvider = new BridgeStorageProvider((Repository) environment.getBenchmarkedRepository(), PrecompiledContracts.BRIDGE_ADDR, constants.getBridgeConstants(), activationConfig.forBlock(0));
        btcBlockStore = new RepositoryBtcBlockStoreWithCache(BridgeRegTestConstants.getInstance().getBtcParams(), (Repository) environment.getBenchmarkedRepository(), new HashMap<>(), PrecompiledContracts.BRIDGE_ADDR, bridgeConstants, bridgeStorageProvider, activationConfig.forBlock(0));
        Sha256Hash bestBlockHash = null;
        try {
            bestBlockHash = btcBlockStore.getChainHead().getHeader().getHash();
        } catch (BlockStoreException e) {
            Assert.fail(e.getMessage());
        }
        Assert.assertEquals(expectedBestBlock.getHash(), bestBlockHash);
    });
}
Also used : RepositoryBtcBlockStoreWithCache(co.rsk.peg.RepositoryBtcBlockStoreWithCache) Repository(org.ethereum.core.Repository) BlockStoreException(co.rsk.bitcoinj.store.BlockStoreException) HashMap(java.util.HashMap) BridgeStorageProvider(co.rsk.peg.BridgeStorageProvider) Sha256Hash(co.rsk.bitcoinj.core.Sha256Hash)

Example 3 with BlockStoreException

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

the class RepositoryBtcBlockStoreWithCache method getStoredBlockAtMainChainHeight.

@Override
public StoredBlock getStoredBlockAtMainChainHeight(int height) throws BlockStoreException {
    StoredBlock chainHead = getChainHead();
    int depth = chainHead.getHeight() - height;
    logger.trace("Getting btc block at depth: {}", depth);
    if (depth < 0) {
        String message = String.format("Height provided is higher than chain head. provided: %n. chain head: %n", height, chainHead.getHeight());
        logger.trace("[getStoredBlockAtMainChainHeight] {}", message);
        throw new BlockStoreException(message);
    }
    if (activations.isActive(ConsensusRule.RSKIP199)) {
        int btcHeightWhenBlockIndexActivates = this.bridgeConstants.getBtcHeightWhenBlockIndexActivates();
        int maxDepthToSearch = this.bridgeConstants.getMaxDepthToSearchBlocksBelowIndexActivation();
        int limit;
        if (chainHead.getHeight() - btcHeightWhenBlockIndexActivates > maxDepthToSearch) {
            limit = btcHeightWhenBlockIndexActivates;
        } else {
            limit = chainHead.getHeight() - maxDepthToSearch;
        }
        logger.trace("[getStoredBlockAtMainChainHeight] Chain head height is {} and the depth limit {}", chainHead.getHeight(), limit);
        if (height < limit) {
            String message = String.format("Height provided is lower than the depth limit defined to search for blocks. Provided: %n, limit: %n", height, limit);
            logger.trace("[getStoredBlockAtMainChainHeight] {}", message);
            throw new BlockStoreException(message);
        }
    }
    StoredBlock block;
    Optional<StoredBlock> blockOptional = getInMainchain(height);
    if (blockOptional.isPresent()) {
        block = blockOptional.get();
    } else {
        block = getStoredBlockAtMainChainDepth(depth);
    }
    return block;
}
Also used : StoredBlock(co.rsk.bitcoinj.core.StoredBlock) BlockStoreException(co.rsk.bitcoinj.store.BlockStoreException)

Example 4 with BlockStoreException

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

the class Bridge method registerBtcTransaction.

public void registerBtcTransaction(Object[] args) throws VMException {
    logger.trace("registerBtcTransaction");
    byte[] btcTxSerialized = (byte[]) args[0];
    int height = ((BigInteger) args[1]).intValue();
    byte[] pmtSerialized = (byte[]) args[2];
    try {
        bridgeSupport.registerBtcTransaction(rskTx, btcTxSerialized, height, pmtSerialized);
    } catch (IOException | BlockStoreException e) {
        logger.warn("Exception in registerBtcTransaction", e);
        throw new VMException("Exception in registerBtcTransaction", e);
    }
}
Also used : BlockStoreException(co.rsk.bitcoinj.store.BlockStoreException) VMException(org.ethereum.vm.exception.VMException) BigInteger(java.math.BigInteger) IOException(java.io.IOException)

Example 5 with BlockStoreException

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

the class BridgeSupport method getBtcTransactionConfirmationsGetCost.

public Long getBtcTransactionConfirmationsGetCost(Object[] args) {
    final long BASIC_COST = 27_000;
    final long STEP_COST = 315;
    // 72 * 2. 72 is the cost of the hash operation
    final long DOUBLE_HASH_COST = 144;
    Sha256Hash btcBlockHash;
    int branchHashesSize;
    try {
        btcBlockHash = Sha256Hash.wrap((byte[]) args[1]);
        Object[] merkleBranchHashesArray = (Object[]) args[3];
        branchHashesSize = merkleBranchHashesArray.length;
    } catch (NullPointerException | IllegalArgumentException e) {
        return BASIC_COST;
    }
    // Dynamic cost based on the depth of the block that contains
    // the transaction. Find such depth first, then calculate
    // the cost.
    Context.propagate(btcContext);
    try {
        this.ensureBtcBlockStore();
        final StoredBlock block = btcBlockStore.getFromCache(btcBlockHash);
        // Block not found, default to basic cost
        if (block == null) {
            return BASIC_COST;
        }
        final int bestChainHeight = getBtcBlockchainBestChainHeight();
        // Make sure calculated depth is >= 0
        final int blockDepth = Math.max(0, bestChainHeight - block.getHeight());
        // Block too deep, default to basic cost
        if (blockDepth > BTC_TRANSACTION_CONFIRMATION_MAX_DEPTH) {
            return BASIC_COST;
        }
        return BASIC_COST + blockDepth * STEP_COST + branchHashesSize * DOUBLE_HASH_COST;
    } catch (IOException | BlockStoreException e) {
        logger.warn("getBtcTransactionConfirmationsGetCost btcBlockHash:{} there was a problem " + "gathering the block depth while calculating the gas cost. " + "Defaulting to basic cost.", btcBlockHash, e);
        return BASIC_COST;
    }
}
Also used : StoredBlock(co.rsk.bitcoinj.core.StoredBlock) BlockStoreException(co.rsk.bitcoinj.store.BlockStoreException) Sha256Hash(co.rsk.bitcoinj.core.Sha256Hash) IOException(java.io.IOException)

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