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