Search in sources :

Example 6 with StoredBlock

use of co.rsk.bitcoinj.core.StoredBlock 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)

Example 7 with StoredBlock

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

the class BridgeSupport method getBtcBlockchainBlockHeaderByHeight.

public byte[] getBtcBlockchainBlockHeaderByHeight(int height) throws BlockStoreException, IOException {
    Context.propagate(btcContext);
    this.ensureBtcBlockStore();
    StoredBlock block = btcBlockStore.getStoredBlockAtMainChainHeight(height);
    return serializeBlockHeader(block);
}
Also used : StoredBlock(co.rsk.bitcoinj.core.StoredBlock)

Example 8 with StoredBlock

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

the class BridgeSupport method getLowestBlock.

/**
 * Returns the first bitcoin block we have. It is either a checkpoint or the genesis
 */
private StoredBlock getLowestBlock() throws IOException {
    InputStream checkpoints = this.getCheckPoints();
    if (checkpoints == null) {
        BtcBlock genesis = bridgeConstants.getBtcParams().getGenesisBlock();
        return new StoredBlock(genesis, genesis.getWork(), 0);
    }
    CheckpointManager manager = new CheckpointManager(bridgeConstants.getBtcParams(), checkpoints);
    long time = getActiveFederation().getCreationTime().toEpochMilli();
    // Go back 1 week to match CheckpointManager.checkpoint() behaviour
    time -= 86400 * 7;
    return manager.getCheckpointBefore(time);
}
Also used : StoredBlock(co.rsk.bitcoinj.core.StoredBlock) InputStream(java.io.InputStream) CheckpointManager(co.rsk.bitcoinj.core.CheckpointManager) BtcBlock(co.rsk.bitcoinj.core.BtcBlock)

Example 9 with StoredBlock

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

the class BridgeSupport method getBtcBlockchainBlockLocator.

/**
 * @deprecated
 * Returns an array of block hashes known by the bridge contract.
 * Federators can use this to find what is the latest block in the mainchain the bridge has.
 * @return a List of bitcoin block hashes
 */
@Deprecated
public List<Sha256Hash> getBtcBlockchainBlockLocator() throws IOException, BlockStoreException {
    StoredBlock initialBtcStoredBlock = this.getLowestBlock();
    final int maxHashesToInform = 100;
    List<Sha256Hash> blockLocator = new ArrayList<>();
    StoredBlock cursor = getBtcBlockchainChainHead();
    int bestBlockHeight = cursor.getHeight();
    blockLocator.add(cursor.getHeader().getHash());
    if (bestBlockHeight > initialBtcStoredBlock.getHeight()) {
        boolean stop = false;
        int i = 0;
        try {
            while (blockLocator.size() <= maxHashesToInform && !stop) {
                int blockHeight = (int) (bestBlockHeight - Math.pow(2, i));
                if (blockHeight <= initialBtcStoredBlock.getHeight()) {
                    blockLocator.add(initialBtcStoredBlock.getHeader().getHash());
                    stop = true;
                } else {
                    cursor = this.getPrevBlockAtHeight(cursor, blockHeight);
                    blockLocator.add(cursor.getHeader().getHash());
                }
                i++;
            }
        } catch (Exception e) {
            logger.error("Failed to walk the block chain whilst constructing a locator");
            panicProcessor.panic("btcblockchain", "Failed to walk the block chain whilst constructing a locator");
            throw new RuntimeException(e);
        }
        if (!stop) {
            blockLocator.add(initialBtcStoredBlock.getHeader().getHash());
        }
    }
    return blockLocator;
}
Also used : StoredBlock(co.rsk.bitcoinj.core.StoredBlock) Sha256Hash(co.rsk.bitcoinj.core.Sha256Hash) ArrayList(java.util.ArrayList) VMException(org.ethereum.vm.exception.VMException) UTXOProviderException(co.rsk.bitcoinj.core.UTXOProviderException) VerificationException(co.rsk.bitcoinj.core.VerificationException) InsufficientMoneyException(co.rsk.bitcoinj.core.InsufficientMoneyException) AddressFormatException(co.rsk.bitcoinj.core.AddressFormatException) IOException(java.io.IOException) BlockStoreException(co.rsk.bitcoinj.store.BlockStoreException) PeginInstructionsException(co.rsk.peg.pegininstructions.PeginInstructionsException)

Example 10 with StoredBlock

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

the class RepositoryBtcBlockStoreWithCacheTest method getInMainchain.

@Test
public void getInMainchain() throws BlockStoreException {
    Repository repository = createRepository();
    BtcBlockStoreWithCache.Factory btcBlockStoreFactory = new RepositoryBtcBlockStoreWithCache.Factory(bridgeConstants.getBtcParams());
    int blockHeight = 100;
    BtcBlock genesis = networkParameters.getGenesisBlock();
    StoredBlock storedBlock1 = createStoredBlock(genesis, blockHeight, 0);
    BridgeStorageProvider provider = mock(BridgeStorageProvider.class);
    when(provider.getBtcBestBlockHashByHeight(blockHeight)).thenReturn(Optional.of(storedBlock1.getHeader().getHash()));
    ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
    BtcBlockStoreWithCache btcBlockStore = btcBlockStoreFactory.newInstance(repository, bridgeConstants, provider, activations);
    btcBlockStore.put(storedBlock1);
    Optional<StoredBlock> blockOptional = btcBlockStore.getInMainchain(blockHeight);
    Assert.assertTrue(blockOptional.isPresent());
    Assert.assertEquals(storedBlock1, blockOptional.get());
}
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