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));
}
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;
}
}
};
}
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);
}
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);
}
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);
}
}
}
Aggregations