Search in sources :

Example 16 with Block

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

the class GetBlocksHashHandler method onEvent.

@Override
public void onEvent(GetBlocksHashRequest event, String fromId) {
    if (event.getEventBody().getEnd() > NulsContext.getInstance().getBestBlock().getHeader().getHeight()) {
        return;
    }
    boolean b = event.getEventBody().getStart() == event.getEventBody().getEnd();
    if (b) {
        BlockHashResponse response = new BlockHashResponse();
        Block block;
        if (event.getEventBody().getEnd() <= 0) {
            block = NulsContext.getInstance().getBestBlock();
        } else {
            block = blockService.getBlock(event.getEventBody().getEnd());
        }
        if (null == block) {
            Log.warn("block can not get:" + event.getEventBody().getEnd());
            return;
        }
        response.put(block.getHeader().getHeight(), block.getHeader().getHash());
        sendResponse(response, fromId);
    } else {
        List<BlockHeader> list = this.blockService.getBlockHeaderList(event.getEventBody().getStart(), event.getEventBody().getEnd(), event.getEventBody().getSplit());
        List<Long> resultHeightList = new ArrayList<>();
        List<NulsDigestData> resultHashList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            resultHeightList.add(list.get(i).getHeight());
            resultHashList.add(list.get(i).getHash());
        }
        if (resultHeightList.isEmpty() || resultHeightList.get(resultHeightList.size() - 1) < event.getEventBody().getEnd()) {
            Block block = this.blockService.getBlock(event.getEventBody().getEnd());
            if (block == null) {
                // todo why?
                Log.warn("block can not get:" + event.getEventBody().getEnd());
                return;
            }
            resultHeightList.add(block.getHeader().getHeight());
            resultHashList.add(block.getHeader().getHash());
        }
        final int size = 50000;
        for (int i = 0; i < resultHashList.size(); i += size) {
            BlockHashResponse response = new BlockHashResponse();
            int end = i + size;
            if (end > resultHeightList.size()) {
                end = resultHeightList.size();
            }
            response.setHeightList(resultHeightList.subList(i, end));
            response.setHashList(resultHashList.subList(i, end));
            sendResponse(response, fromId);
        }
    }
}
Also used : BlockHashResponse(io.nuls.consensus.entity.BlockHashResponse) ArrayList(java.util.ArrayList) Block(io.nuls.core.chain.entity.Block) NulsDigestData(io.nuls.core.chain.entity.NulsDigestData) BlockHeader(io.nuls.core.chain.entity.BlockHeader)

Example 17 with Block

use of io.nuls.core.chain.entity.Block 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 18 with Block

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

the class BlockResource method loadBlock.

@GET
@Path("/hash/{hash}")
@Produces(MediaType.APPLICATION_JSON)
public RpcResult loadBlock(@PathParam("hash") String hash) {
    RpcResult result;
    if (!StringUtils.validHash(hash)) {
        return RpcResult.getFailed(ErrorCode.PARAMETER_ERROR);
    }
    Block block = blockService.getBlock(hash);
    if (block == null) {
        result = RpcResult.getFailed(ErrorCode.DATA_NOT_FOUND);
    } else {
        long reward = ledgerService.getBlockReward(block.getHeader().getHeight());
        long fee = ledgerService.getBlockFee(block.getHeader().getHeight());
        result = RpcResult.getSuccess();
        try {
            result.setData(new BlockDto(block, reward, fee));
        } catch (IOException e) {
            // todo
            e.printStackTrace();
        }
    }
    return result;
}
Also used : RpcResult(io.nuls.rpc.entity.RpcResult) Block(io.nuls.core.chain.entity.Block) IOException(java.io.IOException) BlockDto(io.nuls.rpc.entity.BlockDto)

Example 19 with Block

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

the class BlockMaintenanceThread method checkGenesisBlock.

public void checkGenesisBlock() throws Exception {
    Block genesisBlock = NulsContext.getInstance().getGenesisBlock();
    ValidateResult result = genesisBlock.verify();
    if (result.isFailed()) {
        throw new NulsRuntimeException(ErrorCode.DATA_ERROR, result.getMessage());
    }
    Block localGenesisBlock = this.blockService.getGengsisBlock();
    if (null == localGenesisBlock) {
        for (Transaction tx : genesisBlock.getTxs()) {
            ledgerService.approvalTx(tx);
        }
        this.blockService.saveBlock(genesisBlock);
        return;
    }
    localGenesisBlock.verify();
    String logicHash = genesisBlock.getHeader().getHash().getDigestHex();
    String localHash = localGenesisBlock.getHeader().getHash().getDigestHex();
    if (!logicHash.equals(localHash)) {
        throw new NulsRuntimeException(ErrorCode.DATA_ERROR);
    }
}
Also used : Transaction(io.nuls.core.chain.entity.Transaction) ValidateResult(io.nuls.core.validate.ValidateResult) Block(io.nuls.core.chain.entity.Block) BestCorrectBlock(io.nuls.consensus.entity.block.BestCorrectBlock) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException)

Example 20 with Block

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

the class ConsensusMeetingRunner method packing.

private void packing(PocMeetingMember self) throws NulsException, IOException {
    Block bestBlock = context.getBestBlock();
    List<Transaction> txList = txCacheManager.getTxList();
    txList.sort(TxTimeComparator.getInstance());
    BlockData bd = new BlockData();
    bd.setHeight(bestBlock.getHeader().getHeight() + 1);
    bd.setPreHash(bestBlock.getHeader().getHash());
    BlockRoundData roundData = new BlockRoundData();
    roundData.setRoundIndex(consensusManager.getCurrentRound().getIndex());
    roundData.setConsensusMemberCount(consensusManager.getCurrentRound().getMemberCount());
    roundData.setPackingIndexOfRound(self.getIndexOfRound());
    roundData.setRoundStartTime(consensusManager.getCurrentRound().getStartTime());
    bd.setRoundData(roundData);
    List<Integer> outTxList = new ArrayList<>();
    List<NulsDigestData> outHashList = new ArrayList<>();
    List<NulsDigestData> hashList = new ArrayList<>();
    long totalSize = 0L;
    for (int i = 0; i < txList.size(); i++) {
        if ((self.getPackTime() - TimeService.currentTimeMillis()) <= 100) {
            break;
        }
        Transaction tx = txList.get(i);
        totalSize += tx.size();
        if (totalSize >= PocConsensusConstant.MAX_BLOCK_SIZE) {
            break;
        }
        outHashList.add(tx.getHash());
        ValidateResult result = tx.verify();
        if (result.isFailed()) {
            Log.error(result.getMessage());
            outTxList.add(i);
            continue;
        }
        try {
            ledgerService.approvalTx(tx);
        } catch (Exception e) {
            Log.error(e);
            outTxList.add(i);
            continue;
        }
        confirmingTxCacheManager.putTx(tx);
    }
    txCacheManager.removeTx(hashList);
    for (int i = outTxList.size() - 1; i >= 0; i--) {
        txList.remove(i);
    }
    txCacheManager.removeTx(outHashList);
    if (totalSize < PocConsensusConstant.MAX_BLOCK_SIZE) {
        addOrphanTx(txList, totalSize, self);
    }
    addConsensusTx(bestBlock, txList, self);
    bd.setTxList(txList);
    Log.info("txCount:" + txList.size());
    Block newBlock = ConsensusTool.createBlock(bd, consensusManager.getConsensusStatusInfo().getAccount());
    ValidateResult result = newBlock.verify();
    if (result.isFailed()) {
        Log.warn("packing block error:" + result.getMessage());
        for (Transaction tx : newBlock.getTxs()) {
            if (tx.getType() == TransactionConstant.TX_TYPE_COIN_BASE) {
                continue;
            }
            ledgerService.rollbackTx(tx);
        }
        return;
    }
    confirmingTxCacheManager.putTx(newBlock.getTxs().get(0));
    blockManager.addBlock(newBlock, false, null);
    BlockHeaderEvent event = new BlockHeaderEvent();
    event.setEventBody(newBlock.getHeader());
    eventBroadcaster.broadcastAndCache(event, false);
    PackedBlockNotice notice = new PackedBlockNotice();
    notice.setEventBody(newBlock.getHeader());
    eventBroadcaster.publishToLocal(notice);
}
Also used : ValidateResult(io.nuls.core.validate.ValidateResult) NulsException(io.nuls.core.exception.NulsException) IOException(java.io.IOException) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) BlockHeaderEvent(io.nuls.consensus.event.BlockHeaderEvent) CoinBaseTransaction(io.nuls.ledger.entity.tx.CoinBaseTransaction) Transaction(io.nuls.core.chain.entity.Transaction) RedPunishTransaction(io.nuls.consensus.entity.tx.RedPunishTransaction) YellowPunishTransaction(io.nuls.consensus.entity.tx.YellowPunishTransaction) BlockRoundData(io.nuls.consensus.entity.block.BlockRoundData) Block(io.nuls.core.chain.entity.Block) NulsDigestData(io.nuls.core.chain.entity.NulsDigestData) PackedBlockNotice(io.nuls.consensus.event.notice.PackedBlockNotice) BlockData(io.nuls.consensus.entity.block.BlockData)

Aggregations

Block (io.nuls.core.chain.entity.Block)31 BlockHeader (io.nuls.core.chain.entity.BlockHeader)11 NulsException (io.nuls.core.exception.NulsException)9 Transaction (io.nuls.core.chain.entity.Transaction)8 NulsRuntimeException (io.nuls.core.exception.NulsRuntimeException)6 IOException (java.io.IOException)5 ValidateResult (io.nuls.core.validate.ValidateResult)4 ArrayList (java.util.ArrayList)4 BestCorrectBlock (io.nuls.consensus.entity.block.BestCorrectBlock)3 BlockInfo (io.nuls.consensus.utils.BlockInfo)3 NulsDigestData (io.nuls.core.chain.entity.NulsDigestData)3 BlockHeaderPo (io.nuls.db.entity.BlockHeaderPo)3 BlockRoundData (io.nuls.consensus.entity.block.BlockRoundData)2 BlockHeaderEvent (io.nuls.consensus.event.BlockHeaderEvent)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 GetTxGroupParam (io.nuls.consensus.entity.GetTxGroupParam)1 NodeDownloadingStatus (io.nuls.consensus.entity.NodeDownloadingStatus)1