use of co.rsk.core.BlockDifficulty in project rskj by rsksmart.
the class NodeMessageHandler method sendStatusToAll.
private synchronized void sendStatusToAll() {
BlockChainStatus blockChainStatus = this.blockProcessor.getBlockchain().getStatus();
Block block = blockChainStatus.getBestBlock();
BlockDifficulty totalDifficulty = blockChainStatus.getTotalDifficulty();
Status status = new Status(block.getNumber(), block.getHash().getBytes(), block.getParentHash().getBytes(), totalDifficulty);
logger.trace("Sending status best block to all {} {}", status.getBestBlockNumber(), Hex.toHexString(status.getBestBlockHash()).substring(0, 8));
this.channelManager.broadcastStatus(status);
}
use of co.rsk.core.BlockDifficulty in project rskj by rsksmart.
the class SyncPeerStatus method peerTotalDifficultyComparator.
public int peerTotalDifficultyComparator(SyncPeerStatus other) {
BlockDifficulty ttd = this.status.getTotalDifficulty();
BlockDifficulty otd = other.status.getTotalDifficulty();
if (ttd == null && otd == null) {
return 0;
}
if (ttd == null) {
return -1;
}
if (otd == null) {
return 1;
}
return ttd.compareTo(otd);
}
use of co.rsk.core.BlockDifficulty in project rskj by rsksmart.
the class BootstrapImporter method insertBlocks.
private static void insertBlocks(BlockStore blockStore, BlockFactory blockFactory, RLPElement encodedTuples) {
RLPList blocksData = RLP.decodeList(encodedTuples.getRLPData());
for (int k = 0; k < blocksData.size(); k++) {
RLPElement element = blocksData.get(k);
RLPList blockData = RLP.decodeList(element.getRLPData());
RLPList tuple = RLP.decodeList(blockData.getRLPData());
Block block = blockFactory.decodeBlock(Objects.requireNonNull(tuple.get(0).getRLPData(), "block data is missing"));
BlockDifficulty blockDifficulty = new BlockDifficulty(new BigInteger(Objects.requireNonNull(tuple.get(1).getRLPData(), "block difficulty data is missing")));
blockStore.saveBlock(block, blockDifficulty, true);
}
blockStore.flush();
}
use of co.rsk.core.BlockDifficulty in project rskj by rsksmart.
the class BlockChainLoader method loadBlockchain.
public BlockChainImpl loadBlockchain() {
BlockDifficulty totalDifficulty;
Block bestBlock = blockStore.getBestBlock();
if (bestBlock == null) {
logger.info("DB is empty - adding Genesis");
bestBlock = genesis;
totalDifficulty = genesis.getCumulativeDifficulty();
listener.onBlock(genesis, new ArrayList<>());
logger.info("Genesis block loaded");
} else {
totalDifficulty = blockStore.getTotalDifficultyForHash(bestBlock.getHash().getBytes());
logger.info("*** Loaded up to block [{}] totalDifficulty [{}] with stateRoot [{}]", bestBlock.getNumber(), totalDifficulty, ByteUtil.toHexString(bestBlock.getStateRoot()));
}
if (!isBlockConsistent(bestBlock)) {
String errorMessage = String.format("Best block is not consistent with the state db. Consider using `%s` cli tool to rewind inconsistent blocks", RewindBlocks.class.getSimpleName());
logger.error(errorMessage);
throw new RuntimeException(errorMessage);
}
BlockChainImpl blockchain = new BlockChainImpl(blockStore, receiptStore, transactionPool, listener, blockValidator, blockExecutor, stateRootHandler);
blockchain.setStatus(bestBlock, totalDifficulty);
return blockchain;
}
use of co.rsk.core.BlockDifficulty in project rskj by rsksmart.
the class NodeBlockProcessorTest method syncingThenNoSyncing.
@Test
@Ignore("Ignored when Process status deleted on block processor")
public void syncingThenNoSyncing() {
NetBlockStore store = new NetBlockStore();
Block block = new BlockGenerator().createBlock(30, 0);
Blockchain blockchain = new BlockChainBuilder().ofSize(0);
BlockNodeInformation nodeInformation = new BlockNodeInformation();
SyncConfiguration syncConfiguration = SyncConfiguration.IMMEDIATE_FOR_TESTING;
TestSystemProperties config = new TestSystemProperties();
BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, syncConfiguration, DummyBlockValidator.VALID_RESULT_INSTANCE);
final NodeBlockProcessor processor = new NodeBlockProcessor(store, blockchain, nodeInformation, blockSyncService, syncConfiguration);
Assert.assertFalse(processor.hasBetterBlockToSync());
// Status status = new Status(block.getNumber(), block.getHash());
// processor.processStatus(new SimpleNodeChannel(null, null), status);
Assert.assertTrue(processor.hasBetterBlockToSync());
Assert.assertTrue(processor.hasBetterBlockToSync());
blockchain.setStatus(block, new BlockDifficulty(BigInteger.valueOf(30)));
Assert.assertFalse(processor.hasBetterBlockToSync());
Assert.assertFalse(processor.hasBetterBlockToSync());
Block block2 = new BlockGenerator().createBlock(60, 0);
// Status status2 = new Status(block2.getNumber(), block2.getHash());
// processor.processStatus(new SimpleNodeChannel(null, null), status2);
Assert.assertTrue(processor.hasBetterBlockToSync());
Assert.assertFalse(processor.hasBetterBlockToSync());
}
Aggregations