Search in sources :

Example 1 with UtxoData

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

the class AmountValidator method validate.

@Override
public ValidateResult validate(AbstractCoinTransaction tx) {
    UtxoData data = (UtxoData) tx.getCoinData();
    if (tx.getType() == TransactionConstant.TX_TYPE_TRANSFER) {
        long inTotal = 0;
        for (int i = 0; i < data.getInputs().size(); i++) {
            inTotal += data.getInputs().get(i).getFrom().getValue();
        }
        long outTotal = 0;
        for (int i = 0; i < data.getOutputs().size(); i++) {
            {
                long amount = data.getOutputs().get(i).getValue();
                // todo:  validate overflow attack
                if (amount > Na.MAX_NA_VALUE) {
                    return ValidateResult.getFailedResult(ErrorCode.INVALID_AMOUNT);
                }
                outTotal += amount;
            }
        // todo:  fee must be calculated every yeah
        }
        long fee = 1000000;
        if (outTotal + fee > inTotal) {
            return ValidateResult.getFailedResult(ErrorCode.INVALID_AMOUNT);
        }
    } else {
    // coinbase
    // todo :validate the amount for every staker
    // todo :validata the coinbase is legal
    }
    return ValidateResult.getSuccessResult();
}
Also used : UtxoData(io.nuls.ledger.entity.UtxoData)

Example 2 with UtxoData

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

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

the class UtxoTransferTool method transferCoinData.

private static void transferCoinData(Transaction tx, List<UtxoInputPo> inputPoList, List<UtxoOutputPo> outputPoList) {
    if (tx instanceof AbstractCoinTransaction) {
        AbstractCoinTransaction coinTx = (AbstractCoinTransaction) tx;
        UtxoData utxoData = new UtxoData();
        Set<String> addressSet = new HashSet<>();
        for (UtxoInputPo inputPo : inputPoList) {
            utxoData.getInputs().add(toInput(inputPo));
            addressSet.add(inputPo.getFromOutPut().getAddress());
        }
        for (int i = 0; i < outputPoList.size(); i++) {
            UtxoOutputPo outputPo = outputPoList.get(i);
            utxoData.getOutputs().add(toOutput(outputPo));
        }
        coinTx.setCoinData(utxoData);
    }
}
Also used : AbstractCoinTransaction(io.nuls.ledger.entity.tx.AbstractCoinTransaction) UtxoData(io.nuls.ledger.entity.UtxoData) UtxoInputPo(io.nuls.db.entity.UtxoInputPo) UtxoOutputPo(io.nuls.db.entity.UtxoOutputPo) HashSet(java.util.HashSet)

Example 4 with UtxoData

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

UtxoData (io.nuls.ledger.entity.UtxoData)4 UtxoOutput (io.nuls.ledger.entity.UtxoOutput)2 NulsException (io.nuls.core.exception.NulsException)1 P2PKHScriptSig (io.nuls.core.script.P2PKHScriptSig)1 UtxoInputPo (io.nuls.db.entity.UtxoInputPo)1 UtxoOutputPo (io.nuls.db.entity.UtxoOutputPo)1 UtxoInput (io.nuls.ledger.entity.UtxoInput)1 AbstractCoinTransaction (io.nuls.ledger.entity.tx.AbstractCoinTransaction)1 HashSet (java.util.HashSet)1