Search in sources :

Example 6 with NulsRuntimeException

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);
    }
}
Also used : NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException)

Example 7 with NulsRuntimeException

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);
}
Also used : ArrayList(java.util.ArrayList) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) CoinBaseTransaction(io.nuls.protocol.model.tx.CoinBaseTransaction) CoinBaseTransaction(io.nuls.protocol.model.tx.CoinBaseTransaction) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Example 8 with NulsRuntimeException

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();
}
Also used : OutputDto(io.nuls.ledger.rpc.model.OutputDto) TransactionDto(io.nuls.ledger.rpc.model.TransactionDto) VarInt(io.nuls.kernel.utils.VarInt) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) TransactionSignature(io.nuls.kernel.script.TransactionSignature) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) IOException(java.io.IOException) NulsException(io.nuls.kernel.exception.NulsException) NulsByteBuffer(io.nuls.kernel.utils.NulsByteBuffer)

Example 9 with NulsRuntimeException

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;
}
Also used : Transaction(io.nuls.kernel.model.Transaction) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException)

Example 10 with NulsRuntimeException

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;
}
Also used : NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) Method(java.lang.reflect.Method) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException)

Aggregations

NulsRuntimeException (io.nuls.kernel.exception.NulsRuntimeException)68 IOException (java.io.IOException)35 NulsException (io.nuls.kernel.exception.NulsException)26 ArrayList (java.util.ArrayList)21 CoinDataResult (io.nuls.account.ledger.model.CoinDataResult)10 Result (io.nuls.kernel.model.Result)9 Account (io.nuls.account.model.Account)8 MultiSigAccount (io.nuls.account.model.MultiSigAccount)8 Entry (io.nuls.db.model.Entry)8 Agent (io.nuls.consensus.poc.protocol.entity.Agent)7 VarInt (io.nuls.kernel.utils.VarInt)7 CreateAgentTransaction (io.nuls.consensus.poc.protocol.tx.CreateAgentTransaction)6 ValidateResult (io.nuls.kernel.validate.ValidateResult)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 Deposit (io.nuls.consensus.poc.protocol.entity.Deposit)5 DepositTransaction (io.nuls.consensus.poc.protocol.tx.DepositTransaction)5 DepositPo (io.nuls.consensus.poc.storage.po.DepositPo)5 PunishLogPo (io.nuls.consensus.poc.storage.po.PunishLogPo)5 TransferTransaction (io.nuls.protocol.model.tx.TransferTransaction)5 StopAgent (io.nuls.consensus.poc.protocol.entity.StopAgent)4