use of co.rsk.bitcoinj.core.StoredBlock in project rskj by rsksmart.
the class RepositoryBtcBlockStoreWithCacheTest method checkDifferentInstancesWithSameRepoHaveSameContentTest.
@Test
public void checkDifferentInstancesWithSameRepoHaveSameContentTest() 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 = createRepository();
BtcBlockStoreWithCache.Factory btcBlockStoreFactory = new RepositoryBtcBlockStoreWithCache.Factory(bridgeConstants.getBtcParams());
BtcBlockStoreWithCache store = btcBlockStoreFactory.newInstance(repository, bridgeConstants, mock(BridgeStorageProvider.class), mock(ActivationConfig.ForBlock.class));
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
BtcBlockStoreWithCache store2 = btcBlockStoreFactory.newInstance(repository, bridgeConstants, mock(BridgeStorageProvider.class), mock(ActivationConfig.ForBlock.class));
// 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 RepositoryBtcBlockStoreWithCacheTest method getStoredBlockAtMainChainHeight_postIris_heightLowerThanMaxDepth_limitInBtcHeightWhenBlockIndexActivates.
@Test(expected = BlockStoreException.class)
public void getStoredBlockAtMainChainHeight_postIris_heightLowerThanMaxDepth_limitInBtcHeightWhenBlockIndexActivates() throws BlockStoreException {
Repository repository = createRepository();
BtcBlockStoreWithCache.Factory btcBlockStoreFactory = new RepositoryBtcBlockStoreWithCache.Factory(bridgeConstants.getBtcParams());
BridgeStorageProvider provider = mock(BridgeStorageProvider.class);
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
when(activations.isActive(ConsensusRule.RSKIP199)).thenReturn(true);
BtcBlockStoreWithCache btcBlockStore = btcBlockStoreFactory.newInstance(repository, bridgeConstants, provider, activations);
BtcBlock genesis = networkParameters.getGenesisBlock();
int btcHeightWhenBlockIndexActivates = bridgeConstants.getBtcHeightWhenBlockIndexActivates();
int maxDepthToSearchBlocksBelowIndexActivation = bridgeConstants.getMaxDepthToSearchBlocksBelowIndexActivation();
int blockHeight = btcHeightWhenBlockIndexActivates + maxDepthToSearchBlocksBelowIndexActivation + 1;
StoredBlock storedBlock1 = createStoredBlock(genesis, blockHeight, 0);
btcBlockStore.put(storedBlock1);
btcBlockStore.setChainHead(storedBlock1);
assertEquals(storedBlock1, btcBlockStore.getChainHead());
// Search for a block in a height lower than the max depth, should fail
// Since the chain height is above btcHeightWhenBlockIndexActivates + maxDepthToSearchBlocksBelowIndexActivation
int maxDepth = btcHeightWhenBlockIndexActivates;
btcBlockStore.getStoredBlockAtMainChainHeight(maxDepth - 1);
}
use of co.rsk.bitcoinj.core.StoredBlock in project rskj by rsksmart.
the class RepositoryBtcBlockStoreWithCacheTest method getStoredBlockAtMainChainDepth_Error.
@Test
public void getStoredBlockAtMainChainDepth_Error() throws BlockStoreException {
BtcBlockStoreWithCache btcBlockStore = createBlockStore();
BtcBlock parent = networkParameters.getGenesisBlock();
BtcBlock blockHeader1 = new BtcBlock(networkParameters, 2L, parent.getHash(), Sha256Hash.ZERO_HASH, parent.getTimeSeconds() + 1, parent.getDifficultyTarget(), 0, new ArrayList<>());
StoredBlock storedBlock1 = new StoredBlock(blockHeader1, new BigInteger("0"), 2);
btcBlockStore.put(storedBlock1);
parent = blockHeader1;
BtcBlock blockHeader2 = new BtcBlock(networkParameters, 2L, parent.getHash(), Sha256Hash.ZERO_HASH, parent.getTimeSeconds() + 1, parent.getDifficultyTarget(), 0, new ArrayList<>());
StoredBlock storedBlock2 = new StoredBlock(blockHeader2, new BigInteger("0"), 2);
btcBlockStore.put(storedBlock2);
btcBlockStore.setChainHead(storedBlock2);
// getStoredBlockAtMainChainDepth should fail as the block is at a inconsistent height
try {
btcBlockStore.getStoredBlockAtMainChainDepth(1);
fail();
} catch (BlockStoreException e) {
assertTrue(true);
}
// getStoredBlockAtMainChainHeight should fail as the block is at a inconsistent height
try {
btcBlockStore.getStoredBlockAtMainChainHeight(1);
fail();
} catch (BlockStoreException e) {
assertTrue(true);
}
}
use of co.rsk.bitcoinj.core.StoredBlock in project rskj by rsksmart.
the class BridgeSupport method getPrevBlockAtHeight.
private StoredBlock getPrevBlockAtHeight(StoredBlock cursor, int height) throws BlockStoreException {
if (cursor.getHeight() == height) {
return cursor;
}
boolean stop = false;
StoredBlock current = cursor;
while (!stop) {
current = current.getPrev(this.btcBlockStore);
stop = current.getHeight() == height;
}
return current;
}
use of co.rsk.bitcoinj.core.StoredBlock in project rskj by rsksmart.
the class BridgeSupport method getBtcBlockchainBlockHashAtDepth.
public Sha256Hash getBtcBlockchainBlockHashAtDepth(int depth) throws BlockStoreException, IOException {
Context.propagate(btcContext);
this.ensureBtcBlockStore();
StoredBlock head = btcBlockStore.getChainHead();
int maxDepth = head.getHeight() - getLowestBlock().getHeight();
if (depth < 0 || depth > maxDepth) {
throw new IndexOutOfBoundsException(String.format("Depth must be between 0 and %d", maxDepth));
}
StoredBlock blockAtDepth = btcBlockStore.getStoredBlockAtMainChainDepth(depth);
return blockAtDepth.getHeader().getHash();
}
Aggregations