Search in sources :

Example 31 with BlockHeader

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

the class CheckingBestHeaderSyncState method newBlockHeaders.

@Override
public void newBlockHeaders(List<BlockHeader> chunk) {
    BlockHeader header = chunk.get(0);
    if (!ByteUtil.fastEquals(header.getHash().getBytes(), miniChunk.getHash()) || !syncInformation.blockHeaderIsValid(header)) {
        syncEventsHandler.onErrorSyncing("Invalid chunk received from node {}", EventType.INVALID_HEADER, this.getClass(), syncInformation.getSelectedPeerId());
        return;
    }
    syncEventsHandler.startFindingConnectionPoint();
}
Also used : BlockHeader(org.ethereum.core.BlockHeader)

Example 32 with BlockHeader

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

the class NodeBlockProcessor method processBlockHeadersRequest.

/**
 * processBlockHeadersRequest sends a list of block headers.
 *
 * @param sender the sender of the BlockHeadersRequest message.
 * @param requestId the id of the request
 * @param hash   the hash of the block to be processed
 * @param count  the number of headers to send
 */
@Override
public void processBlockHeadersRequest(@Nonnull final MessageChannel sender, long requestId, @Nonnull final byte[] hash, int count) {
    logger.trace("Processing headers request {} {} from {}", requestId, Hex.toHexString(hash).substring(0, 10), sender.getPeerNodeID());
    Block block = blockSyncService.getBlockFromStoreOrBlockchain(hash);
    if (block == null) {
        return;
    }
    List<BlockHeader> headers = new ArrayList<>();
    headers.add(block.getHeader());
    for (int k = 1; k < count; k++) {
        block = blockSyncService.getBlockFromStoreOrBlockchain(block.getParentHash().getBytes());
        if (block == null) {
            break;
        }
        headers.add(block.getHeader());
    }
    BlockHeadersResponseMessage response = new BlockHeadersResponseMessage(requestId, headers);
    sender.sendMessage(response);
}
Also used : Block(org.ethereum.core.Block) BlockHeader(org.ethereum.core.BlockHeader)

Example 33 with BlockHeader

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

the class FamilyUtils method getUnclesHeaders.

public static List<BlockHeader> getUnclesHeaders(@Nonnull BlockStore store, long blockNumber, byte[] parentHash, int levels) {
    List<BlockHeader> uncles = new ArrayList<>();
    Set<Keccak256> unclesHeaders = getUncles(store, blockNumber, parentHash, levels);
    for (Keccak256 uncleHash : unclesHeaders) {
        Block uncle = store.getBlockByHash(uncleHash.getBytes());
        if (uncle != null) {
            uncles.add(uncle.getHeader());
        }
    }
    return uncles;
}
Also used : ArrayList(java.util.ArrayList) Block(org.ethereum.core.Block) Keccak256(co.rsk.crypto.Keccak256) BlockHeader(org.ethereum.core.BlockHeader)

Example 34 with BlockHeader

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

the class BlockParentNumberRule method isValid.

@Override
public boolean isValid(Block block, Block parent) {
    if (block == null || parent == null) {
        logger.warn("BlockParentNumberRule - block or parent are null");
        return false;
    }
    BlockHeader header = block.getHeader();
    BlockHeader parentHeader = parent.getHeader();
    if (header.getNumber() != (parentHeader.getNumber() + 1)) {
        logger.warn(String.format("#%d: block number is not parentBlock number + 1", header.getNumber()));
        return false;
    }
    return true;
}
Also used : BlockHeader(org.ethereum.core.BlockHeader)

Example 35 with BlockHeader

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

the class BlockUnclesValidationRule method validateUncleList.

/**
 * Validate an uncle list.
 * It validates that the uncle list is not too large
 * It validates that each uncle
 * - is not an ancestor
 * - is not a used uncle
 * - has a common ancestor with the block
 *
 * @param blockNumber the number of the block containing the uncles
 * @param uncles      the uncle list to validate
 * @param ancestors   the list of direct ancestors of the block containing the uncles
 * @param used        used uncles
 * @return true if the uncles in the list are valid, false if not
 */
public boolean validateUncleList(long blockNumber, List<BlockHeader> uncles, Set<Keccak256> ancestors, Set<Keccak256> used) {
    if (uncles.size() > uncleListLimit) {
        logger.error("Uncle list to big: block.getUncleList().size() > UNCLE_LIST_LIMIT");
        panicProcessor.panic(INVALIDUNCLE, "Uncle list to big: block.getUncleList().size() > UNCLE_LIST_LIMIT");
        return false;
    }
    Set<Keccak256> hashes = new HashSet<>();
    for (BlockHeader uncle : uncles) {
        Block blockForUncleHeader = new Block(uncle);
        if (!this.validations.isValid(blockForUncleHeader) || !validateParentNumber(uncle, blockNumber)) {
            return false;
        }
        Keccak256 uncleHash = uncle.getHash();
        /* Just checking that the uncle is not added twice */
        if (hashes.contains(uncleHash)) {
            return false;
        }
        hashes.add(uncleHash);
        if (!validateUnclesAncestors(ancestors, uncleHash) || !validateIfUncleWasNeverUsed(used, uncleHash) || !validateUncleParent(ancestors, blockForUncleHeader)) {
            return false;
        }
    }
    return true;
}
Also used : Block(org.ethereum.core.Block) Keccak256(co.rsk.crypto.Keccak256) BlockHeader(org.ethereum.core.BlockHeader) HashSet(java.util.HashSet)

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