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