From bbe67403061ccf5c894df8e59e4503f2c788fe5c Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Tue, 28 Apr 2020 16:01:10 +0800 Subject: [PATCH 001/131] fix double alert of same group --- .../webase/node/mgr/alert/task/AuditMonitorTask.java | 9 +++++---- .../node/mgr/alert/task/NodeStatusMonitorTask.java | 9 ++++----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/webank/webase/node/mgr/alert/task/AuditMonitorTask.java b/src/main/java/com/webank/webase/node/mgr/alert/task/AuditMonitorTask.java index 49b1d8441..ed3642413 100644 --- a/src/main/java/com/webank/webase/node/mgr/alert/task/AuditMonitorTask.java +++ b/src/main/java/com/webank/webase/node/mgr/alert/task/AuditMonitorTask.java @@ -20,10 +20,11 @@ import com.webank.webase.node.mgr.alert.rule.AlertRuleService; import com.webank.webase.node.mgr.alert.rule.entity.TbAlertRule; import com.webank.webase.node.mgr.base.enums.AlertRuleType; +import com.webank.webase.node.mgr.base.enums.DataStatus; import com.webank.webase.node.mgr.base.properties.ConstantProperties; import com.webank.webase.node.mgr.base.tools.AlertRuleTools; -import com.webank.webase.node.mgr.frontgroupmap.entity.FrontGroup; -import com.webank.webase.node.mgr.frontgroupmap.entity.FrontGroupMapCache; +import com.webank.webase.node.mgr.group.GroupService; +import com.webank.webase.node.mgr.group.entity.TbGroup; import com.webank.webase.node.mgr.monitor.MonitorService; import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.annotation.Autowired; @@ -47,7 +48,7 @@ public class AuditMonitorTask { @Autowired private ConstantProperties cProperties; @Autowired - private FrontGroupMapCache frontGroupMapCache; + private GroupService groupService; @Autowired private MailService alertMailService; @Autowired @@ -74,7 +75,7 @@ public synchronized void checkUserAndContractForAlert() { " for beyond alert interval:{}", alertRule); return; } - List groupList = frontGroupMapCache.getAllMap(); + List groupList = groupService.getGroupList(DataStatus.NORMAL.getValue()); if (groupList == null || groupList.size() == 0) { log.warn("checkUserAndContractForAlert jump over: not found any group"); return; diff --git a/src/main/java/com/webank/webase/node/mgr/alert/task/NodeStatusMonitorTask.java b/src/main/java/com/webank/webase/node/mgr/alert/task/NodeStatusMonitorTask.java index d0db1672a..04082d656 100644 --- a/src/main/java/com/webank/webase/node/mgr/alert/task/NodeStatusMonitorTask.java +++ b/src/main/java/com/webank/webase/node/mgr/alert/task/NodeStatusMonitorTask.java @@ -23,8 +23,8 @@ import com.webank.webase.node.mgr.base.enums.AlertRuleType; import com.webank.webase.node.mgr.base.enums.DataStatus; import com.webank.webase.node.mgr.base.tools.AlertRuleTools; -import com.webank.webase.node.mgr.frontgroupmap.entity.FrontGroup; -import com.webank.webase.node.mgr.frontgroupmap.entity.FrontGroupMapCache; +import com.webank.webase.node.mgr.group.GroupService; +import com.webank.webase.node.mgr.group.entity.TbGroup; import com.webank.webase.node.mgr.node.Node; import com.webank.webase.node.mgr.node.NodeService; import com.webank.webase.node.mgr.node.TbNode; @@ -39,7 +39,6 @@ import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; -import java.util.concurrent.CountDownLatch; /** * cannot connect to node triggers alert mail @@ -51,7 +50,7 @@ public class NodeStatusMonitorTask { @Autowired private NodeService nodeService; @Autowired - private FrontGroupMapCache frontGroupMapCache; + private GroupService groupService; @Autowired private PrecompiledService precompiledService; @Autowired @@ -78,7 +77,7 @@ public synchronized void checkAllNodeStatusForAlert() { return; } - List groupList = frontGroupMapCache.getAllMap(); + List groupList = groupService.getGroupList(DataStatus.NORMAL.getValue()); if (groupList == null || groupList.size() == 0) { log.warn("checkNodeStatusForAlert jump over: not found any group"); return; From 6e364ab6f53d687e533ccfcf6de3064e48853d47 Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Tue, 28 Apr 2020 16:37:55 +0800 Subject: [PATCH 002/131] fix cert alert repeat --- .../com/webank/webase/node/mgr/alert/task/CertMonitorTask.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/webank/webase/node/mgr/alert/task/CertMonitorTask.java b/src/main/java/com/webank/webase/node/mgr/alert/task/CertMonitorTask.java index 8bbf4ff92..9cfa74628 100644 --- a/src/main/java/com/webank/webase/node/mgr/alert/task/CertMonitorTask.java +++ b/src/main/java/com/webank/webase/node/mgr/alert/task/CertMonitorTask.java @@ -77,9 +77,9 @@ public synchronized void checkCertValidityForAlert() { return; } List certList = certService.loadAllX509Certs(); - List alertContentList = new ArrayList<>(); certList.stream() .forEach(cert -> { + List alertContentList = new ArrayList<>(); Date certNotAfter = cert.getNotAfter(); if(checkWithin7days(certNotAfter)){ log.warn("cert validity alert. certNotAfter:{}", From 0413d2559f076fd1c0480efd565ee6450b37934f Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Thu, 30 Apr 2020 14:13:31 +0800 Subject: [PATCH 003/131] update log warn --- .../webase/node/mgr/node/NodeService.java | 2 +- .../node/mgr/scheduler/PullKeyStoreTask.java | 206 ------------------ 2 files changed, 1 insertion(+), 207 deletions(-) delete mode 100644 src/main/java/com/webank/webase/node/mgr/scheduler/PullKeyStoreTask.java diff --git a/src/main/java/com/webank/webase/node/mgr/node/NodeService.java b/src/main/java/com/webank/webase/node/mgr/node/NodeService.java index fd31e7fb5..9e926e635 100644 --- a/src/main/java/com/webank/webase/node/mgr/node/NodeService.java +++ b/src/main/java/com/webank/webase/node/mgr/node/NodeService.java @@ -215,7 +215,7 @@ public void checkAndUpdateNodeStatus(int groupId) { Duration duration = Duration.between(modifyTime, LocalDateTime.now()); Long subTime = duration.toMillis(); if (subTime < CHECK_NODE_WAIT_MIN_MILLIS && createTime.isBefore(modifyTime)) { - log.info("checkNodeStatus jump over. subTime:{}", subTime); + log.warn("checkNodeStatus jump over. subTime:{}", subTime); return; } diff --git a/src/main/java/com/webank/webase/node/mgr/scheduler/PullKeyStoreTask.java b/src/main/java/com/webank/webase/node/mgr/scheduler/PullKeyStoreTask.java deleted file mode 100644 index b3b2960b5..000000000 --- a/src/main/java/com/webank/webase/node/mgr/scheduler/PullKeyStoreTask.java +++ /dev/null @@ -1,206 +0,0 @@ -///** -// * Copyright 2014-2019 the original author or authors. -// *

-// * Licensed under the Apache License, Version 2.0 (the "License"); -// * you may not use this file except in compliance with the License. -// * You may obtain a copy of the License at -// *

-// * http://www.apache.org/licenses/LICENSE-2.0 -// *

-// * Unless required by applicable law or agreed to in writing, software -// * distributed under the License is distributed on an "AS IS" BASIS, -// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// * See the License for the specific language governing permissions and -// * limitations under the License. -// */ -// -//package com.webank.webase.node.mgr.scheduler; -// -//import com.webank.webase.node.mgr.base.enums.HasPk; -//import com.webank.webase.node.mgr.base.enums.UserType; -//import com.webank.webase.node.mgr.front.FrontService; -//import com.webank.webase.node.mgr.front.entity.FrontParam; -//import com.webank.webase.node.mgr.front.entity.TbFront; -//import com.webank.webase.node.mgr.frontinterface.FrontInterfaceService; -//import com.webank.webase.node.mgr.group.GroupService; -//import com.webank.webase.node.mgr.group.entity.TbGroup; -//import com.webank.webase.node.mgr.user.UserMapper; -//import com.webank.webase.node.mgr.user.UserService; -//import com.webank.webase.node.mgr.user.entity.KeyPair; -//import com.webank.webase.node.mgr.user.entity.TbUser; -//import com.webank.webase.node.mgr.user.entity.UserParam; -//import lombok.extern.log4j.Log4j2; -//import org.springframework.beans.factory.annotation.Autowired; -//import org.springframework.scheduling.annotation.Scheduled; -//import org.springframework.stereotype.Component; -// -//import java.time.Duration; -//import java.time.Instant; -//import java.util.*; -// -///** -// * pull key store from different front and refresh local users -// * @author marsli -// */ -//@Log4j2 -//@Component -//public class PullKeyStoreTask { -// -// @Autowired -// private UserService userService; -// @Autowired -// private UserMapper userMapper; -// @Autowired -// private FrontInterfaceService frontInterfaceService; -// @Autowired -// private FrontService frontService; -// @Autowired -// private GroupService groupService; -// -// @Scheduled(fixedDelayString = "${constant.pullKeyStoreTaskFixedDelay}") -// public void taskStart() { -// refreshLocalUser(); -// } -// -// public synchronized void refreshLocalUser() { -// Instant startTime = Instant.now(); -// log.info("start refreshLocalUser startTime:{}", startTime.toEpochMilli()); -// List frontList = frontService.getFrontList(new FrontParam()); -// if (frontList == null || frontList.size() == 0) { -// log.warn("refreshLocalUser jump over: not found any front"); -// return; -// } -// List groupIdList = getGroupIdList(); -// -// // -// Map> pullAddressMap = pullAddressListFromFront(frontList, groupIdList); -// for(Integer groupId : groupIdList) { -// // // -// Map> localAddressMap = getLocalUserAddressList(); -// // group's list of address -// List localAddressList = localAddressMap.get(groupId); -// // all keypair from front -// final List pullAddressList = pullAddressMap.get(groupId); -// if (localAddressList.isEmpty()) { -// continue; -// } -// -// // delete user in db that in localList but not in pullList -// localAddressList.forEach(address -> { -// // judge contain -// long equalCount = 0; -// equalCount = pullAddressList.stream().filter(keyPair -> -// address.equals(keyPair.getAddress())).count(); -// // if pull list not have local user's address, remove it -// log.debug("refreshLocalUser localAddressList groupId:{}, address:{}, equalCount:{}", -// groupId, address, equalCount); -// if (equalCount == 0 ) { -// userService.deleteByAddress(address); -// } -// }); -// // add user in db that in pullList but not in localList -// pullAddressList.forEach(keyPair -> { -// // if local list don't have key pair, add it in db -// log.debug("refreshLocalUser pullAddressList groupId:{}, address:{}", -// groupId, keyPair.getAddress()); -// if (!localAddressList.contains(keyPair.getAddress())) { -// addUserInfo(groupId, keyPair); -// } -// }); -// } -// log.info("end refreshLocalUser useTime:{} ", -// Duration.between(startTime, Instant.now()).toMillis()); -// } -// -// /** -// * get local user list of address -// * @return list sorted by groupId -// */ -// private Map> getLocalUserAddressList() { -// log.debug("start getLocalUserAddressList."); -// Map> localGroupIdAddressMap = new HashMap<>(); -// // get group id list -// List groupIdList = getGroupIdList(); -// for (Integer groupId: groupIdList) { -// // init address list of groupId in map -// localGroupIdAddressMap.put(groupId, new ArrayList<>()); -// // get user address list by group id -// List addressList = new ArrayList<>(); -// UserParam userParam = new UserParam(); -// userParam.setGroupId(groupId); -// List localUserList = userService.qureyUserList(userParam); -// localUserList.forEach(user -> addressList.add(user.getAddress())); -// // set map's address list -// localGroupIdAddressMap.get(groupId).addAll(addressList); -// } -// log.debug("end getLocalUserAddressList. localGroupIdAddressMap:{}", localGroupIdAddressMap); -// return localGroupIdAddressMap; -// } -// -// /** -// * pull key store list from all front -// * @param frontList -// * @param groupIdList -// * @return list sorted by groupId -// */ -// private Map> pullAddressListFromFront(List frontList, List groupIdList) { -// log.debug("start pullAddressListFromFront."); -// Map> groupKeyPairListMap = new HashMap<>(); -// // init map with empty list by groupId -// groupIdList.forEach(groupId -> groupKeyPairListMap.put(groupId, new ArrayList<>())); -// // set map -// for (TbFront front : frontList) { -// String frontIp = front.getFrontIp(); -// int frontPort = front.getFrontPort(); -// // group id useless in getting keyStore list -// List keyPairList = frontInterfaceService.getKeyStoreList(1, frontIp, frontPort); -// keyPairList.forEach(keyPair -> { -// String userName = keyPair.getUserName() + "_" + front.getFrontId(); -// keyPair.setUserName(userName); -// }); -// // fill each group with the same key pair list -// groupIdList.forEach(groupId -> -// groupKeyPairListMap -// .get(groupId) -// .addAll(keyPairList)); -// } -// log.debug("end getLocalUserAddressList. groupKeyPairListMap:{}", groupKeyPairListMap); -// return groupKeyPairListMap; -// } -// -// /** -// * getGroupIdList -// * @return list of group id int -// */ -// private List getGroupIdList() { -// // get all group id -// List allGroup = groupService.getGroupList(null); -// List resList = new ArrayList<>(); -// allGroup.forEach(tbGroup -> resList.add(tbGroup.getGroupId())); -// return resList; -// } -// -// /** -// * add user by group id -// * @param keyPair -// */ -// private void addUserInfo(int groupId, KeyPair keyPair) { -// String address = keyPair.getAddress(); -// String publicKey = keyPair.getPublicKey(); -// String[] nameAndDesc = keyPair.getUserName().split("_"); -// String userName = nameAndDesc[0] + "_" + address.substring(0, 5); -// String descPrefix = "from front: "; -// String description = descPrefix + nameAndDesc[1]; -// // add row in all group -// TbUser newUserRow = new TbUser(HasPk.HAS.getValue(), UserType.GENERALUSER.getValue(), -// userName, -// groupId, address, publicKey, -// description); -// Integer affectRow = userMapper.addUserRow(newUserRow); -// if (affectRow == 0) { -// log.warn("pull user save: affect 0 rows of tb_user"); -// } -// -// } -// -//} From ed1998caa5a1c11f1250c481f5cbfa4097bb2194 Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Thu, 30 Apr 2020 18:08:10 +0800 Subject: [PATCH 004/131] update mapper --- src/main/resources/mapper/FrontMapper.xml | 5 +++++ src/main/resources/mapper/GroupMapper.xml | 8 ++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/main/resources/mapper/FrontMapper.xml b/src/main/resources/mapper/FrontMapper.xml index fe7195d8c..8fc7024cc 100644 --- a/src/main/resources/mapper/FrontMapper.xml +++ b/src/main/resources/mapper/FrontMapper.xml @@ -71,4 +71,9 @@ select * from tb_front where front_id = #{frontId} + + diff --git a/src/main/resources/mapper/GroupMapper.xml b/src/main/resources/mapper/GroupMapper.xml index c2deb3b2f..be23a7b97 100644 --- a/src/main/resources/mapper/GroupMapper.xml +++ b/src/main/resources/mapper/GroupMapper.xml @@ -12,6 +12,10 @@ property="groupStatus"/> + + insert into tb_group - (group_id,group_name,node_count,create_time) - values(#{groupId},#{groupName},#{nodeCount},NOW()) + (group_id,group_name,node_count,description,group_type,create_time) + values(#{groupId},#{groupName},#{nodeCount},#{description},#{groupType},NOW()) ON DUPLICATE KEY UPDATE node_count=#{nodeCount},modify_time=NOW() From 5d2777d40ac5c4627b43ffafa803c83834a08490 Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Thu, 30 Apr 2020 18:08:38 +0800 Subject: [PATCH 005/131] support dynamic group --- script/webase-ddl.sql | 19 +- .../node/mgr/base/code/ConstantCode.java | 3 + .../webase/node/mgr/base/enums/GroupType.java | 43 ++++ .../webase/node/mgr/front/FrontMapper.java | 2 + .../webase/node/mgr/front/FrontService.java | 16 +- .../webase/node/mgr/front/entity/TbFront.java | 4 +- .../frontinterface/FrontInterfaceService.java | 18 +- .../mgr/frontinterface/FrontRestTools.java | 2 +- .../node/mgr/group/GroupController.java | 130 +++++++--- .../webase/node/mgr/group/GroupService.java | 225 +++++++++++++----- .../mgr/group/entity/ReqBatchStartGroup.java | 37 +++ .../mgr/group/entity/ReqGenerateGroup.java | 41 ++++ .../mgr/group/entity/ReqOperateGroup.java | 34 +++ .../webase/node/mgr/group/entity/TbGroup.java | 9 +- 14 files changed, 464 insertions(+), 119 deletions(-) create mode 100644 src/main/java/com/webank/webase/node/mgr/base/enums/GroupType.java create mode 100644 src/main/java/com/webank/webase/node/mgr/group/entity/ReqBatchStartGroup.java create mode 100644 src/main/java/com/webank/webase/node/mgr/group/entity/ReqGenerateGroup.java create mode 100644 src/main/java/com/webank/webase/node/mgr/group/entity/ReqOperateGroup.java diff --git a/script/webase-ddl.sql b/script/webase-ddl.sql index 946eab5cd..b05ba0ef0 100644 --- a/script/webase-ddl.sql +++ b/script/webase-ddl.sql @@ -3,15 +3,16 @@ -- Table structure for tb_group -- ---------------------------- CREATE TABLE IF NOT EXISTS tb_group ( - group_id int(11) NOT NULL COMMENT '群组ID', - group_name varchar(64) NOT NULL COMMENT '群组名字', - group_status int(1) DEFAULT '1' COMMENT '状态(1-正常 2-异常)', - node_count int DEFAULT '0' COMMENT '群组下节点数', - group_desc varchar(1024) COMMENT '群组描述', - create_time datetime DEFAULT NULL COMMENT '创建时间', - modify_time datetime DEFAULT NULL COMMENT '修改时间', - PRIMARY KEY (group_id) - ) COMMENT='群组信息表' ENGINE=InnoDB CHARSET=utf8; + group_id int(11) NOT NULL COMMENT '群组ID', + group_name varchar(64) NOT NULL COMMENT '群组名字', + group_status int(1) DEFAULT '1' COMMENT '状态(1-正常 2-异常)', + node_count int DEFAULT '0' COMMENT '群组下节点数', + description varchar(1024) COMMENT '群组描述', + group_type int COMMENT '群组类型(1-拉取,2-动态创建)', + create_time datetime DEFAULT NULL COMMENT '创建时间', + modify_time datetime DEFAULT NULL COMMENT '修改时间', + PRIMARY KEY (group_id) +) COMMENT='群组信息表' ENGINE=InnoDB CHARSET=utf8; -- ---------------------------- diff --git a/src/main/java/com/webank/webase/node/mgr/base/code/ConstantCode.java b/src/main/java/com/webank/webase/node/mgr/base/code/ConstantCode.java index f9585d1ac..7d5007cc0 100644 --- a/src/main/java/com/webank/webase/node/mgr/base/code/ConstantCode.java +++ b/src/main/java/com/webank/webase/node/mgr/base/code/ConstantCode.java @@ -143,6 +143,9 @@ public class ConstantCode { public static final RetCode ENCRYPT_TYPE_NOT_MATCH = RetCode.mark(202091, "Front's encrypt type not matches with nodemgr"); + /* dynamic group manage */ + public static final RetCode GROUP_ID_EXISTS = RetCode.mark(202300, "group id already exists"); + public static final RetCode NODE_NOT_EXISTS = RetCode.mark(202301, "node front not exists"); /* auth */ public static final RetCode USER_NOT_LOGGED_IN = RetCode.mark(302000, "user not logged in"); diff --git a/src/main/java/com/webank/webase/node/mgr/base/enums/GroupType.java b/src/main/java/com/webank/webase/node/mgr/base/enums/GroupType.java new file mode 100644 index 000000000..a09b97fa4 --- /dev/null +++ b/src/main/java/com/webank/webase/node/mgr/base/enums/GroupType.java @@ -0,0 +1,43 @@ +/** + * Copyright 2014-2020 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.webank.webase.node.mgr.base.enums; + +/** + * Enumeration of group type. + * @author marsli + */ +public enum GroupType { + /** + * sync from node + */ + SYNC(1), + /** + * manual generated + */ + MANUAL(2); + + private int value; + + GroupType(Integer groupType) { + this.value = groupType; + } + + public int getValue() { + return this.value; + } +} + diff --git a/src/main/java/com/webank/webase/node/mgr/front/FrontMapper.java b/src/main/java/com/webank/webase/node/mgr/front/FrontMapper.java index ee1cdd0f9..4e8b17c05 100644 --- a/src/main/java/com/webank/webase/node/mgr/front/FrontMapper.java +++ b/src/main/java/com/webank/webase/node/mgr/front/FrontMapper.java @@ -31,4 +31,6 @@ public interface FrontMapper { List getList(FrontParam param); TbFront getById(@Param("frontId") int frontId); + + TbFront getByNodeId(@Param("nodeId") String nodeId); } \ No newline at end of file diff --git a/src/main/java/com/webank/webase/node/mgr/front/FrontService.java b/src/main/java/com/webank/webase/node/mgr/front/FrontService.java index 6742c8770..ed857cf31 100644 --- a/src/main/java/com/webank/webase/node/mgr/front/FrontService.java +++ b/src/main/java/com/webank/webase/node/mgr/front/FrontService.java @@ -15,6 +15,7 @@ import com.alibaba.fastjson.JSON; import com.webank.webase.node.mgr.base.code.ConstantCode; +import com.webank.webase.node.mgr.base.enums.GroupType; import com.webank.webase.node.mgr.base.exception.NodeMgrException; import com.webank.webase.node.mgr.base.properties.ConstantProperties; import com.webank.webase.node.mgr.base.tools.CertTools; @@ -128,8 +129,9 @@ public TbFront newFront(FrontInfo frontInfo) { PeerInfo[] peerArr = frontInterface .getPeersFromSpecificFront(frontIp, frontPort, group); List peerList = Arrays.asList(peerArr); - //add groupId - groupService.saveGroupId(group, groupPeerList.size()); + //add group + groupService.saveGroup(group, groupPeerList.size(), "synchronous", + GroupType.SYNC.getValue()); //save front group map frontGroupMapService.newFrontGroup(tbFront.getFrontId(), group); //save nodes @@ -236,6 +238,16 @@ public TbFront getById(int frontId) { return frontMapper.getById(frontId); } + /** + * query front by nodeId. + */ + public TbFront getByNodeId(String nodeId) { + if (StringUtils.isBlank(nodeId)) { + return null; + } + return frontMapper.getByNodeId(nodeId); + } + /** * remove front */ diff --git a/src/main/java/com/webank/webase/node/mgr/front/entity/TbFront.java b/src/main/java/com/webank/webase/node/mgr/front/entity/TbFront.java index d876cb956..67537c9ba 100644 --- a/src/main/java/com/webank/webase/node/mgr/front/entity/TbFront.java +++ b/src/main/java/com/webank/webase/node/mgr/front/entity/TbFront.java @@ -25,7 +25,9 @@ public class TbFront { private String frontIp; private Integer frontPort; private String agency; - // node version + /** + * node version + */ private String clientVersion; private LocalDateTime createTime; private LocalDateTime modifyTime; diff --git a/src/main/java/com/webank/webase/node/mgr/frontinterface/FrontInterfaceService.java b/src/main/java/com/webank/webase/node/mgr/frontinterface/FrontInterfaceService.java index 90f5e4354..a79abf555 100644 --- a/src/main/java/com/webank/webase/node/mgr/frontinterface/FrontInterfaceService.java +++ b/src/main/java/com/webank/webase/node/mgr/frontinterface/FrontInterfaceService.java @@ -446,16 +446,14 @@ public GroupHandleResult generateGroup(String frontIp, Integer frontPort, /** * start group. */ - public GroupHandleResult startGroup(String frontIp, Integer frontPort, Integer startGroupId) { - log.debug("start startGroup frontIp:{} frontPort:{} startGroupId:{}", - frontIp, frontPort, startGroupId); - - Integer groupId = Integer.MAX_VALUE; - String uri = String.format(FrontRestTools.URI_START_GROUP, startGroupId); - GroupHandleResult groupHandleResult = - getFromSpecificFront(groupId, frontIp, frontPort, uri, GroupHandleResult.class); - - log.debug("end startGroup"); + public Object operateGroup(String frontIp, Integer frontPort, Integer groupId, String type) { + log.debug("start operateGroup frontIp:{} frontPort:{} groupId:{}", frontIp, frontPort, + groupId); + String uri = String.format(FrontRestTools.URI_OPERATE_GROUP, type); + Object groupHandleResult = + getFromSpecificFront(groupId, frontIp, frontPort, uri, Object.class); + + log.debug("end operateGroup"); return groupHandleResult; } diff --git a/src/main/java/com/webank/webase/node/mgr/frontinterface/FrontRestTools.java b/src/main/java/com/webank/webase/node/mgr/frontinterface/FrontRestTools.java index 43f1826c7..e167dc479 100644 --- a/src/main/java/com/webank/webase/node/mgr/frontinterface/FrontRestTools.java +++ b/src/main/java/com/webank/webase/node/mgr/frontinterface/FrontRestTools.java @@ -72,7 +72,7 @@ public class FrontRestTools { public static final String URI_GET_OBSERVER_LIST = "web3/observerList"; public static final String URI_GET_CLIENT_VERSION = "web3/clientVersion"; public static final String URI_GENERATE_GROUP = "web3/generateGroup"; - public static final String URI_START_GROUP = "web3/startGroup/%1s"; + public static final String URI_OPERATE_GROUP = "web3/operateGroup/%1s"; public static final String URI_REFRESH_FRONT = "web3/refresh"; public static final String FRONT_PERFORMANCE_RATIO = "performance"; public static final String FRONT_PERFORMANCE_CONFIG = "performance/config"; diff --git a/src/main/java/com/webank/webase/node/mgr/group/GroupController.java b/src/main/java/com/webank/webase/node/mgr/group/GroupController.java index 24cbdeacd..28f51f567 100644 --- a/src/main/java/com/webank/webase/node/mgr/group/GroupController.java +++ b/src/main/java/com/webank/webase/node/mgr/group/GroupController.java @@ -21,8 +21,7 @@ import com.webank.webase.node.mgr.base.enums.DataStatus; import com.webank.webase.node.mgr.base.exception.NodeMgrException; import com.webank.webase.node.mgr.frontinterface.entity.GenerateGroupInfo; -import com.webank.webase.node.mgr.group.entity.GroupGeneral; -import com.webank.webase.node.mgr.group.entity.TbGroup; +import com.webank.webase.node.mgr.group.entity.*; import com.webank.webase.node.mgr.scheduler.ResetGroupListTask; import com.webank.webase.node.mgr.scheduler.StatisticsTransdailyTask; import com.webank.webase.node.mgr.transdaily.SeventDaysTrans; @@ -58,43 +57,7 @@ public class GroupController extends BaseController { private StatisticsTransdailyTask statisticsTask; @Autowired private ResetGroupListTask resetGroupListTask; - - /** - * generate group. - */ - @PostMapping("/generate/{frontIp}/{frontPort}") - public BaseResponse generateGroup(@RequestBody @Valid GenerateGroupInfo req, - @PathVariable("frontIp") String frontIp, @PathVariable("frontPort") Integer frontPort, - BindingResult result) throws NodeMgrException { - checkBindResult(result); - Instant startTime = Instant.now(); - BaseResponse baseResponse = new BaseResponse(ConstantCode.SUCCESS); - log.info("start generateGroup startTime:{} groupId:{}", startTime.toEpochMilli(), - req.getGenerateGroupId()); - groupService.generateGroup(req, frontIp, frontPort); - log.info("end getGroupGeneral useTime:{} result:{}", - Duration.between(startTime, Instant.now()).toMillis(), - JSON.toJSONString(baseResponse)); - return baseResponse; - } - - /** - * start group. - */ - @GetMapping("/start/{startGroupId}/{frontIp}/{frontPort}") - public BaseResponse startGroup(@PathVariable("startGroupId") Integer startGroupId, - @PathVariable("frontIp") String frontIp, @PathVariable("frontPort") Integer frontPort) - throws NodeMgrException { - Instant startTime = Instant.now(); - BaseResponse baseResponse = new BaseResponse(ConstantCode.SUCCESS); - log.info("start startGroup startTime:{} groupId:{}", startTime.toEpochMilli(), - startGroupId); - groupService.startGroup(startGroupId, frontIp, frontPort); - log.info("end startGroup useTime:{} result:{}", - Duration.between(startTime, Instant.now()).toMillis(), - JSON.toJSONString(baseResponse)); - return baseResponse; - } + /** * get group general. @@ -173,4 +136,93 @@ public BaseResponse getTransDaily(@PathVariable("groupId") Integer groupId) thro JSON.toJSONString(pagesponse)); return pagesponse; } + + /** + * generate group to single node. + */ + @PostMapping("/generate/{nodeId}") + public BaseResponse generateToSingleNode(@PathVariable("nodeId") String nodeId, + @RequestBody @Valid ReqGenerateGroup req, + BindingResult result) throws NodeMgrException { + checkBindResult(result); + Instant startTime = Instant.now(); + BaseResponse baseResponse = new BaseResponse(ConstantCode.SUCCESS); + log.info("start generateToSingleNode startTime:{} nodeId:{}", startTime.toEpochMilli(), + nodeId); + TbGroup tbGroup = groupService.generateToSingleNode(nodeId, req); + baseResponse.setData(tbGroup); + log.info("end generateToSingleNode useTime:{}", + Duration.between(startTime, Instant.now()).toMillis()); + return baseResponse; + } + + /** + * generate group. + */ + @PostMapping("/generate") + public BaseResponse generateGroup(@RequestBody @Valid ReqGenerateGroup req, + BindingResult result) throws NodeMgrException { + checkBindResult(result); + Instant startTime = Instant.now(); + BaseResponse baseResponse = new BaseResponse(ConstantCode.SUCCESS); + log.info("start generateGroup startTime:{} groupId:{}", startTime.toEpochMilli(), + req.getGenerateGroupId()); + TbGroup tbGroup = groupService.generateGroup(req); + baseResponse.setData(tbGroup); + log.info("end generateGroup useTime:{}", + Duration.between(startTime, Instant.now()).toMillis()); + return baseResponse; + } + + /** + * operate group. + */ + @PostMapping("/operate/{nodeId}") + public Object operateGroup(@PathVariable("nodeId") String nodeId, @RequestBody @Valid ReqOperateGroup req, + BindingResult result) throws NodeMgrException { + checkBindResult(result); + Instant startTime = Instant.now(); + Integer groupId = req.getGenerateGroupId(); + String type = req.getType(); + log.info("start operateGroup startTime:{} groupId:{}", startTime.toEpochMilli(), groupId); + + Object groupHandleResult = groupService.operateGroup(nodeId, groupId, type); + log.info("end operateGroup useTime:{} result:{}", + Duration.between(startTime, Instant.now()).toMillis(), + JSON.toJSONString(groupHandleResult)); + return groupHandleResult; + } + + /** + * batch start group. + */ + @PostMapping("/batchStart") + public BaseResponse batchStartGroup(@RequestBody @Valid ReqBatchStartGroup req, BindingResult result) + throws NodeMgrException { + checkBindResult(result); + Instant startTime = Instant.now(); + BaseResponse baseResponse = new BaseResponse(ConstantCode.SUCCESS); + log.info("start batchStartGroup startTime:{} groupId:{}", startTime.toEpochMilli(), + req.getGenerateGroupId()); + groupService.batchStartGroup(req); + log.info("end batchStartGroup useTime:{} result:{}", + Duration.between(startTime, Instant.now()).toMillis(), + JSON.toJSONString(baseResponse)); + return baseResponse; + } + + /** + * update group. + */ + @GetMapping("/update") + public BaseResponse updateGroup() throws NodeMgrException { + Instant startTime = Instant.now(); + BaseResponse baseResponse = new BaseResponse(ConstantCode.SUCCESS); + log.info("start updateGroup startTime:{}", startTime.toEpochMilli()); + groupService.resetGroupList(); + log.info("end updateGroup useTime:{} result:{}", + Duration.between(startTime, Instant.now()).toMillis(), + JSON.toJSONString(baseResponse)); + return baseResponse; + } } diff --git a/src/main/java/com/webank/webase/node/mgr/group/GroupService.java b/src/main/java/com/webank/webase/node/mgr/group/GroupService.java index dd2122e22..7b554b82d 100644 --- a/src/main/java/com/webank/webase/node/mgr/group/GroupService.java +++ b/src/main/java/com/webank/webase/node/mgr/group/GroupService.java @@ -16,6 +16,7 @@ import com.alibaba.fastjson.JSON; import com.webank.webase.node.mgr.base.code.ConstantCode; import com.webank.webase.node.mgr.base.enums.DataStatus; +import com.webank.webase.node.mgr.base.enums.GroupType; import com.webank.webase.node.mgr.base.exception.NodeMgrException; import com.webank.webase.node.mgr.base.properties.ConstantProperties; import com.webank.webase.node.mgr.base.tools.NodeMgrTools; @@ -29,9 +30,7 @@ import com.webank.webase.node.mgr.frontinterface.FrontInterfaceService; import com.webank.webase.node.mgr.frontinterface.entity.GenerateGroupInfo; import com.webank.webase.node.mgr.frontinterface.entity.GroupHandleResult; -import com.webank.webase.node.mgr.group.entity.GroupGeneral; -import com.webank.webase.node.mgr.group.entity.StatisticalGroupTransInfo; -import com.webank.webase.node.mgr.group.entity.TbGroup; +import com.webank.webase.node.mgr.group.entity.*; import com.webank.webase.node.mgr.method.MethodService; import com.webank.webase.node.mgr.node.NodeService; import com.webank.webase.node.mgr.node.TbNode; @@ -40,6 +39,7 @@ import com.webank.webase.node.mgr.transdaily.TransDailyService; import com.webank.webase.node.mgr.user.UserService; import lombok.extern.log4j.Log4j2; +import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -82,57 +82,6 @@ public class GroupService { @Autowired private ConstantProperties constants; - /** - * generate group. - * - * @return - */ - public void generateGroup(GenerateGroupInfo generateGroupInfo, String frontIp, Integer frontPort) { - // request front to generate - GroupHandleResult groupHandleResult = frontInterface.generateGroup(frontIp, - frontPort, generateGroupInfo); - // check result - int code = NodeMgrTools.parseHexStr2Int(groupHandleResult.getCode()); - if (code != 0) { - log.error("fail generateGroupId:{} code:{}", generateGroupInfo.getGenerateGroupId(), code); - throw new NodeMgrException(code, groupHandleResult.getMessage()); - } - } - - /** - * start group. - * - * @param startGroupId - */ - public void startGroup(Integer startGroupId, String frontIp, Integer frontPort) { - // request front to start - GroupHandleResult groupHandleResult = frontInterface.startGroup(frontIp, - frontPort, startGroupId); - // check result - int code = NodeMgrTools.parseHexStr2Int(groupHandleResult.getCode()); - if (code != 0) { - log.error("fail startGroup startGroupId:{} code:{}", startGroupId, code); - throw new NodeMgrException(code, groupHandleResult.getMessage()); - } - // @Deprecated: already refresh in front when start a group - // frontInterface.refreshFront(frontIp, frontPort); - } - - /** - * save group id - */ - public void saveGroupId(int groupId, int nodeCount) { - if (groupId == 0) { - return; - } - //save group id - String groupName = "group" + groupId; - TbGroup tbGroup = new TbGroup(groupId, groupName, nodeCount); - groupMapper.save(tbGroup); - - //create table by group id - tableService.newTableByGroupId(groupId); - } /** * query count of group. @@ -264,8 +213,9 @@ public void resetGroupList() { allGroupSet.add(gId); //peer in group List groupPeerList = frontInterface.getNodeIDListFromSpecificFront(frontIp, frontPort, gId); - //save groupId - saveGroupId(gId, groupPeerList.size()); + // save group + saveGroup(gId, groupPeerList.size(), "synchronous", + GroupType.SYNC.getValue()); frontGroupMapService.newFrontGroup(front.getFrontId(), gId); //save new peers savePeerList(frontIp, frontPort, gId, groupPeerList); @@ -429,4 +379,167 @@ private void removeByGroupId(int groupId) { //drop table. tableService.dropTableByGroupId(groupId); } + + /** + * generate group to single node. + * + * @param req info + * @return + */ + public TbGroup generateToSingleNode(String nodeId, ReqGenerateGroup req) { + Integer generateGroupId = req.getGenerateGroupId(); + + TbFront tbFront = frontService.getByNodeId(nodeId); + if (tbFront == null) { + log.error("fail generateToSingleNode node front not exists."); + throw new NodeMgrException(ConstantCode.NODE_NOT_EXISTS); + } + // request front to generate + GenerateGroupInfo generateGroupInfo = new GenerateGroupInfo(); + BeanUtils.copyProperties(req, generateGroupInfo); + frontInterface.generateGroup(tbFront.getFrontIp(), tbFront.getFrontPort(), + generateGroupInfo); + // save group + TbGroup tbGroup = saveGroup(generateGroupId, req.getNodeList().size(), + req.getDescription(), GroupType.MANUAL.getValue()); + return tbGroup; + } + + /** + * generate group. + * + * @param req info + * @return + */ + public TbGroup generateGroup(ReqGenerateGroup req) { + Integer generateGroupId = req.getGenerateGroupId(); + checkGroupIdExisted(generateGroupId); + + for (String nodeId : req.getNodeList()) { + // get front + TbFront tbFront = frontService.getByNodeId(nodeId); + if (tbFront == null) { + log.error("fail generateGroup node front not exists."); + throw new NodeMgrException(ConstantCode.NODE_NOT_EXISTS); + } + // request front to generate + GenerateGroupInfo generateGroupInfo = new GenerateGroupInfo(); + BeanUtils.copyProperties(req, generateGroupInfo); + frontInterface.generateGroup(tbFront.getFrontIp(), tbFront.getFrontPort(), + generateGroupInfo); + } + // save group + TbGroup tbGroup = saveGroup(generateGroupId, req.getNodeList().size(), + req.getDescription(), GroupType.MANUAL.getValue()); + return tbGroup; + } + + /** + * operate group. + * + * @param nodeId + * @param groupId + * @param type + * @return + */ + public Object operateGroup(String nodeId, Integer groupId, String type) { + // get front + TbFront tbFront = frontService.getByNodeId(nodeId); + if (tbFront == null) { + log.error("fail operateGroup node front not exists."); + throw new NodeMgrException(ConstantCode.NODE_NOT_EXISTS); + } + // request front to operate + Object groupHandleResult = frontInterface.operateGroup(tbFront.getFrontIp(), + tbFront.getFrontPort(), groupId, type); + + // refresh + resetGroupList(); + + // return + return groupHandleResult; + } + + /** + * batch start group. + * + * @param req + */ + public void batchStartGroup(ReqBatchStartGroup req) { + Integer groupId = req.getGenerateGroupId(); + // check id + checkGroupIdValid(groupId); + for (String nodeId : req.getNodeList()) { + // get front + TbFront tbFront = frontService.getByNodeId(nodeId); + if (tbFront == null) { + log.error("fail batchStartGroup node not exists."); + throw new NodeMgrException(ConstantCode.NODE_NOT_EXISTS); + } + // request front to start + frontInterface.operateGroup(tbFront.getFrontIp(), tbFront.getFrontPort(), groupId, + "start"); + } + // refresh + resetGroupList(); + } + + + /** + * save group id + */ + @Transactional + public TbGroup saveGroup(int groupId, int nodeCount, String description, + int groupType) { + if (groupId == 0) { + return null; + } + // save group id + String groupName = "group" + groupId; + TbGroup tbGroup = + new TbGroup(groupId, groupName, nodeCount, description, groupType); + groupMapper.save(tbGroup); + // create table by group id + tableService.newTableByGroupId(groupId); + return tbGroup; + } + + /** + * Check the validity of the groupId. + */ + public void checkGroupIdExisted(Integer groupId) throws NodeMgrException { + log.debug("start checkGroupIdExisted groupId:{}", groupId); + + if (groupId == null) { + log.error("fail checkGroupIdExisted groupId is null"); + throw new NodeMgrException(ConstantCode.GROUP_ID_NULL); + } + + Integer groupCount = countOfGroup(groupId, null); + log.debug("checkGroupIdExisted groupId:{} groupCount:{}", groupId, groupCount); + if (groupCount != null && groupCount > 0) { + throw new NodeMgrException(ConstantCode.GROUP_ID_EXISTS); + } + log.debug("end checkGroupIdExisted"); + } + + /** + * Check the validity of the groupId. + */ + public void checkGroupIdValid(Integer groupId) throws NodeMgrException { + log.debug("start checkGroupIdValid groupId:{}", groupId); + + if (groupId == null) { + log.error("fail checkGroupIdValid groupId is null"); + throw new NodeMgrException(ConstantCode.GROUP_ID_NULL); + } + + Integer groupCount = countOfGroup(groupId, null); + log.debug("checkGroupIdValid groupId:{} groupCount:{}", groupId, groupCount); + if (groupCount == null || groupCount == 0) { + throw new NodeMgrException(ConstantCode.INVALID_GROUP_ID); + } + log.debug("end checkGroupIdValid"); + } + } diff --git a/src/main/java/com/webank/webase/node/mgr/group/entity/ReqBatchStartGroup.java b/src/main/java/com/webank/webase/node/mgr/group/entity/ReqBatchStartGroup.java new file mode 100644 index 000000000..996f7e1ee --- /dev/null +++ b/src/main/java/com/webank/webase/node/mgr/group/entity/ReqBatchStartGroup.java @@ -0,0 +1,37 @@ +/** + * Copyright 2014-2020 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.webank.webase.node.mgr.group.entity; + +import java.util.List; +import javax.validation.constraints.NotEmpty; +import javax.validation.constraints.NotNull; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * @author marsli + */ +@Data +@NoArgsConstructor +@AllArgsConstructor +public class ReqBatchStartGroup { + @NotNull + private Integer generateGroupId; + @NotEmpty + private List nodeList; +} \ No newline at end of file diff --git a/src/main/java/com/webank/webase/node/mgr/group/entity/ReqGenerateGroup.java b/src/main/java/com/webank/webase/node/mgr/group/entity/ReqGenerateGroup.java new file mode 100644 index 000000000..b9e3c5eb3 --- /dev/null +++ b/src/main/java/com/webank/webase/node/mgr/group/entity/ReqGenerateGroup.java @@ -0,0 +1,41 @@ +/** + * Copyright 2014-2020 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.webank.webase.node.mgr.group.entity; + +import java.math.BigInteger; +import java.util.List; +import javax.validation.constraints.NotEmpty; +import javax.validation.constraints.NotNull; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * @author marsli + */ +@Data +@NoArgsConstructor +@AllArgsConstructor +public class ReqGenerateGroup { + @NotNull + private Integer generateGroupId; + @NotNull + private BigInteger timestamp; + @NotEmpty + private List nodeList; + private String description; +} \ No newline at end of file diff --git a/src/main/java/com/webank/webase/node/mgr/group/entity/ReqOperateGroup.java b/src/main/java/com/webank/webase/node/mgr/group/entity/ReqOperateGroup.java new file mode 100644 index 000000000..6dff79242 --- /dev/null +++ b/src/main/java/com/webank/webase/node/mgr/group/entity/ReqOperateGroup.java @@ -0,0 +1,34 @@ +/** + * Copyright 2014-2020 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.webank.webase.node.mgr.group.entity; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class ReqOperateGroup { + @NotNull + private Integer generateGroupId; + @NotBlank + private String type; +} diff --git a/src/main/java/com/webank/webase/node/mgr/group/entity/TbGroup.java b/src/main/java/com/webank/webase/node/mgr/group/entity/TbGroup.java index be6558431..2b77fb9af 100644 --- a/src/main/java/com/webank/webase/node/mgr/group/entity/TbGroup.java +++ b/src/main/java/com/webank/webase/node/mgr/group/entity/TbGroup.java @@ -35,10 +35,17 @@ public class TbGroup { private BigInteger transCount = BigInteger.ZERO; private LocalDateTime createTime; private LocalDateTime modifyTime; + private String description; + private Integer groupType; - public TbGroup(Integer groupId, String groupName,Integer nodeCount){ + public TbGroup(Integer groupId, String groupName, Integer nodeCount, + String description, Integer groupType) { this.groupId = groupId; this.groupName = groupName; this.nodeCount = nodeCount; + this.description = description; + this.groupType = groupType; } + + } \ No newline at end of file From 680c301263e75e4a550048339d31d313ebd12435 Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Thu, 30 Apr 2020 18:08:56 +0800 Subject: [PATCH 006/131] update web3sdk 2.4.0 --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 0b018dfad..bb7186aec 100644 --- a/build.gradle +++ b/build.gradle @@ -39,7 +39,7 @@ List jaxb = [ ] List web3jsdk = [ - "org.fisco-bcos:web3sdk:2.2.0" + "org.fisco-bcos:web3sdk:2.4.0" ] List lombok = [ From d590915d2d02c0e10509bcffd3a21ab553cb74d3 Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Wed, 6 May 2020 17:03:06 +0800 Subject: [PATCH 007/131] add abi module --- script/webase-ddl.sql | 19 +- .../webase/node/mgr/abi/AbiController.java | 117 ++++++++++++ .../webank/webase/node/mgr/abi/AbiMapper.java | 45 +++++ .../webase/node/mgr/abi/AbiService.java | 174 ++++++++++++++++++ .../webase/node/mgr/abi/entity/AbiInfo.java | 45 +++++ .../node/mgr/abi/entity/ReqAbiListParam.java | 36 ++++ .../webase/node/mgr/abi/entity/ReqDelAbi.java | 17 ++ .../node/mgr/abi/entity/ReqImportAbi.java | 41 +++++ .../node/mgr/base/code/ConstantCode.java | 7 + .../node/mgr/base/tools/NodeMgrTools.java | 27 +++ src/main/resources/mapper/AbiMapper.xml | 89 +++++++++ 11 files changed, 616 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/webank/webase/node/mgr/abi/AbiController.java create mode 100644 src/main/java/com/webank/webase/node/mgr/abi/AbiMapper.java create mode 100644 src/main/java/com/webank/webase/node/mgr/abi/AbiService.java create mode 100644 src/main/java/com/webank/webase/node/mgr/abi/entity/AbiInfo.java create mode 100644 src/main/java/com/webank/webase/node/mgr/abi/entity/ReqAbiListParam.java create mode 100644 src/main/java/com/webank/webase/node/mgr/abi/entity/ReqDelAbi.java create mode 100644 src/main/java/com/webank/webase/node/mgr/abi/entity/ReqImportAbi.java create mode 100644 src/main/resources/mapper/AbiMapper.xml diff --git a/script/webase-ddl.sql b/script/webase-ddl.sql index b05ba0ef0..278fb65a1 100644 --- a/script/webase-ddl.sql +++ b/script/webase-ddl.sql @@ -106,7 +106,7 @@ CREATE TABLE IF NOT EXISTS tb_method( create_time datetime DEFAULT NULL COMMENT '创建时间', modify_time datetime DEFAULT NULL COMMENT '修改时间', PRIMARY KEY (method_id,group_id) -) COMMENT='方法解析信息表' ENGINE=InnoDB DEFAULT CHARSET=utf8; +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='方法解析信息表'; @@ -302,3 +302,20 @@ CREATE TABLE IF NOT EXISTS tb_alert_log ( PRIMARY KEY (log_id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='告警日志表'; +-- ---------------------------- +-- Table structure for tb_abi, unrelated with tb_contract +-- ---------------------------- +CREATE TABLE IF NOT EXISTS tb_abi ( + abi_id int(11) NOT NULL AUTO_INCREMENT COMMENT '合约ABI的编号', + group_id int(11) NOT NULL COMMENT '合约ABI所属群组的编号', + contract_name varchar(120) NOT NULL COMMENT '合约ABI的合约名', + contract_address varchar(64) NOT NULL COMMENT '合约ABI的合约地址', + contract_abi text NOT NULL COMMENT '合约ABI的内容', + contract_bin text NOT NULL COMMENT '合约ABI的runtime-bin', + create_time datetime DEFAULT NULL COMMENT '合约ABI的创建时间', + modify_time datetime DEFAULT NULL COMMENT '合约ABI的修改时间' + PRIMARY KEY (abi_id), + UNIQUE KEY unique_address (group_id,contract_address) + UNIQUE KEY unique_name (group_id,contract_name) +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='合约ABI表'; + diff --git a/src/main/java/com/webank/webase/node/mgr/abi/AbiController.java b/src/main/java/com/webank/webase/node/mgr/abi/AbiController.java new file mode 100644 index 000000000..fcb39e019 --- /dev/null +++ b/src/main/java/com/webank/webase/node/mgr/abi/AbiController.java @@ -0,0 +1,117 @@ +/** + * Copyright 2014-2020 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.webank.webase.node.mgr.abi; + +import com.alibaba.fastjson.JSON; +import com.webank.webase.node.mgr.abi.entity.AbiInfo; +import com.webank.webase.node.mgr.abi.entity.ReqAbiListParam; +import com.webank.webase.node.mgr.abi.entity.ReqImportAbi; +import com.webank.webase.node.mgr.base.code.ConstantCode; +import com.webank.webase.node.mgr.base.controller.BaseController; +import com.webank.webase.node.mgr.base.entity.BasePageResponse; +import com.webank.webase.node.mgr.base.entity.BaseResponse; +import com.webank.webase.node.mgr.base.enums.SqlSortType; +import com.webank.webase.node.mgr.base.properties.ConstantProperties; +import lombok.extern.log4j.Log4j2; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.annotation.*; + +import javax.validation.Valid; +import java.time.Duration; +import java.time.Instant; +import java.util.List; +import java.util.Optional; + +@Log4j2 +@RestController +@RequestMapping("abi") +public class AbiController extends BaseController { + @Autowired + AbiService abiService; + + @GetMapping("/list/{pageNumber}/{pageSize}") + public Object listAbi(@PathVariable("pageNumber") Integer pageNumber, + @PathVariable("pageSize") Integer pageSize) { + Instant startTime = Instant.now(); + if (pageNumber < 1 || pageSize <= 0) { + return new BaseResponse(ConstantCode.PARAM_EXCEPTION); + } + log.info("start listAbi. startTime:{},pageNumber:{},pageSize:{}", + startTime.toEpochMilli(), pageNumber, pageSize); + + Integer start = Optional.ofNullable(pageNumber).map(page -> (page - 1) * pageSize) + .orElse(0); + ReqAbiListParam param = new ReqAbiListParam(start, pageSize, + SqlSortType.DESC.getValue()); + List resList = abiService.getListByGroupId(param); + // total count + int count = abiService.countOfAbi(); + + log.info("end listAbi. useTime:{}, resList:{}", + Duration.between(startTime, Instant.now()).toMillis(), resList); + return new BasePageResponse(ConstantCode.SUCCESS, resList, count); + } + + @GetMapping("/{abiId}") + public Object getAbiById(@PathVariable("abiId") Integer abiId) { + Instant startTime = Instant.now(); + log.info("start getAbiById. startTime:{} abiId:{}", + startTime.toEpochMilli(), abiId); + AbiInfo res = abiService.getAbiById(abiId); + log.info("end getAbiById. useTime:{}, res:{}", + Duration.between(startTime, Instant.now()).toMillis(), res); + return new BaseResponse(ConstantCode.SUCCESS, res); + } + + @PostMapping("") + @PreAuthorize(ConstantProperties.HAS_ROLE_ADMIN) + public Object saveAbi(@Valid @RequestBody ReqImportAbi param, BindingResult result) { + checkBindResult(result); + Instant startTime = Instant.now(); + log.info("start saveAbi. startTime:{} ReqImportAbi:{}", + startTime.toEpochMilli(), JSON.toJSONString(param)); + abiService.saveAbi(param); + log.info("end saveAbi. useTime:{}", + Duration.between(startTime, Instant.now()).toMillis()); + return new BaseResponse(ConstantCode.SUCCESS); + } + + /** + * @param param abiId is not empty to update + * @return + */ + @PutMapping("") + @PreAuthorize(ConstantProperties.HAS_ROLE_ADMIN) + public Object updateAbi(@RequestBody ReqImportAbi param, BindingResult result) { + checkBindResult(result); + Instant startTime = Instant.now(); + log.info("start updateAbi. startTime:{} ReqImportAbi:{}", + startTime.toEpochMilli(), JSON.toJSONString(param)); + if(param.getAbiId() == null) { + return new BaseResponse(ConstantCode.PARAM_FAIL_ABI_ID_EMPTY); + } + abiService.saveAbi(param); + AbiInfo res = abiService.getAbiById(param.getAbiId()); + log.info("end updateAbi. useTime:{}, res:{}", + Duration.between(startTime, Instant.now()).toMillis(), res); + return new BaseResponse(ConstantCode.SUCCESS, res); + } + + +} diff --git a/src/main/java/com/webank/webase/node/mgr/abi/AbiMapper.java b/src/main/java/com/webank/webase/node/mgr/abi/AbiMapper.java new file mode 100644 index 000000000..9e8f76a9b --- /dev/null +++ b/src/main/java/com/webank/webase/node/mgr/abi/AbiMapper.java @@ -0,0 +1,45 @@ +/** + * Copyright 2014-2020 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.webank.webase.node.mgr.abi; + +import com.webank.webase.node.mgr.abi.entity.AbiInfo; +import com.webank.webase.node.mgr.abi.entity.ReqAbiListParam; +import org.apache.ibatis.annotations.Param; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public interface AbiMapper { + + List listOfAbi(@Param("param") ReqAbiListParam param); + + Integer countOfAbi(); + + AbiInfo queryByAbiId(@Param("abiId") int abiId); + + AbiInfo queryByGroupIdAndAddress(@Param("groupId") int groupId, + @Param("contractAddress") String contractAddress); + + AbiInfo queryByGroupIdAndContractName(@Param("groupId") int groupId, + @Param("contractName") String contractName); + void add(AbiInfo abiInfo); + + void update(AbiInfo abiInfo); + + void deleteByAbiId(@Param("abiId") int abiId); +} diff --git a/src/main/java/com/webank/webase/node/mgr/abi/AbiService.java b/src/main/java/com/webank/webase/node/mgr/abi/AbiService.java new file mode 100644 index 000000000..a9be1351f --- /dev/null +++ b/src/main/java/com/webank/webase/node/mgr/abi/AbiService.java @@ -0,0 +1,174 @@ +/** + * Copyright 2014-2020 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.webank.webase.node.mgr.abi; + +import com.alibaba.fastjson.JSON; +import com.webank.webase.node.mgr.abi.entity.AbiInfo; +import com.webank.webase.node.mgr.abi.entity.ReqAbiListParam; +import com.webank.webase.node.mgr.abi.entity.ReqImportAbi; +import com.webank.webase.node.mgr.base.code.ConstantCode; +import com.webank.webase.node.mgr.base.exception.NodeMgrException; +import com.webank.webase.node.mgr.base.tools.NodeMgrTools; +import com.webank.webase.node.mgr.frontinterface.FrontInterfaceService; +import lombok.extern.log4j.Log4j2; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.BeanUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.math.BigInteger; +import java.time.LocalDateTime; +import java.util.List; +import java.util.Objects; + +@Log4j2 +@Service +public class AbiService { + + @Autowired + AbiMapper abiMapper; + @Autowired + FrontInterfaceService frontInterfaceService; + + + public List getListByGroupId(ReqAbiListParam param) { + List abiList = abiMapper.listOfAbi(param); + return abiList; + } + + public void saveAbi(ReqImportAbi param) { + if (Objects.isNull(param.getAbiId())) { + insertAbiInfo(param); + } else { + updateAbiInfo(param); + } + } + + @Transactional + public void insertAbiInfo(ReqImportAbi param) { + int groupId = param.getGroupId(); + String contractName = param.getContractName(); + String contractAddress = param.getContractAddress(); + String contractAbiStr; + try { + contractAbiStr = JSON.toJSONString(param.getContractAbi()); + } catch (Exception e) { + log.warn("abi parse string error:{}", param.getContractAbi()); + throw new NodeMgrException(ConstantCode.PARAM_FAIL_ABI_INVALID); + } + // check address + String contractBin = getAddressRuntimeBin(groupId, contractAddress); + // check name and address of abi not exist + checkAbiExist(groupId, contractName, contractAddress); + + AbiInfo saveAbi = new AbiInfo(); + BeanUtils.copyProperties(param, saveAbi); + saveAbi.setContractAbi(contractAbiStr); + saveAbi.setContractBin(contractBin); + saveAbi.setCreateTime(LocalDateTime.now()); + saveAbi.setModifyTime(LocalDateTime.now()); + abiMapper.add(saveAbi); + } + + @Transactional + public void updateAbiInfo(ReqImportAbi param) { + Integer abiId = param.getAbiId(); + // check id exists + checkAbiIdExist(abiId); + // update + AbiInfo updateAbi = new AbiInfo(); + BeanUtils.copyProperties(param, updateAbi); + String contractAbiStr; + try { + contractAbiStr = JSON.toJSONString(param.getContractAbi()); + } catch (Exception e) { + log.warn("abi parse string error:{}", param.getContractAbi()); + throw new NodeMgrException(ConstantCode.PARAM_FAIL_ABI_INVALID); + } + // check address + String contractBin = getAddressRuntimeBin(param.getGroupId(), param.getContractAddress()); + updateAbi.setContractAbi(contractAbiStr); + updateAbi.setContractBin(contractBin); + updateAbi.setModifyTime(LocalDateTime.now()); + abiMapper.update(updateAbi); + } + + public void delete(Integer id) { + checkAbiIdExist(id); + abiMapper.deleteByAbiId(id); + } + + private void checkAbiExist(int groupId, String contractName, String address) { + AbiInfo checkAbiName = abiMapper.queryByGroupIdAndContractName(groupId, contractName); + if (Objects.nonNull(checkAbiName)) { + throw new NodeMgrException(ConstantCode.CONTRACT_NAME_REPEAT); + } + AbiInfo checkAbiAddressExist = abiMapper.queryByGroupIdAndAddress(groupId, address); + if (Objects.nonNull(checkAbiAddressExist)) { + throw new NodeMgrException(ConstantCode.CONTRACT_ADDRESS_ALREADY_EXISTS); + } + } + + public AbiInfo getAbiById(Integer abiId) { + return abiMapper.queryByAbiId(abiId); + } + + private void checkAbiIdExist(Integer abiId) { + AbiInfo checkAbiId = getAbiById(abiId); + if (Objects.isNull(checkAbiId)) { + throw new NodeMgrException(ConstantCode.ABI_INFO_NOT_EXISTS); + } + } + + /** + * check address is valid. + * @return address's runtime bin + */ + private String getAddressRuntimeBin(int groupId, String contractAddress) { + if (StringUtils.isBlank(contractAddress)) { + log.error("fail getAddressRuntimeBin. contractAddress is empty"); + throw new NodeMgrException(ConstantCode.CONTRACT_ADDRESS_NULL); + } + String binOnChain; + try { + binOnChain = frontInterfaceService.getCodeFromFront(groupId, contractAddress, BigInteger.ZERO); + } catch (Exception e) { + log.error("fail getAddressRuntimeBin.", e); + throw new NodeMgrException(ConstantCode.CONTRACT_ADDRESS_INVALID); + } + log.info("getAddressRuntimeBin address:{} binOnChain:{}", contractAddress, binOnChain); + String runtimeBin = NodeMgrTools.removeFirstStr(binOnChain, "0x"); + if (StringUtils.isBlank(runtimeBin)) { + log.error("fail getAddressRuntimeBin. runtimeBin is null, address:{}", contractAddress); + throw new NodeMgrException(ConstantCode.CONTRACT_NOT_DEPLOY); + } + return runtimeBin; + } + + public int countOfAbi() { + log.debug("start countOfAbi "); + try { + int count = abiMapper.countOfAbi(); + log.debug("end countOfAbi count:{}", count); + return count; + }catch (Exception e) { + log.error("countOfAbi error exception:[]", e); + throw new NodeMgrException(ConstantCode.DB_EXCEPTION.getCode(), + e.getMessage()); + } + }} diff --git a/src/main/java/com/webank/webase/node/mgr/abi/entity/AbiInfo.java b/src/main/java/com/webank/webase/node/mgr/abi/entity/AbiInfo.java new file mode 100644 index 000000000..16b361e63 --- /dev/null +++ b/src/main/java/com/webank/webase/node/mgr/abi/entity/AbiInfo.java @@ -0,0 +1,45 @@ +/** + * Copyright 2014-2020 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.webank.webase.node.mgr.abi.entity; + +import lombok.Data; + +import java.time.LocalDateTime; + +/** + * entity for table tb_abi to store raw contract abi + */ +@Data +public class AbiInfo { + private Integer abiId; + private Integer groupId; + private String contractName; + private String contractAddress; + private String contractAbi; + /** + * runtime bin + */ + private String contractBin; + /** + * 1-normal, 2-invalid + */ + // private Integer contractStatus; + private LocalDateTime createTime; + private LocalDateTime modifyTime; + +} + diff --git a/src/main/java/com/webank/webase/node/mgr/abi/entity/ReqAbiListParam.java b/src/main/java/com/webank/webase/node/mgr/abi/entity/ReqAbiListParam.java new file mode 100644 index 000000000..8476df99e --- /dev/null +++ b/src/main/java/com/webank/webase/node/mgr/abi/entity/ReqAbiListParam.java @@ -0,0 +1,36 @@ +/** + * Copyright 2014-2020 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.webank.webase.node.mgr.abi.entity; + +import com.webank.webase.node.mgr.base.entity.BaseQueryParam; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; +import lombok.ToString; + +@Data +@NoArgsConstructor +@ToString(callSuper = true) +@EqualsAndHashCode(callSuper = true) +public class ReqAbiListParam extends BaseQueryParam { + + private Integer groupId; + public ReqAbiListParam(Integer start, Integer pageSize, + String flagSortedByTime) { + super(start, pageSize, flagSortedByTime); + } +} diff --git a/src/main/java/com/webank/webase/node/mgr/abi/entity/ReqDelAbi.java b/src/main/java/com/webank/webase/node/mgr/abi/entity/ReqDelAbi.java new file mode 100644 index 000000000..c6e25371f --- /dev/null +++ b/src/main/java/com/webank/webase/node/mgr/abi/entity/ReqDelAbi.java @@ -0,0 +1,17 @@ +package com.webank.webase.node.mgr.abi.entity; + +import lombok.Data; +import lombok.NoArgsConstructor; + +import javax.validation.constraints.NotNull; + +/** + * entity to delete abi info by id + * @author marsli + */ +@Data +@NoArgsConstructor +public class ReqDelAbi { + @NotNull + private Long abiId; +} \ No newline at end of file diff --git a/src/main/java/com/webank/webase/node/mgr/abi/entity/ReqImportAbi.java b/src/main/java/com/webank/webase/node/mgr/abi/entity/ReqImportAbi.java new file mode 100644 index 000000000..a803e471b --- /dev/null +++ b/src/main/java/com/webank/webase/node/mgr/abi/entity/ReqImportAbi.java @@ -0,0 +1,41 @@ +/** + * Copyright 2014-2020 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.webank.webase.node.mgr.abi.entity; + +import lombok.Data; +import lombok.NoArgsConstructor; + +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; +import java.util.List; + +/** + * entity to import/update abi info + */ +@Data +@NoArgsConstructor +public class ReqImportAbi { + private Integer abiId; + @NotNull + private Integer groupId; + @NotBlank + private String contractName; + @NotBlank + private String contractAddress; + @NotNull + private List contractAbi; +} diff --git a/src/main/java/com/webank/webase/node/mgr/base/code/ConstantCode.java b/src/main/java/com/webank/webase/node/mgr/base/code/ConstantCode.java index 7d5007cc0..8546a60af 100644 --- a/src/main/java/com/webank/webase/node/mgr/base/code/ConstantCode.java +++ b/src/main/java/com/webank/webase/node/mgr/base/code/ConstantCode.java @@ -143,6 +143,13 @@ public class ConstantCode { public static final RetCode ENCRYPT_TYPE_NOT_MATCH = RetCode.mark(202091, "Front's encrypt type not matches with nodemgr"); + // abi import + public static final RetCode CONTRACT_ADDRESS_ALREADY_EXISTS = RetCode.mark(202096, "contract address already exists"); + public static final RetCode ABI_INFO_NOT_EXISTS = RetCode.mark(202097, "abi info of this id not exists"); + public static final RetCode PARAM_FAIL_ABI_INVALID = RetCode.mark(202098, "Contract abi invalid, please check abi"); + public static final RetCode PARAM_FAIL_ABI_ID_EMPTY = RetCode.mark(202099, "Abi Id cannot be empty"); + public static final RetCode CONTRACT_ADDRESS_NULL = RetCode.mark(202100, "contractAddress is null"); + /* dynamic group manage */ public static final RetCode GROUP_ID_EXISTS = RetCode.mark(202300, "group id already exists"); public static final RetCode NODE_NOT_EXISTS = RetCode.mark(202301, "node front not exists"); diff --git a/src/main/java/com/webank/webase/node/mgr/base/tools/NodeMgrTools.java b/src/main/java/com/webank/webase/node/mgr/base/tools/NodeMgrTools.java index 4c5de9f3e..51ba993ec 100644 --- a/src/main/java/com/webank/webase/node/mgr/base/tools/NodeMgrTools.java +++ b/src/main/java/com/webank/webase/node/mgr/base/tools/NodeMgrTools.java @@ -485,4 +485,31 @@ public static int parseHexStr2Int(String str) { } return Integer.parseInt(str.substring(2), 16); } + + /** + * remove "0x" and last character. + */ + public static String removeBinFirstAndLast(String contractBin, int removaLastLength) { + if (StringUtils.isBlank(contractBin)) { + return null; + } + contractBin = removeFirstStr(contractBin, "0x"); + if (contractBin.length() > removaLastLength) { + contractBin = contractBin.substring(0, contractBin.length() - removaLastLength); + } + return contractBin; + } + + /** + * remove fist string. + */ + public static String removeFirstStr(String constant, String target) { + if (StringUtils.isBlank(constant) || StringUtils.isBlank(target)) { + return constant; + } + if (constant.startsWith(target)) { + constant = StringUtils.removeStart(constant, target); + } + return constant; + } } diff --git a/src/main/resources/mapper/AbiMapper.xml b/src/main/resources/mapper/AbiMapper.xml new file mode 100644 index 000000000..f42f09b15 --- /dev/null +++ b/src/main/resources/mapper/AbiMapper.xml @@ -0,0 +1,89 @@ + + + + + + + + + + + + + + + + + REPLACE INTO tb_abi(group_id,contract_name,contract_address,contract_abi,contract_bin, + create_time,modify_time) + VALUES (#{groupId},#{contractName},#{contractAddress},#{contractAbi},#{contractBin}, now(), now()) + + + + delete from tb_abi + where abi_id = #{abiId} + + + + update tb_abi set modify_time = now() + + ,contract_name = #{contractName} + + + ,contract_address = #{contractAddress} + + + ,contract_abi = #{contractAbi} + + + ,contract_bin = #{contractBin} + + where abi_id = #{abiId} + + + + + + + + + + + \ No newline at end of file From 9b013d58b28c806614b4eacacb51e2a86ddc4416 Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Wed, 6 May 2020 17:10:04 +0800 Subject: [PATCH 008/131] optimize alert code --- .../node/mgr/alert/log/AlertLogController.java | 6 ++---- .../node/mgr/alert/mail/MailController.java | 6 +++--- .../webase/node/mgr/alert/mail/MailService.java | 16 +++++----------- .../node/mgr/alert/rule/AlertRuleController.java | 6 +++--- .../webase/node/mgr/base/code/ConstantCode.java | 2 +- 5 files changed, 14 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/webank/webase/node/mgr/alert/log/AlertLogController.java b/src/main/java/com/webank/webase/node/mgr/alert/log/AlertLogController.java index b934dfb59..b0e70aa80 100644 --- a/src/main/java/com/webank/webase/node/mgr/alert/log/AlertLogController.java +++ b/src/main/java/com/webank/webase/node/mgr/alert/log/AlertLogController.java @@ -63,7 +63,7 @@ public Object listAlertLog(@PathVariable("pageNumber") Integer pageNumber, Duration.between(startTime, Instant.now()).toMillis(), resList); return new BasePageResponse(ConstantCode.SUCCESS, resList, count); }catch (NodeMgrException e) { - log.debug("listAlertLog, error, exception:[] ", e); + log.error("listAlertLog, error, exception:[] ", e); return new BaseResponse(ConstantCode.ALERT_LOG_ERROR, e.getMessage()); } } @@ -80,14 +80,12 @@ public Object updateAlertLog(@RequestBody ReqLogParam param) { log.info("start updateAlertLog. startTime:{} ReqAlertLogParam:{}", startTime.toEpochMilli(), JSON.toJSONString(param)); if(param.getStatus() == null || param.getLogId() == null) { - log.debug("updateAlertLog, error:{} ", - ConstantCode.ALERT_LOG_PARAM_EMPTY); return new BaseResponse(ConstantCode.ALERT_LOG_PARAM_EMPTY); } try{ alertLogService.updateAlertLog(param); }catch (NodeMgrException e) { - log.debug("updateAlertLog, error, exception:[] ", e); + log.error("updateAlertLog, error, exception:[] ", e); return new BaseResponse(ConstantCode.ALERT_LOG_ERROR, e.getMessage()); } AlertLog res = alertLogService.queryByLogId(param.getLogId()); diff --git a/src/main/java/com/webank/webase/node/mgr/alert/mail/MailController.java b/src/main/java/com/webank/webase/node/mgr/alert/mail/MailController.java index 06524bc31..629cfc36c 100644 --- a/src/main/java/com/webank/webase/node/mgr/alert/mail/MailController.java +++ b/src/main/java/com/webank/webase/node/mgr/alert/mail/MailController.java @@ -71,7 +71,7 @@ public Object sendTestMail(@PathVariable("toMailAddress")String toMailAddress, try{ checkParamEmpty(reqMailServerConfigParam); }catch (NodeMgrException e){ - return new BaseResponse(ConstantCode.MAIL_SERVER_CONFIG__PARAM_EMPTY); + return new BaseResponse(ConstantCode.MAIL_SERVER_CONFIG_PARAM_EMPTY); } // get configuration from web and refresh JavaMailSender mailService.refreshJavaMailSenderConfigFromWeb(reqMailServerConfigParam); @@ -101,13 +101,13 @@ public void checkParamEmpty(ReqMailServerConfigParam reqMailServerConfigParam) { reqMailServerConfigParam.getAuthentication() == null || StringUtils.isEmpty(reqMailServerConfigParam.getHost())) { log.error("error checkParamEmpty reqMailServerConfigParam:{}", reqMailServerConfigParam); - throw new NodeMgrException(ConstantCode.MAIL_SERVER_CONFIG__PARAM_EMPTY); + throw new NodeMgrException(ConstantCode.MAIL_SERVER_CONFIG_PARAM_EMPTY); } if(reqMailServerConfigParam.getAuthentication() == EnableStatus.ON.getValue()) { if(StringUtils.isEmpty(reqMailServerConfigParam.getUsername()) || StringUtils.isEmpty(reqMailServerConfigParam.getPassword())) { log.error("error checkParamEmpty in auth reqMailServerConfigParam:{}", reqMailServerConfigParam); - throw new NodeMgrException(ConstantCode.MAIL_SERVER_CONFIG__PARAM_EMPTY); + throw new NodeMgrException(ConstantCode.MAIL_SERVER_CONFIG_PARAM_EMPTY); } } log.debug("end checkParamEmpty reqMailServerConfigParam:{}", reqMailServerConfigParam); diff --git a/src/main/java/com/webank/webase/node/mgr/alert/mail/MailService.java b/src/main/java/com/webank/webase/node/mgr/alert/mail/MailService.java index 116413a5e..5f9395e67 100644 --- a/src/main/java/com/webank/webase/node/mgr/alert/mail/MailService.java +++ b/src/main/java/com/webank/webase/node/mgr/alert/mail/MailService.java @@ -180,21 +180,15 @@ public void sendMailByRule(int ruleId, String replacementText) { TbMailServerConfig latestMailServerConfig = mailServerConfigService.getLatestMailServerConfig(); // if mail server not turn ON if(latestMailServerConfig.getEnable() == EnableStatus.OFF.getValue()) { - log.error("end sendMailByRule for server config not enable:{}", latestMailServerConfig); + log.warn("end sendMailByRule for server config not enable:{}", latestMailServerConfig); return; } TbAlertRule alertRule = alertRuleMapper.queryByRuleId(ruleId); // if alert not activated if(alertRule.getEnable() == EnableStatus.OFF.getValue()) { - log.debug("end sendMailByRule non-sending mail for alertRule not enabled:{}", alertRule); + log.warn("end sendMailByRule non-sending mail for alertRule not enabled:{}", alertRule); return; } - // last time alert by now, if within interval, not send - // 告警间隔时间的刷新放到遍历group异常的for循环外面 -// if(isWithinAlertIntervalByNow(alertRule)) { -// log.debug("end sendMailByRule non-sending mail for beyond alert interval:{}", alertRule); -// return; -// } // if userList is empty or default email if(StringUtils.isEmpty(alertRule.getUserList())) { log.error("end sendMailByRule for no receive mail address:{}", alertRule); @@ -246,13 +240,13 @@ public void sendMailByRule(int ruleId, List replacementTextList) { TbMailServerConfig latestMailServerConfig = mailServerConfigService.getLatestMailServerConfig(); // if mail server not turn ON if(latestMailServerConfig.getEnable() == EnableStatus.OFF.getValue()) { - log.error("end sendMailByRule for server config not enable:{}", latestMailServerConfig); + log.warn("end sendMailByRule for server config not enable:{}", latestMailServerConfig); return; } TbAlertRule alertRule = alertRuleMapper.queryByRuleId(ruleId); // if alert not activated if(alertRule.getEnable() == EnableStatus.OFF.getValue()) { - log.debug("end sendMailByRule non-sending mail for alertRule not enabled:{}", alertRule); + log.warn("end sendMailByRule non-sending mail for alertRule not enabled:{}", alertRule); return; } // last time alert by now, if within interval, not send @@ -329,7 +323,7 @@ public void handleAllUserEmail(TbAlertRule alertRule, String emailTitle, String try { userList = (List) JSON.parse(alertRule.getUserList()); }catch (Exception e) { - log.error("handleAllUserEmail parse error: e:[], getUserList{}", + log.error("handleAllUserEmail parse error: e:{}, getUserList{}", e, alertRule.getUserList()); } for(String userMailAddress: userList) { diff --git a/src/main/java/com/webank/webase/node/mgr/alert/rule/AlertRuleController.java b/src/main/java/com/webank/webase/node/mgr/alert/rule/AlertRuleController.java index 7c33355af..267d5f228 100644 --- a/src/main/java/com/webank/webase/node/mgr/alert/rule/AlertRuleController.java +++ b/src/main/java/com/webank/webase/node/mgr/alert/rule/AlertRuleController.java @@ -69,7 +69,7 @@ public Object listAlertRules() { Duration.between(startTime, Instant.now()).toMillis(), resList); return new BaseResponse(ConstantCode.SUCCESS, resList); }catch (NodeMgrException e) { - log.debug("listAlertRules, error, exception:[] ", e); + log.error("listAlertRules, error, exception:[] ", e); return new BaseResponse(ConstantCode.ALERT_RULE_ERROR, e.getMessage()); } } @@ -103,7 +103,7 @@ public Object updateAlertRule(@RequestBody ReqAlertRuleParam param) { try{ alertRuleService.updateAlertRule(param); }catch (NodeMgrException e) { - log.debug("updateAlertRule, error, exception:[] ", e); + log.error("updateAlertRule, error, exception:[] ", e); return new BaseResponse(ConstantCode.ALERT_RULE_ERROR, e.getMessage()); } TbAlertRule res = alertRuleService.queryByRuleId(param.getRuleId()); @@ -131,7 +131,7 @@ public Object toggleAlertRule(@RequestBody ReqAlertRuleParam param) { try{ alertRuleService.updateAlertRule(param); }catch (NodeMgrException e) { - log.debug("toggleAlertRule error, exception:[] ", e); + log.error("toggleAlertRule error, exception:[] ", e); return new BaseResponse(ConstantCode.ALERT_RULE_ERROR, e.getMessage()); } TbAlertRule res = alertRuleService.queryByRuleId(param.getRuleId()); diff --git a/src/main/java/com/webank/webase/node/mgr/base/code/ConstantCode.java b/src/main/java/com/webank/webase/node/mgr/base/code/ConstantCode.java index 8546a60af..98b7cd7c4 100644 --- a/src/main/java/com/webank/webase/node/mgr/base/code/ConstantCode.java +++ b/src/main/java/com/webank/webase/node/mgr/base/code/ConstantCode.java @@ -124,7 +124,7 @@ public class ConstantCode { // 邮件告警错误 public static final RetCode MAIL_SERVER_CONFIG_ERROR = RetCode.mark(202070, "Mail server config error."); - public static final RetCode MAIL_SERVER_CONFIG__PARAM_EMPTY = RetCode.mark(202071, + public static final RetCode MAIL_SERVER_CONFIG_PARAM_EMPTY = RetCode.mark(202071, "Mail server config param empty/not match."); public static final RetCode MAIL_SERVER_CONFIG_ERROR_NO_DATA_IN_DB = RetCode.mark(202072, "Mail server config error, db's server config is empty"); From 4c56f8c9145d12e78ba38a50ebe6eba7d5815e6e Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Wed, 6 May 2020 17:55:32 +0800 Subject: [PATCH 009/131] fix alert error code --- .../alert/mail/server/config/MailServerConfigController.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/webank/webase/node/mgr/alert/mail/server/config/MailServerConfigController.java b/src/main/java/com/webank/webase/node/mgr/alert/mail/server/config/MailServerConfigController.java index ce6a2b5f4..587af27c1 100644 --- a/src/main/java/com/webank/webase/node/mgr/alert/mail/server/config/MailServerConfigController.java +++ b/src/main/java/com/webank/webase/node/mgr/alert/mail/server/config/MailServerConfigController.java @@ -86,8 +86,8 @@ public Object updateMailServerConfig(@RequestBody ReqMailServerConfigParam param startTime.toEpochMilli(), JSON.toJSONString(param)); if(param.getServerId() == null) { log.debug("updateMailServerConfig, error:{} ", - ConstantCode.MAIL_SERVER_CONFIG__PARAM_EMPTY); - return new BaseResponse(ConstantCode.MAIL_SERVER_CONFIG__PARAM_EMPTY); + ConstantCode.MAIL_SERVER_CONFIG_PARAM_EMPTY); + return new BaseResponse(ConstantCode.MAIL_SERVER_CONFIG_PARAM_EMPTY); } try{ mailServerConfigService.updateMailServerConfig(param); From 7c396d5f241b42f124b0c12cfb39d210548f1518 Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Wed, 6 May 2020 18:00:24 +0800 Subject: [PATCH 010/131] fix sql syntax --- script/webase-ddl.sql | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/script/webase-ddl.sql b/script/webase-ddl.sql index 278fb65a1..688a4aa57 100644 --- a/script/webase-ddl.sql +++ b/script/webase-ddl.sql @@ -313,9 +313,9 @@ CREATE TABLE IF NOT EXISTS tb_abi ( contract_abi text NOT NULL COMMENT '合约ABI的内容', contract_bin text NOT NULL COMMENT '合约ABI的runtime-bin', create_time datetime DEFAULT NULL COMMENT '合约ABI的创建时间', - modify_time datetime DEFAULT NULL COMMENT '合约ABI的修改时间' + modify_time datetime DEFAULT NULL COMMENT '合约ABI的修改时间', PRIMARY KEY (abi_id), - UNIQUE KEY unique_address (group_id,contract_address) + UNIQUE KEY unique_address (group_id,contract_address), UNIQUE KEY unique_name (group_id,contract_name) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='合约ABI表'; From 82d8aab5bad224cb2b8edefbac1233c88a63ecc7 Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Wed, 6 May 2020 19:47:26 +0800 Subject: [PATCH 011/131] fix abi mapper & add abi test --- .../webase/node/mgr/abi/AbiController.java | 29 ++-- .../webase/node/mgr/abi/entity/AbiInfo.java | 2 + .../webase/node/mgr/abi/entity/ReqDelAbi.java | 4 +- src/main/resources/mapper/AbiMapper.xml | 7 +- .../node/mgr/test/abi/AbiControllerTest.java | 136 ++++++++++++++++++ .../node/mgr/test/abi/AbiServiceTest.java | 73 ++++++++++ 6 files changed, 238 insertions(+), 13 deletions(-) create mode 100644 src/test/java/node/mgr/test/abi/AbiControllerTest.java create mode 100644 src/test/java/node/mgr/test/abi/AbiServiceTest.java diff --git a/src/main/java/com/webank/webase/node/mgr/abi/AbiController.java b/src/main/java/com/webank/webase/node/mgr/abi/AbiController.java index fcb39e019..7c51d5aab 100644 --- a/src/main/java/com/webank/webase/node/mgr/abi/AbiController.java +++ b/src/main/java/com/webank/webase/node/mgr/abi/AbiController.java @@ -19,6 +19,7 @@ import com.alibaba.fastjson.JSON; import com.webank.webase.node.mgr.abi.entity.AbiInfo; import com.webank.webase.node.mgr.abi.entity.ReqAbiListParam; +import com.webank.webase.node.mgr.abi.entity.ReqDelAbi; import com.webank.webase.node.mgr.abi.entity.ReqImportAbi; import com.webank.webase.node.mgr.base.code.ConstantCode; import com.webank.webase.node.mgr.base.controller.BaseController; @@ -45,20 +46,23 @@ public class AbiController extends BaseController { @Autowired AbiService abiService; - @GetMapping("/list/{pageNumber}/{pageSize}") - public Object listAbi(@PathVariable("pageNumber") Integer pageNumber, - @PathVariable("pageSize") Integer pageSize) { + @GetMapping("/list/{groupId}/{pageNumber}/{pageSize}") + public Object listAbi( + @PathVariable("groupId") Integer groupId, + @PathVariable("pageNumber") Integer pageNumber, + @PathVariable("pageSize") Integer pageSize) { Instant startTime = Instant.now(); if (pageNumber < 1 || pageSize <= 0) { return new BaseResponse(ConstantCode.PARAM_EXCEPTION); } - log.info("start listAbi. startTime:{},pageNumber:{},pageSize:{}", - startTime.toEpochMilli(), pageNumber, pageSize); + log.info("start listAbi. startTime:{},groupId:{},pageNumber:{},pageSize:{}", + startTime.toEpochMilli(), groupId, pageNumber, pageSize); Integer start = Optional.ofNullable(pageNumber).map(page -> (page - 1) * pageSize) .orElse(0); ReqAbiListParam param = new ReqAbiListParam(start, pageSize, SqlSortType.DESC.getValue()); + param.setGroupId(groupId); List resList = abiService.getListByGroupId(param); // total count int count = abiService.countOfAbi(); @@ -80,7 +84,7 @@ public Object getAbiById(@PathVariable("abiId") Integer abiId) { } @PostMapping("") - @PreAuthorize(ConstantProperties.HAS_ROLE_ADMIN) +// @PreAuthorize(ConstantProperties.HAS_ROLE_ADMIN) public Object saveAbi(@Valid @RequestBody ReqImportAbi param, BindingResult result) { checkBindResult(result); Instant startTime = Instant.now(); @@ -97,7 +101,7 @@ public Object saveAbi(@Valid @RequestBody ReqImportAbi param, BindingResult resu * @return */ @PutMapping("") - @PreAuthorize(ConstantProperties.HAS_ROLE_ADMIN) +// @PreAuthorize(ConstantProperties.HAS_ROLE_ADMIN) public Object updateAbi(@RequestBody ReqImportAbi param, BindingResult result) { checkBindResult(result); Instant startTime = Instant.now(); @@ -113,5 +117,14 @@ public Object updateAbi(@RequestBody ReqImportAbi param, BindingResult result) { return new BaseResponse(ConstantCode.SUCCESS, res); } - + @DeleteMapping("") + @PreAuthorize(ConstantProperties.HAS_ROLE_ADMIN) + public BaseResponse deleteAbi(@Valid @RequestBody ReqDelAbi param, BindingResult result) { + checkBindResult(result); + Integer abiId = param.getAbiId(); + log.debug("start deleteAbi. abiId:{}", abiId); + abiService.delete(abiId); + log.debug("end deleteAbi"); + return new BaseResponse(ConstantCode.SUCCESS); + } } diff --git a/src/main/java/com/webank/webase/node/mgr/abi/entity/AbiInfo.java b/src/main/java/com/webank/webase/node/mgr/abi/entity/AbiInfo.java index 16b361e63..4e718b93b 100644 --- a/src/main/java/com/webank/webase/node/mgr/abi/entity/AbiInfo.java +++ b/src/main/java/com/webank/webase/node/mgr/abi/entity/AbiInfo.java @@ -17,6 +17,7 @@ package com.webank.webase.node.mgr.abi.entity; import lombok.Data; +import lombok.NoArgsConstructor; import java.time.LocalDateTime; @@ -24,6 +25,7 @@ * entity for table tb_abi to store raw contract abi */ @Data +@NoArgsConstructor public class AbiInfo { private Integer abiId; private Integer groupId; diff --git a/src/main/java/com/webank/webase/node/mgr/abi/entity/ReqDelAbi.java b/src/main/java/com/webank/webase/node/mgr/abi/entity/ReqDelAbi.java index c6e25371f..a59dd5252 100644 --- a/src/main/java/com/webank/webase/node/mgr/abi/entity/ReqDelAbi.java +++ b/src/main/java/com/webank/webase/node/mgr/abi/entity/ReqDelAbi.java @@ -1,5 +1,6 @@ package com.webank.webase.node.mgr.abi.entity; +import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @@ -11,7 +12,8 @@ */ @Data @NoArgsConstructor +@AllArgsConstructor public class ReqDelAbi { @NotNull - private Long abiId; + private Integer abiId; } \ No newline at end of file diff --git a/src/main/resources/mapper/AbiMapper.xml b/src/main/resources/mapper/AbiMapper.xml index f42f09b15..17386bacb 100644 --- a/src/main/resources/mapper/AbiMapper.xml +++ b/src/main/resources/mapper/AbiMapper.xml @@ -23,7 +23,7 @@ - + REPLACE INTO tb_abi(group_id,contract_name,contract_address,contract_abi,contract_bin, create_time,modify_time) VALUES (#{groupId},#{contractName},#{contractAddress},#{contractAbi},#{contractBin}, now(), now()) @@ -34,7 +34,7 @@ where abi_id = #{abiId} - + update tb_abi set modify_time = now() ,contract_name = #{contractName} @@ -54,8 +54,7 @@ + - select a.group_id,b.maxDay,c.block_number,c.trans_count from tb_group a LEFT JOIN (select group_id,max(trans_day) as maxDay from tb_trans_daily GROUP BY group_id)b From 6867b1e35a4e51a08cf42e7d5a8c1f6f1d14965a Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Tue, 12 May 2020 15:55:28 +0800 Subject: [PATCH 030/131] judge before update group --- .../com/webank/webase/node/mgr/front/FrontService.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/webank/webase/node/mgr/front/FrontService.java b/src/main/java/com/webank/webase/node/mgr/front/FrontService.java index 2bf457c0d..f56019312 100644 --- a/src/main/java/com/webank/webase/node/mgr/front/FrontService.java +++ b/src/main/java/com/webank/webase/node/mgr/front/FrontService.java @@ -29,6 +29,7 @@ import com.webank.webase.node.mgr.frontinterface.FrontInterfaceService; import com.webank.webase.node.mgr.frontinterface.entity.SyncStatus; import com.webank.webase.node.mgr.group.GroupService; +import com.webank.webase.node.mgr.group.entity.TbGroup; import com.webank.webase.node.mgr.node.NodeParam; import com.webank.webase.node.mgr.node.NodeService; import com.webank.webase.node.mgr.node.TbNode; @@ -38,6 +39,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Objects; import lombok.extern.log4j.Log4j2; import org.apache.commons.lang3.StringUtils; @@ -131,8 +133,12 @@ public TbFront newFront(FrontInfo frontInfo) { .getPeersFromSpecificFront(frontIp, frontPort, group); List peerList = Arrays.asList(peerArr); //add group - groupService.saveGroup(group, groupPeerList.size(), "synchronous", - GroupType.SYNC.getValue(), DataStatus.NORMAL.getValue()); + // check group not existed or node count differs + TbGroup checkGroup = groupService.getGroupById(group); + if (Objects.isNull(checkGroup) || groupPeerList.size() != checkGroup.getNodeCount()) { + groupService.saveGroup(group, groupPeerList.size(), "synchronous", + GroupType.SYNC.getValue(), DataStatus.NORMAL.getValue()); + } //save front group map frontGroupMapService.newFrontGroup(tbFront.getFrontId(), group); //save nodes From 06e95e8089e1c27a9c62d83021861b12524f6c6e Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Tue, 12 May 2020 16:06:14 +0800 Subject: [PATCH 031/131] add groupId validation --- .../webase/node/mgr/group/GroupService.java | 23 +++---------------- 1 file changed, 3 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/webank/webase/node/mgr/group/GroupService.java b/src/main/java/com/webank/webase/node/mgr/group/GroupService.java index 4bb30f292..e406f17ff 100644 --- a/src/main/java/com/webank/webase/node/mgr/group/GroupService.java +++ b/src/main/java/com/webank/webase/node/mgr/group/GroupService.java @@ -123,6 +123,7 @@ public void updateGroupStatus(int groupId, int groupStatus) { /** * Check the validity of the groupId. + * @throws NodeMgrException INVALID_GROUP_ID */ public void checkGroupId(Integer groupId) throws NodeMgrException { log.debug("start checkGroupId groupId:{}", groupId); @@ -362,6 +363,7 @@ protected void removeAllDataByGroupId(int groupId) { if (groupId == 0) { return; } + checkGroupId(groupId); log.warn("removeAllDataByGroupId! groupId:{}", groupId); //remove groupId. groupMapper.remove(groupId); @@ -508,7 +510,7 @@ public void batchStartGroup(ReqBatchStartGroup req) { log.debug("start batchStartGroup:{}", req); Integer groupId = req.getGenerateGroupId(); // check id - checkGroupIdValid(groupId); + checkGroupId(groupId); for (String nodeId : req.getNodeList()) { // get front TbFront tbFront = frontService.getByNodeId(nodeId); @@ -584,25 +586,6 @@ public boolean checkGroupIdExisted(Integer groupId) throws NodeMgrException { return false; } - /** - * Check the validity of the groupId. - */ - public void checkGroupIdValid(Integer groupId) throws NodeMgrException { - log.debug("start checkGroupIdValid groupId:{}", groupId); - - if (groupId == null) { - log.error("fail checkGroupIdValid groupId is null"); - throw new NodeMgrException(ConstantCode.GROUP_ID_NULL); - } - - Integer groupCount = countOfGroup(groupId, null); - log.debug("checkGroupIdValid groupId:{} groupCount:{}", groupId, groupCount); - if (groupCount == null || groupCount == 0) { - throw new NodeMgrException(ConstantCode.INVALID_GROUP_ID); - } - log.debug("end checkGroupIdValid"); - } - public TbGroup getGroupById(Integer groupId) { log.debug("getGroupById groupId:{}", groupId); return groupMapper.getGroupById(groupId); From eb0b50219074764b71963c914c0ad2cc914f3b8a Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Tue, 12 May 2020 16:16:53 +0800 Subject: [PATCH 032/131] alter tb_group timestamp to groupTimestamp --- script/webase-ddl.sql | 2 ++ .../java/com/webank/webase/node/mgr/group/GroupService.java | 2 +- .../java/com/webank/webase/node/mgr/group/entity/TbGroup.java | 2 +- src/main/resources/mapper/GroupMapper.xml | 4 ++++ 4 files changed, 8 insertions(+), 2 deletions(-) diff --git a/script/webase-ddl.sql b/script/webase-ddl.sql index 688a4aa57..57f08734a 100644 --- a/script/webase-ddl.sql +++ b/script/webase-ddl.sql @@ -9,6 +9,8 @@ CREATE TABLE IF NOT EXISTS tb_group ( node_count int DEFAULT '0' COMMENT '群组下节点数', description varchar(1024) COMMENT '群组描述', group_type int COMMENT '群组类型(1-拉取,2-动态创建)', + group_timestamp varchar(64) COMMENT '群组创世块时间戳', + node_id_list text COMMENT '群组成员节点的ID', create_time datetime DEFAULT NULL COMMENT '创建时间', modify_time datetime DEFAULT NULL COMMENT '修改时间', PRIMARY KEY (group_id) diff --git a/src/main/java/com/webank/webase/node/mgr/group/GroupService.java b/src/main/java/com/webank/webase/node/mgr/group/GroupService.java index e406f17ff..d4c6c4cd3 100644 --- a/src/main/java/com/webank/webase/node/mgr/group/GroupService.java +++ b/src/main/java/com/webank/webase/node/mgr/group/GroupService.java @@ -558,7 +558,7 @@ public TbGroup saveGroup(int groupId, int nodeCount, String description, int gro TbGroup tbGroup = new TbGroup(groupId, groupName, nodeCount, description, groupType, groupStatus); - tbGroup.setTimestamp(String.valueOf(timestamp)); + tbGroup.setGroupTimestamp(String.valueOf(timestamp)); tbGroup.setNodeIdList(JSON.toJSONString(nodeIdList)); groupMapper.save(tbGroup); // create table by group id diff --git a/src/main/java/com/webank/webase/node/mgr/group/entity/TbGroup.java b/src/main/java/com/webank/webase/node/mgr/group/entity/TbGroup.java index 5a7ea583f..584ea89fb 100644 --- a/src/main/java/com/webank/webase/node/mgr/group/entity/TbGroup.java +++ b/src/main/java/com/webank/webase/node/mgr/group/entity/TbGroup.java @@ -43,7 +43,7 @@ public class TbGroup { /** * group.x.genesis timestamp */ - private String timestamp; + private String groupTimestamp; /** * group peers nodeid */ diff --git a/src/main/resources/mapper/GroupMapper.xml b/src/main/resources/mapper/GroupMapper.xml index cef69c797..dc15dcf15 100644 --- a/src/main/resources/mapper/GroupMapper.xml +++ b/src/main/resources/mapper/GroupMapper.xml @@ -16,6 +16,10 @@ property="description"/> + + Date: Tue, 12 May 2020 17:52:32 +0800 Subject: [PATCH 033/131] fix save group --- .../java/com/webank/webase/node/mgr/group/GroupService.java | 2 +- src/main/resources/mapper/GroupMapper.xml | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/webank/webase/node/mgr/group/GroupService.java b/src/main/java/com/webank/webase/node/mgr/group/GroupService.java index d4c6c4cd3..d087e765b 100644 --- a/src/main/java/com/webank/webase/node/mgr/group/GroupService.java +++ b/src/main/java/com/webank/webase/node/mgr/group/GroupService.java @@ -558,7 +558,7 @@ public TbGroup saveGroup(int groupId, int nodeCount, String description, int gro TbGroup tbGroup = new TbGroup(groupId, groupName, nodeCount, description, groupType, groupStatus); - tbGroup.setGroupTimestamp(String.valueOf(timestamp)); + tbGroup.setGroupTimestamp(timestamp.toString(10)); tbGroup.setNodeIdList(JSON.toJSONString(nodeIdList)); groupMapper.save(tbGroup); // create table by group id diff --git a/src/main/resources/mapper/GroupMapper.xml b/src/main/resources/mapper/GroupMapper.xml index dc15dcf15..48377f269 100644 --- a/src/main/resources/mapper/GroupMapper.xml +++ b/src/main/resources/mapper/GroupMapper.xml @@ -44,8 +44,9 @@ insert into tb_group - (group_id,group_name,node_count,description,group_type,create_time) - values(#{groupId},#{groupName},#{nodeCount},#{description},#{groupType},NOW()) + (group_id,group_name,node_count,description,group_type,group_status,group_timestamp,node_id_list,create_time) + values(#{groupId},#{groupName},#{nodeCount},#{description},#{groupType},#{groupStatus}, + #{groupTimestamp},#{nodeIdList},NOW()) ON DUPLICATE KEY UPDATE node_count=#{nodeCount},modify_time=NOW() From 88ecc6ec17740faace0e01cbe316e278a4fbb15b Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Tue, 12 May 2020 19:03:21 +0800 Subject: [PATCH 034/131] fix modifyTime all same --- .../com/webank/webase/node/mgr/group/GroupService.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/webank/webase/node/mgr/group/GroupService.java b/src/main/java/com/webank/webase/node/mgr/group/GroupService.java index d087e765b..30d460e81 100644 --- a/src/main/java/com/webank/webase/node/mgr/group/GroupService.java +++ b/src/main/java/com/webank/webase/node/mgr/group/GroupService.java @@ -19,7 +19,6 @@ import com.webank.webase.node.mgr.base.enums.GroupType; import com.webank.webase.node.mgr.base.exception.NodeMgrException; import com.webank.webase.node.mgr.base.properties.ConstantProperties; -import com.webank.webase.node.mgr.base.tools.NodeMgrTools; import com.webank.webase.node.mgr.contract.ContractService; import com.webank.webase.node.mgr.front.FrontService; import com.webank.webase.node.mgr.front.entity.FrontParam; @@ -322,8 +321,8 @@ private void checkAndUpdateGroupStatus(Set allGroupOnChain) { int localGroupId = localGroup.getGroupId(); long count = allGroupOnChain.stream().filter(id -> id == localGroupId).count(); try { - // found groupId in groupOnChain - if (count > 0) { + // found groupId in groupOnChain, local status is invalid, set as normal + if (count > 0 && localGroup.getGroupStatus() != DataStatus.NORMAL.getValue()) { log.warn("group is normal, localGroupId:{}", localGroupId); //update NORMAL updateGroupStatus(localGroupId, DataStatus.NORMAL.getValue()); @@ -338,7 +337,7 @@ private void checkAndUpdateGroupStatus(Set allGroupOnChain) { // removeAllDataByGroupId(localGroupId); // continue; // } - // if not found in groupOnChain + // if not found in groupOnChain and local status is normal, set as invalid log.warn("group is invalid, localGroupId:{}", localGroupId); if (DataStatus.NORMAL.getValue() == localGroup.getGroupStatus()) { // update invalid From 4644fe1124ace59f54a1a5e4c39c2bb79c175e82 Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Tue, 12 May 2020 20:02:07 +0800 Subject: [PATCH 035/131] update delete abi api --- .../java/com/webank/webase/node/mgr/abi/AbiController.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/webank/webase/node/mgr/abi/AbiController.java b/src/main/java/com/webank/webase/node/mgr/abi/AbiController.java index f933caf88..c6ce7e554 100644 --- a/src/main/java/com/webank/webase/node/mgr/abi/AbiController.java +++ b/src/main/java/com/webank/webase/node/mgr/abi/AbiController.java @@ -117,11 +117,9 @@ public Object updateAbi(@RequestBody ReqImportAbi param, BindingResult result) { return new BaseResponse(ConstantCode.SUCCESS, res); } - @DeleteMapping("") + @DeleteMapping("/{abiId}") @PreAuthorize(ConstantProperties.HAS_ROLE_ADMIN) - public BaseResponse deleteAbi(@Valid @RequestBody ReqDelAbi param, BindingResult result) { - checkBindResult(result); - Integer abiId = param.getAbiId(); + public BaseResponse deleteAbi(@PathVariable("abiId") Integer abiId) { log.debug("start deleteAbi. abiId:{}", abiId); abiService.delete(abiId); log.debug("end deleteAbi"); From c0fe5b30d51b390975b15cbec7aa5e8e3c1af388 Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Tue, 12 May 2020 20:06:40 +0800 Subject: [PATCH 036/131] fix code style of tools --- .../node/mgr/base/tools/NodeMgrTools.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/webank/webase/node/mgr/base/tools/NodeMgrTools.java b/src/main/java/com/webank/webase/node/mgr/base/tools/NodeMgrTools.java index d3d6d9190..4ab83a8c0 100644 --- a/src/main/java/com/webank/webase/node/mgr/base/tools/NodeMgrTools.java +++ b/src/main/java/com/webank/webase/node/mgr/base/tools/NodeMgrTools.java @@ -496,23 +496,23 @@ public static String removeBinFirstAndLast(String contractBin, int removaLastLen if (StringUtils.isBlank(contractBin)) { return null; } - contractBin = removeFirstStr(contractBin, "0x"); - if (contractBin.length() > removaLastLength) { - contractBin = contractBin.substring(0, contractBin.length() - removaLastLength); + String contractBinResult = removeFirstStr(contractBin, "0x"); + if (contractBinResult.length() > removaLastLength) { + contractBinResult = contractBinResult.substring(0, contractBinResult.length() - removaLastLength); } - return contractBin; + return contractBinResult; } /** * remove fist string. */ - public static String removeFirstStr(String constant, String target) { - if (StringUtils.isBlank(constant) || StringUtils.isBlank(target)) { - return constant; + public static String removeFirstStr(String input, String target) { + if (StringUtils.isBlank(input) || StringUtils.isBlank(target)) { + return input; } - if (constant.startsWith(target)) { - constant = StringUtils.removeStart(constant, target); + if (input.startsWith(target)) { + input = StringUtils.removeStart(input, target); } - return constant; + return input; } } From b77b5db9a675bbc62be5c4f07a30d28d4f2f90c6 Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Wed, 13 May 2020 10:00:37 +0800 Subject: [PATCH 037/131] rm del abi entity --- .../webase/node/mgr/abi/AbiController.java | 1 - .../webase/node/mgr/abi/entity/ReqDelAbi.java | 19 ------------------- .../node/mgr/test/abi/AbiServiceTest.java | 1 - 3 files changed, 21 deletions(-) delete mode 100644 src/main/java/com/webank/webase/node/mgr/abi/entity/ReqDelAbi.java diff --git a/src/main/java/com/webank/webase/node/mgr/abi/AbiController.java b/src/main/java/com/webank/webase/node/mgr/abi/AbiController.java index c6ce7e554..bd2d3eeef 100644 --- a/src/main/java/com/webank/webase/node/mgr/abi/AbiController.java +++ b/src/main/java/com/webank/webase/node/mgr/abi/AbiController.java @@ -19,7 +19,6 @@ import com.alibaba.fastjson.JSON; import com.webank.webase.node.mgr.abi.entity.AbiInfo; import com.webank.webase.node.mgr.abi.entity.ReqAbiListParam; -import com.webank.webase.node.mgr.abi.entity.ReqDelAbi; import com.webank.webase.node.mgr.abi.entity.ReqImportAbi; import com.webank.webase.node.mgr.base.code.ConstantCode; import com.webank.webase.node.mgr.base.controller.BaseController; diff --git a/src/main/java/com/webank/webase/node/mgr/abi/entity/ReqDelAbi.java b/src/main/java/com/webank/webase/node/mgr/abi/entity/ReqDelAbi.java deleted file mode 100644 index a59dd5252..000000000 --- a/src/main/java/com/webank/webase/node/mgr/abi/entity/ReqDelAbi.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.webank.webase.node.mgr.abi.entity; - -import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.NoArgsConstructor; - -import javax.validation.constraints.NotNull; - -/** - * entity to delete abi info by id - * @author marsli - */ -@Data -@NoArgsConstructor -@AllArgsConstructor -public class ReqDelAbi { - @NotNull - private Integer abiId; -} \ No newline at end of file diff --git a/src/test/java/node/mgr/test/abi/AbiServiceTest.java b/src/test/java/node/mgr/test/abi/AbiServiceTest.java index 70d3a3ca0..01cf2a1e5 100644 --- a/src/test/java/node/mgr/test/abi/AbiServiceTest.java +++ b/src/test/java/node/mgr/test/abi/AbiServiceTest.java @@ -21,7 +21,6 @@ import com.webank.webase.node.mgr.abi.AbiService; import com.webank.webase.node.mgr.abi.entity.AbiInfo; import com.webank.webase.node.mgr.abi.entity.ReqAbiListParam; -import com.webank.webase.node.mgr.abi.entity.ReqDelAbi; import com.webank.webase.node.mgr.abi.entity.ReqImportAbi; import com.webank.webase.node.mgr.base.enums.SqlSortType; import org.junit.Assert; From e363a5bbbe1519758a564bed6f13b7dc7cfce7c7 Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Wed, 13 May 2020 14:24:55 +0800 Subject: [PATCH 038/131] fix update group invalid --- .../webase/node/mgr/group/GroupService.java | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/webank/webase/node/mgr/group/GroupService.java b/src/main/java/com/webank/webase/node/mgr/group/GroupService.java index 30d460e81..f2db127af 100644 --- a/src/main/java/com/webank/webase/node/mgr/group/GroupService.java +++ b/src/main/java/com/webank/webase/node/mgr/group/GroupService.java @@ -319,27 +319,19 @@ private void checkAndUpdateGroupStatus(Set allGroupOnChain) { for (TbGroup localGroup : allLocalGroup) { int localGroupId = localGroup.getGroupId(); - long count = allGroupOnChain.stream().filter(id -> id == localGroupId).count(); + long count = 0; + count = allGroupOnChain.stream().filter(id -> id == localGroupId).count(); try { // found groupId in groupOnChain, local status is invalid, set as normal - if (count > 0 && localGroup.getGroupStatus() != DataStatus.NORMAL.getValue()) { + if (count > 0 && localGroup.getGroupStatus() == DataStatus.INVALID.getValue()) { log.warn("group is normal, localGroupId:{}", localGroupId); //update NORMAL updateGroupStatus(localGroupId, DataStatus.NORMAL.getValue()); continue; } - // v1.3.1: removed by api, not aumotically - // check from last modifyTime if within gray period -// if (!NodeMgrTools.isWithinPeriod(localGroup.getModifyTime(), -// constants.getGroupInvalidGrayscaleValue())) { -// log.warn("remove group, localGroup:{}", JSON.toJSONString(localGroup)); -// //remove group -// removeAllDataByGroupId(localGroupId); -// continue; -// } // if not found in groupOnChain and local status is normal, set as invalid log.warn("group is invalid, localGroupId:{}", localGroupId); - if (DataStatus.NORMAL.getValue() == localGroup.getGroupStatus()) { + if (count == 0 && DataStatus.NORMAL.getValue() == localGroup.getGroupStatus()) { // update invalid updateGroupStatus(localGroupId, DataStatus.INVALID.getValue()); continue; From 4280cab0036f8e843bb45e88b114b0b180d64b3d Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Wed, 13 May 2020 14:26:36 +0800 Subject: [PATCH 039/131] fix update group log --- .../com/webank/webase/node/mgr/group/GroupService.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/webank/webase/node/mgr/group/GroupService.java b/src/main/java/com/webank/webase/node/mgr/group/GroupService.java index f2db127af..6f4e35f7d 100644 --- a/src/main/java/com/webank/webase/node/mgr/group/GroupService.java +++ b/src/main/java/com/webank/webase/node/mgr/group/GroupService.java @@ -324,16 +324,17 @@ private void checkAndUpdateGroupStatus(Set allGroupOnChain) { try { // found groupId in groupOnChain, local status is invalid, set as normal if (count > 0 && localGroup.getGroupStatus() == DataStatus.INVALID.getValue()) { - log.warn("group is normal, localGroupId:{}", localGroupId); + log.info("group is normal, localGroupId:{}", localGroupId); //update NORMAL updateGroupStatus(localGroupId, DataStatus.NORMAL.getValue()); continue; } // if not found in groupOnChain and local status is normal, set as invalid - log.warn("group is invalid, localGroupId:{}", localGroupId); + if (count == 0 && DataStatus.NORMAL.getValue() == localGroup.getGroupStatus()) { // update invalid - updateGroupStatus(localGroupId, DataStatus.INVALID.getValue()); + log.warn("group is invalid, localGroupId:{}", localGroupId); + updateGroupStatus(localGroupId, DataStatus.INVALID.getValue()); continue; } From aded26ea341b03087c6748844870f361ada3b2b3 Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Wed, 13 May 2020 19:02:49 +0800 Subject: [PATCH 040/131] add nodeIdList api --- .../frontinterface/FrontInterfaceService.java | 5 +++++ .../webase/node/mgr/node/NodeController.java | 19 +++++++++++++++++++ .../webase/node/mgr/node/NodeService.java | 7 +++++++ .../mgr/test/node/NodeControllerTest.java | 11 +++++++++++ 4 files changed, 42 insertions(+) diff --git a/src/main/java/com/webank/webase/node/mgr/frontinterface/FrontInterfaceService.java b/src/main/java/com/webank/webase/node/mgr/frontinterface/FrontInterfaceService.java index fb03ad136..f3a5474bc 100644 --- a/src/main/java/com/webank/webase/node/mgr/frontinterface/FrontInterfaceService.java +++ b/src/main/java/com/webank/webase/node/mgr/frontinterface/FrontInterfaceService.java @@ -524,4 +524,9 @@ public List getKeyStoreList(Integer groupId, String frontIp, Integer fr List resList = JSON.parseArray(JSON.toJSONString(data), KeyPair.class); return resList; } + + public List getNodeIdList(int groupId) { + return frontRestTools.getForEntity(groupId, FrontRestTools.URI_NODEID_LIST, List.class); + + } } diff --git a/src/main/java/com/webank/webase/node/mgr/node/NodeController.java b/src/main/java/com/webank/webase/node/mgr/node/NodeController.java index 32ab638cc..491740cb0 100644 --- a/src/main/java/com/webank/webase/node/mgr/node/NodeController.java +++ b/src/main/java/com/webank/webase/node/mgr/node/NodeController.java @@ -112,4 +112,23 @@ public BaseResponse getNodeInfo(@PathVariable("groupId") Integer groupId) return baseResponse; } + /** + * get node id list + */ + @GetMapping("/nodeIdList/{groupId}") + public BaseResponse getNodeIdList(@PathVariable("groupId") Integer groupId) { + Instant startTime = Instant.now(); + log.info("start getNodeIdList startTime:{} groupId:{}", + startTime.toEpochMilli(), groupId); + List res = nodeService.getNodeIdListService(groupId); + + BaseResponse baseResponse = new BaseResponse(ConstantCode.SUCCESS); + baseResponse.setData(res); + + log.info("end getNodeIdList useTime:{} result:{}", + Duration.between(startTime, Instant.now()).toMillis(), JSON.toJSONString(baseResponse)); + return baseResponse; + } + + } diff --git a/src/main/java/com/webank/webase/node/mgr/node/NodeService.java b/src/main/java/com/webank/webase/node/mgr/node/NodeService.java index d53a2d91e..9a05db384 100644 --- a/src/main/java/com/webank/webase/node/mgr/node/NodeService.java +++ b/src/main/java/com/webank/webase/node/mgr/node/NodeService.java @@ -315,4 +315,11 @@ public List getSealerAndObserverList(int groupId) { log.debug("end getSealerAndObserverList resList:{}", resList); return resList; } + + public List getNodeIdListService(int groupId) { + log.debug("start getSealerAndObserverList groupId:{}", groupId); + List nodeIdList = frontInterface.getNodeIdList(groupId); + log.debug("end getSealerAndObserverList nodeIdList:{}", nodeIdList); + return nodeIdList; + } } diff --git a/src/test/java/node/mgr/test/node/NodeControllerTest.java b/src/test/java/node/mgr/test/node/NodeControllerTest.java index 9e673d70a..bb39b2593 100644 --- a/src/test/java/node/mgr/test/node/NodeControllerTest.java +++ b/src/test/java/node/mgr/test/node/NodeControllerTest.java @@ -64,4 +64,15 @@ public void testQueryNodeId() { System.out.println(tbNode); Assert.assertNull(tbNode); } + + @Test + public void testGetNodeIdList() throws Exception { + ResultActions resultActions = mockMvc + .perform(MockMvcRequestBuilders.get("/node/nodeIdList/2")); + resultActions. +// andExpect(MockMvcResultMatchers.status().isOk()). + andDo(MockMvcResultHandlers.print()); + System.out.println("======================response:" + +resultActions.andReturn().getResponse().getContentAsString()); + } } From 61a35c583778596afcf7597a50343c1edf3c2861 Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Wed, 13 May 2020 19:03:05 +0800 Subject: [PATCH 041/131] fix groupPeers --- .../java/com/webank/webase/node/mgr/front/FrontService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/webank/webase/node/mgr/front/FrontService.java b/src/main/java/com/webank/webase/node/mgr/front/FrontService.java index f56019312..39ec31468 100644 --- a/src/main/java/com/webank/webase/node/mgr/front/FrontService.java +++ b/src/main/java/com/webank/webase/node/mgr/front/FrontService.java @@ -127,7 +127,7 @@ public TbFront newFront(FrontInfo frontInfo) { Integer group = Integer.valueOf(groupId); //peer in group List groupPeerList = frontInterface - .getNodeIDListFromSpecificFront(frontIp, frontPort, group); + .getGroupPeersFromSpecificFront(frontIp, frontPort, group); //get peers on chain PeerInfo[] peerArr = frontInterface .getPeersFromSpecificFront(frontIp, frontPort, group); From 514e9d27a84f3a3a376990e0451ac9f114459d64 Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Thu, 14 May 2020 11:13:39 +0800 Subject: [PATCH 042/131] fix abi send tx contractId null --- .../webase/node/mgr/abi/AbiService.java | 8 +++++ .../node/mgr/contract/ContractService.java | 19 +++++++++--- .../entity/TransactionInputParam.java | 3 +- .../webase/node/mgr/group/GroupService.java | 29 ++++++++++++++++++- 4 files changed, 53 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/webank/webase/node/mgr/abi/AbiService.java b/src/main/java/com/webank/webase/node/mgr/abi/AbiService.java index a9be1351f..730160207 100644 --- a/src/main/java/com/webank/webase/node/mgr/abi/AbiService.java +++ b/src/main/java/com/webank/webase/node/mgr/abi/AbiService.java @@ -135,6 +135,14 @@ private void checkAbiIdExist(Integer abiId) { } } + public AbiInfo getAbiByGroupIdAndAddress(Integer groupId, String contractAddress) { + AbiInfo abiInfo = abiMapper.queryByGroupIdAndAddress(groupId, contractAddress); + if (Objects.isNull(abiInfo)) { + throw new NodeMgrException(ConstantCode.ABI_INFO_NOT_EXISTS); + } + return abiInfo; + } + /** * check address is valid. * @return address's runtime bin diff --git a/src/main/java/com/webank/webase/node/mgr/contract/ContractService.java b/src/main/java/com/webank/webase/node/mgr/contract/ContractService.java index 00b39f69f..fa3df47a1 100644 --- a/src/main/java/com/webank/webase/node/mgr/contract/ContractService.java +++ b/src/main/java/com/webank/webase/node/mgr/contract/ContractService.java @@ -15,6 +15,7 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; +import com.webank.webase.node.mgr.abi.AbiService; import com.webank.webase.node.mgr.base.code.ConstantCode; import com.webank.webase.node.mgr.base.enums.ContractStatus; import com.webank.webase.node.mgr.base.exception.NodeMgrException; @@ -63,7 +64,8 @@ public class ContractService { @Autowired private UserService userService; private static final int CONTRACT_ADDRESS_LENGTH = 42; - + @Autowired + private AbiService abiService; /** * add new contract data. */ @@ -269,15 +271,24 @@ public Object sendTransaction(TransactionInputParam param) throws NodeMgrExcepti throw new NodeMgrException(ConstantCode.INVALID_PARAM_INFO); } - //check contractId - TbContract contract = verifyContractIdExist(param.getContractId(), param.getGroupId()); + // check contractId + String contractAbiStr = ""; + if (param.getContractId() != null) { + // get abi by contract + TbContract contract = verifyContractIdExist(param.getContractId(), param.getGroupId()); + contractAbiStr = contract.getContractAbi(); + } else { + // send tx by abi, no need to check abi exist in db + contractAbiStr = JSON.toJSONString(param.getContractAbi()); + } + //send abi to front sendAbi(param.getGroupId(), param.getContractId(), param.getContractAddress()); //check contract deploy verifyContractDeploy(param.getContractId(), param.getGroupId()); // if constant, signUserId is useless - AbiDefinition funcAbi = Web3Tools.getAbiDefinition(param.getFuncName(), contract.getContractAbi()); + AbiDefinition funcAbi = Web3Tools.getAbiDefinition(param.getFuncName(), contractAbiStr); String signUserId = "empty"; if (!funcAbi.isConstant()) { signUserId = userService.getSignUserIdByAddress(param.getGroupId(), param.getUser()); diff --git a/src/main/java/com/webank/webase/node/mgr/contract/entity/TransactionInputParam.java b/src/main/java/com/webank/webase/node/mgr/contract/entity/TransactionInputParam.java index 1cccd264c..0da1f1678 100644 --- a/src/main/java/com/webank/webase/node/mgr/contract/entity/TransactionInputParam.java +++ b/src/main/java/com/webank/webase/node/mgr/contract/entity/TransactionInputParam.java @@ -24,13 +24,14 @@ /** * param of send transaction from web + * support send by contract api and abi api */ @Data @NoArgsConstructor @ToString(callSuper = true) @EqualsAndHashCode(callSuper = true) public class TransactionInputParam extends TransactionParam { - @NotNull + private Integer contractId; /** * user address, identify signUserId diff --git a/src/main/java/com/webank/webase/node/mgr/group/GroupService.java b/src/main/java/com/webank/webase/node/mgr/group/GroupService.java index 6f4e35f7d..ab61e2b8d 100644 --- a/src/main/java/com/webank/webase/node/mgr/group/GroupService.java +++ b/src/main/java/com/webank/webase/node/mgr/group/GroupService.java @@ -237,7 +237,8 @@ public void resetGroupList() { Duration.between(startTime, Instant.now()).toMillis()); } - /** + + /** * save new peers that not in group peers */ private void savePeerList(String frontIp, Integer frontPort, int groupId, List groupPeerList) { @@ -346,6 +347,32 @@ private void checkAndUpdateGroupStatus(Set allGroupOnChain) { } } + /** + * check group's genesis block the same from each front + */ +// private void checkGroupGenesisSame() { +// //get all front +// List frontList = frontService.getFrontList(new FrontParam()); +// if (frontList == null || frontList.size() == 0) { +// log.info("not fount any front, start remove all group"); +// //remove all group TODO +// // removeAllGroup(); +// return; +// } +// List allGroupList = getGroupList(null); +// if (allGroupList.isEmpty()) { +// return; +// } +// +// for (TbGroup tbGroup : allGroupList) { +// int groupId = tbGroup.getGroupId(); +// for (TbFront front : frontList) { +// String frontIp = front.getFrontIp(); +// int frontPort = front.getFrontPort(); +// +// } +// } +// } /** * remove all data by groupId. From 00ffdb078d16db46bb009d71edb068ae4ebae274 Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Thu, 14 May 2020 11:26:53 +0800 Subject: [PATCH 043/131] fix abi send tx nullpointer --- .../node/mgr/contract/ContractService.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/webank/webase/node/mgr/contract/ContractService.java b/src/main/java/com/webank/webase/node/mgr/contract/ContractService.java index fa3df47a1..07e621bd8 100644 --- a/src/main/java/com/webank/webase/node/mgr/contract/ContractService.java +++ b/src/main/java/com/webank/webase/node/mgr/contract/ContractService.java @@ -16,6 +16,7 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.webank.webase.node.mgr.abi.AbiService; +import com.webank.webase.node.mgr.abi.entity.AbiInfo; import com.webank.webase.node.mgr.base.code.ConstantCode; import com.webank.webase.node.mgr.base.enums.ContractStatus; import com.webank.webase.node.mgr.base.exception.NodeMgrException; @@ -277,16 +278,17 @@ public Object sendTransaction(TransactionInputParam param) throws NodeMgrExcepti // get abi by contract TbContract contract = verifyContractIdExist(param.getContractId(), param.getGroupId()); contractAbiStr = contract.getContractAbi(); + //send abi to front + sendAbi(param.getGroupId(), param.getContractId(), param.getContractAddress()); + //check contract deploy + verifyContractDeploy(param.getContractId(), param.getGroupId()); } else { - // send tx by abi, no need to check abi exist in db - contractAbiStr = JSON.toJSONString(param.getContractAbi()); + // send tx by abi + // get from db and it's deployed + AbiInfo abiInfo = abiService.getAbiByGroupIdAndAddress(param.getGroupId(), param.getContractAddress()); + contractAbiStr = abiInfo.getContractAbi(); } - //send abi to front - sendAbi(param.getGroupId(), param.getContractId(), param.getContractAddress()); - //check contract deploy - verifyContractDeploy(param.getContractId(), param.getGroupId()); - // if constant, signUserId is useless AbiDefinition funcAbi = Web3Tools.getAbiDefinition(param.getFuncName(), contractAbiStr); String signUserId = "empty"; From 257ee145f05b00d904138c4c305cf7cf667f6a93 Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Thu, 14 May 2020 17:04:20 +0800 Subject: [PATCH 044/131] add check genesis block --- .../node/mgr/base/enums/GroupStatus.java | 47 ++++++++++ .../frontinterface/FrontInterfaceService.java | 9 ++ .../webase/node/mgr/group/GroupService.java | 88 +++++++++++-------- 3 files changed, 107 insertions(+), 37 deletions(-) create mode 100644 src/main/java/com/webank/webase/node/mgr/base/enums/GroupStatus.java diff --git a/src/main/java/com/webank/webase/node/mgr/base/enums/GroupStatus.java b/src/main/java/com/webank/webase/node/mgr/base/enums/GroupStatus.java new file mode 100644 index 000000000..0c43da2ee --- /dev/null +++ b/src/main/java/com/webank/webase/node/mgr/base/enums/GroupStatus.java @@ -0,0 +1,47 @@ +/** + * Copyright 2014-2020 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.webank.webase.node.mgr.base.enums; + +/** + * group status in tb_group + */ +public enum GroupStatus { + /** + * running + */ + NORMAL(1), + /** + * stopped or removed + */ + MAINTAINING(2), + /** + * invalid for group data conflicts etc. + */ + CONFLICT(3); + + private int value; + + GroupStatus(Integer groupType) { + this.value = groupType; + } + + public int getValue() { + return this.value; + } + +} + diff --git a/src/main/java/com/webank/webase/node/mgr/frontinterface/FrontInterfaceService.java b/src/main/java/com/webank/webase/node/mgr/frontinterface/FrontInterfaceService.java index f3a5474bc..2f5c9f7fe 100644 --- a/src/main/java/com/webank/webase/node/mgr/frontinterface/FrontInterfaceService.java +++ b/src/main/java/com/webank/webase/node/mgr/frontinterface/FrontInterfaceService.java @@ -171,6 +171,15 @@ public SyncStatus getSyncStatusFromSpecificFront(String frontIp, Integer frontPo SyncStatus.class); } + public BlockInfo getBlockByNumberFromSpecificFront(String frontIp, Integer frontPort, + Integer groupId, BigInteger blockNumber) { + log.debug("start getBlockByNumberFromSpecificFront frontIp:{},frontPort{}," + + " groupId:{} blockNumber:{}", frontIp, frontPort, groupId, blockNumber); + String uri = String.format(FrontRestTools.URI_BLOCK_BY_NUMBER, blockNumber); + BlockInfo blockInfo = getFromSpecificFront(groupId, frontIp, frontPort, uri, BlockInfo.class); + return blockInfo; + } + /** * get peers. */ diff --git a/src/main/java/com/webank/webase/node/mgr/group/GroupService.java b/src/main/java/com/webank/webase/node/mgr/group/GroupService.java index ab61e2b8d..2c569663c 100644 --- a/src/main/java/com/webank/webase/node/mgr/group/GroupService.java +++ b/src/main/java/com/webank/webase/node/mgr/group/GroupService.java @@ -15,10 +15,11 @@ import com.alibaba.fastjson.JSON; import com.webank.webase.node.mgr.base.code.ConstantCode; -import com.webank.webase.node.mgr.base.enums.DataStatus; +import com.webank.webase.node.mgr.base.enums.GroupStatus; import com.webank.webase.node.mgr.base.enums.GroupType; import com.webank.webase.node.mgr.base.exception.NodeMgrException; import com.webank.webase.node.mgr.base.properties.ConstantProperties; +import com.webank.webase.node.mgr.block.entity.BlockInfo; import com.webank.webase.node.mgr.contract.ContractService; import com.webank.webase.node.mgr.front.FrontService; import com.webank.webase.node.mgr.front.entity.FrontParam; @@ -187,9 +188,6 @@ public void resetGroupList() { //get all front List frontList = frontService.getFrontList(new FrontParam()); if (frontList == null || frontList.size() == 0) { - log.info("not fount any front, start remove all group"); - //remove all group TODO - // removeAllGroup(); return; } //get group from chain @@ -214,7 +212,7 @@ public void resetGroupList() { if (Objects.isNull(checkGroup) || groupPeerList.size() != checkGroup.getNodeCount()) { // save group saveGroup(gId, groupPeerList.size(), "synchronous", - GroupType.SYNC.getValue(), DataStatus.NORMAL.getValue()); + GroupType.SYNC.getValue(), GroupStatus.NORMAL.getValue()); } frontGroupMapService.newFrontGroup(front.getFrontId(), gId); //save new peers @@ -226,8 +224,10 @@ public void resetGroupList() { } } - // check group status + // check group status(normal or maintaining) checkAndUpdateGroupStatus(allGroupSet); + // check group's genesis block same(check dirty data of different chain) + checkGroupGenesisSame(); // remove front_group_map that not in tb_front or tb_group frontGroupMapService.removeInvalidFrontGroupMap(); //clear cache @@ -324,18 +324,18 @@ private void checkAndUpdateGroupStatus(Set allGroupOnChain) { count = allGroupOnChain.stream().filter(id -> id == localGroupId).count(); try { // found groupId in groupOnChain, local status is invalid, set as normal - if (count > 0 && localGroup.getGroupStatus() == DataStatus.INVALID.getValue()) { + if (count > 0 && localGroup.getGroupStatus() == GroupStatus.MAINTAINING.getValue()) { log.info("group is normal, localGroupId:{}", localGroupId); //update NORMAL - updateGroupStatus(localGroupId, DataStatus.NORMAL.getValue()); + updateGroupStatus(localGroupId, GroupStatus.NORMAL.getValue()); continue; } - // if not found in groupOnChain and local status is normal, set as invalid - if (count == 0 && DataStatus.NORMAL.getValue() == localGroup.getGroupStatus()) { + // if not found in groupOnChain and local status is normal, set as invalid + if (count == 0 && GroupStatus.NORMAL.getValue() == localGroup.getGroupStatus()) { // update invalid log.warn("group is invalid, localGroupId:{}", localGroupId); - updateGroupStatus(localGroupId, DataStatus.INVALID.getValue()); + updateGroupStatus(localGroupId, GroupStatus.MAINTAINING.getValue()); continue; } @@ -348,31 +348,45 @@ private void checkAndUpdateGroupStatus(Set allGroupOnChain) { } /** - * check group's genesis block the same from each front + * check group's genesis block the same with each other */ -// private void checkGroupGenesisSame() { -// //get all front -// List frontList = frontService.getFrontList(new FrontParam()); -// if (frontList == null || frontList.size() == 0) { -// log.info("not fount any front, start remove all group"); -// //remove all group TODO -// // removeAllGroup(); -// return; -// } -// List allGroupList = getGroupList(null); -// if (allGroupList.isEmpty()) { -// return; -// } -// -// for (TbGroup tbGroup : allGroupList) { -// int groupId = tbGroup.getGroupId(); -// for (TbFront front : frontList) { -// String frontIp = front.getFrontIp(); -// int frontPort = front.getFrontPort(); -// -// } -// } -// } + private void checkGroupGenesisSame() { + log.info("start checkGroupGenesisSame."); + //get all front + List frontList = frontService.getFrontList(new FrontParam()); + if (frontList == null || frontList.size() == 0) { + log.warn("checkGroupGenesisSame not found any front."); + return; + } + List allGroupList = getGroupList(null); + if (allGroupList.isEmpty()) { + log.warn("checkGroupGenesisSame not found any group of front."); + return; + } + + for (TbGroup tbGroup : allGroupList) { + int groupId = tbGroup.getGroupId(); + String lastBlockHash = ""; + for (TbFront front : frontList) { + String frontIp = front.getFrontIp(); + int frontPort = front.getFrontPort(); + // check genesis block + BlockInfo genesisBlock = frontInterface.getBlockByNumberFromSpecificFront(frontIp, + frontPort, groupId, BigInteger.ZERO); + + if (!"".equals(lastBlockHash) && !lastBlockHash.equals(genesisBlock.getHash())) { + log.warn("checkGroupGenesisSame genesis block hash conflicts with other group," + + " groupId:{}, frontId:{}", groupId, front.getFrontId()); + updateGroupStatus(groupId, GroupStatus.CONFLICT.getValue()); + } + lastBlockHash = genesisBlock.getHash(); + log.debug("checkGroupGenesisSame, groupId:{}, frontId:{}, genesis blockHash:{}", + groupId, front.getFrontId(), lastBlockHash); + } + } + } + + /** * remove all data by groupId. @@ -422,7 +436,7 @@ public TbGroup generateToSingleNode(String nodeId, ReqGenerateGroup req) { generateGroupInfo); // save group, saved as invalid status until start TbGroup tbGroup = saveGroup(generateGroupId, req.getNodeList().size(), - req.getDescription(), GroupType.MANUAL.getValue(), DataStatus.INVALID.getValue(), + req.getDescription(), GroupType.MANUAL.getValue(), GroupStatus.MAINTAINING.getValue(), req.getTimestamp(), req.getNodeList()); return tbGroup; } @@ -454,7 +468,7 @@ public TbGroup generateGroup(ReqGenerateGroup req) { } // save group, saved as invalid status until start TbGroup tbGroup = saveGroup(generateGroupId, req.getNodeList().size(), - req.getDescription(), GroupType.MANUAL.getValue(), DataStatus.INVALID.getValue(), + req.getDescription(), GroupType.MANUAL.getValue(), GroupStatus.MAINTAINING.getValue(), req.getTimestamp(), req.getNodeList()); return tbGroup; } From c3d6dd6ae2b94e3f2cd3e28986fbfa62d6d8c80e Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Thu, 14 May 2020 17:23:05 +0800 Subject: [PATCH 045/131] fix invalid group when check --- .../webase/node/mgr/group/GroupService.java | 11 ++++-- .../node/mgr/test/group/GroupServiceTest.java | 38 +++++++++++++++++++ .../mgr/test/node/NodeControllerTest.java | 2 +- 3 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 src/test/java/node/mgr/test/group/GroupServiceTest.java diff --git a/src/main/java/com/webank/webase/node/mgr/group/GroupService.java b/src/main/java/com/webank/webase/node/mgr/group/GroupService.java index 2c569663c..cc3ed622d 100644 --- a/src/main/java/com/webank/webase/node/mgr/group/GroupService.java +++ b/src/main/java/com/webank/webase/node/mgr/group/GroupService.java @@ -358,14 +358,15 @@ private void checkGroupGenesisSame() { log.warn("checkGroupGenesisSame not found any front."); return; } - List allGroupList = getGroupList(null); - if (allGroupList.isEmpty()) { + List allNormalGroupList = getGroupList(GroupStatus.NORMAL.getValue()); + if (allNormalGroupList.isEmpty()) { log.warn("checkGroupGenesisSame not found any group of front."); return; } - for (TbGroup tbGroup : allGroupList) { + for (TbGroup tbGroup : allNormalGroupList) { int groupId = tbGroup.getGroupId(); + log.error("checkGroupGenesisSame groupId:{}",groupId); String lastBlockHash = ""; for (TbFront front : frontList) { String frontIp = front.getFrontIp(); @@ -373,7 +374,9 @@ private void checkGroupGenesisSame() { // check genesis block BlockInfo genesisBlock = frontInterface.getBlockByNumberFromSpecificFront(frontIp, frontPort, groupId, BigInteger.ZERO); - + if (genesisBlock == null) { + continue; + } if (!"".equals(lastBlockHash) && !lastBlockHash.equals(genesisBlock.getHash())) { log.warn("checkGroupGenesisSame genesis block hash conflicts with other group," + " groupId:{}, frontId:{}", groupId, front.getFrontId()); diff --git a/src/test/java/node/mgr/test/group/GroupServiceTest.java b/src/test/java/node/mgr/test/group/GroupServiceTest.java new file mode 100644 index 000000000..1377789fc --- /dev/null +++ b/src/test/java/node/mgr/test/group/GroupServiceTest.java @@ -0,0 +1,38 @@ +/** + * Copyright 2014-2020 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package node.mgr.test.group; + +import com.webank.webase.node.mgr.Application; +import com.webank.webase.node.mgr.group.GroupService; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = Application.class) +public class GroupServiceTest { + + @Autowired + private GroupService groupService; + + @Test + public void testCheckGroupGenesisSame() { + groupService.resetGroupList(); + } +} diff --git a/src/test/java/node/mgr/test/node/NodeControllerTest.java b/src/test/java/node/mgr/test/node/NodeControllerTest.java index bb39b2593..1e8b25f38 100644 --- a/src/test/java/node/mgr/test/node/NodeControllerTest.java +++ b/src/test/java/node/mgr/test/node/NodeControllerTest.java @@ -70,7 +70,7 @@ public void testGetNodeIdList() throws Exception { ResultActions resultActions = mockMvc .perform(MockMvcRequestBuilders.get("/node/nodeIdList/2")); resultActions. -// andExpect(MockMvcResultMatchers.status().isOk()). + andExpect(MockMvcResultMatchers.status().isOk()). andDo(MockMvcResultHandlers.print()); System.out.println("======================response:" +resultActions.andReturn().getResponse().getContentAsString()); From 393ab616b63f87a251f9ee396c1d50a25b905e5b Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Fri, 15 May 2020 15:20:57 +0800 Subject: [PATCH 046/131] add check dirty group data --- .../node/mgr/base/enums/GroupStatus.java | 8 +- .../node/mgr/base/tools/NodeMgrTools.java | 25 +++++- .../webase/node/mgr/block/BlockMapper.java | 8 ++ .../webase/node/mgr/block/BlockService.java | 32 +++++++ .../webase/node/mgr/group/GroupService.java | 84 ++++++++++++++++--- src/main/resources/mapper/BlockMapper.xml | 11 +++ 6 files changed, 149 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/webank/webase/node/mgr/base/enums/GroupStatus.java b/src/main/java/com/webank/webase/node/mgr/base/enums/GroupStatus.java index 0c43da2ee..b5ae4aeb8 100644 --- a/src/main/java/com/webank/webase/node/mgr/base/enums/GroupStatus.java +++ b/src/main/java/com/webank/webase/node/mgr/base/enums/GroupStatus.java @@ -29,9 +29,13 @@ public enum GroupStatus { */ MAINTAINING(2), /** - * invalid for group data conflicts etc. + * invalid for different front's group data conflicts with each other, need to remove invalid front */ - CONFLICT(3); + CONFLICT_GROUP_GENESIS(3), + /** + * invalid for db's group data conflicts with chain, need to delete data + */ + CONFLICT_LOCAL_DATA(4); private int value; diff --git a/src/main/java/com/webank/webase/node/mgr/base/tools/NodeMgrTools.java b/src/main/java/com/webank/webase/node/mgr/base/tools/NodeMgrTools.java index 4ab83a8c0..f9f0347ec 100644 --- a/src/main/java/com/webank/webase/node/mgr/base/tools/NodeMgrTools.java +++ b/src/main/java/com/webank/webase/node/mgr/base/tools/NodeMgrTools.java @@ -26,10 +26,13 @@ import java.net.Socket; import java.net.SocketAddress; import java.security.MessageDigest; +import java.sql.Timestamp; import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; +import java.time.ZoneOffset; import java.time.format.DateTimeFormatter; +import java.time.temporal.ChronoField; import java.util.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -89,14 +92,28 @@ public static LocalDateTime hex2LocalDateTime(String hexStr) { /** * convert timestamp to localDateTime. */ - public static LocalDateTime timestamp2LocalDateTime(Long inputTime) { - if (inputTime == null) { - log.warn("timestamp2LocalDateTime fail. inputTime is null"); + public static LocalDateTime timestamp2LocalDateTime(Long inputTimeStamp) { + if (inputTimeStamp == null) { + log.warn("timestamp2LocalDateTime fail. inputTimeStamp is null"); return null; } - Instant instant = Instant.ofEpochMilli(inputTime); + Instant instant = Instant.ofEpochMilli(inputTimeStamp); ZoneId zone = ZoneId.systemDefault(); return LocalDateTime.ofInstant(instant, zone); +// Timestamp time = new Timestamp(inputTimeStamp); +// return time.toLocalDateTime(); + } + /** + * LocalDateTime to timestamp + */ + public static Long localDateTime2Timestamp(LocalDateTime inputDateTime) { + if (inputDateTime == null) { + log.warn("localDateTime2Timestamp fail. inputDateTime is null"); + return null; + } +// Timestamp time = Timestamp.valueOf(inputDateTime); +// return time.getTime(); + return inputDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli(); } /** diff --git a/src/main/java/com/webank/webase/node/mgr/block/BlockMapper.java b/src/main/java/com/webank/webase/node/mgr/block/BlockMapper.java index 871d79446..008015939 100644 --- a/src/main/java/com/webank/webase/node/mgr/block/BlockMapper.java +++ b/src/main/java/com/webank/webase/node/mgr/block/BlockMapper.java @@ -31,6 +31,11 @@ public interface BlockMapper { */ BigInteger getLatestBlockNumber(@Param("tableName") String tableName); + /** + * query smallest block number + */ + BigInteger getSmallestBlockNumber(@Param("tableName") String tableName); + /** * Add new block data. */ @@ -62,4 +67,7 @@ Integer getCount(@Param("tableName") String tableName, @Param("pkHash") String p */ Integer remove(@Param("tableName") String tableName, @Param("blockRetainMax") BigInteger blockRetainMax); + + TbBlock getBlockByBlockNumber(@Param("tableName") String tableName, + @Param("blockNumber") BigInteger blockNumber); } diff --git a/src/main/java/com/webank/webase/node/mgr/block/BlockService.java b/src/main/java/com/webank/webase/node/mgr/block/BlockService.java index 17885225a..0f8bbaec0 100644 --- a/src/main/java/com/webank/webase/node/mgr/block/BlockService.java +++ b/src/main/java/com/webank/webase/node/mgr/block/BlockService.java @@ -307,4 +307,36 @@ public BlockInfo getBlockFromFrontByNumber(int groupId, BigInteger blockNumber) public BlockInfo getblockFromFrontByHash(int groupId, String pkHash) { return frontInterface.getblockByHash(groupId, pkHash); } + + /** + * get smallest block height in local db + * + */ + public TbBlock getSmallestBlockInfo(int groupId) { + BigInteger smallestHeight = getSmallestBlockHeight(groupId); + if (smallestHeight == null) { + log.debug("getSmallestBlockInfo groupId:{} has no block local", groupId); + return null; + } + return getBlockByBlockNumber(groupId, smallestHeight); + } + + /** + * get block of smallest height in local db + * @param groupId + */ + public BigInteger getSmallestBlockHeight(int groupId) { + return blockmapper.getSmallestBlockNumber(TableName.BLOCK.getTableName(groupId)); + } + + /** + * get local tbBlock by blockNumber + * @param groupId + * @param blockNumber + * @return TbBlock + */ + public TbBlock getBlockByBlockNumber(int groupId, BigInteger blockNumber) { + return blockmapper.getBlockByBlockNumber(TableName.BLOCK.getTableName(groupId), + blockNumber); + } } diff --git a/src/main/java/com/webank/webase/node/mgr/group/GroupService.java b/src/main/java/com/webank/webase/node/mgr/group/GroupService.java index cc3ed622d..270e2ec03 100644 --- a/src/main/java/com/webank/webase/node/mgr/group/GroupService.java +++ b/src/main/java/com/webank/webase/node/mgr/group/GroupService.java @@ -19,7 +19,10 @@ import com.webank.webase.node.mgr.base.enums.GroupType; import com.webank.webase.node.mgr.base.exception.NodeMgrException; import com.webank.webase.node.mgr.base.properties.ConstantProperties; +import com.webank.webase.node.mgr.base.tools.NodeMgrTools; +import com.webank.webase.node.mgr.block.BlockService; import com.webank.webase.node.mgr.block.entity.BlockInfo; +import com.webank.webase.node.mgr.block.entity.TbBlock; import com.webank.webase.node.mgr.contract.ContractService; import com.webank.webase.node.mgr.front.FrontService; import com.webank.webase.node.mgr.front.entity.FrontParam; @@ -46,6 +49,7 @@ import java.math.BigInteger; import java.time.Duration; import java.time.Instant; +import java.time.LocalDateTime; import java.util.*; /** @@ -77,7 +81,8 @@ public class GroupService { private TransDailyService transDailyService; @Autowired private ConstantProperties constants; - + @Autowired + private BlockService blockService; /** * query count of group. @@ -226,8 +231,10 @@ public void resetGroupList() { // check group status(normal or maintaining) checkAndUpdateGroupStatus(allGroupSet); - // check group's genesis block same(check dirty data of different chain) - checkGroupGenesisSame(); + // check group's genesis block same with each other + checkGroupGenesisSameWithEach(); + // check group if has dirty data + checkSameChainDataWithLocal(); // remove front_group_map that not in tb_front or tb_group frontGroupMapService.removeInvalidFrontGroupMap(); //clear cache @@ -348,25 +355,24 @@ private void checkAndUpdateGroupStatus(Set allGroupOnChain) { } /** - * check group's genesis block the same with each other + * check group's genesis block the same with each other on chain */ - private void checkGroupGenesisSame() { - log.info("start checkGroupGenesisSame."); - //get all front + private void checkGroupGenesisSameWithEach() { + log.info("start checkGroupGenesisSameWithEach."); + // get all front List frontList = frontService.getFrontList(new FrontParam()); if (frontList == null || frontList.size() == 0) { - log.warn("checkGroupGenesisSame not found any front."); + log.warn("checkGroupGenesisSameWithEach not found any front."); return; } List allNormalGroupList = getGroupList(GroupStatus.NORMAL.getValue()); if (allNormalGroupList.isEmpty()) { - log.warn("checkGroupGenesisSame not found any group of front."); + log.warn("checkGroupGenesisSameWithEach not found any group of front."); return; } for (TbGroup tbGroup : allNormalGroupList) { int groupId = tbGroup.getGroupId(); - log.error("checkGroupGenesisSame groupId:{}",groupId); String lastBlockHash = ""; for (TbFront front : frontList) { String frontIp = front.getFrontIp(); @@ -378,17 +384,69 @@ private void checkGroupGenesisSame() { continue; } if (!"".equals(lastBlockHash) && !lastBlockHash.equals(genesisBlock.getHash())) { - log.warn("checkGroupGenesisSame genesis block hash conflicts with other group," + + log.warn("checkGroupGenesisSameWithEach genesis block hash conflicts with other group," + " groupId:{}, frontId:{}", groupId, front.getFrontId()); - updateGroupStatus(groupId, GroupStatus.CONFLICT.getValue()); + updateGroupStatus(groupId, GroupStatus.CONFLICT_GROUP_GENESIS.getValue()); } lastBlockHash = genesisBlock.getHash(); - log.debug("checkGroupGenesisSame, groupId:{}, frontId:{}, genesis blockHash:{}", + log.debug("checkGroupGenesisSameWithEach, groupId:{}, frontId:{}, genesis blockHash:{}", groupId, front.getFrontId(), lastBlockHash); } } } + /** + * check local block's hash same with blockHash on chain + * group status abnormal cases: + * @case1 rebuild chain(different genesis), + * @case2 drop group data and restart chain(same genesis), when local smallest block height greater than 0, mark as CONFLICT + */ + private void checkSameChainDataWithLocal() { + log.info("start checkSameChainData."); + // get all group + List allNormalGroupList = getGroupList(GroupStatus.NORMAL.getValue()); + if (allNormalGroupList.isEmpty()) { + log.warn("checkSameChainData not found any group of front."); + return; + } + + for (TbGroup tbGroup : allNormalGroupList) { + int groupId = tbGroup.getGroupId(); + // find smallest block from db of group + TbBlock smallestBlockLocal = blockService.getSmallestBlockInfo(groupId); + // if no block in local db + if (smallestBlockLocal == null) { + continue; + } + BigInteger blockHeightLocal = smallestBlockLocal.getBlockNumber(); + Long blockTimestampLocal = NodeMgrTools.localDateTime2Timestamp(smallestBlockLocal.getBlockTimestamp()); + + // get same height block from chain, contrast block hash + BlockInfo smallestBlockOnChain = frontInterface.getBlockByNumber(groupId, blockHeightLocal); + // if no block in each node, not same chain + if (smallestBlockOnChain == null) { + log.warn("checkSameChainDataWithLocal block of {} on chain not exists, " + + "conflict with local block of same height", blockHeightLocal); + updateGroupStatus(groupId, GroupStatus.CONFLICT_LOCAL_DATA.getValue()); + continue; + } + Long blockTimestampOnChain = Long.valueOf(smallestBlockOnChain.getTimestamp()); + // drop nano second + blockTimestampOnChain = Math.round(blockTimestampOnChain / 1000.00) * 1000; + log.debug("checkSameChainData groupId:{},blockHeight:{},localTime:{},chainTime:{} ", + groupId, blockHeightLocal, blockTimestampLocal, blockTimestampOnChain); + // check same timestamp, the same chain + if (blockTimestampLocal.compareTo(blockTimestampOnChain) != 0) { + log.warn("checkSameChainDataWithLocal block of {} on chain conflicts with local block data", + blockHeightLocal); + updateGroupStatus(groupId, GroupStatus.CONFLICT_LOCAL_DATA.getValue()); + continue; + } else { + updateGroupStatus(groupId, GroupStatus.NORMAL.getValue()); + } + } + + } /** diff --git a/src/main/resources/mapper/BlockMapper.xml b/src/main/resources/mapper/BlockMapper.xml index d2faf828f..f51c679fc 100644 --- a/src/main/resources/mapper/BlockMapper.xml +++ b/src/main/resources/mapper/BlockMapper.xml @@ -25,6 +25,10 @@ select max(block_number) from ${tableName} + + insert ignore into ${tableName} (pk_hash,block_number,block_timestamp,trans_count,sealer_index,create_time,modify_time) @@ -79,4 +83,11 @@ DELETE tb from ${tableName} as tb,(SELECT max(block_number) maxBlock FROM ${tableName}) AS tmp where tb.block_number <= tmp.maxBlock - ${blockRetainMax} + + + \ No newline at end of file From 69b07ffe12f9946c91fe51fbc30ca705ba019659 Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Fri, 15 May 2020 15:21:18 +0800 Subject: [PATCH 047/131] add get block by number test --- .../java/node/mgr/test/frontInterface/FrontServiceTest.java | 4 ++-- src/test/java/node/mgr/test/group/GroupServiceTest.java | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/test/java/node/mgr/test/frontInterface/FrontServiceTest.java b/src/test/java/node/mgr/test/frontInterface/FrontServiceTest.java index 1c24ed197..be3a9b5eb 100644 --- a/src/test/java/node/mgr/test/frontInterface/FrontServiceTest.java +++ b/src/test/java/node/mgr/test/frontInterface/FrontServiceTest.java @@ -72,8 +72,8 @@ public void getTransactionTest() { @Test public void getBlockByNumberTest() { - BlockInfo blockInfo = frontInterface.getBlockByNumber(groupId, blockNumber); - assert (blockInfo != null); + BlockInfo blockInfo = frontInterface.getBlockByNumber(groupId, new BigInteger("1000")); +// assert (blockInfo != null); System.out.println(JSON.toJSONString(blockInfo)); } diff --git a/src/test/java/node/mgr/test/group/GroupServiceTest.java b/src/test/java/node/mgr/test/group/GroupServiceTest.java index 1377789fc..fd762176e 100644 --- a/src/test/java/node/mgr/test/group/GroupServiceTest.java +++ b/src/test/java/node/mgr/test/group/GroupServiceTest.java @@ -17,6 +17,7 @@ package node.mgr.test.group; import com.webank.webase.node.mgr.Application; +import com.webank.webase.node.mgr.base.tools.NodeMgrTools; import com.webank.webase.node.mgr.group.GroupService; import org.junit.Test; import org.junit.runner.RunWith; @@ -24,6 +25,9 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; +import java.time.LocalDateTime; +import java.time.ZoneOffset; + @RunWith(SpringRunner.class) @SpringBootTest(classes = Application.class) public class GroupServiceTest { From dc707d6eb18a55356287bcef16a21fc1c18b94ca Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Fri, 15 May 2020 15:29:13 +0800 Subject: [PATCH 048/131] fix page in group controller --- .../java/com/webank/webase/node/mgr/group/GroupController.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/webank/webase/node/mgr/group/GroupController.java b/src/main/java/com/webank/webase/node/mgr/group/GroupController.java index f6442553b..95f11db1c 100644 --- a/src/main/java/com/webank/webase/node/mgr/group/GroupController.java +++ b/src/main/java/com/webank/webase/node/mgr/group/GroupController.java @@ -129,7 +129,6 @@ public BasePageResponse getAllGroupIncludeInvalidGroup(@PathVariable("pageNumber List2Page list2Page = new List2Page(groupList, pageSize, pageNumber); pagesponse.setData(list2Page.getPagedList()); pagesponse.setTotalCount(count); - pagesponse.setData(groupList); } // reset group From 177ba4234d289e97f59e7da93bcf419d42cc670d Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Fri, 15 May 2020 15:50:53 +0800 Subject: [PATCH 049/131] add group list by status api --- .../node/mgr/group/GroupController.java | 38 ++++++++++++++++++- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/webank/webase/node/mgr/group/GroupController.java b/src/main/java/com/webank/webase/node/mgr/group/GroupController.java index 95f11db1c..8f74f94c2 100644 --- a/src/main/java/com/webank/webase/node/mgr/group/GroupController.java +++ b/src/main/java/com/webank/webase/node/mgr/group/GroupController.java @@ -19,6 +19,7 @@ import com.webank.webase.node.mgr.base.entity.BasePageResponse; import com.webank.webase.node.mgr.base.entity.BaseResponse; import com.webank.webase.node.mgr.base.enums.DataStatus; +import com.webank.webase.node.mgr.base.enums.GroupStatus; import com.webank.webase.node.mgr.base.exception.NodeMgrException; import com.webank.webase.node.mgr.base.properties.ConstantProperties; import com.webank.webase.node.mgr.base.tools.pagetools.List2Page; @@ -99,9 +100,9 @@ public BasePageResponse getAllGroup() throws NodeMgrException { log.info("start getAllGroup startTime:{}", startTime.toEpochMilli()); // get group list - int count = groupService.countOfGroup(null, DataStatus.NORMAL.getValue()); + int count = groupService.countOfGroup(null, GroupStatus.NORMAL.getValue()); if (count > 0) { - List groupList = groupService.getGroupList(DataStatus.NORMAL.getValue()); + List groupList = groupService.getGroupList(GroupStatus.NORMAL.getValue()); pagesponse.setTotalCount(count); pagesponse.setData(groupList); } @@ -115,6 +116,13 @@ public BasePageResponse getAllGroup() throws NodeMgrException { return pagesponse; } + /** + * get all group include invalid group status + * @param pageNumber + * @param pageSize + * @return + * @throws NodeMgrException + */ @GetMapping("/all/invalidIncluded/{pageNumber}/{pageSize}") public BasePageResponse getAllGroupIncludeInvalidGroup(@PathVariable("pageNumber") Integer pageNumber, @PathVariable("pageSize") Integer pageSize) throws NodeMgrException { @@ -140,6 +148,32 @@ public BasePageResponse getAllGroupIncludeInvalidGroup(@PathVariable("pageNumber return pagesponse; } + @GetMapping("/all/{groupStatus}") + public BaseResponse getAllGroupOfStatus(@PathVariable("groupStatus") Integer groupStatus) throws NodeMgrException { + if (groupStatus > GroupStatus.CONFLICT_LOCAL_DATA.getValue() + || groupStatus < GroupStatus.NORMAL.getValue()) { + return new BaseResponse(ConstantCode.INVALID_PARAM_INFO); + } + BaseResponse response = new BaseResponse(ConstantCode.SUCCESS); + Instant startTime = Instant.now(); + log.info("start getAllGroupOfStatus startTime:{}", startTime.toEpochMilli()); + + // get group list + int count = groupService.countOfGroup(null, groupStatus); + if (count > 0) { + List groupList = groupService.getGroupList(groupStatus); + response.setData(groupList); + } + + // reset group + resetGroupListTask.asyncResetGroupList(); + + log.info("end getAllGroupOfStatus useTime:{} result:{}", + Duration.between(startTime, Instant.now()).toMillis(), + JSON.toJSONString(response)); + return response; + } + /** * get trans daily. */ From 8bb749b054bb45176bde9ec9cf1f0de14867a97a Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Fri, 15 May 2020 18:59:31 +0800 Subject: [PATCH 050/131] add group test --- .../java/node/mgr/test/group/GroupControllerTest.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/test/java/node/mgr/test/group/GroupControllerTest.java b/src/test/java/node/mgr/test/group/GroupControllerTest.java index c818d82d3..f9e6aef7c 100644 --- a/src/test/java/node/mgr/test/group/GroupControllerTest.java +++ b/src/test/java/node/mgr/test/group/GroupControllerTest.java @@ -87,6 +87,17 @@ public void testGetAllInvalidIncluded() throws Exception { System.out.println("=================================response:"+resultActions.andReturn().getResponse().getContentAsString()); } + @Test + public void testGetAllInvalid() throws Exception { + + ResultActions resultActions = mockMvc.perform(MockMvcRequestBuilders.get("/group/all/4")); + resultActions. + andExpect(MockMvcResultMatchers.status().isOk()). + andDo(MockMvcResultHandlers.print()); + System.out.println("=================================response:"+resultActions.andReturn().getResponse().getContentAsString()); + } + + /** * dynamic group manage */ From 3ba78268e6d7a0beeee109c42eb82058b868938d Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Mon, 18 May 2020 16:12:47 +0800 Subject: [PATCH 051/131] fix timestamp floor --- .../java/com/webank/webase/node/mgr/group/GroupService.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/webank/webase/node/mgr/group/GroupService.java b/src/main/java/com/webank/webase/node/mgr/group/GroupService.java index 270e2ec03..701f7747d 100644 --- a/src/main/java/com/webank/webase/node/mgr/group/GroupService.java +++ b/src/main/java/com/webank/webase/node/mgr/group/GroupService.java @@ -425,14 +425,15 @@ private void checkSameChainDataWithLocal() { BlockInfo smallestBlockOnChain = frontInterface.getBlockByNumber(groupId, blockHeightLocal); // if no block in each node, not same chain if (smallestBlockOnChain == null) { - log.warn("checkSameChainDataWithLocal block of {} on chain not exists, " + + log.warn("checkSameChainDataWithLocal block of height: {} on chain not exists, " + "conflict with local block of same height", blockHeightLocal); updateGroupStatus(groupId, GroupStatus.CONFLICT_LOCAL_DATA.getValue()); continue; } Long blockTimestampOnChain = Long.valueOf(smallestBlockOnChain.getTimestamp()); // drop nano second - blockTimestampOnChain = Math.round(blockTimestampOnChain / 1000.00) * 1000; + Double floorValue = Math.floor(blockTimestampOnChain / 1000.00) * 1000; + blockTimestampOnChain = floorValue.longValue(); log.debug("checkSameChainData groupId:{},blockHeight:{},localTime:{},chainTime:{} ", groupId, blockHeightLocal, blockTimestampLocal, blockTimestampOnChain); // check same timestamp, the same chain From 3f16caa32b059b42fcc0e6b7461f63f45f85a51d Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Tue, 19 May 2020 18:10:21 +0800 Subject: [PATCH 052/131] fix check chain dirty data by block hash --- .../webase/node/mgr/group/GroupService.java | 24 +++++++++---------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/webank/webase/node/mgr/group/GroupService.java b/src/main/java/com/webank/webase/node/mgr/group/GroupService.java index 701f7747d..7304aac14 100644 --- a/src/main/java/com/webank/webase/node/mgr/group/GroupService.java +++ b/src/main/java/com/webank/webase/node/mgr/group/GroupService.java @@ -419,27 +419,25 @@ private void checkSameChainDataWithLocal() { continue; } BigInteger blockHeightLocal = smallestBlockLocal.getBlockNumber(); - Long blockTimestampLocal = NodeMgrTools.localDateTime2Timestamp(smallestBlockLocal.getBlockTimestamp()); + String blockHashLocal = smallestBlockLocal.getPkHash(); // get same height block from chain, contrast block hash BlockInfo smallestBlockOnChain = frontInterface.getBlockByNumber(groupId, blockHeightLocal); // if no block in each node, not same chain if (smallestBlockOnChain == null) { - log.warn("checkSameChainDataWithLocal block of height: {} on chain not exists, " + - "conflict with local block of same height", blockHeightLocal); + log.warn("checkSameChainDataWithLocal groupId: {} block of height: {} on chain not exists, " + + "conflict with local block of same height", groupId, blockHeightLocal); updateGroupStatus(groupId, GroupStatus.CONFLICT_LOCAL_DATA.getValue()); continue; } - Long blockTimestampOnChain = Long.valueOf(smallestBlockOnChain.getTimestamp()); - // drop nano second - Double floorValue = Math.floor(blockTimestampOnChain / 1000.00) * 1000; - blockTimestampOnChain = floorValue.longValue(); - log.debug("checkSameChainData groupId:{},blockHeight:{},localTime:{},chainTime:{} ", - groupId, blockHeightLocal, blockTimestampLocal, blockTimestampOnChain); - // check same timestamp, the same chain - if (blockTimestampLocal.compareTo(blockTimestampOnChain) != 0) { - log.warn("checkSameChainDataWithLocal block of {} on chain conflicts with local block data", - blockHeightLocal); + String blockHashOnChain = smallestBlockOnChain.getHash(); + log.debug("checkSameChainData groupId:{},blockHeight:{},localHash:{},chainHash:{} ", + groupId, blockHeightLocal, blockHashLocal, blockHashOnChain); + // check same block hash, the same chain + if (!blockHashLocal.equals(blockHashOnChain)) { + log.warn("checkSameChainDataWithLocal group: {} block of height:{} on chain " + + "conflicts with local block data", + groupId, blockHeightLocal); updateGroupStatus(groupId, GroupStatus.CONFLICT_LOCAL_DATA.getValue()); continue; } else { From 60fcfdb98ff7231e31708e0d02e88c9755665f26 Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Wed, 20 May 2020 11:08:59 +0800 Subject: [PATCH 053/131] add full list of group --- .../node/mgr/group/GroupController.java | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/webank/webase/node/mgr/group/GroupController.java b/src/main/java/com/webank/webase/node/mgr/group/GroupController.java index 8f74f94c2..46fc7ca7d 100644 --- a/src/main/java/com/webank/webase/node/mgr/group/GroupController.java +++ b/src/main/java/com/webank/webase/node/mgr/group/GroupController.java @@ -123,9 +123,10 @@ public BasePageResponse getAllGroup() throws NodeMgrException { * @return * @throws NodeMgrException */ - @GetMapping("/all/invalidIncluded/{pageNumber}/{pageSize}") - public BasePageResponse getAllGroupIncludeInvalidGroup(@PathVariable("pageNumber") Integer pageNumber, - @PathVariable("pageSize") Integer pageSize) throws NodeMgrException { + @GetMapping({"/all/invalidIncluded/{pageNumber}/{pageSize}", + "/all/invalidIncluded"}) + public BasePageResponse getAllGroupIncludeInvalidGroup(@PathVariable(value = "pageNumber",required = false) Integer pageNumber, + @PathVariable(value = "pageSize", required = false) Integer pageSize) throws NodeMgrException { BasePageResponse pagesponse = new BasePageResponse(ConstantCode.SUCCESS); Instant startTime = Instant.now(); log.info("start getAllGroupIncludeInvalidGroup startTime:{}", startTime.toEpochMilli()); @@ -134,11 +135,15 @@ public BasePageResponse getAllGroupIncludeInvalidGroup(@PathVariable("pageNumber int count = groupService.countOfGroup(null, null); if (count > 0) { List groupList = groupService.getGroupList(null); - List2Page list2Page = new List2Page(groupList, pageSize, pageNumber); - pagesponse.setData(list2Page.getPagedList()); - pagesponse.setTotalCount(count); + if (pageNumber == null && pageSize == null) { + pagesponse.setData(groupList); + pagesponse.setTotalCount(count); + } else { + List2Page list2Page = new List2Page(groupList, pageSize, pageNumber); + pagesponse.setData(list2Page.getPagedList()); + pagesponse.setTotalCount(count); + } } - // reset group resetGroupListTask.asyncResetGroupList(); From 3568cc63e4ca56f9ce15cfb5ffa4676bf4a99579 Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Wed, 20 May 2020 14:14:25 +0800 Subject: [PATCH 054/131] add input/message/root of tx receipt --- .../webase/node/mgr/transaction/TransHashController.java | 1 - .../webase/node/mgr/transaction/entity/TransReceipt.java | 9 +++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/webank/webase/node/mgr/transaction/TransHashController.java b/src/main/java/com/webank/webase/node/mgr/transaction/TransHashController.java index cdff084ef..438bf2e18 100644 --- a/src/main/java/com/webank/webase/node/mgr/transaction/TransHashController.java +++ b/src/main/java/com/webank/webase/node/mgr/transaction/TransHashController.java @@ -22,7 +22,6 @@ import com.webank.webase.node.mgr.base.enums.SqlSortType; import com.webank.webase.node.mgr.base.exception.NodeMgrException; import com.webank.webase.node.mgr.group.GroupService; -import com.webank.webase.node.mgr.group.entity.GroupGeneral; import com.webank.webase.node.mgr.transaction.entity.TbTransHash; import com.webank.webase.node.mgr.transaction.entity.TransListParam; import com.webank.webase.node.mgr.transaction.entity.TransReceipt; diff --git a/src/main/java/com/webank/webase/node/mgr/transaction/entity/TransReceipt.java b/src/main/java/com/webank/webase/node/mgr/transaction/entity/TransReceipt.java index 0a097e546..9b1211978 100644 --- a/src/main/java/com/webank/webase/node/mgr/transaction/entity/TransReceipt.java +++ b/src/main/java/com/webank/webase/node/mgr/transaction/entity/TransReceipt.java @@ -40,4 +40,13 @@ public class TransReceipt { private String transactionIndexRaw; private boolean statusOK; private String gasUsedRaw; + + private String root; + private String message; + private String input; + /** + * list of MerkleProofUnit + */ + private List txProof; + private List receiptProof; } From f22f8649df7af81997cacba66c7c7dd05444e57c Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Wed, 20 May 2020 14:22:25 +0800 Subject: [PATCH 055/131] add tx entity comment --- .../webase/node/mgr/transaction/entity/TransReceipt.java | 3 +++ .../webase/node/mgr/transaction/entity/TransactionInfo.java | 3 +++ 2 files changed, 6 insertions(+) diff --git a/src/main/java/com/webank/webase/node/mgr/transaction/entity/TransReceipt.java b/src/main/java/com/webank/webase/node/mgr/transaction/entity/TransReceipt.java index 9b1211978..b0f02cbd0 100644 --- a/src/main/java/com/webank/webase/node/mgr/transaction/entity/TransReceipt.java +++ b/src/main/java/com/webank/webase/node/mgr/transaction/entity/TransReceipt.java @@ -20,6 +20,9 @@ import java.util.List; import lombok.Data; +/** + * transaction receipt entity + */ @Data public class TransReceipt { diff --git a/src/main/java/com/webank/webase/node/mgr/transaction/entity/TransactionInfo.java b/src/main/java/com/webank/webase/node/mgr/transaction/entity/TransactionInfo.java index d02d798e7..be9f0bc8c 100644 --- a/src/main/java/com/webank/webase/node/mgr/transaction/entity/TransactionInfo.java +++ b/src/main/java/com/webank/webase/node/mgr/transaction/entity/TransactionInfo.java @@ -18,6 +18,9 @@ import java.math.BigInteger; import lombok.Data; +/** + * transaction info entity (different from receipt) + */ @Data public class TransactionInfo { private String hash; From b89fe5e6cd1c49954a3e1f2e37096063bc60d5ba Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Wed, 20 May 2020 15:55:54 +0800 Subject: [PATCH 056/131] fix repeat groupid --- .../webase/node/mgr/scheduler/PullBlockTransTask.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/webank/webase/node/mgr/scheduler/PullBlockTransTask.java b/src/main/java/com/webank/webase/node/mgr/scheduler/PullBlockTransTask.java index 521b224dd..d979c742c 100644 --- a/src/main/java/com/webank/webase/node/mgr/scheduler/PullBlockTransTask.java +++ b/src/main/java/com/webank/webase/node/mgr/scheduler/PullBlockTransTask.java @@ -13,9 +13,10 @@ */ package com.webank.webase.node.mgr.scheduler; +import com.webank.webase.node.mgr.base.enums.GroupStatus; import com.webank.webase.node.mgr.block.BlockService; -import com.webank.webase.node.mgr.frontgroupmap.entity.FrontGroup; -import com.webank.webase.node.mgr.frontgroupmap.entity.FrontGroupMapCache; +import com.webank.webase.node.mgr.group.GroupService; +import com.webank.webase.node.mgr.group.entity.TbGroup; import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; @@ -37,7 +38,7 @@ public class PullBlockTransTask { @Autowired private BlockService blockService; @Autowired - private FrontGroupMapCache frontGroupMapCache; + private GroupService groupService; @Scheduled(fixedDelayString = "${constant.pullBlockTaskFixedDelay}") public void taskStart() { @@ -50,7 +51,7 @@ public void taskStart() { public synchronized void pullBlockStart() { Instant startTime = Instant.now(); log.info("start pullBLock startTime:{}", startTime.toEpochMilli()); - List groupList = frontGroupMapCache.getAllMap(); + List groupList = groupService.getGroupList(GroupStatus.NORMAL.getValue()); if (groupList == null || groupList.size() == 0) { log.warn("pullBlock jump over: not found any group"); return; From e1848f1c446a5e3d163700320b4f5cdf40e5fae7 Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Thu, 21 May 2020 14:16:14 +0800 Subject: [PATCH 057/131] add null genesisi block warn log --- .../java/com/webank/webase/node/mgr/group/GroupService.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/com/webank/webase/node/mgr/group/GroupService.java b/src/main/java/com/webank/webase/node/mgr/group/GroupService.java index 7304aac14..541cecac4 100644 --- a/src/main/java/com/webank/webase/node/mgr/group/GroupService.java +++ b/src/main/java/com/webank/webase/node/mgr/group/GroupService.java @@ -381,6 +381,7 @@ private void checkGroupGenesisSameWithEach() { BlockInfo genesisBlock = frontInterface.getBlockByNumberFromSpecificFront(frontIp, frontPort, groupId, BigInteger.ZERO); if (genesisBlock == null) { + log.warn("checkGroupGenesisSameWithEach getGenesisBlock is null"); continue; } if (!"".equals(lastBlockHash) && !lastBlockHash.equals(genesisBlock.getHash())) { @@ -416,6 +417,7 @@ private void checkSameChainDataWithLocal() { TbBlock smallestBlockLocal = blockService.getSmallestBlockInfo(groupId); // if no block in local db if (smallestBlockLocal == null) { + log.warn("checkSameChainDataWithLocal smallestBlockLocal is null"); continue; } BigInteger blockHeightLocal = smallestBlockLocal.getBlockNumber(); @@ -441,6 +443,7 @@ private void checkSameChainDataWithLocal() { updateGroupStatus(groupId, GroupStatus.CONFLICT_LOCAL_DATA.getValue()); continue; } else { + log.warn("checkSameChainDataWithLocal set groupId:{} as normal", groupId); updateGroupStatus(groupId, GroupStatus.NORMAL.getValue()); } } From dd4693decbd7d8123889b596598cdc4d5976e45a Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Thu, 21 May 2020 16:03:28 +0800 Subject: [PATCH 058/131] add deploy permission check --- .../node/mgr/base/code/ConstantCode.java | 3 + .../node/mgr/contract/ContractService.java | 55 ++++++++++++++++--- .../frontinterface/FrontInterfaceService.java | 4 +- .../webase/node/mgr/group/GroupService.java | 3 +- .../PermissionManageController.java | 4 +- .../permission/PermissionManageService.java | 17 +++--- 6 files changed, 66 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/webank/webase/node/mgr/base/code/ConstantCode.java b/src/main/java/com/webank/webase/node/mgr/base/code/ConstantCode.java index 764718ab3..b461492cd 100644 --- a/src/main/java/com/webank/webase/node/mgr/base/code/ConstantCode.java +++ b/src/main/java/com/webank/webase/node/mgr/base/code/ConstantCode.java @@ -32,6 +32,9 @@ public class ConstantCode { public static final RetCode SYSTEM_EXCEPTION = RetCode.mark(102000, "system exception"); public static final RetCode SYSTEM_EXCEPTION_GET_PRIVATE_KEY_FAIL = RetCode.mark(102000, "system exception: please check front"); + /* front error code */ + public static final RetCode PERMISSION_DENIED = RetCode.mark(201202, "permission denied"); + /** * Business exception. */ diff --git a/src/main/java/com/webank/webase/node/mgr/contract/ContractService.java b/src/main/java/com/webank/webase/node/mgr/contract/ContractService.java index 07e621bd8..94566797f 100644 --- a/src/main/java/com/webank/webase/node/mgr/contract/ContractService.java +++ b/src/main/java/com/webank/webase/node/mgr/contract/ContractService.java @@ -18,28 +18,29 @@ import com.webank.webase.node.mgr.abi.AbiService; import com.webank.webase.node.mgr.abi.entity.AbiInfo; import com.webank.webase.node.mgr.base.code.ConstantCode; +import com.webank.webase.node.mgr.base.entity.BasePageResponse; import com.webank.webase.node.mgr.base.enums.ContractStatus; import com.webank.webase.node.mgr.base.exception.NodeMgrException; -import com.webank.webase.node.mgr.base.properties.ConstantProperties; import com.webank.webase.node.mgr.base.tools.Web3Tools; import com.webank.webase.node.mgr.contract.entity.Contract; import com.webank.webase.node.mgr.contract.entity.ContractParam; import com.webank.webase.node.mgr.contract.entity.DeployInputParam; import com.webank.webase.node.mgr.contract.entity.TbContract; import com.webank.webase.node.mgr.contract.entity.TransactionInputParam; +import com.webank.webase.node.mgr.event.entity.NewBlockEventInfo; import com.webank.webase.node.mgr.front.entity.TransactionParam; import com.webank.webase.node.mgr.frontinterface.FrontInterfaceService; import com.webank.webase.node.mgr.frontinterface.FrontRestTools; import com.webank.webase.node.mgr.frontinterface.entity.PostAbiInfo; import com.webank.webase.node.mgr.monitor.MonitorService; +import com.webank.webase.node.mgr.precompiled.permission.PermissionManageService; import com.webank.webase.node.mgr.user.UserService; import java.time.LocalDateTime; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; +import java.util.*; + import lombok.extern.log4j.Log4j2; import org.apache.commons.lang3.StringUtils; +import org.fisco.bcos.web3j.precompile.permission.PermissionInfo; import org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -53,6 +54,9 @@ @Service public class ContractService { + private static final int CONTRACT_ADDRESS_LENGTH = 42; + private static final String PERMISSION_TYPE_DEPLOY_AND_CREATE = "deployAndCreate"; + @Autowired private ContractMapper contractMapper; @Autowired @@ -64,9 +68,11 @@ public class ContractService { private FrontInterfaceService frontInterface; @Autowired private UserService userService; - private static final int CONTRACT_ADDRESS_LENGTH = 42; @Autowired private AbiService abiService; + @Autowired + private PermissionManageService permissionManageService; + /** * add new contract data. */ @@ -202,8 +208,11 @@ public List queryContractByBin(Integer groupId, String contractBin) public TbContract deployContract(DeployInputParam inputParam) throws NodeMgrException { log.info("start deployContract. inputParam:{}", JSON.toJSONString(inputParam)); int groupId = inputParam.getGroupId(); + + // check deploy permission + checkDeployPermission(groupId, inputParam.getUser()); + String contractName = inputParam.getContractName(); - // String version = Instant.now().toEpochMilli() + ""; //check contract verifyContractNotDeploy(inputParam.getContractId(), inputParam.getGroupId()); //check contractName @@ -217,7 +226,9 @@ public TbContract deployContract(DeployInputParam inputParam) throws NodeMgrExce } // deploy param + // get signUserId String signUserId = userService.getSignUserIdByAddress(groupId, inputParam.getUser()); + Map params = new HashMap<>(); params.put("groupId", groupId); params.put("signUserId", signUserId); @@ -428,4 +439,34 @@ public void sendAbi(int groupId, int contractId, String address) { contract.setDescription("address add by sendAbi"); contractMapper.update(contract); } + + /** + * check user deploy permission + */ + private void checkDeployPermission(int groupId, String userAddress) { + // get deploy permission list + List deployUserList = new ArrayList<>(); + BasePageResponse response = permissionManageService.listPermissionFull(groupId, PERMISSION_TYPE_DEPLOY_AND_CREATE, null); + if (response.getCode() != 0) { + log.error("checkDeployPermission get permission list error"); + return; + } else { + List listData = (List) response.getData(); + deployUserList = JSON.parseArray(JSON.toJSONString(listData), PermissionInfo.class); + } + + // check user in the list, + if (deployUserList.isEmpty()) { + return; + } else { + long count = 0; + count = deployUserList.stream().filter( admin -> userAddress.equals(admin.getAddress())).count(); + // if not in the list, permission denied + if (count == 0) { + log.error("checkDeployPermission permission denied for user:{}", userAddress); + throw new NodeMgrException(ConstantCode.PERMISSION_DENIED); + } + } + } + } diff --git a/src/main/java/com/webank/webase/node/mgr/frontinterface/FrontInterfaceService.java b/src/main/java/com/webank/webase/node/mgr/frontinterface/FrontInterfaceService.java index 2f5c9f7fe..f627f0340 100644 --- a/src/main/java/com/webank/webase/node/mgr/frontinterface/FrontInterfaceService.java +++ b/src/main/java/com/webank/webase/node/mgr/frontinterface/FrontInterfaceService.java @@ -505,7 +505,7 @@ public List getNewBlockEventInfo(String frontIp, Integer fron if (response.getData() == null) { return new ArrayList<>(); } - List data = (List) response.getData(); + List data = (List) response.getData(); List resList = JSON.parseArray(JSON.toJSONString(data), NewBlockEventInfo.class); resList.forEach(info -> info.setFrontInfo(frontIp)); return resList; @@ -521,7 +521,7 @@ public List getContractEventInfo(String frontIp, Integer fron if (response.getData() == null) { return new ArrayList<>(); } - List data = (List) response.getData(); + List data = (List) response.getData(); List resList = JSON.parseArray(JSON.toJSONString(data), ContractEventInfo.class); resList.forEach(info -> info.setFrontInfo(frontIp)); return resList; diff --git a/src/main/java/com/webank/webase/node/mgr/group/GroupService.java b/src/main/java/com/webank/webase/node/mgr/group/GroupService.java index 541cecac4..2f59470a2 100644 --- a/src/main/java/com/webank/webase/node/mgr/group/GroupService.java +++ b/src/main/java/com/webank/webase/node/mgr/group/GroupService.java @@ -257,7 +257,8 @@ private void savePeerList(String frontIp, Integer frontPort, int groupId, List groupId == ln.getGroupId() && nodeId.equals(ln.getNodeId())).count(); + ln -> nodeId.equals(ln.getNodeId()) && groupId == ln.getGroupId()).count(); + // local node not contains this one: if (count == 0) { PeerInfo newPeer = peerList.stream().filter(peer -> nodeId.equals(peer.getNodeId())) .findFirst().orElseGet(() -> new PeerInfo(nodeId)); diff --git a/src/main/java/com/webank/webase/node/mgr/precompiled/permission/PermissionManageController.java b/src/main/java/com/webank/webase/node/mgr/precompiled/permission/PermissionManageController.java index 9b6d325ae..bfbb7b8e1 100644 --- a/src/main/java/com/webank/webase/node/mgr/precompiled/permission/PermissionManageController.java +++ b/src/main/java/com/webank/webase/node/mgr/precompiled/permission/PermissionManageController.java @@ -88,7 +88,7 @@ public Object listPermissionManager( Instant startTime = Instant.now(); log.info("start listPermissionManager startTime:{}", startTime.toEpochMilli()); - Object result = permissionManageService.listPermission(groupId, permissionType, tableName, pageSize, pageNumber); + Object result = permissionManageService.listPermissionPaged(groupId, permissionType, tableName, pageSize, pageNumber); log.info("end listPermissionManager useTime:{} result:{}", Duration.between(startTime, Instant.now()).toMillis(), JSON.toJSONString(result)); @@ -109,7 +109,7 @@ public Object listFullPermissionManager( Instant startTime = Instant.now(); log.info("start listFullPermissionManager startTime:{}", startTime.toEpochMilli()); - Object result = permissionManageService.getPermissionFullList(groupId, permissionType, tableName); + Object result = permissionManageService.listPermissionFull(groupId, permissionType, tableName); log.info("end listFullPermissionManager useTime:{} result:{}", Duration.between(startTime, Instant.now()).toMillis(), JSON.toJSONString(result)); diff --git a/src/main/java/com/webank/webase/node/mgr/precompiled/permission/PermissionManageService.java b/src/main/java/com/webank/webase/node/mgr/precompiled/permission/PermissionManageService.java index 91c3bdd3e..e85973420 100644 --- a/src/main/java/com/webank/webase/node/mgr/precompiled/permission/PermissionManageService.java +++ b/src/main/java/com/webank/webase/node/mgr/precompiled/permission/PermissionManageService.java @@ -17,6 +17,7 @@ import com.alibaba.fastjson.JSON; import com.webank.webase.node.mgr.base.code.ConstantCode; +import com.webank.webase.node.mgr.base.entity.BasePageResponse; import com.webank.webase.node.mgr.base.exception.NodeMgrException; import com.webank.webase.node.mgr.base.tools.HttpRequestTools; import com.webank.webase.node.mgr.frontinterface.FrontInterfaceService; @@ -25,6 +26,7 @@ import com.webank.webase.node.mgr.user.entity.TbUser; import com.webank.webase.node.mgr.user.entity.UserParam; import lombok.extern.log4j.Log4j2; +import org.fisco.bcos.web3j.precompile.permission.PermissionInfo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -97,8 +99,8 @@ public Map listPermissionStateFull(int groupId) { /** * get paged permission list */ - public Object listPermission(int groupId, String permissionType, String tableName, int pageSize, int pageNumber) { - log.debug("start listPermission. groupId:{}, permissionType:{}" , groupId , permissionType); + public Object listPermissionPaged(int groupId, String permissionType, String tableName, int pageSize, int pageNumber) { + log.debug("start listPermissionPaged. groupId:{}, permissionType:{}" , groupId , permissionType); String uri; Map map = new HashMap<>(); map.put("groupId", String.valueOf(groupId)); @@ -113,15 +115,15 @@ public Object listPermission(int groupId, String permissionType, String tableNam } Object frontRsp = frontRestTools.getForEntity(groupId, uri, Object.class); - log.debug("end listPermission. frontRsp:{}", JSON.toJSONString(frontRsp)); + log.debug("end listPermissionPaged. frontRsp:{}", JSON.toJSONString(frontRsp)); return frontRsp; } /** * get full permission list */ - public Object getPermissionFullList(int groupId, String permissionType, String tableName) { - log.debug("start getPermissionFullList. groupId:{}, permissionType:{}" , groupId , permissionType); + public BasePageResponse listPermissionFull(int groupId, String permissionType, String tableName) { + log.debug("start listPermissionFull. groupId:{}, permissionType:{}" , groupId , permissionType); String uri; Map map = new HashMap<>(); map.put("groupId", String.valueOf(groupId)); @@ -132,9 +134,8 @@ public Object getPermissionFullList(int groupId, String permissionType, String t map.put("tableName", tableName); uri = HttpRequestTools.getQueryUri(FrontRestTools.URI_PERMISSION_FULL_LIST, map); } - - Object frontRsp = frontRestTools.getForEntity(groupId, uri, Object.class); - log.debug("end getPermissionFullList. frontRsp:{}", JSON.toJSONString(frontRsp)); + BasePageResponse frontRsp = frontRestTools.getForEntity(groupId, uri, BasePageResponse.class); + log.debug("end listPermissionFull. frontRsp:{}", JSON.toJSONString(frontRsp)); return frontRsp; } From bb16ea598bb6207b67b43f856b4ba56f71363a44 Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Thu, 21 May 2020 16:17:44 +0800 Subject: [PATCH 059/131] add address 0x00 validation --- .../webank/webase/node/mgr/contract/ContractController.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/com/webank/webase/node/mgr/contract/ContractController.java b/src/main/java/com/webank/webase/node/mgr/contract/ContractController.java index 5753c174b..90e0d368c 100644 --- a/src/main/java/com/webank/webase/node/mgr/contract/ContractController.java +++ b/src/main/java/com/webank/webase/node/mgr/contract/ContractController.java @@ -34,6 +34,7 @@ import java.util.Optional; import javax.validation.Valid; import lombok.extern.log4j.Log4j2; +import org.fisco.bcos.web3j.abi.datatypes.Address; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; @@ -183,6 +184,10 @@ public BaseResponse deployContract(@RequestBody @Valid DeployInputParam deployIn public BaseResponse sendTransaction(@RequestBody @Valid TransactionInputParam param, BindingResult result) throws NodeMgrException { checkBindResult(result); + // 0x0000000000000000000000000000000000000000 address is invalid + if (Address.DEFAULT.toString().equals(param.getContractAddress())) { + throw new NodeMgrException(ConstantCode.CONTRACT_ADDRESS_INVALID); + } Instant startTime = Instant.now(); log.info("start sendTransaction startTime:{} param:{}", startTime.toEpochMilli(), JSON.toJSONString(param)); From 453560b5c2bdaa00cc8d4cf1ea9e3d05a06056d7 Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Thu, 21 May 2020 22:28:15 +0800 Subject: [PATCH 060/131] add import pri & pem --- .../node/mgr/base/code/ConstantCode.java | 2 + .../node/mgr/base/tools/NodeMgrTools.java | 9 ++- .../webase/node/mgr/base/tools/PemUtils.java | 68 ++++++++++++++++ .../mgr/frontinterface/FrontRestTools.java | 1 + .../webase/node/mgr/user/UserController.java | 56 +++++++++++-- .../webase/node/mgr/user/UserService.java | 81 +++++++++++++------ .../node/mgr/user/entity/ReqImportPem.java | 16 ++++ .../mgr/user/entity/ReqImportPrivateKey.java | 29 +++++++ 8 files changed, 230 insertions(+), 32 deletions(-) create mode 100644 src/main/java/com/webank/webase/node/mgr/base/tools/PemUtils.java create mode 100644 src/main/java/com/webank/webase/node/mgr/user/entity/ReqImportPem.java create mode 100644 src/main/java/com/webank/webase/node/mgr/user/entity/ReqImportPrivateKey.java diff --git a/src/main/java/com/webank/webase/node/mgr/base/code/ConstantCode.java b/src/main/java/com/webank/webase/node/mgr/base/code/ConstantCode.java index b461492cd..c222b094d 100644 --- a/src/main/java/com/webank/webase/node/mgr/base/code/ConstantCode.java +++ b/src/main/java/com/webank/webase/node/mgr/base/code/ConstantCode.java @@ -34,6 +34,8 @@ public class ConstantCode { /* front error code */ public static final RetCode PERMISSION_DENIED = RetCode.mark(201202, "permission denied"); + public static final RetCode PEM_FORMAT_ERROR = RetCode.mark(201232, "Pem file format error, must surrounded by -----XXXXX PRIVATE KEY-----"); + public static final RetCode PEM_CONTENT_ERROR = RetCode.mark(201233, "Pem file content error"); /** * Business exception. diff --git a/src/main/java/com/webank/webase/node/mgr/base/tools/NodeMgrTools.java b/src/main/java/com/webank/webase/node/mgr/base/tools/NodeMgrTools.java index f9f0347ec..a669ce1d4 100644 --- a/src/main/java/com/webank/webase/node/mgr/base/tools/NodeMgrTools.java +++ b/src/main/java/com/webank/webase/node/mgr/base/tools/NodeMgrTools.java @@ -26,13 +26,11 @@ import java.net.Socket; import java.net.SocketAddress; import java.security.MessageDigest; -import java.sql.Timestamp; import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZoneOffset; import java.time.format.DateTimeFormatter; -import java.time.temporal.ChronoField; import java.util.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -532,4 +530,11 @@ public static String removeFirstStr(String input, String target) { } return input; } + + public static String encodedBase64Str(String input) { + if (input == null) { + return null; + } + return Base64.getEncoder().encodeToString(input.getBytes()); + } } diff --git a/src/main/java/com/webank/webase/node/mgr/base/tools/PemUtils.java b/src/main/java/com/webank/webase/node/mgr/base/tools/PemUtils.java new file mode 100644 index 000000000..97196cef3 --- /dev/null +++ b/src/main/java/com/webank/webase/node/mgr/base/tools/PemUtils.java @@ -0,0 +1,68 @@ +/** + * Copyright 2014-2020 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.webank.webase.node.mgr.base.tools; + +import com.webank.webase.node.mgr.base.code.ConstantCode; +import com.webank.webase.node.mgr.base.exception.NodeMgrException; + +import java.util.ArrayList; +import java.util.List; + +public class PemUtils { + public static final String crtContentHead = "-----BEGIN PRIVATE KEY-----\n" ; + public static final String crtContentTail = "-----END PRIVATE KEY-----\n" ; + public static final String crtTailForConcat = "\n-----END PRIVATE KEY-----\n" ; + + /** + * get pem file's content without head and tail + * support importing multiple pem private key + * @param certContent + * @return + */ + public static List getPemBareContentList(String certContent) { + List list = new ArrayList<>(); + if(!certContent.startsWith(crtContentHead)){ + throw new NodeMgrException(ConstantCode.PEM_FORMAT_ERROR); + } + String[] nodeCrtStrArray = certContent.split(crtContentHead); + for(int i = 0; i < nodeCrtStrArray.length; i++) { + String[] nodeCrtStrArray2 = nodeCrtStrArray[i].split(crtContentTail); + for(int j = 0; j < nodeCrtStrArray2.length; j++) { + String ca = nodeCrtStrArray2[j]; + if(ca.length() != 0) { + list.add(formatPemStr(ca)); + } + } + } + return list; + } + + public static String formatPemStr(String string) { + return string.substring(0, string.length() - 1); + } + + /** + * 给pem的内容加上头和尾 + * begin ... + * end ... + */ + public static String addPemHeadAndTail(String certContent) { + String headToConcat = crtContentHead; + String fullCert = headToConcat.concat(certContent).concat(crtTailForConcat); + return fullCert; + } +} diff --git a/src/main/java/com/webank/webase/node/mgr/frontinterface/FrontRestTools.java b/src/main/java/com/webank/webase/node/mgr/frontinterface/FrontRestTools.java index 5f4318d76..0d0456c9b 100644 --- a/src/main/java/com/webank/webase/node/mgr/frontinterface/FrontRestTools.java +++ b/src/main/java/com/webank/webase/node/mgr/frontinterface/FrontRestTools.java @@ -79,6 +79,7 @@ public class FrontRestTools { public static final String FRONT_PERFORMANCE_CONFIG = "performance/config"; public static final String URI_KEY_PAIR = "privateKey?type=2&userName=%s&signUserId=%s&appId=%s"; public static final String URI_KEY_PAIR_LOCAL_KEYSTORE = "privateKey/localKeyStores"; + public static final String URI_KEY_PAIR_IMPORT_WITH_SIGN = "privateKey/importWithSign"; public static final String URI_CONTRACT_DEPLOY_WITH_SIGN = "contract/deployWithSign"; public static final String URI_CONTRACT_SENDABI = "contract/abiInfo"; public static final String URI_SEND_TRANSACTION_WITH_SIGN = "trans/handleWithSign"; diff --git a/src/main/java/com/webank/webase/node/mgr/user/UserController.java b/src/main/java/com/webank/webase/node/mgr/user/UserController.java index bdc332c46..480dfb038 100644 --- a/src/main/java/com/webank/webase/node/mgr/user/UserController.java +++ b/src/main/java/com/webank/webase/node/mgr/user/UserController.java @@ -20,11 +20,9 @@ import com.webank.webase.node.mgr.base.code.ConstantCode; import com.webank.webase.node.mgr.base.exception.NodeMgrException; import com.webank.webase.node.mgr.base.properties.ConstantProperties; -import com.webank.webase.node.mgr.user.entity.BindUserInputParam; -import com.webank.webase.node.mgr.user.entity.NewUserInputParam; -import com.webank.webase.node.mgr.user.entity.TbUser; -import com.webank.webase.node.mgr.user.entity.UpdateUserInputParam; -import com.webank.webase.node.mgr.user.entity.UserParam; +import com.webank.webase.node.mgr.base.tools.PemUtils; +import com.webank.webase.node.mgr.user.entity.*; + import java.time.Duration; import java.time.Instant; import java.util.List; @@ -66,7 +64,8 @@ public BaseResponse addUserInfo(@RequestBody @Valid NewUserInputParam user, Instant startTime = Instant.now(); // add user row - Integer userId = userService.addUserInfo(user); + Integer userId = userService.addUserInfo(user.getGroupId(), + user.getUserName(), user.getDescription(), user.getUserType(), null); // query user row TbUser userRow = userService.queryByUserId(userId); @@ -160,4 +159,49 @@ public BasePageResponse userList(@PathVariable("groupId") Integer groupId, Duration.between(startTime, Instant.now()).toMillis(), JSON.toJSONString(pagesponse)); return pagesponse; } + + @PostMapping("/import") + @PreAuthorize(ConstantProperties.HAS_ROLE_ADMIN) + public BaseResponse importPrivateKey(@Valid @RequestBody ReqImportPrivateKey reqImport, BindingResult result) { + checkBindResult(result); + BaseResponse baseResponse = new BaseResponse(ConstantCode.SUCCESS); + Instant startTime = Instant.now(); + + // encoded by web in base64 + String privateKeyEncoded = reqImport.getPrivateKey(); + // add user row + Integer userId = userService.addUserInfo(reqImport.getGroupId(), reqImport.getUserName(), + reqImport.getDescription(), reqImport.getUserType(), privateKeyEncoded); + + // query user row + TbUser userRow = userService.queryByUserId(userId); + baseResponse.setData(userRow); + + log.info("end importPrivateKey useTime:{} result:{}", + Duration.between(startTime, Instant.now()).toMillis(), JSON.toJSONString(baseResponse)); + return baseResponse; + } + + @PostMapping("/importPem") + @PreAuthorize(ConstantProperties.HAS_ROLE_ADMIN) + public BaseResponse importPemPrivateKey(@Valid @RequestBody ReqImportPem reqImportPem, BindingResult result) { + checkBindResult(result); + BaseResponse baseResponse = new BaseResponse(ConstantCode.SUCCESS); + Instant startTime = Instant.now(); + + String pemContent = reqImportPem.getPemContent(); + if(!pemContent.startsWith(PemUtils.crtContentHead)) { + throw new NodeMgrException(ConstantCode.PEM_FORMAT_ERROR); + } + // import + Integer userId = userService.importPem(reqImportPem); + // query user row + TbUser userRow = userService.queryByUserId(userId); + baseResponse.setData(userRow); + + log.info("end importPemPrivateKey useTime:{} result:{}", + Duration.between(startTime, Instant.now()).toMillis(), JSON.toJSONString(baseResponse)); + return baseResponse; + } + } diff --git a/src/main/java/com/webank/webase/node/mgr/user/UserService.java b/src/main/java/com/webank/webase/node/mgr/user/UserService.java index e9ea48fb9..76cfb9607 100644 --- a/src/main/java/com/webank/webase/node/mgr/user/UserService.java +++ b/src/main/java/com/webank/webase/node/mgr/user/UserService.java @@ -18,23 +18,20 @@ import com.webank.webase.node.mgr.base.enums.HasPk; import com.webank.webase.node.mgr.base.exception.NodeMgrException; import com.webank.webase.node.mgr.base.properties.ConstantProperties; +import com.webank.webase.node.mgr.base.tools.NodeMgrTools; import com.webank.webase.node.mgr.base.tools.Web3Tools; import com.webank.webase.node.mgr.frontinterface.FrontRestTools; import com.webank.webase.node.mgr.group.GroupService; import com.webank.webase.node.mgr.monitor.MonitorService; -import com.webank.webase.node.mgr.user.entity.BindUserInputParam; -import com.webank.webase.node.mgr.user.entity.KeyPair; -import com.webank.webase.node.mgr.user.entity.NewUserInputParam; -import com.webank.webase.node.mgr.user.entity.TbUser; -import com.webank.webase.node.mgr.user.entity.UpdateUserInputParam; -import com.webank.webase.node.mgr.user.entity.UserParam; -import java.util.List; -import java.util.Objects; -import java.util.Optional; -import java.util.UUID; +import com.webank.webase.node.mgr.user.entity.*; + +import java.io.ByteArrayInputStream; +import java.util.*; import lombok.extern.log4j.Log4j2; import org.apache.commons.lang3.StringUtils; +import org.fisco.bcos.channel.client.PEMManager; +import org.fisco.bcos.web3j.utils.Numeric; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; @@ -61,16 +58,15 @@ public class UserService { * add new user data. */ @Transactional - public Integer addUserInfo(NewUserInputParam user) throws NodeMgrException { - log.debug("start addUserInfo User:{}", JSON.toJSONString(user)); - - Integer groupId = user.getGroupId(); - + public Integer addUserInfo(Integer groupId, String userName, String description, + Integer userType, String privateKeyEncoded) throws NodeMgrException { + log.debug("start addUserInfo groupId:{},userName:{},description:{},userType:{},", + groupId, userName, description, userType); // check group id groupService.checkGroupId(groupId); // check userName - TbUser userRow = queryByName(user.getGroupId(), user.getUserName()); + TbUser userRow = queryByName(groupId, userName); if (userRow != null) { log.warn("fail addUserInfo. user info already exists"); throw new NodeMgrException(ConstantCode.USER_EXISTS); @@ -78,10 +74,23 @@ public Integer addUserInfo(NewUserInputParam user) throws NodeMgrException { // add user by webase-front->webase-sign String signUserId = UUID.randomUUID().toString().replaceAll("-",""); // group id as appId - String appId = user.getGroupId().toString(); - String keyUri = String.format(FrontRestTools.URI_KEY_PAIR, user.getUserName(), signUserId, appId); - KeyPair keyPair = frontRestTools - .getForEntity(groupId, keyUri, KeyPair.class); + String appId = groupId.toString(); + + // request sign + KeyPair keyPair; + if (StringUtils.isNotBlank(privateKeyEncoded)) { + Map param = new HashMap<>(); + param.put("signUserId", signUserId); + param.put("appId", appId); + // already encoded privateKey + param.put("privateKey", privateKeyEncoded); + keyPair = frontRestTools.postForEntity(groupId, FrontRestTools.URI_KEY_PAIR_IMPORT_WITH_SIGN, + param, KeyPair.class); + } else { + String keyUri = String.format(FrontRestTools.URI_KEY_PAIR, userName, signUserId, appId); + keyPair = frontRestTools.getForEntity(groupId, keyUri, KeyPair.class); + } + String publicKey = Optional.ofNullable(keyPair).map(k -> k.getPublicKey()).orElse(null); String address = Optional.ofNullable(keyPair).map(k -> k.getAddress()).orElse(null); @@ -91,9 +100,8 @@ public Integer addUserInfo(NewUserInputParam user) throws NodeMgrException { } // add row - TbUser newUserRow = new TbUser(HasPk.HAS.getValue(), user.getUserType(), user.getUserName(), - groupId, address, publicKey, - user.getDescription()); + TbUser newUserRow = new TbUser(HasPk.HAS.getValue(), userType, userName, + groupId, address, publicKey, description); newUserRow.setSignUserId(signUserId); newUserRow.setAppId(appId); Integer affectRow = userMapper.addUserRow(newUserRow); @@ -103,7 +111,7 @@ public Integer addUserInfo(NewUserInputParam user) throws NodeMgrException { } // update monitor unusual user's info - monitorService.updateUnusualUser(groupId, user.getUserName(), address); + monitorService.updateUnusualUser(groupId, userName, address); Integer userId = newUserRow.getUserId(); log.debug("end addNodeInfo userId:{}", userId); @@ -309,4 +317,29 @@ public void deleteByAddress(String address) throws NodeMgrException{ userMapper.deleteByAddress(address); log.debug("end deleteByAddress"); } + + /** + * import pem file to import privateKey + * @param reqImportPem + * @return userId + */ + public Integer importPem(ReqImportPem reqImportPem) { + PEMManager pemManager = new PEMManager(); + String privateKey; + try { + String pemContent = reqImportPem.getPemContent(); + pemManager.load(new ByteArrayInputStream(pemContent.getBytes())); + privateKey = Numeric.toHexStringNoPrefix(pemManager.getECKeyPair().getPrivateKey()); + }catch (Exception e) { + log.error("importKeyStoreFromPem error:[]", e); + throw new NodeMgrException(ConstantCode.PEM_CONTENT_ERROR); + } + // pem's privateKey encoded here + String privateKeyEncoded = NodeMgrTools.encodedBase64Str(privateKey); + + // store local and save in sign + Integer userId = addUserInfo(reqImportPem.getGroupId(), reqImportPem.getUserName(), + reqImportPem.getDescription(), reqImportPem.getUserType(), privateKeyEncoded); + return userId; + } } \ No newline at end of file diff --git a/src/main/java/com/webank/webase/node/mgr/user/entity/ReqImportPem.java b/src/main/java/com/webank/webase/node/mgr/user/entity/ReqImportPem.java new file mode 100644 index 000000000..bbb0d02c6 --- /dev/null +++ b/src/main/java/com/webank/webase/node/mgr/user/entity/ReqImportPem.java @@ -0,0 +1,16 @@ +package com.webank.webase.node.mgr.user.entity; + +import lombok.Data; +import lombok.NoArgsConstructor; + +import javax.validation.constraints.NotBlank; + +/** + * import pem private key + */ +@Data +@NoArgsConstructor +public class ReqImportPem extends NewUserInputParam { + @NotBlank + private String pemContent; +} \ No newline at end of file diff --git a/src/main/java/com/webank/webase/node/mgr/user/entity/ReqImportPrivateKey.java b/src/main/java/com/webank/webase/node/mgr/user/entity/ReqImportPrivateKey.java new file mode 100644 index 000000000..9f4e2044b --- /dev/null +++ b/src/main/java/com/webank/webase/node/mgr/user/entity/ReqImportPrivateKey.java @@ -0,0 +1,29 @@ +/** + * Copyright 2014-2020 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.webank.webase.node.mgr.user.entity; + +import lombok.Data; +import lombok.NoArgsConstructor; + +import javax.validation.constraints.NotBlank; + +@Data +@NoArgsConstructor +public class ReqImportPrivateKey extends NewUserInputParam{ + @NotBlank + private String privateKey; +} From 85f5bd7a6aa0a7c9818cb829aa3cc80697428d5e Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Thu, 21 May 2020 22:35:58 +0800 Subject: [PATCH 061/131] add call super of entity --- .../webase/node/mgr/contract/entity/TransactionInputParam.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/webank/webase/node/mgr/contract/entity/TransactionInputParam.java b/src/main/java/com/webank/webase/node/mgr/contract/entity/TransactionInputParam.java index 0da1f1678..efc33cefa 100644 --- a/src/main/java/com/webank/webase/node/mgr/contract/entity/TransactionInputParam.java +++ b/src/main/java/com/webank/webase/node/mgr/contract/entity/TransactionInputParam.java @@ -16,7 +16,6 @@ package com.webank.webase.node.mgr.contract.entity; import com.webank.webase.node.mgr.front.entity.TransactionParam; -import javax.validation.constraints.NotNull; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; From 06acd0b7079c82f7c263335f75b41c872d4ad9af Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Fri, 22 May 2020 15:20:46 +0800 Subject: [PATCH 062/131] add call suer --- .../java/com/webank/webase/node/mgr/group/GroupService.java | 1 + .../com/webank/webase/node/mgr/user/entity/ReqImportPem.java | 4 ++++ .../webase/node/mgr/user/entity/ReqImportPrivateKey.java | 4 ++++ 3 files changed, 9 insertions(+) diff --git a/src/main/java/com/webank/webase/node/mgr/group/GroupService.java b/src/main/java/com/webank/webase/node/mgr/group/GroupService.java index 2f59470a2..86f8acfa7 100644 --- a/src/main/java/com/webank/webase/node/mgr/group/GroupService.java +++ b/src/main/java/com/webank/webase/node/mgr/group/GroupService.java @@ -258,6 +258,7 @@ private void savePeerList(String frontIp, Integer frontPort, int groupId, List nodeId.equals(ln.getNodeId()) && groupId == ln.getGroupId()).count(); + log.error("=========== savePeerList:{}", count); // local node not contains this one: if (count == 0) { PeerInfo newPeer = peerList.stream().filter(peer -> nodeId.equals(peer.getNodeId())) diff --git a/src/main/java/com/webank/webase/node/mgr/user/entity/ReqImportPem.java b/src/main/java/com/webank/webase/node/mgr/user/entity/ReqImportPem.java index bbb0d02c6..7dec055f0 100644 --- a/src/main/java/com/webank/webase/node/mgr/user/entity/ReqImportPem.java +++ b/src/main/java/com/webank/webase/node/mgr/user/entity/ReqImportPem.java @@ -1,7 +1,9 @@ package com.webank.webase.node.mgr.user.entity; import lombok.Data; +import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; +import lombok.ToString; import javax.validation.constraints.NotBlank; @@ -10,6 +12,8 @@ */ @Data @NoArgsConstructor +@ToString(callSuper = true) +@EqualsAndHashCode(callSuper = true) public class ReqImportPem extends NewUserInputParam { @NotBlank private String pemContent; diff --git a/src/main/java/com/webank/webase/node/mgr/user/entity/ReqImportPrivateKey.java b/src/main/java/com/webank/webase/node/mgr/user/entity/ReqImportPrivateKey.java index 9f4e2044b..4e405a586 100644 --- a/src/main/java/com/webank/webase/node/mgr/user/entity/ReqImportPrivateKey.java +++ b/src/main/java/com/webank/webase/node/mgr/user/entity/ReqImportPrivateKey.java @@ -17,12 +17,16 @@ package com.webank.webase.node.mgr.user.entity; import lombok.Data; +import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; +import lombok.ToString; import javax.validation.constraints.NotBlank; @Data @NoArgsConstructor +@ToString(callSuper = true) +@EqualsAndHashCode(callSuper = true) public class ReqImportPrivateKey extends NewUserInputParam{ @NotBlank private String privateKey; From 3170c65e3d2b27a591bea105205531afb310d803 Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Fri, 22 May 2020 15:34:00 +0800 Subject: [PATCH 063/131] add p12 api --- .../node/mgr/base/code/ConstantCode.java | 6 ++- .../node/mgr/base/tools/NodeMgrTools.java | 8 ++++ .../webase/node/mgr/user/UserController.java | 29 ++++++++++++++ .../webase/node/mgr/user/UserService.java | 40 +++++++++++++++++-- 4 files changed, 79 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/webank/webase/node/mgr/base/code/ConstantCode.java b/src/main/java/com/webank/webase/node/mgr/base/code/ConstantCode.java index c222b094d..de05d13ad 100644 --- a/src/main/java/com/webank/webase/node/mgr/base/code/ConstantCode.java +++ b/src/main/java/com/webank/webase/node/mgr/base/code/ConstantCode.java @@ -34,8 +34,12 @@ public class ConstantCode { /* front error code */ public static final RetCode PERMISSION_DENIED = RetCode.mark(201202, "permission denied"); + public static final RetCode CERT_FILE_NOT_FOUND = RetCode.mark(201231, "Cert file not found, please check cert path in config"); public static final RetCode PEM_FORMAT_ERROR = RetCode.mark(201232, "Pem file format error, must surrounded by -----XXXXX PRIVATE KEY-----"); - public static final RetCode PEM_CONTENT_ERROR = RetCode.mark(201233, "Pem file content error"); + public static final RetCode PEM_CONTENT_ERROR = RetCode.mark(201234, "Pem file content error"); + public static final RetCode P12_PASSWORD_NOT_CHINESE = RetCode.mark(201235, "p12's password cannot be chinese"); + public static final RetCode P12_PASSWORD_ERROR = RetCode.mark(201236, "p12's password cannot be chinese"); + public static final RetCode P12_FILE_ERROR = RetCode.mark(201237, "P12 file content error"); /** * Business exception. diff --git a/src/main/java/com/webank/webase/node/mgr/base/tools/NodeMgrTools.java b/src/main/java/com/webank/webase/node/mgr/base/tools/NodeMgrTools.java index a669ce1d4..72b9c7b67 100644 --- a/src/main/java/com/webank/webase/node/mgr/base/tools/NodeMgrTools.java +++ b/src/main/java/com/webank/webase/node/mgr/base/tools/NodeMgrTools.java @@ -537,4 +537,12 @@ public static String encodedBase64Str(String input) { } return Base64.getEncoder().encodeToString(input.getBytes()); } + + /** + * 只包含中文 + */ + public static boolean notContainsChinese(String input) { + String regex = "[^\\u4e00-\\u9fa5]+"; + return input.matches(regex); + } } diff --git a/src/main/java/com/webank/webase/node/mgr/user/UserController.java b/src/main/java/com/webank/webase/node/mgr/user/UserController.java index 480dfb038..fde2f567a 100644 --- a/src/main/java/com/webank/webase/node/mgr/user/UserController.java +++ b/src/main/java/com/webank/webase/node/mgr/user/UserController.java @@ -20,9 +20,11 @@ import com.webank.webase.node.mgr.base.code.ConstantCode; import com.webank.webase.node.mgr.base.exception.NodeMgrException; import com.webank.webase.node.mgr.base.properties.ConstantProperties; +import com.webank.webase.node.mgr.base.tools.NodeMgrTools; import com.webank.webase.node.mgr.base.tools.PemUtils; import com.webank.webase.node.mgr.user.entity.*; +import java.io.IOException; import java.time.Duration; import java.time.Instant; import java.util.List; @@ -40,6 +42,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; /** * Key pair manage @@ -204,4 +207,30 @@ public BaseResponse importPemPrivateKey(@Valid @RequestBody ReqImportPem reqImpo return baseResponse; } + @PostMapping("/importP12") + @PreAuthorize(ConstantProperties.HAS_ROLE_ADMIN) + public BaseResponse importP12PrivateKey(@RequestParam MultipartFile p12File, @RequestParam String p12Password, + @RequestParam Integer groupId, @RequestParam String userName, + @RequestParam(required = false) String description, BindingResult result) { + checkBindResult(result); + BaseResponse baseResponse = new BaseResponse(ConstantCode.SUCCESS); + Instant startTime = Instant.now(); + + if (!NodeMgrTools.notContainsChinese(p12Password)) { + throw new NodeMgrException(ConstantCode.P12_PASSWORD_NOT_CHINESE); + } + if (p12File.getSize() == 0) { + throw new NodeMgrException(ConstantCode.P12_FILE_ERROR); + } + Integer userId = userService.importKeyStoreFromP12(p12File, p12Password, + groupId, userName, description); + // query user row + TbUser userRow = userService.queryByUserId(userId); + baseResponse.setData(userRow); + + log.info("end importPemPrivateKey useTime:{} result:{}", + Duration.between(startTime, Instant.now()).toMillis(), JSON.toJSONString(baseResponse)); + return new BaseResponse(ConstantCode.SUCCESS); + } + } diff --git a/src/main/java/com/webank/webase/node/mgr/user/UserService.java b/src/main/java/com/webank/webase/node/mgr/user/UserService.java index 76cfb9607..b8b0a9e2a 100644 --- a/src/main/java/com/webank/webase/node/mgr/user/UserService.java +++ b/src/main/java/com/webank/webase/node/mgr/user/UserService.java @@ -16,6 +16,7 @@ import com.alibaba.fastjson.JSON; import com.webank.webase.node.mgr.base.code.ConstantCode; import com.webank.webase.node.mgr.base.enums.HasPk; +import com.webank.webase.node.mgr.base.enums.UserType; import com.webank.webase.node.mgr.base.exception.NodeMgrException; import com.webank.webase.node.mgr.base.properties.ConstantProperties; import com.webank.webase.node.mgr.base.tools.NodeMgrTools; @@ -30,12 +31,14 @@ import lombok.extern.log4j.Log4j2; import org.apache.commons.lang3.StringUtils; +import org.fisco.bcos.channel.client.P12Manager; import org.fisco.bcos.channel.client.PEMManager; import org.fisco.bcos.web3j.utils.Numeric; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.multipart.MultipartFile; /** * services for user data. @@ -76,7 +79,7 @@ public Integer addUserInfo(Integer groupId, String userName, String description, // group id as appId String appId = groupId.toString(); - // request sign + // request sign or not KeyPair keyPair; if (StringUtils.isNotBlank(privateKeyEncoded)) { Map param = new HashMap<>(); @@ -84,8 +87,8 @@ public Integer addUserInfo(Integer groupId, String userName, String description, param.put("appId", appId); // already encoded privateKey param.put("privateKey", privateKeyEncoded); - keyPair = frontRestTools.postForEntity(groupId, FrontRestTools.URI_KEY_PAIR_IMPORT_WITH_SIGN, - param, KeyPair.class); + keyPair = frontRestTools.postForEntity(groupId, + FrontRestTools.URI_KEY_PAIR_IMPORT_WITH_SIGN, param, KeyPair.class); } else { String keyUri = String.format(FrontRestTools.URI_KEY_PAIR, userName, signUserId, appId); keyPair = frontRestTools.getForEntity(groupId, keyUri, KeyPair.class); @@ -342,4 +345,35 @@ public Integer importPem(ReqImportPem reqImportPem) { reqImportPem.getDescription(), reqImportPem.getUserType(), privateKeyEncoded); return userId; } + + /** + * import keystore info from p12 file input stream and its password + * @param p12File + * @param p12Password + * @param userName + * @return KeyStoreInfo + */ + public Integer importKeyStoreFromP12(MultipartFile p12File, String p12Password, Integer groupId, + String userName, String description) { + P12Manager p12Manager = new P12Manager(); + String privateKey; + try { + p12Manager.load(p12File.getInputStream(), p12Password); + privateKey = Numeric.toHexStringNoPrefix(p12Manager.getECKeyPair().getPrivateKey()); + }catch (Exception e) { + log.error("importKeyStoreFromP12 error:[]", e); + if (e.getMessage().contains("password")) { + throw new NodeMgrException(ConstantCode.P12_PASSWORD_ERROR); + } + throw new NodeMgrException(ConstantCode.P12_FILE_ERROR); + } + // pem's privateKey encoded here + String privateKeyEncoded = NodeMgrTools.encodedBase64Str(privateKey); + + // store local and save in sign + Integer userId = addUserInfo(groupId, userName, description, UserType.GENERALUSER.getValue(), + privateKeyEncoded); + + return userId; + } } \ No newline at end of file From 3cd3b0b926d5711305caf99d94e238a35504465b Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Fri, 22 May 2020 16:05:55 +0800 Subject: [PATCH 064/131] fix reset all group --- .../com/webank/webase/node/mgr/base/code/ConstantCode.java | 2 +- .../java/com/webank/webase/node/mgr/group/GroupService.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/webank/webase/node/mgr/base/code/ConstantCode.java b/src/main/java/com/webank/webase/node/mgr/base/code/ConstantCode.java index de05d13ad..b7748d96f 100644 --- a/src/main/java/com/webank/webase/node/mgr/base/code/ConstantCode.java +++ b/src/main/java/com/webank/webase/node/mgr/base/code/ConstantCode.java @@ -38,7 +38,7 @@ public class ConstantCode { public static final RetCode PEM_FORMAT_ERROR = RetCode.mark(201232, "Pem file format error, must surrounded by -----XXXXX PRIVATE KEY-----"); public static final RetCode PEM_CONTENT_ERROR = RetCode.mark(201234, "Pem file content error"); public static final RetCode P12_PASSWORD_NOT_CHINESE = RetCode.mark(201235, "p12's password cannot be chinese"); - public static final RetCode P12_PASSWORD_ERROR = RetCode.mark(201236, "p12's password cannot be chinese"); + public static final RetCode P12_PASSWORD_ERROR = RetCode.mark(201236, "p12's password not match"); public static final RetCode P12_FILE_ERROR = RetCode.mark(201237, "P12 file content error"); /** diff --git a/src/main/java/com/webank/webase/node/mgr/group/GroupService.java b/src/main/java/com/webank/webase/node/mgr/group/GroupService.java index 86f8acfa7..bbc182abe 100644 --- a/src/main/java/com/webank/webase/node/mgr/group/GroupService.java +++ b/src/main/java/com/webank/webase/node/mgr/group/GroupService.java @@ -407,7 +407,7 @@ private void checkGroupGenesisSameWithEach() { private void checkSameChainDataWithLocal() { log.info("start checkSameChainData."); // get all group - List allNormalGroupList = getGroupList(GroupStatus.NORMAL.getValue()); + List allNormalGroupList = getGroupList(null); if (allNormalGroupList.isEmpty()) { log.warn("checkSameChainData not found any group of front."); return; @@ -419,7 +419,7 @@ private void checkSameChainDataWithLocal() { TbBlock smallestBlockLocal = blockService.getSmallestBlockInfo(groupId); // if no block in local db if (smallestBlockLocal == null) { - log.warn("checkSameChainDataWithLocal smallestBlockLocal is null"); + log.warn("checkSameChainDataWithLocal groupId {} smallestBlockLocal is null", groupId); continue; } BigInteger blockHeightLocal = smallestBlockLocal.getBlockNumber(); From 4ffbe9af3f7b3082f7122e94be267c8addb96ee6 Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Fri, 22 May 2020 16:10:33 +0800 Subject: [PATCH 065/131] fix reset all group genesis --- .../webank/webase/node/mgr/group/GroupService.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/webank/webase/node/mgr/group/GroupService.java b/src/main/java/com/webank/webase/node/mgr/group/GroupService.java index bbc182abe..ed314d80e 100644 --- a/src/main/java/com/webank/webase/node/mgr/group/GroupService.java +++ b/src/main/java/com/webank/webase/node/mgr/group/GroupService.java @@ -367,13 +367,13 @@ private void checkGroupGenesisSameWithEach() { log.warn("checkGroupGenesisSameWithEach not found any front."); return; } - List allNormalGroupList = getGroupList(GroupStatus.NORMAL.getValue()); - if (allNormalGroupList.isEmpty()) { + List allGroupList = getGroupList(null); + if (allGroupList.isEmpty()) { log.warn("checkGroupGenesisSameWithEach not found any group of front."); return; } - for (TbGroup tbGroup : allNormalGroupList) { + for (TbGroup tbGroup : allGroupList) { int groupId = tbGroup.getGroupId(); String lastBlockHash = ""; for (TbFront front : frontList) { @@ -383,7 +383,7 @@ private void checkGroupGenesisSameWithEach() { BlockInfo genesisBlock = frontInterface.getBlockByNumberFromSpecificFront(frontIp, frontPort, groupId, BigInteger.ZERO); if (genesisBlock == null) { - log.warn("checkGroupGenesisSameWithEach getGenesisBlock is null"); + log.debug("checkGroupGenesisSameWithEach getGenesisBlock is null"); continue; } if (!"".equals(lastBlockHash) && !lastBlockHash.equals(genesisBlock.getHash())) { @@ -419,7 +419,7 @@ private void checkSameChainDataWithLocal() { TbBlock smallestBlockLocal = blockService.getSmallestBlockInfo(groupId); // if no block in local db if (smallestBlockLocal == null) { - log.warn("checkSameChainDataWithLocal groupId {} smallestBlockLocal is null", groupId); + log.debug("checkSameChainDataWithLocal groupId {} smallestBlockLocal is null", groupId); continue; } BigInteger blockHeightLocal = smallestBlockLocal.getBlockNumber(); @@ -445,7 +445,7 @@ private void checkSameChainDataWithLocal() { updateGroupStatus(groupId, GroupStatus.CONFLICT_LOCAL_DATA.getValue()); continue; } else { - log.warn("checkSameChainDataWithLocal set groupId:{} as normal", groupId); + log.info("checkSameChainDataWithLocal set groupId:{} as normal", groupId); updateGroupStatus(groupId, GroupStatus.NORMAL.getValue()); } } From 24f9c8143673acada37b485eaeed51e51411619c Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Fri, 22 May 2020 16:29:12 +0800 Subject: [PATCH 066/131] fix group dirty data case --- .../com/webank/webase/node/mgr/group/GroupService.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/webank/webase/node/mgr/group/GroupService.java b/src/main/java/com/webank/webase/node/mgr/group/GroupService.java index ed314d80e..cafa34c2c 100644 --- a/src/main/java/com/webank/webase/node/mgr/group/GroupService.java +++ b/src/main/java/com/webank/webase/node/mgr/group/GroupService.java @@ -429,9 +429,10 @@ private void checkSameChainDataWithLocal() { BlockInfo smallestBlockOnChain = frontInterface.getBlockByNumber(groupId, blockHeightLocal); // if no block in each node, not same chain if (smallestBlockOnChain == null) { - log.warn("checkSameChainDataWithLocal groupId: {} block of height: {} on chain not exists, " + - "conflict with local block of same height", groupId, blockHeightLocal); - updateGroupStatus(groupId, GroupStatus.CONFLICT_LOCAL_DATA.getValue()); + log.info("smallestBlockOnChain groupId: {} height: {} return null, " + + "please check groupStatus", groupId, blockHeightLocal); + // null block not means conflict + // updateGroupStatus(groupId, GroupStatus.CONFLICT_LOCAL_DATA.getValue()); continue; } String blockHashOnChain = smallestBlockOnChain.getHash(); From 841f483c837f3ff1fc27095a4da7c24d4f589507 Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Fri, 22 May 2020 16:55:13 +0800 Subject: [PATCH 067/131] update check group order --- .../mgr/frontinterface/FrontInterfaceService.java | 2 +- .../webank/webase/node/mgr/group/GroupService.java | 13 ++++++------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/webank/webase/node/mgr/frontinterface/FrontInterfaceService.java b/src/main/java/com/webank/webase/node/mgr/frontinterface/FrontInterfaceService.java index f627f0340..7b5c6d5a5 100644 --- a/src/main/java/com/webank/webase/node/mgr/frontinterface/FrontInterfaceService.java +++ b/src/main/java/com/webank/webase/node/mgr/frontinterface/FrontInterfaceService.java @@ -238,7 +238,7 @@ public BlockInfo getBlockByNumber(Integer groupId, BigInteger blockNumber) try { blockInfo = frontRestTools.getForEntity(groupId, uri, BlockInfo.class); } catch (Exception ex) { - log.info("fail getBlockByNumber,exception:{}", ex); + log.error("fail getBlockByNumber,exception:[]", ex); } log.debug("end getBlockByNumber"); return blockInfo; diff --git a/src/main/java/com/webank/webase/node/mgr/group/GroupService.java b/src/main/java/com/webank/webase/node/mgr/group/GroupService.java index cafa34c2c..993053c08 100644 --- a/src/main/java/com/webank/webase/node/mgr/group/GroupService.java +++ b/src/main/java/com/webank/webase/node/mgr/group/GroupService.java @@ -231,10 +231,10 @@ public void resetGroupList() { // check group status(normal or maintaining) checkAndUpdateGroupStatus(allGroupSet); - // check group's genesis block same with each other - checkGroupGenesisSameWithEach(); // check group if has dirty data checkSameChainDataWithLocal(); + // check group's genesis block same with each other + checkGroupGenesisSameWithEach(); // remove front_group_map that not in tb_front or tb_group frontGroupMapService.removeInvalidFrontGroupMap(); //clear cache @@ -429,8 +429,8 @@ private void checkSameChainDataWithLocal() { BlockInfo smallestBlockOnChain = frontInterface.getBlockByNumber(groupId, blockHeightLocal); // if no block in each node, not same chain if (smallestBlockOnChain == null) { - log.info("smallestBlockOnChain groupId: {} height: {} return null, " + - "please check groupStatus", groupId, blockHeightLocal); + log.info("smallestBlockOnChain groupId: {} height: {} return null block, " + + "please check group's node", groupId, blockHeightLocal); // null block not means conflict // updateGroupStatus(groupId, GroupStatus.CONFLICT_LOCAL_DATA.getValue()); continue; @@ -440,9 +440,8 @@ private void checkSameChainDataWithLocal() { groupId, blockHeightLocal, blockHashLocal, blockHashOnChain); // check same block hash, the same chain if (!blockHashLocal.equals(blockHashOnChain)) { - log.warn("checkSameChainDataWithLocal group: {} block of height:{} on chain " + - "conflicts with local block data", - groupId, blockHeightLocal); + log.warn("checkSameChainDataWithLocal blockHashOnChain conflicts with local block data " + + "groupId: {} height:{} on chain ", groupId, blockHeightLocal); updateGroupStatus(groupId, GroupStatus.CONFLICT_LOCAL_DATA.getValue()); continue; } else { From feec19a045e0788f9da45a5931287f478cfcff0c Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Fri, 22 May 2020 17:43:02 +0800 Subject: [PATCH 068/131] valid signUserId exist --- .../com/webank/webase/node/mgr/base/code/ConstantCode.java | 3 +++ .../java/com/webank/webase/node/mgr/user/UserService.java | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/src/main/java/com/webank/webase/node/mgr/base/code/ConstantCode.java b/src/main/java/com/webank/webase/node/mgr/base/code/ConstantCode.java index b7748d96f..f62e93f17 100644 --- a/src/main/java/com/webank/webase/node/mgr/base/code/ConstantCode.java +++ b/src/main/java/com/webank/webase/node/mgr/base/code/ConstantCode.java @@ -159,6 +159,9 @@ public class ConstantCode { public static final RetCode PARAM_FAIL_ABI_ID_EMPTY = RetCode.mark(202099, "Abi Id cannot be empty"); public static final RetCode CONTRACT_ADDRESS_NULL = RetCode.mark(202100, "contractAddress is null"); + public static final RetCode USER_NOT_EXIST = RetCode.mark(202110, "User's signUserId not exist"); + + /* dynamic group manage */ public static final RetCode GROUP_ID_EXISTS = RetCode.mark(202300, "group id already exists"); public static final RetCode NODE_NOT_EXISTS = RetCode.mark(202301, "node's front not exists"); diff --git a/src/main/java/com/webank/webase/node/mgr/user/UserService.java b/src/main/java/com/webank/webase/node/mgr/user/UserService.java index b8b0a9e2a..8270d61a4 100644 --- a/src/main/java/com/webank/webase/node/mgr/user/UserService.java +++ b/src/main/java/com/webank/webase/node/mgr/user/UserService.java @@ -31,6 +31,7 @@ import lombok.extern.log4j.Log4j2; import org.apache.commons.lang3.StringUtils; +import org.apache.tomcat.util.bcel.Const; import org.fisco.bcos.channel.client.P12Manager; import org.fisco.bcos.channel.client.PEMManager; import org.fisco.bcos.web3j.utils.Numeric; @@ -263,6 +264,9 @@ public TbUser queryByUserId(Integer userId) throws NodeMgrException { */ public String getSignUserIdByAddress(int groupId, String address) throws NodeMgrException { TbUser user = queryUser(null, groupId, null, address); + if (user == null) { + throw new NodeMgrException(ConstantCode.USER_NOT_EXIST); + } return user.getSignUserId(); } From 1bc85e50ebdf1569fadd59ada584da444b1470f7 Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Fri, 22 May 2020 19:16:53 +0800 Subject: [PATCH 069/131] fix update all group bug --- .../com/webank/webase/node/mgr/group/GroupService.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/webank/webase/node/mgr/group/GroupService.java b/src/main/java/com/webank/webase/node/mgr/group/GroupService.java index 993053c08..e7a20f384 100644 --- a/src/main/java/com/webank/webase/node/mgr/group/GroupService.java +++ b/src/main/java/com/webank/webase/node/mgr/group/GroupService.java @@ -407,13 +407,13 @@ private void checkGroupGenesisSameWithEach() { private void checkSameChainDataWithLocal() { log.info("start checkSameChainData."); // get all group - List allNormalGroupList = getGroupList(null); - if (allNormalGroupList.isEmpty()) { + List allGroupList = getGroupList(null); + if (allGroupList.isEmpty()) { log.warn("checkSameChainData not found any group of front."); return; } - for (TbGroup tbGroup : allNormalGroupList) { + for (TbGroup tbGroup : allGroupList) { int groupId = tbGroup.getGroupId(); // find smallest block from db of group TbBlock smallestBlockLocal = blockService.getSmallestBlockInfo(groupId); @@ -444,7 +444,8 @@ private void checkSameChainDataWithLocal() { "groupId: {} height:{} on chain ", groupId, blockHeightLocal); updateGroupStatus(groupId, GroupStatus.CONFLICT_LOCAL_DATA.getValue()); continue; - } else { + } else if (tbGroup.getGroupStatus() == GroupStatus.CONFLICT_LOCAL_DATA.getValue()){ + // if old group is conflict, now normal, set as normal log.info("checkSameChainDataWithLocal set groupId:{} as normal", groupId); updateGroupStatus(groupId, GroupStatus.NORMAL.getValue()); } From 742b8171a7fc48e4a7b98590683fd7f99607fe68 Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Fri, 22 May 2020 19:30:40 +0800 Subject: [PATCH 070/131] fix request normal front_group --- .../webase/node/mgr/contract/ContractService.java | 2 +- .../mgr/frontgroupmap/entity/FrontGroupMapCache.java | 10 +++++++++- .../webase/node/mgr/frontinterface/FrontRestTools.java | 4 +++- .../com/webank/webase/node/mgr/group/GroupService.java | 7 +++++++ 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/webank/webase/node/mgr/contract/ContractService.java b/src/main/java/com/webank/webase/node/mgr/contract/ContractService.java index 94566797f..2e3aa6e2e 100644 --- a/src/main/java/com/webank/webase/node/mgr/contract/ContractService.java +++ b/src/main/java/com/webank/webase/node/mgr/contract/ContractService.java @@ -455,7 +455,7 @@ private void checkDeployPermission(int groupId, String userAddress) { deployUserList = JSON.parseArray(JSON.toJSONString(listData), PermissionInfo.class); } - // check user in the list, + // check user in the list if (deployUserList.isEmpty()) { return; } else { diff --git a/src/main/java/com/webank/webase/node/mgr/frontgroupmap/entity/FrontGroupMapCache.java b/src/main/java/com/webank/webase/node/mgr/frontgroupmap/entity/FrontGroupMapCache.java index 6b8c6dc47..0b9a6de66 100644 --- a/src/main/java/com/webank/webase/node/mgr/frontgroupmap/entity/FrontGroupMapCache.java +++ b/src/main/java/com/webank/webase/node/mgr/frontgroupmap/entity/FrontGroupMapCache.java @@ -14,9 +14,12 @@ package com.webank.webase.node.mgr.frontgroupmap.entity; +import com.webank.webase.node.mgr.base.enums.GroupStatus; import com.webank.webase.node.mgr.frontgroupmap.FrontGroupMapService; import java.util.List; import java.util.stream.Collectors; + +import com.webank.webase.node.mgr.group.GroupService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -25,6 +28,8 @@ public class FrontGroupMapCache { @Autowired private FrontGroupMapService mapService; + @Autowired + private GroupService groupService; private static List mapList; @@ -52,7 +57,10 @@ public List getMapListByGroupId(int groupId) { if (list == null) { return null; } - List map = list.stream().filter(m -> groupId == m.getGroupId()) + // filter all FrontGroup which groupStatus is normal + List map = list.stream() + .filter(m -> groupId == m.getGroupId() + && GroupStatus.NORMAL.getValue() == groupService.getGroupStatus(groupId)) .collect(Collectors.toList()); return map; } diff --git a/src/main/java/com/webank/webase/node/mgr/frontinterface/FrontRestTools.java b/src/main/java/com/webank/webase/node/mgr/frontinterface/FrontRestTools.java index 0d0456c9b..b7f440574 100644 --- a/src/main/java/com/webank/webase/node/mgr/frontinterface/FrontRestTools.java +++ b/src/main/java/com/webank/webase/node/mgr/frontinterface/FrontRestTools.java @@ -32,6 +32,8 @@ import java.util.List; import java.util.Map; import java.util.Objects; + +import com.webank.webase.node.mgr.group.GroupService; import lombok.extern.log4j.Log4j2; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -302,7 +304,7 @@ private T restTemplateExchange(int groupId, String uri, HttpMethod method, ArrayList list = new ArrayList<>(frontList); RestTemplate restTemplate = caseRestemplate(uri); - while (list != null && list.size() > 0) { + while (list.size() > 0) { String url = buildFrontUrl(list, uri, method);//build url try { HttpEntity entity = buildHttpEntity(param);// build entity diff --git a/src/main/java/com/webank/webase/node/mgr/group/GroupService.java b/src/main/java/com/webank/webase/node/mgr/group/GroupService.java index e7a20f384..fbe09c905 100644 --- a/src/main/java/com/webank/webase/node/mgr/group/GroupService.java +++ b/src/main/java/com/webank/webase/node/mgr/group/GroupService.java @@ -690,4 +690,11 @@ public TbGroup getGroupById(Integer groupId) { return groupMapper.getGroupById(groupId); } + /** + * get status of group + * @return groupStatus + */ + public Integer getGroupStatus(Integer groupId) { + return getGroupById(groupId).getGroupStatus(); + } } From dc118e2fed5769aba7cb16d88d96c72d295bd5b0 Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Fri, 22 May 2020 20:10:30 +0800 Subject: [PATCH 071/131] fix p12 load --- .../com/webank/webase/node/mgr/user/UserController.java | 1 - .../java/com/webank/webase/node/mgr/user/UserService.java | 7 ++++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/webank/webase/node/mgr/user/UserController.java b/src/main/java/com/webank/webase/node/mgr/user/UserController.java index fde2f567a..cb90832b2 100644 --- a/src/main/java/com/webank/webase/node/mgr/user/UserController.java +++ b/src/main/java/com/webank/webase/node/mgr/user/UserController.java @@ -24,7 +24,6 @@ import com.webank.webase.node.mgr.base.tools.PemUtils; import com.webank.webase.node.mgr.user.entity.*; -import java.io.IOException; import java.time.Duration; import java.time.Instant; import java.util.List; diff --git a/src/main/java/com/webank/webase/node/mgr/user/UserService.java b/src/main/java/com/webank/webase/node/mgr/user/UserService.java index 8270d61a4..996ebe927 100644 --- a/src/main/java/com/webank/webase/node/mgr/user/UserService.java +++ b/src/main/java/com/webank/webase/node/mgr/user/UserService.java @@ -27,6 +27,7 @@ import com.webank.webase.node.mgr.user.entity.*; import java.io.ByteArrayInputStream; +import java.io.IOException; import java.util.*; import lombok.extern.log4j.Log4j2; @@ -362,14 +363,18 @@ public Integer importKeyStoreFromP12(MultipartFile p12File, String p12Password, P12Manager p12Manager = new P12Manager(); String privateKey; try { + p12Manager.setPassword(p12Password); p12Manager.load(p12File.getInputStream(), p12Password); privateKey = Numeric.toHexStringNoPrefix(p12Manager.getECKeyPair().getPrivateKey()); - }catch (Exception e) { + } catch ( IOException e) { log.error("importKeyStoreFromP12 error:[]", e); if (e.getMessage().contains("password")) { throw new NodeMgrException(ConstantCode.P12_PASSWORD_ERROR); } throw new NodeMgrException(ConstantCode.P12_FILE_ERROR); + } catch (Exception e) { + log.error("importKeyStoreFromP12 error:[]", e); + throw new NodeMgrException(ConstantCode.P12_FILE_ERROR.getCode(), e.getMessage()); } // pem's privateKey encoded here String privateKeyEncoded = NodeMgrTools.encodedBase64Str(privateKey); From bc680716f713047b2f9dc4d92feb5ae137da1c46 Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Fri, 22 May 2020 20:38:20 +0800 Subject: [PATCH 072/131] adjust log level --- .../entity/FrontGroupMapCache.java | 3 +++ .../webase/node/mgr/group/GroupService.java | 13 ++++++---- .../node/mgr/scheduler/DeleteInfoTask.java | 24 +++++++++---------- .../mgr/scheduler/PullBlockTransTask.java | 4 ++-- .../scheduler/StatisticsTransdailyTask.java | 8 +++---- .../node/mgr/scheduler/TransMonitorTask.java | 6 ++--- 6 files changed, 33 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/webank/webase/node/mgr/frontgroupmap/entity/FrontGroupMapCache.java b/src/main/java/com/webank/webase/node/mgr/frontgroupmap/entity/FrontGroupMapCache.java index 0b9a6de66..f3286db9f 100644 --- a/src/main/java/com/webank/webase/node/mgr/frontgroupmap/entity/FrontGroupMapCache.java +++ b/src/main/java/com/webank/webase/node/mgr/frontgroupmap/entity/FrontGroupMapCache.java @@ -20,9 +20,11 @@ import java.util.stream.Collectors; import com.webank.webase.node.mgr.group.GroupService; +import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +@Log4j2 @Component public class FrontGroupMapCache { @@ -55,6 +57,7 @@ public List resetMapList() { public List getMapListByGroupId(int groupId) { List list = getAllMap(); if (list == null) { + log.warn("getMapListByGroupId getAllMap is null."); return null; } // filter all FrontGroup which groupStatus is normal diff --git a/src/main/java/com/webank/webase/node/mgr/group/GroupService.java b/src/main/java/com/webank/webase/node/mgr/group/GroupService.java index fbe09c905..bd2bcb5c1 100644 --- a/src/main/java/com/webank/webase/node/mgr/group/GroupService.java +++ b/src/main/java/com/webank/webase/node/mgr/group/GroupService.java @@ -256,11 +256,13 @@ private void savePeerList(String frontIp, Integer frontPort, int groupId, List peerList = Arrays.asList(peerArr); //save new nodes for (String nodeId : groupPeerList) { + // if local hash node, count = 1 long count = localNodeList.stream().filter( ln -> nodeId.equals(ln.getNodeId()) && groupId == ln.getGroupId()).count(); + // TODO remove log.error("=========== savePeerList:{}", count); // local node not contains this one: - if (count == 0) { + if (count != 1) { PeerInfo newPeer = peerList.stream().filter(peer -> nodeId.equals(peer.getNodeId())) .findFirst().orElseGet(() -> new PeerInfo(nodeId)); nodeService.addNodeInfo(groupId, newPeer); @@ -296,9 +298,12 @@ private void removeInvalidPeer(int groupId, List groupPeerList) { return; } //remove node that's not in groupPeerList and not in sealer/observer list - localNodes.stream().filter(node -> !groupPeerList.contains(node.getNodeId()) - && !checkSealerAndObserverListContains(groupId, node.getNodeId())) - .forEach(n -> nodeService.deleteByNodeAndGroupId(n.getNodeId(), groupId)); + localNodes.stream() + .filter(node -> + !groupPeerList.contains(node.getNodeId()) + && !checkSealerAndObserverListContains(groupId, node.getNodeId())) + .forEach(n -> + nodeService.deleteByNodeAndGroupId(n.getNodeId(), groupId)); } private boolean checkSealerAndObserverListContains(int groupId, String nodeId) { diff --git a/src/main/java/com/webank/webase/node/mgr/scheduler/DeleteInfoTask.java b/src/main/java/com/webank/webase/node/mgr/scheduler/DeleteInfoTask.java index 6b667a038..a180d4bd2 100644 --- a/src/main/java/com/webank/webase/node/mgr/scheduler/DeleteInfoTask.java +++ b/src/main/java/com/webank/webase/node/mgr/scheduler/DeleteInfoTask.java @@ -63,17 +63,17 @@ public void taskStart() { */ public void deleteInfoStart() { Instant startTime = Instant.now(); - log.info("start deleteInfoStart. startTime:{}", startTime.toEpochMilli()); + log.debug("start deleteInfoStart. startTime:{}", startTime.toEpochMilli()); //get group list List groupList = groupService.getGroupList(DataStatus.NORMAL.getValue()); if (groupList == null || groupList.size() == 0) { - log.info("DeleteInfoTask jump over .not found any group"); + log.warn("DeleteInfoTask jump over, not found any group"); return; } groupList.stream().forEach(g -> deleteByGroupId(g.getGroupId())); - log.info("end deleteInfoStart useTime:{}", + log.debug("end deleteInfoStart useTime:{}", Duration.between(startTime, Instant.now()).toMillis()); } @@ -94,12 +94,12 @@ private void deleteByGroupId(int groupId) { * delete block. */ private void deleteBlock(int groupId) { - log.info("start deleteBlock. groupId:{}", groupId); + log.debug("start deleteBlock. groupId:{}", groupId); try { Integer removeCount = blockService.remove(groupId, cProperties.getBlockRetainMax()); - log.info("end deleteBlock. groupId:{} removeCount:{}", groupId, removeCount); + log.debug("end deleteBlock. groupId:{} removeCount:{}", groupId, removeCount); } catch (Exception ex) { - log.info("fail deleteBlock. groupId:{}", groupId, ex); + log.error("fail deleteBlock. groupId:{}", groupId, ex); } } @@ -107,7 +107,7 @@ private void deleteBlock(int groupId) { * delete transHash. */ private void deleteTransHash(int groupId) { - log.info("start deleteTransHash. groupId:{}", groupId); + log.debug("start deleteTransHash. groupId:{}", groupId); try { // TransListParam queryParam = new TransListParam(null, null); // Integer count = transHashService.queryCountOfTran(groupId, queryParam); @@ -117,9 +117,9 @@ private void deleteTransHash(int groupId) { Integer subTransNum = count - cProperties.getTransRetainMax().intValue(); removeCount = transHashService.remove(groupId, subTransNum); } - log.info("end deleteTransHash. groupId:{} removeCount:{}", groupId, removeCount); + log.debug("end deleteTransHash. groupId:{} removeCount:{}", groupId, removeCount); } catch (Exception ex) { - log.info("fail deleteTransHash. groupId:{}", groupId, ex); + log.error("fail deleteTransHash. groupId:{}", groupId, ex); } } @@ -128,13 +128,13 @@ private void deleteTransHash(int groupId) { * delete monitor info. */ private void deleteTransMonitor(int groupId) { - log.info("start deleteTransMonitor. groupId:{}", groupId); + log.debug("start deleteTransMonitor. groupId:{}", groupId); try { Integer removeCount = monitorService .delete(groupId, cProperties.getMonitorInfoRetainMax()); - log.info("end deleteTransMonitor. groupId:{} removeCount:{}", groupId, removeCount); + log.debug("end deleteTransMonitor. groupId:{} removeCount:{}", groupId, removeCount); } catch (Exception ex) { - log.info("fail deleteTransMonitor. groupId:{}", groupId, ex); + log.error("fail deleteTransMonitor. groupId:{}", groupId, ex); } } } diff --git a/src/main/java/com/webank/webase/node/mgr/scheduler/PullBlockTransTask.java b/src/main/java/com/webank/webase/node/mgr/scheduler/PullBlockTransTask.java index d979c742c..e3d8a9732 100644 --- a/src/main/java/com/webank/webase/node/mgr/scheduler/PullBlockTransTask.java +++ b/src/main/java/com/webank/webase/node/mgr/scheduler/PullBlockTransTask.java @@ -50,7 +50,7 @@ public void taskStart() { */ public synchronized void pullBlockStart() { Instant startTime = Instant.now(); - log.info("start pullBLock startTime:{}", startTime.toEpochMilli()); + log.debug("start pullBLock startTime:{}", startTime.toEpochMilli()); List groupList = groupService.getGroupList(GroupStatus.NORMAL.getValue()); if (groupList == null || groupList.size() == 0) { log.warn("pullBlock jump over: not found any group"); @@ -68,7 +68,7 @@ public synchronized void pullBlockStart() { Thread.currentThread().interrupt(); } - log.info("end pullBLock useTime:{} ", + log.debug("end pullBLock useTime:{} ", Duration.between(startTime, Instant.now()).toMillis()); } } diff --git a/src/main/java/com/webank/webase/node/mgr/scheduler/StatisticsTransdailyTask.java b/src/main/java/com/webank/webase/node/mgr/scheduler/StatisticsTransdailyTask.java index 4ebdae57f..671b42fc5 100644 --- a/src/main/java/com/webank/webase/node/mgr/scheduler/StatisticsTransdailyTask.java +++ b/src/main/java/com/webank/webase/node/mgr/scheduler/StatisticsTransdailyTask.java @@ -60,7 +60,7 @@ public void taskStart() { */ public synchronized void updateTransdailyData() { Instant startTime = Instant.now(); - log.info("start updateTransdailyData startTime:{}", startTime.toEpochMilli()); + log.debug("start updateTransdailyData startTime:{}", startTime.toEpochMilli()); try { // query all group statistical info @@ -73,7 +73,7 @@ public synchronized void updateTransdailyData() { } catch (Exception ex) { log.error("fail updateTransdailyData", ex); } - log.info("end updateTransdailyData useTime:{}", + log.debug("end updateTransdailyData useTime:{}", Duration.between(startTime, Instant.now()).toMillis()); } @@ -104,7 +104,7 @@ private void traverseNetList(List groupStatisticalLis // Traversing block list if (blockList == null | blockList.size() == 0) { - log.info("updateTransdailyData jump over .This chain [{}] did not find new block", + log.debug("updateTransdailyData jump over .This chain [{}] did not find new block", groupId); continue; } @@ -113,7 +113,7 @@ private void traverseNetList(List groupStatisticalLis LocalDate blockDate = tbBlock.getBlockTimestamp() == null ? null : tbBlock.getBlockTimestamp().toLocalDate(); if (blockDate == null) { - log.warn("updateTransdailyData jump over . blockDate is null"); + log.debug("updateTransdailyData jump over . blockDate is null"); continue; } diff --git a/src/main/java/com/webank/webase/node/mgr/scheduler/TransMonitorTask.java b/src/main/java/com/webank/webase/node/mgr/scheduler/TransMonitorTask.java index 73d1be3d7..351dd58b8 100644 --- a/src/main/java/com/webank/webase/node/mgr/scheduler/TransMonitorTask.java +++ b/src/main/java/com/webank/webase/node/mgr/scheduler/TransMonitorTask.java @@ -50,11 +50,11 @@ public void taskStart() { */ public synchronized void monitorStart() { Instant startTime = Instant.now(); - log.info("=== start monitor. startTime:{}", startTime.toEpochMilli()); + log.debug("=== start monitor. startTime:{}", startTime.toEpochMilli()); //get group list List groupList = groupService.getGroupList(DataStatus.NORMAL.getValue()); if (groupList == null || groupList.size() == 0) { - log.info("monitor jump over .not found any group"); + log.warn("monitor jump over, not found any group"); return; } // count down group, make sure all group's transMonitor finished @@ -69,7 +69,7 @@ public synchronized void monitorStart() { Thread.currentThread().interrupt(); } - log.info("=== end monitor. useTime:{} ", + log.debug("=== end monitor. useTime:{} ", Duration.between(startTime, Instant.now()).toMillis()); } } From b6f5c175bd8e28a605c5a6e551ca0f97d96d8726 Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Mon, 25 May 2020 10:37:32 +0800 Subject: [PATCH 073/131] src/main/java/com/webank/webase/node/mgr/ --- .../frontgroupmap/{entity => }/FrontGroupMapCache.java | 3 +-- src/main/resources/mapper/FrontGroupMapMapper.xml | 9 ++++++++- 2 files changed, 9 insertions(+), 3 deletions(-) rename src/main/java/com/webank/webase/node/mgr/frontgroupmap/{entity => }/FrontGroupMapCache.java (93%) diff --git a/src/main/java/com/webank/webase/node/mgr/frontgroupmap/entity/FrontGroupMapCache.java b/src/main/java/com/webank/webase/node/mgr/frontgroupmap/FrontGroupMapCache.java similarity index 93% rename from src/main/java/com/webank/webase/node/mgr/frontgroupmap/entity/FrontGroupMapCache.java rename to src/main/java/com/webank/webase/node/mgr/frontgroupmap/FrontGroupMapCache.java index f3286db9f..b2fa080aa 100644 --- a/src/main/java/com/webank/webase/node/mgr/frontgroupmap/entity/FrontGroupMapCache.java +++ b/src/main/java/com/webank/webase/node/mgr/frontgroupmap/FrontGroupMapCache.java @@ -62,8 +62,7 @@ public List getMapListByGroupId(int groupId) { } // filter all FrontGroup which groupStatus is normal List map = list.stream() - .filter(m -> groupId == m.getGroupId() - && GroupStatus.NORMAL.getValue() == groupService.getGroupStatus(groupId)) + .filter(m -> groupId == m.getGroupId()) .collect(Collectors.toList()); return map; } diff --git a/src/main/resources/mapper/FrontGroupMapMapper.xml b/src/main/resources/mapper/FrontGroupMapMapper.xml index 5fd0afe27..16db4d847 100644 --- a/src/main/resources/mapper/FrontGroupMapMapper.xml +++ b/src/main/resources/mapper/FrontGroupMapMapper.xml @@ -11,12 +11,13 @@ + insert ignore into tb_front_group_map - (front_id,group_id,create_time,modify_time)values(#{frontId},#{groupId},NOW(),NOW()) + (front_id,group_id,status,create_time,modify_time)values(#{frontId},#{groupId},#{status},NOW(),NOW()) SELECT LAST_INSERT_ID() @@ -34,6 +35,9 @@ and group_id = #{groupId} + + and status = #{status} + From 2308b9983085a9d8d7710006e15d56c1814f7184 Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Mon, 25 May 2020 10:40:52 +0800 Subject: [PATCH 074/131] fix group status of front_group --- .../node/mgr/base/filter/FrontFilter.java | 2 +- .../webase/node/mgr/front/FrontService.java | 5 ++-- .../mgr/frontgroupmap/FrontGroupMapCache.java | 8 ++++-- .../frontgroupmap/FrontGroupMapService.java | 28 +++++++++++++++++-- .../mgr/frontgroupmap/entity/FrontGroup.java | 1 + .../frontgroupmap/entity/MapListParam.java | 1 + .../frontgroupmap/entity/TbFrontGroupMap.java | 8 +++++- .../mgr/frontinterface/FrontRestTools.java | 5 ++-- .../webase/node/mgr/group/GroupService.java | 15 +++------- 9 files changed, 50 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/webank/webase/node/mgr/base/filter/FrontFilter.java b/src/main/java/com/webank/webase/node/mgr/base/filter/FrontFilter.java index 25c5901e8..f38a2e30a 100644 --- a/src/main/java/com/webank/webase/node/mgr/base/filter/FrontFilter.java +++ b/src/main/java/com/webank/webase/node/mgr/base/filter/FrontFilter.java @@ -21,7 +21,7 @@ import com.webank.webase.node.mgr.base.properties.ConstantProperties; import com.webank.webase.node.mgr.base.tools.HttpRequestTools; import com.webank.webase.node.mgr.frontgroupmap.entity.FrontGroup; -import com.webank.webase.node.mgr.frontgroupmap.entity.FrontGroupMapCache; +import com.webank.webase.node.mgr.frontgroupmap.FrontGroupMapCache; import java.io.IOException; import java.util.Arrays; import java.util.List; diff --git a/src/main/java/com/webank/webase/node/mgr/front/FrontService.java b/src/main/java/com/webank/webase/node/mgr/front/FrontService.java index 39ec31468..9d84a4a18 100644 --- a/src/main/java/com/webank/webase/node/mgr/front/FrontService.java +++ b/src/main/java/com/webank/webase/node/mgr/front/FrontService.java @@ -25,14 +25,13 @@ import com.webank.webase.node.mgr.front.entity.FrontParam; import com.webank.webase.node.mgr.front.entity.TbFront; import com.webank.webase.node.mgr.frontgroupmap.FrontGroupMapService; -import com.webank.webase.node.mgr.frontgroupmap.entity.FrontGroupMapCache; +import com.webank.webase.node.mgr.frontgroupmap.FrontGroupMapCache; import com.webank.webase.node.mgr.frontinterface.FrontInterfaceService; import com.webank.webase.node.mgr.frontinterface.entity.SyncStatus; import com.webank.webase.node.mgr.group.GroupService; import com.webank.webase.node.mgr.group.entity.TbGroup; import com.webank.webase.node.mgr.node.NodeParam; import com.webank.webase.node.mgr.node.NodeService; -import com.webank.webase.node.mgr.node.TbNode; import com.webank.webase.node.mgr.node.entity.PeerInfo; import com.webank.webase.node.mgr.scheduler.ResetGroupListTask; @@ -140,7 +139,7 @@ public TbFront newFront(FrontInfo frontInfo) { GroupType.SYNC.getValue(), DataStatus.NORMAL.getValue()); } //save front group map - frontGroupMapService.newFrontGroup(tbFront.getFrontId(), group); + frontGroupMapService.newFrontGroupWithStatus(tbFront, group); //save nodes for (String nodeId : groupPeerList) { PeerInfo newPeer = peerList.stream().map(p -> NodeMgrTools diff --git a/src/main/java/com/webank/webase/node/mgr/frontgroupmap/FrontGroupMapCache.java b/src/main/java/com/webank/webase/node/mgr/frontgroupmap/FrontGroupMapCache.java index b2fa080aa..759d4314e 100644 --- a/src/main/java/com/webank/webase/node/mgr/frontgroupmap/FrontGroupMapCache.java +++ b/src/main/java/com/webank/webase/node/mgr/frontgroupmap/FrontGroupMapCache.java @@ -11,7 +11,7 @@ * or implied. See the License for the specific language governing permissions and limitations under * the License. */ -package com.webank.webase.node.mgr.frontgroupmap.entity; +package com.webank.webase.node.mgr.frontgroupmap; import com.webank.webase.node.mgr.base.enums.GroupStatus; @@ -19,6 +19,8 @@ import java.util.List; import java.util.stream.Collectors; +import com.webank.webase.node.mgr.frontgroupmap.entity.FrontGroup; +import com.webank.webase.node.mgr.frontgroupmap.entity.MapListParam; import com.webank.webase.node.mgr.group.GroupService; import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.annotation.Autowired; @@ -62,8 +64,10 @@ public List getMapListByGroupId(int groupId) { } // filter all FrontGroup which groupStatus is normal List map = list.stream() - .filter(m -> groupId == m.getGroupId()) + .filter(m -> groupId == m.getGroupId() + && m.getStatus() == GroupStatus.NORMAL.getValue()) .collect(Collectors.toList()); + log.error("getMapListByGroupId map size:{}", map.size()); return map; } diff --git a/src/main/java/com/webank/webase/node/mgr/frontgroupmap/FrontGroupMapService.java b/src/main/java/com/webank/webase/node/mgr/frontgroupmap/FrontGroupMapService.java index d9b8779cd..50f908e41 100644 --- a/src/main/java/com/webank/webase/node/mgr/frontgroupmap/FrontGroupMapService.java +++ b/src/main/java/com/webank/webase/node/mgr/frontgroupmap/FrontGroupMapService.java @@ -13,10 +13,14 @@ */ package com.webank.webase.node.mgr.frontgroupmap; +import com.webank.webase.node.mgr.base.entity.BaseResponse; +import com.webank.webase.node.mgr.base.enums.GroupStatus; import com.webank.webase.node.mgr.front.FrontService; +import com.webank.webase.node.mgr.front.entity.TbFront; import com.webank.webase.node.mgr.frontgroupmap.entity.FrontGroup; import com.webank.webase.node.mgr.frontgroupmap.entity.MapListParam; import com.webank.webase.node.mgr.frontgroupmap.entity.TbFrontGroupMap; +import com.webank.webase.node.mgr.frontinterface.FrontInterfaceService; import com.webank.webase.node.mgr.group.GroupService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; @@ -34,18 +38,38 @@ public class FrontGroupMapService { private GroupService groupService; @Autowired private FrontService frontService; + @Autowired + private FrontInterfaceService frontInterface; /** * add new mapping */ - public TbFrontGroupMap newFrontGroup(Integer frontId, Integer groupId) { - TbFrontGroupMap tbFrontGroupMap = new TbFrontGroupMap(frontId, groupId); + private TbFrontGroupMap newFrontGroup(Integer frontId, Integer groupId, Integer status) { + TbFrontGroupMap tbFrontGroupMap = new TbFrontGroupMap(frontId, groupId, status); //add db frontGroupMapMapper.add(tbFrontGroupMap); return tbFrontGroupMap; } + /** + * new front group map only for normal group + */ + public void newFrontGroupWithStatus(TbFront front, Integer groupId) { + // check front's all group status + BaseResponse res = frontInterface.operateGroup(front.getFrontIp(), front.getFrontPort(), + groupId, "getStatus"); + // "INEXISTENT"、"STOPPING"、"RUNNING"、"STOPPED"、"DELETED" + if (res.getCode() == 0) { + String groupStatus = (String) res.getData(); + if ("RUNNING".equals(groupStatus)) { + newFrontGroup(front.getFrontId(), groupId, GroupStatus.NORMAL.getValue()); + } else { + newFrontGroup(front.getFrontId(), groupId, GroupStatus.MAINTAINING.getValue()); + } + } + } + /** * get map count */ diff --git a/src/main/java/com/webank/webase/node/mgr/frontgroupmap/entity/FrontGroup.java b/src/main/java/com/webank/webase/node/mgr/frontgroupmap/entity/FrontGroup.java index ac35210f2..273b97e77 100644 --- a/src/main/java/com/webank/webase/node/mgr/frontgroupmap/entity/FrontGroup.java +++ b/src/main/java/com/webank/webase/node/mgr/frontgroupmap/entity/FrontGroup.java @@ -25,4 +25,5 @@ public class FrontGroup { private Integer frontId; private String frontIp; private Integer frontPort; + private Integer status; } diff --git a/src/main/java/com/webank/webase/node/mgr/frontgroupmap/entity/MapListParam.java b/src/main/java/com/webank/webase/node/mgr/frontgroupmap/entity/MapListParam.java index 5ae5eb9d6..031c057ed 100644 --- a/src/main/java/com/webank/webase/node/mgr/frontgroupmap/entity/MapListParam.java +++ b/src/main/java/com/webank/webase/node/mgr/frontgroupmap/entity/MapListParam.java @@ -20,4 +20,5 @@ public class MapListParam { private int mapId; private int frontId; private int groupId; + private int status; } diff --git a/src/main/java/com/webank/webase/node/mgr/frontgroupmap/entity/TbFrontGroupMap.java b/src/main/java/com/webank/webase/node/mgr/frontgroupmap/entity/TbFrontGroupMap.java index 4e7731685..2cdc00513 100644 --- a/src/main/java/com/webank/webase/node/mgr/frontgroupmap/entity/TbFrontGroupMap.java +++ b/src/main/java/com/webank/webase/node/mgr/frontgroupmap/entity/TbFrontGroupMap.java @@ -26,9 +26,15 @@ public class TbFrontGroupMap { private Integer groupId; private LocalDateTime createTime; private LocalDateTime modifyTime; + /** + * front's group status, 1-normal + * @related GroupStatus enum + */ + private Integer status; - public TbFrontGroupMap(Integer frontId,Integer groupId){ + public TbFrontGroupMap(Integer frontId,Integer groupId, Integer status){ this.frontId = frontId; this.groupId = groupId; + this.status = status; } } diff --git a/src/main/java/com/webank/webase/node/mgr/frontinterface/FrontRestTools.java b/src/main/java/com/webank/webase/node/mgr/frontinterface/FrontRestTools.java index b7f440574..a9edbaf58 100644 --- a/src/main/java/com/webank/webase/node/mgr/frontinterface/FrontRestTools.java +++ b/src/main/java/com/webank/webase/node/mgr/frontinterface/FrontRestTools.java @@ -20,7 +20,7 @@ import com.webank.webase.node.mgr.base.exception.NodeMgrException; import com.webank.webase.node.mgr.base.properties.ConstantProperties; import com.webank.webase.node.mgr.frontgroupmap.entity.FrontGroup; -import com.webank.webase.node.mgr.frontgroupmap.entity.FrontGroupMapCache; +import com.webank.webase.node.mgr.frontgroupmap.FrontGroupMapCache; import com.webank.webase.node.mgr.frontinterface.entity.FailInfo; import java.time.Duration; import java.time.Instant; @@ -33,7 +33,6 @@ import java.util.Map; import java.util.Objects; -import com.webank.webase.node.mgr.group.GroupService; import lombok.extern.log4j.Log4j2; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -298,7 +297,7 @@ private T restTemplateExchange(int groupId, String uri, HttpMethod method, Object param, Class clazz) { List frontList = frontGroupMapCache.getMapListByGroupId(groupId); if (frontList == null || frontList.size() == 0) { - log.error("fail restTemplateExchange. frontList is empty"); + log.error("fail restTemplateExchange. frontList is empty groupId:{}", groupId); throw new NodeMgrException(ConstantCode.FRONT_LIST_NOT_FOUNT); } ArrayList list = new ArrayList<>(frontList); diff --git a/src/main/java/com/webank/webase/node/mgr/group/GroupService.java b/src/main/java/com/webank/webase/node/mgr/group/GroupService.java index bd2bcb5c1..3dbd39e78 100644 --- a/src/main/java/com/webank/webase/node/mgr/group/GroupService.java +++ b/src/main/java/com/webank/webase/node/mgr/group/GroupService.java @@ -15,11 +15,11 @@ import com.alibaba.fastjson.JSON; import com.webank.webase.node.mgr.base.code.ConstantCode; +import com.webank.webase.node.mgr.base.entity.BaseResponse; import com.webank.webase.node.mgr.base.enums.GroupStatus; import com.webank.webase.node.mgr.base.enums.GroupType; import com.webank.webase.node.mgr.base.exception.NodeMgrException; import com.webank.webase.node.mgr.base.properties.ConstantProperties; -import com.webank.webase.node.mgr.base.tools.NodeMgrTools; import com.webank.webase.node.mgr.block.BlockService; import com.webank.webase.node.mgr.block.entity.BlockInfo; import com.webank.webase.node.mgr.block.entity.TbBlock; @@ -29,7 +29,7 @@ import com.webank.webase.node.mgr.front.entity.TbFront; import com.webank.webase.node.mgr.front.entity.TotalTransCountInfo; import com.webank.webase.node.mgr.frontgroupmap.FrontGroupMapService; -import com.webank.webase.node.mgr.frontgroupmap.entity.FrontGroupMapCache; +import com.webank.webase.node.mgr.frontgroupmap.FrontGroupMapCache; import com.webank.webase.node.mgr.frontinterface.FrontInterfaceService; import com.webank.webase.node.mgr.frontinterface.entity.GenerateGroupInfo; import com.webank.webase.node.mgr.group.entity.*; @@ -49,7 +49,6 @@ import java.math.BigInteger; import java.time.Duration; import java.time.Instant; -import java.time.LocalDateTime; import java.util.*; /** @@ -219,7 +218,8 @@ public void resetGroupList() { saveGroup(gId, groupPeerList.size(), "synchronous", GroupType.SYNC.getValue(), GroupStatus.NORMAL.getValue()); } - frontGroupMapService.newFrontGroup(front.getFrontId(), gId); + // refresh front group map + frontGroupMapService.newFrontGroupWithStatus(front, gId); //save new peers savePeerList(frontIp, frontPort, gId, groupPeerList); //remove invalid peers @@ -695,11 +695,4 @@ public TbGroup getGroupById(Integer groupId) { return groupMapper.getGroupById(groupId); } - /** - * get status of group - * @return groupStatus - */ - public Integer getGroupStatus(Integer groupId) { - return getGroupById(groupId).getGroupStatus(); - } } From 40fba39904cc25d57759d37d5a26e00b9ce64d1d Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Mon, 25 May 2020 10:46:02 +0800 Subject: [PATCH 075/131] add status group_map --- script/webase-ddl.sql | 1 + 1 file changed, 1 insertion(+) diff --git a/script/webase-ddl.sql b/script/webase-ddl.sql index 57f08734a..c63a809bd 100644 --- a/script/webase-ddl.sql +++ b/script/webase-ddl.sql @@ -43,6 +43,7 @@ CREATE TABLE IF NOT EXISTS tb_front_group_map ( group_id int(11) NOT NULL COMMENT '群组编号', create_time datetime DEFAULT NULL COMMENT '创建时间', modify_time datetime DEFAULT NULL COMMENT '修改时间', + status int(11) DEFAULT 1 NOT NULL COMMENT '节点(前置)的群组状态', PRIMARY KEY (map_id), unique unique_front_group (front_id,group_id) ) ENGINE=InnoDB AUTO_INCREMENT=600001 DEFAULT CHARSET=utf8 COMMENT='前置群组映射表'; From 5b6f810a2f016709fd296dac0c9c8f001f922cfe Mon Sep 17 00:00:00 2001 From: CodingCattwo <847701726@qq.com> Date: Mon, 25 May 2020 11:52:38 +0800 Subject: [PATCH 076/131] fix group_map status null --- .../webase/node/mgr/frontgroupmap/FrontGroupMapCache.java | 3 +++ .../java/com/webank/webase/node/mgr/group/GroupService.java | 3 +-- src/main/resources/mapper/FrontGroupMapMapper.xml | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/webank/webase/node/mgr/frontgroupmap/FrontGroupMapCache.java b/src/main/java/com/webank/webase/node/mgr/frontgroupmap/FrontGroupMapCache.java index 759d4314e..7ee64fe4c 100644 --- a/src/main/java/com/webank/webase/node/mgr/frontgroupmap/FrontGroupMapCache.java +++ b/src/main/java/com/webank/webase/node/mgr/frontgroupmap/FrontGroupMapCache.java @@ -16,6 +16,9 @@ import com.webank.webase.node.mgr.base.enums.GroupStatus; import com.webank.webase.node.mgr.frontgroupmap.FrontGroupMapService; + +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.stream.Collectors; diff --git a/src/main/java/com/webank/webase/node/mgr/group/GroupService.java b/src/main/java/com/webank/webase/node/mgr/group/GroupService.java index 3dbd39e78..8b5b073b4 100644 --- a/src/main/java/com/webank/webase/node/mgr/group/GroupService.java +++ b/src/main/java/com/webank/webase/node/mgr/group/GroupService.java @@ -15,7 +15,6 @@ import com.alibaba.fastjson.JSON; import com.webank.webase.node.mgr.base.code.ConstantCode; -import com.webank.webase.node.mgr.base.entity.BaseResponse; import com.webank.webase.node.mgr.base.enums.GroupStatus; import com.webank.webase.node.mgr.base.enums.GroupType; import com.webank.webase.node.mgr.base.exception.NodeMgrException; @@ -327,7 +326,7 @@ private void checkAndUpdateGroupStatus(Set allGroupOnChain) { return; } - List allLocalGroup = getGroupList(null); + List allLocalGroup = getGroupList(null); if (CollectionUtils.isEmpty(allLocalGroup)) { return; } diff --git a/src/main/resources/mapper/FrontGroupMapMapper.xml b/src/main/resources/mapper/FrontGroupMapMapper.xml index 16db4d847..b462ff997 100644 --- a/src/main/resources/mapper/FrontGroupMapMapper.xml +++ b/src/main/resources/mapper/FrontGroupMapMapper.xml @@ -42,7 +42,7 @@ + select * from tb_front_group_map where 1=1 + + and map_id = #{param.mapId} + + + and front_id = #{frontId} + + + and group_id = #{groupId} + + + and status = #{status} + + limit 1 + +