Search in sources :

Example 1 with Transaction

use of io.nuls.core.chain.entity.Transaction 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 2 with Transaction

use of io.nuls.core.chain.entity.Transaction in project nuls by nuls-io.

the class BlockStorageService method getBlockList.

public List<Block> getBlockList(long startHeight, long endHeight) throws NulsException {
    List<Block> blockList = new ArrayList<>();
    List<BlockHeaderPo> poList = headerDao.getHeaderList(startHeight, endHeight);
    List<Long> heightList = new ArrayList<>();
    if (!poList.isEmpty()) {
        List<Transaction> txList = null;
        try {
            txList = ledgerService.getTxList(startHeight, endHeight);
        } catch (Exception e) {
            Log.error(e);
        }
        Map<Long, List<Transaction>> txListGroup = txListGrouping(txList);
        for (BlockHeaderPo po : poList) {
            BlockHeader header = null;
            try {
                header = ConsensusTool.fromPojo(po);
            } catch (NulsException e) {
                throw e;
            }
            heightList.add(header.getHeight());
            blockList.add(fillBlock(header, txListGroup.get(header.getHeight())));
        }
    }
    if ((endHeight - startHeight + 1) > blockList.size()) {
        for (long i = startHeight; i <= endHeight; i++) {
            if (heightList.contains(i)) {
                continue;
            }
            try {
                blockList.add(this.getBlock(i));
            } catch (Exception e) {
                Log.error(e);
            }
        }
    }
    return blockList;
}
Also used : ArrayList(java.util.ArrayList) NulsException(io.nuls.core.exception.NulsException) Transaction(io.nuls.core.chain.entity.Transaction) NulsException(io.nuls.core.exception.NulsException) Block(io.nuls.core.chain.entity.Block) ArrayList(java.util.ArrayList) List(java.util.List) BlockHeader(io.nuls.core.chain.entity.BlockHeader) BlockHeaderPo(io.nuls.db.entity.BlockHeaderPo)

Example 3 with Transaction

use of io.nuls.core.chain.entity.Transaction in project nuls by nuls-io.

the class BlockStorageService method getBlock.

public Block getBlock(String hash) throws Exception {
    Block block = blockCacheManager.getBlock(hash);
    if (null != block) {
        return block;
    }
    BlockHeader header = getBlockHeader(hash);
    if (null == header) {
        return null;
    }
    List<Transaction> txList = null;
    try {
        txList = ledgerService.getTxList(header.getHeight());
    } catch (Exception e) {
        Log.error(e);
    }
    if (txList.size() != header.getTxCount()) {
        System.out.println();
    }
    return fillBlock(header, txList);
}
Also used : Transaction(io.nuls.core.chain.entity.Transaction) Block(io.nuls.core.chain.entity.Block) BlockHeader(io.nuls.core.chain.entity.BlockHeader) NulsException(io.nuls.core.exception.NulsException)

Example 4 with Transaction

use of io.nuls.core.chain.entity.Transaction in project nuls by nuls-io.

the class ExitConsensusTxService method onApproval.

@Override
public void onApproval(PocExitConsensusTransaction tx) throws NulsException {
    Transaction joinTx = ledgerService.getTx(tx.getTxData());
    if (joinTx == null) {
        joinTx = ConfirmingTxCacheManager.getInstance().getTx(tx.getTxData());
    }
    if (joinTx.getType() == TransactionConstant.TX_TYPE_REGISTER_AGENT) {
        RegisterAgentTransaction raTx = (RegisterAgentTransaction) joinTx;
        manager.changeAgentStatusByHash(raTx.getTxData().getHexHash(), ConsensusStatusEnum.NOT_IN);
        manager.changeDepositStatusByAgentHash(raTx.getTxData().getHexHash(), ConsensusStatusEnum.NOT_IN);
        this.ledgerService.unlockTxApprove(tx.getTxData().getDigestHex());
        return;
    }
    PocJoinConsensusTransaction pjcTx = (PocJoinConsensusTransaction) joinTx;
    Consensus<Deposit> cd = pjcTx.getTxData();
    manager.changeDepositStatus(cd.getHexHash(), ConsensusStatusEnum.NOT_IN);
    this.ledgerService.unlockTxApprove(tx.getTxData().getDigestHex());
}
Also used : Deposit(io.nuls.consensus.entity.member.Deposit) RegisterAgentTransaction(io.nuls.consensus.entity.tx.RegisterAgentTransaction) PocExitConsensusTransaction(io.nuls.consensus.entity.tx.PocExitConsensusTransaction) Transaction(io.nuls.core.chain.entity.Transaction) RegisterAgentTransaction(io.nuls.consensus.entity.tx.RegisterAgentTransaction) PocJoinConsensusTransaction(io.nuls.consensus.entity.tx.PocJoinConsensusTransaction) PocJoinConsensusTransaction(io.nuls.consensus.entity.tx.PocJoinConsensusTransaction)

Example 5 with Transaction

use of io.nuls.core.chain.entity.Transaction in project nuls by nuls-io.

the class ExitConsensusTxService method onCommit.

@Override
public void onCommit(PocExitConsensusTransaction tx) throws NulsException {
    Transaction joinTx = ledgerService.getTx(tx.getTxData());
    if (joinTx.getType() == TransactionConstant.TX_TYPE_REGISTER_AGENT) {
        RegisterAgentTransaction raTx = (RegisterAgentTransaction) joinTx;
        manager.delAgent(raTx.getTxData().getHexHash());
        manager.delDepositByAgentHash(raTx.getTxData().getHexHash());
        this.ledgerService.unlockTxSave(tx.getTxData().getDigestHex());
        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("agentHash", raTx.getTxData().getHexHash());
        List<DepositPo> poList = depositDataService.getList(paramsMap);
        for (DepositPo po : poList) {
            this.ledgerService.unlockTxSave(po.getTxHash());
        }
        this.agentDataService.delete(raTx.getTxData().getHexHash());
        this.depositDataService.deleteByAgentHash(raTx.getTxData().getHexHash());
        return;
    }
    PocJoinConsensusTransaction pjcTx = (PocJoinConsensusTransaction) joinTx;
    Consensus<Deposit> cd = pjcTx.getTxData();
    manager.delDeposit(cd.getHexHash());
    this.depositDataService.delete(cd.getHexHash());
    this.ledgerService.unlockTxSave(tx.getTxData().getDigestHex());
}
Also used : Deposit(io.nuls.consensus.entity.member.Deposit) RegisterAgentTransaction(io.nuls.consensus.entity.tx.RegisterAgentTransaction) DepositPo(io.nuls.db.entity.DepositPo) PocExitConsensusTransaction(io.nuls.consensus.entity.tx.PocExitConsensusTransaction) Transaction(io.nuls.core.chain.entity.Transaction) RegisterAgentTransaction(io.nuls.consensus.entity.tx.RegisterAgentTransaction) PocJoinConsensusTransaction(io.nuls.consensus.entity.tx.PocJoinConsensusTransaction) HashMap(java.util.HashMap) PocJoinConsensusTransaction(io.nuls.consensus.entity.tx.PocJoinConsensusTransaction)

Aggregations

Transaction (io.nuls.core.chain.entity.Transaction)34 NulsRuntimeException (io.nuls.core.exception.NulsRuntimeException)13 NulsException (io.nuls.core.exception.NulsException)12 Block (io.nuls.core.chain.entity.Block)8 HashMap (java.util.HashMap)7 NulsDigestData (io.nuls.core.chain.entity.NulsDigestData)6 ValidateResult (io.nuls.core.validate.ValidateResult)6 BlockHeader (io.nuls.core.chain.entity.BlockHeader)5 IOException (java.io.IOException)5 Deposit (io.nuls.consensus.entity.member.Deposit)4 PocExitConsensusTransaction (io.nuls.consensus.entity.tx.PocExitConsensusTransaction)4 PocJoinConsensusTransaction (io.nuls.consensus.entity.tx.PocJoinConsensusTransaction)4 RegisterAgentTransaction (io.nuls.consensus.entity.tx.RegisterAgentTransaction)4 ArrayList (java.util.ArrayList)4 RedPunishTransaction (io.nuls.consensus.entity.tx.RedPunishTransaction)3 YellowPunishTransaction (io.nuls.consensus.entity.tx.YellowPunishTransaction)3 NulsByteBuffer (io.nuls.core.utils.io.NulsByteBuffer)3 AbstractCoinTransaction (io.nuls.ledger.entity.tx.AbstractCoinTransaction)3 CoinBaseTransaction (io.nuls.ledger.entity.tx.CoinBaseTransaction)3 Account (io.nuls.account.entity.Account)2