Search in sources :

Example 11 with NulsDigestData

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

the class BlockHashResponse method size.

@Override
public int size() {
    int size = 0;
    size += Utils.sizeOfInt48();
    size += Utils.sizeOfInt(heightList.size());
    for (Long height : heightList) {
        size += Utils.sizeOfLong(height);
    }
    size += Utils.sizeOfInt(hashList.size());
    for (NulsDigestData hash : hashList) {
        size += Utils.sizeOfNulsData(hash);
    }
    return size;
}
Also used : NulsDigestData(io.nuls.core.chain.entity.NulsDigestData)

Example 12 with NulsDigestData

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

the class UtxoTransferTool method toInput.

public static UtxoInput toInput(UtxoInputPo po) {
    UtxoInput input = new UtxoInput();
    input.setTxHash(new NulsDigestData(Hex.decode(po.getTxHash())));
    input.setIndex(po.getInIndex());
    input.setFromHash(new NulsDigestData(Hex.decode(po.getFromHash())));
    input.setFromIndex(po.getFromIndex());
    UtxoOutput output = new UtxoOutput();
    output.setTxHash(new NulsDigestData(Hex.decode(po.getFromOutPut().getTxHash())));
    output.setIndex(po.getFromOutPut().getOutIndex());
    output.setLockTime(po.getFromOutPut().getLockTime());
    output.setValue(po.getFromOutPut().getValue());
    output.setAddress(po.getFromOutPut().getAddress());
    input.setFrom(output);
    return input;
}
Also used : NulsDigestData(io.nuls.core.chain.entity.NulsDigestData) UtxoInput(io.nuls.ledger.entity.UtxoInput) UtxoOutput(io.nuls.ledger.entity.UtxoOutput)

Example 13 with NulsDigestData

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

the class TxSignValidator method validate.

@Override
public ValidateResult validate(Transaction tx) {
    byte[] scriptSig = tx.getScriptSig();
    tx.setScriptSig(null);
    NulsDigestData nulsDigestData;
    try {
        nulsDigestData = NulsDigestData.calcDigestData(tx.serialize());
    } catch (Exception e) {
        return ValidateResult.getFailedResult(ErrorCode.DATA_ERROR);
    } finally {
        tx.setScriptSig(scriptSig);
    }
    if (!Arrays.equals(nulsDigestData.getDigestBytes(), tx.getHash().getDigestBytes())) {
        return ValidateResult.getFailedResult(ErrorCode.DATA_ERROR);
    }
    P2PKHScriptSig p2PKHScriptSig = null;
    try {
        p2PKHScriptSig = new NulsByteBuffer(scriptSig).readNulsData(new P2PKHScriptSig());
    } catch (NulsException e) {
        return ValidateResult.getFailedResult(ErrorCode.SIGNATURE_ERROR);
    }
    return p2PKHScriptSig.verifySign(tx.getHash());
}
Also used : P2PKHScriptSig(io.nuls.core.script.P2PKHScriptSig) NulsException(io.nuls.core.exception.NulsException) NulsDigestData(io.nuls.core.chain.entity.NulsDigestData) NulsException(io.nuls.core.exception.NulsException) NulsByteBuffer(io.nuls.core.utils.io.NulsByteBuffer)

Example 14 with NulsDigestData

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

Example 15 with NulsDigestData

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

the class BlockBatchDownloadUtils method checkHash.

private Result checkHash() throws InterruptedException {
    for (long i = currentRound.getStart(); i <= currentRound.getEnd(); i++) {
        Block block = blockMap.get(i);
        if (null == block) {
            return Result.getFailed("data error");
        }
        if ((i - currentRound.getStart()) % DOWNLOAD_BLOCKS_PER_TIME == 0) {
            NulsDigestData mustHash = blocksHash.getHash(block.getHeader().getHeight());
            NulsDigestData hash = block.getHeader().getHash();
            if (null == hash || null == mustHash || !mustHash.getDigestHex().equals(hash.getDigestHex())) {
                failedExecute(block.getHeader().getHeight());
                return Result.getFailed("hash wrong!");
            }
        }
        String preHash = block.getHeader().getPreHash().getDigestHex();
        Block preBlock = blockMap.get(block.getHeader().getHeight() - 1);
        if (preBlock == null) {
            preBlock = blockService.getBlock(preHash);
        }
        if (null == preBlock || preBlock.getHeader().getHeight() != (block.getHeader().getHeight() - 1)) {
            failedExecute(block.getHeader().getHeight());
            return Result.getFailed("prehash wrong!");
        }
    }
    return Result.getSuccess();
}
Also used : Block(io.nuls.core.chain.entity.Block) NulsDigestData(io.nuls.core.chain.entity.NulsDigestData)

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