From 341ea237555a30bcc18da8aee9c533401a2f5acb Mon Sep 17 00:00:00 2001 From: leej1012 Date: Fri, 9 Aug 2024 16:15:50 +0800 Subject: [PATCH] Update Node --- .../ontio/controller/NodesController.java | 31 ++++++++++ .../github/ontio/mapper/BadNodeMapper.java | 15 +++++ .../com/github/ontio/mapper/CommonMapper.java | 3 + .../ontio/mapper/NodeInfoOnChainMapper.java | 2 + .../com/github/ontio/model/dao/BadNode.java | 27 ++++++++ .../ontio/model/dto/NodeInfoOnChainDto.java | 20 ++++++ .../github/ontio/service/INodesService.java | 8 ++- .../ontio/service/impl/NodesServiceImpl.java | 62 +++++++++++++++++-- .../db/migration/V1.84__tbl_bad_node.sql | 9 +++ .../main/resources/mapper/BadNodeMapper.xml | 10 +++ .../main/resources/mapper/CommonMapper.xml | 13 ++++ .../mapper/NodeInfoOnChainMapper.xml | 32 ++++++++++ 12 files changed, 226 insertions(+), 6 deletions(-) create mode 100644 back-end-projects/Explorer/src/main/java/com/github/ontio/mapper/BadNodeMapper.java create mode 100644 back-end-projects/Explorer/src/main/java/com/github/ontio/model/dao/BadNode.java create mode 100644 back-end-projects/Explorer/src/main/resources/db/migration/V1.84__tbl_bad_node.sql create mode 100644 back-end-projects/Explorer/src/main/resources/mapper/BadNodeMapper.xml diff --git a/back-end-projects/Explorer/src/main/java/com/github/ontio/controller/NodesController.java b/back-end-projects/Explorer/src/main/java/com/github/ontio/controller/NodesController.java index 03342db7..e6166d7c 100644 --- a/back-end-projects/Explorer/src/main/java/com/github/ontio/controller/NodesController.java +++ b/back-end-projects/Explorer/src/main/java/com/github/ontio/controller/NodesController.java @@ -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) { @@ -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); + } + + } diff --git a/back-end-projects/Explorer/src/main/java/com/github/ontio/mapper/BadNodeMapper.java b/back-end-projects/Explorer/src/main/java/com/github/ontio/mapper/BadNodeMapper.java new file mode 100644 index 00000000..133a2f37 --- /dev/null +++ b/back-end-projects/Explorer/src/main/java/com/github/ontio/mapper/BadNodeMapper.java @@ -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 { + + List selectBadNodeByCycle(int cycle); + +} \ No newline at end of file diff --git a/back-end-projects/Explorer/src/main/java/com/github/ontio/mapper/CommonMapper.java b/back-end-projects/Explorer/src/main/java/com/github/ontio/mapper/CommonMapper.java index 7d663145..8552a9eb 100644 --- a/back-end-projects/Explorer/src/main/java/com/github/ontio/mapper/CommonMapper.java +++ b/back-end-projects/Explorer/src/main/java/com/github/ontio/mapper/CommonMapper.java @@ -18,4 +18,7 @@ public interface CommonMapper { List getStakingInfoByAddress(@Param("address") String address); + List getStakingAddressByPublicKey(@Param("pubKey") String pubKey); + + List getAllStakingAddress(); } diff --git a/back-end-projects/Explorer/src/main/java/com/github/ontio/mapper/NodeInfoOnChainMapper.java b/back-end-projects/Explorer/src/main/java/com/github/ontio/mapper/NodeInfoOnChainMapper.java index bab4b64c..b0fd392e 100644 --- a/back-end-projects/Explorer/src/main/java/com/github/ontio/mapper/NodeInfoOnChainMapper.java +++ b/back-end-projects/Explorer/src/main/java/com/github/ontio/mapper/NodeInfoOnChainMapper.java @@ -13,6 +13,8 @@ public interface NodeInfoOnChainMapper extends Mapper { List selectAllInfo(String publicKey); + NodeInfoOnChainDto selectNodeInfoByPublicKey(String publicKey, int lastCycle, int currentCycle); + Long selectTotalStake(); NodeInfoOnChainDto selectByPublicKey(String publicKey); diff --git a/back-end-projects/Explorer/src/main/java/com/github/ontio/model/dao/BadNode.java b/back-end-projects/Explorer/src/main/java/com/github/ontio/model/dao/BadNode.java new file mode 100644 index 00000000..ba4c5fbd --- /dev/null +++ b/back-end-projects/Explorer/src/main/java/com/github/ontio/model/dao/BadNode.java @@ -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; +} \ No newline at end of file diff --git a/back-end-projects/Explorer/src/main/java/com/github/ontio/model/dto/NodeInfoOnChainDto.java b/back-end-projects/Explorer/src/main/java/com/github/ontio/model/dto/NodeInfoOnChainDto.java index eb52b6fc..316ddf5b 100644 --- a/back-end-projects/Explorer/src/main/java/com/github/ontio/model/dto/NodeInfoOnChainDto.java +++ b/back-end-projects/Explorer/src/main/java/com/github/ontio/model/dto/NodeInfoOnChainDto.java @@ -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() { } @@ -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(); } } \ No newline at end of file diff --git a/back-end-projects/Explorer/src/main/java/com/github/ontio/service/INodesService.java b/back-end-projects/Explorer/src/main/java/com/github/ontio/service/INodesService.java index 868b2891..c5fbdd57 100644 --- a/back-end-projects/Explorer/src/main/java/com/github/ontio/service/INodesService.java +++ b/back-end-projects/Explorer/src/main/java/com/github/ontio/service/INodesService.java @@ -39,6 +39,8 @@ public interface INodesService { List getCurrentOnChainStakeInfo(String publicKey); + NodeInfoOnChainDto getSingleNodeInfo(String publicKey); + NodeInfoOffChain getCurrentOffChainInfo(String publicKey, Integer openFlag); NodeInfoOffChain getCurrentOffChainInfoPublic(String publicKey, Integer openFlag, String channel); @@ -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); } diff --git a/back-end-projects/Explorer/src/main/java/com/github/ontio/service/impl/NodesServiceImpl.java b/back-end-projects/Explorer/src/main/java/com/github/ontio/service/impl/NodesServiceImpl.java index ac124be5..8e5fc58a 100644 --- a/back-end-projects/Explorer/src/main/java/com/github/ontio/service/impl/NodesServiceImpl.java +++ b/back-end-projects/Explorer/src/main/java/com/github/ontio/service/impl/NodesServiceImpl.java @@ -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, @@ -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; @@ -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; @@ -164,6 +165,43 @@ public List 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(); @@ -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); @@ -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 addressList = commonMapper.getAllStakingAddress(); + return new ResponseBean(ErrorInfo.SUCCESS.code(), ErrorInfo.SUCCESS.desc(), addressList); + } + + public ResponseBean getStakingAddressByNode(String publicKey) { + List addressList = commonMapper.getStakingAddressByPublicKey(publicKey); + return new ResponseBean(ErrorInfo.SUCCESS.code(), ErrorInfo.SUCCESS.desc(), addressList); + } + + public ResponseBean getBadNode(Integer cycle) { + List nodeList = badNodeMapper.selectBadNodeByCycle(cycle); + return new ResponseBean(ErrorInfo.SUCCESS.code(), ErrorInfo.SUCCESS.desc(), nodeList); + } } diff --git a/back-end-projects/Explorer/src/main/resources/db/migration/V1.84__tbl_bad_node.sql b/back-end-projects/Explorer/src/main/resources/db/migration/V1.84__tbl_bad_node.sql new file mode 100644 index 00000000..d69754bf --- /dev/null +++ b/back-end-projects/Explorer/src/main/resources/db/migration/V1.84__tbl_bad_node.sql @@ -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; \ No newline at end of file diff --git a/back-end-projects/Explorer/src/main/resources/mapper/BadNodeMapper.xml b/back-end-projects/Explorer/src/main/resources/mapper/BadNodeMapper.xml new file mode 100644 index 00000000..5a0184b6 --- /dev/null +++ b/back-end-projects/Explorer/src/main/resources/mapper/BadNodeMapper.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/back-end-projects/Explorer/src/main/resources/mapper/CommonMapper.xml b/back-end-projects/Explorer/src/main/resources/mapper/CommonMapper.xml index dee2d62b..8208ae8a 100644 --- a/back-end-projects/Explorer/src/main/resources/mapper/CommonMapper.xml +++ b/back-end-projects/Explorer/src/main/resources/mapper/CommonMapper.xml @@ -35,4 +35,17 @@ INNER JOIN tbl_node_info_off_chain b ON a.peer_pub_key = b.public_key WHERE a.address = #{address} + + + + \ No newline at end of file diff --git a/back-end-projects/Explorer/src/main/resources/mapper/NodeInfoOnChainMapper.xml b/back-end-projects/Explorer/src/main/resources/mapper/NodeInfoOnChainMapper.xml index 4e064caa..d921fc74 100644 --- a/back-end-projects/Explorer/src/main/resources/mapper/NodeInfoOnChainMapper.xml +++ b/back-end-projects/Explorer/src/main/resources/mapper/NodeInfoOnChainMapper.xml @@ -59,6 +59,38 @@ + +