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