use of io.nuls.db.entity.UtxoInputPo in project nuls by nuls-io.
the class UtxoCoinDataProvider method save.
/**
* 1. change spending output status (cache and database)
* 2. save new input
* 3. save new unSpend output (cache and database)
* 4. finally, calc balance
*/
@Override
@DbSession
public void save(CoinData coinData, Transaction tx) throws NulsException {
UtxoData utxoData = (UtxoData) coinData;
List<UtxoInputPo> inputPoList = new ArrayList<>();
List<UtxoOutput> spends = new ArrayList<>();
List<UtxoOutputPo> spendPoList = new ArrayList<>();
List<TxAccountRelationPo> txRelations = new ArrayList<>();
Set<String> addressSet = new HashSet<>();
try {
processDataInput(utxoData, inputPoList, spends, spendPoList, addressSet);
List<UtxoOutputPo> outputPoList = new ArrayList<>();
for (int i = 0; i < utxoData.getOutputs().size(); i++) {
UtxoOutput output = utxoData.getOutputs().get(i);
output = ledgerCacheService.getUtxo(output.getKey());
if (output == null) {
throw new NulsRuntimeException(ErrorCode.DATA_NOT_FOUND);
}
if (output.isConfirm() || OutPutStatusEnum.UTXO_SPENT == output.getStatus()) {
Log.error("-----------------------------------save() output status is" + output.getStatus().name());
throw new NulsRuntimeException(ErrorCode.DATA_ERROR, "use a not legal utxo");
}
if (OutPutStatusEnum.UTXO_UNCONFIRM_CONSENSUS_LOCK == output.getStatus()) {
output.setStatus(OutPutStatusEnum.UTXO_CONFIRM_CONSENSUS_LOCK);
} else if (OutPutStatusEnum.UTXO_UNCONFIRM_TIME_LOCK == output.getStatus()) {
output.setStatus(OutPutStatusEnum.UTXO_CONFIRM_TIME_LOCK);
} else if (OutPutStatusEnum.UTXO_UNCONFIRM_UNSPEND == output.getStatus()) {
output.setStatus(OutPutStatusEnum.UTXO_CONFIRM_UNSPEND);
} else if (OutPutStatusEnum.UTXO_UNCONFIRM_SPEND == output.getStatus()) {
output.setStatus(OutPutStatusEnum.UTXO_CONFIRM_SPEND);
}
UtxoOutputPo outputPo = UtxoTransferTool.toOutputPojo(output);
outputPoList.add(outputPo);
addressSet.add(Address.fromHashs(output.getAddress()).getBase58());
}
for (String address : addressSet) {
TxAccountRelationPo relationPo = new TxAccountRelationPo(tx.getHash().getDigestHex(), address);
txRelations.add(relationPo);
}
outputDataService.updateStatus(spendPoList);
inputDataService.save(inputPoList);
outputDataService.save(outputPoList);
relationDataService.save(txRelations);
afterSaveDatabase(spends, utxoData, tx);
for (String address : addressSet) {
UtxoTransactionTool.getInstance().calcBalance(address, true);
}
} catch (Exception e) {
// }
throw e;
}
}
use of io.nuls.db.entity.UtxoInputPo in project nuls by nuls-io.
the class UtxoLedgerServiceImpl method saveTxInLocal.
@Override
@DbSession
public void saveTxInLocal(String address) {
List<TransactionPo> poList = txDao.getTxs(null, address, 0, 0, 0);
if (poList.isEmpty()) {
return;
}
List<TransactionLocalPo> localPoList = new ArrayList<>();
for (TransactionPo po : poList) {
TransactionLocalPo localPo = txDao.getLocaltx(po.getHash());
if (localPo != null) {
continue;
}
localPo = new TransactionLocalPo(po);
for (UtxoInputPo inputPo : po.getInputs()) {
if (inputPo.getFromOutPut().getAddress().equals(address)) {
localPo.setTransferType(Transaction.TRANSFER_SEND);
break;
}
}
localPoList.add(localPo);
}
txDao.saveLocalList(localPoList);
}
use of io.nuls.db.entity.UtxoInputPo in project nuls by nuls-io.
the class UtxoTransferTool method toInputPojo.
public static UtxoInputPo toInputPojo(UtxoInput input) {
UtxoInputPo po = new UtxoInputPo();
po.setTxHash(input.getTxHash().getDigestHex());
po.setInIndex(input.getIndex());
po.setFromHash(input.getFromHash().getDigestHex());
po.setFromIndex(input.getFromIndex());
return po;
}
use of io.nuls.db.entity.UtxoInputPo 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);
}
}
Aggregations