Search in sources :

Example 36 with NulsRuntimeException

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

the class AESEncrypt method decrypt.

/**
 * 解密
 *
 * @param dataToDecrypt
 * @param aesKey
 * @return byte[]
 * @throws NulsRuntimeException
 */
public static byte[] decrypt(EncryptedData dataToDecrypt, KeyParameter aesKey) throws NulsRuntimeException {
    Utils.checkNotNull(dataToDecrypt);
    Utils.checkNotNull(aesKey);
    try {
        ParametersWithIV keyWithIv = new ParametersWithIV(new KeyParameter(aesKey.getKey()), dataToDecrypt.getInitialisationVector());
        // Decrypt the message.
        BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
        cipher.init(false, keyWithIv);
        byte[] cipherBytes = dataToDecrypt.getEncryptedBytes();
        byte[] decryptedBytes = new byte[cipher.getOutputSize(cipherBytes.length)];
        final int length1 = cipher.processBytes(cipherBytes, 0, cipherBytes.length, decryptedBytes, 0);
        final int length2 = cipher.doFinal(decryptedBytes, length1);
        return Arrays.copyOf(decryptedBytes, length1 + length2);
    } catch (Exception e) {
        throw new NulsRuntimeException(e);
    }
}
Also used : ParametersWithIV(org.spongycastle.crypto.params.ParametersWithIV) PaddedBufferedBlockCipher(org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher) BufferedBlockCipher(org.spongycastle.crypto.BufferedBlockCipher) PaddedBufferedBlockCipher(org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher) KeyParameter(org.spongycastle.crypto.params.KeyParameter) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) CBCBlockCipher(org.spongycastle.crypto.modes.CBCBlockCipher) AESFastEngine(org.spongycastle.crypto.engines.AESFastEngine) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException)

Example 37 with NulsRuntimeException

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

the class DistributedBlockInfoRequestUtils method getBlockInfo.

private BlockInfo getBlockInfo() {
    while (true) {
        if (null != bestBlockInfo && bestBlockInfo.isFinished()) {
            break;
        }
        try {
            Thread.sleep(10L);
        } catch (InterruptedException e) {
            Log.error(e);
        }
        long timeout = 10000L;
        if ((TimeService.currentTimeMillis() - startTime) > (timeout - 1000L) && hashesMap.size() >= ((nodeIdList.size() + 1) / 2) && start == end && start <= 0) {
            long localHeight = NulsContext.getInstance().getBestBlock().getHeader().getHeight();
            long minHeight = Long.MAX_VALUE;
            NulsDigestData minHash = null;
            List<String> nodeIds = new ArrayList<>();
            try {
                for (String nodeId : hashesMap.keySet()) {
                    BlockHashResponse response = hashesMap.get(nodeId);
                    long height = response.getHeightList().get(0);
                    NulsDigestData hash = response.getHashList().get(0);
                    if (height >= localHeight) {
                        if (height <= minHeight) {
                            minHeight = height;
                            minHash = hash;
                        }
                        nodeIds.add(nodeId);
                    }
                }
            } catch (Exception e) {
                break;
            }
            BlockInfo result = new BlockInfo();
            result.putHash(minHeight, minHash);
            result.setBestHash(minHash);
            result.setBestHeight(minHeight);
            result.setNodeIdList(nodeIds);
            result.setFinished(true);
            if (result.getBestHeight() < Long.MAX_VALUE) {
                bestBlockInfo = result;
            } else {
                throw new NulsRuntimeException(ErrorCode.TIME_OUT);
            }
        } else if ((TimeService.currentTimeMillis() - startTime) > timeout && !(hashesMap.size() >= ((nodeIdList.size() + 1) / 2) && start == end && start <= 0)) {
            throw new NulsRuntimeException(ErrorCode.TIME_OUT);
        }
    }
    BlockInfo info = bestBlockInfo;
    bestBlockInfo = null;
    requesting = false;
    return info;
}
Also used : BlockHashResponse(io.nuls.consensus.entity.BlockHashResponse) ArrayList(java.util.ArrayList) NulsDigestData(io.nuls.core.chain.entity.NulsDigestData) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException)

Example 38 with NulsRuntimeException

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

the class DownloadDataUtils method requestTxGroup.

public void requestTxGroup(NulsDigestData blockHash, String nodeId) {
    GetTxGroupRequest request = new GetTxGroupRequest();
    GetTxGroupParam data = new GetTxGroupParam();
    data.setBlockHash(blockHash);
    List<NulsDigestData> txHashList = new ArrayList<>();
    SmallBlock smb = temporaryCacheManager.getSmallBlock(blockHash.getDigestHex());
    for (NulsDigestData txHash : smb.getTxHashList()) {
        boolean exist = txCacheManager.txExist(txHash);
        if (!exist) {
            txHashList.add(txHash);
        }
    }
    if (txHashList.isEmpty()) {
        BlockHeader header = temporaryCacheManager.getBlockHeader(smb.getBlockHash().getDigestHex());
        if (null == header) {
            return;
        }
        Block block = new Block();
        block.setHeader(header);
        List<Transaction> txs = new ArrayList<>();
        for (NulsDigestData txHash : smb.getTxHashList()) {
            Transaction tx = txCacheManager.getTx(txHash);
            if (null == tx) {
                throw new NulsRuntimeException(ErrorCode.DATA_ERROR);
            }
            txs.add(tx);
        }
        block.setTxs(txs);
        ValidateResult<RedPunishData> vResult = block.verify();
        if (null == vResult || vResult.isFailed()) {
            if (vResult.getLevel() == SeverityLevelEnum.FLAGRANT_FOUL) {
                RedPunishData redPunishData = vResult.getObject();
                ConsensusMeetingRunner.putPunishData(redPunishData);
            }
            return;
        }
        blockManager.addBlock(block, false, nodeId);
        AssembledBlockNotice notice = new AssembledBlockNotice();
        notice.setEventBody(header);
        eventBroadcaster.publishToLocal(notice);
        return;
    }
    data.setTxHashList(txHashList);
    request.setEventBody(data);
    tgRequest.put(blockHash.getDigestHex(), System.currentTimeMillis());
    Integer value = tgRequestCount.get(blockHash.getDigestHex());
    if (null == value) {
        value = 0;
    }
    tgRequestCount.put(blockHash.getDigestHex(), 1 + value);
    if (StringUtils.isBlank(nodeId)) {
        eventBroadcaster.broadcastAndCache(request, false);
    } else {
        eventBroadcaster.sendToNode(request, nodeId);
    }
}
Also used : GetTxGroupRequest(io.nuls.consensus.event.GetTxGroupRequest) ArrayList(java.util.ArrayList) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) RedPunishData(io.nuls.consensus.entity.RedPunishData) GetTxGroupParam(io.nuls.consensus.entity.GetTxGroupParam) AssembledBlockNotice(io.nuls.consensus.event.notice.AssembledBlockNotice)

Example 39 with NulsRuntimeException

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

the class BaseNulsData method serialize.

/**
 * First, serialize the version field
 *
 * @return
 */
public final byte[] serialize() throws IOException {
    ByteArrayOutputStream bos = null;
    try {
        int size = size();
        bos = new UnsafeByteArrayOutputStream(size);
        NulsOutputStreamBuffer buffer = new NulsOutputStreamBuffer(bos);
        if (size == 0) {
            bos.write(NulsConstant.PLACE_HOLDER);
        } else {
            serializeToStream(buffer);
        }
        byte[] bytes = bos.toByteArray();
        if (bytes.length != this.size()) {
            throw new NulsRuntimeException(ErrorCode.FAILED, "序列化和size长度不一致:" + this.getClass());
        }
        return bytes;
    } finally {
        if (bos != null) {
            try {
                bos.close();
            } catch (IOException e) {
                throw e;
            }
        }
    }
}
Also used : NulsOutputStreamBuffer(io.nuls.core.utils.io.NulsOutputStreamBuffer) UnsafeByteArrayOutputStream(io.nuls.core.crypto.UnsafeByteArrayOutputStream) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) UnsafeByteArrayOutputStream(io.nuls.core.crypto.UnsafeByteArrayOutputStream) IOException(java.io.IOException)

Example 40 with NulsRuntimeException

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

the class Block method parse.

@Override
protected void parse(NulsByteBuffer byteBuffer) throws NulsException {
    header = new BlockHeader();
    header.parse(byteBuffer);
    try {
        txs = TransactionManager.getInstances(byteBuffer, header.getTxCount());
    } catch (Exception e) {
        throw new NulsRuntimeException(ErrorCode.PARSE_OBJECT_ERROR, e.getMessage());
    }
}
Also used : NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) NulsException(io.nuls.core.exception.NulsException) IOException(java.io.IOException) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException)

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