Search in sources :

Example 1 with OutputDto

use of io.nuls.ledger.rpc.model.OutputDto in project nuls by nuls-io.

the class TransactionResource method calTransactionValue.

/**
 * 计算交易实际发生的金额
 * Calculate the actual amount of the transaction.
 *
 * @param txDto
 */
private void calTransactionValue(TransactionDto txDto) {
    if (txDto == null) {
        return;
    }
    List<InputDto> inputDtoList = txDto.getInputs();
    Set<String> inputAdressSet = new HashSet<>(inputDtoList.size());
    for (InputDto inputDto : inputDtoList) {
        inputAdressSet.add(inputDto.getAddress());
    }
    Na value = Na.ZERO;
    List<OutputDto> outputDtoList = txDto.getOutputs();
    for (OutputDto outputDto : outputDtoList) {
        if (inputAdressSet.contains(outputDto.getAddress())) {
            continue;
        }
        value = value.add(Na.valueOf(outputDto.getValue()));
    }
    txDto.setValue(value.getValue());
}
Also used : OutputDto(io.nuls.ledger.rpc.model.OutputDto) InputDto(io.nuls.ledger.rpc.model.InputDto)

Example 2 with OutputDto

use of io.nuls.ledger.rpc.model.OutputDto in project nuls by nuls-io.

the class TransactionResource method getTxByHash.

@GET
@Path("/hash/{hash}")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "根据hash查询交易")
@ApiResponses(value = { @ApiResponse(code = 200, message = "success", response = TransactionDto.class) })
public RpcClientResult getTxByHash(@ApiParam(name = "hash", value = "交易hash", required = true) @PathParam("hash") String hash) {
    if (StringUtils.isBlank(hash)) {
        return Result.getFailed(LedgerErrorCode.NULL_PARAMETER).toRpcClientResult();
    }
    if (!NulsDigestData.validHash(hash)) {
        return Result.getFailed(LedgerErrorCode.PARAMETER_ERROR).toRpcClientResult();
    }
    Result result = null;
    try {
        Transaction tx = ledgerService.getTx(NulsDigestData.fromDigestHex(hash));
        if (tx == null) {
            result = Result.getFailed(TransactionErrorCode.TX_NOT_EXIST);
        } else {
            tx.setStatus(TxStatusEnum.CONFIRMED);
            TransactionDto txDto = null;
            CoinData coinData = tx.getCoinData();
            String fromAddress = null;
            if (tx.getTransactionSignature() != null) {
                TransactionSignature signature = new TransactionSignature();
                signature.parse(new NulsByteBuffer(tx.getTransactionSignature()));
                if (signature.getP2PHKSignatures() != null && signature.getP2PHKSignatures().size() == 1) {
                    byte[] addressBytes = AddressTool.getAddress(signature.getP2PHKSignatures().get(0).getPublicKey());
                    fromAddress = AddressTool.getStringAddressByBytes(addressBytes);
                }
            }
            if (coinData != null) {
                // 组装from数据
                List<Coin> froms = coinData.getFrom();
                if (froms != null && froms.size() > 0) {
                    byte[] fromHash, owner;
                    int fromIndex;
                    NulsDigestData fromHashObj;
                    Transaction fromTx;
                    Coin fromUtxo;
                    for (Coin from : froms) {
                        if (fromAddress != null) {
                            from.setFromAddress(fromAddress);
                        } else {
                            owner = from.getOwner();
                            // owner拆分出txHash和index
                            fromHash = LedgerUtil.getTxHashBytes(owner);
                            fromIndex = LedgerUtil.getIndex(owner);
                            // 查询from UTXO
                            fromHashObj = new NulsDigestData();
                            fromHashObj.parse(fromHash, 0);
                            fromTx = ledgerService.getTx(fromHashObj);
                            fromUtxo = fromTx.getCoinData().getTo().get(fromIndex);
                            from.setFrom(fromUtxo);
                        }
                    }
                }
                txDto = new TransactionDto(tx);
                List<OutputDto> outputDtoList = new ArrayList<>();
                // 组装to数据
                List<Coin> tos = coinData.getTo();
                if (tos != null && tos.size() > 0) {
                    byte[] txHashBytes = tx.getHash().serialize();
                    String txHash = hash;
                    OutputDto outputDto = null;
                    Coin to, temp;
                    long bestHeight = NulsContext.getInstance().getBestHeight();
                    long currentTime = TimeService.currentTimeMillis();
                    long lockTime;
                    for (int i = 0, length = tos.size(); i < length; i++) {
                        to = tos.get(i);
                        outputDto = new OutputDto(to);
                        outputDto.setTxHash(txHash);
                        outputDto.setIndex(i);
                        temp = utxoLedgerUtxoStorageService.getUtxo(Arrays.concatenate(txHashBytes, new VarInt(i).encode()));
                        if (temp == null) {
                            // 已花费
                            outputDto.setStatus(3);
                        } else {
                            lockTime = temp.getLockTime();
                            if (lockTime < 0) {
                                // 共识锁定
                                outputDto.setStatus(2);
                            } else if (lockTime == 0) {
                                // 正常未花费
                                outputDto.setStatus(0);
                            } else if (lockTime > NulsConstant.BlOCKHEIGHT_TIME_DIVIDE) {
                                // 判定是否时间高度锁定
                                if (lockTime > currentTime) {
                                    // 时间高度锁定
                                    outputDto.setStatus(1);
                                } else {
                                    // 正常未花费
                                    outputDto.setStatus(0);
                                }
                            } else {
                                // 判定是否区块高度锁定
                                if (lockTime > bestHeight) {
                                    // 区块高度锁定
                                    outputDto.setStatus(1);
                                } else {
                                    // 正常未花费
                                    outputDto.setStatus(0);
                                }
                            }
                        }
                        outputDtoList.add(outputDto);
                    }
                }
                txDto.setOutputs(outputDtoList);
                // 计算交易实际发生的金额
                calTransactionValue(txDto);
            }
            result = Result.getSuccess();
            result.setData(txDto);
        }
    } catch (NulsRuntimeException e) {
        Log.error(e);
        result = Result.getFailed(e.getErrorCode());
    } catch (Exception e) {
        Log.error(e);
        result = Result.getFailed(LedgerErrorCode.SYS_UNKOWN_EXCEPTION);
    }
    return result.toRpcClientResult();
}
Also used : OutputDto(io.nuls.ledger.rpc.model.OutputDto) TransactionDto(io.nuls.ledger.rpc.model.TransactionDto) VarInt(io.nuls.kernel.utils.VarInt) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) TransactionSignature(io.nuls.kernel.script.TransactionSignature) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) IOException(java.io.IOException) NulsException(io.nuls.kernel.exception.NulsException) NulsByteBuffer(io.nuls.kernel.utils.NulsByteBuffer)

Aggregations

OutputDto (io.nuls.ledger.rpc.model.OutputDto)2 NulsException (io.nuls.kernel.exception.NulsException)1 NulsRuntimeException (io.nuls.kernel.exception.NulsRuntimeException)1 TransactionSignature (io.nuls.kernel.script.TransactionSignature)1 NulsByteBuffer (io.nuls.kernel.utils.NulsByteBuffer)1 VarInt (io.nuls.kernel.utils.VarInt)1 InputDto (io.nuls.ledger.rpc.model.InputDto)1 TransactionDto (io.nuls.ledger.rpc.model.TransactionDto)1 IOException (java.io.IOException)1