Search in sources :

Example 31 with NulsException

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

the class VersionManager method start.

public static void start() throws NulsException {
    URL libsUrl = VersionUtils.class.getClassLoader().getResource("libs");
    if (null == libsUrl) {
        // devlopment
        return;
    }
    List<NulsVersion> versionList = NulsContext.getServiceBeanList(NulsVersion.class);
    String localVersion = null;
    for (NulsVersion version : versionList) {
        if (null == localVersion) {
            localVersion = version.getVersion();
            continue;
        }
        if (VersionUtils.higherThan(version.getVersion(), localVersion)) {
            localVersion = version.getVersion();
            continue;
        }
    }
    NulsContext.VERSION = localVersion;
    String jsonStr = null;
    try {
        jsonStr = new String(HttpDownloadUtils.download(HIGHEST_VERDION_FILE_URL), NulsContext.DEFAULT_ENCODING);
    } catch (IOException e) {
        Log.error(e);
        return;
    }
    Map<String, Object> map = null;
    try {
        map = JSONUtils.json2map(jsonStr);
    } catch (Exception e) {
        Log.error(e);
        throw new NulsException(ErrorCode.FAILED, "Parse version json faild!");
    }
    String version = (String) map.get("version");
    String sign = (String) map.get("sign");
    // todo 验证签名
    NulsContext.NEWEST_VERSION = version;
    // todo 自动更新开关
    boolean autoUpdate = true;
    if (VersionUtils.equalsWith(version, localVersion)) {
        return;
    } else if (autoUpdate) {
        try {
            updateFile(localVersion, version, versionList, libsUrl);
        } catch (IOException e) {
            throw new NulsException(e);
        }
    }
}
Also used : NulsException(io.nuls.core.exception.NulsException) NulsVersion(io.nuls.core.chain.intf.NulsVersion) VersionUtils(io.nuls.core.utils.str.VersionUtils) URL(java.net.URL) NulsException(io.nuls.core.exception.NulsException)

Example 32 with NulsException

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

the class BaseEvent method parse.

@Override
protected void parse(NulsByteBuffer byteBuffer) throws NulsException {
    this.header = byteBuffer.readNulsData(new EventHeader());
    this.eventBody = parseEventBody(byteBuffer);
    try {
        this.hash = NulsDigestData.calcDigestData(this.serialize());
    } catch (IOException e) {
        Log.error(e);
        throw new NulsException(ErrorCode.DATA_PARSE_ERROR);
    }
}
Also used : NulsException(io.nuls.core.exception.NulsException) IOException(java.io.IOException)

Example 33 with NulsException

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

the class BlockManager method rollbackTxList.

private void rollbackTxList(List<Transaction> txList, int start, int end) {
    List<NulsDigestData> txHashList = new ArrayList<>();
    for (int i = start; i <= end && i < txList.size(); i++) {
        Transaction tx = txList.get(i);
        if (tx.getStatus() == TxStatusEnum.AGREED) {
            try {
                ledgerService.rollbackTx(tx);
            } catch (NulsException e) {
                Log.error(e);
            }
            txHashList.add(tx.getHash());
        }
    }
    confirmingTxCacheManager.removeTxList(txHashList);
}
Also used : Transaction(io.nuls.core.chain.entity.Transaction) NulsException(io.nuls.core.exception.NulsException) ArrayList(java.util.ArrayList) NulsDigestData(io.nuls.core.chain.entity.NulsDigestData)

Example 34 with NulsException

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

the class BlockManager method appravalBlock.

private void appravalBlock(Block block) {
    for (int i = 0; i < block.getHeader().getTxCount(); i++) {
        Transaction tx = block.getTxs().get(i);
        tx.setBlockHeight(block.getHeader().getHeight());
        tx.setIndex(i);
        tx.setIndex(i);
        if (tx.getStatus() == null || tx.getStatus() == TxStatusEnum.CACHED) {
            try {
                this.ledgerService.approvalTx(tx);
                confirmingTxCacheManager.putTx(tx);
            } catch (NulsException e) {
                rollbackTxList(block.getTxs(), 0, i);
                Log.error(e);
                throw new NulsRuntimeException(e);
            }
        }
    }
    txCacheManager.removeTx(block.getTxHashList());
}
Also used : Transaction(io.nuls.core.chain.entity.Transaction) NulsException(io.nuls.core.exception.NulsException) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException)

Example 35 with NulsException

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

the class HeaderContinuityValidator method validate.

@Override
public ValidateResult validate(BlockHeader header) {
    ValidateResult result = ValidateResult.getSuccessResult();
    boolean failed = false;
    do {
        if (header.getHeight() == 0) {
            failed = !header.getPreHash().equals(NulsDigestData.EMPTY_HASH);
            break;
        }
        BlockHeader preHeader = null;
        try {
            preHeader = NulsContext.getServiceBean(BlockService.class).getBlockHeader(header.getHeight() - 1);
        } catch (NulsException e) {
            // todo
            e.printStackTrace();
        }
        if (null == preHeader) {
            break;
        }
        failed = !preHeader.getHash().equals(header.getPreHash());
        if (failed) {
            break;
        }
        BlockRoundData roundData = new BlockRoundData();
        try {
            roundData.parse(header.getExtend());
        } catch (NulsException e) {
            Log.error(e);
        }
        long shouldTime = roundData.getRoundStartTime() + roundData.getPackingIndexOfRound() * PocConsensusConstant.BLOCK_TIME_INTERVAL_SECOND * 1000;
        // todo 3 seconds error
        long difference = header.getTime() - shouldTime;
        failed = difference > 3000 || difference < -3000;
        if (failed) {
            break;
        }
    } while (false);
    if (failed) {
        result = ValidateResult.getFailedResult(ERROR_MESSAGE);
    }
    return result;
}
Also used : NulsException(io.nuls.core.exception.NulsException) ValidateResult(io.nuls.core.validate.ValidateResult) BlockRoundData(io.nuls.consensus.entity.block.BlockRoundData) BlockHeader(io.nuls.core.chain.entity.BlockHeader)

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