diff --git a/script/uploadToServer.sh b/script/uploadToServer.sh index 5acecfd..9afdaaa 100644 --- a/script/uploadToServer.sh +++ b/script/uploadToServer.sh @@ -1,3 +1,5 @@ #/bin/bash scp -P 222 ./build/bdserver.zip dev@47.95.110.68:/data/public/releases/bdcontract/$1/ + +#In_235813 \ No newline at end of file diff --git a/src/main/java/org/bdware/heartbeat/HeartBeatUtil.java b/src/main/java/org/bdware/heartbeat/HeartBeatUtil.java index 6bb82f8..50e415a 100644 --- a/src/main/java/org/bdware/heartbeat/HeartBeatUtil.java +++ b/src/main/java/org/bdware/heartbeat/HeartBeatUtil.java @@ -38,12 +38,13 @@ public class HeartBeatUtil { public synchronized void schedule(TimerTask checkAliveTask, int delay, int period) { try { - if (!recordedFuture.containsKey(checkAliveTask)) { - ScheduledFuture future = - ContractManager.scheduledThreadPool.scheduleWithFixedDelay( - checkAliveTask, delay, period, TimeUnit.MILLISECONDS); - recordedFuture.put(checkAliveTask, future); + if (recordedFuture.containsKey(checkAliveTask)) { + cancel(checkAliveTask); } + ScheduledFuture future = + ContractManager.scheduledThreadPool.scheduleWithFixedDelay( + checkAliveTask, delay, period, TimeUnit.MILLISECONDS); + recordedFuture.put(checkAliveTask, future); } catch (Exception e) { e.printStackTrace(); } diff --git a/src/main/java/org/bdware/server/action/CMActions.java b/src/main/java/org/bdware/server/action/CMActions.java index 9e22d12..313095d 100644 --- a/src/main/java/org/bdware/server/action/CMActions.java +++ b/src/main/java/org/bdware/server/action/CMActions.java @@ -674,7 +674,7 @@ public class CMActions implements OnHashCallback { c.setScript(script); c.setOwner(GlobalConf.instance.keyPair.getPublicKeyStr()); c.doSignature(GlobalConf.instance.keyPair); - ret.put(fileName, manager.startContractAndRedirect(c, System.out)); + ret.put(fileName, manager.startContract(c)); if (args.has("dumpPeriod")) { LOGGER.debug("启动后设置dump周期" + args.get("dumpPeriod").getAsString()); @@ -734,16 +734,16 @@ public class CMActions implements OnHashCallback { if (args.has("createParam")) { c.setCreateParam(args.get("createParam")); } - // if (!c.verifySignature()) { - // ret.put("data", "verify failed"); - // resultCallback.onResult(gson.toJson(ret)); - // return; - // } - + if (args.has("remoteDebugPort")) { + c.setRemoteDebugPort(args.get("remoteDebugPort").getAsInt()); + } + if (args.has("isDebug")) { + c.setDebug(true); + } if (path != null && path.startsWith("/")) { String parPath; if (args.has("isPrivate") && args.get("isPrivate").getAsBoolean()) { - parPath = GlobalConf.instance.privateCompiledDir + "/" + handler.getPubKey(); + parPath = GlobalConf.instance.privateCompiledDir + "/" + getLoginPubkey(args); } else { parPath = GlobalConf.instance.publicCompiledDir; } @@ -751,33 +751,11 @@ public class CMActions implements OnHashCallback { // TODO script should encoded!! File temp = new File(parPath + path); if (temp.isFile() && temp.exists()) c.setScript(temp.getAbsolutePath()); - - // TODO - // if (DoConfig.openContractDORegister) { - // File file = new File(dir.getAbsolutePath() + - // "/manifest.json"); - // if (file.isFile() && file.exists()) { - // try { - // InputStream manifestInput = new FileInputStream(file); - // ContractManifest cm = - // JsonUtil - // .fromJson(new - // InputStreamReader(manifestInput), ContractManifest.class); - // c.setDoipFlag(cm.doipFlag); - // if (cm.doi != null && !cm.doi.equals("")) - // c.setDOI(cm.doi); - // } catch (Exception e) { - // e.printStackTrace(); - // } - // } - // } else { - // c.setDoipFlag(false); - // } } catch (Exception e) { e.printStackTrace(); } } - ret.put("data", manager.startContractAndRedirect(c, System.out)); // createPS() + ret.put("data", manager.startContract(c)); // ret.put("cid", c.getID()); ret.put("executeTime", System.currentTimeMillis() - start); resultCallback.onResult(ret); @@ -834,15 +812,12 @@ public class CMActions implements OnHashCallback { if (args.has("createParam")) { c.setCreateParam(args.get("createParam")); } - // 上面那些。。有空再整理一下 - c.setDebug(false); + if (args.has("remoteDebugPort")) { + c.setRemoteDebugPort(args.get("remoteDebugPort").getAsInt()); + } + if (args.has("isDebug") && args.get("isDebug").getAsBoolean()) + c.setDebug(true); c.setOwner(args.get("verifiedPubKey").getAsString()); - - // if (!c.verifySignature()) { - // ret.put("data", "verify failed"); - // resultCallback.onResult(gson.toJson(ret)); - // return; - // } if (path != null && path.startsWith("/")) { String parPath; @@ -858,7 +833,7 @@ public class CMActions implements OnHashCallback { String ypkPath; File dir; if (args.has("isPrivate") && args.get("isPrivate").getAsBoolean()) { - parPath = GlobalConf.instance.privateDir + "/" + handler.getPubKey(); + parPath = GlobalConf.instance.privateDir + "/" + getLoginPubkey(args); dir = new File(parPath, parentPath.toString()); ypkPath = FileActions.autoCompile(parPath, dir.getName()); @@ -872,37 +847,12 @@ public class CMActions implements OnHashCallback { // TODO script should encoded!! c.setScript(ypkPath); - - /* - if (DoConfig.openContractDORegister) { - File file = new File(dir.getAbsolutePath() + "/manifest.json"); - if (file.isFile() && file.exists()) { - try { - InputStream manifestInput = new FileInputStream(file); - ContractManifest cm = - JsonUtil - .fromJson( - new InputStreamReader(manifestInput), - ContractManifest.class); - c.setDoipFlag(cm.doipFlag); - if (cm.doi != null && !cm.doi.equals("")) c.setDOI(cm.doi); - if (cm.authInfoPersistDOI != null - && !cm.authInfoPersistDOI.equals("")) - c.setAuthInfoPersistDOI(cm.authInfoPersistDOI); - } catch (Exception e) { - e.printStackTrace(); - } - } - } else { - c.setDoipFlag(false); - } - */ } catch (Exception e) { e.printStackTrace(); } } - ret.put("data", manager.startContractAndRedirect(c, System.out)); + ret.put("data", manager.startContract(c)); ret.put("status", true); ret.put("cid", c.getID()); ret.put("executeTime", System.currentTimeMillis() - start); @@ -929,6 +879,16 @@ public class CMActions implements OnHashCallback { ExecutionManager.instance.updateLocalContractToNodeCenter(); } + private String getLoginPubkey(JsonObject args) { + try { + if (handler != null) return handler.getPubKey(); + return args.get("pubKey").getAsString(); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + @Action(userPermission = 1 << 14, async = true) public void startContractAsDebug(JsonObject args, ResultCallback resultCallback) { if (args.has("verifiedPubKey")) { @@ -963,6 +923,9 @@ public class CMActions implements OnHashCallback { if (args.has("createParam")) { c.setCreateParam(args.get("createParam")); } + if (args.has("remoteDebugPort")) { + c.setRemoteDebugPort(args.get("remoteDebugPort").getAsInt()); + } // 上面那些。。有空再整理一下 c.setOwner(args.get("verifiedPubKey").getAsString()); @@ -986,7 +949,7 @@ public class CMActions implements OnHashCallback { String ypkPath; File dir; if (args.has("isPrivate") && args.get("isPrivate").getAsBoolean()) { - parPath = GlobalConf.instance.privateDir + "/" + handler.getPubKey(); + parPath = GlobalConf.instance.privateDir + "/" + getLoginPubkey(args); dir = new File(parPath, parentPath.toString()); ypkPath = FileActions.autoCompile(parPath, dir.getName()); @@ -1028,8 +991,7 @@ public class CMActions implements OnHashCallback { e.printStackTrace(); } } - - ret.put("data", manager.startContractAndRedirectWithDebug(c)); // createPS() + ret.put("data", manager.startContract(c)); // createPS() ret.put("cid", c.getID()); ret.put("executeTime", System.currentTimeMillis() - start); if (args.has("requestID")) { @@ -1178,7 +1140,6 @@ public class CMActions implements OnHashCallback { r.put("data", "success"); r.put("action", "transferHandler"); // 从这继续写 - LOGGER.debug("forContract :"); // strs2.append("[CMActions] forContract :\n"); resultCallback.onResult(r); @@ -1289,7 +1250,7 @@ public class CMActions implements OnHashCallback { ContractMeta meta = manager.statusRecorder.getContractMeta(rc.getContractID()); Contract contract = meta.contract; killContractProcess(args, resultCallback); - manager.startContractAndRedirect(contract, System.out); + manager.startContract(contract); String data = manager.listContractsWithOwner( args.get("verifiedPubKey").getAsString(), null, @@ -1366,7 +1327,7 @@ public class CMActions implements OnHashCallback { } String parPath = GlobalConf.instance.publicDir; if (args.has("isPrivate") && args.get("isPrivate").getAsBoolean()) { - parPath = GlobalConf.instance.privateDir + "/" + handler.getPubKey(); + parPath = GlobalConf.instance.privateDir + "/" + getLoginPubkey(args); } byte[] bb = YJSPacker.pack(new File(parPath, parentPath.toString()).getAbsolutePath()); @@ -1417,7 +1378,7 @@ public class CMActions implements OnHashCallback { resultCallback.onResult(r); } - @Action(userPermission = 1L << 26, async = true, httpAccess = false) + @Action(userPermission = 1L << 26, async = true) public void killAllContract(JsonObject args, ResultCallback resultCallback) { if (args.has("verifiedPubKey")) { if (ContractManager.checkNodeManager(args.get(("verifiedPubKey")).getAsString())) { @@ -1443,7 +1404,6 @@ public class CMActions implements OnHashCallback { if (sc2.hasNextInt()) pid = sc2.nextInt() + ""; } sc2.close(); - if (pid != null) { ProcessBuilder subProcess = new ProcessBuilder("kill", "-9", pid); @@ -1598,9 +1558,12 @@ public class CMActions implements OnHashCallback { unitPeers = JsonUtil.parseString(jsonObject.get("value").getAsString()).getAsJsonArray(); for (JsonElement jsonElemenPeer : unitPeers) { jsonPeer = jsonElemenPeer.getAsJsonObject(); - nodeName = jsonPeer.get("nodeName").getAsString(); - peerID = jsonPeer.get("peerID").getAsString(); - ipPort = jsonPeer.get("ipPort").getAsString(); + if (jsonPeer.has("nodeName")) + nodeName = jsonPeer.get("nodeName").getAsString(); + if (jsonPeer.has("peerID")) + peerID = jsonPeer.get("peerID").getAsString(); + if (jsonPeer.has("ipPort")) + ipPort = jsonPeer.get("ipPort").getAsString(); // NetworkManager.instance.peerID2TCPAddress.put(peer, ipPort); // try { // NetworkManager.instance.createTCPClient(peerID, ipPort); diff --git a/src/main/java/org/bdware/server/action/FileActions.java b/src/main/java/org/bdware/server/action/FileActions.java index 4d7fe89..ffb4e4f 100644 --- a/src/main/java/org/bdware/server/action/FileActions.java +++ b/src/main/java/org/bdware/server/action/FileActions.java @@ -55,6 +55,7 @@ public class FileActions { static Map fileMap = new HashMap<>(); static Map updateTime = new HashMap<>(); static io.netty.util.TimerTask clearUploadFailed; + ContractManagerFrameHandler handler; static { clearUploadFailed = @@ -69,11 +70,6 @@ public class FileActions { SyncResult.timer.newTimeout(clearUploadFailed, 5, TimeUnit.MINUTES); } - ContractManagerFrameHandler handler; - - // public FileActions() { - // handler = null; - // } public FileActions(ContractManagerFrameHandler webSocketFrameHandler) { handler = webSocketFrameHandler; @@ -420,7 +416,7 @@ public class FileActions { c.setScript(ypkPath); c.setOwner(owner); c.setDebug(isDebug); - CMActions.manager.startContractAndRedirect(c, System.out); // createPS() + CMActions.manager.startContractAndRedirect(c, System.out, null); // createPS() } } catch (Exception e) { e.printStackTrace(); @@ -658,7 +654,7 @@ public class FileActions { boolean isPrivate = false; if (args.has("isPrivate") && args.get("isPrivate").getAsBoolean()) { - parPath = GlobalConf.instance.privateDir + "/" + handler.getPubKey(); + parPath = GlobalConf.instance.privateDir + "/" + getPubkey(args); isPrivate = true; } changeSet.add(new File(parPath, toProjectName(projectName)).getAbsolutePath()); @@ -685,7 +681,7 @@ public class FileActions { boolean isPrivate = false; if (args.has("isPrivate") && args.get("isPrivate").getAsBoolean()) { - parPath = GlobalConf.instance.privateDir + "/" + handler.getPubKey(); + parPath = GlobalConf.instance.privateDir + "/" + getPubkey(args); isPrivate = true; } File dir = new File(parPath, toProjectName(projectName)); @@ -775,41 +771,41 @@ public class FileActions { } @Action(userPermission = 1L << 17) - public void listCompiledFiles(JsonObject json, ResultCallback resultCallback) { + public void listCompiledFiles(JsonObject args, ResultCallback resultCallback) { Response response = new Response(); response.action = "onListCompiledFiles"; response.data = "[]"; response.isPrivate = false; List dirs = new ArrayList<>(); - ReplyUtil.injectRequestID(response, json); + ReplyUtil.injectRequestID(response, args); File f; - if (json.has("isPrivate") && json.get("isPrivate").getAsBoolean()) { + if (args.has("isPrivate") && args.get("isPrivate").getAsBoolean()) { response.isPrivate = true; - f = new File(GlobalConf.instance.privateCompiledDir + "/" + handler.getPubKey()); + f = new File(GlobalConf.instance.privateCompiledDir + "/" + getPubkey(args)); } else { f = new File(GlobalConf.instance.publicCompiledDir); } - ReplyUtil.injectRequestID(response, json); + ReplyUtil.injectRequestID(response, args); returnFileListResponse(resultCallback, response, dirs, f); } // 合约提供者 @Action(userPermission = 1L << 15) - public void listProjects(JsonObject json, ResultCallback resultCallback) { + public void listProjects(JsonObject args, ResultCallback resultCallback) { Response response = new Response(); response.action = "onListProjects"; response.data = "[]"; response.isPrivate = false; List dirs = new ArrayList<>(); - ReplyUtil.injectRequestID(response, json); + ReplyUtil.injectRequestID(response, args); File f; - if (json.has("isPrivate") && json.get("isPrivate").getAsBoolean()) { + if (args.has("isPrivate") && args.get("isPrivate").getAsBoolean()) { response.isPrivate = true; - f = new File(GlobalConf.instance.privateDir + "/" + handler.getPubKey()); + f = new File(GlobalConf.instance.privateDir + "/" + getPubkey(args)); } else { f = new File(GlobalConf.instance.publicDir); } @@ -833,20 +829,20 @@ public class FileActions { } @Action(userPermission = 1 << 14) - public void getProject(JsonObject json, ResultCallback resultCallback) { + public void getProject(JsonObject args, ResultCallback resultCallback) { Response response = new Response(); response.action = "onGetProject"; response.data = "[]"; response.isPrivate = false; - ReplyUtil.injectRequestID(response, json); + ReplyUtil.injectRequestID(response, args); List dirs = new ArrayList<>(); - String project = json.get("project").getAsString(); + String project = args.get("project").getAsString(); String parPath; - if (json.has("isPrivate") && json.get("isPrivate").getAsBoolean()) { + if (args.has("isPrivate") && args.get("isPrivate").getAsBoolean()) { response.isPrivate = true; - parPath = GlobalConf.instance.privateDir + "/" + handler.getPubKey(); + parPath = GlobalConf.instance.privateDir + "/" + getPubkey(args); } else { parPath = GlobalConf.instance.publicDir; } @@ -867,21 +863,21 @@ public class FileActions { } @Action(userPermission = 1L << 15) - public void listProject(JsonObject json, ResultCallback resultCallback) { + public void listProject(JsonObject args, ResultCallback resultCallback) { Response response = new Response(); response.action = "onListProject"; - String project = json.get("project").getAsString(); + String project = args.get("project").getAsString(); response.data = "[]"; response.isPrivate = false; - ReplyUtil.injectRequestID(response, json); + ReplyUtil.injectRequestID(response, args); ListProjectResp resp = new ListProjectResp(); // List dirs = new ArrayList<>(); File f; - if (json.has("isPrivate") && json.get("isPrivate").getAsBoolean()) { - String str = "/" + handler.getPubKey(); + if (args.has("isPrivate") && args.get("isPrivate").getAsBoolean()) { + String str = "/" + getPubkey(args); f = new File(GlobalConf.instance.privateDir + str, project); response.isPrivate = true; } else { @@ -908,26 +904,26 @@ public class FileActions { } @Action(userPermission = 1L << 15) - public void listFile(JsonObject json, ResultCallback resultCallback) { + public void listFile(JsonObject args, ResultCallback resultCallback) { Response response = new Response(); response.action = "onListFile"; response.isPrivate = false; - ReplyUtil.injectRequestID(response, json); + ReplyUtil.injectRequestID(response, args); ListProjectResp resp = new ListProjectResp(); String parPath; - if (json.has("isPrivate") && json.get("isPrivate").getAsBoolean()) { + if (args.has("isPrivate") && args.get("isPrivate").getAsBoolean()) { response.isPrivate = true; - parPath = GlobalConf.instance.privateDir + "/" + handler.getPubKey(); + parPath = GlobalConf.instance.privateDir + "/" + getPubkey(args); } else { parPath = GlobalConf.instance.publicDir; } if (GlobalConf.instance.projectDir != null) { resp.isDir = false; - resp.path = json.get("path").getAsString(); + resp.path = args.get("path").getAsString(); File f = new File(parPath + resp.path); if (!resp.path.contains("..") && f.exists()) { if (isTextFile(f.getName())) { @@ -944,20 +940,20 @@ public class FileActions { } @Action(async = true, userPermission = 1 << 14) - public void downloadContractFromOtherHost(JsonObject json, ResultCallback resultCallback) { - String fileName = json.get("fileName").getAsString(); - String mainHost = json.get("mainHost").getAsString(); - String projectName = json.get("projectName").getAsString(); + public void downloadContractFromOtherHost(JsonObject args, ResultCallback resultCallback) { + String fileName = args.get("fileName").getAsString(); + String mainHost = args.get("mainHost").getAsString(); + String projectName = args.get("projectName").getAsString(); String url; Response response = new Response(); response.isPrivate = false; - ReplyUtil.injectRequestID(response, json); + ReplyUtil.injectRequestID(response, args); String projectDir; - if (json.has("isPrivate") && json.get("isPrivate").getAsBoolean()) { + if (args.has("isPrivate") && args.get("isPrivate").getAsBoolean()) { response.isPrivate = true; - String strr = "/" + handler.getPubKey(); + String strr = "/" + getPubkey(args); projectDir = GlobalConf.instance.privateDir + strr; // url = // "http://" @@ -1061,25 +1057,25 @@ public class FileActions { } @Action(userPermission = 1 << 14) - public void renameFile(JsonObject json, ResultCallback resultCallback) { + public void renameFile(JsonObject args, ResultCallback resultCallback) { Response response = new Response(); response.action = "onRenameFile"; response.data = "failed"; response.isPrivate = false; - ReplyUtil.injectRequestID(response, json); + ReplyUtil.injectRequestID(response, args); String parPath; - if (json.has("isPrivate") && json.get("isPrivate").getAsBoolean()) { + if (args.has("isPrivate") && args.get("isPrivate").getAsBoolean()) { response.isPrivate = true; - parPath = GlobalConf.instance.privateDir + "/" + handler.getPubKey(); + parPath = GlobalConf.instance.privateDir + "/" + getPubkey(args); } else { parPath = GlobalConf.instance.publicDir; } if (GlobalConf.instance.projectDir != null) { try { - String oldFile = json.get("oldFile").getAsString(); - String newFile = json.get("newFile").getAsString(); + String oldFile = args.get("oldFile").getAsString(); + String newFile = args.get("newFile").getAsString(); File f = new File(parPath + "/" + oldFile); if (!oldFile.contains("..") && !newFile.contains("..") && f.exists()) { File newF = new File(parPath + "/" + newFile); @@ -1099,19 +1095,19 @@ public class FileActions { } @Action(userPermission = 1L << 14) - public void createFile(JsonObject json, ResultCallback resultCallback) { - String dir = json.get("dir").getAsString(); - String fileName = json.get("name").getAsString(); - boolean isFolder = json.get("isFolder").getAsBoolean(); + public void createFile(JsonObject args, ResultCallback resultCallback) { + String dir = args.get("dir").getAsString(); + String fileName = args.get("name").getAsString(); + boolean isFolder = args.get("isFolder").getAsBoolean(); if (fileName.contains("..") || dir.contains("..")) { resultCallback.onResult( "{\"action\":\"onCreateFile\",\"status\":\"failed\",\"data\":\"incorrect fileName or dir\"}"); return; } String parPath; - if (json.has("isPrivate") && json.get("isPrivate").getAsBoolean()) { - parPath = GlobalConf.instance.privateDir + "/" + handler.getPubKey(); - if (isLocked(handler.getPubKey())) { + if (args.has("isPrivate") && args.get("isPrivate").getAsBoolean()) { + parPath = GlobalConf.instance.privateDir + "/" + getPubkey(args); + if (isLocked(getPubkey(args))) { resultCallback.onResult( "{\"action\":\"onCreateFile\",\"status\":\"failed\",\"data\":\"locked status\"}"); @@ -1137,8 +1133,8 @@ public class FileActions { File mainyjs = new File(newFile, fileName + ".yjs"); FileOutputStream mainyjsFout = new FileOutputStream(mainyjs, false); - if (json.has("projectTemplate")) { - switch (json.get("projectTemplate").getAsString()) { + if (args.has("projectTemplate")) { + switch (args.get("projectTemplate").getAsString()) { case "数据共享项目": case "Data Sharing Project": // 注册一个用于授权状态持久化的DO,并设置到manifest @@ -1178,14 +1174,14 @@ public class FileActions { // return; // } - if (json.has("projectDOI")) + if (args.has("projectDOI")) manifestFout.write( ("{\n \"main\":\"" + fileName + ".yjs\",\n" + " \"doipFlag\":true,\n" + " \"doi\":\"" - + json.get("projectDOI").getAsString() + + args.get("projectDOI").getAsString() + "\",\n" + " \"authInfoPersistDOI\":\"" + authInfoPersistDOI @@ -1214,12 +1210,12 @@ public class FileActions { .getBytes()); break; default: - if (json.has("projectDOI")) + if (args.has("projectDOI")) manifestFout.write( ("{\n \"main\":\"" + fileName + ".yjs\",\n \"doipFlag\":true,\n \"doi\":\"" - + json.get("projectDOI").getAsString() + + args.get("projectDOI").getAsString() + "\"\n}") .getBytes()); else @@ -1242,20 +1238,20 @@ public class FileActions { e.printStackTrace(); } } - listProjects(json, resultCallback); + listProjects(args, resultCallback); } @Action(userPermission = 1 << 14) - public void deleteFile(JsonObject json, ResultCallback resultCallback) { + public void deleteFile(JsonObject args, ResultCallback resultCallback) { Response response = new Response(); response.action = "onDeleteFile"; response.data = "failed"; response.isPrivate = false; boolean compiled = false, isPrivate = false; - if (json.has("isCompiled") && json.get("isCompiled").getAsBoolean()) { + if (args.has("isCompiled") && args.get("isCompiled").getAsBoolean()) { compiled = true; } - if (json.has("isPrivate") && json.get("isPrivate").getAsBoolean()) { + if (args.has("isPrivate") && args.get("isPrivate").getAsBoolean()) { response.isPrivate = true; isPrivate = true; } @@ -1264,14 +1260,14 @@ public class FileActions { if (compiled) { if (isPrivate) { - parPath = GlobalConf.instance.privateCompiledDir + "/" + handler.getPubKey(); + parPath = GlobalConf.instance.privateCompiledDir + "/" + getPubkey(args); } else { parPath = GlobalConf.instance.publicCompiledDir; } } else { if (isPrivate) { - parPath = GlobalConf.instance.privateDir + "/" + handler.getPubKey(); - if (isLocked(handler.getPubKey())) { + parPath = GlobalConf.instance.privateDir + "/" + getPubkey(args); + if (isLocked(getPubkey(args))) { response.data = "failed, in locked status"; resultCallback.onResult(JsonUtil.toJson(response)); return; @@ -1283,7 +1279,7 @@ public class FileActions { if (GlobalConf.instance.projectDir != null) { try { - String oldFile = json.get("file").getAsString(); + String oldFile = args.get("file").getAsString(); File f = new File(parPath + "/" + oldFile); if (!oldFile.contains("..") && f.exists()) { @@ -1303,7 +1299,7 @@ public class FileActions { e.printStackTrace(); } } - ReplyUtil.injectRequestID(response, json); + ReplyUtil.injectRequestID(response, args); resultCallback.onResult(JsonUtil.toJson(response)); } @@ -1361,18 +1357,18 @@ public class FileActions { // 将私有路径下合约传至共有路径 @Action(userPermission = 1 << 14) - public void changePublic(JsonObject json, ResultCallback resultCallback) throws Exception { - String pubkey = handler.getPubKey(); - String fileName = json.get("fileName").getAsString(); // 文件夹名 + public void changePublic(JsonObject args, ResultCallback resultCallback) throws Exception { + + String fileName = args.get("fileName").getAsString(); // 文件夹名 Response response = new Response(); response.action = "onChangePublic"; response.data = "failed"; response.isPrivate = false; - ReplyUtil.injectRequestID(response, json); + ReplyUtil.injectRequestID(response, args); - File file = new File(GlobalConf.instance.privateDir + "/" + handler.getPubKey(), fileName); + File file = new File(GlobalConf.instance.privateDir + "/" + getPubkey(args), fileName); LOGGER.debug("[FileActions] from " + file.getAbsolutePath()); File copyFile = new File(GlobalConf.instance.publicDir + "/" + fileName); @@ -1393,30 +1389,30 @@ public class FileActions { // 参数:isAppend // 参数:content @Action(userPermission = 1 << 14) - public void uploadFile(JsonObject json, ResultCallback resultCallback) throws Exception { + public void uploadFile(JsonObject args, ResultCallback resultCallback) throws Exception { Response response = new Response(); response.action = "onUploadFile"; response.data = "failed"; response.isPrivate = false; - ReplyUtil.injectRequestID(response, json); + ReplyUtil.injectRequestID(response, args); - String fileName = json.get("fileName").getAsString(); + String fileName = args.get("fileName").getAsString(); if (!checkFileType(fileName)) { response.data = "Illegal file"; resultCallback.onResult(JsonUtil.toJson(response)); return; } try { - boolean isAppend = json.get("isAppend").getAsBoolean(); - String path = json.get("path").getAsString(); + boolean isAppend = args.get("isAppend").getAsBoolean(); + String path = args.get("path").getAsString(); - String content = json.get("content").getAsString(); + String content = args.get("content").getAsString(); File target; String parPath; - if (json.has("isPrivate") && json.get("isPrivate").getAsBoolean()) { + if (args.has("isPrivate") && args.get("isPrivate").getAsBoolean()) { response.isPrivate = true; - parPath = GlobalConf.instance.privateDir + "/" + handler.getPubKey(); + parPath = GlobalConf.instance.privateDir + "/" + getPubkey(args); } else { parPath = GlobalConf.instance.publicDir; } @@ -1467,6 +1463,16 @@ public class FileActions { // 参数增加一个,type,支持:text类型和byte[](base64编码后上传) // 参数增加一个,path? /public/ziptest/xxxxx; /567896789056789/zip + private String getPubkey(JsonObject args) { + try { + if (handler != null) return handler.getPubKey(); + return args.get("pubKey").getAsString(); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + @Action(userPermission = 1 << 14) public void saveFile(JsonObject json, ResultCallback resultCallback) throws Exception { Response response = new Response(); @@ -1483,8 +1489,8 @@ public class FileActions { String parPath; if (json.has("isPrivate") && json.get("isPrivate").getAsBoolean()) { response.isPrivate = true; - parPath = GlobalConf.instance.privateDir + "/" + handler.getPubKey(); - if (isLocked(handler.getPubKey())) { + parPath = GlobalConf.instance.privateDir + "/" + getPubkey(json); + if (isLocked(getPubkey(json))) { response.data = "failed, in locked status"; resultCallback.onResult(JsonUtil.toJson(response)); return; @@ -1495,13 +1501,14 @@ public class FileActions { LOGGER.debug("[FileAction] saveFile:" + content); File target = new File(parPath + "/" + path); - if (!path.contains("..") && target.exists() && isTextFile(path)) { - + if (!target.exists()) { + response.data = "failed, file not exists! create file first."; + } else if (!path.contains("..") && isTextFile(path)) { FileOutputStream fout = new FileOutputStream(target, isAppend); fout.write(content.getBytes()); fout.close(); changeSet.add(new File(parPath, toProjectName(path)).getAbsolutePath()); - response.data = "successs"; + response.data = "success"; } else { response.data = "failed, not text type!"; } @@ -1538,7 +1545,7 @@ public class FileActions { // ContractManager.toObject(contractDO.elements.get(0).getData()); // String projectName = contractDOI.replace('/', '_'); // String projectDirStr = - // GlobalConf.instance.privateDir + "/" + handler.getPubKey() + "/" + + // GlobalConf.instance.privateDir + "/" + getPubkey(args) + "/" + // projectName; // File projectDir = new File(projectDirStr); // if (projectDir.isDirectory()) { @@ -1593,7 +1600,7 @@ public class FileActions { String projectName = args.get("path").getAsString(); String parPath = GlobalConf.instance.publicDir; if (args.has("isPrivate") && args.get("isPrivate").getAsBoolean()) { - parPath = GlobalConf.instance.privateDir + "/" + handler.getPubKey(); + parPath = GlobalConf.instance.privateDir + "/" + getPubkey(args); } String ypkPath = autoCompile(parPath, toProjectName(projectName)); Map ret = new HashMap<>(); @@ -1619,9 +1626,14 @@ public class FileActions { @Action(async = true, userPermission = 1L << 14) public void distributeContract(JsonObject json, ResultCallback resultCallback) { + if (getPubkey(json).startsWith("04303")) { + resultCallback.onResult("ERROR"); + return; + } + SM2KeyPair keyPair = GlobalConf.instance.keyPair; json.addProperty("sponsorPubkey", keyPair.getPublicKeyStr()); // 节点 - json.addProperty("pubkey", handler.getPubKey()); // 用户 + json.addProperty("pubkey", getPubkey(json)); // 用户 String reqID = System.currentTimeMillis() + "_" + (int) (Math.random() * 10000) + "_distribute"; json.addProperty("distributeID", reqID); diff --git a/src/main/java/org/bdware/server/action/ManagerActions.java b/src/main/java/org/bdware/server/action/ManagerActions.java index 7fd6e58..992227e 100644 --- a/src/main/java/org/bdware/server/action/ManagerActions.java +++ b/src/main/java/org/bdware/server/action/ManagerActions.java @@ -42,11 +42,7 @@ public class ManagerActions { public void getEncodedUUID(JsonObject args, ResultCallback resultCallback) { String data = null; try { - data = - ByteUtils.toHexString( - SM2Util.encrypt( - GlobalConf.instance.keyPair.getPublicKey(), - HardwareInfo.getCPUID().getBytes())); + data = ByteUtils.toHexString(SM2Util.encrypt(GlobalConf.instance.keyPair.getPublicKey(), HardwareInfo.getCPUID().getBytes())); } catch (Exception e) { e.printStackTrace(); } @@ -55,8 +51,7 @@ public class ManagerActions { @Action(userPermission = 1 << 10) public void downloadUUID(JsonObject args, ResultCallback resultCallback) { - String uuid = - ByteUtil.encodeBASE64(HardwareInfo.getCPUID().getBytes()).replaceAll("\n", ""); + String uuid = ByteUtil.encodeBASE64(HardwareInfo.getCPUID().getBytes()).replaceAll("\n", ""); resultCallback.onResult(uuid); } @@ -65,8 +60,7 @@ public class ManagerActions { try { String key = args.get("key").getAsString(); String val = args.get("val").getAsString(); - boolean - status = true; + boolean status = true; Object data; switch (key) { case "licence": @@ -87,6 +81,9 @@ public class ManagerActions { case "nodeCenter": data = status = GlobalConf.resetNodeCenter(val); break; + case "nodeCenterWS": + data = status = GlobalConf.resetNodeCenterWS(val); + break; case "nodeName": data = status = GlobalConf.resetNodeName(val); break; @@ -101,8 +98,9 @@ public class ManagerActions { return; } catch (Exception e) { e.printStackTrace(); + ReplyUtil.replyWithStatus(resultCallback, "onUpdateConfig", false, "exception:" + e.getMessage()); + } - resultCallback.onResult("Exception"); } @Action(userPermission = 1L << 11) @@ -111,8 +109,7 @@ public class ManagerActions { Map data = new HashMap<>(); data.put("projectDir", GlobalConf.instance.projectDir); data.put("yjsPath", GlobalConf.instance.yjsPath); - if (GlobalConf.instance.datachainConf != null) - data.put("datachainConf", GlobalConf.instance.datachainConf); + if (GlobalConf.instance.datachainConf != null) data.put("datachainConf", GlobalConf.instance.datachainConf); data.put("nodeCenter", GlobalConf.getNodeCenterUrl()); data.put("licence", GlobalConf.instance.licence); data.put("expireTime", new Date(GlobalConf.instance.expireTime) + ""); @@ -123,13 +120,12 @@ public class ManagerActions { @Action(userPermission = 1L << 11) public void listNodeInfos(JsonObject args, ResultCallback resultCallback) { - ControllerManager.getNodeCenterController().getNodeInfos( - new ResultCallback() { - @Override - public void onResult(String str) { - ReplyUtil.replyWithStatus(resultCallback, "onListNodeInfos", true, str); - } - }); + ControllerManager.getNodeCenterController().getNodeInfos(new ResultCallback() { + @Override + public void onResult(String str) { + ReplyUtil.replyWithStatus(resultCallback, "onListNodeInfos", true, str); + } + }); } @Action(userPermission = 1L << 26) @@ -154,7 +150,7 @@ public class ManagerActions { data.put("clusterConnected", String.valueOf(NetworkManager.instance.isConnectedToNodeCenter())); data.put("nodePubKey", GlobalConf.instance.keyPair.getPublicKeyStr()); data.put("masterAddress", GlobalConf.instance.masterAddress); - data.put("nodeCenterWS",GlobalConf.getNodeCenterWSUrl()); + data.put("nodeCenterWS", GlobalConf.getNodeCenterWSUrl()); ReplyUtil.replyWithStatus(resultCallback, "onLoadNodeConfig", true, data); } @@ -164,6 +160,7 @@ public class ManagerActions { LOGGER.debug(JsonUtil.toJson(args)); ReplyUtil.replyWithStatus(resultCallback, "onChangeNodeCenter", true, GlobalConf.resetNodeCenter(val)); } + @Action(userPermission = 1 << 10) public void changeNodeCenterWS(JsonObject args, ResultCallback resultCallback) { String val = args.get("data").getAsString(); @@ -221,8 +218,7 @@ public class ManagerActions { String pubKey = null; if (json.has("verifiedPubKey")) pubKey = json.get("verifiedPubKey").getAsString(); if (pubKey == null) { - resultCallback.onResult( - "{\"action\":\"onApplyNodeRole\",\"data\":\"Failed: Illegal user\"}"); + resultCallback.onResult("{\"action\":\"onApplyNodeRole\",\"data\":\"Failed: Illegal user\"}"); return; } String ro = json.get("role").getAsString(); @@ -230,28 +226,21 @@ public class ManagerActions { LOGGER.debug("[role]" + ro); Role role = Role.parse(json.get("role").getAsString()); if (role != Role.Anonymous) { - String str = - KeyValueDBUtil.instance.getValue(CMTables.ApplyRole.toString(), pubKey); - String already = - KeyValueDBUtil.instance.getValue(CMTables.NodeRole.toString(), pubKey); + String str = KeyValueDBUtil.instance.getValue(CMTables.ApplyRole.toString(), pubKey); + String already = KeyValueDBUtil.instance.getValue(CMTables.NodeRole.toString(), pubKey); if (already != null && already.contains(role.toString())) { ReplyUtil.simpleReply(resultCallback, "onApplyNodeRole", "already has!"); return; } if (str == null || str.length() == 0) { - KeyValueDBUtil.instance.setValue( - CMTables.ApplyRole.toString(), pubKey, role.name()); + KeyValueDBUtil.instance.setValue(CMTables.ApplyRole.toString(), pubKey, role.name()); } else { if (!str.contains(role.name())) - KeyValueDBUtil.instance.setValue( - CMTables.ApplyRole.toString(), pubKey, str + "," + role.name()); + KeyValueDBUtil.instance.setValue(CMTables.ApplyRole.toString(), pubKey, str + "," + role.name()); } - KeyValueDBUtil.instance.setValue( - CMTables.ApplyTime.toString(), - pubKey, - Long.toString(new Date().getTime())); + KeyValueDBUtil.instance.setValue(CMTables.ApplyTime.toString(), pubKey, Long.toString(new Date().getTime())); ReplyUtil.simpleReply(resultCallback, "onApplyNodeRole", "success"); return; @@ -261,13 +250,11 @@ public class ManagerActions { ret.addProperty("action", "onApplyNodeRole"); ret.addProperty("data", "success"); ret.addProperty("role", ro); - resultCallback.onResult( - ret); + resultCallback.onResult(ret); } catch (Exception e) { e.printStackTrace(); - resultCallback.onResult( - "{\"action\":\"onApplyNodeRole\",\"data\":\"Failed: Exception happened\"}"); + resultCallback.onResult("{\"action\":\"onApplyNodeRole\",\"data\":\"Failed: Exception happened\"}"); } } } diff --git a/src/main/java/org/bdware/server/action/MasterWSAction.java b/src/main/java/org/bdware/server/action/MasterWSAction.java index fe12551..23b364b 100644 --- a/src/main/java/org/bdware/server/action/MasterWSAction.java +++ b/src/main/java/org/bdware/server/action/MasterWSAction.java @@ -1,5 +1,7 @@ package org.bdware.server.action; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; import com.google.gson.JsonObject; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -18,33 +20,34 @@ import org.bdware.server.action.p2p.MasterServerTCPAction; import org.bdware.server.trustedmodel.MultiPointContractInfo; import org.bdware.server.trustedmodel.MultiPointCooperateContractInfo; import org.bdware.server.trustedmodel.ResultCollector; +import org.bdware.server.ws.ContractManagerFrameHandler; import org.bdware.units.NetworkManager; import org.zz.gmhelper.SM2KeyPair; import org.zz.gmhelper.SM2Util; import java.util.Arrays; import java.util.HashMap; +import java.util.List; import java.util.Map; -import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Collectors; // WebSocket接口 public class MasterWSAction { private static final Logger LOGGER = LogManager.getLogger(MasterWSAction.class); - private final UserManagerAction managerAction; + private final ContractManagerFrameHandler handler; - public MasterWSAction(UserManagerAction userManagerAction) { - this.managerAction = userManagerAction; + public MasterWSAction(ContractManagerFrameHandler handler) { + this.handler = handler; } public static boolean hostMaster(String contractID) { return CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID).isMaster(); } - private boolean waitForConnection(Set nodeNames) { + private boolean waitForConnection(List nodeNames) { LOGGER.info("waitForAllNodes:" + JsonUtil.toJson(nodeNames)); - for (int i = 0; i < 5; i++) { + for (int i = 0; i < 500; i++) { boolean all = true; for (String str : nodeNames) { if (!NetworkManager.instance.hasAgentConnection(str)) { @@ -68,6 +71,16 @@ public class MasterWSAction { return false; } + @Action(async = true) + public void warmConnection(JsonObject args, ResultCallback rc) { + long start = System.currentTimeMillis(); + JsonArray pubkeys = args.get("pubKeys").getAsJsonArray(); + for (JsonElement pri : pubkeys) + NetworkManager.instance.connectToAgent(pri.getAsString(), null); + long end = System.currentTimeMillis(); + rc.onResult("{\"duration\":" + (end - start) + ", \"action\":\"onWarmConnection\"}"); + } + @Action(async = true, userPermission = 1L << 26) // zyx modified public void startContractMultiPoint(JsonObject args, final ResultCallback rc) { @@ -92,8 +105,7 @@ public class MasterWSAction { } else if (args.has("projectName")) { contract.setScript("/" + args.get("projectName").getAsString()); } - if (args.has("createParam")) - contract.setCreateParam(args.get("createParam")); + if (args.has("createParam")) contract.setCreateParam(args.get("createParam")); // contract.setScript("/" + args.get("projectName").getAsString() + "/manifest.json"); // String toVerify = @@ -120,10 +132,7 @@ public class MasterWSAction { SM2KeyPair sm2Key; String parPath = GlobalConf.instance.publicCompiledDir; if (args.has("isPrivate") && args.get("isPrivate").getAsBoolean()) { - parPath = - GlobalConf.instance.privateCompiledDir - + "/" - + args.get("verifiedPubKey").getAsString(); + parPath = GlobalConf.instance.privateCompiledDir + "/" + args.get("verifiedPubKey").getAsString(); } String scriptOrPath = contract.getScriptStr(); @@ -131,8 +140,7 @@ public class MasterWSAction { scriptOrPath = parPath + contract.getScriptStr(); } String key = ContractManager.getContractMd5(scriptOrPath, contract); - String privateKeyStr = - KeyValueDBUtil.instance.getValue(CMTables.ContractInfo.toString(), key); + String privateKeyStr = KeyValueDBUtil.instance.getValue(CMTables.ContractInfo.toString(), key); if (privateKeyStr == null || privateKeyStr.length() == 0) { privateKeyStr = SM2Util.generateSM2KeyPair().toJson(); KeyValueDBUtil.instance.setValue(CMTables.ContractInfo.toString(), key, privateKeyStr); @@ -162,7 +170,7 @@ public class MasterWSAction { } } */ - Set nodeNames; // nodes' peerID + List nodeNames; // nodes' peerID // if (contract.getType().equals(ContractExecType.SelfAdaptiveSharding)) { // if (ContractManager.instance.nodeCenterConn.listNodes().length < 3) { // rc.onResult( @@ -177,21 +185,17 @@ public class MasterWSAction { // Math.max(unitSize, 3))); // } else { // all nodes' peerID in the unit - String[] nodeNamesStr = - args.get("peersID").getAsString().split(","); + String[] nodeNamesStr = args.get("peersID").getAsString().split(","); // record information of these nodes - nodeNames = - Arrays.stream(nodeNamesStr) - .filter(x -> null != x && !x.isEmpty()) - .collect(Collectors.toSet()); + nodeNames = Arrays.stream(nodeNamesStr).filter(x -> null != x && !x.isEmpty()).collect(Collectors.toList()); // } - String masterNode = GlobalConf.instance.keyPair.getPublicKeyStr(); - nodeNames.add(masterNode); + SM2KeyPair keyPair = GlobalConf.instance.keyPair; + String masterNode = keyPair.getPublicKeyStr(); + assert nodeNames.contains(masterNode); + //nodeNames.add(masterNode); int nodeSize = nodeNames.size(); - // 方式一向NodeCenter发,要求Slave节点主动连接到Master节点. - Map requestConnect = new HashMap<>(); requestConnect.put("action", "requestConnectToMaster"); LOGGER.debug(multiPointContractInfo.masterNode); @@ -200,50 +204,43 @@ public class MasterWSAction { if (contract.getType() == ContractExecType.Sharding) { requestConnect.put("connectAll", true); } - NetworkManager.instance.sendToNodeCenter(JsonUtil.toJson(requestConnect)); // 向NC发 - waitForConnection(nodeNames); + // NetworkManager.instance.sendToNodeCenter(JsonUtil.toJson(requestConnect)); // 向NC发 + //waitForConnection(nodeNames); LOGGER.debug(JsonUtil.toPrettyJson(requestConnect)); - ContractManager.threadPool.execute( - () -> { - // 多点合约更新repository信息 - if (contract.getDOI() != null && !contract.getDOI().equals("null")) { - LOGGER.info("multipoint contract doi register"); - GRPCPool.instance.reRegister(contract.getDOI()); - } else { - LOGGER.info( - "multipoint contract don't update register " - + (null == contract.getDOI() ? "null" : contract.getDOI())); - } - }); + ContractManager.threadPool.execute(() -> { + // 多点合约更新repository信息 + if (contract.getDOI() != null && !contract.getDOI().equals("null")) { + LOGGER.info("multipoint contract doi register"); + GRPCPool.instance.reRegister(contract.getDOI()); + } else { + LOGGER.info("multipoint contract don't update register " + (null == contract.getDOI() ? "null" : contract.getDOI())); + } + }); final long curr = System.currentTimeMillis(); String requestID = curr + "_" + (int) (Math.random() * 10000); - ResultCollector collector = - new ResultCollector( - requestID, - new ResultCallback() { - @Override - public void onResult(String str) { - Map ret = new HashMap<>(); - ret.put("action", "onStartContractTrustfullyResult"); - if (args.has("requestID")) { - ret.put("responseID", args.get("requestID").getAsString()); - } - ret.put("data", str); - ret.put("executionTime", (System.currentTimeMillis() - curr) + ""); - rc.onResult(ret); - } - }, - nodeSize); + ResultCollector collector = new ResultCollector(requestID, new ResultCallback() { + @Override + public void onResult(String str) { + Map ret = new HashMap<>(); + ret.put("action", "onStartContractTrustfullyResult"); + if (args.has("requestID")) { + ret.put("responseID", args.get("requestID").getAsString()); + } + ret.put("data", str); + ret.put("executionTime", (System.currentTimeMillis() - curr) + ""); + rc.onResult(ret); + } + }, nodeSize); MasterServerTCPAction.sync.sleepWithTimeout(requestID, collector, 20); Map request = new HashMap<>(); - request.put("master", masterNode); + request.put("master", keyPair.getPublicKeyStr()); if (args.has("isPrivate")) { request.put("isPrivate", args.get("isPrivate").getAsString()); } if (args.has("isDebug")) { contract.setDebug(args.get("isDebug").getAsBoolean()); } - request.put("pubKey", managerAction.getPubKey()); + request.put("pubKey", getPubKey(args)); request.put("action", "startContractTrustfully"); request.put("requestID", requestID); request.put("members", nodeNames); // 执行这个合约的所有节点的pubKey @@ -282,16 +279,14 @@ public class MasterWSAction { if (!MasterServerRecoverMechAction.recoverStatus.containsKey(nodeID)) { MasterServerRecoverMechAction.recoverStatus.put(nodeID, new ConcurrentHashMap<>()); } - MasterServerRecoverMechAction.recoverStatus - .get(nodeID) - .put(contractID, RecoverFlag.Fine); + MasterServerRecoverMechAction.recoverStatus.get(nodeID).put(contractID, RecoverFlag.Fine); } - rc.onResult( - "{\"status\":\"Success\",\"result\":\"" - + contract.getID() - + "\"," - + "\"action\":\"onStartTrustfulContract\"}"); + rc.onResult("{\"status\":\"Success\",\"result\":\"" + contract.getID() + "\"," + "\"action\":\"onStartTrustfulContract\"}"); LOGGER.info("startContractMultiPoint succeed!"); } + public String getPubKey(JsonObject jo) { + if (handler != null) return handler.getPubKey(); + return jo.get("pubKey").getAsString(); + } } diff --git a/src/main/java/org/bdware/server/action/UserManagerAction.java b/src/main/java/org/bdware/server/action/UserManagerAction.java index 7830887..c527379 100644 --- a/src/main/java/org/bdware/server/action/UserManagerAction.java +++ b/src/main/java/org/bdware/server/action/UserManagerAction.java @@ -32,7 +32,7 @@ public class UserManagerAction { private final Random random = new Random(); ContractManagerFrameHandler handler; private String sessionID; - private String pubKey; + private String publicKey; public static String getNodeManager() { return KeyValueDBUtil.instance.getValue(CMTables.ConfigDB.toString(), NodeManager); @@ -42,6 +42,7 @@ public class UserManagerAction { handler = contractManagerFrameHandler; } + @Action(userPermission = 0) public void getSessionID(JsonObject json, ResultCallback resultCallback) { sessionID = random.nextLong() + "_session_node"; @@ -57,7 +58,7 @@ public class UserManagerAction { LOGGER.debug("[NodeManagerAction] session:" + (sessionID)); String pubKey = json.get("pubKey").getAsString(); if (SM2Util.plainStrVerify(pubKey, sessionID, json.get("signature").getAsString())) { - this.pubKey = pubKey; + this.publicKey = pubKey; counter.inc(); // 是否有该用户的私有目录,没有就创建 // BUG修复:需要仅在验证通过后可创建。 @@ -67,7 +68,8 @@ public class UserManagerAction { } getNodeRole(json, resultCallback); } else { - handler.setPermission(0); + + setHandlerPermission(0); resultCallback.onResult( "{\"action\":\"onLogin\",\"status\":\"verify sign failed\"," + "\"data\":\"" + Role.Anonymous.name() + "\"}"); @@ -75,8 +77,13 @@ public class UserManagerAction { } } + private void setHandlerPermission(long i) { + if (handler != null) handler.setPermission(i); + } + public void getNodeRole(JsonObject json, ResultCallback resultCallback) { - if (pubKey == null) { + String pubKey = getPubKey(json); + if (getPubKey(json) == null) { resultCallback.onResult( "{\"action\":\"onLogin\",\"data\":\"" + Role.Anonymous.name() + "\"}"); return; @@ -89,20 +96,20 @@ public class UserManagerAction { ret = KeyValueDBUtil.instance.getValue(CMTables.NodeRole.toString(), pubKey); if (isNodeManager) { if (ret != null && ret.length() > 0) { - handler.setPermission(0x86000f41L | Role.compoundValue(ret.split(","))); + setHandlerPermission(0x86000f41L | Role.compoundValue(ret.split(","))); resultCallback.onResult( "{\"action\":\"onLogin\",\"data\":\"" + ret + "\"}"); } else { - handler.setPermission(0x86000f41L); + setHandlerPermission(0x86000f41L); resultCallback.onResult( "{\"action\":\"onLogin\",\"data\":\"NodeManager\"}"); } } else { if (ret == null || ret.equals("")) { ret = Role.Anonymous.name(); - handler.setPermission(0); + setHandlerPermission(0); } else { - handler.setPermission(Role.compoundValue(ret.split(","))); + setHandlerPermission(Role.compoundValue(ret.split(","))); } resultCallback.onResult("{\"action\":\"onLogin\",\"data\":\"" + ret + "\"}"); } @@ -112,7 +119,7 @@ public class UserManagerAction { CMTables.NodeRole.toString(), pubKey, "NodeManager"); KeyValueDBUtil.instance.setValue( CMTables.NodeTime.toString(), pubKey, Long.toString(new Date().getTime())); - handler.setPermission(0x86000d41L); + setHandlerPermission(0x86000d41L); resultCallback.onResult("{\"action\":\"onLogin\",\"data\":\"NodeManager\"}"); } } catch (Exception e) { @@ -124,7 +131,7 @@ public class UserManagerAction { @Action(userPermission = 0) public void applyNodeRole(JsonObject json, ResultCallback resultCallback) { - if (pubKey == null) { + if (getPubKey(json) == null) { resultCallback.onResult("{\"action\":\"onApplyNodeRole\",\"data\":\"missing pubKey\"}"); return; } @@ -133,7 +140,7 @@ public class UserManagerAction { LOGGER.debug("[role]" + ro); Role role = Role.parse(json.get("role").getAsString()); if (role != Role.Anonymous) { - applyNodeRoleAtDB(role, resultCallback); + applyNodeRoleAtDB(role, getPubKey(json), resultCallback); return; } } @@ -141,7 +148,7 @@ public class UserManagerAction { "{\"action\":\"onApplyNodeRole\",\"data\":\"success\",\"role\":\"" + ro + "\"}"); } - private void applyNodeRoleAtDB(Role role, ResultCallback resultCallback) { + private void applyNodeRoleAtDB(Role role, String pubKey, ResultCallback resultCallback) { String str = KeyValueDBUtil.instance.getValue(CMTables.ApplyRole.toString(), pubKey); String already = KeyValueDBUtil.instance.getValue(CMTables.NodeRole.toString(), pubKey); if (already != null && already.contains(role.toString())) { @@ -250,6 +257,9 @@ public class UserManagerAction { @Action(userPermission = 1 << 10) public void authNodeRole(JsonObject json, ResultCallback resultCallback) { String pubKey = json.get("pubKey").getAsString(); + if (json.has("authorizedPubKey")) { + pubKey = json.get("authorizedPubKey").getAsString(); + } boolean isAccept = json.get("isAccept").getAsBoolean(); if (pubKey == null || pubKey.length() == 0) { resultCallback.onResult("{\"action\":\"onAuthNodeRole\",\"data\":\"missing pubKey\"}"); @@ -286,15 +296,25 @@ public class UserManagerAction { @Action(userPermission = 0) public void queryRole(JsonObject args, final ResultCallback resultCallback) { - if (pubKey == null) { + String pubkey = getPubKey(args); + if (pubkey == null) { resultCallback.onResult("{\"action\":\"onQueryRole\",\"data\":\"anonymous\"}"); return; } - args.addProperty("pubKey", pubKey); + args.addProperty("pubKey", pubkey); args.addProperty("requestID", random.nextLong() + "_ID"); } public String getPubKey() { - return pubKey; + return getPubKey(null); + } + + private String getPubKey(JsonObject json) { + try { + if (this.publicKey != null) return this.publicKey; + return json.get("pubKey").getAsString(); + } catch (Exception e) { + return null; + } } } diff --git a/src/main/java/org/bdware/server/action/p2p/AliveCheckClientAction.java b/src/main/java/org/bdware/server/action/p2p/AliveCheckClientAction.java index 2562003..df9d2d4 100644 --- a/src/main/java/org/bdware/server/action/p2p/AliveCheckClientAction.java +++ b/src/main/java/org/bdware/server/action/p2p/AliveCheckClientAction.java @@ -20,8 +20,8 @@ import java.util.Map; import java.util.TimerTask; public class AliveCheckClientAction { - public static final int sendDelay = 2000; - public static final int checkDelay = 5000; + public static final int sendDelay = 20000; + public static final int checkDelay = 50000; private static final Logger LOGGER = LogManager.getLogger(AliveCheckClientAction.class); private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd.HH:mm:ss"); private final String masterPubkey; diff --git a/src/main/java/org/bdware/server/action/p2p/AliveCheckServerAction.java b/src/main/java/org/bdware/server/action/p2p/AliveCheckServerAction.java index a87d0cc..38532b5 100644 --- a/src/main/java/org/bdware/server/action/p2p/AliveCheckServerAction.java +++ b/src/main/java/org/bdware/server/action/p2p/AliveCheckServerAction.java @@ -11,7 +11,6 @@ import org.bdware.server.action.Action; import org.bdware.server.tcp.TCPServerFrameHandler; import org.bdware.units.NetworkManager; -import java.text.SimpleDateFormat; import java.util.*; public class AliveCheckServerAction { @@ -57,10 +56,10 @@ public class AliveCheckServerAction { try { long cur = System.currentTimeMillis(); if (cur - lastSlavePingTime >= (2L * delay)) { - LOGGER.info( - new SimpleDateFormat("yyyy-MM-dd.HH:mm:ss").format(lastSlavePingTime) - + " " - + delay); +// LOGGER.info( +// new SimpleDateFormat("yyyy-MM-dd.HH:mm:ss").format(lastSlavePingTime) +// + " " +// + delay); Set contracts = new HashSet<>(); String nodeID = pubKey; if (nodeID == null) { diff --git a/src/main/java/org/bdware/server/action/p2p/MasterClientTCPAction.java b/src/main/java/org/bdware/server/action/p2p/MasterClientTCPAction.java index 028fb11..02200b5 100644 --- a/src/main/java/org/bdware/server/action/p2p/MasterClientTCPAction.java +++ b/src/main/java/org/bdware/server/action/p2p/MasterClientTCPAction.java @@ -59,19 +59,12 @@ public class MasterClientTCPAction { // } request.put("contractID", contractID); - int lastSeq = - CMActions.manager - .multiContractRecorder - .getMultiContractMeta(contractID) - .getLastExeSeq(); + int lastSeq = CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID).getLastExeSeq(); request.put("lastExe", lastSeq + ""); request.put("master", "null"); // 不管之前master是哪个,NC处是null,所有节点重选 if (null != uniNumber) request.put("uniNumber", uniNumber); - String members = CMActions.manager - .multiContractRecorder - .getMultiContractMeta(contractID) - .joinMembers(","); + String members = CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID).joinMembers(","); request.put("members", members); NetworkManager.instance.sendToNodeCenter(JsonUtil.toJson(request)); @@ -114,8 +107,7 @@ public class MasterClientTCPAction { if (isMultiReq) { // 开始执行多点合约请求,需要将缓存的其他节点发来的同requestID的请求也触发调用 LOGGER.info("开始执行多点合约请求,需要将缓存的其他节点发来的同requestID的请求也触发调用"); - MultiRequestInfo mri = - MultiRequestInfo.reqInfos.get(request.getRequestID()); + MultiRequestInfo mri = MultiRequestInfo.reqInfos.get(request.getRequestID()); for (String uniID : mri.callbackMap.keySet()) { LOGGER.info("触发 uniID=" + uniID); MultiRequestInfo.exeMultiReq(request, uniID); @@ -142,10 +134,8 @@ public class MasterClientTCPAction { LOGGER.info("返回 uniID=" + cei.uniReqIDMap.get(seq) + " 的结果"); cei.resultMap.get(seq).onResult(JsonUtil.toJson(ret)); cei.setLastExeSeq(seq); - if (KeyValueDBUtil.instance.containsKey( - CMTables.LastExeSeq.toString(), contractID)) { - KeyValueDBUtil.instance.setValue( - CMTables.LastExeSeq.toString(), contractID, seq + ""); + if (KeyValueDBUtil.instance.containsKey(CMTables.LastExeSeq.toString(), contractID)) { + KeyValueDBUtil.instance.setValue(CMTables.LastExeSeq.toString(), contractID, seq + ""); } // ledger检查点 public static final int LEDGER_PERIOD = 100; //账本检查点 @@ -154,11 +144,8 @@ public class MasterClientTCPAction { boolean isMaster = CMActions.manager.getContractIsMaster(contractID); if (isMaster) { String lastHash; - if (KeyValueDBUtil.instance.containsKey( - CMTables.CheckPointLastHash.toString(), contractID)) { - lastHash = - KeyValueDBUtil.instance.getValue( - CMTables.CheckPointLastHash.toString(), contractID); + if (KeyValueDBUtil.instance.containsKey(CMTables.CheckPointLastHash.toString(), contractID)) { + lastHash = KeyValueDBUtil.instance.getValue(CMTables.CheckPointLastHash.toString(), contractID); } else { lastHash = "firstCheckPoint"; } @@ -167,8 +154,7 @@ public class MasterClientTCPAction { map.put("state", state); map.put("lastHash", lastHash); String data = JsonUtil.toJson(map); - String requestID = - contractID + "_" + "checkPoint_" + new Random().nextInt(); + String requestID = contractID + "_" + "checkPoint_" + new Random().nextInt(); CheckPointCallback cb = new CheckPointCallback(); ContractManager.checkPointToLedger(cb, contractID, data, requestID); } else { @@ -194,10 +180,7 @@ public class MasterClientTCPAction { SM2KeyPair keyPair = GlobalConf.instance.keyPair; ret.put("nodeID", keyPair.getPublicKeyStr()); ret.put("responseID", responseID); - ContractResult cr = - new ContractResult( - ContractResult.Status.Error, - new JsonPrimitive(ComponedContractResult.EXPIRED_REQ)); + ContractResult cr = new ContractResult(ContractResult.Status.Error, new JsonPrimitive(ComponedContractResult.EXPIRED_REQ)); ret.put("data", JsonUtil.toJson(cr)); cb.onResult(JsonUtil.toJson(ret)); } @@ -228,8 +211,7 @@ public class MasterClientTCPAction { public void onKillContractProcess(JsonObject jo, ResultCallback result) { LOGGER.info("[MasterClientTCPAction] : onKillContractProcess"); if (killUnitContractMap.containsKey(jo.get("requestID").getAsString())) { - KillUnitContractInfo info = - killUnitContractMap.get(jo.get("requestID").getAsString()); + KillUnitContractInfo info = killUnitContractMap.get(jo.get("requestID").getAsString()); Map r = new HashMap<>(); r.put("action", "onKillContractProcess"); r.put("data", jo.get("data")); @@ -251,12 +233,10 @@ public class MasterClientTCPAction { ContractClient cc = CMActions.manager.getClient(id); - if (KeyValueDBUtil.instance.containsKey( - CMTables.LastExeSeq.toString(), cc.getContractID())) { + if (KeyValueDBUtil.instance.containsKey(CMTables.LastExeSeq.toString(), cc.getContractID())) { KeyValueDBUtil.instance.delete(CMTables.LastExeSeq.toString(), cc.getContractID()); } - if (KeyValueDBUtil.instance.containsKey( - CMTables.UnitContracts.toString(), cc.getContractID())) { + if (KeyValueDBUtil.instance.containsKey(CMTables.UnitContracts.toString(), cc.getContractID())) { KeyValueDBUtil.instance.delete(CMTables.UnitContracts.toString(), cc.getContractID()); } File file = new File(GlobalConf.instance.ADSPDir + "/" + cc.getContractID()); @@ -298,8 +278,7 @@ public class MasterClientTCPAction { } //TODO master连接 // contractID2MasterInfo.put(contractID, this); // 记录contractID 和 master之间的对应关系 - MultiContractMeta multiContractMeta = - CMActions.manager.multiContractRecorder.createIfNotExist(contractID); + MultiContractMeta multiContractMeta = CMActions.manager.multiContractRecorder.createIfNotExist(contractID); multiContractMeta.setLastExeSeq(-1); if (!contract.getScriptStr().startsWith("/")) { contract.setScript(dumpToDisk(contract, jo)); @@ -308,6 +287,7 @@ public class MasterClientTCPAction { String[] pp = contract.getScriptStr().split("/"); String parPath = GlobalConf.instance.publicCompiledDir; + //TODO 第一次来的meta没有cid!!! synchronized (multiContractMeta) { // 合约执行信息 multiContractMeta.setYpkName(pp[pp.length - 1]); multiContractMeta.setMembers(jo.get("members").getAsJsonArray()); @@ -315,10 +295,7 @@ public class MasterClientTCPAction { multiContractMeta.key = contract.getKey(); multiContractMeta.publicKey = contract.getPublicKey(); if (jo.has("isPrivate") && jo.get("isPrivate").getAsBoolean()) { - parPath = - GlobalConf.instance.privateCompiledDir - + "/" - + jo.get("pubKey").getAsString(); + parPath = GlobalConf.instance.privateCompiledDir + "/" + jo.get("pubKey").getAsString(); multiContractMeta.setIsPrivate(true); multiContractMeta.setPubKeyPath(jo.get("pubKey").getAsString()); @@ -329,12 +306,7 @@ public class MasterClientTCPAction { try { File temp = new File(parPath, pp[pp.length - 1]); if (!temp.exists()) { - result.onResult( - String.format( - "{\"action\":\"onStartContractTrustfully\",\"result\":\"missing contract files\"," + - "\"requestID\":\"%s\",\"pubKey\":\"%s\"}", - jo.get("requestID").getAsString(), - GlobalConf.instance.keyPair.getPublicKeyStr())); + result.onResult(String.format("{\"action\":\"onStartContractTrustfully\",\"result\":\"missing contract files\"," + "\"requestID\":\"%s\",\"pubKey\":\"%s\"}", jo.get("requestID").getAsString(), GlobalConf.instance.keyPair.getPublicKeyStr())); return; } contract.setScript(temp.getAbsolutePath()); @@ -348,25 +320,19 @@ public class MasterClientTCPAction { else { multiContractMeta.setIsMaster(true); } - - LOGGER.debug("startup arguments: " + JsonUtil.toJson(contract)); - String ret = CMActions.manager.startContract(contract); // 调用CMActions 里的启动合约的方法,启动结果 - - LOGGER.debug("startup result: " + ret); CMActions.manager.multiContractRecorder.updateValue(multiContractMeta); - ContractMeta meta = CMActions.manager.statusRecorder.createIfNotExist(contractID); - + LOGGER.info("startup arguments: " + JsonUtil.toJson(contract)); + String ret = CMActions.manager.startContract(contract); // 调用CMActions 里的启动合约的方法,启动结果 + LOGGER.info("startup result: " + ret); + //IMPORTANT!!!!!!!!!! + // meta should get after manger.startContract, because startContract reInitilized it! + ContractMeta meta = CMActions.manager.statusRecorder.getContractMeta(contractID); + //IMPORTANT!!!!!!!!!! meta.setContractExecutor(createContractExecutor(contract, contractID, jo.get("master").getAsString(), multiContractMeta.getMembers())); // 分配不同的Executor // TODO 合约终止后从数据库中移除,但是为了测试可以人为制造合约终止但不从数据库中移除(异常停止) KeyValueDBUtil.instance.setValue(CMTables.UnitContracts.toString(), contractID, "exist"); - if (CMActions.manager.getClient(contract.getID()) != null) { - result.onResult( - "{\"action\":\"onStartContractTrustfully\",\"result\":\"success\",\"requestID\":\"" - + jo.get("requestID").getAsString() - + "\",\"pubKey\":\"" - + GlobalConf.instance.keyPair.getPublicKeyStr() - + "\"}"); + result.onResult("{\"action\":\"onStartContractTrustfully\",\"result\":\"success\",\"requestID\":\"" + jo.get("requestID").getAsString() + "\",\"pubKey\":\"" + GlobalConf.instance.keyPair.getPublicKeyStr() + "\"}"); ExecutionManager.instance.updateLocalContractToNodeCenter(); } else { Map resultMap = new HashMap<>(); @@ -385,19 +351,15 @@ public class MasterClientTCPAction { } private String dumpToDisk(Contract contract, JsonObject jo) { - String scriptName = - String.format("Script_%s", Math.abs(contract.getScriptStr().hashCode())); + String scriptName = String.format("Script_%s", Math.abs(contract.getScriptStr().hashCode())); try { jo.addProperty("isPrivate", true); - File scriptDir = - new File( - GlobalConf.instance.privateCompiledDir, jo.get("pubKey").getAsString()); + File scriptDir = new File(GlobalConf.instance.privateCompiledDir, jo.get("pubKey").getAsString()); if (!scriptDir.exists()) { scriptDir.mkdirs(); } - FileOutputStream fout = - new FileOutputStream(new File(scriptDir, scriptName + ".ypk"), false); + FileOutputStream fout = new FileOutputStream(new File(scriptDir, scriptName + ".ypk"), false); ZipOutputStream zout = new ZipOutputStream(fout); zout.putNextEntry(new ZipEntry("/manifest.json")); zout.write(("{\"main\":\"" + scriptName + ".yjs\"}").getBytes()); @@ -428,17 +390,10 @@ public class MasterClientTCPAction { @Action(async = true) public void executeContractLocally(JsonObject jo, ResultCallback result) { - final ContractRequest request = - JsonUtil.fromJson(jo.get("data").toString(), ContractRequest.class); - LOGGER.info("[MasterCientTCPAction] executeContractLocally " + JsonUtil.toJson(jo)); + final ContractRequest request = JsonUtil.fromJson(jo.get("data").toString(), ContractRequest.class); + // LOGGER.info("[MasterCientTCPAction] executeContractLocally " + JsonUtil.toJson(jo)); - LOGGER.info( - "[MasterClient] executeLocally,uniReq:" - + jo.get("uniReqID").getAsString() - + " ->reqID:" - + request.getRequestID() - + " " - + jo.get("data").toString()); + // LOGGER.info("[MasterClient] executeLocally,uniReq:" + jo.get("uniReqID").getAsString() + " ->reqID:" + request.getRequestID() + " " + jo.get("data").toString()); // requestOne and responseOne do not need sequencing if (!request.needSeq) { @@ -456,8 +411,7 @@ public class MasterClientTCPAction { result.onResult(JsonUtil.toJson(ret)); } else { // reqeust all response all need seq String contractID = request.getContractID(); - MultiContractMeta cei = - CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID); + MultiContractMeta cei = CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID); boolean putToQueue = false; // 对于多点合约的请求 if (request.getRequestID().endsWith("_mul")) { @@ -469,14 +423,12 @@ public class MasterClientTCPAction { // cei.curExeSeq + " cei.getLastExeSeq()=" + cei.getLastExeSeq() + " // 非第一个请求"); - MultiRequestInfo mri = - MultiRequestInfo.reqInfos.get(request.getRequestID()); + MultiRequestInfo mri = MultiRequestInfo.reqInfos.get(request.getRequestID()); if (request.seq == cei.curExeSeq) { // 正在执行多点请求时收到多点请求,说明这个请求不是第一个到的同requestID的请求s mri.callbackMap.put(jo.get("uniReqID").getAsString(), result); mri.putFlag(jo.get("uniReqID").getAsString(), false); - } else if (request.seq > cei.curExeSeq - && request.seq > cei.getLastExeSeq()) { + } else if (request.seq > cei.curExeSeq && request.seq > cei.getLastExeSeq()) { mri.callbackMap.put(jo.get("uniReqID").getAsString(), result); mri.putFlag(jo.get("uniReqID").getAsString(), false); } @@ -484,8 +436,7 @@ public class MasterClientTCPAction { // logger.info("【收到多点合约请求】request.seq=" + request.seq + " cei.curExeSeq=" + // cei.curExeSeq + " cei.getLastExeSeq()=" + cei.getLastExeSeq() + " // 是第一个请求"); - MultiRequestInfo mri = - new MultiRequestInfo(request.getRequestID(), request.seq); + MultiRequestInfo mri = new MultiRequestInfo(request.getRequestID(), request.seq); MultiRequestInfo.reqInfos.put(request.getRequestID(), mri); cei.addRequestQueue(request, jo.get("uniReqID").getAsString(), result); LOGGER.info("队列中加入请求 " + request.seq); @@ -500,13 +451,7 @@ public class MasterClientTCPAction { } } else { synchronized (cei) { - LOGGER.info( - "合约" - + contractID - + " 请求序号为 " - + request.seq - + " 上一次执行请求序号为 " - + cei.getLastExeSeq()); + LOGGER.info("合约" + contractID + " 请求序号为 " + request.seq + " 上一次执行请求序号为 " + cei.getLastExeSeq()); // cei.printContent(); if (request.seq <= cei.getLastExeSeq()) { @@ -540,8 +485,7 @@ public class MasterClientTCPAction { @Action(async = true) public void receiveContractExecutionServer(JsonObject jsonObject, ResultCallback resultCallback) { - MasterServerTCPAction.sync.wakeUp( - jsonObject.get("responseID").getAsString(), jsonObject.get("data").getAsString()); + MasterServerTCPAction.sync.wakeUp(jsonObject.get("responseID").getAsString(), jsonObject.get("data").getAsString()); } @Action(async = true) @@ -561,56 +505,32 @@ public class MasterClientTCPAction { @Action(async = true) public void reRouteContract(JsonObject jo, ResultCallback result) { LOGGER.info("Receive Reroute Info:" + jo.toString()); - MasterServerTCPAction.sync.instrumentWakeUp( - jo.get("responseID").getAsString(), - (resultCallback, result1) -> { - resultCallback.cancelTimeOut(); - LOGGER.info("try To reRoute"); + MasterServerTCPAction.sync.instrumentWakeUp(jo.get("responseID").getAsString(), (resultCallback, result1) -> { + resultCallback.cancelTimeOut(); + LOGGER.info("try To reRoute"); - JsonObject cr = result1.get("data").getAsJsonObject(); - if (resultCallback instanceof SyncResult.ContractResultCallback) { - SyncResult.ContractResultCallback cb = - (SyncResult.ContractResultCallback) resultCallback; - if (cb.getReRouteCount() > 0) { - ContractResult contractResult = - new ContractResult( - ContractResult.Status.Error, - new JsonPrimitive( - "Contract " - + cr.get("contractID").getAsString() - + "can't be located in router")); - resultCallback.onResult(JsonUtil.toJson(contractResult)); - return; - } else cb.incReRouteCount(); - LOGGER.info("inc reRoute:" + cb.getReRouteCount()); - } - String pubkey = - CMActions.manager.nodeCenterConn.reRouteContract( - cr.get("contractID").getAsString()); - LOGGER.info( - "ReRoute Result:" - + cr.get("contractID").getAsString() - + " pubKey:" - + pubkey); + JsonObject cr = result1.get("data").getAsJsonObject(); + if (resultCallback instanceof SyncResult.ContractResultCallback) { + SyncResult.ContractResultCallback cb = (SyncResult.ContractResultCallback) resultCallback; + if (cb.getReRouteCount() > 0) { + ContractResult contractResult = new ContractResult(ContractResult.Status.Error, new JsonPrimitive("Contract " + cr.get("contractID").getAsString() + "can't be located in router")); + resultCallback.onResult(JsonUtil.toJson(contractResult)); + return; + } else cb.incReRouteCount(); + LOGGER.info("inc reRoute:" + cb.getReRouteCount()); + } + String pubkey = CMActions.manager.nodeCenterConn.reRouteContract(cr.get("contractID").getAsString()); + LOGGER.info("ReRoute Result:" + cr.get("contractID").getAsString() + " pubKey:" + pubkey); - if (pubkey == null) { - ContractResult contractResult = - new ContractResult( - ContractResult.Status.Error, - new JsonPrimitive( - "Contract " - + cr.get("contractID").getAsString() - + "can't be located in router using reroute")); - resultCallback.onResult(JsonUtil.toJson(contractResult)); - return; - } - LOGGER.debug("Receive Reroute Result:" + pubkey); - ContractRequest contractRequest = - JsonUtil.fromJson(cr, ContractRequest.class); - CMActions.manager.masterStub.executeByOtherNodeAsync( - pubkey, contractRequest, resultCallback); - }, - jo); + if (pubkey == null) { + ContractResult contractResult = new ContractResult(ContractResult.Status.Error, new JsonPrimitive("Contract " + cr.get("contractID").getAsString() + "can't be located in router using reroute")); + resultCallback.onResult(JsonUtil.toJson(contractResult)); + return; + } + LOGGER.debug("Receive Reroute Result:" + pubkey); + ContractRequest contractRequest = JsonUtil.fromJson(cr, ContractRequest.class); + CMActions.manager.masterStub.executeByOtherNodeAsync(pubkey, contractRequest, resultCallback); + }, jo); } // public String requestContractExecution(ContractRequest c) { diff --git a/src/main/java/org/bdware/server/action/p2p/MasterServerRecoverMechAction.java b/src/main/java/org/bdware/server/action/p2p/MasterServerRecoverMechAction.java index 24fe0b6..9fc4c61 100644 --- a/src/main/java/org/bdware/server/action/p2p/MasterServerRecoverMechAction.java +++ b/src/main/java/org/bdware/server/action/p2p/MasterServerRecoverMechAction.java @@ -202,6 +202,8 @@ public class MasterServerRecoverMechAction { public static void unitModeCheck(String contractID) { String stateful = CMActions.manager.getContractStateful(contractID); + //just disable unitModeCheck + if (stateful!=null) return; if (stateful.equals("false")) { LOGGER.info("无状态集群合约无需运行模式检查及切换!"); return; diff --git a/src/main/java/org/bdware/server/action/p2p/MasterServerTCPAction.java b/src/main/java/org/bdware/server/action/p2p/MasterServerTCPAction.java index c95ad9f..348b287 100644 --- a/src/main/java/org/bdware/server/action/p2p/MasterServerTCPAction.java +++ b/src/main/java/org/bdware/server/action/p2p/MasterServerTCPAction.java @@ -184,7 +184,7 @@ public class MasterServerTCPAction { @Action(async = true) public void receiveTrustfullyResult(JsonObject jo, ResultCallback cb) { String responseID = jo.get("responseID").getAsString(); - LOGGER.info("========== ExecuteContractLocally wakeUp:" + responseID); + // LOGGER.info("========== ExecuteContractLocally wakeUp:" + responseID); MasterServerTCPAction.sync.wakeUp(responseID, jo.toString()); } diff --git a/src/main/java/org/bdware/server/action/p2p/_UNUSED_ExecutionAction.java b/src/main/java/org/bdware/server/action/p2p/_UNUSED_ExecutionAction.java index 167ffa4..cc2d6f9 100644 --- a/src/main/java/org/bdware/server/action/p2p/_UNUSED_ExecutionAction.java +++ b/src/main/java/org/bdware/server/action/p2p/_UNUSED_ExecutionAction.java @@ -97,7 +97,7 @@ public class _UNUSED_ExecutionAction implements OnHashCallback { c.setScript(content); } - content = CMActions.manager.startContractAndRedirect(c, System.out); + content = CMActions.manager.startContract(c); LOGGER.info("[START] result: " + content); result = JsonUtil.fromJson(content, ContractResult.class); unitContractMessage.msgType = UnitContractMessageType.ContractUnitResponse.getValue(); diff --git a/src/main/java/org/bdware/server/http/CMHttpHandler.java b/src/main/java/org/bdware/server/http/CMHttpHandler.java index 1a76e23..ebe6bfb 100644 --- a/src/main/java/org/bdware/server/http/CMHttpHandler.java +++ b/src/main/java/org/bdware/server/http/CMHttpHandler.java @@ -30,7 +30,6 @@ import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.nio.charset.StandardCharsets; import java.util.HashMap; -import java.util.List; import java.util.Map; import static io.netty.handler.codec.http.HttpResponseStatus.OK; @@ -60,17 +59,21 @@ public class CMHttpHandler extends SimpleChannelInboundHandler { }; HttpFileHandleAdapter otherFileService = new HttpFileHandleAdapter( new File("./WebContent/").getAbsolutePath(), fileFilter) { - @URIPath({"/client/", "/doc/"}) + @URIPath({"/client/", "/doc/", "/blog/"}) private void handleFile(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception { channelRead0(ctx, request); } }; + UserManagerAction userManagerAction = new UserManagerAction(null); + MasterWSAction masterWSAction = new MasterWSAction(null); actionExecutor = new ActionExecutor( ContractManagerFrameHandler.executorService, new CMActions(), ManagerActions.instance, + new FileActions(null), + userManagerAction, masterWSAction, GRPCPool.instance, new HttpFileAction(), new BDIndexerAction(), @@ -115,11 +118,11 @@ public class CMHttpHandler extends SimpleChannelInboundHandler { return flag && flag2; } }; - - for (String str : wsPluginActions) { - Object obj = createInstanceByClzName(str); - actionExecutor.appendHandler(obj); - } + if (wsPluginActions != null) + for (String str : wsPluginActions) { + Object obj = createInstanceByClzName(str); + actionExecutor.appendHandler(obj); + } uriHandlers = new URIHandler(); DOIPOverHttpHandler doipOverHttpHandler = new DOIPOverHttpHandler(); @@ -171,6 +174,7 @@ public class CMHttpHandler extends SimpleChannelInboundHandler { fullResponse.headers().remove("Access-Control-Allow-Origin"); fullResponse.headers().remove("Access-Control-Allow-Headers"); fullResponse.headers().add("Access-Control-Allow-Origin", "*"); + fullResponse.headers().add("Access-Control-Allow-Methods", "*"); fullResponse .headers() .add("Access-Control-Allow-Headers", @@ -183,55 +187,28 @@ public class CMHttpHandler extends SimpleChannelInboundHandler { @URIPath({"/SCIDE/CMManager", "/SCIDE/SCManager"}) public void handleHttpGet(ChannelHandlerContext ctx, FullHttpRequest msg) throws Exception { - QueryStringDecoder decoderQuery = new QueryStringDecoder(msg.uri()); - Map> parameters = decoderQuery.parameters(); - JsonObject transformedParam = new JsonObject(); - for (String key : parameters.keySet()) { - List val = parameters.get(key); - if (null != val) { - transformedParam.addProperty(key, val.get(0)); - } - } - - // 匿名用户权限为0 - transformedParam.addProperty("permission", 0); - transformedParam.remove("verifiedPubKey"); - - // 验签 有pubKey就必须有sign - String uri = URLDecoder.decode(msg.uri(), "UTF-8").split("\\?")[1]; - if (transformedParam.has("pubKey")) { - int index = uri.lastIndexOf('&'); - String str = uri.substring(0, index); - LOGGER.info("before verifying: " + str); - boolean verify = false; - try { - verify = - SM2Util.plainStrVerify( - transformedParam.get("pubKey").getAsString(), - str, - transformedParam.get("sign").getAsString()); - } catch (Exception e) { - LOGGER.error(e.getMessage()); - LOGGER.debug(ExceptionUtil.exceptionToString(e)); - } - LOGGER.info("verify signature: " + verify); - if (verify) { - // 查permission - String pubkey = transformedParam.get("pubKey").getAsString(); - String ret = KeyValueDBUtil.instance.getValue(CMTables.NodeRole.toString(), pubkey); - long permission; - if (ret != null && ret.length() > 0) { - permission = 0x86000d41L | Role.compoundValue(ret.split(",")); - } else { - assert ret != null; - permission = Role.compoundValue(ret.split(",")); + JsonObject transformedParam = ArgParser.parseGetAndVerify(msg, new ArgParser.VerifiedCallback() { + @Override + public void onResult(boolean verified, JsonObject transformedParam) { + LOGGER.info("verify signature: " + verified + " " + transformedParam.toString()); + if (verified) { + // 查permission + String pubkey = transformedParam.get("pubKey").getAsString(); + String ret = KeyValueDBUtil.instance.getValue(CMTables.NodeRole.toString(), pubkey); + long permission; + if (ret != null && ret.length() > 0) { + permission = 0x86000d41L | Role.compoundValue(ret.split(",")); + } else { + assert ret != null; + permission = Role.compoundValue(ret.split(",")); + } + transformedParam.addProperty("permission", permission); + LOGGER.info("user permission: " + permission); + transformedParam.addProperty( + "verifiedPubKey", transformedParam.get("pubKey").getAsString()); } - transformedParam.addProperty("permission", permission); - LOGGER.info("user permission: " + permission); - transformedParam.addProperty( - "verifiedPubKey", transformedParam.get("pubKey").getAsString()); } - } + }); handleReq(transformedParam, ctx, msg); } @@ -244,8 +221,6 @@ public class CMHttpHandler extends SimpleChannelInboundHandler { // 倒数第二个参数是pubKey,倒数第一个参数是sign // 有pubKey就必须签名 String uri = URLDecoder.decode(msg.uri(), "UTF-8"); - LOGGER.info("handle " + uri); - ByteBuf content = msg.content(); byte[] reqContent = new byte[content.readableBytes()]; content.readBytes(reqContent); @@ -307,7 +282,11 @@ public class CMHttpHandler extends SimpleChannelInboundHandler { DefaultFullHttpResponse response = new DefaultFullHttpResponse( HttpVersion.HTTP_1_1, OK, Unpooled.wrappedBuffer(ret)); + response.headers().add("Access-Control-Allow-Origin", "*"); + response.headers().add("Access-Control-Allow-Methods", "*"); ChannelFuture f = ctx.write(response); + response.headers().add("Access-Control-Allow-Origin", "*"); + response.headers().add("Access-Control-Allow-Methods", "*"); f.addListener(ChannelFutureListener.CLOSE); return; } else { @@ -317,14 +296,28 @@ public class CMHttpHandler extends SimpleChannelInboundHandler { HttpResultCallback cb; if (action.equals("downloadContract")) { cb = new FileDownloaderCallback(ctx, req); + } else if (action.equals("distributeContract")) { + cb = new HttpServerSentEventResultCallback(ctx, null); + cb.addHeader("Access-Control-Allow-Origin", "*"); + cb.addHeader("Access-Control-Allow-Methods", "*"); + cb.addHeader("Content-Type", "text/event-stream"); + cb.addHeader("Cache-Control", "no-cache, no-store, max-age=0, must-revalidate"); + cb.addHeader("Transfer-Encoding", "chunked"); + ((HttpServerSentEventResultCallback) cb).writeInitialHead(); } else if (map.has("callback")) { cb = new HttpResultCallback(ctx, map.get("callback").getAsString()); + cb.addHeader("Access-Control-Allow-Origin", "*"); + cb.addHeader("Access-Control-Allow-Methods", "*"); cb.addHeader("Content-Type", "application/json"); } else { cb = new HttpResultCallback(ctx, null); cb.addHeader("Content-Type", "application/json"); + cb.addHeader("Access-Control-Allow-Origin", "*"); + cb.addHeader("Access-Control-Allow-Methods", "*"); } if (map.get("action").getAsString().equals("downloadUUID")) { + cb.addHeader("Access-Control-Allow-Origin", "*"); + cb.addHeader("Access-Control-Allow-Methods", "*"); cb.addHeader("content-disposition", "attachment;filename=encodeduuid.key"); } actionExecutor.handle(action, map, cb); @@ -335,6 +328,8 @@ public class CMHttpHandler extends SimpleChannelInboundHandler { HttpVersion.HTTP_1_1, OK, Unpooled.wrappedBuffer(e.getMessage().getBytes())); + response.headers().add("Access-Control-Allow-Origin", "*"); + response.headers().add("Access-Control-Allow-Methods", "*"); ChannelFuture f = ctx.write(response); f.addListener(ChannelFutureListener.CLOSE); } catch (Exception e) { @@ -345,6 +340,8 @@ public class CMHttpHandler extends SimpleChannelInboundHandler { HttpVersion.HTTP_1_1, OK, Unpooled.wrappedBuffer(JsonUtil.toJson(ret).getBytes())); + response.headers().add("Access-Control-Allow-Origin", "*"); + response.headers().add("Access-Control-Allow-Methods", "*"); ChannelFuture f = ctx.write(response); f.addListener(ChannelFutureListener.CLOSE); } diff --git a/src/main/java/org/bdware/server/http/DOIPOverHttpHandler.java b/src/main/java/org/bdware/server/http/DOIPOverHttpHandler.java index 55ab9eb..d1ffa4e 100644 --- a/src/main/java/org/bdware/server/http/DOIPOverHttpHandler.java +++ b/src/main/java/org/bdware/server/http/DOIPOverHttpHandler.java @@ -20,10 +20,7 @@ import org.zz.gmhelper.SM2Util; import java.io.*; import java.net.URLDecoder; -import java.util.EnumSet; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; @@ -32,6 +29,28 @@ import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1; public class DOIPOverHttpHandler { private static final Logger LOGGER = LogManager.getLogger(DOIPOverHttpHandler.class); Map zipFilePool = new HashMap<>(); + Map lastVisit = new HashMap<>(); + + public synchronized void pruneZipPool() { + Set toPrune = new HashSet<>(); + long current = System.currentTimeMillis(); + for (String str : zipFilePool.keySet()) { + Long last = lastVisit.get(str); + if (last != null && current - last > 30L * 24L * 3600L * 1000L) { + toPrune.add(str); + } + } + for (String str : toPrune) { + try { + ZipFile zf = zipFilePool.get(str); + zipFilePool.remove(str); + lastVisit.remove(str); + zf.close(); + } catch (Exception e) { + + } + } + } @URIPath({"/contracts/", "/DOIP/"}) public void mockDOIP(ChannelHandlerContext ctx, FullHttpRequest request) { @@ -142,7 +161,6 @@ public class DOIPOverHttpHandler { return; } - // TODO @杨环宇 把zipFilePool用起来,做一个延迟关闭的东西,避免频繁打开文件的操作。 private void sendAssets(JsonObject arg, ChannelHandlerContext ctx) throws Exception { ContractMeta meta = @@ -153,9 +171,8 @@ public class DOIPOverHttpHandler { return; } String path = arg.get("argument").getAsString(); - ZipFile zipFile = new ZipFile(new File(meta.contract.getScriptStr())); + ZipFile zipFile = getZipFileFromPool(meta.contract.getScriptStr()); ZipEntry zipEntry = zipFile.getEntry(path); - if (zipEntry == null) { HttpFileHandleAdapter.sendError(ctx, HttpResponseStatus.NOT_FOUND); return; @@ -170,7 +187,7 @@ public class DOIPOverHttpHandler { @Override public void operationComplete(Future arg0) throws Exception { ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT); - zipFile.close(); + in.close(); } }); // 写入文件尾部 @@ -178,6 +195,22 @@ public class DOIPOverHttpHandler { return; } + private ZipFile getZipFileFromPool(String scriptStr) throws Exception { + try { + ZipFile cachedFile = zipFilePool.get(scriptStr); + if (cachedFile == null) { + cachedFile = + new ZipFile(new File(scriptStr)); + zipFilePool.put(scriptStr, cachedFile); + } + lastVisit.put(scriptStr, System.currentTimeMillis()); + return cachedFile; + } catch (Exception e) { + e.printStackTrace(); + } + return new ZipFile(new File(scriptStr)); + } + private void injectExtraArgs(JsonObject transformedParam, String fulluri) { String[] data = fulluri.split("/"); @@ -213,7 +246,7 @@ public class DOIPOverHttpHandler { String path = fulluri.substring(index); path = path.replaceAll("\\?.*$", ""); if (path.equals("/assets/")) - path+="index.html"; + path += "index.html"; transformedParam.addProperty("argument", path); } if (assetPath.startsWith("ypk?")) { diff --git a/src/main/java/org/bdware/server/nodecenter/client/NodeCenterClientController.java b/src/main/java/org/bdware/server/nodecenter/client/NodeCenterClientController.java index 3e59ff9..897ef57 100644 --- a/src/main/java/org/bdware/server/nodecenter/client/NodeCenterClientController.java +++ b/src/main/java/org/bdware/server/nodecenter/client/NodeCenterClientController.java @@ -540,9 +540,7 @@ public class NodeCenterClientController implements NodeCenterConn { } public void queryNCRepoDOI(JsonObject json, ResultCallback result) { - LOGGER.debug("sendProject: position ---- 3"); String projectName = json.get("projectName").getAsString(); - LOGGER.debug("sendProject: position ---- 3\tprojectName=" + projectName); String parPath = GlobalConf.instance.publicCompiledDir; boolean isPrivate = false; if (json.has("isPrivate") && json.get("isPrivate").getAsBoolean()) { @@ -563,7 +561,7 @@ public class NodeCenterClientController implements NodeCenterConn { LOGGER.debug("tempZip = " + tempZip.getAbsolutePath()); try { FileInputStream fin = new FileInputStream(tempZip); - byte[] buff = new byte[30 * 1024]; + byte[] buff = new byte[500 * 1024]; long total = tempZip.length(); LOGGER.debug("temp length = " + total); long count = 0; @@ -663,9 +661,8 @@ public class NodeCenterClientController implements NodeCenterConn { } @Action(async = true) - public void onDistribute(JsonObject json, ResultCallback rc) { + public void onDistribute(JsonObject json, ResultCallback rc) throws IOException { if (json.has("over")) { - String distributeID = null; if (json.has("distributeID")) distributeID = json.get("distributeID").getAsString(); @@ -673,6 +670,9 @@ public class NodeCenterClientController implements NodeCenterConn { ResultCallback to = distributeReqMap.get(distributeID); distributeReqMap.remove(distributeID); to.onResult(json.get("content").getAsString()); + if (to instanceof Closeable) { + ((Closeable) to).close(); + } } else { String distributeID = json.get("distributeID").getAsString(); distributeReqMap.get(distributeID).onResult(json.get("content").getAsString()); diff --git a/src/main/java/org/bdware/server/ws/ContractManagerFrameHandler.java b/src/main/java/org/bdware/server/ws/ContractManagerFrameHandler.java index cd0d698..f914faa 100644 --- a/src/main/java/org/bdware/server/ws/ContractManagerFrameHandler.java +++ b/src/main/java/org/bdware/server/ws/ContractManagerFrameHandler.java @@ -57,7 +57,7 @@ public class ContractManagerFrameHandler extends SimpleChannelInboundHandler