use of io.nuls.kernel.exception.NulsRuntimeException in project nuls by nuls-io.
the class PocConsensusResource method createMutilAgent.
@POST
@Path("/multiAccount/createMultiAgent")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Create an agent for consensus! 创建共识(代理)节点 [3.6.3]", notes = "返回创建的节点成功的交易hash")
@ApiResponses(value = { @ApiResponse(code = 200, message = "success", response = String.class) })
public RpcClientResult createMutilAgent(@ApiParam(name = "form", value = "多签地址创建节点表单数据", required = true) CreateMultiAgentForm form) throws Exception {
if (NulsContext.MAIN_NET_VERSION <= 1) {
return Result.getFailed(KernelErrorCode.VERSION_TOO_LOW).toRpcClientResult();
}
AssertUtil.canNotEmpty(form);
AssertUtil.canNotEmpty(form.getAgentAddress(), "agent address can not be null");
AssertUtil.canNotEmpty(form.getSignAddress(), "agent address can not be null");
AssertUtil.canNotEmpty(form.getCommissionRate(), "commission rate can not be null");
AssertUtil.canNotEmpty(form.getDeposit(), "deposit can not be null");
AssertUtil.canNotEmpty(form.getPackingAddress(), "packing address can not be null");
if (!AddressTool.isPackingAddress(form.getPackingAddress()) || !AddressTool.validAddress(form.getAgentAddress()) || !AddressTool.validAddress(form.getSignAddress())) {
throw new NulsRuntimeException(AccountErrorCode.ADDRESS_ERROR);
}
Account account = accountService.getAccount(form.getSignAddress()).getData();
if (null == account) {
return Result.getFailed(AccountErrorCode.ACCOUNT_NOT_EXIST).toRpcClientResult();
}
if (account.isEncrypted() && account.isLocked()) {
AssertUtil.canNotEmpty(form.getPassword(), "password is wrong");
if (!account.validatePassword(form.getPassword())) {
return Result.getFailed(AccountErrorCode.PASSWORD_IS_WRONG).toRpcClientResult();
}
}
Result<MultiSigAccount> sigAccountResult = accountService.getMultiSigAccount(form.getAgentAddress());
MultiSigAccount multiSigAccount = sigAccountResult.getData();
// 验证签名账户是否属于多签账户,如果不是多签账户下的地址则提示错误
if (!AddressTool.validSignAddress(multiSigAccount.getPubKeyList(), account.getPubKey())) {
return Result.getFailed(AccountErrorCode.SIGN_ADDRESS_NOT_MATCH).toRpcClientResult();
}
Script redeemScript = accountLedgerService.getRedeemScript(multiSigAccount);
if (redeemScript == null) {
return Result.getFailed(AccountErrorCode.ACCOUNT_NOT_EXIST).toRpcClientResult();
}
CreateAgentTransaction tx = new CreateAgentTransaction();
tx.setTime(TimeService.currentTimeMillis());
Agent agent = new Agent();
agent.setAgentAddress(AddressTool.getAddress(form.getAgentAddress()));
agent.setPackingAddress(AddressTool.getAddress(form.getPackingAddress()));
if (StringUtils.isBlank(form.getRewardAddress())) {
agent.setRewardAddress(agent.getAgentAddress());
} else {
agent.setRewardAddress(AddressTool.getAddress(form.getRewardAddress()));
}
TransactionSignature transactionSignature = new TransactionSignature();
List<Script> scripts = new ArrayList<>();
agent.setDeposit(Na.valueOf(form.getDeposit()));
agent.setCommissionRate(form.getCommissionRate());
tx.setTxData(agent);
CoinData coinData = new CoinData();
List<Coin> toList = new ArrayList<>();
if (agent.getAgentAddress()[2] == NulsContext.P2SH_ADDRESS_TYPE) {
Script scriptPubkey = SignatureUtil.createOutputScript(agent.getAgentAddress());
toList.add(new Coin(scriptPubkey.getProgram(), agent.getDeposit(), PocConsensusConstant.CONSENSUS_LOCK_TIME));
} else {
toList.add(new Coin(agent.getAgentAddress(), agent.getDeposit(), PocConsensusConstant.CONSENSUS_LOCK_TIME));
}
coinData.setTo(toList);
tx.setCoinData(coinData);
// 交易签名的长度为m*单个签名长度+赎回脚本长度
int scriptSignLenth = redeemScript.getProgram().length + ((int) multiSigAccount.getM()) * 72;
CoinDataResult result = accountLedgerService.getMutilCoinData(agent.getAgentAddress(), agent.getDeposit(), tx.size() + scriptSignLenth, TransactionFeeCalculator.OTHER_PRICE_PRE_1024_BYTES);
if (null != result) {
if (result.isEnough()) {
tx.getCoinData().setFrom(result.getCoinList());
if (null != result.getChange()) {
tx.getCoinData().getTo().add(result.getChange());
}
} else {
return Result.getFailed(TransactionErrorCode.INSUFFICIENT_BALANCE).toRpcClientResult();
}
}
tx.setHash(NulsDigestData.calcDigestData(tx.serializeForHash()));
// 将赎回脚本先存储在签名脚本中
scripts.add(redeemScript);
transactionSignature.setScripts(scripts);
Result finalResult = accountLedgerService.txMultiProcess(tx, transactionSignature, account, form.getPassword());
if (finalResult.isSuccess()) {
Map<String, String> valueMap = new HashMap<>();
valueMap.put("txData", (String) finalResult.getData());
return Result.getSuccess().setData(valueMap).toRpcClientResult();
}
return finalResult.toRpcClientResult();
}
use of io.nuls.kernel.exception.NulsRuntimeException in project nuls by nuls-io.
the class PocConsensusResource method depositToAgent.
@POST
@Path("/deposit")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "deposit nuls to a bank! 申请参与共识 ", notes = "返回申请成功交易hash")
@ApiResponses(value = { @ApiResponse(code = 200, message = "success", response = String.class) })
public RpcClientResult depositToAgent(@ApiParam(name = "form", value = "申请参与共识表单数据", required = true) DepositForm form) throws NulsException {
AssertUtil.canNotEmpty(form);
AssertUtil.canNotEmpty(form.getAddress());
AssertUtil.canNotEmpty(form.getAgentHash());
if (!NulsDigestData.validHash(form.getAgentHash())) {
return Result.getFailed(PocConsensusErrorCode.AGENT_NOT_EXIST).toRpcClientResult();
}
AssertUtil.canNotEmpty(form.getDeposit());
if (!AddressTool.validAddress(form.getAddress())) {
throw new NulsRuntimeException(KernelErrorCode.PARAMETER_ERROR);
}
Account account = accountService.getAccount(form.getAddress()).getData();
if (null == account) {
return Result.getFailed(AccountErrorCode.ACCOUNT_NOT_EXIST).toRpcClientResult();
}
if (account.isEncrypted() && account.isLocked()) {
AssertUtil.canNotEmpty(form.getPassword(), "password is wrong");
if (!account.validatePassword(form.getPassword())) {
return Result.getFailed(AccountErrorCode.PASSWORD_IS_WRONG).toRpcClientResult();
}
}
if (!account.isOk()) {
return Result.getFailed(AccountErrorCode.IMPORTING_ACCOUNT).toRpcClientResult();
}
DepositTransaction tx = new DepositTransaction();
Deposit deposit = new Deposit();
deposit.setAddress(AddressTool.getAddress(form.getAddress()));
deposit.setAgentHash(NulsDigestData.fromDigestHex(form.getAgentHash()));
deposit.setDeposit(Na.valueOf(form.getDeposit()));
tx.setTxData(deposit);
CoinData coinData = new CoinData();
List<Coin> toList = new ArrayList<>();
toList.add(new Coin(deposit.getAddress(), deposit.getDeposit(), PocConsensusConstant.CONSENSUS_LOCK_TIME));
coinData.setTo(toList);
tx.setCoinData(coinData);
CoinDataResult result = accountLedgerService.getCoinData(deposit.getAddress(), deposit.getDeposit(), tx.size(), TransactionFeeCalculator.OTHER_PRICE_PRE_1024_BYTES);
RpcClientResult result1 = this.txProcessing(tx, result, account, form.getPassword());
if (!result1.isSuccess()) {
return result1;
}
Map<String, String> valueMap = new HashMap<>();
valueMap.put("value", tx.getHash().getDigestHex());
return Result.getSuccess().setData(valueMap).toRpcClientResult();
}
use of io.nuls.kernel.exception.NulsRuntimeException in project nuls by nuls-io.
the class AgentStorageServiceImpl method save.
@Override
public boolean save(AgentPo agentPo) {
if (agentPo == null || agentPo.getHash() == null) {
return false;
}
byte[] hash;
try {
hash = agentPo.getHash().serialize();
} catch (IOException e) {
Log.error(e);
throw new NulsRuntimeException(e);
}
Result result = null;
try {
result = dbService.put(ConsensusStorageConstant.DB_NAME_CONSENSUS_AGENT, hash, agentPo.serialize());
} catch (IOException e) {
Log.error(e);
return false;
}
return result.isSuccess();
}
use of io.nuls.kernel.exception.NulsRuntimeException in project nuls by nuls-io.
the class AgentStorageServiceImpl method getList.
@Override
public List<AgentPo> getList() {
List<Entry<byte[], byte[]>> list = dbService.entryList(ConsensusStorageConstant.DB_NAME_CONSENSUS_AGENT);
List<AgentPo> resultList = new ArrayList<>();
if (list == null) {
return resultList;
}
for (Entry<byte[], byte[]> entry : list) {
AgentPo agentPo = new AgentPo();
try {
agentPo.parse(entry.getValue(), 0);
} catch (NulsException e) {
Log.error(e);
throw new NulsRuntimeException(e);
}
NulsDigestData hash = new NulsDigestData();
try {
hash.parse(entry.getKey(), 0);
} catch (NulsException e) {
Log.error(e);
}
agentPo.setHash(hash);
resultList.add(agentPo);
}
return resultList;
}
use of io.nuls.kernel.exception.NulsRuntimeException in project nuls by nuls-io.
the class DepositStorageServiceImpl method get.
@Override
public DepositPo get(NulsDigestData hash) {
if (hash == null) {
return null;
}
byte[] body = null;
try {
body = dbService.get(ConsensusStorageConstant.DB_NAME_CONSENSUS_DEPOSIT, hash.serialize());
} catch (IOException e) {
Log.error(e);
}
if (body == null) {
return null;
}
DepositPo depositPo = new DepositPo();
try {
depositPo.parse(body, 0);
} catch (NulsException e) {
Log.error(e);
throw new NulsRuntimeException(e);
}
depositPo.setTxHash(hash);
return depositPo;
}
Aggregations