Search in sources :

Example 11 with NulsException

use of io.nuls.core.exception.NulsException 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 12 with NulsException

use of io.nuls.core.exception.NulsException in project nuls by nuls-io.

the class UtxoCoinDataProvider method createByTransferData.

@Override
public CoinData createByTransferData(Transaction tx, CoinTransferData coinParam, String password) throws NulsException {
    UtxoData utxoData = new UtxoData();
    List<UtxoInput> inputs = new ArrayList<>();
    List<UtxoOutput> outputs = new ArrayList<>();
    if (coinParam.getTotalNa().equals(Na.ZERO)) {
        utxoData.setInputs(inputs);
        utxoData.setOutputs(outputs);
        return utxoData;
    }
    long inputValue = 0;
    if (!coinParam.getFrom().isEmpty()) {
        // find unSpends to create inputs for this tx
        Na totalFee = Na.ZERO;
        if (tx instanceof UnlockNulsTransaction) {
            totalFee = coinParam.getFee();
        } else {
            totalFee = coinParam.getTotalNa().add(coinParam.getFee());
        }
        List<UtxoOutput> unSpends = coinManager.getAccountsUnSpend(coinParam.getFrom(), totalFee);
        if (unSpends.isEmpty()) {
            throw new NulsException(ErrorCode.BALANCE_NOT_ENOUGH);
        }
        for (int i = 0; i < unSpends.size(); i++) {
            UtxoOutput output = unSpends.get(i);
            UtxoInput input = new UtxoInput();
            input.setFrom(output);
            input.setFromHash(output.getTxHash());
            input.setFromIndex(output.getIndex());
            input.setTxHash(tx.getHash());
            input.setIndex(i);
            inputValue += output.getValue();
            inputs.add(input);
        }
    }
    // get EcKey for output's script
    Account account = null;
    byte[] priKey = null;
    if (coinParam.getPriKey() != null) {
        priKey = coinParam.getPriKey();
    } else if (!coinParam.getFrom().isEmpty()) {
        account = accountService.getAccount(coinParam.getFrom().get(0));
        if (account == null) {
            throw new NulsException(ErrorCode.ACCOUNT_NOT_EXIST);
        }
        if (account.isEncrypted() && account.isLocked()) {
            if (!account.unlock(password)) {
                throw new NulsException(ErrorCode.PASSWORD_IS_WRONG);
            }
            priKey = account.getPriKey();
            account.lock();
        } else {
            priKey = account.getPriKey();
        }
    }
    // create outputs
    int i = 0;
    long outputValue = 0;
    for (Map.Entry<String, List<Coin>> entry : coinParam.getToMap().entrySet()) {
        String address = entry.getKey();
        List<Coin> coinList = entry.getValue();
        for (Coin coin : coinList) {
            UtxoOutput output = new UtxoOutput();
            output.setAddress(address);
            output.setValue(coin.getNa().getValue());
            if (output.getLockTime() > 0) {
                output.setStatus(OutPutStatusEnum.UTXO_UNCONFIRM_TIME_LOCK);
            } else if (tx instanceof LockNulsTransaction) {
                output.setStatus(OutPutStatusEnum.UTXO_UNCONFIRM_CONSENSUS_LOCK);
            } else {
                output.setStatus(OutPutStatusEnum.UTXO_UNCONFIRM_UNSPEND);
            }
            output.setIndex(i);
            P2PKHScript p2PKHScript = new P2PKHScript(new NulsDigestData(NulsDigestData.DIGEST_ALG_SHA160, new Address(address).getHash160()));
            output.setP2PKHScript(p2PKHScript);
            if (coin.getUnlockHeight() > 0) {
                output.setLockTime(coin.getUnlockHeight());
            } else if (coin.getUnlockTime() > 0) {
                output.setLockTime(coin.getUnlockTime());
            } else {
                output.setLockTime(0L);
            }
            output.setTxHash(tx.getHash());
            outputValue += output.getValue();
            outputs.add(output);
            i++;
        }
    }
    // the balance leave to myself
    long balance = inputValue - outputValue - coinParam.getFee().getValue();
    if (balance > 0) {
        UtxoOutput output = new UtxoOutput();
        // todo script
        output.setAddress(inputs.get(0).getFrom().getAddress());
        output.setValue(balance);
        output.setIndex(i);
        output.setTxHash(tx.getHash());
        output.setStatus(OutPutStatusEnum.UTXO_UNCONFIRM_UNSPEND);
        P2PKHScript p2PKHScript = new P2PKHScript(new NulsDigestData(NulsDigestData.DIGEST_ALG_SHA160, account.getHash160()));
        output.setP2PKHScript(p2PKHScript);
        outputs.add(output);
    }
    utxoData.setInputs(inputs);
    utxoData.setOutputs(outputs);
    return utxoData;
}
Also used : P2PKHScript(io.nuls.core.script.P2PKHScript) Account(io.nuls.account.entity.Account) Address(io.nuls.account.entity.Address) Coin(io.nuls.ledger.entity.params.Coin) LockNulsTransaction(io.nuls.ledger.entity.tx.LockNulsTransaction) Na(io.nuls.core.chain.entity.Na) NulsException(io.nuls.core.exception.NulsException) UnlockNulsTransaction(io.nuls.ledger.entity.tx.UnlockNulsTransaction) NulsDigestData(io.nuls.core.chain.entity.NulsDigestData)

Example 13 with NulsException

use of io.nuls.core.exception.NulsException in project nuls by nuls-io.

the class UtxoLedgerServiceImpl method saveTxList.

@Override
@DbSession
public boolean saveTxList(List<Transaction> txList) throws IOException {
    lock.lock();
    try {
        List<TransactionPo> poList = new ArrayList<>();
        List<TransactionLocalPo> localPoList = new ArrayList<>();
        for (int i = 0; i < txList.size(); i++) {
            Transaction tx = txList.get(i);
            boolean isMine = false;
            try {
                isMine = this.checkTxIsMine(tx);
            } catch (NulsException e) {
                throw new NulsRuntimeException(e);
            }
            TransactionPo po = UtxoTransferTool.toTransactionPojo(tx);
            poList.add(po);
            if (isMine) {
                TransactionLocalPo localPo = UtxoTransferTool.toLocalTransactionPojo(tx);
                localPoList.add(localPo);
            }
        }
        txDao.saveTxList(poList);
        if (localPoList.size() > 0) {
            txDao.saveLocalList(localPoList);
        }
    } finally {
        lock.unlock();
    }
    return false;
}
Also used : TransferTransaction(io.nuls.ledger.entity.tx.TransferTransaction) AbstractCoinTransaction(io.nuls.ledger.entity.tx.AbstractCoinTransaction) LockNulsTransaction(io.nuls.ledger.entity.tx.LockNulsTransaction) NulsException(io.nuls.core.exception.NulsException) TransactionLocalPo(io.nuls.db.entity.TransactionLocalPo) ArrayList(java.util.ArrayList) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) TransactionPo(io.nuls.db.entity.TransactionPo) DbSession(io.nuls.db.transactional.annotation.DbSession)

Example 14 with NulsException

use of io.nuls.core.exception.NulsException in project nuls by nuls-io.

the class NetworkEventService method publish.

public void publish(byte[] event, String nodeId) {
    try {
        BaseEvent eventObject = EventManager.getInstance(event);
        this.publish(eventObject, nodeId);
    } catch (NulsException e) {
        Log.error(e);
    }
}
Also used : NulsException(io.nuls.core.exception.NulsException) BaseEvent(io.nuls.core.event.BaseEvent)

Example 15 with NulsException

use of io.nuls.core.exception.NulsException in project nuls by nuls-io.

the class UtxoLedgerServiceImpl method lock.

@Override
public Result lock(String address, String password, Na amount, long unlockTime, String remark) {
    LockNulsTransaction tx = null;
    try {
        CoinTransferData coinData = new CoinTransferData(OperationType.LOCK, amount, address, getTxFee(TransactionConstant.TX_TYPE_LOCK));
        coinData.addTo(address, new Coin(amount, unlockTime));
        tx = UtxoTransactionTool.getInstance().createLockNulsTx(coinData, password, remark);
        tx.verify();
        TransactionEvent event = new TransactionEvent();
        event.setEventBody(tx);
        eventBroadcaster.broadcastAndCacheAysn(event, true);
    } catch (Exception e) {
        Log.error(e);
        try {
            rollbackTx(tx);
        } catch (NulsException e1) {
            Log.error(e1);
        }
        return new Result(false, e.getMessage());
    }
    return new Result(true, "OK", tx.getHash().getDigestHex());
}
Also used : LockNulsTransaction(io.nuls.ledger.entity.tx.LockNulsTransaction) Coin(io.nuls.ledger.entity.params.Coin) TransactionEvent(io.nuls.ledger.event.TransactionEvent) CoinTransferData(io.nuls.ledger.entity.params.CoinTransferData) NulsException(io.nuls.core.exception.NulsException) NulsException(io.nuls.core.exception.NulsException) IOException(java.io.IOException) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) ValidateResult(io.nuls.core.validate.ValidateResult)

Aggregations

NulsException (io.nuls.core.exception.NulsException)69 NulsRuntimeException (io.nuls.core.exception.NulsRuntimeException)32 IOException (java.io.IOException)17 ValidateResult (io.nuls.core.validate.ValidateResult)12 Account (io.nuls.account.entity.Account)11 Transaction (io.nuls.core.chain.entity.Transaction)8 BlockRoundData (io.nuls.consensus.entity.block.BlockRoundData)7 Block (io.nuls.core.chain.entity.Block)6 DbSession (io.nuls.db.transactional.annotation.DbSession)6 Coin (io.nuls.ledger.entity.params.Coin)6 CoinTransferData (io.nuls.ledger.entity.params.CoinTransferData)6 Result (io.nuls.core.chain.entity.Result)5 TransactionEvent (io.nuls.ledger.event.TransactionEvent)5 BlockHeader (io.nuls.core.chain.entity.BlockHeader)4 NulsDigestData (io.nuls.core.chain.entity.NulsDigestData)4 Address (io.nuls.account.entity.Address)3 PocMeetingRound (io.nuls.consensus.entity.meeting.PocMeetingRound)3 P2PKHScriptSig (io.nuls.core.script.P2PKHScriptSig)3 NulsByteBuffer (io.nuls.core.utils.io.NulsByteBuffer)3 AccountPo (io.nuls.db.entity.AccountPo)3