Search in sources :

Example 1 with UtxoInputPo

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;
    }
}
Also used : TxAccountRelationPo(io.nuls.db.entity.TxAccountRelationPo) UtxoOutputPo(io.nuls.db.entity.UtxoOutputPo) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) NulsException(io.nuls.core.exception.NulsException) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) UtxoInputPo(io.nuls.db.entity.UtxoInputPo) DbSession(io.nuls.db.transactional.annotation.DbSession)

Example 2 with UtxoInputPo

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);
}
Also used : TransactionLocalPo(io.nuls.db.entity.TransactionLocalPo) ArrayList(java.util.ArrayList) UtxoInputPo(io.nuls.db.entity.UtxoInputPo) TransactionPo(io.nuls.db.entity.TransactionPo) DbSession(io.nuls.db.transactional.annotation.DbSession)

Example 3 with UtxoInputPo

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;
}
Also used : UtxoInputPo(io.nuls.db.entity.UtxoInputPo)

Example 4 with UtxoInputPo

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

Aggregations

UtxoInputPo (io.nuls.db.entity.UtxoInputPo)4 UtxoOutputPo (io.nuls.db.entity.UtxoOutputPo)2 DbSession (io.nuls.db.transactional.annotation.DbSession)2 NulsException (io.nuls.core.exception.NulsException)1 NulsRuntimeException (io.nuls.core.exception.NulsRuntimeException)1 TransactionLocalPo (io.nuls.db.entity.TransactionLocalPo)1 TransactionPo (io.nuls.db.entity.TransactionPo)1 TxAccountRelationPo (io.nuls.db.entity.TxAccountRelationPo)1 UtxoData (io.nuls.ledger.entity.UtxoData)1 AbstractCoinTransaction (io.nuls.ledger.entity.tx.AbstractCoinTransaction)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1