Search in sources :

Example 16 with StoredBlock

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

the class RepositoryBlockStoreTest method test.

@Test
public void test() 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 = new RepositoryImplForTesting();
    RskSystemProperties config = new RskSystemProperties();
    RepositoryBlockStore store = new RepositoryBlockStore(config, repository, PrecompiledContracts.BRIDGE_ADDR);
    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
    RepositoryBlockStore store2 = new RepositoryBlockStore(config, repository, PrecompiledContracts.BRIDGE_ADDR);
    // 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) RepositoryImplForTesting(co.rsk.db.RepositoryImplForTesting) BigInteger(java.math.BigInteger) Triple(org.apache.commons.lang3.tuple.Triple) Repository(org.ethereum.core.Repository) BigInteger(java.math.BigInteger) BtcBlock(co.rsk.bitcoinj.core.BtcBlock) RskSystemProperties(co.rsk.config.RskSystemProperties) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 17 with StoredBlock

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

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

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

the class RepositoryBtcBlockStoreWithCache method populateCache.

private synchronized void populateCache(StoredBlock chainHead) {
    logger.trace("Populating BTC Block Store Cache.");
    if (this.btcNetworkParams.getGenesisBlock().equals(chainHead.getHeader())) {
        return;
    }
    cacheBlocks.put(chainHead.getHeader().getHash(), chainHead);
    Sha256Hash blockHash = chainHead.getHeader().getPrevBlockHash();
    int depth = this.maxDepthBlockCache - 1;
    while (blockHash != null && depth > 0) {
        if (cacheBlocks.get(blockHash) != null) {
            break;
        }
        StoredBlock currentBlock = get(blockHash);
        if (currentBlock == null) {
            break;
        }
        cacheBlocks.put(currentBlock.getHeader().getHash(), currentBlock);
        depth--;
        blockHash = currentBlock.getHeader().getPrevBlockHash();
    }
    logger.trace("END Populating BTC Block Store Cache.");
}
Also used : StoredBlock(co.rsk.bitcoinj.core.StoredBlock) Sha256Hash(co.rsk.bitcoinj.core.Sha256Hash)

Example 20 with StoredBlock

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

the class RepositoryBtcBlockStoreWithCache method get.

@Override
public synchronized StoredBlock get(Sha256Hash hash) {
    logger.trace("[get] Looking in storage for block with hash {}", hash);
    byte[] ba = repository.getStorageBytes(contractAddress, DataWord.valueFromHex(hash.toString()));
    if (ba == null) {
        logger.trace("[get] Block with hash {} not found in storage", hash);
        return null;
    }
    StoredBlock storedBlock = byteArrayToStoredBlock(ba);
    return storedBlock;
}
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