use of io.nuls.account.model.Account in project nuls by nuls-io.
the class PocConsensusResource method getStopAgentFee.
@GET
@Path("/agent/stop/fee")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "get the fee of stop agent! 获取停止节点的手续费", notes = "返回停止节点交易手续费")
@ApiResponses(value = { @ApiResponse(code = 200, message = "success", response = String.class) })
public RpcClientResult getStopAgentFee(@ApiParam(name = "address", value = "创建节点的账户地址", required = true) @QueryParam("address") String address) throws NulsException, IOException {
AssertUtil.canNotEmpty(address, "address can not be null");
if (!AddressTool.validAddress(address)) {
return Result.getFailed(KernelErrorCode.PARAMETER_ERROR).toRpcClientResult();
}
Account account = accountService.getAccount(address).getData();
if (null == account) {
return Result.getFailed(AccountErrorCode.ACCOUNT_NOT_EXIST).toRpcClientResult();
}
if (!account.isOk()) {
return Result.getFailed(AccountErrorCode.IMPORTING_ACCOUNT).toRpcClientResult();
}
StopAgentTransaction tx = new StopAgentTransaction();
StopAgent stopAgent = new StopAgent();
stopAgent.setAddress(AddressTool.getAddress(address));
List<Agent> agentList = PocConsensusContext.getChainManager().getMasterChain().getChain().getAgentList();
Agent agent = null;
for (Agent a : agentList) {
if (a.getDelHeight() > 0) {
continue;
}
if (Arrays.equals(a.getAgentAddress(), stopAgent.getAddress())) {
agent = a;
break;
}
}
if (agent == null || agent.getDelHeight() > 0) {
return Result.getFailed(PocConsensusErrorCode.AGENT_NOT_EXIST).toRpcClientResult();
}
NulsDigestData createTxHash = agent.getTxHash();
stopAgent.setCreateTxHash(createTxHash);
tx.setTxData(stopAgent);
CoinData coinData = ConsensusTool.getStopAgentCoinData(agent, TimeService.currentTimeMillis() + PocConsensusConstant.STOP_AGENT_LOCK_TIME);
tx.setCoinData(coinData);
Na fee = TransactionFeeCalculator.getMaxFee(108 + tx.size());
coinData.getTo().get(0).setNa(coinData.getTo().get(0).getNa().subtract(fee));
Na resultFee = TransactionFeeCalculator.getMaxFee(108 + tx.size());
Map<String, Long> map = new HashMap<>();
map.put("fee", fee.getValue());
map.put("maxAmount", getMaxAmount(resultFee, address, tx));
return Result.getSuccess().setData(map).toRpcClientResult();
}
use of io.nuls.account.model.Account 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.account.model.Account in project nuls by nuls-io.
the class PocConsensusResource method withdraw.
@POST
@Path("/withdraw")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "退出共识 [3.6.11]", notes = "返回退出成功的交易hash")
@ApiResponses(value = { @ApiResponse(code = 200, message = "success", response = String.class) })
public RpcClientResult withdraw(@ApiParam(name = "form", value = "退出共识表单数据", required = true) WithdrawForm form) throws NulsException, IOException {
AssertUtil.canNotEmpty(form);
AssertUtil.canNotEmpty(form.getTxHash());
if (!NulsDigestData.validHash(form.getTxHash())) {
return Result.getFailed(KernelErrorCode.PARAMETER_ERROR).toRpcClientResult();
}
AssertUtil.canNotEmpty(form.getAddress());
if (!AddressTool.validAddress(form.getAddress())) {
return Result.getFailed(AccountErrorCode.ADDRESS_ERROR).toRpcClientResult();
}
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();
}
}
CancelDepositTransaction tx = new CancelDepositTransaction();
CancelDeposit cancelDeposit = new CancelDeposit();
NulsDigestData hash = NulsDigestData.fromDigestHex(form.getTxHash());
DepositTransaction depositTransaction = null;
try {
depositTransaction = (DepositTransaction) ledgerService.getTx(hash);
} catch (Exception e) {
return Result.getFailed(KernelErrorCode.PARAMETER_ERROR).toRpcClientResult();
}
if (null == depositTransaction) {
return Result.getFailed(TransactionErrorCode.TX_NOT_EXIST).toRpcClientResult();
}
cancelDeposit.setAddress(AddressTool.getAddress(form.getAddress()));
cancelDeposit.setJoinTxHash(hash);
tx.setTxData(cancelDeposit);
CoinData coinData = new CoinData();
List<Coin> toList = new ArrayList<>();
toList.add(new Coin(cancelDeposit.getAddress(), depositTransaction.getTxData().getDeposit(), 0));
coinData.setTo(toList);
List<Coin> fromList = new ArrayList<>();
for (int index = 0; index < depositTransaction.getCoinData().getTo().size(); index++) {
Coin coin = depositTransaction.getCoinData().getTo().get(index);
if (coin.getLockTime() == -1L && coin.getNa().equals(depositTransaction.getTxData().getDeposit())) {
coin.setOwner(ArraysTool.concatenate(hash.serialize(), new VarInt(index).encode()));
fromList.add(coin);
break;
}
}
if (fromList.isEmpty()) {
return Result.getFailed(KernelErrorCode.DATA_ERROR).toRpcClientResult();
}
coinData.setFrom(fromList);
tx.setCoinData(coinData);
Na fee = TransactionFeeCalculator.getMaxFee(108 + tx.size());
coinData.getTo().get(0).setNa(coinData.getTo().get(0).getNa().subtract(fee));
RpcClientResult result1 = this.txProcessing(tx, null, 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.account.model.Account in project nuls by nuls-io.
the class PocConsensusResource method createAgent.
@POST
@Path("/agent")
@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 createAgent(@ApiParam(name = "form", value = "创建节点表单数据", required = true) CreateAgentForm form) throws NulsException {
AssertUtil.canNotEmpty(form);
AssertUtil.canNotEmpty(form.getAgentAddress(), "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())) {
throw new NulsRuntimeException(AccountErrorCode.ADDRESS_ERROR);
}
Account account = accountService.getAccount(form.getAgentAddress()).getData();
if (null == account) {
return Result.getFailed(AccountErrorCode.ACCOUNT_NOT_EXIST).toRpcClientResult();
}
if (!account.isOk()) {
return Result.getFailed(AccountErrorCode.IMPORTING_ACCOUNT).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();
}
}
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()));
}
agent.setDeposit(Na.valueOf(form.getDeposit()));
agent.setCommissionRate(form.getCommissionRate());
tx.setTxData(agent);
CoinData coinData = new CoinData();
List<Coin> toList = new ArrayList<>();
toList.add(new Coin(agent.getAgentAddress(), agent.getDeposit(), PocConsensusConstant.CONSENSUS_LOCK_TIME));
coinData.setTo(toList);
tx.setCoinData(coinData);
CoinDataResult result = accountLedgerService.getCoinData(agent.getAgentAddress(), agent.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.account.model.Account in project nuls by nuls-io.
the class PocConsensusResource method stopAgent.
@POST
@Path("/agent/stop")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "注销共识节点 [3.6.5]", notes = "返回注销成功交易hash")
@ApiResponses(value = { @ApiResponse(code = 200, message = "success", response = String.class) })
public RpcClientResult stopAgent(@ApiParam(name = "form", value = "注销共识节点表单数据", required = true) StopAgentForm form) throws NulsException, IOException {
AssertUtil.canNotEmpty(form);
AssertUtil.canNotEmpty(form.getAddress());
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();
}
}
StopAgentTransaction tx = new StopAgentTransaction();
StopAgent stopAgent = new StopAgent();
stopAgent.setAddress(AddressTool.getAddress(form.getAddress()));
List<Agent> agentList = PocConsensusContext.getChainManager().getMasterChain().getChain().getAgentList();
Agent agent = null;
for (Agent a : agentList) {
if (a.getDelHeight() > 0) {
continue;
}
if (Arrays.equals(a.getAgentAddress(), account.getAddress().getAddressBytes())) {
agent = a;
break;
}
}
if (agent == null || agent.getDelHeight() > 0) {
return Result.getFailed(PocConsensusErrorCode.AGENT_NOT_EXIST).toRpcClientResult();
}
stopAgent.setCreateTxHash(agent.getTxHash());
tx.setTxData(stopAgent);
CoinData coinData = ConsensusTool.getStopAgentCoinData(agent, TimeService.currentTimeMillis() + PocConsensusConstant.STOP_AGENT_LOCK_TIME);
tx.setCoinData(coinData);
Na fee = TransactionFeeCalculator.getMaxFee(108 + tx.size());
coinData.getTo().get(0).setNa(coinData.getTo().get(0).getNa().subtract(fee));
RpcClientResult result1 = this.txProcessing(tx, null, 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();
}
Aggregations