Search in sources :

Example 41 with NulsRuntimeException

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

the class MicroKernelBootstrap method init.

@Override
public void init() {
    try {
        NulsContext.NULS_CONFIG = ConfigLoader.loadIni(NulsConstant.USER_CONFIG_FILE);
        NulsContext.MODULES_CONFIG = ConfigLoader.loadIni(NulsConstant.MODULES_CONFIG_FILE);
    } catch (IOException e) {
        Log.error("Client start failed", e);
        throw new NulsRuntimeException(ErrorCode.FAILED, "Client start failed");
    }
    // set system language
    try {
        NulsContext.DEFAULT_ENCODING = NulsContext.NULS_CONFIG.getCfgValue(NulsConstant.CFG_SYSTEM_SECTION, NulsConstant.CFG_SYSTEM_DEFAULT_ENCODING);
        String language = NulsContext.NULS_CONFIG.getCfgValue(NulsConstant.CFG_SYSTEM_SECTION, NulsConstant.CFG_SYSTEM_LANGUAGE);
        I18nUtils.setLanguage(language);
    } catch (NulsException e) {
        Log.error(e);
    }
    SpringLiteContext.init("io.nuls", new ModularServiceMethodInterceptor());
    try {
        VersionManager.start();
    } catch (NulsException e) {
        Log.error(e);
    }
}
Also used : ModularServiceMethodInterceptor(io.nuls.core.utils.spring.lite.core.ModularServiceMethodInterceptor) NulsException(io.nuls.core.exception.NulsException) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) IOException(java.io.IOException)

Example 42 with NulsRuntimeException

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

the class TransactionManager method getInstance.

public static Transaction getInstance(NulsByteBuffer byteBuffer) throws Exception {
    int txType = (int) new NulsByteBuffer(byteBuffer.getPayloadByCursor()).readVarInt();
    Class<? extends Transaction> txClass = getTxClass(txType);
    if (null == txClass) {
        throw new NulsRuntimeException(ErrorCode.FAILED, "transaction type not exist!");
    }
    Transaction tx = byteBuffer.readNulsData(txClass.getConstructor().newInstance());
    return tx;
}
Also used : Transaction(io.nuls.core.chain.entity.Transaction) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) NulsByteBuffer(io.nuls.core.utils.io.NulsByteBuffer)

Example 43 with NulsRuntimeException

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

the class PocConsensusResource method createAgent.

@POST
@Path("/agent")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation("Create an agent for consensus!")
public RpcResult createAgent(CreateAgentForm form) throws NulsException {
    AssertUtil.canNotEmpty(form);
    AssertUtil.canNotEmpty(form.getAgentAddress());
    AssertUtil.canNotEmpty(form.getAgentName());
    AssertUtil.canNotEmpty(form.getPackingAddress());
    AssertUtil.canNotEmpty(form.getDeposit());
    AssertUtil.canNotEmpty(form.getRemark());
    AssertUtil.canNotEmpty(form.getPassword());
    if (!Address.validAddress(form.getPackingAddress()) || !Address.validAddress(form.getAgentAddress())) {
        throw new NulsRuntimeException(ErrorCode.PARAMETER_ERROR);
    }
    Map<String, Object> paramsMap = new HashMap<>();
    paramsMap.put("deposit", form.getDeposit());
    paramsMap.put("packingAddress", form.getPackingAddress());
    paramsMap.put("introduction", form.getRemark());
    paramsMap.put("commissionRate", form.getCommissionRate());
    paramsMap.put("agentName", form.getAgentName());
    Transaction tx = consensusService.startConsensus(form.getAgentAddress(), form.getPassword(), paramsMap);
    return RpcResult.getSuccess().setData(tx.getHash().getDigestHex());
}
Also used : Transaction(io.nuls.core.chain.entity.Transaction) HashMap(java.util.HashMap) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) ApiOperation(io.swagger.annotations.ApiOperation)

Example 44 with NulsRuntimeException

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

the class TransactionResource method load.

@GET
@Path("/hash/{hash}")
@Produces(MediaType.APPLICATION_JSON)
public RpcResult load(@PathParam("hash") String hash) {
    RpcResult result = null;
    if (StringUtils.isBlank(hash)) {
        return RpcResult.getFailed(ErrorCode.NULL_PARAMETER);
    }
    try {
        Transaction tx = ledgerService.getTx(NulsDigestData.fromDigestHex(hash));
        if (tx == null) {
            result = RpcResult.getFailed("not found");
        } else {
            result = RpcResult.getSuccess();
            result.setData(new TransactionDto(tx));
        }
    } catch (NulsRuntimeException re) {
        Log.error(re);
        result = new RpcResult(false, re.getCode(), re.getMessage());
    } catch (Exception e) {
        Log.error(e);
        result = RpcResult.getFailed(ErrorCode.SYS_UNKOWN_EXCEPTION);
    }
    return result;
}
Also used : TransactionDto(io.nuls.rpc.entity.TransactionDto) Transaction(io.nuls.core.chain.entity.Transaction) RpcResult(io.nuls.rpc.entity.RpcResult) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException)

Example 45 with NulsRuntimeException

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

the class GenesisBlock method initGengsisTxs.

private void initGengsisTxs(Map<String, Object> jsonMap) {
    List<Map<String, Object>> list = (List<Map<String, Object>>) jsonMap.get(CONFIG_FILED_TXS);
    if (null == list || list.isEmpty()) {
        throw new NulsRuntimeException(ErrorCode.CONFIG_ERROR);
    }
    CoinTransferData data = new CoinTransferData(OperationType.COIN_BASE, Na.ZERO);
    data.setPriKey(Hex.decode(priKey));
    Na total = Na.ZERO;
    for (Map<String, Object> map : list) {
        String address = (String) map.get(CONFIG_FILED_ADDRESS);
        AssertUtil.canNotEmpty(address, ErrorCode.NULL_PARAMETER);
        Integer nuls = (Integer) map.get(CONFIG_FILED_NULS);
        AssertUtil.canNotEmpty(nuls, ErrorCode.NULL_PARAMETER);
        Integer height = (Integer) map.get(CONFIG_FILED_UNLOCK_HEIGHT);
        Coin coin = new Coin();
        coin.setNa(Na.parseNuls(nuls));
        coin.setUnlockTime(0);
        if (height == null) {
            coin.setUnlockTime(0);
        } else {
            coin.setUnlockHeight(height.longValue());
        }
        data.addTo(address, coin);
        total = total.add(coin.getNa());
    }
    data.setTotalNa(total);
    CoinBaseTransaction tx = null;
    try {
        tx = new CoinBaseTransaction(data, null);
    } catch (NulsException e) {
        Log.error(e);
        throw new NulsRuntimeException(e);
    }
    tx.setTime(this.blockTime);
    tx.setFee(Na.ZERO);
    try {
        tx.setHash(NulsDigestData.calcDigestData(tx.serialize()));
    } catch (IOException e) {
        Log.error(e);
        throw new NulsRuntimeException(e);
    }
    P2PKHScriptSig p2PKHScriptSig = new P2PKHScriptSig();
    Account account = null;
    try {
        account = AccountTool.createAccount(priKey);
    } catch (NulsException e) {
        e.printStackTrace();
    }
    AccountService accountService = NulsContext.getServiceBean(AccountService.class);
    P2PKHScriptSig scriptSig = null;
    try {
        scriptSig = accountService.createP2PKHScriptSigFromDigest(tx.getHash(), account, "");
    } catch (NulsException e) {
        e.printStackTrace();
    }
    try {
        tx.setScriptSig(scriptSig.serialize());
    } catch (IOException e) {
        e.printStackTrace();
    }
    List<Transaction> txlist = new ArrayList<>();
    // tx.setStatus(TxStatusEnum.AGREED);
    txlist.add(tx);
    setTxs(txlist);
}
Also used : Account(io.nuls.account.entity.Account) ArrayList(java.util.ArrayList) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) IOException(java.io.IOException) BigInteger(java.math.BigInteger) Coin(io.nuls.ledger.entity.params.Coin) P2PKHScriptSig(io.nuls.core.script.P2PKHScriptSig) CoinTransferData(io.nuls.ledger.entity.params.CoinTransferData) CoinBaseTransaction(io.nuls.ledger.entity.tx.CoinBaseTransaction) CoinBaseTransaction(io.nuls.ledger.entity.tx.CoinBaseTransaction) NulsException(io.nuls.core.exception.NulsException) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) AccountService(io.nuls.account.service.intf.AccountService)

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