use of co.rsk.bitcoinj.core.StoredBlock 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.StoredBlock in project rskj by rsksmart.
the class SimpleBlockChain method add.
@Override
public boolean add(BtcBlock block) {
StoredBlock sblock = new StoredBlock(block, BigInteger.ONE, 1);
try {
this.blockStore.put(sblock);
this.block = sblock;
this.setChainHead(sblock);
} catch (BlockStoreException e) {
e.printStackTrace();
return false;
}
return true;
}
use of co.rsk.bitcoinj.core.StoredBlock 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.StoredBlock 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.StoredBlock in project rskj by rsksmart.
the class RepositoryBtcBlockStoreWithCache method get.
@Override
public synchronized StoredBlock get(Sha256Hash hash) {
logger.trace("[get] Looking in storage for block with hash {}", hash);
byte[] ba = repository.getStorageBytes(contractAddress, DataWord.valueFromHex(hash.toString()));
if (ba == null) {
logger.trace("[get] Block with hash {} not found in storage", hash);
return null;
}
StoredBlock storedBlock = byteArrayToStoredBlock(ba);
return storedBlock;
}
Aggregations