Search in sources :

Example 11 with Sha256Hash

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

the class RepositoryBtcBlockStoreWithCacheTest method cacheLivesAcrossInstances.

@Test
public void cacheLivesAcrossInstances() throws BlockStoreException {
    Repository repository = createRepository();
    RepositoryBtcBlockStoreWithCache.Factory factory = createBlockStoreFactory();
    BtcBlockStoreWithCache btcBlockStore = createBlockStoreWithTrack(factory, repository.startTracking());
    BtcBlock genesis = networkParameters.getGenesisBlock();
    StoredBlock firstStoredBlock = createStoredBlock(genesis, 1, 0);
    Sha256Hash firstBlockHash = firstStoredBlock.getHeader().getHash();
    btcBlockStore.put(firstStoredBlock);
    // Cache should have the genesis block and the one we just added
    assertNotNull(btcBlockStore.getFromCache(genesis.getHash()));
    assertEquals(firstStoredBlock, btcBlockStore.getFromCache(firstBlockHash));
    BtcBlockStoreWithCache btcBlockStore2 = createBlockStoreWithTrack(factory, repository.startTracking());
    StoredBlock secondStoredBlock = createStoredBlock(firstStoredBlock.getHeader(), 2, 0);
    Sha256Hash secondBlockHash = secondStoredBlock.getHeader().getHash();
    btcBlockStore2.put(secondStoredBlock);
    // Trie of btcBlockStore1  should have only te second one
    assertEquals(firstStoredBlock, btcBlockStore.get(firstBlockHash));
    assertNull(btcBlockStore.get(secondBlockHash));
    // Trie of btcBlockStore2  should have only te second one
    assertNull(btcBlockStore2.get(firstBlockHash));
    assertEquals(secondStoredBlock, btcBlockStore2.get(secondBlockHash));
    // Cache has both of them
    assertEquals(firstStoredBlock, btcBlockStore2.getFromCache(firstBlockHash));
    assertEquals(secondStoredBlock, btcBlockStore2.getFromCache(secondBlockHash));
}
Also used : Repository(org.ethereum.core.Repository) MutableRepository(org.ethereum.db.MutableRepository) StoredBlock(co.rsk.bitcoinj.core.StoredBlock) Sha256Hash(co.rsk.bitcoinj.core.Sha256Hash) BtcBlock(co.rsk.bitcoinj.core.BtcBlock) Test(org.junit.Test)

Example 12 with Sha256Hash

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

the class LockTest method buildInitializer.

private BridgeStorageProviderInitializer buildInitializer() {
    final int minHashes = 100;
    final int maxHashes = 10000;
    final int minHeight = 1000;
    final int maxHeight = 2000;
    return (BridgeStorageProvider provider, Repository repository, int executionIndex, BtcBlockStore blockStore) -> {
        int hashesToGenerate = Helper.randomInRange(minHashes, maxHashes);
        int randomHashIndex = Helper.randomInRange(0, hashesToGenerate - 1);
        Random rnd = new Random();
        for (int i = 0; i < hashesToGenerate; i++) {
            Sha256Hash hash = Sha256Hash.of(BigInteger.valueOf(rnd.nextLong()).toByteArray());
            long height = Helper.randomInRange(minHeight, maxHeight);
            try {
                provider.setHeightBtcTxhashAlreadyProcessed(hash, height);
            } catch (IOException e) {
                throw new RuntimeException("Exception trying to gather hashes already processed for benchmarking");
            }
            if (i == randomHashIndex) {
                randomHashInMap = hash;
            }
        }
    };
}
Also used : Repository(org.ethereum.core.Repository) Random(java.util.Random) BridgeStorageProvider(co.rsk.peg.BridgeStorageProvider) Sha256Hash(co.rsk.bitcoinj.core.Sha256Hash) BtcBlockStore(co.rsk.bitcoinj.store.BtcBlockStore) IOException(java.io.IOException)

Example 13 with Sha256Hash

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

the class LockTest method getBtcTxHashProcessedHeight_notProcessed.

private void getBtcTxHashProcessedHeight_notProcessed(int times, ExecutionStats stats) throws VMException {
    Sha256Hash hash = Sha256Hash.of(BigInteger.valueOf(new Random().nextLong()).toByteArray());
    ABIEncoder abiEncoder = (int executionIndex) -> Bridge.GET_BTC_TX_HASH_PROCESSED_HEIGHT.encode(new Object[] { ByteUtil.toHexString(hash.getBytes()) });
    executeAndAverage("getBtcTxHashProcessedHeight-notProcessed", times, abiEncoder, buildInitializer(), Helper.getZeroValueRandomSenderTxBuilder(), Helper.getRandomHeightProvider(10), stats);
}
Also used : Random(java.util.Random) Sha256Hash(co.rsk.bitcoinj.core.Sha256Hash)

Example 14 with Sha256Hash

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

the class RepositoryBtcBlockStoreWithCache method getInMainchain.

@Override
public Optional<StoredBlock> getInMainchain(int height) {
    Optional<Sha256Hash> bestBlockHash = bridgeStorageProvider.getBtcBestBlockHashByHeight(height);
    if (!bestBlockHash.isPresent()) {
        logger.trace("[getInMainchain] Block at height {} not present in storage", height);
        return Optional.empty();
    }
    StoredBlock block = get(bestBlockHash.get());
    if (block == null) {
        logger.trace("[getInMainchain] Block with hash {} not found in storage", bestBlockHash.get());
        return Optional.empty();
    }
    logger.trace("[getInMainchain] Found block with hash {} at height {}", bestBlockHash.get(), height);
    return Optional.of(block);
}
Also used : StoredBlock(co.rsk.bitcoinj.core.StoredBlock) Sha256Hash(co.rsk.bitcoinj.core.Sha256Hash)

Example 15 with Sha256Hash

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

the class RepositoryBtcBlockStoreWithCache method put.

@Override
public synchronized void put(StoredBlock storedBlock) {
    Sha256Hash hash = storedBlock.getHeader().getHash();
    byte[] ba = storedBlockToByteArray(storedBlock);
    repository.addStorageBytes(contractAddress, DataWord.valueFromHex(hash.toString()), ba);
    if (cacheBlocks != null) {
        StoredBlock chainHead = getChainHead();
        if (chainHead == null || chainHead.getHeight() - storedBlock.getHeight() < this.maxDepthBlockCache) {
            cacheBlocks.put(storedBlock.getHeader().getHash(), storedBlock);
        }
    }
}
Also used : StoredBlock(co.rsk.bitcoinj.core.StoredBlock) Sha256Hash(co.rsk.bitcoinj.core.Sha256Hash)

Aggregations

Sha256Hash (co.rsk.bitcoinj.core.Sha256Hash)40 Test (org.junit.Test)17 StoredBlock (co.rsk.bitcoinj.core.StoredBlock)15 BtcTransaction (co.rsk.bitcoinj.core.BtcTransaction)11 BtcBlock (co.rsk.bitcoinj.core.BtcBlock)9 Script (co.rsk.bitcoinj.script.Script)7 BlockStoreException (co.rsk.bitcoinj.store.BlockStoreException)7 Repository (org.ethereum.core.Repository)7 BtcECKey (co.rsk.bitcoinj.core.BtcECKey)6 TransactionOutPoint (co.rsk.bitcoinj.core.TransactionOutPoint)6 ArrayList (java.util.ArrayList)6 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)6 IOException (java.io.IOException)5 PartialMerkleTree (co.rsk.bitcoinj.core.PartialMerkleTree)4 TransactionInput (co.rsk.bitcoinj.core.TransactionInput)4 TransactionSignature (co.rsk.bitcoinj.crypto.TransactionSignature)4 ScriptChunk (co.rsk.bitcoinj.script.ScriptChunk)4 BigInteger (java.math.BigInteger)4 MutableRepository (org.ethereum.db.MutableRepository)4 VerificationException (co.rsk.bitcoinj.core.VerificationException)3