Search in sources :

Example 21 with Sha256Hash

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

the class Rskip92MerkleProofValidatorTests method isValid_PassValidHashes_ShouldReturnTrue.

@Test
public void isValid_PassValidHashes_ShouldReturnTrue() {
    List<byte[]> hashList = makeHashList();
    byte[] pmtSerialized = join(hashList);
    Sha256Hash coinbaseHash = Sha256Hash.wrap(hashList.get(0));
    Rskip92MerkleProofValidator rskip92MerkleProofValidator = new Rskip92MerkleProofValidator(pmtSerialized, true);
    Sha256Hash rootHash = hashList.stream().map(Sha256Hash::wrap).reduce(coinbaseHash, MerkleTreeUtils::combineLeftRight);
    boolean actualResult = rskip92MerkleProofValidator.isValid(rootHash, coinbaseHash);
    assertTrue(actualResult);
}
Also used : Rskip92MerkleProofValidator(co.rsk.validators.Rskip92MerkleProofValidator) MerkleTreeUtils(co.rsk.peg.utils.MerkleTreeUtils) Sha256Hash(co.rsk.bitcoinj.core.Sha256Hash) Test(org.junit.Test)

Example 22 with Sha256Hash

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

the class Rskip92MerkleProofValidatorTests method isValid_PassInvalidCoinbaseHash_ShouldReturnFalse.

@Test
public void isValid_PassInvalidCoinbaseHash_ShouldReturnFalse() {
    List<byte[]> hashList = makeHashList();
    byte[] pmtSerialized = join(hashList);
    Sha256Hash coinbaseHash = Sha256Hash.wrap(hashList.get(0));
    Rskip92MerkleProofValidator rskip92MerkleProofValidator = new Rskip92MerkleProofValidator(pmtSerialized, true);
    Sha256Hash rootHash = hashList.stream().map(Sha256Hash::wrap).reduce(coinbaseHash, MerkleTreeUtils::combineLeftRight);
    boolean actualResult = rskip92MerkleProofValidator.isValid(rootHash, Sha256Hash.wrap(new byte[Sha256Hash.LENGTH]));
    assertFalse(actualResult);
}
Also used : Rskip92MerkleProofValidator(co.rsk.validators.Rskip92MerkleProofValidator) MerkleTreeUtils(co.rsk.peg.utils.MerkleTreeUtils) Sha256Hash(co.rsk.bitcoinj.core.Sha256Hash) Test(org.junit.Test)

Example 23 with Sha256Hash

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

the class RepositoryBtcBlockStoreWithCacheTest method ifCacheNullAlwaysGoToDisk.

@Test
public void ifCacheNullAlwaysGoToDisk() throws BlockStoreException {
    Repository repository = createRepository();
    BtcBlockStoreWithCache btcBlockStore = new RepositoryBtcBlockStoreWithCache(networkParameters, repository.startTracking(), null, PrecompiledContracts.BRIDGE_ADDR, bridgeConstants, mock(BridgeStorageProvider.class), mock(ActivationConfig.ForBlock.class));
    BtcBlock genesis = networkParameters.getGenesisBlock();
    StoredBlock firstStoredBlock = createStoredBlock(genesis, 1, 0);
    Sha256Hash firstBlockHash = firstStoredBlock.getHeader().getHash();
    btcBlockStore.put(firstStoredBlock);
    btcBlockStore.setChainHead(firstStoredBlock);
    assertEquals(firstStoredBlock, btcBlockStore.get(firstBlockHash));
    assertNull(btcBlockStore.getFromCache(firstBlockHash));
    assertEquals(firstStoredBlock, btcBlockStore.getChainHead());
    assertEquals(genesis, btcBlockStore.getStoredBlockAtMainChainDepth(1).getHeader());
    assertEquals(genesis, btcBlockStore.getStoredBlockAtMainChainHeight(0).getHeader());
}
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 24 with Sha256Hash

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

the class RepositoryBtcBlockStoreWithCacheTest method getInMainchain_hashNotFound.

@Test
public void getInMainchain_hashNotFound() {
    Repository repository = createRepository();
    BtcBlockStoreWithCache.Factory btcBlockStoreFactory = new RepositoryBtcBlockStoreWithCache.Factory(bridgeConstants.getBtcParams());
    int blockHeight = 100;
    Sha256Hash blockHash = PegTestUtils.createHash(2);
    BridgeStorageProvider provider = mock(BridgeStorageProvider.class);
    when(provider.getBtcBestBlockHashByHeight(blockHeight)).thenReturn(Optional.of(blockHash));
    ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
    BtcBlockStoreWithCache btcBlockStore = btcBlockStoreFactory.newInstance(repository, bridgeConstants, provider, activations);
    Optional<StoredBlock> blockOptional = btcBlockStore.getInMainchain(blockHeight);
    Assert.assertFalse(blockOptional.isPresent());
}
Also used : Repository(org.ethereum.core.Repository) MutableRepository(org.ethereum.db.MutableRepository) StoredBlock(co.rsk.bitcoinj.core.StoredBlock) Sha256Hash(co.rsk.bitcoinj.core.Sha256Hash) ActivationConfig(org.ethereum.config.blockchain.upgrades.ActivationConfig) Test(org.junit.Test)

Example 25 with Sha256Hash

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

the class RepositoryBtcBlockStoreWithCacheTest method put_oldBlockShouldNotGoToCache.

@Test
public void put_oldBlockShouldNotGoToCache() throws BlockStoreException {
    BtcBlockStoreWithCache btcBlockStore = createBlockStore();
    BtcBlock genesis = networkParameters.getGenesisBlock();
    // Set chain head at height 6000
    StoredBlock storedBlock1 = createStoredBlock(genesis, 6000, 0);
    Sha256Hash firstBlockHash = storedBlock1.getHeader().getHash();
    btcBlockStore.put(storedBlock1);
    btcBlockStore.setChainHead(storedBlock1);
    // Store a block of height 1 which is lesser than chainHead - 5000
    StoredBlock storedBlock2 = createStoredBlock(genesis, 1, 1);
    Sha256Hash secondBlockHash = storedBlock2.getHeader().getHash();
    btcBlockStore.put(storedBlock2);
    assertEquals(storedBlock1, btcBlockStore.getFromCache(firstBlockHash));
    assertNull(btcBlockStore.getFromCache(secondBlockHash));
    assertEquals(storedBlock1, btcBlockStore.get(firstBlockHash));
    assertEquals(storedBlock2, btcBlockStore.get(secondBlockHash));
    assertEquals(storedBlock1, btcBlockStore.getChainHead());
}
Also used : StoredBlock(co.rsk.bitcoinj.core.StoredBlock) Sha256Hash(co.rsk.bitcoinj.core.Sha256Hash) BtcBlock(co.rsk.bitcoinj.core.BtcBlock) Test(org.junit.Test)

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