Skip to content

Commit

Permalink
Update Node
Browse files Browse the repository at this point in the history
  • Loading branch information
leej1012 committed Aug 9, 2024
1 parent 1c39253 commit 341ea23
Show file tree
Hide file tree
Showing 12 changed files with 226 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ public ResponseBean getCurrentStake(@RequestParam(value = "public_key", required
return new ResponseBean(ErrorInfo.SUCCESS.code(), ErrorInfo.SUCCESS.desc(), nodeInfoList);
}

@RequestLimit(count = 60)
@ApiOperation(value = "get single node information")
@GetMapping(value = "/single-node-info")
public ResponseBean getSingleNodeInfo(@RequestParam("public_key") @Length(min = 56, max = 128, message = "invalid public key") String publicKey) {
NodeInfoOnChainDto singleNodeInfo = nodesService.getSingleNodeInfo(publicKey);
return new ResponseBean(ErrorInfo.SUCCESS.code(), ErrorInfo.SUCCESS.desc(), singleNodeInfo);
}

@ApiOperation(value = "Get nodes register information")
@GetMapping(value = "/off-chain-infos")
public ResponseBean getOffChainInfo(@RequestParam(value = "node_type", defaultValue = "-1") Integer nodeType) {
Expand Down Expand Up @@ -387,4 +395,27 @@ public ResponseBean getNodeOnChainConfig(@RequestParam @Length(min = 34, max = 3
@RequestParam("public_key") @Length(min = 56, max = 128, message = "invalid public key") String publicKey) {
return nodesService.getNodeOnChainConfig(address, publicKey);
}

@RequestLimit(count = 10)
@ApiOperation(value = "get all staking address")
@GetMapping(value = "/all-staking-address")
public ResponseBean getAllStakingAddress() {
return nodesService.getAllStakingAddress();
}

@RequestLimit(count = 60)
@ApiOperation(value = "get staking address by node")
@GetMapping(value = "/staking-address")
public ResponseBean getStakingAddressByNode(@RequestParam("public_key") @Length(min = 56, max = 128, message = "invalid public key") String publicKey) {
return nodesService.getStakingAddressByNode(publicKey);
}

@RequestLimit(count = 60)
@ApiOperation(value = "get bad nodes")
@GetMapping(value = "/bad-node")
public ResponseBean getBadNode(@RequestParam Integer cycle) {
return nodesService.getBadNode(cycle);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.github.ontio.mapper;

import com.github.ontio.model.dao.BadNode;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;


@Repository
public interface BadNodeMapper extends Mapper<BadNode> {

List<String> selectBadNodeByCycle(int cycle);

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@ public interface CommonMapper {

List<GovernanceInfoDto> getStakingInfoByAddress(@Param("address") String address);

List<String> getStakingAddressByPublicKey(@Param("pubKey") String pubKey);

List<String> getAllStakingAddress();
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public interface NodeInfoOnChainMapper extends Mapper<NodeInfoOnChain> {

List<NodeInfoOnChainDto> selectAllInfo(String publicKey);

NodeInfoOnChainDto selectNodeInfoByPublicKey(String publicKey, int lastCycle, int currentCycle);

Long selectTotalStake();

NodeInfoOnChainDto selectByPublicKey(String publicKey);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.github.ontio.model.dao;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "tbl_bad_node")
public class BadNode {
@Id
@GeneratedValue(generator = "JDBC")
private Integer id;

@Column(name = "public_key")
private String publicKey;

private Integer cycle;
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ public class NodeInfoOnChainDto extends NodeInfoOnChain {
@Transient
private String apr;

@Transient
private String feeSharingRatioNodeT;

@Transient
private String feeSharingRatioNodeT1;

@Transient
private String feeSharingRatioUserT;

@Transient
private String feeSharingRatioUserT1;

@Transient
private Boolean lastBadNode;

@Transient
private Boolean newBadNode;

public NodeInfoOnChainDto() {

}
Expand All @@ -46,5 +64,7 @@ public NodeInfoOnChainDto(NodeInfoOnChainDto nodeInfoOnChain) {
this.risky = nodeInfoOnChain.getRisky();
this.badActor = nodeInfoOnChain.getBadActor();
this.apr = nodeInfoOnChain.getApr();
this.lastBadNode = nodeInfoOnChain.getLastBadNode();
this.newBadNode = nodeInfoOnChain.getNewBadNode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public interface INodesService {

List<NodeInfoOnChainWithRankChange> getCurrentOnChainStakeInfo(String publicKey);

NodeInfoOnChainDto getSingleNodeInfo(String publicKey);

NodeInfoOffChain getCurrentOffChainInfo(String publicKey, Integer openFlag);

NodeInfoOffChain getCurrentOffChainInfoPublic(String publicKey, Integer openFlag, String channel);
Expand Down Expand Up @@ -100,5 +102,9 @@ public interface INodesService {

ResponseBean getAddressRegisterNodeList(String address);

ResponseBean getNodeOnChainConfig(String address, String publicKey);
ResponseBean getAllStakingAddress();

ResponseBean getStakingAddressByNode(String publicKey);

ResponseBean getBadNode(Integer cycle);
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ public class NodesServiceImpl implements INodesService {

private final NodeInspireMapper nodeInspireMapper;

private final TokenServiceImpl tokenService;

private final InspireCalculationParamsMapper inspireCalculationParamsMapper;

private final NodeCycleMapper nodeCycleMapper;

private final BadNodeMapper badNodeMapper;

@Autowired
public NodesServiceImpl(ParamsConfig paramsConfig,
NodeBonusMapper nodeBonusMapper,
Expand All @@ -93,7 +93,8 @@ public NodesServiceImpl(ParamsConfig paramsConfig,
NodeInspireMapper nodeInspireMapper,
TokenServiceImpl tokenService,
InspireCalculationParamsMapper inspireCalculationParamsMapper,
NodeCycleMapper nodeCycleMapper
NodeCycleMapper nodeCycleMapper,
BadNodeMapper badNodeMapper
) {
this.paramsConfig = paramsConfig;
this.nodeBonusMapper = nodeBonusMapper;
Expand All @@ -106,9 +107,9 @@ public NodesServiceImpl(ParamsConfig paramsConfig,
this.commonMapper = commonMapper;
this.nodeOverviewHistoryMapper = nodeOverviewHistoryMapper;
this.nodeInspireMapper = nodeInspireMapper;
this.tokenService = tokenService;
this.inspireCalculationParamsMapper = inspireCalculationParamsMapper;
this.nodeCycleMapper = nodeCycleMapper;
this.badNodeMapper = badNodeMapper;
}

private OntologySDKService sdk;
Expand Down Expand Up @@ -164,6 +165,43 @@ public List<NodeInfoOnChainWithRankChange> getCurrentOnChainStakeInfo(String pub
}
}

@Override
public NodeInfoOnChainDto getSingleNodeInfo(String publicKey) {
initSDK();
int currentRound = sdk.getGovernanceView();
NodeInfoOnChainDto nodeInfo = nodeInfoOnChainMapper.selectNodeInfoByPublicKey(publicKey, currentRound - 1, currentRound);
if (nodeInfo == null) {
throw new ExplorerException(ErrorInfo.NOT_FOUND);
}
String tPeerCost = "";
String t1PeerCost = "";
String tStakeCost = "";
String t1StakeCost = "";
try {
String attributesStr = sdk.getAttributes(publicKey);
if (StringUtils.hasLength(attributesStr)) {
JSONObject attributes = JSONObject.parseObject(attributesStr);
tPeerCost = (100 - attributes.getLong("tPeerCost")) + "%";
t1PeerCost = (100 - attributes.getLong("t1PeerCost")) + "%";
tStakeCost = (100 - attributes.getLong("tStakeCost")) + "%";
t1StakeCost = (100 - attributes.getLong("t1StakeCost")) + "%";
}
} catch (Exception e) {
log.error("getSingleNodeInfo sdk.getAttributes error:{}", e.getMessage());
}
nodeInfo.setFeeSharingRatioNodeT(tPeerCost);
nodeInfo.setFeeSharingRatioNodeT1(t1PeerCost);
nodeInfo.setFeeSharingRatioUserT(tStakeCost);
nodeInfo.setFeeSharingRatioUserT1(t1StakeCost);
Long maxAuthorize = nodeInfo.getMaxAuthorize();
Long initPos = nodeInfo.getInitPos();
long tenTimesInitPos = initPos * 10;
if (maxAuthorize > tenTimesInitPos) {
nodeInfo.setMaxAuthorize(tenTimesInitPos);
}
return nodeInfo;
}

public long getCurrentTotalStake() {
try {
return nodeInfoOnChainMapper.selectTotalStake();
Expand Down Expand Up @@ -1181,7 +1219,6 @@ public ResponseBean getAddressRegisterNodeList(String address) {
return new ResponseBean(ErrorInfo.SUCCESS.code(), ErrorInfo.SUCCESS.desc(), result);
}

@Override
public ResponseBean getNodeOnChainConfig(String address, String publicKey) {
NodeManagementDto nodeManagementDto = new NodeManagementDto();
NodeInspire nodeInspire = nodeInspireMapper.selectByPrimaryKey(publicKey);
Expand Down Expand Up @@ -1244,4 +1281,19 @@ public ResponseBean getNodeOnChainConfig(String address, String publicKey) {
}
return new ResponseBean(ErrorInfo.SUCCESS.code(), ErrorInfo.SUCCESS.desc(), nodeManagementDto);
}

public ResponseBean getAllStakingAddress() {
List<String> addressList = commonMapper.getAllStakingAddress();
return new ResponseBean(ErrorInfo.SUCCESS.code(), ErrorInfo.SUCCESS.desc(), addressList);
}

public ResponseBean getStakingAddressByNode(String publicKey) {
List<String> addressList = commonMapper.getStakingAddressByPublicKey(publicKey);
return new ResponseBean(ErrorInfo.SUCCESS.code(), ErrorInfo.SUCCESS.desc(), addressList);
}

public ResponseBean getBadNode(Integer cycle) {
List<String> nodeList = badNodeMapper.selectBadNodeByCycle(cycle);
return new ResponseBean(ErrorInfo.SUCCESS.code(), ErrorInfo.SUCCESS.desc(), nodeList);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE TABLE `tbl_bad_node`
(
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键',
`public_key` varchar(70) NOT NULL COMMENT '节点公钥',
`cycle` int(11) NOT NULL COMMENT '周期数',
PRIMARY KEY (`id`),
KEY `idx_public_key` (`public_key`),
KEY `idx_cycle` (`cycle`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.github.ontio.mapper.BadNodeMapper">

<select id="selectBadNodeByCycle" resultType="String">
SELECT public_key
FROM tbl_bad_node
WHERE cycle = #{cycle}
</select>
</mapper>
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,17 @@
INNER JOIN tbl_node_info_off_chain b ON a.peer_pub_key = b.public_key
WHERE a.address = #{address}
</select>

<select id="getStakingAddressByPublicKey" resultType="String">
SELECT address
FROM tbl_governance_info
WHERE peer_pub_key = #{pubKey}
AND (consensus_pos > 0 OR candidate_pos > 0)
</select>

<select id="getAllStakingAddress" resultType="String">
SELECT DISTINCT address
FROM tbl_governance_info
WHERE consensus_pos > 0 OR candidate_pos > 0 OR withdraw_candidate_pos > 0
</select>
</mapper>
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,38 @@
</where>
</select>

<select id="selectNodeInfoByPublicKey" resultType="com.github.ontio.model.dto.NodeInfoOnChainDto" useCache="true">
SELECT a.node_rank,
a.name,
a.current_stake,
a.progress,
a.detail_url,
a.public_key,
a.address,
a.status,
a.init_pos,
a.total_pos,
a.max_authorize,
a.node_proportion,
a.user_proportion,
a.current_stake_percentage,
b.introduction,
b.logo_url,
b.ontology_harbinger,
b.fee_sharing_ratio,
b.risky,
b.bad_actor,
IFNULL(c.user_apy,'0.00%') as apr,
d.id IS NOT NULL as lastBadNode,
e.id IS NOT NULL as newBadNode
FROM tbl_node_info_on_chain a
LEFT JOIN tbl_node_info_off_chain b ON a.public_key = b.public_key
LEFT JOIN tbl_node_inspire c ON a.public_key = c.public_key
LEFT JOIN tbl_bad_node d ON a.public_key = d.public_key AND d.cycle = #{lastCycle}
LEFT JOIN tbl_bad_node e ON a.public_key = e.public_key AND e.cycle = #{currentCycle}
WHERE a.public_key = #{publicKey}
</select>

<select id="selectTotalStake" resultType="java.lang.Long" useCache="true">
SELECT SUM(current_stake)
FROM tbl_node_info_on_chain
Expand Down

0 comments on commit 341ea23

Please sign in to comment.