Search in sources :

Example 6 with BlockDifficulty

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);
}
Also used : BlockChainStatus(co.rsk.core.bc.BlockChainStatus) BlockDifficulty(co.rsk.core.BlockDifficulty) Block(org.ethereum.core.Block) BlockChainStatus(co.rsk.core.bc.BlockChainStatus)

Example 7 with BlockDifficulty

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);
}
Also used : BlockDifficulty(co.rsk.core.BlockDifficulty)

Example 8 with BlockDifficulty

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();
}
Also used : BlockDifficulty(co.rsk.core.BlockDifficulty) RLPElement(org.ethereum.util.RLPElement) Block(org.ethereum.core.Block) BigInteger(java.math.BigInteger) RLPList(org.ethereum.util.RLPList)

Example 9 with BlockDifficulty

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;
}
Also used : BlockDifficulty(co.rsk.core.BlockDifficulty) BlockChainImpl(co.rsk.core.bc.BlockChainImpl) Block(org.ethereum.core.Block) RewindBlocks(co.rsk.cli.tools.RewindBlocks)

Example 10 with BlockDifficulty

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());
}
Also used : BlockDifficulty(co.rsk.core.BlockDifficulty) Blockchain(org.ethereum.core.Blockchain) Block(org.ethereum.core.Block) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) TestSystemProperties(co.rsk.config.TestSystemProperties) SyncConfiguration(co.rsk.net.sync.SyncConfiguration) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

BlockDifficulty (co.rsk.core.BlockDifficulty)60 Test (org.junit.Test)32 Block (org.ethereum.core.Block)23 BigInteger (java.math.BigInteger)14 HashMapDB (org.ethereum.datasource.HashMapDB)11 Keccak256 (co.rsk.crypto.Keccak256)9 Ignore (org.junit.Ignore)9 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)8 BlockHeader (org.ethereum.core.BlockHeader)7 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)6 KeyValueDataSource (org.ethereum.datasource.KeyValueDataSource)6 Coin (co.rsk.core.Coin)5 MapDBBlocksIndex (co.rsk.db.MapDBBlocksIndex)5 LevelDbDataSource (org.ethereum.datasource.LevelDbDataSource)5 BlockStore (org.ethereum.db.BlockStore)5 DifficultyCalculator (co.rsk.core.DifficultyCalculator)4 BlockChainImpl (co.rsk.core.bc.BlockChainImpl)4 HashMapBlocksIndex (co.rsk.db.HashMapBlocksIndex)4 BlockChainBuilder (co.rsk.test.builders.BlockChainBuilder)4 ArrayList (java.util.ArrayList)4