Search in sources :

Example 11 with BlockHeader

use of io.nuls.core.chain.entity.BlockHeader in project nuls by nuls-io.

the class HeaderHashValidator method validate.

@Override
public ValidateResult validate(BlockHeader data) {
    ValidateResult result = ValidateResult.getSuccessResult();
    NulsDigestData hash = data.getHash();
    P2PKHScriptSig scriptSig = data.getScriptSig();
    NulsDigestData cfmHash = null;
    try {
        BlockHeader newHeader = new BlockHeader();
        newHeader.parse(data.serialize());
        cfmHash = newHeader.getHash();
    } catch (Exception e) {
        Log.error(e);
    } finally {
        data.setScriptSig(scriptSig);
    }
    if (!cfmHash.getDigestHex().equals(hash.getDigestHex())) {
        result = ValidateResult.getFailedResult(ERROR_MESSAGE);
    }
    return result;
}
Also used : P2PKHScriptSig(io.nuls.core.script.P2PKHScriptSig) ValidateResult(io.nuls.core.validate.ValidateResult) NulsDigestData(io.nuls.core.chain.entity.NulsDigestData) BlockHeader(io.nuls.core.chain.entity.BlockHeader) IOException(java.io.IOException)

Example 12 with BlockHeader

use of io.nuls.core.chain.entity.BlockHeader in project nuls by nuls-io.

the class HeaderPackerValidator method validate.

@Override
public ValidateResult validate(BlockHeader header) {
    BlockHeader preHeader = null;
    try {
        preHeader = NulsContext.getServiceBean(BlockService.class).getBlockHeader(header.getPreHash());
    } catch (NulsException e) {
    // todo
    }
    PocMeetingRound currentRound = consensusManager.getCurrentRound();
    if (header.getHeight() == 0) {
        return ValidateResult.getSuccessResult();
    }
    if (preHeader == null) {
        return ValidateResult.getSuccessResult();
    }
    BlockRoundData roundData = null;
    try {
        roundData = new BlockRoundData(preHeader.getExtend());
    } catch (NulsException e) {
        Log.error(e);
        return ValidateResult.getFailedResult(e.getMessage());
    }
    if (null == currentRound) {
        return ValidateResult.getSuccessResult();
    }
    if (currentRound.getIndex() == roundData.getRoundIndex() && currentRound.getMemberCount() > (roundData.getPackingIndexOfRound() + 1)) {
        if (currentRound.indexOf(header.getPackingAddress()) <= roundData.getPackingIndexOfRound()) {
            return ValidateResult.getFailedResult(ERROR_MESSAGE);
        }
    }
    byte[] packingHash160 = AccountTool.getHash160ByAddress(header.getPackingAddress());
    byte[] hash160InScriptSig = header.getScriptSig().getSignerHash160();
    if (!Arrays.equals(packingHash160, hash160InScriptSig)) {
        return ValidateResult.getFailedResult(ERROR_MESSAGE);
    }
    BlockRoundData nowRoundData = null;
    try {
        nowRoundData = new BlockRoundData(header.getExtend());
    } catch (NulsException e) {
        Log.error(e);
        return ValidateResult.getFailedResult(ERROR_MESSAGE);
    }
    if (!isAdjacent(roundData, nowRoundData)) {
        return ValidateResult.getFailedResult(ERROR_MESSAGE);
    }
    return ValidateResult.getSuccessResult();
}
Also used : NulsException(io.nuls.core.exception.NulsException) PocMeetingRound(io.nuls.consensus.entity.meeting.PocMeetingRound) BlockRoundData(io.nuls.consensus.entity.block.BlockRoundData) BlockHeader(io.nuls.core.chain.entity.BlockHeader)

Example 13 with BlockHeader

use of io.nuls.core.chain.entity.BlockHeader in project nuls by nuls-io.

the class BlockCacheBuffer method getBlock.

public Block getBlock(String hash) {
    if (null == txsCacheMap || headerCacheMap == null) {
        return null;
    }
    List<Transaction> txs = txsCacheMap.get(hash);
    BlockHeader header = headerCacheMap.get(hash);
    if (null == header || null == txs || txs.isEmpty()) {
        return null;
    }
    Block block = new Block();
    block.setHeader(header);
    block.setTxs(txs);
    return block;
}
Also used : Transaction(io.nuls.core.chain.entity.Transaction) Block(io.nuls.core.chain.entity.Block) BlockHeader(io.nuls.core.chain.entity.BlockHeader)

Example 14 with BlockHeader

use of io.nuls.core.chain.entity.BlockHeader in project nuls by nuls-io.

the class BlockStorageService method save.

public void save(Block block) {
    BlockHeader header = block.getHeader();
    header.setSize(block.size());
    headerDao.save(ConsensusTool.toPojo(header));
}
Also used : BlockHeader(io.nuls.core.chain.entity.BlockHeader)

Example 15 with BlockHeader

use of io.nuls.core.chain.entity.BlockHeader in project nuls by nuls-io.

the class BlockStorageService method getBlockHeaderList.

public List<BlockHeader> getBlockHeaderList(long startHeight, long endHeight, long split) {
    List<BlockHeaderPo> strList = this.headerDao.getHashList(startHeight, endHeight, split);
    Map<Long, BlockHeader> headerMap = new HashMap<>();
    for (BlockHeaderPo po : strList) {
        BlockHeader header = new BlockHeader();
        header.setHash(NulsDigestData.fromDigestHex(po.getHash()));
        header.setHeight(po.getHeight());
        headerMap.put(po.getHeight(), header);
    }
    if ((endHeight - startHeight + 1) == headerMap.size()) {
        return new ArrayList<>(headerMap.values());
    }
    List<BlockHeader> headerList = new ArrayList<>();
    for (long i = startHeight; i <= endHeight; i++) {
        if (headerMap.containsKey(i)) {
            headerList.add(headerMap.get(i));
            continue;
        }
        BlockHeader header = blockCacheManager.getBlockHeader(i);
        if (null == header) {
            Block block = blockCacheManager.getBlock(i);
            if (null != block) {
                header = block.getHeader();
            }
        }
        if (null != header) {
            headerList.add(header);
        }
    }
    return headerList;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Block(io.nuls.core.chain.entity.Block) BlockHeader(io.nuls.core.chain.entity.BlockHeader) BlockHeaderPo(io.nuls.db.entity.BlockHeaderPo)

Aggregations

BlockHeader (io.nuls.core.chain.entity.BlockHeader)19 Block (io.nuls.core.chain.entity.Block)11 NulsException (io.nuls.core.exception.NulsException)6 Transaction (io.nuls.core.chain.entity.Transaction)5 BlockHeaderPo (io.nuls.db.entity.BlockHeaderPo)4 ArrayList (java.util.ArrayList)3 BlockRoundData (io.nuls.consensus.entity.block.BlockRoundData)2 NulsDigestData (io.nuls.core.chain.entity.NulsDigestData)2 ValidateResult (io.nuls.core.validate.ValidateResult)2 BlockDto (io.nuls.rpc.entity.BlockDto)2 RpcResult (io.nuls.rpc.entity.RpcResult)2 BlockHashResponse (io.nuls.consensus.entity.BlockHashResponse)1 GetSmallBlockParam (io.nuls.consensus.entity.GetSmallBlockParam)1 BestCorrectBlock (io.nuls.consensus.entity.block.BestCorrectBlock)1 PocMeetingRound (io.nuls.consensus.entity.meeting.PocMeetingRound)1 BlockHeaderEvent (io.nuls.consensus.event.BlockHeaderEvent)1 GetBlockHeaderEvent (io.nuls.consensus.event.GetBlockHeaderEvent)1 GetSmallBlockRequest (io.nuls.consensus.event.GetSmallBlockRequest)1 BlockInfo (io.nuls.consensus.utils.BlockInfo)1 NulsRuntimeException (io.nuls.core.exception.NulsRuntimeException)1