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);
}
}
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;
}
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);
}
}
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;
}
}
}
}
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());
}
}
Aggregations