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;
}
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);
}
}
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;
}
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;
}
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;
}
Aggregations