use of co.rsk.core.BlockDifficulty in project rskj by rsksmart.
the class DifficultyRule method validate.
@Override
public boolean validate(BlockHeader header, BlockHeader parent) {
BlockDifficulty calcDifficulty = difficultyCalculator.calcDifficulty(header, parent);
BlockDifficulty difficulty = header.getDifficulty();
if (!difficulty.equals(calcDifficulty)) {
logger.error("#{}: difficulty != calcDifficulty", header.getNumber());
return false;
}
return true;
}
use of co.rsk.core.BlockDifficulty in project rskj by rsksmart.
the class ExportBlocks method exportBlocks.
private void exportBlocks(String[] args, BlockStore blockStore, PrintStream writer) {
long fromBlockNumber = Long.parseLong(args[0]);
long toBlockNumber = Long.parseLong(args[1]);
for (long n = fromBlockNumber; n <= toBlockNumber; n++) {
Block block = blockStore.getChainBlockByNumber(n);
BlockDifficulty totalDifficulty = blockStore.getTotalDifficultyForHash(block.getHash().getBytes());
writer.println(block.getNumber() + "," + ByteUtil.toHexString(block.getHash().getBytes()) + "," + ByteUtil.toHexString(totalDifficulty.getBytes()) + "," + ByteUtil.toHexString(block.getEncoded()));
}
}
use of co.rsk.core.BlockDifficulty in project rskj by rsksmart.
the class ImportBlocks method importBlocks.
private void importBlocks(BlockFactory blockFactory, BlockStore blockStore, BufferedReader reader) throws IOException {
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
String[] parts = line.split(",");
if (parts.length < 4) {
continue;
}
byte[] encoded = Hex.decode(parts[3]);
Block block = blockFactory.decodeBlock(encoded);
BlockDifficulty totalDifficulty = new BlockDifficulty(new BigInteger(Hex.decode(parts[2])));
blockStore.saveBlock(block, totalDifficulty, true);
}
blockStore.flush();
}
use of co.rsk.core.BlockDifficulty in project rskj by rsksmart.
the class MessageTest method encodeDecodeStatusMessageWithCompleteArguments.
@Test
public void encodeDecodeStatusMessageWithCompleteArguments() {
Block block = blockGenerator.getBlock(1);
Status status = new Status(block.getNumber(), block.getHash().getBytes(), block.getParentHash().getBytes(), new BlockDifficulty(BigInteger.TEN));
StatusMessage message = new StatusMessage(status);
byte[] encoded = message.getEncoded();
Message result = Message.create(blockFactory, encoded);
Assert.assertNotNull(result);
Assert.assertArrayEquals(encoded, result.getEncoded());
Assert.assertEquals(MessageType.STATUS_MESSAGE, result.getMessageType());
StatusMessage newmessage = (StatusMessage) result;
Assert.assertArrayEquals(block.getHash().getBytes(), newmessage.getStatus().getBestBlockHash());
Assert.assertEquals(block.getNumber(), newmessage.getStatus().getBestBlockNumber());
}
use of co.rsk.core.BlockDifficulty in project rskj by rsksmart.
the class StatusMessageTest method createWithCompleteArguments.
@Test
public void createWithCompleteArguments() {
BlockGenerator blockGenerator = new BlockGenerator();
Block genesis = blockGenerator.getGenesisBlock();
Block block = blockGenerator.createChildBlock(genesis);
Status status = new Status(block.getNumber(), block.getHash().getBytes(), block.getParentHash().getBytes(), new BlockDifficulty(BigInteger.TEN));
StatusMessage message = new StatusMessage(status);
Assert.assertEquals(MessageType.STATUS_MESSAGE, message.getMessageType());
Assert.assertSame(status, message.getStatus());
Assert.assertEquals(1, message.getStatus().getBestBlockNumber());
Assert.assertArrayEquals(block.getHash().getBytes(), message.getStatus().getBestBlockHash());
Assert.assertNotNull(message.getStatus().getBestBlockParentHash());
Assert.assertArrayEquals(block.getParentHash().getBytes(), message.getStatus().getBestBlockParentHash());
Assert.assertNotNull(message.getStatus().getTotalDifficulty());
Assert.assertEquals(new BlockDifficulty(BigInteger.TEN), message.getStatus().getTotalDifficulty());
}
Aggregations