Search in sources :

Example 16 with NulsException

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

the class BlockServiceImpl method getBlockList.

@Override
public List<Block> getBlockList(long startHeight, long endHeight) throws NulsException {
    List<Block> blockList = blockStorageService.getBlockList(startHeight, endHeight);
    if (blockList.size() < (endHeight - startHeight + 1)) {
        long currentMaxHeight = blockList.get(blockList.size() - 1).getHeader().getHeight();
        while (currentMaxHeight < endHeight) {
            long next = currentMaxHeight + 1;
            Block block = blockManager.getBlock(next);
            if (null == block) {
                try {
                    block = blockStorageService.getBlock(next);
                } catch (Exception e) {
                    Log.error(e);
                }
            }
            if (null != block) {
                blockList.add(block);
            }
        }
    }
    Collections.sort(blockList, BlockHeightComparator.getInstance());
    return blockList;
}
Also used : Block(io.nuls.core.chain.entity.Block) NulsException(io.nuls.core.exception.NulsException) IOException(java.io.IOException) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException)

Example 17 with NulsException

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

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

the class PocConsensusServiceImpl method joinTheConsensus.

private Transaction joinTheConsensus(Account account, String password, long amount, String agentHash) throws IOException, NulsException {
    AssertUtil.canNotEmpty(account);
    AssertUtil.canNotEmpty(password);
    if (amount < PocConsensusConstant.ENTRUSTER_DEPOSIT_LOWER_LIMIT.getValue()) {
        throw new NulsRuntimeException(ErrorCode.NULL_PARAMETER);
    }
    AssertUtil.canNotEmpty(agentHash);
    TransactionEvent event = new TransactionEvent();
    Consensus<Deposit> ca = new ConsensusDepositImpl();
    ca.setAddress(account.getAddress().toString());
    Deposit deposit = new Deposit();
    deposit.setAgentHash(agentHash);
    deposit.setDeposit(Na.valueOf(amount));
    deposit.setStartTime(TimeService.currentTimeMillis());
    ca.setExtend(deposit);
    CoinTransferData data = new CoinTransferData(OperationType.LOCK, this.ledgerService.getTxFee(TransactionConstant.TX_TYPE_JOIN_CONSENSUS));
    data.setTotalNa(deposit.getDeposit());
    data.addFrom(account.getAddress().toString());
    Coin coin = new Coin();
    coin.setUnlockHeight(0);
    coin.setUnlockTime(0);
    coin.setNa(deposit.getDeposit());
    data.addTo(account.getAddress().toString(), coin);
    PocJoinConsensusTransaction tx = null;
    try {
        tx = new PocJoinConsensusTransaction(data, password);
    } catch (NulsException e) {
        throw new NulsRuntimeException(e);
    }
    tx.setTime(TimeService.currentTimeMillis());
    tx.setTxData(ca);
    tx.setHash(NulsDigestData.calcDigestData(tx.serialize()));
    tx.setScriptSig(accountService.createP2PKHScriptSigFromDigest(tx.getHash(), account, password).serialize());
    tx.verifyWithException();
    event.setEventBody(tx);
    List<String> nodeList = eventBroadcaster.broadcastAndCache(event, true);
    if (null == nodeList || nodeList.isEmpty()) {
        throw new NulsRuntimeException(ErrorCode.FAILED, "broadcast transaction failed!");
    }
    return tx;
}
Also used : Deposit(io.nuls.consensus.entity.member.Deposit) 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) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) PocJoinConsensusTransaction(io.nuls.consensus.entity.tx.PocJoinConsensusTransaction) ConsensusDepositImpl(io.nuls.consensus.entity.ConsensusDepositImpl)

Example 19 with NulsException

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

the class BlockMaintenanceThread method getBestCorrectBlock.

private BestCorrectBlock getBestCorrectBlock() {
    BestCorrectBlock resultCorrentInfo = new BestCorrectBlock();
    Block localBestBlock = this.blockService.getLocalBestBlock();
    do {
        if (null == localBestBlock || localBestBlock.getHeader().getHeight() <= 1) {
            break;
        }
        BlockInfo netBestBlockInfo = DistributedBlockInfoRequestUtils.getInstance().request(0, null);
        resultCorrentInfo.setNetBestBlockInfo(netBestBlockInfo);
        if (null == netBestBlockInfo || netBestBlockInfo.getBestHash() == null) {
            break;
        }
        // same to network nodes
        if (netBestBlockInfo.getBestHeight() == localBestBlock.getHeader().getHeight() && netBestBlockInfo.getBestHash().equals(localBestBlock.getHeader().getHash())) {
            break;
        } else if (netBestBlockInfo.getBestHeight() <= localBestBlock.getHeader().getHeight()) {
            if (netBestBlockInfo.getBestHeight() == 0) {
                break;
            }
            // local height is highest
            BlockHeader header = null;
            try {
                header = blockService.getBlockHeader(netBestBlockInfo.getBestHeight());
            } catch (NulsException e) {
                break;
            }
            if (null != header && header.getHash().equals(netBestBlockInfo.getBestHash())) {
                break;
            }
            if (netBestBlockInfo.getNodeIdList().size() == 1) {
                throw new NulsRuntimeException(ErrorCode.FAILED, "node count not enough!");
            }
            Log.warn("Rollback block start height:{},local is highest and wrong!", localBestBlock.getHeader().getHeight());
            // bifurcation
            rollbackBlock(localBestBlock.getHeader().getHeight());
            localBestBlock = this.blockService.getLocalBestBlock();
            break;
        } else {
            netBestBlockInfo = DistributedBlockInfoRequestUtils.getInstance().request(localBestBlock.getHeader().getHeight(), netBestBlockInfo.getNodeIdList());
            if (netBestBlockInfo.getBestHash().equals(localBestBlock.getHeader().getHash())) {
                break;
            }
            if (localBestBlock.getHeader().getHeight() != netBestBlockInfo.getBestHeight()) {
                throw new NulsRuntimeException(ErrorCode.FAILED, "answer not asked!");
            }
            if (netBestBlockInfo.getNodeIdList().size() == 1) {
                throw new NulsRuntimeException(ErrorCode.FAILED, "node count not enough!");
            }
            Log.warn("Rollback block start height:{},local has wrong blocks!", localBestBlock.getHeader().getHeight());
            // bifurcation
            rollbackBlock(localBestBlock.getHeader().getHeight());
            localBestBlock = this.blockService.getLocalBestBlock();
        }
    } while (false);
    resultCorrentInfo.setLocalBestBlock(localBestBlock);
    return resultCorrentInfo;
}
Also used : BlockInfo(io.nuls.consensus.utils.BlockInfo) NulsException(io.nuls.core.exception.NulsException) Block(io.nuls.core.chain.entity.Block) BestCorrectBlock(io.nuls.consensus.entity.block.BestCorrectBlock) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) BlockHeader(io.nuls.core.chain.entity.BlockHeader) BestCorrectBlock(io.nuls.consensus.entity.block.BestCorrectBlock)

Example 20 with NulsException

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

the class BlockMaintenanceThread method rollbackBlock.

private void rollbackBlock(long startHeight) {
    long height = startHeight - 1;
    Block block = this.blockService.getBlock(height);
    try {
        this.blockService.rollbackBlock(startHeight);
        NulsContext.getInstance().setBestBlock(block);
    } catch (NulsException e) {
        Log.error(e);
        return;
    }
    boolean previousRb = false;
    if (height > 0) {
        NulsContext.getInstance().setBestBlock(block);
        BlockInfo blockInfo = DistributedBlockInfoRequestUtils.getInstance().request(height, null);
        if (null != blockInfo && null != blockInfo.getBestHash() && (!blockInfo.getBestHash().equals(block.getHeader().getHash()) && blockInfo.getBestHeight() == height)) {
            previousRb = true;
        }
    }
    if (previousRb) {
        rollbackBlock(height);
    }
}
Also used : NulsException(io.nuls.core.exception.NulsException) BlockInfo(io.nuls.consensus.utils.BlockInfo) Block(io.nuls.core.chain.entity.Block) BestCorrectBlock(io.nuls.consensus.entity.block.BestCorrectBlock)

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