Search in sources :

Example 1 with UtxoOutputPo

use of io.nuls.db.entity.UtxoOutputPo in project nuls by nuls-io.

the class UtxoInput method parse.

@Override
public void parse(NulsByteBuffer byteBuffer) throws NulsException {
    index = (int) byteBuffer.readVarInt();
    fromHash = byteBuffer.readNulsData(new NulsDigestData());
    fromIndex = (int) byteBuffer.readVarInt();
    LedgerCacheService ledgerCacheService = LedgerCacheService.getInstance();
    UtxoOutput output = ledgerCacheService.getUtxo(this.getKey());
    if (output == null) {
        UtxoOutputDataService utxoOutputDataService = NulsContext.getServiceBean(UtxoOutputDataService.class);
        Map<String, Object> map = new HashMap<>();
        map.put("txHash", this.fromHash.getDigestHex());
        map.put("outIndex", this.fromIndex);
        UtxoOutputPo outputPo = utxoOutputDataService.get(map);
        if (outputPo != null) {
            output = UtxoTransferTool.toOutput(outputPo);
        }
    }
    from = output;
}
Also used : LedgerCacheService(io.nuls.ledger.service.impl.LedgerCacheService) HashMap(java.util.HashMap) UtxoOutputDataService(io.nuls.db.dao.UtxoOutputDataService) NulsDigestData(io.nuls.core.chain.entity.NulsDigestData) UtxoOutputPo(io.nuls.db.entity.UtxoOutputPo)

Example 2 with UtxoOutputPo

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

use of io.nuls.db.entity.UtxoOutputPo in project nuls by nuls-io.

the class UtxoCoinManager method cacheAllUnSpendUtxo.

public void cacheAllUnSpendUtxo() {
    List<UtxoOutputPo> utxoOutputPos = outputDataService.getAllUnSpend();
    Set<String> addressSet = new HashSet<>();
    for (int i = 0; i < utxoOutputPos.size(); i++) {
        UtxoOutputPo po = utxoOutputPos.get(i);
        UtxoOutput output = UtxoTransferTool.toOutput(po);
        ledgerCacheService.putUtxo(output.getKey(), output);
        addressSet.add(po.getAddress());
    }
    for (String str : addressSet) {
        UtxoTransactionTool.getInstance().calcBalance(str, false);
    }
}
Also used : UtxoOutputPo(io.nuls.db.entity.UtxoOutputPo) UtxoOutput(io.nuls.ledger.entity.UtxoOutput) HashSet(java.util.HashSet)

Example 4 with UtxoOutputPo

use of io.nuls.db.entity.UtxoOutputPo in project nuls by nuls-io.

the class UtxoTransferTool method toOutputPojo.

public static UtxoOutputPo toOutputPojo(UtxoOutput output) {
    UtxoOutputPo po = new UtxoOutputPo();
    po.setTxHash(output.getTxHash().getDigestHex());
    po.setOutIndex(output.getIndex());
    po.setValue(output.getValue());
    po.setLockTime(output.getLockTime());
    po.setAddress(output.getAddress());
    if (null != output.getP2PKHScript()) {
        po.setScript(output.getP2PKHScript().getBytes());
    }
    if (OutPutStatusEnum.UTXO_SPENT == output.getStatus()) {
        po.setStatus(UtxoOutputPo.SPENT);
    } else if (OutPutStatusEnum.UTXO_CONFIRM_CONSENSUS_LOCK == output.getStatus()) {
        po.setStatus(UtxoOutputPo.LOCKED);
    } else {
        po.setStatus(UtxoOutputPo.USABLE);
    }
    return po;
}
Also used : UtxoOutputPo(io.nuls.db.entity.UtxoOutputPo)

Example 5 with UtxoOutputPo

use of io.nuls.db.entity.UtxoOutputPo in project nuls by nuls-io.

the class PocConsensusResource method profit.

@GET
@Path("/profit")
@Produces(MediaType.APPLICATION_JSON)
public RpcResult profit(@QueryParam("address") String address) {
    Map<String, Object> map = new HashMap<>();
    if ((address != null && !Address.validAddress(address))) {
        return RpcResult.getFailed(ErrorCode.PARAMETER_ERROR);
    }
    if (address == null) {
        address = accountService.getDefaultAccount().getAddress().getBase58();
    }
    // get all reward
    List<UtxoOutputPo> outputList = outputDataService.getAccountOutputs(TransactionConstant.TX_TYPE_COIN_BASE, address, null, null);
    long value = 0;
    for (UtxoOutputPo output : outputList) {
        value += output.getValue();
    }
    map.put("profit", Na.valueOf(value).toDouble());
    // get last 24 hours reward
    long nowTime = TimeService.currentTimeMillis();
    nowTime = nowTime - DateUtil.DATE_TIME;
    outputList = outputDataService.getAccountOutputs(TransactionConstant.TX_TYPE_COIN_BASE, address, nowTime, null);
    value = 0;
    for (UtxoOutputPo output : outputList) {
        value += output.getValue();
    }
    map.put("lastProfit", Na.valueOf(value).toDouble());
    map.put("investment", NulsContext.INVESTMENT.toDouble());
    RpcResult rpcResult = RpcResult.getSuccess();
    rpcResult.setData(map);
    return rpcResult;
}
Also used : HashMap(java.util.HashMap) RpcResult(io.nuls.rpc.entity.RpcResult) UtxoOutputPo(io.nuls.db.entity.UtxoOutputPo)

Aggregations

UtxoOutputPo (io.nuls.db.entity.UtxoOutputPo)7 UtxoInputPo (io.nuls.db.entity.UtxoInputPo)2 RpcResult (io.nuls.rpc.entity.RpcResult)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 NulsDigestData (io.nuls.core.chain.entity.NulsDigestData)1 NulsException (io.nuls.core.exception.NulsException)1 NulsRuntimeException (io.nuls.core.exception.NulsRuntimeException)1 UtxoOutputDataService (io.nuls.db.dao.UtxoOutputDataService)1 TxAccountRelationPo (io.nuls.db.entity.TxAccountRelationPo)1 DbSession (io.nuls.db.transactional.annotation.DbSession)1 UtxoData (io.nuls.ledger.entity.UtxoData)1 UtxoOutput (io.nuls.ledger.entity.UtxoOutput)1 AbstractCoinTransaction (io.nuls.ledger.entity.tx.AbstractCoinTransaction)1 LedgerCacheService (io.nuls.ledger.service.impl.LedgerCacheService)1 OutputDto (io.nuls.rpc.entity.OutputDto)1 ArrayList (java.util.ArrayList)1