Skip to content

Commit

Permalink
Update node info
Browse files Browse the repository at this point in the history
  • Loading branch information
leej1012 committed May 30, 2024
1 parent 2e6af27 commit 795c52d
Show file tree
Hide file tree
Showing 12 changed files with 262 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -212,15 +212,15 @@ public void exportAddressTransferTxs(
@RequestLimit(count = 30)
@ApiOperation(value = "get address staking info")
@GetMapping(value = "/{address}/staking-info")
public ResponseBean getAddressStakingInfo(@PathVariable @Length(min = 34, max = 42, message = "Incorrect address format") String address,
public ResponseBean getAddressStakingInfo(@PathVariable @Length(min = 34, max = 34, message = "Incorrect address format") String address,
@RequestParam(required = false) String channel) {
return addressService.getAddressStakingInfo(address, channel);
}

@RequestLimit(count = 30)
@ApiOperation(value = "get address staking info when round start")
@GetMapping(value = "/{address}/staking-info/round-start")
public ResponseBean getAddressStakingInfoWhenRoundStart(@PathVariable @Length(min = 34, max = 42, message = "Incorrect address format") String address) {
public ResponseBean getAddressStakingInfoWhenRoundStart(@PathVariable @Length(min = 34, max = 34, message = "Incorrect address format") String address) {
return addressService.getAddressStakingInfoWhenRoundStart(address);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.ontio.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.ontio.aop.RequestLimit;
import com.github.ontio.model.common.PageResponseBean;
import com.github.ontio.model.common.ResponseBean;
import com.github.ontio.model.dao.*;
Expand Down Expand Up @@ -156,13 +157,6 @@ public ResponseBean getOffChainInfoByPublicKey(@RequestParam("public_key") @Leng
return new ResponseBean(ErrorInfo.SUCCESS.code(), ErrorInfo.SUCCESS.desc(), nodeInfoList);
}

// @ApiOperation(value = "insert or update node register information by public key")
// @PostMapping(value = "/off-chain-info")
public ResponseBean updateOffChainInfoByPublicKey(@RequestBody UpdateOffChainNodeInfoDto updateOffChainNodeInfoDto) throws Exception {
ResponseBean responseBean = nodesService.updateOffChainInfoByPublicKey(updateOffChainNodeInfoDto);
return responseBean;
}

@ApiOperation(value = "Get reward per 10000 ONT stake unit")
@GetMapping(value = "/bonus-histories")
public ResponseBean getBonusHistories() {
Expand Down Expand Up @@ -378,4 +372,18 @@ public ResponseBean getNodes(@RequestBody NodesInfoDto dto) {
return new ResponseBean(ErrorInfo.SUCCESS.code(), ErrorInfo.SUCCESS.desc(), response);
}

@RequestLimit(count = 60)
@ApiOperation(value = "get address register node info")
@GetMapping(value = "/register-node-list")
public ResponseBean getAddressRegisterNodeList(@RequestParam @Length(min = 34, max = 34, message = "Incorrect address format") String address) {
return nodesService.getAddressRegisterNodeList(address);
}

@RequestLimit(count = 60)
@ApiOperation(value = "get node on chain config")
@GetMapping(value = "/node-on-chain-config")
public ResponseBean getNodeOnChainConfig(@RequestParam @Length(min = 34, max = 34, message = "Incorrect address format") String address,
@RequestParam("public_key") @Length(min = 56, max = 128, message = "invalid public key") String publicKey) {
return nodesService.getNodeOnChainConfig(address,publicKey);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ public interface NodeInfoOffChainMapper extends Mapper<NodeInfoOffChain> {

List<NodeInfoOffChain> selectAllStakingNodeInfo();

List<NodeInfoOffChain> selectAllRegisterNodeInfo(String address);
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ public class NodeInfoOffChain {
@Transient
private String progress;

@Transient
private Long initPos;

@Transient
private Long totalPos;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public class NodeInfoOffChainDto extends NodeInfoOffChain {

@Builder
public NodeInfoOffChainDto(String publicKey, String name, String address, String ontId, Integer nodeType, String introduction, String logoUrl, String region, BigDecimal longitude, BigDecimal latitude, String ip, String website, String socialMedia, String telegram, String twitter, String facebook, String openMail, String contactMail, Boolean openFlag, Integer verification, Integer contactInfoVerified, Integer feeSharingRatio, Integer ontologyHarbinger, Integer oldNode, Integer badActor, Integer risky, Integer status, String progress, Long totalPos, Long maxAuthorize, String userApy) {
super(publicKey, name, address, ontId, nodeType, introduction, logoUrl, region, longitude, latitude, ip, website, socialMedia, telegram, twitter, facebook, openMail, contactMail, openFlag, verification, contactInfoVerified, feeSharingRatio, ontologyHarbinger, oldNode, badActor, risky, status, progress, totalPos, maxAuthorize, userApy);
public NodeInfoOffChainDto(String publicKey, String name, String address, String ontId, Integer nodeType, String introduction, String logoUrl, String region, BigDecimal longitude, BigDecimal latitude, String ip, String website, String socialMedia, String telegram, String twitter, String facebook, String openMail, String contactMail, Boolean openFlag, Integer verification, Integer contactInfoVerified, Integer feeSharingRatio, Integer ontologyHarbinger, Integer oldNode, Integer badActor, Integer risky, Integer status, String progress, Long initPos, Long totalPos, Long maxAuthorize, String userApy) {
super(publicKey, name, address, ontId, nodeType, introduction, logoUrl, region, longitude, latitude, ip, website, socialMedia, telegram, twitter, facebook, openMail, contactMail, openFlag, verification, contactInfoVerified, feeSharingRatio, ontologyHarbinger, oldNode, badActor, risky, status, progress, initPos, totalPos, maxAuthorize, userApy);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.github.ontio.model.dto;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

/**
* @author lijie
* @version 1.0
* @date 2024/5/30
*/
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class NodeManagementDto {

private String nodeApr = "0.00%";

private String userApr = "0.00%";

private String feeSharingRatioNodeT = "0%";

private String feeSharingRatioNodeT1 = "0%";

private String feeSharingRatioNodeT2 = "0%";

private String feeSharingRatioUserT = "0%";

private String feeSharingRatioUserT1 = "0%";

private String feeSharingRatioUserT2 = "0%";

private String promiseStake = "0";

private String nodeStake = "0";

private String userStake = "0";

private String totalStake = "0";

private String cap = "0";

private String withdrawableAmount;

private String lockedAmount;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.github.ontio.model.dto;

import com.github.ontio.common.Helper;
import com.github.ontio.io.BinaryReader;
import com.github.ontio.io.BinaryWriter;
import com.github.ontio.io.Serializable;

import java.io.IOException;
import java.math.BigInteger;

public class PromisePosInfo implements Serializable {
public String peerPubkey;
public BigInteger promisePos;

public PromisePosInfo() {
}

public void deserialize(BinaryReader reader) throws IOException {
this.peerPubkey = reader.readVarString();
this.promisePos = Helper.BigIntFromNeoBytes(reader.readVarBytes());
}

public void serialize(BinaryWriter writer) throws IOException {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ public interface INodesService {

NodeInfoOffChain getCurrentOffChainInfoPublic(String publicKey, Integer openFlag);

ResponseBean updateOffChainInfoByPublicKey(UpdateOffChainNodeInfoDto nodeInfoOffChainDto) throws Exception;

List<NodeInfoOffChain> getCurrentOffChainInfo();

NodeInfoOnChain getCurrentOnChainInfo(String publicKey);
Expand Down Expand Up @@ -100,4 +98,7 @@ public interface INodesService {

PageResponseBean getNodesByFilter(NodesInfoDto nodesInfo);

ResponseBean getAddressRegisterNodeList(String address);

ResponseBean getNodeOnChainConfig(String address, String publicKey);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,14 @@
package com.github.ontio.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.ontio.common.Helper;
import com.github.ontio.config.ParamsConfig;
import com.github.ontio.core.governance.PeerPoolItem;
import com.github.ontio.exception.ExplorerException;
import com.github.ontio.mapper.*;
import com.github.ontio.model.common.PageResponseBean;
import com.github.ontio.model.common.ResponseBean;
import com.github.ontio.model.dao.*;
import com.github.ontio.model.dto.*;
import com.github.ontio.mapper.CommonMapper;
import com.github.ontio.mapper.NetNodeInfoMapper;
import com.github.ontio.mapper.NodeBonusMapper;
import com.github.ontio.mapper.NodeInfoOffChainMapper;
import com.github.ontio.mapper.NodeInfoOnChainMapper;
import com.github.ontio.mapper.NodeOverviewMapper;
import com.github.ontio.mapper.NodeRankChangeMapper;
import com.github.ontio.mapper.NodeRankHistoryMapper;
import com.github.ontio.model.common.PageResponseBean;
import com.github.ontio.model.dao.NetNodeInfo;
import com.github.ontio.model.dao.NodeBonus;
import com.github.ontio.model.dao.NodeInfoOffChain;
import com.github.ontio.model.dao.NodeInfoOnChain;
import com.github.ontio.model.dao.NodeInfoOnChainWithBonus;
import com.github.ontio.model.dao.NodeInfoOnChainWithRankChange;
import com.github.ontio.model.dao.NodeRankChange;
import com.github.ontio.model.dao.NodeRankHistory;
import com.github.ontio.sdk.exception.SDKException;
import com.github.ontio.service.INodesService;
import com.github.ontio.util.ConstantParam;
Expand Down Expand Up @@ -283,35 +267,6 @@ public NodeInfoOffChain getCurrentOffChainInfoPublic(String publicKey, Integer o
}
}

@Override
public ResponseBean updateOffChainInfoByPublicKey(UpdateOffChainNodeInfoDto updateOffChainNodeInfoDto) throws Exception {
String nodeInfo = updateOffChainNodeInfoDto.getNodeInfo();
String stakePublicKey = updateOffChainNodeInfoDto.getPublicKey();
String signature = updateOffChainNodeInfoDto.getSignature();

byte[] nodeInfoBytes = Helper.hexToBytes(nodeInfo);
initSDK();
boolean verify = sdk.verifySignatureByPublicKey(stakePublicKey, nodeInfoBytes, signature);
if (!verify) {
return new ResponseBean(ErrorInfo.VERIFY_SIGN_FAILED.code(), ErrorInfo.VERIFY_SIGN_FAILED.desc(), "");
}
String nodeInfoStr = new String(nodeInfoBytes, "UTF-8");
NodeInfoOffChain nodeInfoOffChain = JSONObject.parseObject(nodeInfoStr, NodeInfoOffChain.class);
nodeInfoOffChain.setVerification(ConstantParam.NODE_NOT_VERIFIED);
nodeInfoOffChain.setOntId("");
nodeInfoOffChain.setNodeType(ConstantParam.CANDIDATE_NODE);
String nodePublicKey = nodeInfoOffChain.getPublicKey();
NodeInfoOffChainDto nodeInfoOffChainDto = nodeInfoOffChainMapper.selectByPublicKey(nodePublicKey, null);
if (null == nodeInfoOffChainDto) {
// insert
nodeInfoOffChainMapper.insertSelective(nodeInfoOffChain);
} else {
// update
nodeInfoOffChainMapper.updateByPrimaryKeySelective(nodeInfoOffChain);
}
return new ResponseBean(ErrorInfo.SUCCESS.code(), ErrorInfo.SUCCESS.desc(), ErrorInfo.SUCCESS.desc());
}

@Override
public List<NodeBonus> getNodeBonusHistories() {
try {
Expand Down Expand Up @@ -1043,4 +998,98 @@ public PageResponseBean getNodesByFilter(NodesInfoDto dto) {
return new PageResponseBean(respDtoList.subList(start, Math.min(start + pageSize, respSize)), respSize);
}
}

@Override
public ResponseBean getAddressRegisterNodeList(String address) {
List<NodeInfoOffChain> registerNodeList = nodeInfoOffChainMapper.selectAllRegisterNodeInfo(address);
if (!CollectionUtils.isEmpty(registerNodeList)) {
try {
initSDK();
Map peerPoolMap = sdk.getPeerPoolMap();
for (NodeInfoOffChain registerNodeInfo : registerNodeList) {
String publicKey = registerNodeInfo.getPublicKey();
long initPos = 0;
long totalPos = 0;
// status:1-在线;2-正在退出;3-已退出
int status = 3;
if (peerPoolMap.containsKey(publicKey)) {
PeerPoolItem item = (PeerPoolItem) peerPoolMap.get(publicKey);
initPos = item.initPos;
totalPos = item.totalPos;
if (item.status == 1 || item.status == 2) {
status = 1;
} else {
status = 2;
}
} else {
String authorizeInfo = sdk.getAuthorizeInfo(publicKey, address);
if (StringUtils.hasLength(authorizeInfo)) {
JSONObject jsonObject = JSONObject.parseObject(authorizeInfo);
initPos = jsonObject.getLong("withdrawUnfreezePos");
}
}
registerNodeInfo.setStatus(status);
registerNodeInfo.setInitPos(initPos);
registerNodeInfo.setTotalPos(totalPos);
}
} catch (Exception e) {
log.error("getAddressRegisterNodeInfo error:{},{}", address, e.getMessage());
}
}
return new ResponseBean(ErrorInfo.SUCCESS.code(), ErrorInfo.SUCCESS.desc(), registerNodeList);
}

@Override
public ResponseBean getNodeOnChainConfig(String address, String publicKey) {
NodeManagementDto nodeManagementDto = new NodeManagementDto();
NodeInspire nodeInspire = nodeInspireMapper.selectByPrimaryKey(publicKey);
if (nodeInspire != null) {
String nodeApr = Optional.ofNullable(nodeInspire.getNodeApy()).orElse("0.00%");
String userApr = Optional.ofNullable(nodeInspire.getUserApy()).orElse("0.00%");
nodeManagementDto.setNodeApr(nodeApr);
nodeManagementDto.setUserApr(userApr);
}
try {
initSDK();
String peerPoolInfoStr = sdk.getPeerPoolInfo(publicKey);
if (StringUtils.hasLength(peerPoolInfoStr)) {
JSONObject peerPoolInfo = JSONObject.parseObject(peerPoolInfoStr);
Long initPos = peerPoolInfo.getLong("initPos");
Long totalPos = peerPoolInfo.getLong("totalPos");
long allStake = initPos + totalPos;
nodeManagementDto.setNodeStake(initPos.toString());
nodeManagementDto.setUserStake(totalPos.toString());
nodeManagementDto.setTotalStake(String.valueOf(allStake));
}

long promisePos = sdk.getPromisePos(publicKey);
nodeManagementDto.setPromiseStake(String.valueOf(promisePos));

String attributesStr = sdk.getAttributes(publicKey);
if (StringUtils.hasLength(attributesStr)) {
JSONObject attributes = JSONObject.parseObject(attributesStr);
nodeManagementDto.setCap(attributes.getLong("maxAuthorize").toString());
nodeManagementDto.setFeeSharingRatioNodeT(attributes.getLong("tPeerCost") + "%");
nodeManagementDto.setFeeSharingRatioNodeT1(attributes.getLong("t1PeerCost") + "%");
nodeManagementDto.setFeeSharingRatioNodeT2(attributes.getLong("t2PeerCost") + "%");
nodeManagementDto.setFeeSharingRatioUserT(attributes.getLong("tStakeCost") + "%");
nodeManagementDto.setFeeSharingRatioUserT1(attributes.getLong("t1StakeCost") + "%");
nodeManagementDto.setFeeSharingRatioUserT2(attributes.getLong("t2StakeCost") + "%");
}

String authorizeInfoStr = sdk.getAuthorizeInfo(publicKey, address);
if (StringUtils.hasLength(authorizeInfoStr)) {
JSONObject authorizeInfo = JSONObject.parseObject(authorizeInfoStr);
Long withdrawPos = authorizeInfo.getLong("withdrawPos");
Long withdrawFreezePos = authorizeInfo.getLong("withdrawFreezePos");
Long withdrawUnfreezePos = authorizeInfo.getLong("withdrawUnfreezePos");
long lockedAmount = withdrawPos + withdrawFreezePos;
nodeManagementDto.setWithdrawableAmount(withdrawUnfreezePos.toString());
nodeManagementDto.setLockedAmount(String.valueOf(lockedAmount));
}
} catch (Exception e) {
log.error("getNodeOnChainConfig error:{},{},{}", address, publicKey, e.getMessage());
}
return new ResponseBean(ErrorInfo.SUCCESS.code(), ErrorInfo.SUCCESS.desc(), nodeManagementDto);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.github.ontio.io.BinaryReader;
import com.github.ontio.io.BinaryWriter;
import com.github.ontio.io.Serializable;
import com.github.ontio.model.dto.PromisePosInfo;
import com.github.ontio.network.exception.ConnectorException;
import com.github.ontio.sdk.exception.SDKException;
import com.github.ontio.smartcontract.nativevm.abi.NativeBuildParams;
Expand Down Expand Up @@ -630,6 +631,41 @@ public String getAuthorizeInfo(String publicKey, String address) throws SDKExcep
return ontSdk.nativevm().governance().getAuthorizeInfo(publicKey, addr);
}

public Map getPeerPoolMap() throws Exception {
OntSdk ontSdk = getOntSdk();
return ontSdk.nativevm().governance().getPeerPoolMap();
}

public String getPeerPoolInfo(String publicKey) throws Exception {
OntSdk ontSdk = getOntSdk();
return ontSdk.nativevm().governance().getPeerInfo(publicKey);
}

public String getAttributes(String publicKey) throws Exception {
OntSdk ontSdk = getOntSdk();
return ontSdk.nativevm().governance().getPeerAttributes(publicKey);
}

public long getPromisePos(String publicKey) {
OntSdk ontSdk = getOntSdk();
byte[] publicKeyPrefix = Helper.hexToBytes(publicKey);
byte[] promisePos = "promisePos".getBytes();
byte[] key = new byte[promisePos.length + publicKeyPrefix.length];
System.arraycopy(promisePos, 0, key, 0, promisePos.length);
System.arraycopy(publicKeyPrefix, 0, key, promisePos.length, publicKeyPrefix.length);

try {
String res = ontSdk.getConnect().getStorage(Helper.reverse(contractAddress), Helper.toHexString(key));
if (res != null && !res.equals("")) {
PromisePosInfo promisePosInfo = Serializable.from(Helper.hexToBytes(res), PromisePosInfo.class);
return promisePosInfo.promisePos.longValue();
}
} catch (Exception e) {
log.error("getPromisePos error", e);
}
return 0;
}

public int getPreConsensusCount() {
try {
Configuration preConfiguration = getPreConfiguration();
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
ALTER TABLE tbl_node_info_on_chain ADD INDEX idx_public_key (public_key);
ALTER TABLE tbl_node_info_on_chain ADD INDEX idx_public_key (public_key);
ALTER TABLE tbl_node_info_off_chain ADD INDEX idx_address (address);
Loading

0 comments on commit 795c52d

Please sign in to comment.