Search in sources :

Example 26 with BlockHeader

use of org.ethereum.core.BlockHeader in project rskj by rsksmart.

the class BlockDifficultyRule method isValid.

@Override
public boolean isValid(Block block, Block parent) {
    if (block == null || parent == null) {
        logger.warn("BlockDifficultyRule - block or parent are null");
        return false;
    }
    BlockHeader header = block.getHeader();
    BlockDifficulty calcDifficulty = difficultyCalculator.calcDifficulty(header, parent.getHeader());
    BlockDifficulty difficulty = header.getDifficulty();
    if (!difficulty.equals(calcDifficulty)) {
        logger.warn("#{}: difficulty != calcDifficulty", header.getNumber());
        return false;
    }
    return true;
}
Also used : BlockDifficulty(co.rsk.core.BlockDifficulty) BlockHeader(org.ethereum.core.BlockHeader)

Example 27 with BlockHeader

use of org.ethereum.core.BlockHeader in project rskj by rsksmart.

the class BlockParentGasLimitRule method isValid.

@Override
public boolean isValid(Block block, Block parent) {
    if (block == null || parent == null) {
        logger.warn("BlockParentGasLimitRule - block or parent are null");
        return false;
    }
    BlockHeader header = block.getHeader();
    BigInteger headerGasLimit = new BigInteger(1, header.getGasLimit());
    BigInteger parentGasLimit = new BigInteger(1, parent.getGasLimit());
    if (headerGasLimit.compareTo(parentGasLimit.multiply(BigInteger.valueOf(gasLimitBoundDivisor - 1L)).divide(BigInteger.valueOf(gasLimitBoundDivisor))) < 0 || headerGasLimit.compareTo(parentGasLimit.multiply(BigInteger.valueOf(gasLimitBoundDivisor + 1L)).divide(BigInteger.valueOf(gasLimitBoundDivisor))) > 0) {
        logger.warn(String.format("#%d: gas limit exceeds parentBlock.getGasLimit() (+-) GAS_LIMIT_BOUND_DIVISOR", header.getNumber()));
        return false;
    }
    return true;
}
Also used : BigInteger(java.math.BigInteger) BlockHeader(org.ethereum.core.BlockHeader)

Example 28 with BlockHeader

use of org.ethereum.core.BlockHeader in project rskj by rsksmart.

the class ValidGasUsedRule method isValid.

@Override
public boolean isValid(Block block) {
    BlockHeader header = block.getHeader();
    long gasUsed = header.getGasUsed();
    long gasLimit = new BigInteger(1, header.getGasLimit()).longValue();
    if (gasUsed < 0 || gasUsed > gasLimit) {
        logger.warn("Block gas used is less than 0 or more than the gas limit of the block");
        panicProcessor.panic("invalidGasValue", "Block gas used is less than 0 or more than the gas limit of the block");
        return false;
    }
    return true;
}
Also used : BigInteger(java.math.BigInteger) BlockHeader(org.ethereum.core.BlockHeader)

Example 29 with BlockHeader

use of org.ethereum.core.BlockHeader in project rskj by rsksmart.

the class BlockHeadersMessage method encode.

private void encode() {
    List<byte[]> encodedElements = new ArrayList<>();
    for (BlockHeader blockHeader : blockHeaders) {
        encodedElements.add(blockHeader.getEncoded());
    }
    byte[][] encodedElementArray = encodedElements.toArray(new byte[encodedElements.size()][]);
    this.encoded = RLP.encodeList(encodedElementArray);
}
Also used : ArrayList(java.util.ArrayList) BlockHeader(org.ethereum.core.BlockHeader)

Example 30 with BlockHeader

use of org.ethereum.core.BlockHeader in project rskj by rsksmart.

the class BlockUtilsTest method unknowAncestorsHashesUsingUncles.

@Test
public void unknowAncestorsHashesUsingUncles() {
    BlockChainBuilder blockChainBuilder = new BlockChainBuilder();
    BlockGenerator blockGenerator = new BlockGenerator();
    Genesis genesis = blockGenerator.getGenesisBlock();
    BlockChainImpl blockChain = blockChainBuilder.setGenesis(genesis).build();
    BlockStore store = new BlockStore();
    genesis.setStateRoot(blockChain.getRepository().getRoot());
    genesis.flushRLP();
    BlockBuilder blockBuilder = new BlockBuilder(blockChain, blockGenerator);
    Block block1 = blockBuilder.parent(genesis).build();
    Block block1b = blockBuilder.parent(genesis).build();
    Block block2 = blockBuilder.parent(block1).build();
    Block uncle1 = blockBuilder.parent(block1).build();
    Block uncle2 = blockBuilder.parent(block1).build();
    List<BlockHeader> uncles = new ArrayList<>();
    uncles.add(uncle1.getHeader());
    uncles.add(uncle2.getHeader());
    Block block3 = blockBuilder.parent(block2).uncles(uncles).build();
    store.saveBlock(block3);
    blockChain.tryToConnect(genesis);
    blockChain.tryToConnect(block1);
    blockChain.tryToConnect(block1b);
    Set<Keccak256> hashes = BlockUtils.unknownAncestorsHashes(genesis.getHash(), blockChain, store);
    Assert.assertNotNull(hashes);
    Assert.assertTrue(hashes.isEmpty());
    hashes = BlockUtils.unknownAncestorsHashes(block1.getHash(), blockChain, store);
    Assert.assertNotNull(hashes);
    Assert.assertTrue(hashes.isEmpty());
    hashes = BlockUtils.unknownAncestorsHashes(block1b.getHash(), blockChain, store);
    Assert.assertNotNull(hashes);
    Assert.assertTrue(hashes.isEmpty());
    hashes = BlockUtils.unknownAncestorsHashes(block2.getHash(), blockChain, store);
    Assert.assertNotNull(hashes);
    Assert.assertFalse(hashes.isEmpty());
    Assert.assertEquals(1, hashes.size());
    Assert.assertTrue(hashes.contains(block2.getHash()));
    hashes = BlockUtils.unknownAncestorsHashes(block3.getHash(), blockChain, store);
    Assert.assertNotNull(hashes);
    Assert.assertFalse(hashes.isEmpty());
    Assert.assertEquals(3, hashes.size());
    Assert.assertTrue(hashes.contains(block2.getHash()));
    Assert.assertTrue(hashes.contains(uncle1.getHash()));
    Assert.assertTrue(hashes.contains(uncle2.getHash()));
}
Also used : BlockStore(co.rsk.net.BlockStore) ArrayList(java.util.ArrayList) Genesis(org.ethereum.core.Genesis) Block(org.ethereum.core.Block) Keccak256(co.rsk.crypto.Keccak256) BlockHeader(org.ethereum.core.BlockHeader) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) BlockBuilder(co.rsk.test.builders.BlockBuilder) Test(org.junit.Test)

Aggregations

BlockHeader (org.ethereum.core.BlockHeader)65 Test (org.junit.Test)36 Block (org.ethereum.core.Block)27 ArrayList (java.util.ArrayList)13 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)9 BlockDifficulty (co.rsk.core.BlockDifficulty)5 Keccak256 (co.rsk.crypto.Keccak256)5 BlockChainImpl (co.rsk.core.bc.BlockChainImpl)4 BlockBuilder (co.rsk.test.builders.BlockBuilder)4 BlockChainBuilder (co.rsk.test.builders.BlockChainBuilder)4 BlockStore (org.ethereum.db.BlockStore)4 SimpleMessageChannel (co.rsk.net.simples.SimpleMessageChannel)3 BigInteger (java.math.BigInteger)3 MainNetConfig (org.ethereum.config.net.MainNetConfig)3 DataWord (org.ethereum.vm.DataWord)3 DifficultyCalculator (co.rsk.core.DifficultyCalculator)2 BlockChainImplTest (co.rsk.core.bc.BlockChainImplTest)2 HashSet (java.util.HashSet)2 Bloom (org.ethereum.core.Bloom)2 Transaction (org.ethereum.core.Transaction)2