Search in sources :

Example 11 with BtcBlock

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

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);
}
Also used : StoredBlock(co.rsk.bitcoinj.core.StoredBlock) BtcBlock(co.rsk.bitcoinj.core.BtcBlock) Test(org.junit.Test)

Example 13 with BtcBlock

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));
}
Also used : BtcTransaction(co.rsk.bitcoinj.core.BtcTransaction) BtcBlock(co.rsk.bitcoinj.core.BtcBlock)

Example 14 with BtcBlock

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));
}
Also used : BtcBlock(co.rsk.bitcoinj.core.BtcBlock)

Example 15 with BtcBlock

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());
    });
}
Also used : BtcBlock(co.rsk.bitcoinj.core.BtcBlock)

Aggregations

BtcBlock (co.rsk.bitcoinj.core.BtcBlock)37 Test (org.junit.Test)17 StoredBlock (co.rsk.bitcoinj.core.StoredBlock)16 NetworkParameters (co.rsk.bitcoinj.core.NetworkParameters)11 BtcTransaction (co.rsk.bitcoinj.core.BtcTransaction)10 Sha256Hash (co.rsk.bitcoinj.core.Sha256Hash)9 Repository (org.ethereum.core.Repository)8 Context (co.rsk.bitcoinj.core.Context)7 BigInteger (java.math.BigInteger)7 MutableRepository (org.ethereum.db.MutableRepository)6 BlockStoreException (co.rsk.bitcoinj.store.BlockStoreException)4 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)3 SubmitBlockResult (co.rsk.mine.SubmitBlockResult)3 InputStream (java.io.InputStream)3 ActivationConfig (org.ethereum.config.blockchain.upgrades.ActivationConfig)3 MessageSerializer (co.rsk.bitcoinj.core.MessageSerializer)2 VerificationException (co.rsk.bitcoinj.core.VerificationException)2 BlockMiner (co.rsk.blockchain.utils.BlockMiner)2 RskSystemProperties (co.rsk.config.RskSystemProperties)2 Keccak256 (co.rsk.crypto.Keccak256)2