use of co.rsk.bitcoinj.core.Sha256Hash in project rskj by rsksmart.
the class RepositoryBtcBlockStoreWithCache method populateCache.
private synchronized void populateCache(StoredBlock chainHead) {
logger.trace("Populating BTC Block Store Cache.");
if (this.btcNetworkParams.getGenesisBlock().equals(chainHead.getHeader())) {
return;
}
cacheBlocks.put(chainHead.getHeader().getHash(), chainHead);
Sha256Hash blockHash = chainHead.getHeader().getPrevBlockHash();
int depth = this.maxDepthBlockCache - 1;
while (blockHash != null && depth > 0) {
if (cacheBlocks.get(blockHash) != null) {
break;
}
StoredBlock currentBlock = get(blockHash);
if (currentBlock == null) {
break;
}
cacheBlocks.put(currentBlock.getHeader().getHash(), currentBlock);
depth--;
blockHash = currentBlock.getHeader().getPrevBlockHash();
}
logger.trace("END Populating BTC Block Store Cache.");
}
use of co.rsk.bitcoinj.core.Sha256Hash in project rskj by rsksmart.
the class RepositoryBtcBlockStoreWithCache method getStoredBlockAtMainChainDepth.
@Override
@Deprecated
public StoredBlock getStoredBlockAtMainChainDepth(int depth) throws BlockStoreException {
logger.trace("[getStoredBlockAtMainChainDepth] Looking for block at depth {}", depth);
StoredBlock chainHead = getChainHead();
Sha256Hash blockHash = chainHead.getHeader().getHash();
for (int i = 0; i < depth && blockHash != null; i++) {
// If its older than cache go to disk
StoredBlock currentBlock = getFromCache(blockHash);
if (currentBlock == null) {
logger.trace("[getStoredBlockAtMainChainDepth] Block with hash {} not in cache, getting from disk", blockHash);
currentBlock = get(blockHash);
if (currentBlock == null) {
return null;
}
}
blockHash = currentBlock.getHeader().getPrevBlockHash();
}
if (blockHash == null) {
logger.trace("[getStoredBlockAtMainChainDepth] Block not found");
return null;
}
StoredBlock block = getFromCache(blockHash);
if (block == null) {
block = get(blockHash);
}
int expectedHeight = chainHead.getHeight() - depth;
if (block != null && block.getHeight() != expectedHeight) {
String message = String.format("Block %s at depth %d Height is %d but should be %d", block.getHeader().getHash(), depth, block.getHeight(), expectedHeight);
logger.trace("[getStoredBlockAtMainChainDepth] {}", message);
throw new BlockStoreException(message);
}
return block;
}
use of co.rsk.bitcoinj.core.Sha256Hash in project rskj by rsksmart.
the class Rskip92MerkleProofValidatorTests method isValid_PassInvalidRootHash_ShouldReturnFalse.
@Test
public void isValid_PassInvalidRootHash_ShouldReturnFalse() {
List<byte[]> hashList = makeHashList();
byte[] pmtSerialized = join(hashList);
Sha256Hash coinbaseHash = Sha256Hash.wrap(hashList.get(0));
Rskip92MerkleProofValidator rskip92MerkleProofValidator = new Rskip92MerkleProofValidator(pmtSerialized, true);
Sha256Hash rootHash = Sha256Hash.wrap(new byte[Sha256Hash.LENGTH]);
boolean actualResult = rskip92MerkleProofValidator.isValid(rootHash, coinbaseHash);
assertFalse(actualResult);
}
use of co.rsk.bitcoinj.core.Sha256Hash in project rskj by rsksmart.
the class RepositoryBlockStoreTest method test.
@Test
public void test() throws Exception {
// This Is how I produced RepositoryBlockStore_data.ser. I had a bitcoind in regtest with 613 blocks + genesis block
// NetworkParameters params = RegTestParams.get();
// Context context = new Context(params);
// Wallet wallet = new Wallet(context);
// BlockStore store = new SPVBlockStore(params, new File("spvBlockstore"));
// AbstractBlockChain chain = new BlockChain(context, wallet, store);
// PeerGroup peerGroup = new PeerGroup(context, chain);
// peerGroup.start();
// final DownloadProgressTracker listener = new DownloadProgressTracker();
// peerGroup.startBlockChainDownload(listener);
// listener.await();
// peerGroup.stop();
// StoredBlock storedBlock = chain.getChainHead();
// FileOutputStream fos = new FileOutputStream("RepositoryBlockStore_data.ser");
// ObjectOutputStream oos = new ObjectOutputStream(fos);
// for (int i = 0; i < 614; i++) {
// Triple<byte[], BigInteger , Integer> tripleStoredBlock = new ImmutableTriple<>(storedBlock.getHeader().bitcoinSerialize(), storedBlock.getChainWork(), storedBlock.getHeight());
// oos.writeObject(tripleStoredBlock);
// storedBlock = store.get(storedBlock.getHeader().getPrevBlockHash());
// }
// oos.close();
// Read original store
InputStream fileInputStream = ClassLoader.getSystemResourceAsStream("peg/RepositoryBlockStore_data.ser");
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
Repository repository = new RepositoryImplForTesting();
RskSystemProperties config = new RskSystemProperties();
RepositoryBlockStore store = new RepositoryBlockStore(config, repository, PrecompiledContracts.BRIDGE_ADDR);
for (int i = 0; i < 614; i++) {
Triple<byte[], BigInteger, Integer> tripleStoredBlock = (Triple<byte[], BigInteger, Integer>) objectInputStream.readObject();
BtcBlock header = RegTestParams.get().getDefaultSerializer().makeBlock(tripleStoredBlock.getLeft());
StoredBlock storedBlock = new StoredBlock(header, tripleStoredBlock.getMiddle(), tripleStoredBlock.getRight());
if (i == 0) {
store.setChainHead(storedBlock);
}
store.put(storedBlock);
}
// Create a new instance of the store
RepositoryBlockStore store2 = new RepositoryBlockStore(config, repository, PrecompiledContracts.BRIDGE_ADDR);
// Check a specific block that used to fail when we had a bug
assertEquals(store.get(Sha256Hash.wrap("373941fe83961cf70e181e468abc5f9f7cc440c711c3d06948fa66f3912ed27a")), store2.get(Sha256Hash.wrap("373941fe83961cf70e181e468abc5f9f7cc440c711c3d06948fa66f3912ed27a")));
// Check new instance content is identical to the original one
StoredBlock storedBlock = store.getChainHead();
StoredBlock storedBlock2 = store2.getChainHead();
int headHeight = storedBlock.getHeight();
for (int i = 0; i < headHeight; i++) {
assertNotNull(storedBlock);
assertEquals(storedBlock, storedBlock2);
Sha256Hash prevBlockHash = storedBlock.getHeader().getPrevBlockHash();
storedBlock = store.get(prevBlockHash);
storedBlock2 = store2.get(prevBlockHash);
}
}
use of co.rsk.bitcoinj.core.Sha256Hash in project rskj by rsksmart.
the class BridgeState method create.
public static BridgeState create(BridgeConstants bridgeConstants, byte[] data) throws IOException {
RLPList rlpList = (RLPList) RLP.decode2(data).get(0);
byte[] btcBlockchainBestChainHeightBytes = rlpList.get(0).getRLPData();
int btcBlockchainBestChainHeight = btcBlockchainBestChainHeightBytes == null ? 0 : (new BigInteger(1, btcBlockchainBestChainHeightBytes)).intValue();
byte[] btcTxHashesAlreadyProcessedBytes = rlpList.get(1).getRLPData();
Map<Sha256Hash, Long> btcTxHashesAlreadyProcessed = BridgeSerializationUtils.deserializeMapOfHashesToLong(btcTxHashesAlreadyProcessedBytes);
byte[] btcUTXOsBytes = rlpList.get(2).getRLPData();
List<UTXO> btcUTXOs = BridgeSerializationUtils.deserializeUTXOList(btcUTXOsBytes);
byte[] rskTxsWaitingForSignaturesBytes = rlpList.get(3).getRLPData();
SortedMap<Keccak256, BtcTransaction> rskTxsWaitingForSignatures = BridgeSerializationUtils.deserializeMap(rskTxsWaitingForSignaturesBytes, bridgeConstants.getBtcParams(), false);
byte[] releaseRequestQueueBytes = rlpList.get(4).getRLPData();
ReleaseRequestQueue releaseRequestQueue = BridgeSerializationUtils.deserializeReleaseRequestQueue(releaseRequestQueueBytes, bridgeConstants.getBtcParams());
byte[] releaseTransactionSetBytes = rlpList.get(5).getRLPData();
ReleaseTransactionSet releaseTransactionSet = BridgeSerializationUtils.deserializeReleaseTransactionSet(releaseTransactionSetBytes, bridgeConstants.getBtcParams());
return new BridgeState(btcBlockchainBestChainHeight, btcTxHashesAlreadyProcessed, btcUTXOs, rskTxsWaitingForSignatures, releaseRequestQueue, releaseTransactionSet);
}
Aggregations