Search in sources :

Example 6 with DbSession

use of io.nuls.db.transactional.annotation.DbSession 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 7 with DbSession

use of io.nuls.db.transactional.annotation.DbSession 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 8 with DbSession

use of io.nuls.db.transactional.annotation.DbSession in project nuls by nuls-io.

the class UtxoLedgerServiceImpl method unlockTxRollback.

@Override
@DbSession
public void unlockTxRollback(String txHash) {
    boolean b = true;
    int index = 0;
    while (b) {
        UtxoOutput output = ledgerCacheService.getUtxo(txHash + "-" + index);
        if (output != null) {
            if (OutPutStatusEnum.UTXO_UNCONFIRM_UNSPEND == output.getStatus()) {
                output.setStatus(OutPutStatusEnum.UTXO_UNCONFIRM_CONSENSUS_LOCK);
            } else if (OutPutStatusEnum.UTXO_CONFIRM_UNSPEND == output.getStatus()) {
                output.setStatus(OutPutStatusEnum.UTXO_CONFIRM_CONSENSUS_LOCK);
            }
            index++;
        } else {
            b = false;
        }
    }
    txDao.lockTxOutput(txHash);
}
Also used : UtxoOutput(io.nuls.ledger.entity.UtxoOutput) DbSession(io.nuls.db.transactional.annotation.DbSession)

Example 9 with DbSession

use of io.nuls.db.transactional.annotation.DbSession in project nuls by nuls-io.

the class BlockServiceImpl method saveBlock.

@Override
@DbSession
public boolean saveBlock(Block block) throws IOException {
    block.verifyWithException();
    boolean b = false;
    for (int x = 0; x < block.getHeader().getTxCount(); x++) {
        Transaction tx = block.getTxs().get(x);
        if (tx.getStatus() == TxStatusEnum.CACHED) {
            b = true;
            tx.setBlockHeight(block.getHeader().getHeight());
            try {
                ledgerService.approvalTx(tx);
            } catch (Exception e) {
                Log.error(e);
                rollback(block.getTxs(), x);
                throw new NulsRuntimeException(e);
            }
        }
    }
    if (b) {
        block.verifyWithException();
    }
    for (int x = 0; x < block.getHeader().getTxCount(); x++) {
        Transaction tx = block.getTxs().get(x);
        if (tx.getStatus() == TxStatusEnum.AGREED) {
            tx.setBlockHeight(block.getHeader().getHeight());
            try {
                ledgerService.commitTx(tx);
            } catch (Exception e) {
                Log.error(e);
                rollback(block.getTxs(), x);
                throw new NulsRuntimeException(e);
            }
        }
    }
    ledgerService.saveTxList(block.getTxs());
    blockStorageService.save(block);
    return true;
}
Also used : Transaction(io.nuls.core.chain.entity.Transaction) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) NulsException(io.nuls.core.exception.NulsException) IOException(java.io.IOException) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) DbSession(io.nuls.db.transactional.annotation.DbSession)

Example 10 with DbSession

use of io.nuls.db.transactional.annotation.DbSession in project nuls by nuls-io.

the class BlockServiceImpl method rollbackBlock.

@Override
@DbSession
public void rollbackBlock(long height) {
    Block block = this.getBlock(height);
    if (null == block) {
        return;
    }
    blockManager.rollback(block);
    this.rollback(block.getTxs(), block.getTxs().size() - 1);
    this.ledgerService.deleteTx(block.getHeader().getHeight());
    blockStorageService.delete(block.getHeader().getHash().getDigestHex());
}
Also used : Block(io.nuls.core.chain.entity.Block) DbSession(io.nuls.db.transactional.annotation.DbSession)

Aggregations

DbSession (io.nuls.db.transactional.annotation.DbSession)15 NulsException (io.nuls.core.exception.NulsException)7 NulsRuntimeException (io.nuls.core.exception.NulsRuntimeException)6 AccountPo (io.nuls.db.entity.AccountPo)6 Account (io.nuls.account.entity.Account)4 Result (io.nuls.core.chain.entity.Result)3 AliasPo (io.nuls.db.entity.AliasPo)3 TransactionLocalPo (io.nuls.db.entity.TransactionLocalPo)3 ValidateResult (io.nuls.core.validate.ValidateResult)2 TransactionPo (io.nuls.db.entity.TransactionPo)2 UtxoInputPo (io.nuls.db.entity.UtxoInputPo)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Block (io.nuls.core.chain.entity.Block)1 Transaction (io.nuls.core.chain.entity.Transaction)1 Searchable (io.nuls.db.dao.impl.mybatis.util.Searchable)1 TxAccountRelationPo (io.nuls.db.entity.TxAccountRelationPo)1 UtxoOutputPo (io.nuls.db.entity.UtxoOutputPo)1 UtxoOutput (io.nuls.ledger.entity.UtxoOutput)1 AbstractCoinTransaction (io.nuls.ledger.entity.tx.AbstractCoinTransaction)1