use of io.nuls.kernel.exception.NulsRuntimeException in project nuls by nuls-io.
the class ConsensusScheduler method initDatas.
private void initDatas() {
try {
ConsensusStatusContext.setConsensusStatus(ConsensusStatus.LOADING_CACHE);
cacheManager.load();
NulsContext.WALLET_STATUS = NulsConstant.SYNCHING;
ConsensusStatusContext.setConsensusStatus(ConsensusStatus.WAIT_RUNNING);
} catch (Exception e) {
throw new NulsRuntimeException(e);
}
}
use of io.nuls.kernel.exception.NulsRuntimeException in project nuls by nuls-io.
the class GenesisBlock method initGengsisTxs.
private void initGengsisTxs(Map<String, Object> jsonMap) throws Exception {
List<Map<String, Object>> list = (List<Map<String, Object>>) jsonMap.get(CONFIG_FILED_TXS);
if (null == list || list.isEmpty()) {
throw new NulsRuntimeException(KernelErrorCode.CONFIG_ERROR);
}
CoinData coinData = new CoinData();
for (Map<String, Object> map : list) {
String address = (String) map.get(CONFIG_FILED_ADDRESS);
AssertUtil.canNotEmpty(address, KernelErrorCode.NULL_PARAMETER.getMsg());
Double amount = Double.valueOf("" + map.get(CONFIG_FILED_AMOUNT));
AssertUtil.canNotEmpty(amount, KernelErrorCode.NULL_PARAMETER.getMsg());
Long lockTime = Long.valueOf("" + map.get(CONFIG_FILED_LOCK_TIME));
Address ads = Address.fromHashs(address);
Coin coin = new Coin(ads.getAddressBytes(), Na.parseNuls(amount), lockTime == null ? 0 : lockTime.longValue());
coinData.addTo(coin);
}
CoinBaseTransaction tx = new CoinBaseTransaction();
tx.setTime(this.blockTime);
tx.setCoinData(coinData);
String remark = (String) jsonMap.get(CONFIG_FILED_REMARK);
if (StringUtils.isNotBlank(remark)) {
tx.setRemark(Hex.decode(remark));
}
tx.setHash(NulsDigestData.calcDigestData(tx.serializeForHash()));
List<Transaction> txlist = new ArrayList<>();
txlist.add(tx);
setTxs(txlist);
}
use of io.nuls.kernel.exception.NulsRuntimeException in project nuls by nuls-io.
the class TransactionResource method getTxByHash.
@GET
@Path("/hash/{hash}")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "根据hash查询交易")
@ApiResponses(value = { @ApiResponse(code = 200, message = "success", response = TransactionDto.class) })
public RpcClientResult getTxByHash(@ApiParam(name = "hash", value = "交易hash", required = true) @PathParam("hash") String hash) {
if (StringUtils.isBlank(hash)) {
return Result.getFailed(LedgerErrorCode.NULL_PARAMETER).toRpcClientResult();
}
if (!NulsDigestData.validHash(hash)) {
return Result.getFailed(LedgerErrorCode.PARAMETER_ERROR).toRpcClientResult();
}
Result result = null;
try {
Transaction tx = ledgerService.getTx(NulsDigestData.fromDigestHex(hash));
if (tx == null) {
result = Result.getFailed(TransactionErrorCode.TX_NOT_EXIST);
} else {
tx.setStatus(TxStatusEnum.CONFIRMED);
TransactionDto txDto = null;
CoinData coinData = tx.getCoinData();
String fromAddress = null;
if (tx.getTransactionSignature() != null) {
TransactionSignature signature = new TransactionSignature();
signature.parse(new NulsByteBuffer(tx.getTransactionSignature()));
if (signature.getP2PHKSignatures() != null && signature.getP2PHKSignatures().size() == 1) {
byte[] addressBytes = AddressTool.getAddress(signature.getP2PHKSignatures().get(0).getPublicKey());
fromAddress = AddressTool.getStringAddressByBytes(addressBytes);
}
}
if (coinData != null) {
// 组装from数据
List<Coin> froms = coinData.getFrom();
if (froms != null && froms.size() > 0) {
byte[] fromHash, owner;
int fromIndex;
NulsDigestData fromHashObj;
Transaction fromTx;
Coin fromUtxo;
for (Coin from : froms) {
if (fromAddress != null) {
from.setFromAddress(fromAddress);
} else {
owner = from.getOwner();
// owner拆分出txHash和index
fromHash = LedgerUtil.getTxHashBytes(owner);
fromIndex = LedgerUtil.getIndex(owner);
// 查询from UTXO
fromHashObj = new NulsDigestData();
fromHashObj.parse(fromHash, 0);
fromTx = ledgerService.getTx(fromHashObj);
fromUtxo = fromTx.getCoinData().getTo().get(fromIndex);
from.setFrom(fromUtxo);
}
}
}
txDto = new TransactionDto(tx);
List<OutputDto> outputDtoList = new ArrayList<>();
// 组装to数据
List<Coin> tos = coinData.getTo();
if (tos != null && tos.size() > 0) {
byte[] txHashBytes = tx.getHash().serialize();
String txHash = hash;
OutputDto outputDto = null;
Coin to, temp;
long bestHeight = NulsContext.getInstance().getBestHeight();
long currentTime = TimeService.currentTimeMillis();
long lockTime;
for (int i = 0, length = tos.size(); i < length; i++) {
to = tos.get(i);
outputDto = new OutputDto(to);
outputDto.setTxHash(txHash);
outputDto.setIndex(i);
temp = utxoLedgerUtxoStorageService.getUtxo(Arrays.concatenate(txHashBytes, new VarInt(i).encode()));
if (temp == null) {
// 已花费
outputDto.setStatus(3);
} else {
lockTime = temp.getLockTime();
if (lockTime < 0) {
// 共识锁定
outputDto.setStatus(2);
} else if (lockTime == 0) {
// 正常未花费
outputDto.setStatus(0);
} else if (lockTime > NulsConstant.BlOCKHEIGHT_TIME_DIVIDE) {
// 判定是否时间高度锁定
if (lockTime > currentTime) {
// 时间高度锁定
outputDto.setStatus(1);
} else {
// 正常未花费
outputDto.setStatus(0);
}
} else {
// 判定是否区块高度锁定
if (lockTime > bestHeight) {
// 区块高度锁定
outputDto.setStatus(1);
} else {
// 正常未花费
outputDto.setStatus(0);
}
}
}
outputDtoList.add(outputDto);
}
}
txDto.setOutputs(outputDtoList);
// 计算交易实际发生的金额
calTransactionValue(txDto);
}
result = Result.getSuccess();
result.setData(txDto);
}
} catch (NulsRuntimeException e) {
Log.error(e);
result = Result.getFailed(e.getErrorCode());
} catch (Exception e) {
Log.error(e);
result = Result.getFailed(LedgerErrorCode.SYS_UNKOWN_EXCEPTION);
}
return result.toRpcClientResult();
}
use of io.nuls.kernel.exception.NulsRuntimeException in project nuls by nuls-io.
the class TransactionManager method getInstance.
public static Transaction getInstance(NulsByteBuffer byteBuffer) throws Exception {
int txType = byteBuffer.readUint16();
byteBuffer.setCursor(byteBuffer.getCursor() - SerializeUtils.sizeOfUint16());
Class<? extends Transaction> txClass = TYPE_TX_MAP.get(txType);
if (null == txClass) {
throw new NulsRuntimeException(KernelErrorCode.DATA_NOT_FOUND);
}
Transaction tx = byteBuffer.readNulsData(txClass.newInstance());
return tx;
}
use of io.nuls.kernel.exception.NulsRuntimeException in project nuls by nuls-io.
the class ValidatorManager method init.
public static void init() {
List<NulsDataValidator> validatorList = null;
try {
validatorList = SpringLiteContext.getBeanList(NulsDataValidator.class);
} catch (Exception e) {
throw new NulsRuntimeException(e);
}
for (NulsDataValidator validator : validatorList) {
Method[] methods = validator.getClass().getDeclaredMethods();
for (Method method : methods) {
if ("validate".equals(method.getName())) {
Class paramType = method.getParameterTypes()[0];
if (paramType.equals(NulsData.class)) {
continue;
}
addValidator(paramType, validator);
break;
}
}
}
success = true;
}
Aggregations