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