Search in sources :

Example 6 with UtxoOutput

use of io.nuls.ledger.entity.UtxoOutput 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 7 with UtxoOutput

use of io.nuls.ledger.entity.UtxoOutput in project nuls by nuls-io.

the class UtxoTxOutputsValidator method validate.

@Override
public ValidateResult validate(AbstractCoinTransaction tx) {
    UtxoData utxoData = (UtxoData) tx.getCoinData();
    List<UtxoOutput> outputs = utxoData.getOutputs();
    if (null != outputs && outputs.size() > MAX_OUTPUT_COUNT) {
        return ValidateResult.getFailedResult(ERROR_MESSAGE);
    }
    return ValidateResult.getSuccessResult();
}
Also used : UtxoData(io.nuls.ledger.entity.UtxoData) UtxoOutput(io.nuls.ledger.entity.UtxoOutput)

Example 8 with UtxoOutput

use of io.nuls.ledger.entity.UtxoOutput in project nuls by nuls-io.

the class AccountResource method getUtxo.

@GET
@Path("/utxo/")
@Produces(MediaType.APPLICATION_JSON)
public RpcResult getUtxo(@QueryParam("address") String address, @QueryParam("amount") long amount) {
    if (!Address.validAddress(address) || amount <= 0 || amount > Na.MAX_NA_VALUE) {
        return RpcResult.getFailed(ErrorCode.PARAMETER_ERROR);
    }
    UtxoBalance balance = (UtxoBalance) ledgerService.getBalance(address);
    if (balance == null || balance.getUnSpends() == null) {
        return RpcResult.getFailed("balance not enough");
    }
    amount += this.ledgerService.getTxFee(Integer.MAX_VALUE).getValue();
    long usable = 0;
    boolean enough = false;
    List<OutputDto> dtoList = new ArrayList<>();
    for (int i = 0; i < balance.getUnSpends().size(); i++) {
        UtxoOutput output = balance.getUnSpends().get(i);
        if (output.isUsable()) {
            usable += output.getValue();
            dtoList.add(new OutputDto(output));
        }
        if (usable > amount) {
            enough = true;
            break;
        }
    }
    if (!enough) {
        return RpcResult.getFailed("balance not enough");
    }
    return RpcResult.getSuccess().setData(dtoList);
}
Also used : UtxoBalance(io.nuls.ledger.entity.UtxoBalance) ArrayList(java.util.ArrayList) UtxoOutput(io.nuls.ledger.entity.UtxoOutput)

Example 9 with UtxoOutput

use of io.nuls.ledger.entity.UtxoOutput 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)

Example 10 with UtxoOutput

use of io.nuls.ledger.entity.UtxoOutput in project nuls by nuls-io.

the class UtxoTxInputsValidator method validate.

@Override
public ValidateResult validate(AbstractCoinTransaction tx) {
    UtxoData data = (UtxoData) tx.getCoinData();
    for (int i = 0; i < data.getInputs().size(); i++) {
        UtxoInput input = data.getInputs().get(i);
        UtxoOutput output = input.getFrom();
        if (output == null && tx.getStatus() == TxStatusEnum.CACHED) {
            return ValidateResult.getFailedResult(ErrorCode.ORPHAN_TX);
        }
        if (tx.getStatus() == TxStatusEnum.CACHED) {
            if (!output.isUsable()) {
                return ValidateResult.getFailedResult(ErrorCode.UTXO_STATUS_CHANGE);
            }
        } else if (tx.getStatus() == TxStatusEnum.AGREED) {
            if (!output.isSpend()) {
                return ValidateResult.getFailedResult(ErrorCode.UTXO_STATUS_CHANGE);
            }
        }
        byte[] owner = output.getOwner();
        P2PKHScriptSig p2PKHScriptSig = null;
        try {
            p2PKHScriptSig = P2PKHScriptSig.createFromBytes(tx.getScriptSig());
        } catch (NulsException e) {
            return ValidateResult.getFailedResult(ErrorCode.DATA_ERROR);
        }
        byte[] user = p2PKHScriptSig.getSignerHash160();
        if (!Arrays.equals(owner, user)) {
            return ValidateResult.getFailedResult(ErrorCode.INVALID_OUTPUT);
        }
        return ValidateResult.getSuccessResult();
    }
    return ValidateResult.getSuccessResult();
}
Also used : P2PKHScriptSig(io.nuls.core.script.P2PKHScriptSig) NulsException(io.nuls.core.exception.NulsException) UtxoData(io.nuls.ledger.entity.UtxoData) UtxoInput(io.nuls.ledger.entity.UtxoInput) UtxoOutput(io.nuls.ledger.entity.UtxoOutput)

Aggregations

UtxoOutput (io.nuls.ledger.entity.UtxoOutput)12 UtxoBalance (io.nuls.ledger.entity.UtxoBalance)4 NulsDigestData (io.nuls.core.chain.entity.NulsDigestData)2 UtxoData (io.nuls.ledger.entity.UtxoData)2 UtxoInput (io.nuls.ledger.entity.UtxoInput)2 ArrayList (java.util.ArrayList)2 Na (io.nuls.core.chain.entity.Na)1 NulsException (io.nuls.core.exception.NulsException)1 P2PKHScript (io.nuls.core.script.P2PKHScript)1 P2PKHScriptSig (io.nuls.core.script.P2PKHScriptSig)1 UtxoOutputPo (io.nuls.db.entity.UtxoOutputPo)1 DbSession (io.nuls.db.transactional.annotation.DbSession)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1