Search in sources :

Example 6 with NulsRuntimeException

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

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

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

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

the class ConsensusMeetingRunner method calcRound.

private PocMeetingRound calcRound() {
    PocMeetingRound round = new PocMeetingRound(this.consensusManager.getCurrentRound());
    Block bestBlock = context.getBestBlock();
    BlockRoundData lastRoundData;
    try {
        lastRoundData = new BlockRoundData(bestBlock.getHeader().getExtend());
    } catch (NulsException e) {
        Log.error(e);
        throw new NulsRuntimeException(e);
    }
    if (round.getPreviousRound() == null || round.getPreviousRound().getIndex() <= lastRoundData.getRoundIndex()) {
        while (true) {
            if (lastRoundData.getPackingIndexOfRound() == lastRoundData.getConsensusMemberCount() || lastRoundData.getRoundEndTime() <= TimeService.currentTimeMillis()) {
                break;
            }
            try {
                bestBlock = context.getBestBlock();
                lastRoundData = new BlockRoundData(bestBlock.getHeader().getExtend());
            } catch (NulsException e) {
                Log.error(e);
            }
            try {
                Thread.sleep(100L);
            } catch (InterruptedException e) {
                Log.error(e);
            }
        }
        PocMeetingRound preRound = new PocMeetingRound(null);
        preRound.setIndex(lastRoundData.getRoundIndex());
        preRound.setStartTime(lastRoundData.getRoundStartTime());
        preRound.setMemberCount(lastRoundData.getConsensusMemberCount());
        round.setPreviousRound(preRound);
    }
    round.setStartTime(round.getPreviousRound().getEndTime());
    round.setIndex(lastRoundData.getRoundIndex() + 1);
    return round;
}
Also used : NulsException(io.nuls.core.exception.NulsException) PocMeetingRound(io.nuls.consensus.entity.meeting.PocMeetingRound) BlockRoundData(io.nuls.consensus.entity.block.BlockRoundData) Block(io.nuls.core.chain.entity.Block) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException)

Example 10 with NulsRuntimeException

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

the class DistributedBlockInfoRequestUtils method calc.

private void calc() {
    if (null == nodeIdList || nodeIdList.isEmpty()) {
        throw new NulsRuntimeException(ErrorCode.FAILED, "success list of nodes is empty!");
    }
    int size = nodeIdList.size();
    int halfSize = (size + 1) / 2;
    // 
    if (hashesMap.size() < halfSize) {
        return;
    }
    BlockInfo result = null;
    for (String key : calcMap.keySet()) {
        List<String> nodes = calcMap.get(key);
        if (nodes == null) {
            continue;
        }
        // todo =
        if (nodes.size() >= halfSize) {
            result = new BlockInfo();
            BlockHashResponse response = hashesMap.get(nodes.get(0));
            if (response == null || response.getHeightList() == null) {
                // todo check it
                continue;
            }
            Long bestHeight = 0L;
            NulsDigestData bestHash = null;
            for (int i = 0; i < response.getHeightList().size(); i++) {
                Long height = response.getHeightList().get(i);
                NulsDigestData hash = response.getHashList().get(i);
                if (height >= bestHeight) {
                    bestHash = hash;
                    bestHeight = height;
                }
                result.putHash(height, hash);
            }
            result.setBestHash(bestHash);
            result.setBestHeight(bestHeight);
            result.setNodeIdList(nodes);
            result.setFinished(true);
            break;
        }
    }
    if (null != result) {
        bestBlockInfo = result;
    }
// else if (size == calcMap.size()) {
// try {
// Thread.sleep(2000L);
// } catch (InterruptedException e) {
// Log.error(e);
// }
// try {
// this.request(start, end, split);
// } catch (Exception e) {
// Log.error(e.getMessage());
// }
// }
}
Also used : BlockHashResponse(io.nuls.consensus.entity.BlockHashResponse) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) NulsDigestData(io.nuls.core.chain.entity.NulsDigestData)

Aggregations

NulsRuntimeException (io.nuls.core.exception.NulsRuntimeException)64 NulsException (io.nuls.core.exception.NulsException)26 IOException (java.io.IOException)11 Transaction (io.nuls.core.chain.entity.Transaction)10 ArrayList (java.util.ArrayList)9 Account (io.nuls.account.entity.Account)7 AbstractNulsQueue (io.nuls.core.utils.queue.intf.AbstractNulsQueue)7 DbSession (io.nuls.db.transactional.annotation.DbSession)6 CoinTransferData (io.nuls.ledger.entity.params.CoinTransferData)5 ValidateResult (io.nuls.core.validate.ValidateResult)4 Coin (io.nuls.ledger.entity.params.Coin)4 TransactionEvent (io.nuls.ledger.event.TransactionEvent)4 AccountService (io.nuls.account.service.intf.AccountService)3 Block (io.nuls.core.chain.entity.Block)3 AccountPo (io.nuls.db.entity.AccountPo)3 AbstractCoinTransaction (io.nuls.ledger.entity.tx.AbstractCoinTransaction)3 BlockHashResponse (io.nuls.consensus.entity.BlockHashResponse)2 RedPunishData (io.nuls.consensus.entity.RedPunishData)2 BestCorrectBlock (io.nuls.consensus.entity.block.BestCorrectBlock)2 Agent (io.nuls.consensus.entity.member.Agent)2