Search in sources :

Example 36 with BlockDifficulty

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

Example 37 with BlockDifficulty

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

Example 38 with BlockDifficulty

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

Example 39 with BlockDifficulty

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());
}
Also used : Status(co.rsk.net.Status) BlockDifficulty(co.rsk.core.BlockDifficulty) Test(org.junit.Test) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)

Example 40 with BlockDifficulty

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());
}
Also used : Status(co.rsk.net.Status) BlockDifficulty(co.rsk.core.BlockDifficulty) Block(org.ethereum.core.Block) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) 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