Search in sources :

Example 56 with NulsRuntimeException

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

the class RpcServerFilter method toResponse.

@Override
public Response toResponse(Exception e) {
    // System.out.println("---------------" + request.getRequestURI());
    Log.error("RequestURI is " + request.getRequestURI(), e);
    RpcClientResult result;
    if (e instanceof NulsException) {
        NulsException exception = (NulsException) e;
        result = new RpcClientResult(false, exception.getErrorCode());
    } else if (e instanceof NulsRuntimeException) {
        NulsRuntimeException exception = (NulsRuntimeException) e;
        result = new RpcClientResult(false, new ErrorData(exception.getCode(), exception.getMessage()));
    } else {
        result = Result.getFailed().setMsg(e.getMessage()).toRpcClientResult();
    }
    return Response.ok(result, MediaType.APPLICATION_JSON).build();
}
Also used : RpcClientResult(io.nuls.kernel.model.RpcClientResult) NulsException(io.nuls.kernel.exception.NulsException) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) ErrorData(io.nuls.kernel.model.ErrorData)

Example 57 with NulsRuntimeException

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

the class GenesisBlock method fillHeader.

private void fillHeader(Map<String, Object> jsonMap) throws NulsException {
    Integer height = (Integer) jsonMap.get(CONFIG_FILED_HEIGHT);
    AssertUtil.canNotEmpty(height, KernelErrorCode.CONFIG_ERROR.getMsg());
    BlockHeader header = new BlockHeader();
    this.setHeader(header);
    header.setHeight(height);
    header.setTime(blockTime);
    header.setPreHash(NulsDigestData.calcDigestData(new byte[35]));
    header.setTxCount(this.getTxs().size());
    List<NulsDigestData> txHashList = new ArrayList<>();
    for (Transaction tx : this.getTxs()) {
        txHashList.add(tx.getHash());
    }
    header.setMerkleHash(NulsDigestData.calcMerkleDigestData(txHashList));
    BlockExtendsData data = new BlockExtendsData();
    data.setRoundIndex(1);
    data.setRoundStartTime(header.getTime() - ProtocolConstant.BLOCK_TIME_INTERVAL_SECOND * 1000);
    data.setConsensusMemberCount(1);
    data.setPackingIndexOfRound(1);
    try {
        header.setExtend(data.serialize());
    } catch (IOException e) {
        throw new NulsRuntimeException(e);
    }
    header.setHash(NulsDigestData.calcDigestData(header));
    BlockSignature p2PKHScriptSig = new BlockSignature();
    NulsSignData signData = this.signature(header.getHash().getDigestBytes());
    p2PKHScriptSig.setSignData(signData);
    p2PKHScriptSig.setPublicKey(getGenesisPubkey());
    header.setBlockSignature(p2PKHScriptSig);
}
Also used : BigInteger(java.math.BigInteger) CoinBaseTransaction(io.nuls.protocol.model.tx.CoinBaseTransaction) BlockExtendsData(io.nuls.consensus.poc.model.BlockExtendsData) BlockSignature(io.nuls.kernel.script.BlockSignature) ArrayList(java.util.ArrayList) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) IOException(java.io.IOException)

Example 58 with NulsRuntimeException

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

the class GenesisBlock method init.

private synchronized void init(String json) throws Exception {
    if (status > 0) {
        return;
    }
    Map<String, Object> jsonMap = null;
    try {
        jsonMap = JSONUtils.json2map(json);
    } catch (Exception e) {
        Log.error(e);
    }
    String time = (String) jsonMap.get(CONFIG_FILED_TIME);
    AssertUtil.canNotEmpty(time, KernelErrorCode.CONFIG_ERROR.getMsg());
    blockTime = Long.parseLong(time);
    this.initGengsisTxs(jsonMap);
    this.fillHeader(jsonMap);
    ValidateResult validateResult = this.verify();
    if (validateResult.isFailed()) {
        throw new NulsRuntimeException(validateResult.getErrorCode());
    }
    this.status = 1;
}
Also used : ValidateResult(io.nuls.kernel.validate.ValidateResult) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) IOException(java.io.IOException) NulsException(io.nuls.kernel.exception.NulsException)

Example 59 with NulsRuntimeException

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

the class CacheManager method load.

public void load() throws NulsException {
    // load storage data to memory
    List<BlockHeader> blockHeaderList = cacheLoader.loadBlockHeaders(PocConsensusConstant.INIT_HEADERS_OF_ROUND_COUNT);
    List<Block> blockList = cacheLoader.loadBlocks(PocConsensusConstant.INIT_BLOCKS_COUNT);
    if (blockHeaderList == null || blockHeaderList.size() == 0 || blockList == null || blockList.size() == 0) {
        Log.error("load cache error ,not find the block info!");
        throw new NulsRuntimeException(KernelErrorCode.DATA_ERROR);
    }
    List<Agent> agentList = cacheLoader.loadAgents();
    List<Deposit> depositList = cacheLoader.loadDepositList();
    List<PunishLogPo> allPunishList = NulsContext.getServiceBean(PunishLogStorageService.class).getPunishList();
    List<PunishLogPo> yellowPunishList = cacheLoader.loadYellowPunishList(allPunishList, PocConsensusConstant.INIT_HEADERS_OF_ROUND_COUNT);
    List<PunishLogPo> redPunishList = cacheLoader.loadRedPunishList(allPunishList);
    Chain masterChain = new Chain();
    masterChain.initData(blockList.get(0).getHeader(), blockHeaderList, blockList);
    masterChain.setAgentList(agentList);
    masterChain.setDepositList(depositList);
    masterChain.setYellowPunishList(yellowPunishList);
    masterChain.setRedPunishList(redPunishList);
    ChainContainer masterChainContainer = new ChainContainer(masterChain);
    chainManager.setMasterChain(masterChainContainer);
    chainManager.getMasterChain().initRound();
}
Also used : Agent(io.nuls.consensus.poc.protocol.entity.Agent) Deposit(io.nuls.consensus.poc.protocol.entity.Deposit) Chain(io.nuls.consensus.poc.model.Chain) ChainContainer(io.nuls.consensus.poc.container.ChainContainer) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) Block(io.nuls.kernel.model.Block) PunishLogStorageService(io.nuls.consensus.poc.storage.service.PunishLogStorageService) BlockHeader(io.nuls.kernel.model.BlockHeader) PunishLogPo(io.nuls.consensus.poc.storage.po.PunishLogPo)

Example 60 with NulsRuntimeException

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

the class CreateContractTxProcessor method onCommit.

@Override
public Result onCommit(CreateContractTransaction tx, Object secondaryData) {
    ContractResult contractResult = tx.getContractResult();
    contractService.saveContractExecuteResult(tx.getHash(), contractResult);
    CreateContractData txData = tx.getTxData();
    byte[] contractAddress = txData.getContractAddress();
    byte[] sender = txData.getSender();
    String senderStr = AddressTool.getStringAddressByBytes(sender);
    String contractAddressStr = AddressTool.getStringAddressByBytes(contractAddress);
    // 移除未确认的创建合约交易
    contractTxService.removeLocalUnconfirmedCreateContractTransaction(senderStr, contractAddressStr, contractResult);
    // 执行失败的合约直接返回
    if (!contractResult.isSuccess()) {
        return Result.getSuccess();
    }
    NulsDigestData hash = tx.getHash();
    long blockHeight = tx.getBlockHeight();
    long bestBlockHeight = NulsContext.getInstance().getBestHeight();
    ContractAddressInfoPo info = new ContractAddressInfoPo();
    info.setContractAddress(contractAddress);
    info.setSender(sender);
    try {
        info.setCreateTxHash(hash.serialize());
    } catch (IOException e) {
        throw new NulsRuntimeException(e);
    }
    info.setCreateTime(tx.getTime());
    info.setBlockHeight(blockHeight);
    // byte[] stateRoot = contractResult.getStateRoot();
    boolean isNrc20Contract = contractResult.isNrc20();
    boolean acceptDirectTransfer = contractResult.isAcceptDirectTransfer();
    info.setAcceptDirectTransfer(acceptDirectTransfer);
    info.setNrc20(isNrc20Contract);
    // 获取 token tracker
    if (isNrc20Contract) {
        BlockHeader blockHeader = tx.getBlockHeader();
        byte[] newestStateRoot = blockHeader.getStateRoot();
        // NRC20 token 标准方法获取名称数据
        ProgramResult programResult = vmHelper.invokeViewMethod(newestStateRoot, bestBlockHeight, contractAddress, NRC20_METHOD_NAME, null, null);
        if (programResult.isSuccess()) {
            String tokenName = programResult.getResult();
            info.setNrc20TokenName(tokenName);
        }
        programResult = vmHelper.invokeViewMethod(newestStateRoot, bestBlockHeight, contractAddress, NRC20_METHOD_SYMBOL, null, null);
        if (programResult.isSuccess()) {
            String symbol = programResult.getResult();
            info.setNrc20TokenSymbol(symbol);
        }
        programResult = vmHelper.invokeViewMethod(newestStateRoot, bestBlockHeight, contractAddress, NRC20_METHOD_DECIMALS, null, null);
        if (programResult.isSuccess()) {
            String decimals = programResult.getResult();
            if (StringUtils.isNotBlank(decimals)) {
                try {
                    info.setDecimals(new BigInteger(decimals).longValue());
                } catch (Exception e) {
                    Log.error("Get nrc20 decimals error.", e);
                // skip it
                }
            }
        }
        programResult = vmHelper.invokeViewMethod(newestStateRoot, bestBlockHeight, contractAddress, NRC20_METHOD_TOTAL_SUPPLY, null, null);
        if (programResult.isSuccess()) {
            String totalSupply = programResult.getResult();
            if (StringUtils.isNotBlank(totalSupply)) {
                try {
                    info.setTotalSupply(new BigInteger(totalSupply));
                } catch (Exception e) {
                    Log.error("Get nrc20 totalSupply error.", e);
                // skip it
                }
            }
        }
        // 刷新创建者的token余额
        vmHelper.refreshTokenBalance(newestStateRoot, info, senderStr, contractAddressStr);
        // 处理合约事件
        vmHelper.dealEvents(newestStateRoot, tx, contractResult, info);
    }
    Result result = contractAddressStorageService.saveContractAddress(contractAddress, info);
    return result;
}
Also used : ContractResult(io.nuls.contract.dto.ContractResult) ProgramResult(io.nuls.contract.vm.program.ProgramResult) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) IOException(java.io.IOException) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) IOException(java.io.IOException) NulsException(io.nuls.kernel.exception.NulsException) ContractResult(io.nuls.contract.dto.ContractResult) ValidateResult(io.nuls.kernel.validate.ValidateResult) ProgramResult(io.nuls.contract.vm.program.ProgramResult) Result(io.nuls.kernel.model.Result) ContractAddressInfoPo(io.nuls.contract.storage.po.ContractAddressInfoPo) CreateContractData(io.nuls.contract.entity.txdata.CreateContractData) NulsDigestData(io.nuls.kernel.model.NulsDigestData) BigInteger(java.math.BigInteger) BlockHeader(io.nuls.kernel.model.BlockHeader)

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