use of co.rsk.bitcoinj.core.BtcBlock 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.BtcBlock in project rskj by rsksmart.
the class RepositoryBtcBlockStoreWithCacheTest method getStoredBlockAtMainChainHeight_heightGreaterThanChainHead.
@Test(expected = BlockStoreException.class)
public void getStoredBlockAtMainChainHeight_heightGreaterThanChainHead() throws BlockStoreException {
BtcBlockStoreWithCache btcBlockStore = createBlockStore();
BtcBlock genesis = networkParameters.getGenesisBlock();
StoredBlock storedBlock1 = createStoredBlock(genesis, 1, 0);
btcBlockStore.put(storedBlock1);
btcBlockStore.setChainHead(storedBlock1);
assertEquals(storedBlock1, btcBlockStore.getChainHead());
// Search for a block in a height higher than current chain head, should fail
btcBlockStore.getStoredBlockAtMainChainHeight(3);
}
use of co.rsk.bitcoinj.core.BtcBlock in project rskj by rsksmart.
the class MerkleBranchTest method assertBranchDoesntProve.
private void assertBranchDoesntProve(List<byte[]> hashes, int path, byte[] txHash, byte[] expectedMerkleRoot) {
BtcTransaction mockTx = mock(BtcTransaction.class);
when(mockTx.getHash()).thenReturn(Sha256Hash.wrap(txHash));
BtcBlock mockBlock = mock(BtcBlock.class);
when(mockBlock.getMerkleRoot()).thenReturn(Sha256Hash.wrap(expectedMerkleRoot));
MerkleBranch merkleBranch = new MerkleBranch(hashes.stream().map(h -> Sha256Hash.wrap(h)).collect(Collectors.toList()), path);
Assert.assertNotEquals(Sha256Hash.wrap(expectedMerkleRoot), merkleBranch.reduceFrom(Sha256Hash.wrap(txHash)));
Assert.assertFalse(merkleBranch.proves(Sha256Hash.wrap(txHash), mockBlock));
}
use of co.rsk.bitcoinj.core.BtcBlock in project rskj by rsksmart.
the class MerkleBranchTest method assertBranchCorrectlyProves.
private void assertBranchCorrectlyProves(List<byte[]> hashes, int path, byte[] txHash, byte[] expectedMerkleRoot) {
BtcBlock mockBlock = mock(BtcBlock.class);
when(mockBlock.getMerkleRoot()).thenReturn(Sha256Hash.wrap(expectedMerkleRoot));
MerkleBranch merkleBranch = new MerkleBranch(hashes.stream().map(h -> Sha256Hash.wrap(h)).collect(Collectors.toList()), path);
Assert.assertEquals(Sha256Hash.wrap(expectedMerkleRoot), merkleBranch.reduceFrom(Sha256Hash.wrap(txHash)));
Assert.assertTrue(merkleBranch.proves(Sha256Hash.wrap(txHash), mockBlock));
}
use of co.rsk.bitcoinj.core.BtcBlock in project rskj by rsksmart.
the class GetBtcBlockchainParentBlockHeaderByHashTest method getBtcBlockchainParentBlockHeaderByHashTest_success.
private void getBtcBlockchainParentBlockHeaderByHashTest_success(int times, ExecutionStats stats) throws VMException {
BridgeStorageProviderInitializer storageInitializer = generateInitializerForTest(1000, 2000);
executeAndAverage("getBtcBlockchainParentBlockHeaderByHashTest_success", times, getABIEncoder(), storageInitializer, Helper.getZeroValueRandomSenderTxBuilder(), Helper.getRandomHeightProvider(10), stats, (environment, executionResult) -> {
BtcBlock btcBlock = byteArrayToBlockHeader(getByteFromResult(executionResult));
Assert.assertEquals(expectedBlock.getHash(), btcBlock.getHash());
});
}
Aggregations