Search in sources :

Example 6 with NulsDigestData

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

the class SmallBlockHandler method onEvent.

@Override
public void onEvent(SmallBlockEvent event, String fromId) {
    ValidateResult result = event.getEventBody().verify();
    if (result.isFailed()) {
        return;
    }
    temporaryCacheManager.cacheSmallBlock(event.getEventBody());
    downloadDataUtils.removeSmallBlock(event.getEventBody().getBlockHash().getDigestHex());
    GetTxGroupRequest request = new GetTxGroupRequest();
    GetTxGroupParam param = new GetTxGroupParam();
    param.setBlockHash(event.getEventBody().getBlockHash());
    for (NulsDigestData hash : event.getEventBody().getTxHashList()) {
        if (!receivedTxCacheManager.txExist(hash)) {
            param.addHash(hash);
        }
    }
    request.setEventBody(param);
    this.eventBroadcaster.sendToNode(request, fromId);
}
Also used : GetTxGroupRequest(io.nuls.consensus.event.GetTxGroupRequest) ValidateResult(io.nuls.core.validate.ValidateResult) GetTxGroupParam(io.nuls.consensus.entity.GetTxGroupParam) NulsDigestData(io.nuls.core.chain.entity.NulsDigestData)

Example 7 with NulsDigestData

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

the class BlockManager method rollbackTxList.

private void rollbackTxList(List<Transaction> txList, int start, int end) {
    List<NulsDigestData> txHashList = new ArrayList<>();
    for (int i = start; i <= end && i < txList.size(); i++) {
        Transaction tx = txList.get(i);
        if (tx.getStatus() == TxStatusEnum.AGREED) {
            try {
                ledgerService.rollbackTx(tx);
            } catch (NulsException e) {
                Log.error(e);
            }
            txHashList.add(tx.getHash());
        }
    }
    confirmingTxCacheManager.removeTxList(txHashList);
}
Also used : Transaction(io.nuls.core.chain.entity.Transaction) NulsException(io.nuls.core.exception.NulsException) ArrayList(java.util.ArrayList) NulsDigestData(io.nuls.core.chain.entity.NulsDigestData)

Example 8 with NulsDigestData

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

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

the class GetTxGroupHandler method getTxList.

private List<Transaction> getTxList(Block block, List<NulsDigestData> txHashList) {
    List<Transaction> txList = new ArrayList<>();
    Map<String, Integer> allTxMap = new HashMap<>();
    for (int i = 0; i < block.getHeader().getTxCount(); i++) {
        Transaction tx = block.getTxs().get(i);
        allTxMap.put(tx.getHash().getDigestHex(), i);
    }
    for (NulsDigestData hash : txHashList) {
        txList.add(block.getTxs().get(allTxMap.get(hash.getDigestHex())));
    }
    if (txList.size() != txHashList.size()) {
        throw new NulsRuntimeException(ErrorCode.DATA_ERROR);
    }
    return txList;
}
Also used : Transaction(io.nuls.core.chain.entity.Transaction) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) NulsDigestData(io.nuls.core.chain.entity.NulsDigestData) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException)

Example 10 with NulsDigestData

use of io.nuls.core.chain.entity.NulsDigestData 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)

Aggregations

NulsDigestData (io.nuls.core.chain.entity.NulsDigestData)22 Transaction (io.nuls.core.chain.entity.Transaction)6 NulsException (io.nuls.core.exception.NulsException)5 NulsRuntimeException (io.nuls.core.exception.NulsRuntimeException)5 ValidateResult (io.nuls.core.validate.ValidateResult)4 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 BlockHashResponse (io.nuls.consensus.entity.BlockHashResponse)3 Block (io.nuls.core.chain.entity.Block)3 NulsByteBuffer (io.nuls.core.utils.io.NulsByteBuffer)3 LockNulsTransaction (io.nuls.ledger.entity.tx.LockNulsTransaction)3 RedPunishTransaction (io.nuls.consensus.entity.tx.RedPunishTransaction)2 YellowPunishTransaction (io.nuls.consensus.entity.tx.YellowPunishTransaction)2 BlockHeader (io.nuls.core.chain.entity.BlockHeader)2 P2PKHScript (io.nuls.core.script.P2PKHScript)2 P2PKHScriptSig (io.nuls.core.script.P2PKHScriptSig)2 UtxoOutput (io.nuls.ledger.entity.UtxoOutput)2 AbstractCoinTransaction (io.nuls.ledger.entity.tx.AbstractCoinTransaction)2 CoinBaseTransaction (io.nuls.ledger.entity.tx.CoinBaseTransaction)2 HashMap (java.util.HashMap)2