Search in sources :

Example 16 with NulsDigestData

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

the class DistributedBlockInfoRequestUtils method getBlockInfo.

private BlockInfo getBlockInfo() {
    while (true) {
        if (null != bestBlockInfo && bestBlockInfo.isFinished()) {
            break;
        }
        try {
            Thread.sleep(10L);
        } catch (InterruptedException e) {
            Log.error(e);
        }
        long timeout = 10000L;
        if ((TimeService.currentTimeMillis() - startTime) > (timeout - 1000L) && hashesMap.size() >= ((nodeIdList.size() + 1) / 2) && start == end && start <= 0) {
            long localHeight = NulsContext.getInstance().getBestBlock().getHeader().getHeight();
            long minHeight = Long.MAX_VALUE;
            NulsDigestData minHash = null;
            List<String> nodeIds = new ArrayList<>();
            try {
                for (String nodeId : hashesMap.keySet()) {
                    BlockHashResponse response = hashesMap.get(nodeId);
                    long height = response.getHeightList().get(0);
                    NulsDigestData hash = response.getHashList().get(0);
                    if (height >= localHeight) {
                        if (height <= minHeight) {
                            minHeight = height;
                            minHash = hash;
                        }
                        nodeIds.add(nodeId);
                    }
                }
            } catch (Exception e) {
                break;
            }
            BlockInfo result = new BlockInfo();
            result.putHash(minHeight, minHash);
            result.setBestHash(minHash);
            result.setBestHeight(minHeight);
            result.setNodeIdList(nodeIds);
            result.setFinished(true);
            if (result.getBestHeight() < Long.MAX_VALUE) {
                bestBlockInfo = result;
            } else {
                throw new NulsRuntimeException(ErrorCode.TIME_OUT);
            }
        } else if ((TimeService.currentTimeMillis() - startTime) > timeout && !(hashesMap.size() >= ((nodeIdList.size() + 1) / 2) && start == end && start <= 0)) {
            throw new NulsRuntimeException(ErrorCode.TIME_OUT);
        }
    }
    BlockInfo info = bestBlockInfo;
    bestBlockInfo = null;
    requesting = false;
    return info;
}
Also used : BlockHashResponse(io.nuls.consensus.entity.BlockHashResponse) ArrayList(java.util.ArrayList) NulsDigestData(io.nuls.core.chain.entity.NulsDigestData) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException)

Example 17 with NulsDigestData

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

the class BlockHashResponse method serializeToStream.

@Override
protected void serializeToStream(NulsOutputStreamBuffer stream) throws IOException {
    stream.writeInt48(time);
    stream.writeVarInt(heightList.size());
    for (Long height : heightList) {
        stream.writeVarInt(height);
    }
    stream.writeVarInt(hashList.size());
    for (NulsDigestData hash : hashList) {
        stream.writeNulsData(hash);
    }
}
Also used : NulsDigestData(io.nuls.core.chain.entity.NulsDigestData)

Example 18 with NulsDigestData

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

the class UtxoTransferTool method toTransaction.

public static Transaction toTransaction(TransactionPo po) throws Exception {
    Transaction tx = TransactionManager.getInstanceByType(po.getType());
    tx.setHash(new NulsDigestData(Hex.decode(po.getHash())));
    tx.setTime(po.getCreateTime());
    tx.setBlockHeight(po.getBlockHeight());
    tx.setFee(Na.valueOf(po.getFee()));
    tx.setIndex(po.getTxIndex());
    tx.setSize(po.getSize());
    tx.setScriptSig(po.getScriptSig());
    if (StringUtils.isNotBlank(po.getRemark())) {
        tx.setRemark(po.getRemark().getBytes(NulsContext.DEFAULT_ENCODING));
    }
    if (null != po.getTxData()) {
        tx.setTxData(tx.parseTxData(new NulsByteBuffer(po.getTxData())));
    }
    transferCoinData(tx, po.getInputs(), po.getOutputs());
    tx.setStatus(TxStatusEnum.CONFIRMED);
    return tx;
}
Also used : Transaction(io.nuls.core.chain.entity.Transaction) AbstractCoinTransaction(io.nuls.ledger.entity.tx.AbstractCoinTransaction) LockNulsTransaction(io.nuls.ledger.entity.tx.LockNulsTransaction) NulsDigestData(io.nuls.core.chain.entity.NulsDigestData) NulsByteBuffer(io.nuls.core.utils.io.NulsByteBuffer)

Example 19 with NulsDigestData

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

the class UtxoTransferTool method toTransaction.

public static Transaction toTransaction(TransactionLocalPo po) throws Exception {
    Transaction tx = TransactionManager.getInstanceByType(po.getType());
    tx.setHash(new NulsDigestData(Hex.decode(po.getHash())));
    tx.setTime(po.getCreateTime());
    tx.setBlockHeight(po.getBlockHeight());
    tx.setFee(Na.valueOf(po.getFee()));
    tx.setIndex(po.getTxIndex());
    tx.setTransferType(po.getTransferType());
    tx.setSize(po.getSize());
    tx.setScriptSig(po.getScriptSig());
    if (StringUtils.isNotBlank(po.getRemark())) {
        tx.setRemark(po.getRemark().getBytes(NulsContext.DEFAULT_ENCODING));
    }
    if (null != po.getTxData()) {
        tx.parseTxData(new NulsByteBuffer(po.getTxData()));
    }
    tx.setStatus(TxStatusEnum.CONFIRMED);
    transferCoinData(tx, po.getInputs(), po.getOutputs());
    return tx;
}
Also used : Transaction(io.nuls.core.chain.entity.Transaction) AbstractCoinTransaction(io.nuls.ledger.entity.tx.AbstractCoinTransaction) LockNulsTransaction(io.nuls.ledger.entity.tx.LockNulsTransaction) NulsDigestData(io.nuls.core.chain.entity.NulsDigestData) NulsByteBuffer(io.nuls.core.utils.io.NulsByteBuffer)

Example 20 with NulsDigestData

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

the class UtxoTransferTool method toOutput.

public static UtxoOutput toOutput(UtxoOutputPo po) {
    UtxoOutput output = new UtxoOutput();
    output.setTxHash(new NulsDigestData(Hex.decode(po.getTxHash())));
    output.setIndex(po.getOutIndex());
    output.setLockTime(po.getLockTime());
    output.setValue(po.getValue());
    output.setAddress(po.getAddress());
    try {
        output.setP2PKHScript(new P2PKHScript(po.getScript()));
    } catch (Exception e) {
        // todo
        Log.error(e);
    }
    long currentTime = TimeService.currentTimeMillis();
    long genesisTime = NulsContext.getInstance().getGenesisBlock().getHeader().getTime();
    long bestHeight = NulsContext.getInstance().getNetBestBlockHeight();
    if (po.getStatus() == UtxoOutputPo.USABLE) {
        if (po.getLockTime() > 0) {
            if (po.getLockTime() >= genesisTime && po.getLockTime() > currentTime) {
                output.setStatus(OutPutStatusEnum.UTXO_CONFIRM_TIME_LOCK);
            } else if (po.getLockTime() < genesisTime && po.getLockTime() > bestHeight) {
                output.setStatus(OutPutStatusEnum.UTXO_CONFIRM_TIME_LOCK);
            } else {
                output.setStatus(OutPutStatusEnum.UTXO_CONFIRM_UNSPEND);
            }
        } else {
            output.setStatus(OutPutStatusEnum.UTXO_CONFIRM_UNSPEND);
        }
    } else if (po.getStatus() == UtxoOutputPo.LOCKED) {
        output.setStatus(OutPutStatusEnum.UTXO_CONFIRM_CONSENSUS_LOCK);
    } else if (po.getStatus() == UtxoOutputPo.SPENT) {
        output.setStatus(OutPutStatusEnum.UTXO_SPENT);
    }
    if (po.getCreateTime() != null) {
        output.setCreateTime(po.getCreateTime());
    }
    if (po.getTxType() != null) {
        output.setTxType(po.getTxType());
    }
    return output;
}
Also used : P2PKHScript(io.nuls.core.script.P2PKHScript) NulsDigestData(io.nuls.core.chain.entity.NulsDigestData) UtxoOutput(io.nuls.ledger.entity.UtxoOutput) 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