diff --git a/build.gradle b/build.gradle index a579b1c..94917d8 100644 --- a/build.gradle +++ b/build.gradle @@ -5,6 +5,8 @@ plugins { id 'signing' } +apply from: '../spotless.gradle' + group = "org.bdware.sc" version = "1.9.0" tasks.withType(JavaCompile) { diff --git a/src/main/data-mask/maskingJobs/MaskingJob.java b/src/main/data-mask/maskingJobs/MaskingJob.java index cc8856d..cb305b9 100644 --- a/src/main/data-mask/maskingJobs/MaskingJob.java +++ b/src/main/data-mask/maskingJobs/MaskingJob.java @@ -16,8 +16,6 @@ import com.alibaba.datax.plugin.rdbms.util.DBUtil; import com.alibaba.datax.plugin.rdbms.util.DBUtilErrorCode; import com.alibaba.datax.plugin.rdbms.util.DataBaseType; import com.alibaba.datax.plugin.rdbms.util.RdbmsException; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; import com.google.gson.JsonPrimitive; import org.apache.commons.lang3.Validate; import org.bdware.sc.util.JsonUtil; @@ -47,8 +45,8 @@ public class MaskingJob { public void init(String confContent) { maskConf = Configuration.from(confContent); - System.out.println("maskConf"+maskConf.toString()); - System.out.println(("maskCOnfjob"+maskConf.get("job").toString())); + System.out.println("maskConf" + maskConf.toString()); + System.out.println(("maskCOnfjob" + maskConf.get("job").toString())); readerPara = maskConf.getConfiguration(CoreConstant.DATAX_JOB_CONTENT_READER_PARAMETER); System.out.println(readerPara); username = readerPara.getString(Key.USERNAME); @@ -56,16 +54,19 @@ public class MaskingJob { jdbcUrl = readerPara.getString(Key.JDBC_URL); table = readerPara.getString(Key.TABLE); buffer = new ArrayList<>(); - System.out.println("maskConf11"+maskConf.getConfiguration(CoreConstant.DATAX_JOB_CONTENT + "[0]")); - transformerExecs = TransformerUtil.buildTransformerInfo(maskConf.getConfiguration(CoreConstant.DATAX_JOB_CONTENT + "[0]")); + System.out.println( + "maskConf11" + maskConf.getConfiguration(CoreConstant.DATAX_JOB_CONTENT + "[0]")); + transformerExecs = TransformerUtil.buildTransformerInfo( + maskConf.getConfiguration(CoreConstant.DATAX_JOB_CONTENT + "[0]")); } public String buildQuerySql() { String column = "*"; - //String column = readerPara.getString(Key.COLUMN); + // String column = readerPara.getString(Key.COLUMN); String table = readerPara.getString(Key.TABLE); String where = readerPara.getString(Key.WHERE, null); - //String querySql = SingleTableSplitUtil.buildQuerySql(column, table, where) + " limit 100"; + // String querySql = SingleTableSplitUtil.buildQuerySql(column, table, where) + " limit + // 100"; String querySql = SingleTableSplitUtil.buildQuerySql(column, table, where) + " limit 100"; return querySql; @@ -74,24 +75,24 @@ public class MaskingJob { public JsonPrimitive getMaskedData(String confContent) { init(confContent); return startRead(); - //return new JsonPrimitive(getResult()); + // return new JsonPrimitive(getResult()); } public JsonPrimitive startRead() { String querySql = buildQuerySql(); - System.out.println("startRead"+dataBaseType+jdbcUrl+username+password); + System.out.println("startRead" + dataBaseType + jdbcUrl + username + password); Connection conn = DBUtil.getConnection(dataBaseType, jdbcUrl, username, password); - System.out.println(dataBaseType+jdbcUrl+username+password); + System.out.println(dataBaseType + jdbcUrl + username + password); int columnNumber = 0; - String res=""; - ArrayListcolumnName=new ArrayList<>(); + String res = ""; + ArrayList columnName = new ArrayList<>(); try { ResultSet rs = DBUtil.query(conn, querySql); ResultSetMetaData metaData = rs.getMetaData(); columnNumber = metaData.getColumnCount(); - for(int i=1;i<=metaData.getColumnCount();i++){ - //获取列表 index 从1开始、列名、列类型、列的数据长度 - //System.out.println("aaa"+metaData.getColumnName(i)+"\t"+metaData.getColumnTypeName(i)+"\t"+metaData.getColumnDisplaySize(i)); + for (int i = 1; i <= metaData.getColumnCount(); i++) { + // 获取列表 index 从1开始、列名、列类型、列的数据长度 + // System.out.println("aaa"+metaData.getColumnName(i)+"\t"+metaData.getColumnTypeName(i)+"\t"+metaData.getColumnDisplaySize(i)); columnName.add(metaData.getColumnName(i)); } @@ -106,15 +107,14 @@ public class MaskingJob { } finally { DBUtil.closeDBResources(null, conn); } - ////for(int i=0;i= record.getColumnNumber()) { - throw DataXException.asDataXException(TransformerErrorCode.TRANSFORMER_ILLEGAL_PARAMETER, + if (transformerInfoExec.getColumnIndex() != null + && transformerInfoExec.getColumnIndex() >= record.getColumnNumber()) { + throw DataXException.asDataXException( + TransformerErrorCode.TRANSFORMER_ILLEGAL_PARAMETER, String.format("columnIndex[%s] out of bound[%s]. name=%s", transformerInfoExec.getColumnIndex(), record.getColumnNumber(), transformerInfoExec.getTransformerName())); @@ -167,14 +169,15 @@ public class MaskingJob { } try { - result = transformerInfoExec.getTransformer().evaluate(result, transformerInfoExec.gettContext(), transformerInfoExec.getFinalParas()); + result = transformerInfoExec.getTransformer().evaluate(result, + transformerInfoExec.gettContext(), transformerInfoExec.getFinalParas()); } catch (Exception e) { - errorMsg = String.format("transformer(%s) has Exception(%s)", transformerInfoExec.getTransformerName(), - e.getMessage()); + errorMsg = String.format("transformer(%s) has Exception(%s)", + transformerInfoExec.getTransformerName(), e.getMessage()); failed = true; - //LOG.error(errorMsg, e); + // LOG.error(errorMsg, e); // transformerInfoExec.addFailedRecords(1); - //脏数据不再进行后续transformer处理,按脏数据处理,并过滤该record。 + // 脏数据不再进行后续transformer处理,按脏数据处理,并过滤该record。 break; } finally { @@ -193,14 +196,13 @@ public class MaskingJob { if (failed) { return null; } else { - System.out.println("result"+result); + System.out.println("result" + result); return result; } } - protected Record buildRecord(ResultSet rs, ResultSetMetaData metaData, - int columnNumber) { + protected Record buildRecord(ResultSet rs, ResultSetMetaData metaData, int columnNumber) { final byte[] EMPTY_CHAR_ARRAY = new byte[0]; Record record = new DefaultRecord(); try { @@ -276,14 +278,11 @@ public class MaskingJob { break; default: - throw DataXException - .asDataXException( - DBUtilErrorCode.UNSUPPORTED_TYPE, - String.format( - "您的配置文件中的列配置信息有误. 因为DataX 不支持数据库读取这种字段类型. 字段名:[%s], 字段名称:[%s], 字段Java类型:[%s]. 请尝试使用数据库函数将其转换datax支持的类型 或者不同步该字段 .", - metaData.getColumnName(i), - metaData.getColumnType(i), - metaData.getColumnClassName(i))); + throw DataXException.asDataXException(DBUtilErrorCode.UNSUPPORTED_TYPE, + String.format( + "您的配置文件中的列配置信息有误. 因为DataX 不支持数据库读取这种字段类型. 字段名:[%s], 字段名称:[%s], 字段Java类型:[%s]. 请尝试使用数据库函数将其转换datax支持的类型 或者不同步该字段 .", + metaData.getColumnName(i), metaData.getColumnType(i), + metaData.getColumnClassName(i))); } } } catch (Exception e) { @@ -294,33 +293,33 @@ public class MaskingJob { return record; } -// private String recordToString(Record record) { -// final String NEWLINE_FLAG = "\n"; -// String fieldDelimiter = "\t"; -// -// int recordLength = record.getColumnNumber(); -// if (0 == recordLength) { -// return NEWLINE_FLAG; -// } -// -// Column column; -// StringBuilder sb = new StringBuilder(); -// for (int i = 0; i < recordLength; i++) { -// column = record.getColumn(i); -// sb.append(column.asString()).append(fieldDelimiter); -// } -// sb.setLength(sb.length() - 1); -// sb.append(NEWLINE_FLAG); -// -// return sb.toString(); -// } + // private String recordToString(Record record) { + // final String NEWLINE_FLAG = "\n"; + // String fieldDelimiter = "\t"; + // + // int recordLength = record.getColumnNumber(); + // if (0 == recordLength) { + // return NEWLINE_FLAG; + // } + // + // Column column; + // StringBuilder sb = new StringBuilder(); + // for (int i = 0; i < recordLength; i++) { + // column = record.getColumn(i); + // sb.append(column.asString()).append(fieldDelimiter); + // } + // sb.setLength(sb.length() - 1); + // sb.append(NEWLINE_FLAG); + // + // return sb.toString(); + // } public String getResult(ArrayList columnName) { List dataList = new ArrayList<>(); int size = buffer.size(); - //System.out.println("CCULUMN"+readerPara.getString(Key.COLUMN).toString()); - //String[] colmnNames = readerPara.getString(Key.COLUMN).replace(" ", "").split(","); - int colmnSize= columnName.size(); + // System.out.println("CCULUMN"+readerPara.getString(Key.COLUMN).toString()); + // String[] colmnNames = readerPara.getString(Key.COLUMN).replace(" ", "").split(","); + int colmnSize = columnName.size(); for (int i = 0; i < colmnSize; ++i) { Map rowData = new HashMap<>(); for (int j = 0; j < size; ++j) { diff --git a/src/main/debugger/org/bdware/sc/debugger/DebugMain.java b/src/main/debugger/org/bdware/sc/debugger/DebugMain.java index 67fb513..8691dd4 100644 --- a/src/main/debugger/org/bdware/sc/debugger/DebugMain.java +++ b/src/main/debugger/org/bdware/sc/debugger/DebugMain.java @@ -26,41 +26,44 @@ public class DebugMain { DebugConfig config = JsonUtil.fromJson(content, DebugConfig.class); inject(config); String keyPairStr = "{\"publicKey\":\"%s\",\"privateKey\":\"%s\"}"; - SM2KeyPair pair = SM2KeyPair.fromJson(String.format(keyPairStr, config.publicKey, config.privateKey)); + SM2KeyPair pair = + SM2KeyPair.fromJson(String.format(keyPairStr, config.publicKey, config.privateKey)); String uriFormat = "ws://%s/SCIDE/SCExecutor"; if (config.killBeforeStart != null && config.killBeforeStart.length() > 0) { AtomicInteger counter = new AtomicInteger(0); - SmartContractClient client = new SmartContractClient(String.format(uriFormat, config.agentAddress), pair) { - public void onLogin(JsonObject obj) { - counter.incrementAndGet(); - } + SmartContractClient client = + new SmartContractClient(String.format(uriFormat, config.agentAddress), pair) { + public void onLogin(JsonObject obj) { + counter.incrementAndGet(); + } - public void onKillContractProcess(JsonObject obj) { - counter.incrementAndGet(); - } + public void onKillContractProcess(JsonObject obj) { + counter.incrementAndGet(); + } - }; + }; client.waitForConnect(); client.login(); try { - for (; counter.get() == 0; ) + for (; counter.get() == 0;) Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } - client.sendMsg("{\"action\":\"killContractProcess\",\"name\":\"" + config.killBeforeStart + "\"}"); + client.sendMsg("{\"action\":\"killContractProcess\",\"name\":\"" + + config.killBeforeStart + "\"}"); try { - for (; counter.get() == 1; ) + for (; counter.get() == 1;) Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } config.contract.setCreateParam(config.createParam); - ContractProcess.main(new String[]{"-port=" + config.port, "-cmi=" + config.cmi, "-disablePID"}); - ResultCallback printCallback - = new ResultCallback() { + ContractProcess + .main(new String[] {"-port=" + config.port, "-cmi=" + config.cmi, "-disablePID"}); + ResultCallback printCallback = new ResultCallback() { @Override public void onResult(String str) { if (str.contains("Error")) { @@ -70,16 +73,18 @@ public class DebugMain { } }; ContractProcess.instance.handler.setDBInfo(wrap("", config.dbPath), printCallback); - ContractProcess.instance.handler.registerMangerPort(wrap("", Integer.valueOf(config.cPort)), printCallback); + ContractProcess.instance.handler.registerMangerPort(wrap("", Integer.valueOf(config.cPort)), + printCallback); - ContractProcess.instance.handler.setContractBundle(wrap("", config.contract), printCallback); + ContractProcess.instance.handler.setContractBundle(wrap("", config.contract), + printCallback); String urlFormat = "http://%s/SCIDE/SCManager?action=reconnectPort&owner=%s&port=%d%s"; String cpHost = ""; if (config.cpHost != null && config.cpHost.length() > 0) cpHost = "&host=" + config.cpHost; - String url = String.format(urlFormat, config.agentAddress, - config.publicKey, ContractProcess.instance.server.mainPort.get(), cpHost); + String url = String.format(urlFormat, config.agentAddress, config.publicKey, + ContractProcess.instance.server.mainPort.get(), cpHost); Map resp = HttpUtil.httpGet(url); String data = (String) resp.get("response"); @@ -103,7 +108,8 @@ public class DebugMain { config.cPort = jsonObject.get("cPort").getAsInt(); config.port = jsonObject.get("port").getAsInt(); JsonObject ownerAndScript = new JsonObject(); - String arg = "abc&owner=" + config.publicKey + "&script=" + config.ypkPath + "&doipStartPort=" + config.doipStartPort; + String arg = "abc&owner=" + config.publicKey + "&script=" + config.ypkPath + + "&doipStartPort=" + config.doipStartPort; url = String.format(urlFormat, config.agentAddress, "allocateKeyPair", arg); resp = HttpUtil.httpGet(url); LOGGER.info(url); @@ -134,7 +140,7 @@ public class DebugMain { String ypkPath; String cpHost; int doipStartPort; - //AutoAppend + // AutoAppend int port; String cmi; String dbPath; diff --git a/src/main/java/org/bdware/sc/ContractProcess.java b/src/main/java/org/bdware/sc/ContractProcess.java index a845b8f..7cdd194 100644 --- a/src/main/java/org/bdware/sc/ContractProcess.java +++ b/src/main/java/org/bdware/sc/ContractProcess.java @@ -54,8 +54,8 @@ import java.util.*; import java.util.zip.ZipFile; public class ContractProcess { - private static final byte[] ZIP_HEADER_1 = new byte[]{80, 75, 3, 4}; - private static final byte[] ZIP_HEADER_2 = new byte[]{80, 75, 5, 6}; + private static final byte[] ZIP_HEADER_1 = new byte[] {80, 75, 3, 4}; + private static final byte[] ZIP_HEADER_2 = new byte[] {80, 75, 5, 6}; private static final org.apache.logging.log4j.Logger LOGGER = org.apache.logging.log4j.LogManager.getLogger(ContractProcess.class); public static ContractProcess instance; @@ -109,7 +109,7 @@ public class ContractProcess { } } Scanner sc = new Scanner(pidInput); - for (String str; sc.hasNextLine(); ) { + for (String str; sc.hasNextLine();) { str = sc.nextLine(); LOGGER.info("[CP From STDIN] " + str); if (str.contains("CP PID:")) { @@ -137,9 +137,8 @@ public class ContractProcess { byte[] buffer = new byte[4]; int length = input.read(buffer, 0, 4); if (length == 4) { - isArchive = - (Arrays.equals(ZIP_HEADER_1, buffer)) - || (Arrays.equals(ZIP_HEADER_2, buffer)); + isArchive = (Arrays.equals(ZIP_HEADER_1, buffer)) + || (Arrays.equals(ZIP_HEADER_2, buffer)); } } catch (IOException e) { e.printStackTrace(); @@ -194,9 +193,8 @@ public class ContractProcess { cn = czb.mergeContractNode(); } else { - cn = - compiler.compile( - new ByteArrayInputStream(script.getBytes()), "contract_main.yjs"); + cn = compiler.compile(new ByteArrayInputStream(script.getBytes()), + "contract_main.yjs"); } DesktopEngine engine = new DesktopEngine(); // engine.loadJar(zf); engine.loadContract(c, cn, ret.isInsnLimit); @@ -216,8 +214,8 @@ public class ContractProcess { MethodNode mn = methods.get(fn.functionName); if (mn != null) { - System.out.println( - "[ContractManager] getMethodNode, verify:" + fn.functionName); + System.out + .println("[ContractManager] getMethodNode, verify:" + fn.functionName); TaintResult.nLocals = mn.maxLocals; TaintResult.nStack = mn.maxStack; TaintCFG cfg = new TaintCFG(mn); @@ -275,22 +273,17 @@ public class ContractProcess { MethodNode mn = methods.get(fn.functionName); if (mn != null) { /* - * CFGraph cfg = new CFGraph(mn) { - * - * @Override public BasicBlock getBasicBlock(int id) { return new - * BasicBlock(id); } }; FrontCF frontCF = new FrontCF(graph); String[] - data = - * fn.plainText().split("\n"); for (int i = 0; i < - graph.getBasicBlockSize(); - * i++) { BasicBlock bb = graph.getBasicBlockAt(i); String decompiled = - ""; if - * (bb.lineNum - 1 < data.length && bb.lineNum > 0) { decompiled = - * data[bb.lineNum - 1]; } frontCF.addBB(bb, decompiled); Set - suc = - * graph.getSucBlocks(bb); for (BasicBlock sucBB : suc) - frontCF.addEdge(bb, - * sucBB); } - */ + * CFGraph cfg = new CFGraph(mn) { + * + * @Override public BasicBlock getBasicBlock(int id) { return new + * BasicBlock(id); } }; FrontCF frontCF = new FrontCF(graph); String[] data = + * fn.plainText().split("\n"); for (int i = 0; i < graph.getBasicBlockSize(); + * i++) { BasicBlock bb = graph.getBasicBlockAt(i); String decompiled = ""; if + * (bb.lineNum - 1 < data.length && bb.lineNum > 0) { decompiled = + * data[bb.lineNum - 1]; } frontCF.addBB(bb, decompiled); Set suc = + * graph.getSucBlocks(bb); for (BasicBlock sucBB : suc) frontCF.addEdge(bb, + * sucBB); } + */ TaintResult.nLocals = mn.maxLocals; TaintResult.nStack = mn.maxStack; TaintCFG cfg = new TaintCFG(mn); @@ -311,55 +304,56 @@ public class ContractProcess { List ids = map.get(i); frontCF.addBB(bb, decompiled, ids, cfg); Set suc = cfg.getSucBlocks(bb); - for (BasicBlock sucBB : suc) frontCF.addEdge(bb, sucBB); + for (BasicBlock sucBB : suc) + frontCF.addEdge(bb, sucBB); } // get result // TaintBB lastBlock = cfg.getLastBlock(); - // if (lastBlock != null) { - // frontCF.ret = lastBlock.getResultWithTaintBit(); - // // System.out.println(frontCF.ret); - // if (parameters != null && parameters != "") { - // // System.out.println(parameters); - // // frontCF.finalRet = "yes"; - // Gson gson = JsonUtil; - // JsonParser jsonParser = new JsonParser(); - // JsonArray jsonArray = + // if (lastBlock != null) { + // frontCF.ret = lastBlock.getResultWithTaintBit(); + // // System.out.println(frontCF.ret); + // if (parameters != null && parameters != "") { + // // System.out.println(parameters); + // // frontCF.finalRet = "yes"; + // Gson gson = JsonUtil; + // JsonParser jsonParser = new JsonParser(); + // JsonArray jsonArray = // jsonParser.parse(parameters).getAsJsonArray(); - // List listConstraint = new ArrayList<>(); - // List listResource = new ArrayList<>(); - // for (JsonElement je : jsonArray) { - // Bean bean = gson.fromJson(je, Bean.class); - // switch (bean.name) { - // case "open": - // listConstraint.add("open"); - // break; - // case "byValue": - // listConstraint.add("byValue"); - // break; - // case "control": - // listConstraint.add("control"); - // break; - // case "close": - // listConstraint.add("close"); - // break; - // case "originalData": - // listResource.add("originalData"); - // break; - // case "contractCall": - // listResource.add("contractCall"); - // break; - // } - // } - //// String dep = + // List listConstraint = new ArrayList<>(); + // List listResource = new ArrayList<>(); + // for (JsonElement je : jsonArray) { + // Bean bean = gson.fromJson(je, Bean.class); + // switch (bean.name) { + // case "open": + // listConstraint.add("open"); + // break; + // case "byValue": + // listConstraint.add("byValue"); + // break; + // case "control": + // listConstraint.add("control"); + // break; + // case "close": + // listConstraint.add("close"); + // break; + // case "originalData": + // listResource.add("originalData"); + // break; + // case "contractCall": + // listResource.add("contractCall"); + // break; + // } + // } + //// String dep = // frontCF.blocks.get(frontCF.blocks.size() - 1).blockDep; - //// if ((listConstraint.contains("open") - //// || + //// if ((listConstraint.contains("open") + //// || // listConstraint.contains("close")) - //// && (frontCF.ret != null || dep != null)) - //// frontCF.finalRet = "不通过"; - //// else frontCF.finalRet = "通过"; - // } - // } + //// && (frontCF.ret != null || dep != null)) + //// frontCF.finalRet = "不通过"; + //// else frontCF.finalRet = "通过"; + // } + // } result.put(fn.functionName, frontCF); } } @@ -404,10 +398,8 @@ public class ContractProcess { String yancloud_desktop = ""; isOpen.put(pmList[0], pmList[1]); yancloud_desktop += UtilRegistry.getInitStr(pmList[0], pmList[1].equals("open")); - engine.getNashornEngine() - .getContext() - .setAttribute( - ScriptEngine.FILENAME, yancloud_desktop, ScriptContext.ENGINE_SCOPE); + engine.getNashornEngine().getContext().setAttribute(ScriptEngine.FILENAME, + yancloud_desktop, ScriptContext.ENGINE_SCOPE); engine.getNashornEngine().eval(yancloud_desktop); } catch (ScriptException e) { e.printStackTrace(); @@ -429,25 +421,20 @@ public class ContractProcess { // 判断是否满足Oracle和Contact的执行要求 public String verifyOracleAndContractPermission(Contract contract) { // 权限校验 如果是Oracle 启动方式只能是Sole 否则报错 - if (cn.getYjsType() == YjsType.Oracle && contract.getType() != ContractExecType.Sole && contract.getType() != ContractExecType.Sharding) { + if (cn.getYjsType() == YjsType.Oracle && contract.getType() != ContractExecType.Sole + && contract.getType() != ContractExecType.Sharding) { LOGGER.info("Oracle only support Sole ContractType!"); - return JsonUtil.toJson( - new ContractResult( - Status.Error, - new JsonPrimitive("Oracle only support Sole ContractType!"))); + return JsonUtil.toJson(new ContractResult(Status.Error, + new JsonPrimitive("Oracle only support Sole ContractType!"))); } // 权限校验 如果是contract 申请了MySQL等权限 报错 if (cn.getYjsType() == YjsType.Contract) { for (Permission per : cn.getPermission()) { - if (per == Permission.SQL - || per == Permission.Http - || per == Permission.RocksDB + if (per == Permission.SQL || per == Permission.Http || per == Permission.RocksDB || per == Permission.MongoDB) { LOGGER.debug("Contract can not have permissions of IO!"); - return JsonUtil.toJson( - new ContractResult( - Status.Error, - new JsonPrimitive("Contract can not have permissions of IO|"))); + return JsonUtil.toJson(new ContractResult(Status.Error, + new JsonPrimitive("Contract can not have permissions of IO|"))); } } } @@ -458,23 +445,23 @@ public class ContractProcess { JavaScriptEntry.members = members; if (members != null) return members.size() + ""; - else return "0"; + else + return "0"; } public String setContractBundle(Contract contract) { try { - // long start = System.currentTimeMillis(); - // long start0 = start; + // long start = System.currentTimeMillis(); + // long start0 = start; this.contract = contract; JavaScriptEntry.random = new Random(); JavaScriptEntry.invokeID = 0L; JavaScriptEntry.random.setSeed(Integer.parseInt(contract.getID())); JavaScriptEntry.numOfCopies = this.contract.getNumOfCopies(); - JavaScriptEntry.shardingID = - this.contract.getShardingId(); // 设置javaScriptEntry中的shardingID + JavaScriptEntry.shardingID = this.contract.getShardingId(); // 设置javaScriptEntry中的shardingID // JavaScriptEntry // contract.getCreateParam().get("repoId").getAsString()-->去获取repoId。 - //知道自己的BDOID --> repo的id。+contract.getID() + // 知道自己的BDOID --> repo的id。+contract.getID() String zipPath = contract.getScriptStr(); if (isArchiveFile(new File(zipPath))) { ZipFile zf = new ZipFile(zipPath); @@ -487,17 +474,14 @@ public class ContractProcess { memorySet = cn.memorySet; this.contract.sourcePath = zipBundle.getManifest().sourcePath; - LOGGER.debug( - "check sourcePath\n\tin-contract=" - + this.contract.sourcePath - + "\n\tin-manifest=" - + zipBundle.getManifest().sourcePath); + LOGGER.debug("check sourcePath\n\tin-contract=" + this.contract.sourcePath + + "\n\tin-manifest=" + zipBundle.getManifest().sourcePath); // zhanghongwei - /* if (ret.getManifest().getInsnLimit() != 0) { - gasLimit=ret.getManifest().getInsnLimit(); - isInsnLimit = true; - }*/ + /* + * if (ret.getManifest().getInsnLimit() != 0) { + * gasLimit=ret.getManifest().getInsnLimit(); isInsnLimit = true; } + */ String ver = verifyOracleAndContractPermission(contract); if (!ver.isEmpty()) { return ver; @@ -533,15 +517,13 @@ public class ContractProcess { } } catch (MalformedJsonException | JsonSyntaxException e) { - return JsonUtil.toJson( - new ContractResult( - Status.Error, - new JsonPrimitive("parse manifest.json error, not json format!"))); + return JsonUtil.toJson(new ContractResult(Status.Error, + new JsonPrimitive("parse manifest.json error, not json format!"))); } catch (Exception e) { ByteArrayOutputStream bo = new ByteArrayOutputStream(); e.printStackTrace(new PrintStream(bo)); - return JsonUtil.toJson( - new ContractResult(Status.Error, new JsonPrimitive(bo.toString()))); + return JsonUtil + .toJson(new ContractResult(Status.Error, new JsonPrimitive(bo.toString()))); } } @@ -572,13 +554,14 @@ public class ContractProcess { if (fun.isExport()) { - //if(fun.annotations...) + // if(fun.annotations...) AccessHandler accessHandler = createHandlerIfExist(fun, AccessHandler.class); if (accessHandler != null) { fun.appendBeforeInvokeHandler(accessHandler); } fun.appendAfterInvokeHandler(new ObjToJsonHandler()); - ResultSchemaHandler resultSchemaHandler = createHandlerIfExist(fun, ResultSchemaHandler.class); + ResultSchemaHandler resultSchemaHandler = + createHandlerIfExist(fun, ResultSchemaHandler.class); if (resultSchemaHandler != null) { fun.appendAfterInvokeHandler(resultSchemaHandler); } @@ -587,10 +570,10 @@ public class ContractProcess { // if (fun.isMask()) { // String maskConfig = - // engine.getResources().loadAsString("/maskConfig.json"); + // engine.getResources().loadAsString("/maskConfig.json"); // System.out.println("injectMask"+maskConfig); // System.out.println("injectMask"+this.contract.Mask); - // fun.appendAfterInvokeHandler(new MaskHandler()); + // fun.appendAfterInvokeHandler(new MaskHandler()); // } } if (fun.isHomomorphicEncrypt()) { @@ -605,8 +588,10 @@ public class ContractProcess { if (doopRequestHandler == null) { doopRequestHandler = new DOOPRequestHandler(); } - fun.appendBeforeInvokeHandler(new DOOPBeforeExecHandler(fun.getDoipOperationInfo().operation)); - fun.appendAfterInvokeHandler(new DOOPAfterExecHandler(fun.getDoipOperationInfo().operation)); + fun.appendBeforeInvokeHandler( + new DOOPBeforeExecHandler(fun.getDoipOperationInfo().operation)); + fun.appendAfterInvokeHandler( + new DOOPAfterExecHandler(fun.getDoipOperationInfo().operation)); doopRequestHandler.addDoipOperation(fun); } } @@ -614,11 +599,14 @@ public class ContractProcess { T createHandlerIfExist(FunctionNode function, Class clz) { YJSAnnotation annotation = clz.getAnnotation(YJSAnnotation.class); - if (annotation == null) return null; + if (annotation == null) + return null; try { AnnotationNode node = function.getAnnotation(annotation.name()); - if (node == null) return null; - Method m = clz.getDeclaredMethod("fromAnnotationNode", FunctionNode.class, AnnotationNode.class); + if (node == null) + return null; + Method m = clz.getDeclaredMethod("fromAnnotationNode", FunctionNode.class, + AnnotationNode.class); T result = (T) m.invoke(null, function, node); return result; } catch (Exception e) { @@ -660,8 +648,7 @@ public class ContractProcess { handleLog(); LOGGER.info("load script, contract:" + JsonUtil.toJson(contract.getScriptStr())); LOGGER.info("load cn:" + JsonUtil.toJson(cn)); - ContractResult ret = - engine.loadContract(contract, cn, cn.getInstrumentBranch()); + ContractResult ret = engine.loadContract(contract, cn, cn.getInstrumentBranch()); ContractResult onCreate = invokeOnCreate(contract.getCreateParam()); JsonObject jo = new JsonObject(); jo.add("onCreate", JsonUtil.parseObject(onCreate)); @@ -679,14 +666,14 @@ public class ContractProcess { } catch (Exception e) { ByteArrayOutputStream bo = new ByteArrayOutputStream(); e.printStackTrace(new PrintStream(bo)); - return JsonUtil.toJson( - new ContractResult(Status.Error, new JsonPrimitive(bo.toString()))); + return JsonUtil + .toJson(new ContractResult(Status.Error, new JsonPrimitive(bo.toString()))); } } public void updateRepoInfo(JsonElement arg) throws Exception { // 只有0号节点需要初始化IRP连接去updateRepoInfo - //just disable this part + // just disable this part if (JavaScriptEntry.shardingID == -1) { // DOOP relevant logic DoipClusterServer server = DoipClusterServer.getDOOPServerInstance(); @@ -695,9 +682,11 @@ public class ContractProcess { if (createParams.has("router")) { JsonElement routerInfo = createParams.get("router"); if (!routerInfo.isJsonObject()) - throw new Exception("Provide wrong router info in create params to DoipModule"); + throw new Exception( + "Provide wrong router info in create params to DoipModule"); else { - EndpointConfig endpointConfig = JsonUtil.GSON.fromJson(routerInfo.getAsJsonObject(), EndpointConfig.class); + EndpointConfig endpointConfig = JsonUtil.GSON + .fromJson(routerInfo.getAsJsonObject(), EndpointConfig.class); DoipClusterServer.createDOOPServerInstance(endpointConfig); } } else { @@ -712,17 +701,20 @@ public class ContractProcess { } } - public void invokeOnStartingDoipServer(ContractNode cn, JsonElement arg, JsonObject returnValue) { + public void invokeOnStartingDoipServer(ContractNode cn, JsonElement arg, + JsonObject returnValue) { ContractRequest onStartingDoipServer = new ContractRequest(); onStartingDoipServer.setAction("onServerStart"); if (arg == null) { - if (engine != null && engine.getManifest() != null && engine.getManifest().createParam != null) + if (engine != null && engine.getManifest() != null + && engine.getManifest().createParam != null) arg = engine.getManifest().createParam; else arg = new JsonPrimitive(""); } onStartingDoipServer.setArg(arg); - LOGGER.debug("invoke onStartingDoipServer, param:" + onStartingDoipServer.getArg().toString()); + LOGGER.debug( + "invoke onStartingDoipServer, param:" + onStartingDoipServer.getArg().toString()); onStartingDoipServer.setRequester(contract.getOwner()); FunctionNode funNode = cn.getFunction("onServerStart"); @@ -733,13 +725,16 @@ public class ContractProcess { if (arg.isJsonObject() && arg.getAsJsonObject().has("doipStartPort")) { startPort = arg.getAsJsonObject().get("doipStartPort").getAsInt(); } - LOGGER.info("Fetch the onStartingDoipServerRes from router successfully, the result is " + onStartingDoipServerRes); - int doipListenPort = DoipClusterServer.startDoipServer(startPort, onStartingDoipServerRes); + LOGGER.info("Fetch the onStartingDoipServerRes from router successfully, the result is " + + onStartingDoipServerRes); + int doipListenPort = + DoipClusterServer.startDoipServer(startPort, onStartingDoipServerRes); returnValue.addProperty("doipListenPort", doipListenPort); this.contract.setDoipPort(doipListenPort); returnValue.addProperty("doipStartPort", startPort); } catch (Exception e) { - LOGGER.error("DoipLocalSingleton cannot starts properly, plz check the onServerStart function"); + LOGGER.error( + "DoipLocalSingleton cannot starts properly, plz check the onServerStart function"); e.printStackTrace(); } funNode = cn.getFunction("onInitSharableVars"); @@ -774,7 +769,8 @@ public class ContractProcess { } } - if (fun.getLogToBDContract()) detail.append("bdcontract;"); + if (fun.getLogToBDContract()) + detail.append("bdcontract;"); if (fun.getLogToNamedLedger()) { for (String str : fun.getLedgerNames()) { detail.append("bdledger:").append(str).append(";"); @@ -820,7 +816,8 @@ public class ContractProcess { ContractRequest onCreate = new ContractRequest(); onCreate.setAction("onCreate"); if (arg == null) { - if (engine != null && engine.getManifest() != null && engine.getManifest().createParam != null) + if (engine != null && engine.getManifest() != null + && engine.getManifest().createParam != null) arg = engine.getManifest().createParam; else arg = new JsonPrimitive(""); @@ -893,8 +890,7 @@ public class ContractProcess { List hashes = logIndex.request(offset, size); List> jo = new ArrayList<>(); - TypeToken> token = new TypeToken>() { - }; + TypeToken> token = new TypeToken>() {}; for (Long hash : hashes) try { Map obj = @@ -909,8 +905,7 @@ public class ContractProcess { public String requestLast(int count) { List hashes = logIndex.requestLast(count); List> jo = new ArrayList<>(); - TypeToken> token = new TypeToken>() { - }; + TypeToken> token = new TypeToken>() {}; String log; for (Long hash : hashes) try { @@ -920,8 +915,8 @@ public class ContractProcess { } Map obj = JsonUtil.fromJson(log, token.getType()); if (obj == null) { - System.out.println( - "[ContractProcess] requestLast, parseJsonError:" + log + "=="); + System.out + .println("[ContractProcess] requestLast, parseJsonError:" + log + "=="); continue; } obj.put("hash", hash + ""); @@ -964,9 +959,8 @@ public class ContractProcess { try { request = JsonUtil.fromJson(arg, ContractRequest.class); } catch (Exception ignored) { - result = - new ContractResult( - ContractResult.Status.Error, new JsonPrimitive("Illegal Arguments!")); + result = new ContractResult(ContractResult.Status.Error, + new JsonPrimitive("Illegal Arguments!")); return JsonUtil.toJson(result); } String reqID = request.getRequestID(); @@ -987,9 +981,9 @@ public class ContractProcess { engine.redirectTracePS(new Logger(bo, this)); result = engine.executeContract(request); result.analysis = bo.toString(); - System.out.println( - "[ContractProcess] result.analysis = " - + result.analysis); // 动态分析bug null pointer + System.out.println("[ContractProcess] result.analysis = " + result.analysis); // 动态分析bug + // null + // pointer // branchResult = JsonUtil.toJson(result); // branchTrace = result.analysis; engine.redirectTracePS(previous); @@ -1008,16 +1002,8 @@ public class ContractProcess { evaluatesAnalysis(request.getAction()); } - engine.redirectTracePS( - new ProgramPointCounter( - bo, - this, - gasLimit, - functionIndex, - request.getValue(), - 0L, - request.getAction(), - ppCountMap)); + engine.redirectTracePS(new ProgramPointCounter(bo, this, gasLimit, functionIndex, + request.getValue(), 0L, request.getAction(), ppCountMap)); result = engine.executeContract(request); result.analysis = bo.toString(); @@ -1058,13 +1044,12 @@ public class ContractProcess { for (String s : function) { MethodNode mn = methods.get(s); if (mn != null) { - CFGraph cfg = - new CFGraph(mn) { - @Override - public BasicBlock getBasicBlock(int id) { - return new BasicBlock(id); - } - }; + CFGraph cfg = new CFGraph(mn) { + @Override + public BasicBlock getBasicBlock(int id) { + return new BasicBlock(id); + } + }; // cfg.printSelf(); CFGmap.put(s, cfg); PPCount countFee = new PPCount(cfg, flag); @@ -1126,11 +1111,8 @@ public class ContractProcess { TaintBB bb = cfg.getLastBlock(); result.analysis = bb.getResultWithTaintBit(); - System.out.println( - "[ContractProcess] dynamically verify: " - + ac.getAction() - + "-->" - + result.analysis); + System.out.println("[ContractProcess] dynamically verify: " + ac.getAction() + "-->" + + result.analysis); } } @@ -1152,8 +1134,7 @@ public class ContractProcess { return !JavaScriptEntry.topic_handlers.isEmpty(); } - public void beforeSuicide() { - } + public void beforeSuicide() {} public String redo(String path) { return engine.syncUtil.redo(path); @@ -1192,7 +1173,7 @@ public class ContractProcess { public void writeContractDB(Map data) { String path = dbPath; if (path == null) { - // return "nopath"; + // return "nopath"; return; } try { @@ -1202,14 +1183,13 @@ public class ContractProcess { edion.put(String.valueOf(hash), result); } catch (Exception e) { e.printStackTrace(); - // return "failed"; + // return "failed"; } - // return "success"; + // return "success"; } @Override - protected void finalize() { - } + protected void finalize() {} public String getDeclaredEvents() { return JsonUtil.toJson(cn.events); @@ -1224,8 +1204,8 @@ public class ContractProcess { for (FunctionNode fn : cn.getFunctions()) { if (fn.isExport() && !fn.functionName.equals("onCreate")) { function.add(fn.functionName); - FunctionDesp desp = - new FunctionDesp(fn.functionName, fn.annotations, fn.getRouteInfo(), fn.getJoinInfo(), fn.isView()); + FunctionDesp desp = new FunctionDesp(fn.functionName, fn.annotations, + fn.getRouteInfo(), fn.getJoinInfo(), fn.isView()); ret.add(desp); } } @@ -1246,11 +1226,8 @@ public class ContractProcess { public String startAutoDump() { String dumpPeriod = projectConfig.getDumpPeriod(); - System.out.println( - "[ContractProcess] startAutoDump : " - + cn.getContractName() - + " period = " - + dumpPeriod); + System.out.println("[ContractProcess] startAutoDump : " + cn.getContractName() + + " period = " + dumpPeriod); String status = "startAutoDump status 0"; if (null != dt) { if (null == dumpPeriod || dumpPeriod.isEmpty()) { @@ -1355,21 +1332,21 @@ public class ContractProcess { if (period != null && period.length() > 0) { changeDumpPeriod(period); } - // System.out.println("ContractProcessMask"); - // JsonObject argsJS = JsonParser.parseString(args).getAsJsonObject(); - // String contractID = ""; - // String operation = ""; - // JsonElement mask = JsonParser.parseString(""); - // if (argsJS.has("contractID") && argsJS.has("operation") && argsJS.has("maskInfo")) + // System.out.println("ContractProcessMask"); + // JsonObject argsJS = JsonParser.parseString(args).getAsJsonObject(); + // String contractID = ""; + // String operation = ""; + // JsonElement mask = JsonParser.parseString(""); + // if (argsJS.has("contractID") && argsJS.has("operation") && argsJS.has("maskInfo")) // { - // contractID = argsJS.get("contractID").getAsString(); - // System.out.println(contractID); - // operation = argsJS.get("operation").getAsString(); - // System.out.println(operation); - // mask = argsJS.get("maskInfo"); - // System.out.println("mask" + mask); - // this.contract.setMask(operation, mask); - // } + // contractID = argsJS.get("contractID").getAsString(); + // System.out.println(contractID); + // operation = argsJS.get("operation").getAsString(); + // System.out.println(operation); + // mask = argsJS.get("maskInfo"); + // System.out.println("mask" + mask); + // this.contract.setMask(operation, mask); + // } } public String getDependentContracts() { @@ -1431,9 +1408,8 @@ public class ContractProcess { private class DumpTask extends TimerTask { @Override public void run() { - System.out.println( - "[ContractProcess DumpTask] auto dump period : " - + projectConfig.getDumpPeriod()); + System.out.println("[ContractProcess DumpTask] auto dump period : " + + projectConfig.getDumpPeriod()); File file1 = new File(dir); File file2 = new File(file1.getParent()); String dir2 = file2.getParent() + "/memory/"; diff --git a/src/main/java/org/bdware/sc/SM2Helper.java b/src/main/java/org/bdware/sc/SM2Helper.java index e9ec29f..407cfe1 100644 --- a/src/main/java/org/bdware/sc/SM2Helper.java +++ b/src/main/java/org/bdware/sc/SM2Helper.java @@ -31,11 +31,13 @@ public class SM2Helper { backupFile(new File("./cmconfig.json")); System.out.println("Generate file: cmconfig.json"); String content = FileUtil.getFileContent("./cmconfig.json.template"); - JsonObject jo = JsonParser.parseReader(new FileReader("cmvar.json")).getAsJsonObject(); + JsonObject jo = + JsonParser.parseReader(new FileReader("cmvar.json")).getAsJsonObject(); for (String key : jo.keySet()) { content = content.replaceAll(key, jo.get(key).getAsString()); } - JsonObject keypair = JsonParser.parseReader(new FileReader("manager.keypair")).getAsJsonObject(); + JsonObject keypair = + JsonParser.parseReader(new FileReader("manager.keypair")).getAsJsonObject(); content = content.replaceAll("_PRIVKEY", keypair.get("privateKey").getAsString()); content = content.replaceAll("_PUBKEY", keypair.get("publicKey").getAsString()); content = content.replaceAll("CMI", System.currentTimeMillis() + ""); @@ -58,15 +60,18 @@ public class SM2Helper { } private static void backupFile(File file) { - if (!file.exists()) return; + if (!file.exists()) + return; File backup = null; for (int i = 0; i <= 100; i++) { - if (i == 100) throw new IllegalArgumentException("failed to backup:" + file.getAbsolutePath()); + if (i == 100) + throw new IllegalArgumentException("failed to backup:" + file.getAbsolutePath()); backup = new File(file.getParent(), file.getName() + "." + i); if (!backup.exists()) break; } FileUtil.copyFile(file, backup); - System.out.println("Backup: " + file.getAbsolutePath() + " --> " + backup.getAbsolutePath()); + System.out + .println("Backup: " + file.getAbsolutePath() + " --> " + backup.getAbsolutePath()); } } diff --git a/src/main/java/org/bdware/sc/analysis/dynamic/FSAnalysis.java b/src/main/java/org/bdware/sc/analysis/dynamic/FSAnalysis.java index 294a35b..9e7db66 100644 --- a/src/main/java/org/bdware/sc/analysis/dynamic/FSAnalysis.java +++ b/src/main/java/org/bdware/sc/analysis/dynamic/FSAnalysis.java @@ -14,7 +14,6 @@ import org.objectweb.asm.tree.ClassNode; import org.objectweb.asm.tree.MethodNode; import java.io.ByteArrayInputStream; -import java.io.FileInputStream; import java.util.*; import java.util.zip.ZipFile; @@ -44,11 +43,8 @@ public class FSAnalysis extends BreadthFirstSearch { setToAnalysis(toAnalysis); if (isDebug) { System.out.println("===Method:" + cfg.getMethodNode().name + cfg.getMethodNode().desc); - System.out.println( - "===Local:" - + cfg.getMethodNode().maxLocals - + " " - + cfg.getMethodNode().maxStack); + System.out.println("===Local:" + cfg.getMethodNode().maxLocals + " " + + cfg.getMethodNode().maxStack); } } @@ -81,9 +77,8 @@ public class FSAnalysis extends BreadthFirstSearch { ContractZipBundle czb = compiler.compile(zf); cn = czb.mergeContractNode(); } else { - cn = - compiler.compile( - new ByteArrayInputStream(script.getBytes()), "contract_main.yjs"); + cn = compiler.compile(new ByteArrayInputStream(script.getBytes()), + "contract_main.yjs"); } DesktopEngine engine = new DesktopEngine(); // engine.loadJar(zf); engine.loadContract(c, cn, false); @@ -109,7 +104,8 @@ public class FSAnalysis extends BreadthFirstSearch { FSAnalysis analysis = new FSAnalysis(cfg); analysis.analysis(); TaintBB bb = cfg.getLastBlock(); - if (bb != null) result.put(fn.functionName, bb.getResult()); + if (bb != null) + result.put(fn.functionName, bb.getResult()); cfg.printSelf(); } } diff --git a/src/main/java/org/bdware/sc/boundry/AccountIndex.java b/src/main/java/org/bdware/sc/boundry/AccountIndex.java index c946e12..726e122 100644 --- a/src/main/java/org/bdware/sc/boundry/AccountIndex.java +++ b/src/main/java/org/bdware/sc/boundry/AccountIndex.java @@ -126,7 +126,8 @@ public class AccountIndex { } try { - LenVarTimeSerialIndex2 index = getIndexFile(getString(args, "account") + getString(args, "file")); + LenVarTimeSerialIndex2 index = + getIndexFile(getString(args, "account") + getString(args, "file")); long startTime = getLong(args, "startTime"); long endTime = getLong(args, "endTime"); List result = index.requestByTime(startTime, endTime); @@ -183,7 +184,8 @@ public class AccountIndex { } try { - LenVarTimeSerialIndex2 index = getIndexFile(getString(args, "account") + getString(args, "file")); + LenVarTimeSerialIndex2 index = + getIndexFile(getString(args, "account") + getString(args, "file")); long date = getLong(args, "date"); String content = getString(args, "content"); index.manullyIndex(date, HashUtil.str16ToBytes(content)); diff --git a/src/main/java/org/bdware/sc/boundry/JavaScriptEntry.java b/src/main/java/org/bdware/sc/boundry/JavaScriptEntry.java index 1b7184a..5c2f294 100644 --- a/src/main/java/org/bdware/sc/boundry/JavaScriptEntry.java +++ b/src/main/java/org/bdware/sc/boundry/JavaScriptEntry.java @@ -44,12 +44,12 @@ import java.util.concurrent.locks.ReentrantLock; import static org.bdware.sc.event.REvent.REventType.*; public class JavaScriptEntry { - // private static final HostnameVerifier DO_NOT_VERIFY = (hostname, session) -> true; + // private static final HostnameVerifier DO_NOT_VERIFY = (hostname, session) -> true; public static final Map topic_handlers = new HashMap<>(); private static final Logger LOGGER = LogManager.getLogger(JavaScriptEntry.class); public static NashornScriptEngine currentEngine; public static SyncMechUtil currentSyncUtil; - // public static int contractManagerPort; + // public static int contractManagerPort; public static Random random; public static long invokeID; public static String authInfoPersistDOI; @@ -78,9 +78,8 @@ public class JavaScriptEntry { } public static void setSM2KeyPair(String pubKey, String privKey) { - keyPair = - new SM2KeyPair( - SM2KeyPair.publicKeyStr2ECPoint(pubKey), new BigInteger(privKey, 16)); + keyPair = new SM2KeyPair(SM2KeyPair.publicKeyStr2ECPoint(pubKey), + new BigInteger(privKey, 16)); } public static SM2KeyPair getKeyPair() { @@ -126,17 +125,17 @@ public class JavaScriptEntry { return "success"; } - // public static String http(String baseUrl, String method, Map header, + // public static String http(String baseUrl, String method, Map header, // Map argMap, - // List reservedList) { - // return HttpUtil.request(baseUrl, method, header, argMap, reservedList); - // } + // List reservedList) { + // return HttpUtil.request(baseUrl, method, header, argMap, reservedList); + // } public static byte[] inputStreamToBytes(InputStream in) { ByteArrayOutputStream bo = new ByteArrayOutputStream(); byte[] buff = new byte[4 * 1024 * 1024]; try { - for (int count; (count = in.read(buff)) > 0; ) { + for (int count; (count = in.read(buff)) > 0;) { bo.write(buff, 0, count); } } catch (IOException e) { @@ -168,8 +167,8 @@ public class JavaScriptEntry { return new ApiGate(ip, Integer.parseInt(port)); } - public static String executeContractWithSig( - String contractID, String action, String arg, String pubkey, String sig) { + public static String executeContractWithSig(String contractID, String action, String arg, + String pubkey, String sig) { try { ContractRequest app = new ContractRequest(); app.setContractID(contractID).setAction(action).setArg(arg); @@ -182,22 +181,11 @@ public class JavaScriptEntry { app.setFromDebug(isDebug); if (numOfCopies > 1) { // The caller is special. - app.setRequestID( - app.getPublicKey().hashCode() - + "_" - + numOfCopies - + "_" - + (invokeID++) - + "_" - + random.nextInt() - + "_mul"); + app.setRequestID(app.getPublicKey().hashCode() + "_" + numOfCopies + "_" + + (invokeID++) + "_" + random.nextInt() + "_mul"); } else { - app.setRequestID( - app.getPublicKey().hashCode() - + "_" - + (invokeID++) - + "_" - + random.nextInt()); + app.setRequestID(app.getPublicKey().hashCode() + "_" + (invokeID++) + "_" + + random.nextInt()); } return get.syncGet("dd", "executeContract", JsonUtil.toJson(app)); @@ -210,9 +198,8 @@ public class JavaScriptEntry { public static Object executeContract(String contractID, String action, Object arg) { if (currentSyncUtil.engine.recovering) { - String str = - currentSyncUtil.transRecoverUtil.curRecoverRecord.getExecuteResult( - invokeID + ""); + String str = currentSyncUtil.transRecoverUtil.curRecoverRecord + .getExecuteResult(invokeID + ""); String[] strs = str.split(""); String flag1 = strs[0]; String flag = strs[1]; @@ -233,29 +220,22 @@ public class JavaScriptEntry { try { ContractRequest app = new ContractRequest(); - app.setContractID(contractID).setAction(action).setArg(JSONTool.convertMirrorToJson(arg)); + app.setContractID(contractID).setAction(action) + .setArg(JSONTool.convertMirrorToJson(arg)); app.doSignature(keyPair); app.setFromDebug(isDebug); ContractExecType type = ContractProcess.instance.getContract().getType(); if (type.needSeq()) { app.setRequestID( - String.format( - "%d_%d_%d_%d_mul", - keyPair.getPublicKeyStr().hashCode(), - numOfCopies, - (invokeID++), - random.nextInt())); + String.format("%d_%d_%d_%d_mul", keyPair.getPublicKeyStr().hashCode(), + numOfCopies, (invokeID++), random.nextInt())); // The caller is special. flag = 1; flag1 = 1; LOGGER.warn("invoke contractExecution! " + JsonUtil.toJson(app)); } else { - app.setRequestID( - String.format( - "%d_%d_%d", - keyPair.getPublicKeyStr().hashCode(), - (invokeID++), - random.nextInt())); + app.setRequestID(String.format("%d_%d_%d", keyPair.getPublicKeyStr().hashCode(), + (invokeID++), random.nextInt())); flag = 1; flag1 = 1; } @@ -264,75 +244,64 @@ public class JavaScriptEntry { ByteArrayOutputStream bo = new ByteArrayOutputStream(); e.printStackTrace(new PrintStream(bo)); String result = bo.toString(); - if (currentSyncUtil.startFlag - && currentSyncUtil.currType == SyncType.Trans + if (currentSyncUtil.startFlag && currentSyncUtil.currType == SyncType.Trans && !currentSyncUtil.engine.recovering) { - currentSyncUtil.transRecordUtil.recordExecutes( - formerInvokeID + "", flag1 + "" + flag + "" + result); + currentSyncUtil.transRecordUtil.recordExecutes(formerInvokeID + "", + flag1 + "" + flag + "" + result); } return result; } } - private static Object executeContract(long formerInvokeID, int flag1, int flag, ContractRequest app) { + private static Object executeContract(long formerInvokeID, int flag1, int flag, + ContractRequest app) { String result = get.syncGet("dd", "executeContract", JsonUtil.toJson(app)); - if (currentSyncUtil.startFlag - && currentSyncUtil.currType == SyncType.Trans + if (currentSyncUtil.startFlag && currentSyncUtil.currType == SyncType.Trans && !currentSyncUtil.engine.recovering) { - currentSyncUtil.transRecordUtil.recordExecutes( - formerInvokeID + "", + currentSyncUtil.transRecordUtil.recordExecutes(formerInvokeID + "", flag1 + "" + flag + "" + result); } JsonObject jo = JsonUtil.parseStringAsJsonObject(result); return JSONTool.convertJsonElementToMirror(jo); } - public static void executeContractAsyncWithoutSig( - String contractID, String action, String arg, final ScriptFunction cb) { + public static void executeContractAsyncWithoutSig(String contractID, String action, String arg, + final ScriptFunction cb) { try { ContractRequest app = new ContractRequest(); app.setContractID(contractID).setAction(action).setArg(arg); app.setRequestID((invokeID++) + "_" + random.nextInt()); - get.asyncGet( - "dd", - "executeContract", - JsonUtil.toJson(app), - new ResultCallback() { - @Override - public void onResult(String str) { - if (null != cb) { - DesktopEngine.applyWithGlobal( - cb, currentEngine.getNashornGlobal(), str); - } - } - }); + get.asyncGet("dd", "executeContract", JsonUtil.toJson(app), new ResultCallback() { + @Override + public void onResult(String str) { + if (null != cb) { + DesktopEngine.applyWithGlobal(cb, currentEngine.getNashornGlobal(), str); + } + } + }); } catch (Exception e) { ByteArrayOutputStream bo = new ByteArrayOutputStream(); e.printStackTrace(new PrintStream(bo)); } } - public static String executeContractAsync( - String contractID, String action, String arg, final ScriptFunction cb) { + public static String executeContractAsync(String contractID, String action, String arg, + final ScriptFunction cb) { try { ContractRequest app = new ContractRequest(); app.setContractID(contractID).setAction(action).setArg(arg); app.doSignature(keyPair); app.setRequestID((invokeID++) + "_" + random()); - get.asyncGet( - "dd", - "executeContract", - JsonUtil.toJson(app), - new ResultCallback() { - @Override - public void onResult(String str) { - if (cb != null) { - DesktopEngine.applyWithGlobal( - cb, currentEngine.getNashornGlobal(), str, arg); - } - } - }); + get.asyncGet("dd", "executeContract", JsonUtil.toJson(app), new ResultCallback() { + @Override + public void onResult(String str) { + if (cb != null) { + DesktopEngine.applyWithGlobal(cb, currentEngine.getNashornGlobal(), str, + arg); + } + } + }); return "success"; } catch (Exception e) { ByteArrayOutputStream bo = new ByteArrayOutputStream(); @@ -341,7 +310,7 @@ public class JavaScriptEntry { } } - //YancloudUtil.exceptionReturn({"msg":"missing arguments repoId ","code":1}); + // YancloudUtil.exceptionReturn({"msg":"missing arguments repoId ","code":1}); public static void exceptionReturn(Object obj) throws ScriptReturnException { throw new ScriptReturnException(JSONTool.convertMirrorToJson(obj)); } @@ -349,7 +318,7 @@ public class JavaScriptEntry { /** * publish an event with semantic AT_LEAST_ONCE * - * @param topic the topic + * @param topic the topic * @param content the content * @author Kaidong Wu */ @@ -360,16 +329,14 @@ public class JavaScriptEntry { /** * publish an event with some semantic * - * @param topic the topic - * @param content the content + * @param topic the topic + * @param content the content * @param constraint the constraint, AT_LEAST_ONCE, AT_MOST_ONCE, and ONLY_ONCE * @author Kaidong Wu */ public static void pubEventConstraint(String topic, String content, String constraint) { - String reqID = - String.format( - "%d_%d_%d_%s_pe", - keyPair.getPublicKeyStr().hashCode(), numOfCopies, invokeID, random()); + String reqID = String.format("%d_%d_%d_%s_pe", keyPair.getPublicKeyStr().hashCode(), + numOfCopies, invokeID, random()); REvent msg = new REvent(topic, PUBLISH, content, reqID); if (null != constraint) { msg.setSemantics(REvent.REventSemantics.valueOf(constraint)); @@ -381,7 +348,7 @@ public class JavaScriptEntry { * subscribe a topic * * @param topic event topic - * @param fun related handler function + * @param fun related handler function * @author Kaidong Wu */ public static String subscribe(String topic, ScriptFunction fun) { @@ -397,8 +364,8 @@ public class JavaScriptEntry { * subscribe a local event * * @param contractID contractID - * @param event local event topic - * @param fun related handler function + * @param event local event topic + * @param fun related handler function * @author Kaidong Wu */ public static String subscribe(String contractID, String event, ScriptFunction fun) { @@ -412,19 +379,13 @@ public class JavaScriptEntry { } private static void subscribe(String topic, ScriptFunction fun, boolean fromPreSub) { - String reqID = - String.format( - "%d_%d_%d_%s_se", - keyPair.getPublicKeyStr().hashCode(), numOfCopies, invokeID, random()); + String reqID = String.format("%d_%d_%d_%s_se", keyPair.getPublicKeyStr().hashCode(), + numOfCopies, invokeID, random()); - REvent msg = - new REvent( - topic, - SUBSCRIBE, - String.format( - "{\"subscriber\":\"%s\",\"handler\":\"%s\"}", - ContractProcess.instance.getContractName(), fun.getName()), - reqID); + REvent msg = new REvent(topic, SUBSCRIBE, + String.format("{\"subscriber\":\"%s\",\"handler\":\"%s\"}", + ContractProcess.instance.getContractName(), fun.getName()), + reqID); if (fromPreSub) { msg.setSemantics(REvent.REventSemantics.ONLY_ONCE); } @@ -434,10 +395,8 @@ public class JavaScriptEntry { } public static void unsubscribe(String topic) { - String reqID = - String.format( - "%d_%d_%d_%s_us", - keyPair.getPublicKeyStr().hashCode(), numOfCopies, invokeID, random()); + String reqID = String.format("%d_%d_%d_%s_us", keyPair.getPublicKeyStr().hashCode(), + numOfCopies, invokeID, random()); String content; if (null == topic) { content = "{\"subscriber\":\"" + ContractProcess.instance.getContractName() + "\"}"; @@ -447,10 +406,8 @@ public class JavaScriptEntry { }); } else { String handler = topic_handlers.get(topic).getName(); - content = - String.format( - "{\"subscriber\":\"%s\",\"handler\":\"%s\"}", - ContractProcess.instance.getContractName(), handler); + content = String.format("{\"subscriber\":\"%s\",\"handler\":\"%s\"}", + ContractProcess.instance.getContractName(), handler); topic_handlers.remove(topic); ContractProcess.instance.unSubscribe(handler); } @@ -461,17 +418,15 @@ public class JavaScriptEntry { /** * pre-sub in ONLY_ONCE * - * @param topic the topic + * @param topic the topic * @param content the content * @author Kaidong Wu */ public static void preSub(String topic, String content) { String newTopic = topic + "|" + content + "|" + ContractProcess.instance.getContractName(); subscribe(newTopic, topic_handlers.get(topic), true); - String reqID = - String.format( - "%d_%d_%d_%s_pse", - keyPair.getPublicKeyStr().hashCode(), numOfCopies, (invokeID++), random()); + String reqID = String.format("%d_%d_%d_%s_pse", keyPair.getPublicKeyStr().hashCode(), + numOfCopies, (invokeID++), random()); REvent msg = new REvent(topic, REvent.REventType.PRESUB, newTopic, reqID); msg.setSemantics(REvent.REventSemantics.ONLY_ONCE); msgList.add(msg); @@ -482,7 +437,8 @@ public class JavaScriptEntry { * @author Kaidong Wu */ public static String random() { - String seed = String.valueOf(null == random ? System.currentTimeMillis() : random.nextInt()); + String seed = + String.valueOf(null == random ? System.currentTimeMillis() : random.nextInt()); return HashUtil.sha3(seed); } diff --git a/src/main/java/org/bdware/sc/boundry/Resources.java b/src/main/java/org/bdware/sc/boundry/Resources.java index dfaeaa4..b177eca 100644 --- a/src/main/java/org/bdware/sc/boundry/Resources.java +++ b/src/main/java/org/bdware/sc/boundry/Resources.java @@ -10,7 +10,6 @@ import java.util.Scanner; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; -import java.util.zip.ZipInputStream; public class Resources { private final ZipFile zf; YJSClassLoader loader; @@ -49,7 +48,7 @@ public class Resources { InputStream sc = loadAsInputStream(path); ByteArrayOutputStream bo = new ByteArrayOutputStream(); byte[] buff = new byte[1024]; - for (int k = 0; (k = sc.read(buff)) > 0; ) { + for (int k = 0; (k = sc.read(buff)) > 0;) { bo.write(buff, 0, k); } return new String(bo.toByteArray()); @@ -67,7 +66,8 @@ public class Resources { ZipEntry entry = zf.getEntry(path); if (entry == null) return null; - reader = new BufferedReader(new InputStreamReader(zf.getInputStream(entry), StandardCharsets.UTF_8)); + reader = new BufferedReader( + new InputStreamReader(zf.getInputStream(entry), StandardCharsets.UTF_8)); String line = null; while ((line = reader.readLine()) != null) { fileList.add(line); @@ -84,7 +84,8 @@ public class Resources { public String unzipToDir(String path) { ZipEntry entry = zf.getEntry(path); try { - return loader.unzipLibrary(zf.getInputStream(entry), entry.getName().replaceAll(".*/", "")); + return loader.unzipLibrary(zf.getInputStream(entry), + entry.getName().replaceAll(".*/", "")); } catch (IOException e) { e.printStackTrace(); } diff --git a/src/main/java/org/bdware/sc/boundry/utils/AESUtil.java b/src/main/java/org/bdware/sc/boundry/utils/AESUtil.java index c1e8d05..59b9b64 100644 --- a/src/main/java/org/bdware/sc/boundry/utils/AESUtil.java +++ b/src/main/java/org/bdware/sc/boundry/utils/AESUtil.java @@ -16,13 +16,16 @@ import java.security.NoSuchAlgorithmException; @PermissionStub(permission = Permission.AES) public class AESUtil { - public static ScriptObject encrypt(String key, String plaintext) throws NoSuchPaddingException, NoSuchAlgorithmException, BadPaddingException, IllegalBlockSizeException, InvalidAlgorithmParameterException, InvalidKeyException { + public static ScriptObject encrypt(String key, String plaintext) + throws NoSuchPaddingException, NoSuchAlgorithmException, BadPaddingException, + IllegalBlockSizeException, InvalidAlgorithmParameterException, InvalidKeyException { JO ret = new JO(PropertyMap.newMap()); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); String iv = generateKey(128); IvParameterSpec ivSpec = new IvParameterSpec(DatatypeConverter.parseHexBinary(iv)); byte[] byteContent = plaintext.getBytes(); - SecretKeySpec secretKeySpecSpec = new SecretKeySpec(DatatypeConverter.parseHexBinary(key), "AES"); + SecretKeySpec secretKeySpecSpec = + new SecretKeySpec(DatatypeConverter.parseHexBinary(key), "AES"); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpecSpec, ivSpec); byte[] result = cipher.doFinal(byteContent); ret.put("iv", iv, false); @@ -30,12 +33,16 @@ public class AESUtil { return ret; } - public static String decrypt(String key, String ciphertext, String iv) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { -// JO ret = new JO(PropertyMap.newMap()); + public static String decrypt(String key, String ciphertext, String iv) + throws NoSuchPaddingException, NoSuchAlgorithmException, + InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, + IllegalBlockSizeException { + // JO ret = new JO(PropertyMap.newMap()); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); IvParameterSpec ivSpec = new IvParameterSpec(DatatypeConverter.parseHexBinary(iv)); byte[] byteContent = DatatypeConverter.parseHexBinary(ciphertext); - SecretKeySpec secretKeySpecSpec = new SecretKeySpec(DatatypeConverter.parseHexBinary(key), "AES"); + SecretKeySpec secretKeySpecSpec = + new SecretKeySpec(DatatypeConverter.parseHexBinary(key), "AES"); cipher.init(Cipher.DECRYPT_MODE, secretKeySpecSpec, ivSpec); byte[] result = cipher.doFinal(byteContent); return new String(result); diff --git a/src/main/java/org/bdware/sc/boundry/utils/AsyncUtil.java b/src/main/java/org/bdware/sc/boundry/utils/AsyncUtil.java index 046be73..97939c0 100644 --- a/src/main/java/org/bdware/sc/boundry/utils/AsyncUtil.java +++ b/src/main/java/org/bdware/sc/boundry/utils/AsyncUtil.java @@ -18,7 +18,7 @@ import java.util.TimerTask; @PermissionStub(permission = Permission.Async) public class AsyncUtil { private static final Timer TIMER = new Timer(); -// public static ExecutorService executorService = Executors.newFixedThreadPool(10); + // public static ExecutorService executorService = Executors.newFixedThreadPool(10); public static String sleep(long sleep) { try { @@ -30,53 +30,47 @@ public class AsyncUtil { } public static String postFunction(final ScriptFunction callback, Object wrapper) { - ServiceServer.executor.execute( - () -> JavaScriptEntry.executeFunction(callback, wrapper)); + ServiceServer.executor.execute(() -> JavaScriptEntry.executeFunction(callback, wrapper)); return "success"; } - public static TimerTask setTimeOut( - final ScriptFunction callback, long delay, final Object arg) { - TimerTask task = - new TimerTask() { - @Override - public void run() { - JavaScriptEntry.executeFunction(callback, arg); - } - }; + public static TimerTask setTimeOut(final ScriptFunction callback, long delay, + final Object arg) { + TimerTask task = new TimerTask() { + @Override + public void run() { + JavaScriptEntry.executeFunction(callback, arg); + } + }; TIMER.schedule(task, delay); return task; } - public static TimerTask setInterval( - final ScriptFunction callback, long delay, long interval, final Object arg) { - TimerTask task = - new TimerTask() { - @Override - public void run() { - JavaScriptEntry.executeFunction(callback, arg); - } - }; + public static TimerTask setInterval(final ScriptFunction callback, long delay, long interval, + final Object arg) { + TimerTask task = new TimerTask() { + @Override + public void run() { + JavaScriptEntry.executeFunction(callback, arg); + } + }; TIMER.schedule(task, delay, interval); return task; } - public static void executeContractAsyncWithoutSig( - String contractID, String action, String arg, final ScriptFunction cb) { + public static void executeContractAsyncWithoutSig(String contractID, String action, String arg, + final ScriptFunction cb) { try { ContractRequest app = new ContractRequest(); app.setContractID(contractID).setAction(action).setArg(arg); app.setRequestID((JavaScriptEntry.invokeID++) + "_" + JavaScriptEntry.random.nextInt()); - JavaScriptEntry.get.asyncGet( - "dd", - "executeContract", - JsonUtil.toJson(app), + JavaScriptEntry.get.asyncGet("dd", "executeContract", JsonUtil.toJson(app), new ResultCallback() { @Override public void onResult(String str) { if (null != cb) { - DesktopEngine.applyWithGlobal( - cb, JavaScriptEntry.currentEngine.getNashornGlobal(), str); + DesktopEngine.applyWithGlobal(cb, + JavaScriptEntry.currentEngine.getNashornGlobal(), str); } } }); @@ -85,24 +79,22 @@ public class AsyncUtil { e.printStackTrace(new PrintStream(bo)); } } - public static String executeContractAsync( - String contractID, String action, String arg, final ScriptFunction cb) { + + public static String executeContractAsync(String contractID, String action, String arg, + final ScriptFunction cb) { try { ContractRequest app = new ContractRequest(); app.setContractID(contractID).setAction(action).setArg(arg); app.doSignature(JavaScriptEntry.getKeyPair()); app.setRequestID((JavaScriptEntry.invokeID++) + "_" + JavaScriptEntry.random()); - JavaScriptEntry.get.asyncGet( - "dd", - "executeContract", - JsonUtil.toJson(app), + JavaScriptEntry.get.asyncGet("dd", "executeContract", JsonUtil.toJson(app), new ResultCallback() { @Override public void onResult(String str) { if (cb != null) { - DesktopEngine.applyWithGlobal( - cb, JavaScriptEntry.currentEngine.getNashornGlobal(), str, arg); + DesktopEngine.applyWithGlobal(cb, + JavaScriptEntry.currentEngine.getNashornGlobal(), str, arg); } } }); diff --git a/src/main/java/org/bdware/sc/boundry/utils/BDWareTimeSeriesDBUtil.java b/src/main/java/org/bdware/sc/boundry/utils/BDWareTimeSeriesDBUtil.java index 838ce32..821b28b 100644 --- a/src/main/java/org/bdware/sc/boundry/utils/BDWareTimeSeriesDBUtil.java +++ b/src/main/java/org/bdware/sc/boundry/utils/BDWareTimeSeriesDBUtil.java @@ -2,7 +2,6 @@ package org.bdware.sc.boundry.utils; import org.bdware.sc.ContractProcess; import org.bdware.sc.compiler.PermissionStub; -import org.bdware.sc.db.TimeDBUtil; import org.bdware.sc.db.TimeRocksDBUtil; import org.bdware.sc.node.Permission; diff --git a/src/main/java/org/bdware/sc/boundry/utils/DOIPUtil.java b/src/main/java/org/bdware/sc/boundry/utils/DOIPUtil.java index 2a3c8f5..4a8fbca 100644 --- a/src/main/java/org/bdware/sc/boundry/utils/DOIPUtil.java +++ b/src/main/java/org/bdware/sc/boundry/utils/DOIPUtil.java @@ -77,7 +77,8 @@ public class DOIPUtil { msg = ret.get(0); if (msg != null) { return msg; - } else return DoipMessageFactory.createTimeoutResponse(message.requestID); + } else + return DoipMessageFactory.createTimeoutResponse(message.requestID); } catch (Exception ie) { ie.printStackTrace(); return DoipMessageFactory.createConnectFailedResponse(message.requestID); @@ -97,13 +98,14 @@ public class DOIPUtil { } public String hello(String repoID) { - DoipMessage msg = (new DoipMessageFactory.DoipMessageBuilder()).createRequest(repoID, BasicOperations.Hello.getName()).create(); + DoipMessage msg = (new DoipMessageFactory.DoipMessageBuilder()) + .createRequest(repoID, BasicOperations.Hello.getName()).create(); return convertDoipMsgToString(syncGetMessage(msg)); } public String retrieve(String doi, String args) { - DoipMessage msg = (new DoipMessageFactory.DoipMessageBuilder()).createRequest(doi, BasicOperations.Retrieve.getName()) - .create(); + DoipMessage msg = (new DoipMessageFactory.DoipMessageBuilder()) + .createRequest(doi, BasicOperations.Retrieve.getName()).create(); msg.header.parameters.addAttribute("element", ""); return convertDoipMsgToString(syncGetMessage(msg)); } @@ -126,27 +128,21 @@ public class DOIPUtil { } e.dataString = null; } - DoipMessage msg = - new DoipMessageFactory.DoipMessageBuilder() - .createRequest(repoID, BasicOperations.Create.getName()) - .setBody(digitalObject) - .create(); + DoipMessage msg = new DoipMessageFactory.DoipMessageBuilder() + .createRequest(repoID, BasicOperations.Create.getName()).setBody(digitalObject) + .create(); return convertDoipMsgToString(syncGetMessage(msg)); } public String delete(String doID) { - DoipMessage msg = - new DoipMessageFactory.DoipMessageBuilder() - .createRequest(doID, BasicOperations.Delete.getName()) - .create(); + DoipMessage msg = new DoipMessageFactory.DoipMessageBuilder() + .createRequest(doID, BasicOperations.Delete.getName()).create(); return convertDoipMsgToString(syncGetMessage(msg)); } public String listOperation(String doID) { - DoipMessage msg = - new DoipMessageFactory.DoipMessageBuilder() - .createRequest(doID, BasicOperations.ListOps.getName()) - .create(); + DoipMessage msg = new DoipMessageFactory.DoipMessageBuilder() + .createRequest(doID, BasicOperations.ListOps.getName()).create(); return convertDoipMsgToString(syncGetMessage(msg)); } @@ -222,7 +218,8 @@ public class DOIPUtil { return createIrpClient(uri, null, null, null); } - public static IRPClientWrapper createIrpClient(String uri, String pubkey, String privateKey, String repoName) { + public static IRPClientWrapper createIrpClient(String uri, String pubkey, String privateKey, + String repoName) { EndpointConfig config = new EndpointConfig(); config.routerURI = uri; config.repoName = repoName; diff --git a/src/main/java/org/bdware/sc/boundry/utils/DOMUtil.java b/src/main/java/org/bdware/sc/boundry/utils/DOMUtil.java index fff82d8..9521f82 100644 --- a/src/main/java/org/bdware/sc/boundry/utils/DOMUtil.java +++ b/src/main/java/org/bdware/sc/boundry/utils/DOMUtil.java @@ -7,4 +7,4 @@ public class DOMUtil { public static Document parse(String html) { return Jsoup.parse(html); } -} \ No newline at end of file +} diff --git a/src/main/java/org/bdware/sc/boundry/utils/EmailUtil.java b/src/main/java/org/bdware/sc/boundry/utils/EmailUtil.java index d1066c9..16dabab 100644 --- a/src/main/java/org/bdware/sc/boundry/utils/EmailUtil.java +++ b/src/main/java/org/bdware/sc/boundry/utils/EmailUtil.java @@ -25,24 +25,20 @@ public class EmailUtil { props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY); props.setProperty("mail.smtp.socketFactory.fallback", "false"); props.setProperty("mail.smtp.socketFactory.port", jo.get("port").getAsString()); - Session session = - Session.getDefaultInstance( - props, - new Authenticator() { - public PasswordAuthentication getPasswordAuthentication() { - return new PasswordAuthentication( - jo.get("from").getAsString(), - jo.get("pwd").getAsString()); // 发件人邮件用户名、密码 - } - }); + Session session = Session.getDefaultInstance(props, new Authenticator() { + public PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(jo.get("from").getAsString(), + jo.get("pwd").getAsString()); // 发件人邮件用户名、密码 + } + }); // 创建邮件对象 Message msg = new MimeMessage(session); msg.setSubject(jo.get("subject").getAsString()); msg.setText(jo.get("content").getAsString()); msg.setFrom(new InternetAddress(jo.get("from").getAsString())); - msg.addRecipient( - Message.RecipientType.TO, new InternetAddress(jo.get("to").getAsString())); + msg.addRecipient(Message.RecipientType.TO, + new InternetAddress(jo.get("to").getAsString())); Transport.send(msg); } catch (Exception e) { e.printStackTrace(); diff --git a/src/main/java/org/bdware/sc/boundry/utils/HttpUtil.java b/src/main/java/org/bdware/sc/boundry/utils/HttpUtil.java index ce3736a..fae8948 100644 --- a/src/main/java/org/bdware/sc/boundry/utils/HttpUtil.java +++ b/src/main/java/org/bdware/sc/boundry/utils/HttpUtil.java @@ -48,7 +48,8 @@ public class HttpUtil { ScriptObjectMirror som = (ScriptObjectMirror) headers; for (String key : som.getOwnKeys(true)) { Object val = som.get(key); - if (val instanceof String) connection.setRequestProperty(key, (String) val); + if (val instanceof String) + connection.setRequestProperty(key, (String) val); } } else { connection.setRequestProperty("Accept", "application/json"); @@ -64,7 +65,7 @@ public class HttpUtil { InputStream input = connection.getInputStream(); Scanner sc = new Scanner(input); StringBuilder sb = new StringBuilder(); - for (; sc.hasNextLine(); ) { + for (; sc.hasNextLine();) { sb.append(sc.nextLine()).append("\n"); } sc.close(); @@ -77,10 +78,12 @@ public class HttpUtil { } return ret; } - public static String encodeURI(String str){ + + public static String encodeURI(String str) { return URLEncoder.encode(str); } - public static String decodeURI(String str){ + + public static String decodeURI(String str) { return URLDecoder.decode(str); } @@ -131,7 +134,8 @@ public class HttpUtil { connection.setRequestProperty("Content-Type", "application/json"); } connection.connect(); - OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), StandardCharsets.UTF_8); + OutputStreamWriter out = + new OutputStreamWriter(connection.getOutputStream(), StandardCharsets.UTF_8); out.append((String) str.get("data")); out.flush(); out.close(); @@ -156,74 +160,67 @@ public class HttpUtil { private static CloseableHttpClient getHttpClient(String url) { try { SSLContext sslcontext = - SSLContexts.custom() - .loadTrustMaterial( - null, - (arg0, arg1) -> true) - .build(); + SSLContexts.custom().loadTrustMaterial(null, (arg0, arg1) -> true).build(); - SSLConnectionSocketFactory sslSf = - new SSLConnectionSocketFactory( - sslcontext, null, null, new NoopHostnameVerifier()); + SSLConnectionSocketFactory sslSf = new SSLConnectionSocketFactory(sslcontext, null, + null, new NoopHostnameVerifier()); int tle = 10; if (url.contains("data.tj.gov.cn")) { tle = 3; } - return HttpClients.custom() - .setSSLSocketFactory(sslSf) + return HttpClients.custom().setSSLSocketFactory(sslSf) .setKeepAliveStrategy((arg0, arg1) -> 0) - .setConnectionTimeToLive(tle, TimeUnit.SECONDS) - .build(); + .setConnectionTimeToLive(tle, TimeUnit.SECONDS).build(); } catch (Exception e) { e.printStackTrace(); } return null; } - // public static String httpPost(String str) { - // System.out.println("JavaSScriptEntry httpPost:" + str); - // PostRequest req = new PostRequest(); - // req = JsonUtil.fromJson(str, PostRequest.class); - // // System.out.println("url========>" + req.url); - // // System.out.println("data=======>" + req.data); + // public static String httpPost(String str) { + // System.out.println("JavaSScriptEntry httpPost:" + str); + // PostRequest req = new PostRequest(); + // req = JsonUtil.fromJson(str, PostRequest.class); + // // System.out.println("url========>" + req.url); + // // System.out.println("data=======>" + req.data); // - // Result r = new Result(); - // try { - // URL url = new URL(req.url);// - // HttpURLConnection connection = (HttpURLConnection) url.openConnection(); - // connection.setDoOutput(true); - // connection.setDoInput(true); - // connection.setUseCaches(false); - // connection.setInstanceFollowRedirects(true); - // connection.setRequestMethod("POST"); - // connection.setRequestProperty("Accept", "application/json"); - // connection.setRequestProperty("Content-Type", "application/json"); - // connection.connect(); - // OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); // + // Result r = new Result(); + // try { + // URL url = new URL(req.url);// + // HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + // connection.setDoOutput(true); + // connection.setDoInput(true); + // connection.setUseCaches(false); + // connection.setInstanceFollowRedirects(true); + // connection.setRequestMethod("POST"); + // connection.setRequestProperty("Accept", "application/json"); + // connection.setRequestProperty("Content-Type", "application/json"); + // connection.connect(); + // OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); // // utf-8缂栫爜 - // out.append(req.data); - // out.flush(); - // out.close(); + // out.append(req.data); + // out.flush(); + // out.close(); // - // r.resposeCode = connection.getResponseCode(); - // InputStream input = connection.getInputStream(); + // r.resposeCode = connection.getResponseCode(); + // InputStream input = connection.getInputStream(); // - // Scanner sc = new Scanner(input); - // StringBuilder sb = new StringBuilder(); - // for (; sc.hasNextLine();) { - // sb.append(sc.nextLine()).append("\n"); - // } - // sc.close(); - // r.response = sb.toString(); - // return JsonUtil.toJson(r); - // } catch (Throwable e) { - // r.resposeCode = 505; - // // ByteArrayOutputStream bo = new ByteArrayOutputStream(); - // // e.printStackTrace(new PrintStream(bo)); - // r.response = e.getMessage(); - // return JsonUtil.toJson(r); - // } - // } + // Scanner sc = new Scanner(input); + // StringBuilder sb = new StringBuilder(); + // for (; sc.hasNextLine();) { + // sb.append(sc.nextLine()).append("\n"); + // } + // sc.close(); + // r.response = sb.toString(); + // return JsonUtil.toJson(r); + // } catch (Throwable e) { + // r.resposeCode = 505; + // // ByteArrayOutputStream bo = new ByteArrayOutputStream(); + // // e.printStackTrace(new PrintStream(bo)); + // r.response = e.getMessage(); + // return JsonUtil.toJson(r); + // } + // } public static String postTask(String args, final ScriptFunction callback) { System.out.println("[JavaScriptEntry]" + args); @@ -235,62 +232,59 @@ public class HttpUtil { RequestBody.create(MediaType.parse("application/json; charset=utf-8"), req.data); Request request = new Request.Builder().url(req.url).post(body).build(); // 2.瀹氫箟涓�涓猺equest Call call = okHttpClient.newCall(request); // - call.enqueue( - new Callback() { // - @Override - public void onFailure(Call call, IOException e) { - } + call.enqueue(new Callback() { // + @Override + public void onFailure(Call call, IOException e) {} - @Override - public void onResponse(Call call, Response response) throws IOException { - String result = response.body().string(); // - System.out.println("currentEngine:"); - DesktopEngine.applyWithGlobal( - callback, currentEngine.getNashornGlobal(), result); - } - }); + @Override + public void onResponse(Call call, Response response) throws IOException { + String result = response.body().string(); // + System.out.println("currentEngine:"); + DesktopEngine.applyWithGlobal(callback, currentEngine.getNashornGlobal(), result); + } + }); return "success"; } - // public static String httpGet(String str) { - // // System.out.println("JavaScriptEntry httpGet:" + str); - // Result r = new Result(); - // try { - // HttpGet httpGet = new HttpGet(str); - // RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(60000) - // .setConnectTimeout(60000).setSocketTimeout(60000).build(); - // httpGet.setConfig(requestConfig); - // httpGet.addHeader("Pragma", "no-cache"); - // httpGet.addHeader("Cache-Control", "no-cache"); - // httpGet.addHeader("Upgrade-Insecure-Requests", "1"); - // httpGet.addHeader("User-Agent", - // "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) + // public static String httpGet(String str) { + // // System.out.println("JavaScriptEntry httpGet:" + str); + // Result r = new Result(); + // try { + // HttpGet httpGet = new HttpGet(str); + // RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(60000) + // .setConnectTimeout(60000).setSocketTimeout(60000).build(); + // httpGet.setConfig(requestConfig); + // httpGet.addHeader("Pragma", "no-cache"); + // httpGet.addHeader("Cache-Control", "no-cache"); + // httpGet.addHeader("Upgrade-Insecure-Requests", "1"); + // httpGet.addHeader("User-Agent", + // "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) // Chrome/78.0.3904.97 Safari/537.36"); - // httpGet.addHeader("Sec-Fetch-User", "?1"); - // httpGet.addHeader("Accept", + // httpGet.addHeader("Sec-Fetch-User", "?1"); + // httpGet.addHeader("Accept", // - // "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3"); - // httpGet.addHeader("Sec-Fetch-Site", "none"); - // httpGet.addHeader("Sec-Fetch-Mode", "navigate"); - // httpGet.addHeader("Accept-Encoding", "gzip, deflate, br"); - // httpGet.addHeader("Accept-Language", "zh-CN,zh;q=0.9"); - // CloseableHttpResponse response1 = getHttpClient(str).execute(httpGet); - // InputStream content = response1.getEntity().getContent(); - // ByteArrayOutputStream bo = new ByteArrayOutputStream(); - // byte[] buff = new byte[4096]; - // for (int k = 0; (k = content.read(buff)) > 0;) { - // bo.write(buff, 0, k); - // } - // r.response = bo.toString(); - // return JsonUtil.toJson(r); - // } catch (Throwable e) { - // r.resposeCode = 505; - // // ByteArrayOutputStream bo = new ByteArrayOutputStream(); - // // e.printStackTrace(new PrintStream(bo)); - // r.response = e.getMessage(); - // return JsonUtil.toJson(r); - // } - // } + // "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3"); + // httpGet.addHeader("Sec-Fetch-Site", "none"); + // httpGet.addHeader("Sec-Fetch-Mode", "navigate"); + // httpGet.addHeader("Accept-Encoding", "gzip, deflate, br"); + // httpGet.addHeader("Accept-Language", "zh-CN,zh;q=0.9"); + // CloseableHttpResponse response1 = getHttpClient(str).execute(httpGet); + // InputStream content = response1.getEntity().getContent(); + // ByteArrayOutputStream bo = new ByteArrayOutputStream(); + // byte[] buff = new byte[4096]; + // for (int k = 0; (k = content.read(buff)) > 0;) { + // bo.write(buff, 0, k); + // } + // r.response = bo.toString(); + // return JsonUtil.toJson(r); + // } catch (Throwable e) { + // r.resposeCode = 505; + // // ByteArrayOutputStream bo = new ByteArrayOutputStream(); + // // e.printStackTrace(new PrintStream(bo)); + // r.response = e.getMessage(); + // return JsonUtil.toJson(r); + // } + // } public static ApiGate createAPIGate(String ip) { return new ApiGate(ip); diff --git a/src/main/java/org/bdware/sc/boundry/utils/IRPUtil.java b/src/main/java/org/bdware/sc/boundry/utils/IRPUtil.java index d7230ba..006e9fd 100644 --- a/src/main/java/org/bdware/sc/boundry/utils/IRPUtil.java +++ b/src/main/java/org/bdware/sc/boundry/utils/IRPUtil.java @@ -1,33 +1,17 @@ package org.bdware.sc.boundry.utils; -import com.google.api.client.json.Json; import com.google.gson.JsonObject; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.bdware.doip.audit.EndpointConfig; import org.bdware.doip.audit.client.AuditIrpClient; import org.bdware.doip.audit.config.TempConfigStorage; -import org.bdware.doip.codec.digitalObject.DigitalObject; -import org.bdware.doip.codec.digitalObject.Element; -import org.bdware.doip.codec.doipMessage.DoipMessage; -import org.bdware.doip.codec.doipMessage.DoipMessageFactory; -import org.bdware.doip.codec.operations.BasicOperations; -import org.bdware.doip.endpoint.client.ClientConfig; -import org.bdware.doip.endpoint.client.DoipClientImpl; -import org.bdware.doip.endpoint.client.DoipMessageCallback; -import org.bdware.irp.exception.IrpClientException; import org.bdware.irp.stateinfo.StateInfoBase; import org.bdware.sc.compiler.PermissionStub; import org.bdware.sc.engine.JSONTool; import org.bdware.sc.node.Permission; -import org.bdware.sc.util.JsonUtil; import wrp.jdk.nashorn.api.scripting.ScriptObjectMirror; -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.List; @PermissionStub(permission = Permission.IRP) public class IRPUtil { @@ -40,7 +24,8 @@ public class IRPUtil { EndpointConfig endpointConfig = configStorage.loadAsEndpointConfig(); if (jo.has("clientDoId")) { - this.auditIrpClient = new AuditIrpClient(jo.get("clientDoId").getAsString(), endpointConfig); + this.auditIrpClient = + new AuditIrpClient(jo.get("clientDoId").getAsString(), endpointConfig); } else { this.auditIrpClient = new AuditIrpClient(endpointConfig); } diff --git a/src/main/java/org/bdware/sc/boundry/utils/LedgerUtil.java b/src/main/java/org/bdware/sc/boundry/utils/LedgerUtil.java index 891a8f0..481f563 100644 --- a/src/main/java/org/bdware/sc/boundry/utils/LedgerUtil.java +++ b/src/main/java/org/bdware/sc/boundry/utils/LedgerUtil.java @@ -27,7 +27,7 @@ public class LedgerUtil { static SecureRandom random = new SecureRandom((System.currentTimeMillis() + "").getBytes()); public static Object getLedgerParams() { - //format:{nodes:[{ip:ip,port:port}]} + // format:{nodes:[{ip:ip,port:port}]} String ledgerParam = JavaScriptEntry.get.syncGet("", "getLedgerParams", ""); JsonElement jo = JsonParser.parseString(ledgerParam); return JSONTool.convertJsonElementToMirror(jo); @@ -36,7 +36,8 @@ public class LedgerUtil { public static Client getDefaultClient() { String ledgerParam = JavaScriptEntry.get.syncGet("", "getLedgerParams", ""); JsonElement jo = JsonParser.parseString(ledgerParam); - JsonObject param = jo.getAsJsonObject().get("nodes").getAsJsonArray().get(0).getAsJsonObject(); + JsonObject param = + jo.getAsJsonObject().get("nodes").getAsJsonArray().get(0).getAsJsonObject(); return new Client(param.get("ip").getAsString(), param.get("port").getAsInt()); } @@ -57,7 +58,8 @@ public class LedgerUtil { ret.put("to", HashUtil.byteArray2Str(transaction.getTo().toByteArray(), 0), false); ret.put("type", transaction.getType().toString(), false); ret.put("data", new String(transaction.getData().toByteArray()), false); - ret.put("blockHsah", HashUtil.byteArray2Str(transaction.getBlockHash().toByteArray(), 0), false); + ret.put("blockHsah", HashUtil.byteArray2Str(transaction.getBlockHash().toByteArray(), 0), + false); return ret; } @@ -66,14 +68,8 @@ public class LedgerUtil { String from = str.get("from").toString(); String to = str.get("to").toString(); String data = str.get("data").toString(); - SendTransactionResponse result = - c.sendTransactionSync( - ledger, - TransactionType.MESSAGE, - from, - random.nextLong(), - to, - data.getBytes()); + SendTransactionResponse result = c.sendTransactionSync(ledger, TransactionType.MESSAGE, + from, random.nextLong(), to, data.getBytes()); return HashUtil.byteArray2Str(result.getHash().toByteArray(), 0); } } diff --git a/src/main/java/org/bdware/sc/boundry/utils/MongoDBUtil.java b/src/main/java/org/bdware/sc/boundry/utils/MongoDBUtil.java index 7c0218c..5baf807 100644 --- a/src/main/java/org/bdware/sc/boundry/utils/MongoDBUtil.java +++ b/src/main/java/org/bdware/sc/boundry/utils/MongoDBUtil.java @@ -18,12 +18,8 @@ public class MongoDBUtil { List addrs = new ArrayList<>(); addrs.add(serverAddress); Method createeScramSha1 = - Class.forName("com.mongodb.MongoCredential") - .getDeclaredMethod( - "createScramSha1Credential", - String.class, - String.class, - char[].class); + Class.forName("com.mongodb.MongoCredential").getDeclaredMethod( + "createScramSha1Credential", String.class, String.class, char[].class); Object credential = createeScramSha1.invoke(null, usrName, dbName, pwd.toCharArray()); List credentials = new ArrayList<>(); credentials.add(credential); diff --git a/src/main/java/org/bdware/sc/boundry/utils/MultiTagIndexDBUtil.java b/src/main/java/org/bdware/sc/boundry/utils/MultiTagIndexDBUtil.java index 7630a92..70a4d51 100644 --- a/src/main/java/org/bdware/sc/boundry/utils/MultiTagIndexDBUtil.java +++ b/src/main/java/org/bdware/sc/boundry/utils/MultiTagIndexDBUtil.java @@ -116,7 +116,8 @@ public class MultiTagIndexDBUtil { public List countInInterval(String tag, long startTime, long endTime, long interval) { List ret = new ArrayList<>(); - if (interval <= 0) return ret; + if (interval <= 0) + return ret; long start = rocksDB.queryOffset(tag, startTime); long delta; startTime += interval; diff --git a/src/main/java/org/bdware/sc/boundry/utils/RocksDBUtil.java b/src/main/java/org/bdware/sc/boundry/utils/RocksDBUtil.java index 47cc280..2207411 100644 --- a/src/main/java/org/bdware/sc/boundry/utils/RocksDBUtil.java +++ b/src/main/java/org/bdware/sc/boundry/utils/RocksDBUtil.java @@ -35,7 +35,7 @@ public class RocksDBUtil { options.setCreateIfMissing(true); File parent = new File("./ContractDB/" + ContractProcess.getContractDir()); File dir = new File(parent, path); - //LOGGER.info("init RocksDB in " + dir.getAbsolutePath()); + // LOGGER.info("init RocksDB in " + dir.getAbsolutePath()); if (!dir.exists()) { LOGGER.info("create directory " + dir.getAbsolutePath() + ": " + dir.mkdirs()); } diff --git a/src/main/java/org/bdware/sc/boundry/utils/SM2Util.java b/src/main/java/org/bdware/sc/boundry/utils/SM2Util.java index 0976f2b..230a332 100644 --- a/src/main/java/org/bdware/sc/boundry/utils/SM2Util.java +++ b/src/main/java/org/bdware/sc/boundry/utils/SM2Util.java @@ -46,14 +46,13 @@ public class SM2Util { JO ret = new JO(PropertyMap.newMap()); try { byte[] sig = ByteUtils.fromHexString(signature); - ECPublicKeyParameters pubKey = - BCECUtil.createECPublicKeyFromStrParameters( - pubKeyStr, - org.zz.gmhelper.SM2Util.CURVE, - org.zz.gmhelper.SM2Util.DOMAIN_PARAMS); + ECPublicKeyParameters pubKey = BCECUtil.createECPublicKeyFromStrParameters(pubKeyStr, + org.zz.gmhelper.SM2Util.CURVE, org.zz.gmhelper.SM2Util.DOMAIN_PARAMS); boolean value = org.zz.gmhelper.SM2Util.verify(pubKey, content.getBytes(), sig); - if (value) ret.put("status", "success", false); - else ret.put("status", "failed", false); + if (value) + ret.put("status", "success", false); + else + ret.put("status", "failed", false); ret.put("result", value, false); } catch (Exception e) { ret.put("status", "failed", false); @@ -65,9 +64,8 @@ public class SM2Util { public static String encrypt(String content, String pubkey) { try { - return ByteUtils.toHexString( - org.zz.gmhelper.SM2Util.encrypt( - SM2KeyPair.publicKeyStr2ECPoint(pubkey), content.getBytes())); + return ByteUtils.toHexString(org.zz.gmhelper.SM2Util + .encrypt(SM2KeyPair.publicKeyStr2ECPoint(pubkey), content.getBytes())); } catch (InvalidCipherTextException e) { e.printStackTrace(); } @@ -76,12 +74,10 @@ public class SM2Util { public static String decrypt(String content, String privateKey) { try { - ECPrivateKeyParameters privateKeyParam = - new ECPrivateKeyParameters( - new BigInteger(privateKey, 16), org.zz.gmhelper.SM2Util.DOMAIN_PARAMS); - return new String( - org.zz.gmhelper.SM2Util.decrypt( - privateKeyParam, ByteUtils.fromHexString(content))); + ECPrivateKeyParameters privateKeyParam = new ECPrivateKeyParameters( + new BigInteger(privateKey, 16), org.zz.gmhelper.SM2Util.DOMAIN_PARAMS); + return new String(org.zz.gmhelper.SM2Util.decrypt(privateKeyParam, + ByteUtils.fromHexString(content))); } catch (InvalidCipherTextException e) { e.printStackTrace(); } diff --git a/src/main/java/org/bdware/sc/boundry/utils/SQLUtil.java b/src/main/java/org/bdware/sc/boundry/utils/SQLUtil.java index 12d33b2..ac713e1 100644 --- a/src/main/java/org/bdware/sc/boundry/utils/SQLUtil.java +++ b/src/main/java/org/bdware/sc/boundry/utils/SQLUtil.java @@ -16,9 +16,8 @@ public class SQLUtil { .setContextClassLoader(ContractProcess.instance.engine.getClassLoad()); Class.forName(driver, true, ContractProcess.instance.engine.getClassLoad()); } catch (Exception e) { - System.out.println( - "Still can't find class! Cl of SQLUtil:\n\t\t" - + SQLUtil.class.getClassLoader()); + System.out.println("Still can't find class! Cl of SQLUtil:\n\t\t" + + SQLUtil.class.getClassLoader()); System.out.println( "Cl of DEgine:\n\t\t" + ContractProcess.instance.engine.getClassLoad()); e.printStackTrace(); @@ -40,18 +39,15 @@ public class SQLUtil { if (password != null && !"undefined".equals(password)) { info.put("password", password); } - if (url.startsWith("jdbc:postgresql")) info.put("sslmode", "allow"); + if (url.startsWith("jdbc:postgresql")) + info.put("sslmode", "allow"); - Class clz = - Class.forName( - "java.sql.DriverManager", - true, - ContractProcess.instance.engine.getClassLoad()); + Class clz = Class.forName("java.sql.DriverManager", true, + ContractProcess.instance.engine.getClassLoad()); // set caller class into null, thus use YJSClassLoader in // DriverManager.isDriverAllowed(driver,classloader); - Method m = - clz.getDeclaredMethod( - "getConnection", String.class, Properties.class, Class.class); + Method m = clz.getDeclaredMethod("getConnection", String.class, Properties.class, + Class.class); m.setAccessible(true); return (Connection) m.invoke(null, url, info, null); } catch (Exception e) { diff --git a/src/main/java/org/bdware/sc/boundry/utils/UtilRegistry.java b/src/main/java/org/bdware/sc/boundry/utils/UtilRegistry.java index 77a7f9d..acbbb97 100644 --- a/src/main/java/org/bdware/sc/boundry/utils/UtilRegistry.java +++ b/src/main/java/org/bdware/sc/boundry/utils/UtilRegistry.java @@ -23,7 +23,8 @@ public class UtilRegistry { for (String name : allName) { Class clz; try { - clz = Class.forName(String.format("%s.%sUtil", UtilRegistry.class.getPackage().getName(), name)); + clz = Class.forName(String.format("%s.%sUtil", + UtilRegistry.class.getPackage().getName(), name)); ret.add(clz); } catch (Throwable e) { e.printStackTrace(); @@ -52,10 +53,8 @@ public class UtilRegistry { public static String getInitStr(String s, boolean open) { if (stubClzNameMap.containsKey(s)) { - String ret = - String.format( - "%sUtil = %s.%sUtil%s;\n", - s, UtilRegistry.class.getPackage().getName(), s, open ? "" : "Stub"); + String ret = String.format("%sUtil = %s.%sUtil%s;\n", s, + UtilRegistry.class.getPackage().getName(), s, open ? "" : "Stub"); return ret; } return ""; diff --git a/src/main/java/org/bdware/sc/compiler/AnnotationProcessor.java b/src/main/java/org/bdware/sc/compiler/AnnotationProcessor.java index 816b12a..d4b197e 100644 --- a/src/main/java/org/bdware/sc/compiler/AnnotationProcessor.java +++ b/src/main/java/org/bdware/sc/compiler/AnnotationProcessor.java @@ -9,8 +9,8 @@ public abstract class AnnotationProcessor { return; } - public void processFunction( - AnnotationNode anno, ContractNode contractNode, FunctionNode functionNode) throws Exception { + public void processFunction(AnnotationNode anno, ContractNode contractNode, + FunctionNode functionNode) throws Exception { return; } } diff --git a/src/main/java/org/bdware/sc/compiler/PermissionStub.java b/src/main/java/org/bdware/sc/compiler/PermissionStub.java index 0668ec5..3b9ee89 100644 --- a/src/main/java/org/bdware/sc/compiler/PermissionStub.java +++ b/src/main/java/org/bdware/sc/compiler/PermissionStub.java @@ -12,4 +12,4 @@ import java.lang.annotation.Target; public @interface PermissionStub { Permission permission(); -} \ No newline at end of file +} diff --git a/src/main/java/org/bdware/sc/compiler/PermissionStubGenerator.java b/src/main/java/org/bdware/sc/compiler/PermissionStubGenerator.java index b19c04a..2657bd3 100644 --- a/src/main/java/org/bdware/sc/compiler/PermissionStubGenerator.java +++ b/src/main/java/org/bdware/sc/compiler/PermissionStubGenerator.java @@ -34,7 +34,8 @@ public class PermissionStubGenerator extends ClassVisitor implements Opcodes { return null; } - public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) { + public void visit(int version, int access, String name, String signature, String superName, + String[] interfaces) { if (cv != null) { cv.visit(version, access, name + "Stub", null, "java/lang/Object", null); } @@ -53,7 +54,8 @@ public class PermissionStubGenerator extends ClassVisitor implements Opcodes { mv.visitEnd(); } - public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { + public MethodVisitor visitMethod(int access, String name, String desc, String signature, + String[] exceptions) { if (cv != null) { if (name.equals("")) return null; @@ -62,14 +64,15 @@ public class PermissionStubGenerator extends ClassVisitor implements Opcodes { } MethodVisitor mv = cv.visitMethod(access, name, desc, signature, - new String[]{"javax/script/ScriptException"}); + new String[] {"javax/script/ScriptException"}); mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitTypeInsn(NEW, "javax/script/ScriptException"); mv.visitInsn(DUP); mv.visitLdcInsn("Do not have " + p + " Permission"); - mv.visitMethodInsn(INVOKESPECIAL, "javax/script/ScriptException", "", "(Ljava/lang/String;)V"); + mv.visitMethodInsn(INVOKESPECIAL, "javax/script/ScriptException", "", + "(Ljava/lang/String;)V"); mv.visitInsn(ATHROW); mv.visitMaxs(5, 20); mv.visitEnd(); diff --git a/src/main/java/org/bdware/sc/compiler/YJSCompiler.java b/src/main/java/org/bdware/sc/compiler/YJSCompiler.java index f851bf4..d995aa2 100644 --- a/src/main/java/org/bdware/sc/compiler/YJSCompiler.java +++ b/src/main/java/org/bdware/sc/compiler/YJSCompiler.java @@ -34,8 +34,7 @@ public class YJSCompiler { ContractNode contract; private static final Logger LOGGER = LogManager.getLogger(YJSCompiler.class); - public YJSCompiler() { - } + public YJSCompiler() {} public static ScriptFunction compileWithGlobal(Source source, Global global, Context context) { Global oldGlobal = Context.getGlobal(); @@ -55,8 +54,8 @@ public class YJSCompiler { } } - private static Context makeContext( - final InputStream in, final OutputStream out, final OutputStream err) { + private static Context makeContext(final InputStream in, final OutputStream out, + final OutputStream err) { final PrintStream pout = out instanceof PrintStream ? (PrintStream) out : new PrintStream(out); final PrintStream perr = @@ -68,7 +67,7 @@ public class YJSCompiler { final ErrorManager errors = new ErrorManager(werr); // Set up options. final Options options = new Options("nashorn", werr); - options.process(new String[]{}); + options.process(new String[] {}); // detect scripting mode by any source's first character being '#' options.set("persistent.code.cache", true); options.set("print.code", "true"); @@ -92,14 +91,8 @@ public class YJSCompiler { } } } - return new Context( - options, - errors, - wout, - werr, - Thread.currentThread().getContextClassLoader(), - new YJSFilter(), - null); + return new Context(options, errors, wout, werr, + Thread.currentThread().getContextClassLoader(), new YJSFilter(), null); } public ContractZipBundle compile(ZipFile zf) throws Exception { @@ -115,9 +108,8 @@ public class YJSCompiler { // Gson gson = new GsonBuilder().registerTypeAdapter(Contract.Type.class, // typeAdapter) - ContractManifest cm = - JsonUtil.GSON.fromJson( - new InputStreamReader(manifestInput), ContractManifest.class); + ContractManifest cm = JsonUtil.GSON.fromJson(new InputStreamReader(manifestInput), + ContractManifest.class); // 如果没有就不限制,根据gas进行插装 if (0L != cm.getInsnLimit()) { LOGGER.info("++++++++++++++++++++++true"); @@ -128,7 +120,7 @@ public class YJSCompiler { Set todo = new HashSet<>(); Set allEntries = new HashSet<>(); Enumeration iter = zf.entries(); - for (; iter.hasMoreElements(); ) { + for (; iter.hasMoreElements();) { ZipEntry ele = iter.nextElement(); if (ele != null) allEntries.add(ele.getName()); @@ -161,23 +153,21 @@ public class YJSCompiler { // add function _preSub // Kaidong Wu String preSubConName = cm.main.substring(0, cm.main.length() - 4) + "PreSub"; - String preSubContract = - "contract " - + preSubConName - + " { function _preSub (e) { YancloudUtil.preSub(e.topic, e.content); }}"; + String preSubContract = "contract " + preSubConName + + " { function _preSub (e) { YancloudUtil.preSub(e.topic, e.content); }}"; ContractNode preSubNode = - compile( - new ByteArrayInputStream(preSubContract.getBytes(StandardCharsets.UTF_8)), + compile(new ByteArrayInputStream(preSubContract.getBytes(StandardCharsets.UTF_8)), preSubConName + ".yjs"); czb.put(preSubConName + ".yjs", preSubNode); LOGGER.info("--compile-- " + preSubConName); String globalBeanName = cm.main.substring(0, cm.main.length() - 4) + "GlobalBean"; - String globalBeanContract = - "contract " + globalBeanName + "{ function setGlobal (_global) { Global = _global; }\n" + - " function getGlobal () { return Global; }}"; + String globalBeanContract = "contract " + globalBeanName + + "{ function setGlobal (_global) { Global = _global; }\n" + + " function getGlobal () { return Global; }}"; czb.put(globalBeanName + ".yjs", - compile(new ByteArrayInputStream(globalBeanContract.getBytes(StandardCharsets.UTF_8)), + compile(new ByteArrayInputStream( + globalBeanContract.getBytes(StandardCharsets.UTF_8)), globalBeanName + ".yjs")); LOGGER.info("--compile-- " + globalBeanName); czb.setMergedContractNode(); @@ -202,7 +192,7 @@ public class YJSCompiler { contract = reader.visitProgram(tree); // 遍历完 获取 contract 里的 yjs type contract.initPlainText(cts); - handleModuleAnnotation(contract);//处理注解 + handleModuleAnnotation(contract);// 处理注解 handleFunctionAnnotation(contract); return contract; } @@ -219,7 +209,7 @@ public class YJSCompiler { private void handleFunctionAnnotation(ContractNode contractNode) throws Exception { for (FunctionNode functionNode : contractNode.getFunctions()) { - List annos = functionNode.annotations;//函数里的annotation + List annos = functionNode.annotations;// 函数里的annotation if (annos != null) for (AnnotationNode anno : annos) { AnnotationProcessor processor = findProcessor(anno); diff --git a/src/main/java/org/bdware/sc/compiler/ap/Confidential.java b/src/main/java/org/bdware/sc/compiler/ap/Confidential.java index 8a7a655..84cef27 100644 --- a/src/main/java/org/bdware/sc/compiler/ap/Confidential.java +++ b/src/main/java/org/bdware/sc/compiler/ap/Confidential.java @@ -7,6 +7,8 @@ import org.bdware.sc.node.FunctionNode; public class Confidential extends AnnotationProcessor { @Override - public void processFunction(AnnotationNode anno, ContractNode contractNode, FunctionNode functionNode) { - functionNode.setConfidential(true); } + public void processFunction(AnnotationNode anno, ContractNode contractNode, + FunctionNode functionNode) { + functionNode.setConfidential(true); + } } diff --git a/src/main/java/org/bdware/sc/compiler/ap/Cost.java b/src/main/java/org/bdware/sc/compiler/ap/Cost.java index 99fa2f6..2438cd3 100644 --- a/src/main/java/org/bdware/sc/compiler/ap/Cost.java +++ b/src/main/java/org/bdware/sc/compiler/ap/Cost.java @@ -9,8 +9,8 @@ import org.bdware.sc.util.JsonUtil; public class Cost extends AnnotationProcessor { @Override - public void processFunction( - AnnotationNode anno, ContractNode contractNode, FunctionNode functionNode) { + public void processFunction(AnnotationNode anno, ContractNode contractNode, + FunctionNode functionNode) { CostDetail detail = JsonUtil.fromJson(anno.getArgs().get(0), CostDetail.class); functionNode.setCost(detail); if (detail.isCountGas()) diff --git a/src/main/java/org/bdware/sc/compiler/ap/DOOP.java b/src/main/java/org/bdware/sc/compiler/ap/DOOP.java index 16e0451..99803c2 100644 --- a/src/main/java/org/bdware/sc/compiler/ap/DOOP.java +++ b/src/main/java/org/bdware/sc/compiler/ap/DOOP.java @@ -10,7 +10,8 @@ import org.bdware.sc.node.FunctionNode; // DOOP is designed for DoipModule which contains specific functions for RepositoryHandler public class DOOP extends AnnotationProcessor { @Override - public void processFunction(AnnotationNode anno, ContractNode contractNode, FunctionNode functionNode) throws Exception { + public void processFunction(AnnotationNode anno, ContractNode contractNode, + FunctionNode functionNode) throws Exception { // 通过DOOP注解,解析对应的值,并放进对应的FunctionNode中 functionNode.setIsExport(true); functionNode.setIsDoipOperation(true); diff --git a/src/main/java/org/bdware/sc/compiler/ap/HomomorphicDecrypt.java b/src/main/java/org/bdware/sc/compiler/ap/HomomorphicDecrypt.java index d1d9309..39a7c77 100644 --- a/src/main/java/org/bdware/sc/compiler/ap/HomomorphicDecrypt.java +++ b/src/main/java/org/bdware/sc/compiler/ap/HomomorphicDecrypt.java @@ -8,7 +8,8 @@ import org.bdware.sc.node.FunctionNode; public class HomomorphicDecrypt extends AnnotationProcessor { @Override - public void processFunction(AnnotationNode anno, ContractNode contractNode, FunctionNode functionNode) { + public void processFunction(AnnotationNode anno, ContractNode contractNode, + FunctionNode functionNode) { functionNode.setHomomorphicDecrypt(true); functionNode.setKeyManagerID(anno.getArgs().get(0)); functionNode.setSecretID(anno.getArgs().get(1)); diff --git a/src/main/java/org/bdware/sc/compiler/ap/HomomorphicEncrypt.java b/src/main/java/org/bdware/sc/compiler/ap/HomomorphicEncrypt.java index 2e8109b..ace3c62 100644 --- a/src/main/java/org/bdware/sc/compiler/ap/HomomorphicEncrypt.java +++ b/src/main/java/org/bdware/sc/compiler/ap/HomomorphicEncrypt.java @@ -8,7 +8,8 @@ import org.bdware.sc.node.FunctionNode; public class HomomorphicEncrypt extends AnnotationProcessor { @Override - public void processFunction(AnnotationNode anno, ContractNode contractNode, FunctionNode functionNode) { + public void processFunction(AnnotationNode anno, ContractNode contractNode, + FunctionNode functionNode) { functionNode.setHomomorphicEncrypt(true); functionNode.setKeyManagerID(anno.getArgs().get(0)); functionNode.setSecretID(anno.getArgs().get(1)); diff --git a/src/main/java/org/bdware/sc/compiler/ap/Join.java b/src/main/java/org/bdware/sc/compiler/ap/Join.java index 4820828..f2bbfb4 100644 --- a/src/main/java/org/bdware/sc/compiler/ap/Join.java +++ b/src/main/java/org/bdware/sc/compiler/ap/Join.java @@ -8,10 +8,10 @@ import org.bdware.sc.node.FunctionNode; public class Join extends AnnotationProcessor { @Override - public void processFunction( - AnnotationNode anno, ContractNode contractNode, FunctionNode functionNode) { - // functionNode.setRouteInfo(RouteInfo.create(anno,contractNode)); - //增加标记,在ContractNode中记录Join相关的函数和Join规则 - functionNode.setJoinInfo(JoinInfo.create(anno,contractNode)); + public void processFunction(AnnotationNode anno, ContractNode contractNode, + FunctionNode functionNode) { + // functionNode.setRouteInfo(RouteInfo.create(anno,contractNode)); + // 增加标记,在ContractNode中记录Join相关的函数和Join规则 + functionNode.setJoinInfo(JoinInfo.create(anno, contractNode)); } } diff --git a/src/main/java/org/bdware/sc/compiler/ap/LogLocation.java b/src/main/java/org/bdware/sc/compiler/ap/LogLocation.java index 8f08e95..37f5d70 100644 --- a/src/main/java/org/bdware/sc/compiler/ap/LogLocation.java +++ b/src/main/java/org/bdware/sc/compiler/ap/LogLocation.java @@ -14,12 +14,11 @@ public class LogLocation extends AnnotationProcessor { } @Override - public void processFunction( - AnnotationNode anno, ContractNode contractNode, FunctionNode functionNode) { + public void processFunction(AnnotationNode anno, ContractNode contractNode, + FunctionNode functionNode) { if (anno != null && anno.getArgs() != null) for (String s : anno.getArgs()) { - if (s.equals("\"dataware\"") - || s.equals("\"bdledger\"") + if (s.equals("\"dataware\"") || s.equals("\"bdledger\"") || s.equals("\"bdledger:\"")) { functionNode.setLogToBDContract(true); } else if (s.startsWith("\"bdledger:") && s.length() > 11) { diff --git a/src/main/java/org/bdware/sc/compiler/ap/LogType.java b/src/main/java/org/bdware/sc/compiler/ap/LogType.java index 87ef97e..9574e00 100644 --- a/src/main/java/org/bdware/sc/compiler/ap/LogType.java +++ b/src/main/java/org/bdware/sc/compiler/ap/LogType.java @@ -15,12 +15,13 @@ public class LogType extends AnnotationProcessor { } @Override - public void processFunction( - AnnotationNode anno, ContractNode contractNode, FunctionNode functionNode) { + public void processFunction(AnnotationNode anno, ContractNode contractNode, + FunctionNode functionNode) { for (String str : anno.getArgs()) { org.bdware.sc.node.LogType type = org.bdware.sc.node.LogType.parse(str); functionNode.addLogType(type); - if (type == org.bdware.sc.node.LogType.Branch) contractNode.setInstrumentBranch(true); + if (type == org.bdware.sc.node.LogType.Branch) + contractNode.setInstrumentBranch(true); } } } diff --git a/src/main/java/org/bdware/sc/compiler/ap/Route.java b/src/main/java/org/bdware/sc/compiler/ap/Route.java index 7714e85..554098c 100644 --- a/src/main/java/org/bdware/sc/compiler/ap/Route.java +++ b/src/main/java/org/bdware/sc/compiler/ap/Route.java @@ -1,17 +1,15 @@ package org.bdware.sc.compiler.ap; -import com.google.gson.Gson; import org.bdware.sc.bean.RouteInfo; import org.bdware.sc.compiler.AnnotationProcessor; import org.bdware.sc.node.AnnotationNode; import org.bdware.sc.node.ContractNode; -import org.bdware.sc.node.CostDetail; import org.bdware.sc.node.FunctionNode; public class Route extends AnnotationProcessor { @Override - public void processFunction( - AnnotationNode anno, ContractNode contractNode, FunctionNode functionNode) { - functionNode.setRouteInfo(RouteInfo.create(anno,contractNode)); + public void processFunction(AnnotationNode anno, ContractNode contractNode, + FunctionNode functionNode) { + functionNode.setRouteInfo(RouteInfo.create(anno, contractNode)); } } diff --git a/src/main/java/org/bdware/sc/compiler/ap/Split.java b/src/main/java/org/bdware/sc/compiler/ap/Split.java index a720204..edeeda5 100644 --- a/src/main/java/org/bdware/sc/compiler/ap/Split.java +++ b/src/main/java/org/bdware/sc/compiler/ap/Split.java @@ -9,8 +9,8 @@ import org.bdware.sc.node.FunctionNode; public class Split extends AnnotationProcessor { @Override - public void processFunction( - AnnotationNode anno, ContractNode contractNode, FunctionNode functionNode) { - functionNode.setRouteInfo(RouteInfo.create(anno,contractNode)); + public void processFunction(AnnotationNode anno, ContractNode contractNode, + FunctionNode functionNode) { + functionNode.setRouteInfo(RouteInfo.create(anno, contractNode)); } } diff --git a/src/main/java/org/bdware/sc/crdt/SharableVar.java b/src/main/java/org/bdware/sc/crdt/SharableVar.java index a2755e4..e724d9f 100644 --- a/src/main/java/org/bdware/sc/crdt/SharableVar.java +++ b/src/main/java/org/bdware/sc/crdt/SharableVar.java @@ -13,14 +13,15 @@ import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit; public class SharableVar { - public final static HashedWheelTimer HASHED_WHEEL_TIMER = new HashedWheelTimer(new ThreadFactory() { - @Override - public Thread newThread(Runnable r) { - Thread t = Executors.defaultThreadFactory().newThread(r); - t.setDaemon(true); - return t; - } - }, 5, TimeUnit.MILLISECONDS, 2); + public final static HashedWheelTimer HASHED_WHEEL_TIMER = + new HashedWheelTimer(new ThreadFactory() { + @Override + public Thread newThread(Runnable r) { + Thread t = Executors.defaultThreadFactory().newThread(r); + t.setDaemon(true); + return t; + } + }, 5, TimeUnit.MILLISECONDS, 2); private final String varId; private long interval; @@ -31,7 +32,8 @@ public class SharableVar { private SyncTimeout nextTimeOut; - public SharableVar(String cpId, String identifier, SharableVarManager.VarResolveResult resolveResult) { + public SharableVar(String cpId, String identifier, + SharableVarManager.VarResolveResult resolveResult) { counter = new GCounter(cpId, identifier); myId = cpId; varId = identifier; @@ -70,7 +72,7 @@ public class SharableVar { private void initByVarResolve(SharableVarManager.VarResolveResult resolveResult) { sendTo = new ArrayList<>(); - //假设没有同一个人既是reader又是writer。 + // 假设没有同一个人既是reader又是writer。 offset = -1; for (int i = 0; i < resolveResult.writer.length; i++) { if (myId.equals(resolveResult.writer[i])) { diff --git a/src/main/java/org/bdware/sc/crdt/SharableVarManager.java b/src/main/java/org/bdware/sc/crdt/SharableVarManager.java index 6cd1c34..da6392e 100644 --- a/src/main/java/org/bdware/sc/crdt/SharableVarManager.java +++ b/src/main/java/org/bdware/sc/crdt/SharableVarManager.java @@ -49,7 +49,8 @@ public class SharableVarManager { String content = message.header.parameters.attributes.get("content").getAsString(); SharableVar var = getVar(varId); var.join(content); - DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder(); + DoipMessageFactory.DoipMessageBuilder builder = + new DoipMessageFactory.DoipMessageBuilder(); builder.createResponse(DoipResponseCode.Success, message); builder.addAttributes("msg", "success"); return builder.create(); @@ -57,7 +58,8 @@ public class SharableVarManager { ByteArrayOutputStream bo = new ByteArrayOutputStream(); e.printStackTrace(); e.printStackTrace(new PrintStream(bo)); - DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder(); + DoipMessageFactory.DoipMessageBuilder builder = + new DoipMessageFactory.DoipMessageBuilder(); builder.createResponse(DoipResponseCode.UnKnownError, message); builder.addAttributes("exception", bo.toString()); return builder.create(); @@ -83,7 +85,8 @@ public class SharableVarManager { client.sendMessage(doipMessage, new DoipMessageCallback() { @Override public void onResult(DoipMessage doipMessage) { - LOGGER.info("RECV Sync:" + JsonUtil.toJson(JsonDoipMessage.fromDoipMessage(doipMessage))); + LOGGER.info("RECV Sync:" + + JsonUtil.toJson(JsonDoipMessage.fromDoipMessage(doipMessage))); } }); } @@ -104,14 +107,18 @@ public class SharableVarManager { public synchronized SharableVar createVar(String identifier, String type) { try { - if (allVars.containsKey(identifier)) return allVars.get(identifier); + if (allVars.containsKey(identifier)) + return allVars.get(identifier); StateInfoBase stateInfoBase = client.resolve(identifier); - if (stateInfoBase.handleValues.has("bdwType") && stateInfoBase.handleValues.get("bdwType").getAsString().equals("SharableVar")) { - VarResolveResult resolveResult = JsonUtil.fromJson(stateInfoBase.handleValues, VarResolveResult.class); + if (stateInfoBase.handleValues.has("bdwType") && stateInfoBase.handleValues + .get("bdwType").getAsString().equals("SharableVar")) { + VarResolveResult resolveResult = + JsonUtil.fromJson(stateInfoBase.handleValues, VarResolveResult.class); SharableVar sharableVar = new SharableVar(cpId, identifier, resolveResult); allVars.put(identifier, sharableVar); return sharableVar; - } else return null; + } else + return null; } catch (Exception e) { e.printStackTrace(); return null; diff --git a/src/main/java/org/bdware/sc/engine/ConfidentialContractUtil.java b/src/main/java/org/bdware/sc/engine/ConfidentialContractUtil.java index e327c65..35353af 100644 --- a/src/main/java/org/bdware/sc/engine/ConfidentialContractUtil.java +++ b/src/main/java/org/bdware/sc/engine/ConfidentialContractUtil.java @@ -23,23 +23,26 @@ public class ConfidentialContractUtil { public static final String CONFIDENTIAL_TEMPLATE_PATH = System.getenv("GRAPHENE_DIR") + File.separator + "Examples" + File.separator + "nodejs-secret"; - public static final String CONFIDENTIAL_SCRIPT_PATH = System.getenv("GRAPHENE_DIR") - + File.separator + "App"; + public static final String CONFIDENTIAL_SCRIPT_PATH = + System.getenv("GRAPHENE_DIR") + File.separator + "App"; public static final String[] COMMAND = {"bash", "executeContract.sh"}; - private static final Type MapType = TypeToken.getParameterized(HashMap.class, String.class, String.class).getType(); + private static final Type MapType = + TypeToken.getParameterized(HashMap.class, String.class, String.class).getType(); - public static String executeConfidentialContract(ContractRequest input) throws IOException, InterruptedException { + public static String executeConfidentialContract(ContractRequest input) + throws IOException, InterruptedException { File runDir = new File(CONFIDENTIAL_SCRIPT_PATH + File.separator + input.getRequestID()); ProcessBuilder pb = new ProcessBuilder(COMMAND); pb.directory(runDir); Process p = pb.start(); p.waitFor(); - File resultFile = new File(CONFIDENTIAL_SCRIPT_PATH + File.separator + input.getRequestID() + - File.separator + "result.json"); + File resultFile = new File(CONFIDENTIAL_SCRIPT_PATH + File.separator + input.getRequestID() + + File.separator + "result.json"); return FileUtils.readFileToString(resultFile, StandardCharsets.UTF_8); } - public static void generateConfidentialContract(ContractNode cn, ScriptObjectMirror globalVars, Global global) { + public static void generateConfidentialContract(ContractNode cn, ScriptObjectMirror globalVars, + Global global) { List functionNodes = cn.getFunctions(); for (FunctionNode fn : functionNodes) { // assuming only one confidential function for now @@ -54,18 +57,46 @@ public class ConfidentialContractUtil { } } // load necessary Node.js libraries - jsStr.append("var fs = require('fs');\n" + "var crypto = require('crypto');\n" + "var sm2 = require('sm-crypto').sm2;\n"); + jsStr.append("var fs = require('fs');\n" + "var crypto = require('crypto');\n" + + "var sm2 = require('sm-crypto').sm2;\n"); // load Global variables and arguments from files - jsStr.append("let rawGlobal = fs.readFileSync('global.json').toString();\n" + "let Global = JSON.parse(rawGlobal);\n"); - jsStr.append("let rawArg = fs.readFileSync('arg.json').toString();\n" + "let jsonArg = JSON.parse(rawArg);\n" + "let requester = jsonArg.requester;\n" + "let arg = jsonArg.arg;\n"); + jsStr.append("let rawGlobal = fs.readFileSync('global.json').toString();\n" + + "let Global = JSON.parse(rawGlobal);\n"); + jsStr.append("let rawArg = fs.readFileSync('arg.json').toString();\n" + + "let jsonArg = JSON.parse(rawArg);\n" + + "let requester = jsonArg.requester;\n" + "let arg = jsonArg.arg;\n"); jsStr.append("let srcStr = fs.readFileSync('contract.js').toString();\n"); // verify signatures and decrypt all confidential variables Important!!!!! - jsStr.append("for (var k in Global) {\n" + " if (Global.hasOwnProperty(k)) {\n" + " if (k.startsWith('conf_')) {\n" + " let sig = Global[k].signature;\n" + " let pubKey = Global[k].owner;\n" + " let verifyResult = sm2.doVerifySignature(srcStr, sig, pubKey);\n" + " if (verifyResult) {\n" + " let newKey = k.substring(5);\n" + " let decKey = Buffer.from(process.env['KEY_'+pubKey.substring(0,10).toUpperCase()], 'hex');\n" + " let decIv = Buffer.from(Global[k].iv, 'hex');\n" + " let cipherText = Buffer.from(Global[k].cipherText, 'hex');\n" + " let decipher = crypto.createDecipheriv('aes-256-cbc', decKey, decIv);\n" + " let decrypted = decipher.update(cipherText);\n" + " decrypted = Buffer.concat([decrypted, decipher.final()]);\n" + " let plaintext = decrypted.toString();\n" + " Global[newKey] = plaintext;\n" + " }\n" + " }\n" + " }\n" + "}\n"); + jsStr.append("for (var k in Global) {\n" + " if (Global.hasOwnProperty(k)) {\n" + + " if (k.startsWith('conf_')) {\n" + + " let sig = Global[k].signature;\n" + + " let pubKey = Global[k].owner;\n" + + " let verifyResult = sm2.doVerifySignature(srcStr, sig, pubKey);\n" + + " if (verifyResult) {\n" + " let newKey = k.substring(5);\n" + + " let decKey = Buffer.from(process.env['KEY_'+pubKey.substring(0,10).toUpperCase()], 'hex');\n" + + " let decIv = Buffer.from(Global[k].iv, 'hex');\n" + + " let cipherText = Buffer.from(Global[k].cipherText, 'hex');\n" + + " let decipher = crypto.createDecipheriv('aes-256-cbc', decKey, decIv);\n" + + " let decrypted = decipher.update(cipherText);\n" + + " decrypted = Buffer.concat([decrypted, decipher.final()]);\n" + + " let plaintext = decrypted.toString();\n" + + " Global[newKey] = plaintext;\n" + " }\n" + " }\n" + + " }\n" + "}\n"); // call function - jsStr.append("var ret = ").append(fn.functionName).append("(arg, requester, null);\n"); - // TODO: encrypt all confidential variables so state can be updated in confidential function @shujunyi + jsStr.append("var ret = ").append(fn.functionName) + .append("(arg, requester, null);\n"); + // TODO: encrypt all confidential variables so state can be updated in confidential + // function @shujunyi // encrypt return value and write to a file - jsStr.append("var retStr = JSON.stringify(ret);\n" + "var key = Buffer.from(process.env['KEY_'+requester.substring(0,10).toUpperCase()], 'hex');\n" + "var iv = crypto.randomBytes(16);\n" + "let cipher = crypto.createCipheriv('aes-256-cbc', key, iv); \n" + "let encRet = cipher.update(retStr);\n" + "encRet = Buffer.concat([encRet, cipher.final()]);\n" + "let result = {iv: iv.toString('hex'), encryptedData: encRet.toString('hex')};\n" + "let resultStr = JSON.stringify(result);\n" + "fs.writeFileSync('result.json', resultStr);\n"); + jsStr.append("var retStr = JSON.stringify(ret);\n" + + "var key = Buffer.from(process.env['KEY_'+requester.substring(0,10).toUpperCase()], 'hex');\n" + + "var iv = crypto.randomBytes(16);\n" + + "let cipher = crypto.createCipheriv('aes-256-cbc', key, iv); \n" + + "let encRet = cipher.update(retStr);\n" + + "encRet = Buffer.concat([encRet, cipher.final()]);\n" + + "let result = {iv: iv.toString('hex'), encryptedData: encRet.toString('hex')};\n" + + "let resultStr = JSON.stringify(result);\n" + + "fs.writeFileSync('result.json', resultStr);\n"); // put script into Global so owner can send it and collect signatures Object som = ScriptObjectMirror.wrap(jsStr.toString(), global); globalVars.put("src_" + fn.functionName, som); @@ -85,11 +116,8 @@ public class ConfidentialContractUtil { } } - public static void dumpScriptAndStates( - Invocable engine, - FunctionNode functionNode, - ContractRequest input, - ScriptObjectMirror globalVars) + public static void dumpScriptAndStates(Invocable engine, FunctionNode functionNode, + ContractRequest input, ScriptObjectMirror globalVars) throws IOException, ScriptException, NoSuchMethodException { Map globalMap = (Map) convertIntoJavaObject(globalVars); String dest = CONFIDENTIAL_SCRIPT_PATH + File.separator + input.getRequestID(); @@ -136,156 +164,116 @@ public class ConfidentialContractUtil { return dependentFunctions; } - private static String generateGrapheneManifestStr(Invocable engine, ContractRequest input) throws ScriptException, NoSuchMethodException { - String manifestStr = "# Nodejs manifest file example\n" + - "#\n" + - "# This manifest was prepared and tested on Ubuntu 18.04.\n" + - "\n" + - "loader.argv0_override = \"nodejs\"\n" + - "\n" + - "# LibOS layer library of Graphene. There is currently only one implementation,\n" + - "# so it is always set to libsysdb.so.\n" + - "loader.preload = \"file:$(GRAPHENEDIR)/Runtime/libsysdb.so\"\n" + - "\n" + - "# Show/hide debug log of Graphene ('inline' or 'none' respectively).\n" + - "loader.debug_type = \"$(GRAPHENEDEBUG)\"\n" + - "\n" + - "# Read application arguments directly from the command line. Don't use this on production!\n" + - "loader.insecure__use_cmdline_argv = 1\n" + - "\n" + - "# Specify paths to search for libraries. The usual LD_LIBRARY_PATH syntax\n" + - "# applies. Paths must be in-Graphene visible paths, not host-OS paths (i.e.,\n" + - "# paths must be taken from fs.mount.xxx.path, not fs.mount.xxx.uri).\n" + - "loader.env.LD_LIBRARY_PATH = \"/lib:/usr/lib:$(ARCH_LIBDIR):/usr/$(ARCH_LIBDIR):./\"\n" + - "\n" + - "# Mount host-OS directory to required libraries (in 'uri') into in-Graphene\n" + - "# visible directory /lib (in 'path').\n" + - "fs.mount.lib.type = \"chroot\"\n" + - "fs.mount.lib.path = \"/lib\"\n" + - "fs.mount.lib.uri = \"file:$(GRAPHENEDIR)/Runtime\"\n" + - "\n" + - "fs.mount.lib2.type = \"chroot\"\n" + - "fs.mount.lib2.path = \"$(ARCH_LIBDIR)\"\n" + - "fs.mount.lib2.uri = \"file:$(ARCH_LIBDIR)\"\n" + - "\n" + - "#fs.mount.lib3.type = \"chroot\"\n" + - "#fs.mount.lib3.path = \"/usr/$(ARCH_LIBDIR)\"\n" + - "#fs.mount.lib3.uri = \"file:/usr/$(ARCH_LIBDIR)\"\n" + - "\n" + - "fs.mount.usr.type = \"chroot\"\n" + - "fs.mount.usr.path = \"/usr\"\n" + - "fs.mount.usr.uri = \"file:/usr\"\n" + - "\n" + - "# Host-level directory to NSS files required by Glibc + NSS libs\n" + - "fs.mount.etc.type = \"chroot\"\n" + - "fs.mount.etc.path = \"/etc\"\n" + - "fs.mount.etc.uri = \"file:/etc\"\n" + - "\n" + - "# Workload needs to create temporary files\n" + - "fs.mount.tmp.type = \"chroot\"\n" + - "fs.mount.tmp.path = \"/tmp\"\n" + - "fs.mount.tmp.uri = \"file:/tmp\"\n" + - "\n" + - "# Set enclave size to 2GB; NodeJS expects around 1.7GB of heap on startup,\n" + - "# see e.g. https://github.com/nodejs/node/issues/13018.\n" + - "# Recall that SGX v1 requires to specify enclave size at enclave creation time.\n" + - "sgx.enclave_size = \"2G\"\n" + - "\n" + - "# Set maximum number of in-enclave threads (somewhat arbitrarily) to 8. Recall\n" + - "# that SGX v1 requires to specify the maximum number of simultaneous threads at\n" + - "# enclave creation time.\n" + - "sgx.thread_num = 16\n" + - "\n" + - "# Specify all libraries used by Node.js and its dependencies (including all libs\n" + - "# which can be loaded at runtime via dlopen).\n" + - "sgx.trusted_files.ld = \"file:$(GRAPHENEDIR)/Runtime/ld-linux-x86-64.so.2\"\n" + - "sgx.trusted_files.libc = \"file:$(GRAPHENEDIR)/Runtime/libc.so.6\"\n" + - "sgx.trusted_files.libm = \"file:$(GRAPHENEDIR)/Runtime/libm.so.6\"\n" + - "sgx.trusted_files.libdl = \"file:$(GRAPHENEDIR)/Runtime/libdl.so.2\"\n" + - "sgx.trusted_files.librt = \"file:$(GRAPHENEDIR)/Runtime/librt.so.1\"\n" + - "sgx.trusted_files.libutil = \"file:$(GRAPHENEDIR)/Runtime/libutil.so.1\"\n" + - "sgx.trusted_files.libpthread = \"file:$(GRAPHENEDIR)/Runtime/libpthread.so.0\"\n" + - "sgx.trusted_files.libnssdns = \"file:$(GRAPHENEDIR)/Runtime/libnss_dns.so.2\"\n" + - "sgx.trusted_files.libresolv = \"file:$(GRAPHENEDIR)/Runtime/libresolv.so.2\"\n" + - "\n" + - "sgx.trusted_files.libstdc = \"file:/usr/$(ARCH_LIBDIR)/libstdc++.so.6\"\n" + - "sgx.trusted_files.libgccs = \"file:$(ARCH_LIBDIR)/libgcc_s.so.1\"\n" + - "sgx.trusted_files.libaptpkg = \"file:/usr/$(ARCH_LIBDIR)/libapt-pkg.so.5.0\"\n" + - "sgx.trusted_files.liblz4 = \"file:/usr/$(ARCH_LIBDIR)/liblz4.so.1\"\n" + - "sgx.trusted_files.libsystemd = \"file:$(ARCH_LIBDIR)/libsystemd.so.0\"\n" + - "sgx.trusted_files.libselinux = \"file:$(ARCH_LIBDIR)/libselinux.so.1\"\n" + - "sgx.trusted_files.libgcrypt = \"file:$(ARCH_LIBDIR)/libgcrypt.so.20\"\n" + - "sgx.trusted_files.libpcre = \"file:$(ARCH_LIBDIR)/libpcre.so.3\"\n" + - "sgx.trusted_files.libgpgerror = \"file:$(ARCH_LIBDIR)/libgpg-error.so.0\"\n" + - "sgx.trusted_files.libexpat = \"file:$(ARCH_LIBDIR)/libexpat.so.1\"\n" + - "sgx.trusted_files.libz = \"file:$(ARCH_LIBDIR)/libz.so.1\"\n" + - "sgx.trusted_files.libz2 = \"file:$(ARCH_LIBDIR)/libbz2.so.1.0\"\n" + - "sgx.trusted_files.liblzma = \"file:$(ARCH_LIBDIR)/liblzma.so.5\"\n" + - "sgx.trusted_files.libmpdec = \"file:/usr/$(ARCH_LIBDIR)/libmpdec.so.2\"\n" + - "\n" + - "# Name Service Switch (NSS) libraries (Glibc dependencies)\n" + - "sgx.trusted_files.libnssfiles = \"file:$(ARCH_LIBDIR)/libnss_files.so.2\"\n" + - "sgx.trusted_files.libnsscompat = \"file:$(ARCH_LIBDIR)/libnss_compat.so.2\"\n" + - "sgx.trusted_files.libnssnis = \"file:$(ARCH_LIBDIR)/libnss_nis.so.2\"\n" + - "sgx.trusted_files.libnsl = \"file:$(ARCH_LIBDIR)/libnsl.so.1\"\n" + - "sgx.trusted_files.libnssmyhostname = \"file:$(ARCH_LIBDIR)/libnss_myhostname.so.2\"\n" + - "sgx.trusted_files.libnssmdns = \"file:$(ARCH_LIBDIR)/libnss_mdns4_minimal.so.2\"\n" + - "\n" + - "# Scratch space\n" + - "sgx.allowed_files.tmp = \"file:/tmp\"\n" + - "\n" + - "# APT config files\n" + - "sgx.allowed_files.aptconfd = \"file:/etc/apt/apt.conf.d\"\n" + - "sgx.allowed_files.aptconf = \"file:/etc/apt/apt.conf\"\n" + - "sgx.allowed_files.apport = \"file:/etc/default/apport\"\n" + - "\n" + - "# Name Service Switch (NSS) files (Glibc reads these files)\n" + - "sgx.allowed_files.nsswitch = \"file:/etc/nsswitch.conf\"\n" + - "sgx.allowed_files.group = \"file:/etc/group\"\n" + - "sgx.allowed_files.passwd = \"file:/etc/passwd\"\n" + - "\n" + - "# DNS hostname resolution files (Glibc reads these files)\n" + - "sgx.allowed_files.hostconf = \"file:/etc/host.conf\"\n" + - "sgx.allowed_files.hosts = \"file:/etc/hosts\"\n" + - "sgx.allowed_files.gaiconf = \"file:/etc/gai.conf\"\n" + - "sgx.allowed_files.resolv = \"file:/etc/resolv.conf\"\n" + - "\n" + - "sgx.allowed_files.openssl = \"file:/etc/ssl/openssl.cnf\"\n" + - "\n" + - "# System's file system table\n" + - "sgx.allowed_files.fstab = \"file:/etc/fstab\"\n" + - "\n" + - "$(NODEJS_TRUSTED_LIBS)\n" + - "\n" + - "# JavaScript (trusted)\n" + - "sgx.allowed_files.smlib = \"file:node_modules\"\n" + - "sgx.trusted_files.npminfo = \"file:package.json\"\n" + - "sgx.trusted_files.contract = \"file:contract.js\"\n" + - "sgx.trusted_files.globaljson = \"file:global.json\"\n" + - "sgx.trusted_files.argjson = \"file:arg.json\"\n" + - "\n" + - "sys.insecure__allow_eventfd = 1\n" + - "\n" + - "sgx.remote_attestation = 1\n" + - "\n" + - "loader.env.LD_PRELOAD = \"libsecret_prov_attest.so\"\n" + - "loader.env.SECRET_PROVISION_CONSTRUCTOR = \"1\"\n" + - "loader.env.SECRET_PROVISION_SET_PF_KEY = \"1\"\n" + - "loader.env.SECRET_PROVISION_CA_CHAIN_PATH = \"certs/test-ca-sha256.crt\"\n" + - "loader.env.SECRET_PROVISION_SERVERS = \"localhost:4433\"\n" + - "\n" + - "sgx.trusted_files.libsecretprovattest = \"file:libsecret_prov_attest.so\"\n" + - "sgx.trusted_files.cachain = \"file:certs/test-ca-sha256.crt\"\n" + - "\n" + - "# Specify your SPID and linkable/unlinkable attestation policy\n" + - "sgx.ra_client_spid = \"DF3A8BA098E93F66CC64E8A215E98333\"\n" + - "sgx.ra_client_linkable = 0\n"; + private static String generateGrapheneManifestStr(Invocable engine, ContractRequest input) + throws ScriptException, NoSuchMethodException { + String manifestStr = "# Nodejs manifest file example\n" + "#\n" + + "# This manifest was prepared and tested on Ubuntu 18.04.\n" + "\n" + + "loader.argv0_override = \"nodejs\"\n" + "\n" + + "# LibOS layer library of Graphene. There is currently only one implementation,\n" + + "# so it is always set to libsysdb.so.\n" + + "loader.preload = \"file:$(GRAPHENEDIR)/Runtime/libsysdb.so\"\n" + "\n" + + "# Show/hide debug log of Graphene ('inline' or 'none' respectively).\n" + + "loader.debug_type = \"$(GRAPHENEDEBUG)\"\n" + "\n" + + "# Read application arguments directly from the command line. Don't use this on production!\n" + + "loader.insecure__use_cmdline_argv = 1\n" + "\n" + + "# Specify paths to search for libraries. The usual LD_LIBRARY_PATH syntax\n" + + "# applies. Paths must be in-Graphene visible paths, not host-OS paths (i.e.,\n" + + "# paths must be taken from fs.mount.xxx.path, not fs.mount.xxx.uri).\n" + + "loader.env.LD_LIBRARY_PATH = \"/lib:/usr/lib:$(ARCH_LIBDIR):/usr/$(ARCH_LIBDIR):./\"\n" + + "\n" + + "# Mount host-OS directory to required libraries (in 'uri') into in-Graphene\n" + + "# visible directory /lib (in 'path').\n" + "fs.mount.lib.type = \"chroot\"\n" + + "fs.mount.lib.path = \"/lib\"\n" + + "fs.mount.lib.uri = \"file:$(GRAPHENEDIR)/Runtime\"\n" + "\n" + + "fs.mount.lib2.type = \"chroot\"\n" + "fs.mount.lib2.path = \"$(ARCH_LIBDIR)\"\n" + + "fs.mount.lib2.uri = \"file:$(ARCH_LIBDIR)\"\n" + "\n" + + "#fs.mount.lib3.type = \"chroot\"\n" + + "#fs.mount.lib3.path = \"/usr/$(ARCH_LIBDIR)\"\n" + + "#fs.mount.lib3.uri = \"file:/usr/$(ARCH_LIBDIR)\"\n" + "\n" + + "fs.mount.usr.type = \"chroot\"\n" + "fs.mount.usr.path = \"/usr\"\n" + + "fs.mount.usr.uri = \"file:/usr\"\n" + "\n" + + "# Host-level directory to NSS files required by Glibc + NSS libs\n" + + "fs.mount.etc.type = \"chroot\"\n" + "fs.mount.etc.path = \"/etc\"\n" + + "fs.mount.etc.uri = \"file:/etc\"\n" + "\n" + + "# Workload needs to create temporary files\n" + + "fs.mount.tmp.type = \"chroot\"\n" + "fs.mount.tmp.path = \"/tmp\"\n" + + "fs.mount.tmp.uri = \"file:/tmp\"\n" + "\n" + + "# Set enclave size to 2GB; NodeJS expects around 1.7GB of heap on startup,\n" + + "# see e.g. https://github.com/nodejs/node/issues/13018.\n" + + "# Recall that SGX v1 requires to specify enclave size at enclave creation time.\n" + + "sgx.enclave_size = \"2G\"\n" + "\n" + + "# Set maximum number of in-enclave threads (somewhat arbitrarily) to 8. Recall\n" + + "# that SGX v1 requires to specify the maximum number of simultaneous threads at\n" + + "# enclave creation time.\n" + "sgx.thread_num = 16\n" + "\n" + + "# Specify all libraries used by Node.js and its dependencies (including all libs\n" + + "# which can be loaded at runtime via dlopen).\n" + + "sgx.trusted_files.ld = \"file:$(GRAPHENEDIR)/Runtime/ld-linux-x86-64.so.2\"\n" + + "sgx.trusted_files.libc = \"file:$(GRAPHENEDIR)/Runtime/libc.so.6\"\n" + + "sgx.trusted_files.libm = \"file:$(GRAPHENEDIR)/Runtime/libm.so.6\"\n" + + "sgx.trusted_files.libdl = \"file:$(GRAPHENEDIR)/Runtime/libdl.so.2\"\n" + + "sgx.trusted_files.librt = \"file:$(GRAPHENEDIR)/Runtime/librt.so.1\"\n" + + "sgx.trusted_files.libutil = \"file:$(GRAPHENEDIR)/Runtime/libutil.so.1\"\n" + + "sgx.trusted_files.libpthread = \"file:$(GRAPHENEDIR)/Runtime/libpthread.so.0\"\n" + + "sgx.trusted_files.libnssdns = \"file:$(GRAPHENEDIR)/Runtime/libnss_dns.so.2\"\n" + + "sgx.trusted_files.libresolv = \"file:$(GRAPHENEDIR)/Runtime/libresolv.so.2\"\n" + + "\n" + "sgx.trusted_files.libstdc = \"file:/usr/$(ARCH_LIBDIR)/libstdc++.so.6\"\n" + + "sgx.trusted_files.libgccs = \"file:$(ARCH_LIBDIR)/libgcc_s.so.1\"\n" + + "sgx.trusted_files.libaptpkg = \"file:/usr/$(ARCH_LIBDIR)/libapt-pkg.so.5.0\"\n" + + "sgx.trusted_files.liblz4 = \"file:/usr/$(ARCH_LIBDIR)/liblz4.so.1\"\n" + + "sgx.trusted_files.libsystemd = \"file:$(ARCH_LIBDIR)/libsystemd.so.0\"\n" + + "sgx.trusted_files.libselinux = \"file:$(ARCH_LIBDIR)/libselinux.so.1\"\n" + + "sgx.trusted_files.libgcrypt = \"file:$(ARCH_LIBDIR)/libgcrypt.so.20\"\n" + + "sgx.trusted_files.libpcre = \"file:$(ARCH_LIBDIR)/libpcre.so.3\"\n" + + "sgx.trusted_files.libgpgerror = \"file:$(ARCH_LIBDIR)/libgpg-error.so.0\"\n" + + "sgx.trusted_files.libexpat = \"file:$(ARCH_LIBDIR)/libexpat.so.1\"\n" + + "sgx.trusted_files.libz = \"file:$(ARCH_LIBDIR)/libz.so.1\"\n" + + "sgx.trusted_files.libz2 = \"file:$(ARCH_LIBDIR)/libbz2.so.1.0\"\n" + + "sgx.trusted_files.liblzma = \"file:$(ARCH_LIBDIR)/liblzma.so.5\"\n" + + "sgx.trusted_files.libmpdec = \"file:/usr/$(ARCH_LIBDIR)/libmpdec.so.2\"\n" + "\n" + + "# Name Service Switch (NSS) libraries (Glibc dependencies)\n" + + "sgx.trusted_files.libnssfiles = \"file:$(ARCH_LIBDIR)/libnss_files.so.2\"\n" + + "sgx.trusted_files.libnsscompat = \"file:$(ARCH_LIBDIR)/libnss_compat.so.2\"\n" + + "sgx.trusted_files.libnssnis = \"file:$(ARCH_LIBDIR)/libnss_nis.so.2\"\n" + + "sgx.trusted_files.libnsl = \"file:$(ARCH_LIBDIR)/libnsl.so.1\"\n" + + "sgx.trusted_files.libnssmyhostname = \"file:$(ARCH_LIBDIR)/libnss_myhostname.so.2\"\n" + + "sgx.trusted_files.libnssmdns = \"file:$(ARCH_LIBDIR)/libnss_mdns4_minimal.so.2\"\n" + + "\n" + "# Scratch space\n" + "sgx.allowed_files.tmp = \"file:/tmp\"\n" + "\n" + + "# APT config files\n" + + "sgx.allowed_files.aptconfd = \"file:/etc/apt/apt.conf.d\"\n" + + "sgx.allowed_files.aptconf = \"file:/etc/apt/apt.conf\"\n" + + "sgx.allowed_files.apport = \"file:/etc/default/apport\"\n" + "\n" + + "# Name Service Switch (NSS) files (Glibc reads these files)\n" + + "sgx.allowed_files.nsswitch = \"file:/etc/nsswitch.conf\"\n" + + "sgx.allowed_files.group = \"file:/etc/group\"\n" + + "sgx.allowed_files.passwd = \"file:/etc/passwd\"\n" + "\n" + + "# DNS hostname resolution files (Glibc reads these files)\n" + + "sgx.allowed_files.hostconf = \"file:/etc/host.conf\"\n" + + "sgx.allowed_files.hosts = \"file:/etc/hosts\"\n" + + "sgx.allowed_files.gaiconf = \"file:/etc/gai.conf\"\n" + + "sgx.allowed_files.resolv = \"file:/etc/resolv.conf\"\n" + "\n" + + "sgx.allowed_files.openssl = \"file:/etc/ssl/openssl.cnf\"\n" + "\n" + + "# System's file system table\n" + + "sgx.allowed_files.fstab = \"file:/etc/fstab\"\n" + "\n" + + "$(NODEJS_TRUSTED_LIBS)\n" + "\n" + "# JavaScript (trusted)\n" + + "sgx.allowed_files.smlib = \"file:node_modules\"\n" + + "sgx.trusted_files.npminfo = \"file:package.json\"\n" + + "sgx.trusted_files.contract = \"file:contract.js\"\n" + + "sgx.trusted_files.globaljson = \"file:global.json\"\n" + + "sgx.trusted_files.argjson = \"file:arg.json\"\n" + "\n" + + "sys.insecure__allow_eventfd = 1\n" + "\n" + "sgx.remote_attestation = 1\n" + "\n" + + "loader.env.LD_PRELOAD = \"libsecret_prov_attest.so\"\n" + + "loader.env.SECRET_PROVISION_CONSTRUCTOR = \"1\"\n" + + "loader.env.SECRET_PROVISION_SET_PF_KEY = \"1\"\n" + + "loader.env.SECRET_PROVISION_CA_CHAIN_PATH = \"certs/test-ca-sha256.crt\"\n" + + "loader.env.SECRET_PROVISION_SERVERS = \"localhost:4433\"\n" + "\n" + + "sgx.trusted_files.libsecretprovattest = \"file:libsecret_prov_attest.so\"\n" + + "sgx.trusted_files.cachain = \"file:certs/test-ca-sha256.crt\"\n" + "\n" + + "# Specify your SPID and linkable/unlinkable attestation policy\n" + + "sgx.ra_client_spid = \"DF3A8BA098E93F66CC64E8A215E98333\"\n" + + "sgx.ra_client_linkable = 0\n"; // add secret servers manifestStr += "loader.env.SECRET_PROVISION_CC_SERVERS = "; - Object resultStr = engine.invokeFunction( - "getAllSecret", - "", - input.getRequester(), + Object resultStr = engine.invokeFunction("getAllSecret", "", input.getRequester(), input.getRequesterDOI()); Map resultMap = JsonUtil.fromJson(resultStr.toString(), MapType); Map serverMap = JsonUtil.fromJson(resultMap.get("result"), MapType); diff --git a/src/main/java/org/bdware/sc/engine/DesktopEngine.java b/src/main/java/org/bdware/sc/engine/DesktopEngine.java index 20a9636..b3e1995 100644 --- a/src/main/java/org/bdware/sc/engine/DesktopEngine.java +++ b/src/main/java/org/bdware/sc/engine/DesktopEngine.java @@ -1,6 +1,5 @@ package org.bdware.sc.engine; -import com.google.gson.JsonElement; import com.google.gson.JsonPrimitive; import org.apache.commons.lang3.StringUtils; import org.apache.logging.log4j.LogManager; @@ -52,18 +51,18 @@ public class DesktopEngine extends JSEngine { private static final String ScriptFileName = "contract_main.yjs"; private static final Logger LOGGER = LogManager.getLogger(DesktopEngine.class); public static boolean _with_init_script = true; - // static String script = ""; + // static String script = ""; public NashornScriptEngine engine; public SyncMechUtil syncUtil; public boolean recovering; // 如果正在通过trace、trans恢复,设置为true,此时即使是StableMode也不记录 Resources resources; - // Class clz; - // byte[] stub; + // Class clz; + // byte[] stub; YJSClassLoader classLoader; private ContractNode cn; private Global global; - // private Object obj; - // private SimpleScriptContext simpleContext; + // private Object obj; + // private SimpleScriptContext simpleContext; // private String traceDir; private ContractProcess.Logger tracePS = null; private Contract contract; @@ -137,37 +136,28 @@ public class DesktopEngine extends JSEngine { syncUtil = new SyncMechUtil(this); ClassLoader ccl = Thread.currentThread().getContextClassLoader(); ccl = (ccl == null) ? NashornScriptEngineFactory.class.getClassLoader() : ccl; - String[] args = new String[]{"--loader-per-compile=false", "-strict=false"}; + String[] args = new String[] {"--loader-per-compile=false", "-strict=false"}; classLoader = new YJSClassLoader(ccl, new YJSFilter()); - engine = - (NashornScriptEngine) - new NashornScriptEngineFactory() - .getScriptEngine( - args, // "--print-ast", - // "true", - // "-d=/Users/huaqiancai/Downloads/dumpedClz", - // "--trace-callsites=enterexit" - // "--log=methodhandles:all", - // fields:all, - // "--print-parse", "true" "--print-code", - // fields:finest - classLoader); + engine = (NashornScriptEngine) new NashornScriptEngineFactory().getScriptEngine(args, // "--print-ast", + // "true", + // "-d=/Users/huaqiancai/Downloads/dumpedClz", + // "--trace-callsites=enterexit" + // "--log=methodhandles:all", + // fields:all, + // "--print-parse", "true" "--print-code", + // fields:finest + classLoader); Context.TRACEIF = false; // engine = (NashornScriptEngine) new // NashornScriptEngineFactory().getScriptEngine(new YJSFilter()); if (_with_init_script) { - InputStream in = - DesktopEngine.class - .getClassLoader() - .getResourceAsStream("org/bdware/sc/engine/yancloud_desktop.js"); + InputStream in = DesktopEngine.class.getClassLoader() + .getResourceAsStream("org/bdware/sc/engine/yancloud_desktop.js"); assert in != null; InputStreamReader streamReader = new InputStreamReader(in); - engine.getContext() - .setAttribute( - ScriptEngine.FILENAME, - "org/bdware/sc/engine/yancloud_desktop.js", - ScriptContext.ENGINE_SCOPE); + engine.getContext().setAttribute(ScriptEngine.FILENAME, + "org/bdware/sc/engine/yancloud_desktop.js", ScriptContext.ENGINE_SCOPE); engine.eval(streamReader); } global = engine.getNashornGlobal(); @@ -194,12 +184,9 @@ public class DesktopEngine extends JSEngine { for (String str : permissionStub) { yancloud_desktop.append(UtilRegistry.getInitStr(str, false)); } - // LOGGER.debug("[initScript] " + yancloud_desktop); - engine.getContext() - .setAttribute( - ScriptEngine.FILENAME, - yancloud_desktop.toString(), - ScriptContext.ENGINE_SCOPE); + // LOGGER.debug("[initScript] " + yancloud_desktop); + engine.getContext().setAttribute(ScriptEngine.FILENAME, yancloud_desktop.toString(), + ScriptContext.ENGINE_SCOPE); engine.eval(yancloud_desktop.toString()); } catch (ScriptException e) { e.printStackTrace(); @@ -211,14 +198,14 @@ public class DesktopEngine extends JSEngine { ScriptLoader loader = engine.getNashornContext().getScriptLoader(); Map clzCache = loader.getClzCache(); Map ret = new HashMap<>(clzCache); - // for (String str : clzCache.keySet()) { - // System.out.println("===ScriptClzName:" + str); - // } + // for (String str : clzCache.keySet()) { + // System.out.println("===ScriptClzName:" + str); + // } StructureLoader sLoader = (StructureLoader) (engine.getNashornContext().getStructLoader()); clzCache = sLoader.getClzCache(); - // for (String str : clzCache.keySet()) { - // System.out.println("===StructureClzName:" + str); - // } + // for (String str : clzCache.keySet()) { + // System.out.println("===StructureClzName:" + str); + // } ret.putAll(clzCache); return ret; } @@ -252,11 +239,11 @@ public class DesktopEngine extends JSEngine { } @Override - public ContractResult loadContract( - Contract contract, ContractNode contractNode, boolean isInsnLimit) { + public ContractResult loadContract(Contract contract, ContractNode contractNode, + boolean isInsnLimit) { cn = contractNode; - engine.getContext() - .setAttribute(ScriptEngine.FILENAME, ScriptFileName, ScriptContext.ENGINE_SCOPE); + engine.getContext().setAttribute(ScriptEngine.FILENAME, ScriptFileName, + ScriptContext.ENGINE_SCOPE); try { setPermission(cn.getPermission()); } catch (Exception e) { @@ -266,11 +253,8 @@ public class DesktopEngine extends JSEngine { for (FunctionNode fun : contractNode.getFunctions()) try { String str = fun.plainText(); - engine.getContext() - .setAttribute( - ScriptEngine.FILENAME, - fun.getFileName(), - ScriptContext.ENGINE_SCOPE); + engine.getContext().setAttribute(ScriptEngine.FILENAME, fun.getFileName(), + ScriptContext.ENGINE_SCOPE); compileFunction(fun, str, isInsnLimit); } catch (ScriptException e) { return wrapperException(e, fun); @@ -280,7 +264,8 @@ public class DesktopEngine extends JSEngine { e.printStackTrace(); return new ContractResult(Status.Error, new JsonPrimitive(bo.toString())); } - LOGGER.debug(JsonUtil.toJson(contractNode.events) + "\n\t" + JsonUtil.toJson(contractNode.logs)); + LOGGER.debug( + JsonUtil.toJson(contractNode.events) + "\n\t" + JsonUtil.toJson(contractNode.logs)); for (String topic : contractNode.events.keySet()) { compileEventFunction(topic, topic, contractNode.events.get(topic)); } @@ -311,22 +296,18 @@ public class DesktopEngine extends JSEngine { try { String str; if (REventSemantics.AT_LEAST_ONCE.equals(semantics)) { - str = - String.format( - "function %s(arg) { YancloudUtil.pubEvent(\"%s\", arg); }", - name, topic); + str = String.format("function %s(arg) { YancloudUtil.pubEvent(\"%s\", arg); }", + name, topic); } else { - str = - String.format( - "function %s(arg) { YancloudUtil.pubEventConstraint(\"%s\", arg, \"%s\"); }", - name, topic, semantics.name()); + str = String.format( + "function %s(arg) { YancloudUtil.pubEventConstraint(\"%s\", arg, \"%s\"); }", + name, topic, semantics.name()); } compileFunction(null, str, false); LOGGER.debug("compile function " + name + " success!"); - str = - String.format( - "function %ss(arg0, arg1) { YancloudUtil.pubEventConstraint(\"%s\", arg0, arg1); }", - name, topic); + str = String.format( + "function %ss(arg0, arg1) { YancloudUtil.pubEventConstraint(\"%s\", arg0, arg1); }", + name, topic); compileFunction(null, str, false); LOGGER.debug("compile function " + name + "s success!"); } catch (ScriptException e) { @@ -334,79 +315,80 @@ public class DesktopEngine extends JSEngine { } } - // /** - // * Load a contract into contract engine - // * - // * @param contractNode a contract node generated by YJS compiler - // * @return whether contract is loaded successfully - // */ - // @Override - // public ContractResult loadContract(Contract contract, ContractNode contractNode) { - // cn = contractNode; - // engine.getContext() - // .setAttribute(ScriptEngine.FILENAME, ScriptFileName, + // /** + // * Load a contract into contract engine + // * + // * @param contractNode a contract node generated by YJS compiler + // * @return whether contract is loaded successfully + // */ + // @Override + // public ContractResult loadContract(Contract contract, ContractNode contractNode) { + // cn = contractNode; + // engine.getContext() + // .setAttribute(ScriptEngine.FILENAME, ScriptFileName, // ScriptContext.ENGINE_SCOPE); - // try { - // setPermission(cn.getPermission()); - // } catch (Exception e) { - // e.printStackTrace(); - // } + // try { + // setPermission(cn.getPermission()); + // } catch (Exception e) { + // e.printStackTrace(); + // } // - // for (FunctionNode fun : cn.getFunctions()) { - // try { - // String str = fun.plainText(); - // engine.getContext() - // .setAttribute( - // ScriptEngine.FILENAME, - // fun.getFileName(), - // ScriptContext.ENGINE_SCOPE); - // Object scriptFunction = engine.eval(str); - // ScriptObjectMirror sf = (ScriptObjectMirror) scriptFunction; - // compileFunction(sf, fun.getLogTypes().contains(LogType.Branch)); - // } catch (ScriptException e) { - // return wrapperException(e, fun); - // } catch (Exception e) { - // ByteArrayOutputStream bo = new ByteArrayOutputStream(); - // e.printStackTrace(new PrintStream(bo)); - // e.printStackTrace(); - // return new ContractResult(Status.Error, new JsonPrimitive(bo.toString())); - // } - // } - // for (String event : cn.events) { - // String str = - // "function " - // + event - // + "(arg){ return YancloudUtil.pubEvent(\"" - // + event - // + "\",arg);}"; + // for (FunctionNode fun : cn.getFunctions()) { + // try { + // String str = fun.plainText(); + // engine.getContext() + // .setAttribute( + // ScriptEngine.FILENAME, + // fun.getFileName(), + // ScriptContext.ENGINE_SCOPE); + // Object scriptFunction = engine.eval(str); + // ScriptObjectMirror sf = (ScriptObjectMirror) scriptFunction; + // compileFunction(sf, fun.getLogTypes().contains(LogType.Branch)); + // } catch (ScriptException e) { + // return wrapperException(e, fun); + // } catch (Exception e) { + // ByteArrayOutputStream bo = new ByteArrayOutputStream(); + // e.printStackTrace(new PrintStream(bo)); + // e.printStackTrace(); + // return new ContractResult(Status.Error, new JsonPrimitive(bo.toString())); + // } + // } + // for (String event : cn.events) { + // String str = + // "function " + // + event + // + "(arg){ return YancloudUtil.pubEvent(\"" + // + event + // + "\",arg);}"; // - // try { - // Object scriptFunction = engine.eval(str); - // ScriptObjectMirror sf = (ScriptObjectMirror) scriptFunction; - // compileFunction(sf, false); - // } catch (ScriptException e) { - // e.printStackTrace(); - // } - // } + // try { + // Object scriptFunction = engine.eval(str); + // ScriptObjectMirror sf = (ScriptObjectMirror) scriptFunction; + // compileFunction(sf, false); + // } catch (ScriptException e) { + // e.printStackTrace(); + // } + // } // - // for (ClassNode classNode : cn.getClzs()) { - // try { - // System.out.println(classNode.plainText()); - // // engine.eval(cn.plainText()); - // } catch (Exception e) { - // // return wrapperException(e, cn.getFileName(), cn.getLine(), - // // cn.getPos()); - // } - // } + // for (ClassNode classNode : cn.getClzs()) { + // try { + // System.out.println(classNode.plainText()); + // // engine.eval(cn.plainText()); + // } catch (Exception e) { + // // return wrapperException(e, cn.getFileName(), cn.getLine(), + // // cn.getPos()); + // } + // } // - // // dump confidential functions and corresponding dependent functions to a String in + // // dump confidential functions and corresponding dependent functions to a String in // Global - // // variable. - // // The String will be passed to collect signature. + // // variable. + // // The String will be passed to collect signature. // - // return new ContractResult(Status.Success, new JsonPrimitive("")); - // } - private void compileFunction(FunctionNode functionNode, ScriptObjectMirror sf, boolean instrumentBranch) { + // return new ContractResult(Status.Success, new JsonPrimitive("")); + // } + private void compileFunction(FunctionNode functionNode, ScriptObjectMirror sf, + boolean instrumentBranch) { Global oldGlobal = Context.getGlobal(); boolean globalChanged = (oldGlobal != global); try { @@ -423,7 +405,8 @@ public class DesktopEngine extends JSEngine { f.setAccessible(true); ScriptFunctionData scriptFunctioNData = (ScriptFunctionData) f.get(scriptFunction); Object scope = scriptFunction.getScope(); - Method getGeneric = ScriptFunctionData.class.getDeclaredMethod("getGenericInvoker", ScriptObject.class); + Method getGeneric = ScriptFunctionData.class.getDeclaredMethod("getGenericInvoker", + ScriptObject.class); getGeneric.setAccessible(true); MethodHandle methodHandle = (MethodHandle) getGeneric.invoke(scriptFunctioNData, scope); if (methodHandle.getClass() != Class.forName("java.lang.invoke.DirectMethodHandle")) { @@ -441,17 +424,19 @@ public class DesktopEngine extends JSEngine { Class clazz2 = (Class) clazz.get(memberName); if (functionNode != null) functionNode.compiledClazz = clazz2; - //functionNode==null --> event functions + // functionNode==null --> event functions } catch (Exception e) { e.printStackTrace(); } finally { - if (globalChanged) Context.setGlobal(oldGlobal); + if (globalChanged) + Context.setGlobal(oldGlobal); Context.TRACEIF = false; } } - public void compileFunction(FunctionNode functionNode, String snippet, boolean instrumentBranch) throws ScriptException { + public void compileFunction(FunctionNode functionNode, String snippet, boolean instrumentBranch) + throws ScriptException { compileFunction(functionNode, (ScriptObjectMirror) engine.eval(snippet), instrumentBranch); } @@ -481,8 +466,7 @@ public class DesktopEngine extends JSEngine { JavaScriptEntry.msgList = new ArrayList<>(); FunctionNode fun = cn.getFunction(input.getAction()); if (fun == null) { - return new ContractResult( - Status.Exception, + return new ContractResult(Status.Exception, new JsonPrimitive("Action " + input.getAction() + " is not exists")); } ProgramPointCounter ppc = null; @@ -499,23 +483,14 @@ public class DesktopEngine extends JSEngine { HashMap ppCountMap = evaluatesAnalysis(input.getAction(), functions); Long extraGas = getExtraGas(fun.getCost().getExtraGas(), input); bo = new ByteArrayOutputStream(); - ppc = - new ProgramPointCounter( - bo, - previous.getCp(), - Long.MAX_VALUE, - functionIndex, - input.getGasLimit(), - extraGas, - input.getAction(), - ppCountMap); + ppc = new ProgramPointCounter(bo, previous.getCp(), Long.MAX_VALUE, functionIndex, + input.getGasLimit(), extraGas, input.getAction(), ppCountMap); this.redirectTracePS(ppc); } if (fun.isExport() || - // if the function has been registered as event handler - (fun.isHandler() && - null != input.getRequester() && - input.getRequester().startsWith("event"))) { + // if the function has been registered as event handler + (fun.isHandler() && null != input.getRequester() + && input.getRequester().startsWith("event"))) { Object ret; if (fun.isView()) { ret = executeWithoutLock(fun, input, null); @@ -525,8 +500,10 @@ public class DesktopEngine extends JSEngine { } } - //System.out.println("[DesktopEngine MaskConfig]"+ContractProcess.instance.getProjectConfig().getMaskConfig().config.toString()); - ContractResult contractRes = new ContractResult(Status.Success, JSONTool.convertMirrorToJson(ret)); + // System.out.println("[DesktopEngine + // MaskConfig]"+ContractProcess.instance.getProjectConfig().getMaskConfig().config.toString()); + ContractResult contractRes = + new ContractResult(Status.Success, JSONTool.convertMirrorToJson(ret)); if (ppc != null) { contractRes.extraGas = ppc.extraGas; contractRes.executionGas = ppc.cost; @@ -553,10 +530,10 @@ public class DesktopEngine extends JSEngine { if (syncUtil.startFlag && !recovering) { switch (syncUtil.currType) { case Trace: - // syncUtil.traceRecordUtil.eachFinish(); + // syncUtil.traceRecordUtil.eachFinish(); break; case Trans: - // syncUtil.transRecordUtil.eachFinish(); + // syncUtil.transRecordUtil.eachFinish(); break; case Memory: default: @@ -568,16 +545,13 @@ public class DesktopEngine extends JSEngine { } else { // return new ContractResult(Status.Exception, "Action " + input.getAction() + " // is not exported!"); - return new ContractResult( - Status.Exception, + return new ContractResult(Status.Exception, new JsonPrimitive("Action " + input.getAction() + " is not exported!")); } } catch (ScriptReturnException e) { e.printStackTrace(); - return new ContractResult( - Status.Exception, - e.message); + return new ContractResult(Status.Exception, e.message); } catch (ScriptException e) { Throwable cause = e.getCause(); e.printStackTrace(); @@ -588,26 +562,25 @@ public class DesktopEngine extends JSEngine { e.printStackTrace(ps); e.printStackTrace(); if (e.getCause() != null && e.getCause() instanceof ScriptException) { - return new ContractResult( - Status.Exception, - new JsonPrimitive( - extractException(bo1.toString(), extract(cn, e.getCause())))); + return new ContractResult(Status.Exception, new JsonPrimitive( + extractException(bo1.toString(), extract(cn, e.getCause())))); } else { - return new ContractResult( - Status.Exception, + return new ContractResult(Status.Exception, new JsonPrimitive(extractException(bo1.toString(), extract(cn, e)))); } } finally { this.redirectTracePS(previous); - if (globalChanged) Context.setGlobal(oldGlobal); + if (globalChanged) + Context.setGlobal(oldGlobal); } } - public Object executeWithoutLock(FunctionNode fun, ContractRequest input, Object injectedArg) throws ScriptException, NoSuchMethodException { - // long start = System.currentTimeMillis(); + public Object executeWithoutLock(FunctionNode fun, ContractRequest input, Object injectedArg) + throws ScriptException, NoSuchMethodException { + // long start = System.currentTimeMillis(); ArgPacks argPacks = new ArgPacks(input, input.getArg(), null); - if(injectedArg != null) { + if (injectedArg != null) { argPacks.arg = injectedArg; } for (AnnotationHook handler : fun.beforeExecutionAnnotations()) { @@ -616,19 +589,15 @@ public class DesktopEngine extends JSEngine { // actually invoke! if (argPacks.ret == null) { - argPacks.ret = - engine.invokeFunction( - input.getAction(), - (fun.isHandler() - ? JsonUtil.fromJson(input.getArg(), Event.class) - : JSONTool.convertJsonElementToMirror(argPacks.arg)), - input.getRequester(), - input.getRequesterDOI()); + argPacks.ret = engine.invokeFunction(input.getAction(), + (fun.isHandler() ? JsonUtil.fromJson(input.getArg(), Event.class) + : JSONTool.convertJsonElementToMirror(argPacks.arg)), + input.getRequester(), input.getRequesterDOI()); } for (AnnotationHook handler : fun.afterExecutionAnnotations()) { - //Mask在after裏面 - //System.out.println("afterHook"+contract.Mask); + // Mask在after裏面 + // System.out.println("afterHook"+contract.Mask); argPacks = handler.handle(this, argPacks); } return argPacks.ret; @@ -661,13 +630,12 @@ public class DesktopEngine extends JSEngine { for (String function : functions) { MethodNode mn = methods.get(function); if (mn != null) { - CFGraph cfg = - new CFGraph(mn) { - @Override - public BasicBlock getBasicBlock(int id) { - return new BasicBlock(id); - } - }; + CFGraph cfg = new CFGraph(mn) { + @Override + public BasicBlock getBasicBlock(int id) { + return new BasicBlock(id); + } + }; // cfg.printSelf(); CFGmap.put(function, cfg); PPCount countFee = new PPCount(cfg, flag); @@ -686,15 +654,15 @@ public class DesktopEngine extends JSEngine { flag++; } } - /* for (Map.Entry map : PPCount.functionSumGas.entrySet()) { - if (map.getKey().contains(getFunction) && map.getKey().contains("true")) { - System.out.println("[合约方法pub中条件循环为true时:]" + map.getValue()); - } else if (map.getKey().contains(getFunction) && map.getKey().contains("false")) { - System.out.println("[合约方法pub中条件循环为false时:]" + map.getValue()); - } else if (map.getKey().contains(getFunction)) { - System.out.println("[合约方法pub中其他语句消耗:]" + map.getValue()); - } - }*/ + /* + * for (Map.Entry map : PPCount.functionSumGas.entrySet()) { if + * (map.getKey().contains(getFunction) && map.getKey().contains("true")) { + * System.out.println("[合约方法pub中条件循环为true时:]" + map.getValue()); } else if + * (map.getKey().contains(getFunction) && map.getKey().contains("false")) { + * System.out.println("[合约方法pub中条件循环为false时:]" + map.getValue()); } else if + * (map.getKey().contains(getFunction)) { System.out.println("[合约方法pub中其他语句消耗:]" + + * map.getValue()); } } + */ return ppCountMap; } @@ -704,12 +672,8 @@ public class DesktopEngine extends JSEngine { return 0L; } Invocable cal = engine; - Object ret = - cal.invokeFunction( - costFunction, - input.getArg(), - input.getRequester(), - input.getRequesterDOI()); + Object ret = cal.invokeFunction(costFunction, input.getArg(), input.getRequester(), + input.getRequesterDOI()); if (ret != null && StringUtils.isNumeric(ret.toString())) { return Long.parseLong(ret.toString()); } else { @@ -765,15 +729,16 @@ public class DesktopEngine extends JSEngine { if (actLine != -1) { actLine += line - 1; } - message = - message.replaceAll( - "at line number " + e.getLineNumber(), "at line number " + (actLine)); + message = message.replaceAll("at line number " + e.getLineNumber(), + "at line number " + (actLine)); if (fun.getFileName() != null) message = message.replace("in contract_main.yjs", "in " + fun.getFileName()); content.append(message); content.append("("); - if (fun.functionName != null) content.append(fun.functionName); - else content.append("contract_main.yjs"); + if (fun.functionName != null) + content.append(fun.functionName); + else + content.append("contract_main.yjs"); content.append(":"); content.append(actLine); @@ -792,17 +757,15 @@ public class DesktopEngine extends JSEngine { String methodName = element.getMethodName(); String fileName = element.getFileName(); - if (fileName.endsWith(".java")) continue; + if (fileName.endsWith(".java")) + continue; if (c.isBundle()) { fileName = fixFile(c, methodName); } - if (fileName.equals("--")) continue; - ret.add( - String.format( - "at %s(%s:%d)", - methodName, - fileName, - (fixLine(c, methodName) + element.getLineNumber()))); + if (fileName.equals("--")) + continue; + ret.add(String.format("at %s(%s:%d)", methodName, fileName, + (fixLine(c, methodName) + element.getLineNumber()))); } return ret; } @@ -918,13 +881,13 @@ public class DesktopEngine extends JSEngine { } if (entry.getName().endsWith(".so") || entry.getName().endsWith(".so.1")) { System.out.println("unzip library:" + entry.getName().replaceAll(".*/", "")); - loader.unzipLibrary( - zf.getInputStream(entry), entry.getName().replaceAll(".*/", "")); + loader.unzipLibrary(zf.getInputStream(entry), + entry.getName().replaceAll(".*/", "")); } if (HardwareInfo.type == OSType.mac && entry.getName().endsWith(".dylib")) { System.out.println("unzip library:" + entry.getName().replaceAll(".*/", "")); - loader.unzipLibrary( - zf.getInputStream(entry), entry.getName().replaceAll(".*/", "")); + loader.unzipLibrary(zf.getInputStream(entry), + entry.getName().replaceAll(".*/", "")); } } catch (IOException e) { e.printStackTrace(); diff --git a/src/main/java/org/bdware/sc/engine/JSONTool.java b/src/main/java/org/bdware/sc/engine/JSONTool.java index ce0b4b7..0ec62bf 100644 --- a/src/main/java/org/bdware/sc/engine/JSONTool.java +++ b/src/main/java/org/bdware/sc/engine/JSONTool.java @@ -50,11 +50,14 @@ public class JSONTool { } private static JsonElement convertMirrorToJsonInternal(Object obj, Set recorded) { - if (recorded.contains(obj)) return JsonNull.INSTANCE; - if (obj == null) return JsonNull.INSTANCE; + if (recorded.contains(obj)) + return JsonNull.INSTANCE; + if (obj == null) + return JsonNull.INSTANCE; if (obj.getClass() == wrp.jdk.nashorn.internal.runtime.Undefined.class) return JsonNull.INSTANCE; - if (obj instanceof JsonElement) return (JsonElement) obj; + if (obj instanceof JsonElement) + return (JsonElement) obj; if (obj.getClass().isArray()) { Object[] arr = (Object[]) obj; recorded.add(obj); diff --git a/src/main/java/org/bdware/sc/engine/SyncMechUtil.java b/src/main/java/org/bdware/sc/engine/SyncMechUtil.java index 319e0b4..5b15ef0 100644 --- a/src/main/java/org/bdware/sc/engine/SyncMechUtil.java +++ b/src/main/java/org/bdware/sc/engine/SyncMechUtil.java @@ -128,17 +128,18 @@ public class SyncMechUtil { } /* - * memory的操作 - * 无需startFlag就可以使用memory的操作 + * memory的操作 无需startFlag就可以使用memory的操作 */ public synchronized String dumpMemory(String path) { - if (memoryDumpUtil == null) memoryDumpUtil = new MemoryDumpUtil(engine.engine); + if (memoryDumpUtil == null) + memoryDumpUtil = new MemoryDumpUtil(engine.engine); return memoryDumpUtil.dumpMemory(path, true); } public synchronized String dumpMemory(String path, boolean stateful) { LOGGER.info("dumpMemroy : stateful=" + stateful); - if (memoryDumpUtil == null) memoryDumpUtil = new MemoryDumpUtil(engine.engine); + if (memoryDumpUtil == null) + memoryDumpUtil = new MemoryDumpUtil(engine.engine); return memoryDumpUtil.dumpMemory(path, stateful); } @@ -158,7 +159,7 @@ public class SyncMechUtil { } /* - * 同步机制操作 + * 同步机制操作 */ // 设置ContractRecord持久化的文件 public void setCRFile(String fileName) { @@ -210,7 +211,8 @@ public class SyncMechUtil { } public void changeCurrType(SyncType t) { - if (t == currType) return; + if (t == currType) + return; // finASyncRecord(); currType = t; @@ -226,7 +228,8 @@ public class SyncMechUtil { } break; case Memory: - if (memoryDumpUtil == null) memoryDumpUtil = new MemoryDumpUtil(engine.engine); + if (memoryDumpUtil == null) + memoryDumpUtil = new MemoryDumpUtil(engine.engine); break; default: break; @@ -256,7 +259,8 @@ public class SyncMechUtil { * Trans操作 */ public synchronized String redo(String path) { - if (transRecoverUtil == null) transRecoverUtil = new TransRecoverUtil(engine); + if (transRecoverUtil == null) + transRecoverUtil = new TransRecoverUtil(engine); // 先清空,否则会重复执行一些trans if (transRecoverUtil.transRecords != null && !transRecoverUtil.transRecords.isEmpty()) @@ -264,10 +268,11 @@ public class SyncMechUtil { // 某一次检查点之后没有transRecords File file = new File(path); - if (!file.exists()) return "success"; + if (!file.exists()) + return "success"; transRecoverUtil.setTraceRecords(path); transRecoverUtil.recoverFromTransRecord(); return "success"; } -} \ No newline at end of file +} diff --git a/src/main/java/org/bdware/sc/engine/TestClassFilter.java b/src/main/java/org/bdware/sc/engine/TestClassFilter.java index 609dfa1..7419df7 100644 --- a/src/main/java/org/bdware/sc/engine/TestClassFilter.java +++ b/src/main/java/org/bdware/sc/engine/TestClassFilter.java @@ -6,9 +6,8 @@ import javax.script.ScriptEngine; public class TestClassFilter { public TestClassFilter() { - final String script = "print(java.lang.System.getProperty(\"java.home\"));" + - "print(\"Create file variable\");" + - "var File = Java.type(\"java.io.File\");"; + final String script = "print(java.lang.System.getProperty(\"java.home\"));" + + "print(\"Create file variable\");" + "var File = Java.type(\"java.io.File\");"; NashornScriptEngineFactory factory = new NashornScriptEngineFactory(); ScriptEngine engine = factory.getScriptEngine(new YJSFilter()); try { diff --git a/src/main/java/org/bdware/sc/engine/YJSClassLoader.java b/src/main/java/org/bdware/sc/engine/YJSClassLoader.java index b506f92..5195052 100644 --- a/src/main/java/org/bdware/sc/engine/YJSClassLoader.java +++ b/src/main/java/org/bdware/sc/engine/YJSClassLoader.java @@ -19,7 +19,7 @@ public class YJSClassLoader extends URLClassLoader { List toLoad = new ArrayList<>(); public YJSClassLoader(ClassLoader parent, ClassFilter cf) { - super(new URL[]{}, parent); + super(new URL[] {}, parent); classFilter = cf; } @@ -50,7 +50,7 @@ public class YJSClassLoader extends URLClassLoader { ByteArrayOutputStream bo = new ByteArrayOutputStream(); byte[] buff = new byte[1024]; - for (int len = 0; (len = in.read(buff)) > 0; ) { + for (int len = 0; (len = in.read(buff)) > 0;) { bo.write(buff, 0, len); } return bo.toByteArray(); @@ -63,7 +63,8 @@ public class YJSClassLoader extends URLClassLoader { public String unzipLibrary(InputStream inputStream, String name) { try { if (libDir == null) { - libDir = new File(System.getProperty("java.io.tmpdir"), "yjscontract_" + System.currentTimeMillis()); + libDir = new File(System.getProperty("java.io.tmpdir"), + "yjscontract_" + System.currentTimeMillis()); libDir.mkdirs(); } File f = new File(libDir, name); @@ -71,7 +72,7 @@ public class YJSClassLoader extends URLClassLoader { toLoad.add(f.getAbsolutePath()); FileOutputStream fout = new FileOutputStream(f); byte[] buff = new byte[1024 * 100]; - for (int k = 0; (k = inputStream.read(buff)) > 0; ) { + for (int k = 0; (k = inputStream.read(buff)) > 0;) { fout.write(buff, 0, k); } fout.close(); @@ -84,7 +85,7 @@ public class YJSClassLoader extends URLClassLoader { private void addDirToPath(String s) { try { - // System.out.println("[YJSClassloader] addtopath:" + s); + // System.out.println("[YJSClassloader] addtopath:" + s); Field field = ClassLoader.class.getDeclaredField("sys_paths"); field.setAccessible(true); String[] path = (String[]) field.get(null); diff --git a/src/main/java/org/bdware/sc/engine/YJSFilter.java b/src/main/java/org/bdware/sc/engine/YJSFilter.java index 02cd2cf..83fa30c 100644 --- a/src/main/java/org/bdware/sc/engine/YJSFilter.java +++ b/src/main/java/org/bdware/sc/engine/YJSFilter.java @@ -3,9 +3,9 @@ package org.bdware.sc.engine; import wrp.jdk.nashorn.api.scripting.ClassFilter; public class YJSFilter implements ClassFilter { - @Override - public boolean exposeToScripts(String arg0) { - return true; - //return arg0.compareTo("java.io.File") != 0; - } + @Override + public boolean exposeToScripts(String arg0) { + return true; + // return arg0.compareTo("java.io.File") != 0; + } } diff --git a/src/main/java/org/bdware/sc/engine/hook/AccessHandler.java b/src/main/java/org/bdware/sc/engine/hook/AccessHandler.java index 0f4c203..b4cba6a 100644 --- a/src/main/java/org/bdware/sc/engine/hook/AccessHandler.java +++ b/src/main/java/org/bdware/sc/engine/hook/AccessHandler.java @@ -43,19 +43,22 @@ public class AccessHandler implements AnnotationHook { public ArgPacks handle(JSEngine desktopEngine, ArgPacks argPacks) { if (requireSign) { if (!argPacks.request.verifySignature()) { - throw new ScriptReturnException(JsonParser.parseString("{\"code\":400,\"msg\":\"permission denied\"}")); + throw new ScriptReturnException( + JsonParser.parseString("{\"code\":400,\"msg\":\"permission denied\"}")); } return argPacks; } if (!argPacks.request.verifySignature()) { - LOGGER.info("verify failed! clear requester," + argPacks.request.getContentStr() + - " -> " + argPacks.request.getPublicKey() + "sign:" + argPacks.request.getSignature()); - //TODO - //FIXME here! + LOGGER.info("verify failed! clear requester," + argPacks.request.getContentStr() + + " -> " + argPacks.request.getPublicKey() + "sign:" + + argPacks.request.getSignature()); + // TODO + // FIXME here! // argPacks.request.setRequester(null); } else LOGGER.info("verify success!" + argPacks.request.getRequester()); - if (acFunction == null) return argPacks; + if (acFunction == null) + return argPacks; DesktopEngine de = (DesktopEngine) desktopEngine; try { ContractRequest input = argPacks.request; diff --git a/src/main/java/org/bdware/sc/engine/hook/ArgSchemaHandler.java b/src/main/java/org/bdware/sc/engine/hook/ArgSchemaHandler.java index cee4794..38e71b6 100644 --- a/src/main/java/org/bdware/sc/engine/hook/ArgSchemaHandler.java +++ b/src/main/java/org/bdware/sc/engine/hook/ArgSchemaHandler.java @@ -21,14 +21,15 @@ public class ArgSchemaHandler implements AnnotationHook { public ArgSchemaHandler(AnnotationNode annoNode) { a = annoNode; String arg = a.getArgs().get(0); -// if (arg.startsWith("/")){ -// ;//ContractProcess.instance.engine.getResources().loadAsString(arg); -// }else { -// ; -// } + // if (arg.startsWith("/")){ + // ;//ContractProcess.instance.engine.getResources().loadAsString(arg); + // }else { + // ; + // } } - public static ArgSchemaHandler fromAnnotationNode(FunctionNode funNode, AnnotationNode annoNode) { + public static ArgSchemaHandler fromAnnotationNode(FunctionNode funNode, + AnnotationNode annoNode) { return new ArgSchemaHandler(annoNode); } @@ -43,7 +44,7 @@ public class ArgSchemaHandler implements AnnotationHook { try { JsonElement obj = JsonParser.parseString(input.getArg().getAsString()); visitor = new ArgSchemaVisitor(obj); - //IMPORTANT automatically convert arg type here + // IMPORTANT automatically convert arg type here input.setArg(obj); argPacks.arg = obj; } catch (Exception e) { @@ -65,7 +66,7 @@ public class ArgSchemaHandler implements AnnotationHook { JsonObject jo = new JsonObject(); jo.addProperty("msg", visitor.getException()); jo.addProperty("code", visitor.errorCode); - jo.add("argSchema",JsonParser.parseString(a.getArgs().get(0))); + jo.add("argSchema", JsonParser.parseString(a.getArgs().get(0))); throw new ScriptReturnException(jo); } return argPacks; diff --git a/src/main/java/org/bdware/sc/engine/hook/ArgSchemaVisitor.java b/src/main/java/org/bdware/sc/engine/hook/ArgSchemaVisitor.java index 6adcdc6..1b66140 100644 --- a/src/main/java/org/bdware/sc/engine/hook/ArgSchemaVisitor.java +++ b/src/main/java/org/bdware/sc/engine/hook/ArgSchemaVisitor.java @@ -7,9 +7,9 @@ public class ArgSchemaVisitor extends JsonVisitor { JsonElement root; public boolean status = true; public int errorCode = 0; - //{"msg":"xxx","code":1000} + // {"msg":"xxx","code":1000} // -// JsonElement message = new JsonPrimitive(""); + // JsonElement message = new JsonPrimitive(""); public String message = ""; public ArgSchemaVisitor(JsonElement ret) { @@ -18,13 +18,13 @@ public class ArgSchemaVisitor extends JsonVisitor { @Override public JsonVisitor visitObject(JsonObject schema) { - //message = new JsonObject(); + // message = new JsonObject(); if (root.isJsonObject()) { JsonObject jo = root.getAsJsonObject(); for (String key : schema.keySet()) { if (key.startsWith("!")) { - //TODO - //if(必選) + // TODO + // if(必選) if (jo.has(key.substring(1))) { ArgSchemaVisitor visitor = new ArgSchemaVisitor(jo.get(key.substring(1))); visitor.visit(schema.get(key)); @@ -84,12 +84,13 @@ public class ArgSchemaVisitor extends JsonVisitor { String type = primitive.getAsString(); try { String result = ""; - //md5不需要参数 + // md5不需要参数 if (type.equals("string")) { if (root.isJsonPrimitive() && root.getAsJsonPrimitive().isString()) { return this; } else { - message = "[Type error] The value (" + root.getAsJsonPrimitive().getAsString() + ") should be string"; + message = "[Type error] The value (" + + root.getAsJsonPrimitive().getAsString() + ") should be string"; status = false; errorCode = 1001; return this; @@ -98,7 +99,8 @@ public class ArgSchemaVisitor extends JsonVisitor { if (root.isJsonPrimitive() && root.getAsJsonPrimitive().isNumber()) { return this; } else { - message = "[Type error] The value (" + root.getAsJsonPrimitive().getAsString() + ") should be number"; + message = "[Type error] The value (" + + root.getAsJsonPrimitive().getAsString() + ") should be number"; status = false; errorCode = 1001; return this; @@ -107,7 +109,8 @@ public class ArgSchemaVisitor extends JsonVisitor { if (root.isJsonPrimitive() && root.getAsJsonPrimitive().isBoolean()) { return this; } else { - message = "[Type error] The value (" + root.getAsJsonPrimitive().getAsString() + ") should be boolean"; + message = "[Type error] The value (" + + root.getAsJsonPrimitive().getAsString() + ") should be boolean"; status = false; errorCode = 1001; return this; @@ -116,7 +119,8 @@ public class ArgSchemaVisitor extends JsonVisitor { if (root.isJsonArray()) { return this; } else { - message = "[Type error] The value (" + root.getAsJsonPrimitive().getAsString() + ") should be array"; + message = "[Type error] The value (" + + root.getAsJsonPrimitive().getAsString() + ") should be array"; status = false; errorCode = 1001; return this; @@ -125,7 +129,8 @@ public class ArgSchemaVisitor extends JsonVisitor { if (root.isJsonObject()) { return this; } else { - message = "[Type error] The value (" + root.getAsJsonPrimitive().getAsString() + ") should be object"; + message = "[Type error] The value (" + + root.getAsJsonPrimitive().getAsString() + ") should be object"; status = false; errorCode = 1001; return this; diff --git a/src/main/java/org/bdware/sc/engine/hook/ConfidentialHandler.java b/src/main/java/org/bdware/sc/engine/hook/ConfidentialHandler.java index 05f55ec..76fa483 100644 --- a/src/main/java/org/bdware/sc/engine/hook/ConfidentialHandler.java +++ b/src/main/java/org/bdware/sc/engine/hook/ConfidentialHandler.java @@ -23,8 +23,8 @@ public class ConfidentialHandler implements AnnotationHook { DesktopEngine desktopEngine = (DesktopEngine) engine; ConfidentialContractUtil.copyTemplateToDestination(input); ScriptObjectMirror globalVars = (ScriptObjectMirror) desktopEngine.get("Global"); - ConfidentialContractUtil.dumpScriptAndStates( - desktopEngine.engine, fun, input, globalVars); + ConfidentialContractUtil.dumpScriptAndStates(desktopEngine.engine, fun, input, + globalVars); // run in SGX instead of Nashorn if function has @Confidential annotation argPacks.ret = ConfidentialContractUtil.executeConfidentialContract(input); return argPacks; diff --git a/src/main/java/org/bdware/sc/engine/hook/DOOPAfterExecHandler.java b/src/main/java/org/bdware/sc/engine/hook/DOOPAfterExecHandler.java index 8808a0f..540013a 100644 --- a/src/main/java/org/bdware/sc/engine/hook/DOOPAfterExecHandler.java +++ b/src/main/java/org/bdware/sc/engine/hook/DOOPAfterExecHandler.java @@ -35,23 +35,27 @@ public class DOOPAfterExecHandler implements AnnotationHook { // if http, directly return if (doipMessagePacker.source.equals("http")) { if (argPacks.ret != null && argPacks.ret instanceof DoipMessage) { - argPacks.ret = JsonUtil.parseObject(JsonDoipMessage.fromDoipMessage((DoipMessage) argPacks.ret)); + argPacks.ret = JsonUtil.parseObject( + JsonDoipMessage.fromDoipMessage((DoipMessage) argPacks.ret)); } return argPacks; } else { // pack if (!(argPacks.ret instanceof DoipMessage)) { JsonObject jsonObjectRes = ((JsonElement) argPacks.ret).getAsJsonObject(); - if (!jsonObjectRes.has("bodyBase64Encoded") || jsonObjectRes.get("bodyBase64Encoded").getAsBoolean() == false) { + if (!jsonObjectRes.has("bodyBase64Encoded") + || jsonObjectRes.get("bodyBase64Encoded").getAsBoolean() == false) { if (jsonObjectRes.has("body")) { String body = jsonObjectRes.get("body").getAsString(); - jsonObjectRes.addProperty("body", ByteUtil.encodeBASE64(body.getBytes(StandardCharsets.UTF_8))); + jsonObjectRes.addProperty("body", + ByteUtil.encodeBASE64(body.getBytes(StandardCharsets.UTF_8))); } } // validate json response ArgSchemaVisitor visitor = new ArgSchemaVisitor(jsonObjectRes); validateJsonElementRulesByArgSchemaVisitor(jsonResponseRules, visitor); - JsonDoipMessage returnedMessage = JsonUtil.fromJson(jsonObjectRes, JsonDoipMessage.class); + JsonDoipMessage returnedMessage = + JsonUtil.fromJson(jsonObjectRes, JsonDoipMessage.class); argPacks.ret = returnedMessage.toResponseDoipMessage(originDoipMsg); } return argPacks; @@ -71,7 +75,8 @@ public class DOOPAfterExecHandler implements AnnotationHook { case Search: case ListOps: case Delete: - return JsonParser.parseString("{\"header\":{\"response\":\"string\",\"attributes\":{}},\"body\":\"string\"}"); + return JsonParser.parseString( + "{\"header\":{\"response\":\"string\",\"attributes\":{}},\"body\":\"string\"}"); case Extension: case Unknown: default: @@ -80,17 +85,21 @@ public class DOOPAfterExecHandler implements AnnotationHook { } // old convert jsonResponse from argPack's ret to Doip response in doip chain logic - public DoipMessage convertJsonResponseToDoipMessage(FunctionNode fn, JsonElement jsonResponse, DoipMessage msg) { + public DoipMessage convertJsonResponseToDoipMessage(FunctionNode fn, JsonElement jsonResponse, + DoipMessage msg) { JsonObject jsonParams = jsonResponse.getAsJsonObject(); // validate json response ArgSchemaVisitor visitor = new ArgSchemaVisitor(jsonResponse); validateJsonElementRulesByArgSchemaVisitor(jsonResponseRules, visitor); - JsonObject header = jsonParams.get("header") != null ? jsonParams.get("header").getAsJsonObject() : null; + JsonObject header = + jsonParams.get("header") != null ? jsonParams.get("header").getAsJsonObject() + : null; String body = jsonParams.get("body") != null ? jsonParams.get("body").getAsString() : null; if (header != null) { - String headerRespCode = header.get("response") != null ? header.get("response").getAsString() : null; + String headerRespCode = + header.get("response") != null ? header.get("response").getAsString() : null; if (headerRespCode != null) { for (DoipResponseCode responseCode : DoipResponseCode.values()) { if (responseCode.toString().equals(headerRespCode)) { @@ -107,7 +116,8 @@ public class DOOPAfterExecHandler implements AnnotationHook { return msg; } - public static void validateJsonElementRulesByArgSchemaVisitor(JsonElement jsonElement, ArgSchemaVisitor visitor) { + public static void validateJsonElementRulesByArgSchemaVisitor(JsonElement jsonElement, + ArgSchemaVisitor visitor) { visitor.visit(jsonElement); if (!visitor.getStatus()) { JsonObject jo = new JsonObject(); diff --git a/src/main/java/org/bdware/sc/engine/hook/DOOPBeforeExecHandler.java b/src/main/java/org/bdware/sc/engine/hook/DOOPBeforeExecHandler.java index 738dd33..702689f 100644 --- a/src/main/java/org/bdware/sc/engine/hook/DOOPBeforeExecHandler.java +++ b/src/main/java/org/bdware/sc/engine/hook/DOOPBeforeExecHandler.java @@ -40,7 +40,8 @@ public class DOOPBeforeExecHandler implements AnnotationHook { doipMsgPackerArg.rawDoipMsg = convertHttpRequestToDoipMessage(httpReq); if (httpReq.verifySignature()) { - doipMsgPackerArg.rawDoipMsg.credential = new MessageCredential(httpReq.getRequester(), new byte[0]); + doipMsgPackerArg.rawDoipMsg.credential = + new MessageCredential(httpReq.getRequester(), new byte[0]); } else { doipMsgPackerArg.rawDoipMsg.credential = null; } @@ -70,11 +71,14 @@ public class DOOPBeforeExecHandler implements AnnotationHook { return JsonParser.parseString("{\"!header\":{\"!identifier\":\"string\"}}"); case Create: case Update: - return JsonParser.parseString("{\"!header\":{\"!identifier\":\"string\"}, \"!body\":\"string\"}"); + return JsonParser.parseString( + "{\"!header\":{\"!identifier\":\"string\"}, \"!body\":\"string\"}"); case Search: - return JsonParser.parseString("{\"!header\":{\"!identifier\":\"string\", \"!attributes\":{\"!query\":\"string\", \"!pageNum\":\"int\", \"!pageSize\":\"int\", \"!type\":\"string\"}}}"); + return JsonParser.parseString( + "{\"!header\":{\"!identifier\":\"string\", \"!attributes\":{\"!query\":\"string\", \"!pageNum\":\"int\", \"!pageSize\":\"int\", \"!type\":\"string\"}}}"); case Retrieve: - return JsonParser.parseString("{\"!header\":{\"!identifier\":\"string\", \"attributes\":{\"element\":\"string\", \"includeElementData\":\"boolean\"}}}"); + return JsonParser.parseString( + "{\"!header\":{\"!identifier\":\"string\", \"attributes\":{\"element\":\"string\", \"includeElementData\":\"boolean\"}}}"); case Extension: case Unknown: default: @@ -92,7 +96,8 @@ public class DOOPBeforeExecHandler implements AnnotationHook { } - public static void validateJsonElementRulesByArgSchemaVisitor(JsonElement jsonElement, ArgSchemaVisitor visitor) { + public static void validateJsonElementRulesByArgSchemaVisitor(JsonElement jsonElement, + ArgSchemaVisitor visitor) { visitor.visit(jsonElement); if (!visitor.getStatus()) { JsonObject jo = new JsonObject(); diff --git a/src/main/java/org/bdware/sc/engine/hook/HomoVisitor.java b/src/main/java/org/bdware/sc/engine/hook/HomoVisitor.java index 97d0f36..04115ee 100644 --- a/src/main/java/org/bdware/sc/engine/hook/HomoVisitor.java +++ b/src/main/java/org/bdware/sc/engine/hook/HomoVisitor.java @@ -74,7 +74,7 @@ public class HomoVisitor extends JsonVisitor { System.out.println(e); } } - + return this; } diff --git a/src/main/java/org/bdware/sc/engine/hook/HomomorphicDecryptHandler.java b/src/main/java/org/bdware/sc/engine/hook/HomomorphicDecryptHandler.java index 76888ae..e88a0e0 100644 --- a/src/main/java/org/bdware/sc/engine/hook/HomomorphicDecryptHandler.java +++ b/src/main/java/org/bdware/sc/engine/hook/HomomorphicDecryptHandler.java @@ -27,25 +27,25 @@ public class HomomorphicDecryptHandler implements AnnotationHook { @Override public ArgPacks handle(JSEngine engine, ArgPacks argPacks) { try { -// GetHomArgs args = -// new GetHomArgs( -// input.getRequester(), this.fun.getSecretID().replaceAll("\"", "")); -// String arg = JsonUtil.toJson(args); + // GetHomArgs args = + // new GetHomArgs( + // input.getRequester(), this.fun.getSecretID().replaceAll("\"", "")); + // String arg = JsonUtil.toJson(args); JsonElement homoDecryptConf = this.fun.getHomoDecryptConf(); if (null != homoDecryptConf && !homoDecryptConf.isJsonNull()) { - String res = (String) - JavaScriptEntry.executeContract( - "keyManager_1", - "getPrivKey", - this.fun.getSecretID().replaceAll("\"", "")); + String res = (String) JavaScriptEntry.executeContract("keyManager_1", "getPrivKey", + this.fun.getSecretID().replaceAll("\"", "")); LOGGER.info("HomomorphicDecryptHandler--------------------------------1: " + res); ContractResult results = JsonUtil.fromJson(res, ContractResult.class); String privKeyStr = results.result.getAsString(); - LOGGER.info("HomomorphicEncryptHandler--------------------------------2: " + privKeyStr); + LOGGER.info("HomomorphicEncryptHandler--------------------------------2: " + + privKeyStr); RSAPrivateKey privkey = (RSAPrivateKey) PaillierKeyPair.pemToPrivateKey(privKeyStr); - LOGGER.info("HomomorphicEncryptHandler--------------------------------3: " + privkey); + LOGGER.info( + "HomomorphicEncryptHandler--------------------------------3: " + privkey); HomoVisitor.privateKey = privkey; - argPacks.ret = getDecryptResult(homoDecryptConf, JsonUtil.parseObject(argPacks.ret)); + argPacks.ret = + getDecryptResult(homoDecryptConf, JsonUtil.parseObject(argPacks.ret)); if (null != argPacks.ret) { return argPacks; } diff --git a/src/main/java/org/bdware/sc/engine/hook/HomomorphicEncryptHandler.java b/src/main/java/org/bdware/sc/engine/hook/HomomorphicEncryptHandler.java index 1d553dc..fb86e23 100644 --- a/src/main/java/org/bdware/sc/engine/hook/HomomorphicEncryptHandler.java +++ b/src/main/java/org/bdware/sc/engine/hook/HomomorphicEncryptHandler.java @@ -29,36 +29,36 @@ public class HomomorphicEncryptHandler implements AnnotationHook { public ArgPacks handle(JSEngine engine, ArgPacks argPacks) { try { ContractRequest input = argPacks.request; - LOGGER.info( - "HomomorphicEncryptHandler--------------------------------1: " - + input.getRequester()); - LOGGER.info( - "HomomorphicEncryptHandler--------------------------------2: " - + this.fun.getSecretID()); + LOGGER.info("HomomorphicEncryptHandler--------------------------------1: " + + input.getRequester()); + LOGGER.info("HomomorphicEncryptHandler--------------------------------2: " + + this.fun.getSecretID()); JsonElement response = (JsonElement) argPacks.ret; JsonElement homoEncryptConf = this.fun.getHomoEncryptConf(); if (homoEncryptConf != null && !homoEncryptConf.isJsonNull()) { - String res = - (String) JavaScriptEntry.executeContract( - "keyManager_1", - "getPubKey", - this.fun.getSecretID().replaceAll("\"", "")); -// String res = -// JavaScriptEntry.executeContract( -// this.fun.getKeyManagerID(), -// "getPubKey", -// this.fun.getSecretID().replaceAll("\"", "")); + String res = (String) JavaScriptEntry.executeContract("keyManager_1", "getPubKey", + this.fun.getSecretID().replaceAll("\"", "")); + // String res = + // JavaScriptEntry.executeContract( + // this.fun.getKeyManagerID(), + // "getPubKey", + // this.fun.getSecretID().replaceAll("\"", "")); LOGGER.info("HomomorphicEncryptHandler--------------------------------4: " + res); ContractResult results = JsonUtil.fromJson(res, ContractResult.class); String pubKeyStr = results.result.getAsString(); - LOGGER.info("HomomorphicEncryptHandler--------------------------------5: " + pubKeyStr); + LOGGER.info( + "HomomorphicEncryptHandler--------------------------------5: " + pubKeyStr); HomoVisitor.publicKey = (RSAPublicKey) PaillierKeyPair.pemToPublicKey(pubKeyStr); -// if (homoEncryptConf.getAsJsonPrimitive().isString()) -// homoEncryptConf = JsonParser.parseString(homoEncryptConf.getAsString()); - LOGGER.info("HomomorphicEncryptHandler--------------------------------6: " + homoEncryptConf); - LOGGER.info("HomomorphicEncryptHandler--------------------------------7: " + argPacks.ret); - LOGGER.info("HomomorphicEncryptHandler--------------------------------8: " + argPacks.ret.toString()); - // LOGGER.info("HomomorphicEncryptHandler--------------------------------9: " + JsonUtil.toJson(ret)); + // if (homoEncryptConf.getAsJsonPrimitive().isString()) + // homoEncryptConf = JsonParser.parseString(homoEncryptConf.getAsString()); + LOGGER.info("HomomorphicEncryptHandler--------------------------------6: " + + homoEncryptConf); + LOGGER.info("HomomorphicEncryptHandler--------------------------------7: " + + argPacks.ret); + LOGGER.info("HomomorphicEncryptHandler--------------------------------8: " + + argPacks.ret.toString()); + // LOGGER.info("HomomorphicEncryptHandler--------------------------------9: " + + // JsonUtil.toJson(ret)); argPacks.ret = getEncryptResult(homoEncryptConf, response); if (argPacks.ret != null) { return argPacks; @@ -73,9 +73,9 @@ public class HomomorphicEncryptHandler implements AnnotationHook { } private Object getEncryptResult(JsonElement homoEncryptConf, JsonElement data) { -// if (null == homoEncryptConf || homoEncryptConf.getAsString().isEmpty()) { -// return data; -// } + // if (null == homoEncryptConf || homoEncryptConf.getAsString().isEmpty()) { + // return data; + // } if (null == homoEncryptConf) { return data; } diff --git a/src/main/java/org/bdware/sc/engine/hook/MaskHandler.java b/src/main/java/org/bdware/sc/engine/hook/MaskHandler.java index 34f424d..6ecdf05 100644 --- a/src/main/java/org/bdware/sc/engine/hook/MaskHandler.java +++ b/src/main/java/org/bdware/sc/engine/hook/MaskHandler.java @@ -25,14 +25,14 @@ public class MaskHandler implements AnnotationHook { if (null != maskConf) { LOGGER.debug("execute maskConf: " + maskConf); String s1 = ret.toString(); - //budeijin - //"{\"count\":1}" - //{"count":1} - //System.out.println(s1); + // budeijin + // "{\"count\":1}" + // {"count":1} + // System.out.println(s1); s1 = s1.replace("\\", ""); s1 = s1.substring(1, s1.length() - 1); - //System.out.println(s1); - //System.out.println(JsonParser.parseString(s1)); + // System.out.println(s1); + // System.out.println(JsonParser.parseString(s1)); MaskVisitor visitor = new MaskVisitor(JsonParser.parseString(s1)); visitor.visit(maskConf); ret = visitor.get(); diff --git a/src/main/java/org/bdware/sc/engine/hook/MaskVisitor.java b/src/main/java/org/bdware/sc/engine/hook/MaskVisitor.java index 20b932f..4082f6b 100644 --- a/src/main/java/org/bdware/sc/engine/hook/MaskVisitor.java +++ b/src/main/java/org/bdware/sc/engine/hook/MaskVisitor.java @@ -23,7 +23,7 @@ public class MaskVisitor extends JsonVisitor { JsonObject jo = root.getAsJsonObject(); for (String key : mask.keySet()) { if (jo.has(key)) { - //TODO + // TODO MaskVisitor visitor = new MaskVisitor(jo.get(key)); visitor.visit(mask.get(key)); jo.add(key, visitor.get()); @@ -58,7 +58,7 @@ public class MaskVisitor extends JsonVisitor { String method = primitive.getAsString(); try { String result = ""; - //md5不需要参数 + // md5不需要参数 if (method.equals("md5")) { MD5EncryptionImpl masker = new MD5EncryptionImpl(); result = masker.execute(root.getAsString()); @@ -69,7 +69,7 @@ public class MaskVisitor extends JsonVisitor { FormatPreservingEncryptionImpl masker = new FormatPreservingEncryptionImpl(); result = masker.execute(root.getAsString()); } - //edp需要精度的参数 + // edp需要精度的参数 else if (method.equals("edp")) { EpsilonDifferentialPrivacyImpl masker = new EpsilonDifferentialPrivacyImpl(); double epsilon = 1; @@ -87,11 +87,11 @@ public class MaskVisitor extends JsonVisitor { } - //String result = masker.execute(primitive.toString()); - //System.out.println(result); + // String result = masker.execute(primitive.toString()); + // System.out.println(result); - //root = new JsonPrimitive(root.getAsString().substring(0, 2)); - //https://github.com/guohf/DataX-Masking + // root = new JsonPrimitive(root.getAsString().substring(0, 2)); + // https://github.com/guohf/DataX-Masking return this; } } diff --git a/src/main/java/org/bdware/sc/engine/hook/MockTemplateHandler.java b/src/main/java/org/bdware/sc/engine/hook/MockTemplateHandler.java index e489a11..bbc3480 100644 --- a/src/main/java/org/bdware/sc/engine/hook/MockTemplateHandler.java +++ b/src/main/java/org/bdware/sc/engine/hook/MockTemplateHandler.java @@ -23,7 +23,8 @@ public class MockTemplateHandler implements AnnotationHook { MockUtil Mock = new MockUtil(); argPacks.ret = Mock.mock(template).toString(); return argPacks; - } else return argPacks; //When mock config is null defined just ignore. + } else + return argPacks; // When mock config is null defined just ignore. } } catch (Exception e) { e.printStackTrace(); diff --git a/src/main/java/org/bdware/sc/engine/hook/ReadMeHandler.java b/src/main/java/org/bdware/sc/engine/hook/ReadMeHandler.java index 3d56690..bf1df9f 100644 --- a/src/main/java/org/bdware/sc/engine/hook/ReadMeHandler.java +++ b/src/main/java/org/bdware/sc/engine/hook/ReadMeHandler.java @@ -13,10 +13,9 @@ import org.commonmark.parser.Parser; public class ReadMeHandler implements AnnotationHook { String getReadMeData(DesktopEngine desktopEngine, ContractRequest c) { - String fileReadme = - desktopEngine - .getResources() - .loadAsString("/assets/README.md"); // is "/README.md" not"./README.md"!!!! + String fileReadme = desktopEngine.getResources().loadAsString("/assets/README.md"); // is + // "/README.md" + // not"./README.md"!!!! // System.out.println("fileReadme:" + fileReadme); if (null == fileReadme) { return "项目目录下无预览文档"; @@ -30,18 +29,11 @@ public class ReadMeHandler implements AnnotationHook { while (visitor != null) { if (visitor instanceof Heading) { if (((Heading) visitor).getLevel() == 2) { - if (((Text) (visitor.getFirstChild())) - .getLiteral() + if (((Text) (visitor.getFirstChild())).getLiteral() .equals(targetFunction)) { FencedCodeBlock blockResult = - (FencedCodeBlock) - (visitor.getNext() - .getNext() - .getNext() - .getNext() - .getNext() - .getNext() - .getNext()); + (FencedCodeBlock) (visitor.getNext().getNext().getNext() + .getNext().getNext().getNext().getNext()); result = blockResult.getLiteral(); break; } diff --git a/src/main/java/org/bdware/sc/engine/hook/ResultSchemaHandler.java b/src/main/java/org/bdware/sc/engine/hook/ResultSchemaHandler.java index 4bfcce7..da6ae72 100644 --- a/src/main/java/org/bdware/sc/engine/hook/ResultSchemaHandler.java +++ b/src/main/java/org/bdware/sc/engine/hook/ResultSchemaHandler.java @@ -48,7 +48,7 @@ public class ResultSchemaHandler implements AnnotationHook { try { JsonElement obj = JsonParser.parseString(je.getAsString()); visitor = new ArgSchemaVisitor(obj); - //IMPORTANT automatically convert arg type here + // IMPORTANT automatically convert arg type here argPacks.ret = obj; } catch (Exception e) { e.printStackTrace(); @@ -78,7 +78,8 @@ public class ResultSchemaHandler implements AnnotationHook { return argPacks; } - public static ResultSchemaHandler fromAnnotationNode(FunctionNode funNode, AnnotationNode annoNode) { + public static ResultSchemaHandler fromAnnotationNode(FunctionNode funNode, + AnnotationNode annoNode) { return new ResultSchemaHandler(annoNode); } } diff --git a/src/main/java/org/bdware/sc/engine/setArgs/GetHomArgs.java b/src/main/java/org/bdware/sc/engine/setArgs/GetHomArgs.java index e1d5166..3109df2 100644 --- a/src/main/java/org/bdware/sc/engine/setArgs/GetHomArgs.java +++ b/src/main/java/org/bdware/sc/engine/setArgs/GetHomArgs.java @@ -3,8 +3,7 @@ package org.bdware.sc.engine.setArgs; public class GetHomArgs { public String secretID; - public GetHomArgs() { - } + public GetHomArgs() {} public GetHomArgs(String secretID) { this.secretID = secretID; diff --git a/src/main/java/org/bdware/sc/entity/DoipMessagePacker.java b/src/main/java/org/bdware/sc/entity/DoipMessagePacker.java index 86d67cd..2636795 100644 --- a/src/main/java/org/bdware/sc/entity/DoipMessagePacker.java +++ b/src/main/java/org/bdware/sc/entity/DoipMessagePacker.java @@ -11,8 +11,7 @@ public class DoipMessagePacker { // the well-composed DoipMessage public DoipMessage rawDoipMsg; - public DoipMessagePacker() { - } + public DoipMessagePacker() {} public DoipMessagePacker(String source, DoipMessage rawMsg) { this.source = source; @@ -28,6 +27,7 @@ public class DoipMessagePacker { } public Object toJSObject() { - return JSONTool.convertJsonElementToMirror(JsonUtil.parseObject(JsonDoipMessage.fromDoipMessage(rawDoipMsg))); + return JSONTool.convertJsonElementToMirror( + JsonUtil.parseObject(JsonDoipMessage.fromDoipMessage(rawDoipMsg))); } } diff --git a/src/main/java/org/bdware/sc/handler/ContractHandler.java b/src/main/java/org/bdware/sc/handler/ContractHandler.java index 1990e83..db178fa 100644 --- a/src/main/java/org/bdware/sc/handler/ContractHandler.java +++ b/src/main/java/org/bdware/sc/handler/ContractHandler.java @@ -104,8 +104,8 @@ public class ContractHandler extends MsgHandler implements Runnable { @Description("set members") public void setMembers(GetMessage msg, ResultCallback cb) { - cb.onResult(cs.setMembers(JsonUtil.fromJson(msg.arg, new TypeToken>() { - }.getType()))); + cb.onResult(cs.setMembers( + JsonUtil.fromJson(msg.arg, new TypeToken>() {}.getType()))); } @Description("get current contract name") diff --git a/src/main/java/org/bdware/sc/handler/DOOPRequestHandler.java b/src/main/java/org/bdware/sc/handler/DOOPRequestHandler.java index 49f730e..f81e164 100644 --- a/src/main/java/org/bdware/sc/handler/DOOPRequestHandler.java +++ b/src/main/java/org/bdware/sc/handler/DOOPRequestHandler.java @@ -42,14 +42,16 @@ public class DOOPRequestHandler implements DoipRequestHandler { @Override public DoipMessage onRequest(ChannelHandlerContext ctx, DoipMessage msg) { String str = msg.header.parameters.operation; - if (msg.header != null && msg.header.parameters.operation.equals(SharableVarManager.SHARABLEOP)) { + if (msg.header != null + && msg.header.parameters.operation.equals(SharableVarManager.SHARABLEOP)) { return SharableVarManager.instance.handleSyncMessage(msg); } logger.debug("[Call operation] name: " + str); if (str != null) { FunctionNode fn; fn = doipFunctionNodeMap.get(str); - if (fn == null) fn = doipFunctionNodeMap.get(BasicOperations.Unknown.getName()); + if (fn == null) + fn = doipFunctionNodeMap.get(BasicOperations.Unknown.getName()); if (fn != null) { return buildRequestAndInvokeEngine(fn, msg); } @@ -62,7 +64,8 @@ public class DOOPRequestHandler implements DoipRequestHandler { DoipMessagePacker arg = new DoipMessagePacker("doip", msg); try { // 改变调用的函数 + 构造DoipMessagePacker - Object ret = ContractProcess.instance.engine.executeWithoutLock(fn, contractRequest, arg); + Object ret = + ContractProcess.instance.engine.executeWithoutLock(fn, contractRequest, arg); DoipMessage finalDoipMsg = (DoipMessage) ret; finalDoipMsg.requestID = msg.requestID; if (finalDoipMsg.header.parameters == null) { @@ -71,16 +74,19 @@ public class DOOPRequestHandler implements DoipRequestHandler { if (finalDoipMsg.header.parameters.attributes == null) { finalDoipMsg.header.parameters.attributes = new JsonObject(); } - finalDoipMsg.header.parameters.attributes.addProperty("nodeID", String.valueOf(JavaScriptEntry.shardingID)); + finalDoipMsg.header.parameters.attributes.addProperty("nodeID", + String.valueOf(JavaScriptEntry.shardingID)); return finalDoipMsg; } catch (Exception e) { ByteArrayOutputStream bo = new ByteArrayOutputStream(); e.printStackTrace(new PrintStream(bo)); - DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder(); + DoipMessageFactory.DoipMessageBuilder builder = + new DoipMessageFactory.DoipMessageBuilder(); builder.createResponse(DoipResponseCode.Declined, msg); builder.setBody(bo.toByteArray()); builder.addAttributes("nodeID", String.valueOf(JavaScriptEntry.shardingID)); - logger.error("buildRequestAndInvokeEngine has something wrong, executeWithoutLock err or validateJsonElementRulesByArgSchemaVisitor err"); + logger.error( + "buildRequestAndInvokeEngine has something wrong, executeWithoutLock err or validateJsonElementRulesByArgSchemaVisitor err"); return builder.create(); } } diff --git a/src/main/java/org/bdware/sc/memory/JSEDump.java b/src/main/java/org/bdware/sc/memory/JSEDump.java index 1e10ec9..7a7fd58 100644 --- a/src/main/java/org/bdware/sc/memory/JSEDump.java +++ b/src/main/java/org/bdware/sc/memory/JSEDump.java @@ -7,13 +7,13 @@ public class JSEDump implements Serializable { long ranSeed; int numsOfCopies; - public JSEDump(long id,long ra,int nums){ + public JSEDump(long id, long ra, int nums) { invokeID = id; ranSeed = ra; numsOfCopies = nums; } - public void printContent(){ + public void printContent() { System.out.println("invokeID=" + invokeID); System.out.println("ranSeed=" + ranSeed); System.out.println("numsOfCopies=" + numsOfCopies); diff --git a/src/main/java/org/bdware/sc/memory/MOType.java b/src/main/java/org/bdware/sc/memory/MOType.java index 3aff8e6..be04869 100644 --- a/src/main/java/org/bdware/sc/memory/MOType.java +++ b/src/main/java/org/bdware/sc/memory/MOType.java @@ -4,37 +4,38 @@ import jdk.internal.dynalink.beans.StaticClass; import wrp.jdk.nashorn.api.scripting.ScriptObjectMirror; public enum MOType { - String(true), Int(true), Double(true), Boolean(true),JSObject(false), JSArray(false), JavaObject(false), Method(false), - Undefined(true), JSFunction(false),JSStatic(false); + String(true), Int(true), Double(true), Boolean(true), JSObject(false), JSArray( + false), JavaObject( + false), Method(false), Undefined(true), JSFunction(false), JSStatic(false); - private boolean isPrimitive; + private boolean isPrimitive; - MOType(boolean isPrimitive) { - this.isPrimitive = (isPrimitive); - } + MOType(boolean isPrimitive) { + this.isPrimitive = (isPrimitive); + } - public static MOType getType(Object obj) { - if (obj == null) - return Undefined; - if (obj instanceof Integer) { - return Int; - } else if (obj instanceof Double) { - return Double; - } else if (obj instanceof String) { - return String; - } else if (obj instanceof ScriptObjectMirror) { - // ------ - return JSObject; - }else if(obj instanceof StaticClass) { - return JSStatic; - }else if(obj instanceof Boolean){ - return Boolean; - } - return JSObject; - } + public static MOType getType(Object obj) { + if (obj == null) + return Undefined; + if (obj instanceof Integer) { + return Int; + } else if (obj instanceof Double) { + return Double; + } else if (obj instanceof String) { + return String; + } else if (obj instanceof ScriptObjectMirror) { + // ------ + return JSObject; + } else if (obj instanceof StaticClass) { + return JSStatic; + } else if (obj instanceof Boolean) { + return Boolean; + } + return JSObject; + } - public boolean isPrimitive() { - return isPrimitive; - } + public boolean isPrimitive() { + return isPrimitive; + } -} \ No newline at end of file +} diff --git a/src/main/java/org/bdware/sc/memory/MemoryArrayObject.java b/src/main/java/org/bdware/sc/memory/MemoryArrayObject.java index 99fc177..88cf6b6 100644 --- a/src/main/java/org/bdware/sc/memory/MemoryArrayObject.java +++ b/src/main/java/org/bdware/sc/memory/MemoryArrayObject.java @@ -1,12 +1,12 @@ package org.bdware.sc.memory; -public class MemoryArrayObject extends MemoryJSObject{ +public class MemoryArrayObject extends MemoryJSObject { - private static final long serialVersionUID = -5805776423219733634L; + private static final long serialVersionUID = -5805776423219733634L; - public MemoryArrayObject(long id) { - super(id); - type = MOType.JSArray; - } + public MemoryArrayObject(long id) { + super(id); + type = MOType.JSArray; + } } diff --git a/src/main/java/org/bdware/sc/memory/MemoryDump.java b/src/main/java/org/bdware/sc/memory/MemoryDump.java index aabda2c..0219d9d 100644 --- a/src/main/java/org/bdware/sc/memory/MemoryDump.java +++ b/src/main/java/org/bdware/sc/memory/MemoryDump.java @@ -19,9 +19,9 @@ import java.util.Map.Entry; public class MemoryDump implements Serializable { transient long id; - transient Map allocated; //js对象,id + transient Map allocated; // js对象,id - Map objects; //id,memory对象 + Map objects; // id,memory对象 JSEDump jseDump; transient Map recreate; @@ -99,12 +99,11 @@ public class MemoryDump implements Serializable { id++; currID = id; if (obj.getClass() == jdk.internal.dynalink.beans.StaticClass.class) { - /* - String obj2 = "jdk.internal.dynalink.beans.StaticClass.class"; - if (allocated.containsKey(obj2)) - return allocated.get(obj2); - allocated.put(obj2, currID); - */ + /* + * String obj2 = "jdk.internal.dynalink.beans.StaticClass.class"; if + * (allocated.containsKey(obj2)) return allocated.get(obj2); allocated.put(obj2, + * currID); + */ } else { if (allocated.containsKey(obj)) return allocated.get(obj); @@ -137,12 +136,12 @@ public class MemoryDump implements Serializable { } else if (obj.getClass() == wrp.jdk.nashorn.internal.runtime.Undefined.class) { } else if (obj.getClass() == jdk.internal.dynalink.beans.StaticClass.class) { - //regard as String + // regard as String -// MemoryObject mo = new MemoryObject(currID); -// mo.type = MOType.String; -// mo.data = "jdk.internal.dynalink.beans.StaticClass"; -// objects.put(currID, mo); + // MemoryObject mo = new MemoryObject(currID); + // mo.type = MOType.String; + // mo.data = "jdk.internal.dynalink.beans.StaticClass"; + // objects.put(currID, mo); } else { MOType type = MOType.getType(obj); if (type.isPrimitive()) { @@ -151,7 +150,8 @@ public class MemoryDump implements Serializable { mo.data = obj; objects.put(currID, mo); } else - System.out.println("[MemoryDump] Allocat MetType:" + obj.getClass() + " now id=" + currID); + System.out.println( + "[MemoryDump] Allocat MetType:" + obj.getClass() + " now id=" + currID); } return currID; @@ -203,10 +203,12 @@ public class MemoryDump implements Serializable { } switch (mo.type) { case JSArray: - obj = ScriptObjectMirror.wrap(Global.allocate(new int[0]), JavaScriptEntry.getEngineGlobal()); + obj = ScriptObjectMirror.wrap(Global.allocate(new int[0]), + JavaScriptEntry.getEngineGlobal()); break; case JSObject: - obj = ScriptObjectMirror.wrap(new JO(PropertyMap.newMap()), JavaScriptEntry.getEngineGlobal()); + obj = ScriptObjectMirror.wrap(new JO(PropertyMap.newMap()), + JavaScriptEntry.getEngineGlobal()); break; case JSFunction: break; @@ -226,4 +228,4 @@ public class MemoryDump implements Serializable { recreate.put(key, obj); } } -} \ No newline at end of file +} diff --git a/src/main/java/org/bdware/sc/memory/MemoryDumpUtil.java b/src/main/java/org/bdware/sc/memory/MemoryDumpUtil.java index 7c9c8c2..c6bcc0c 100644 --- a/src/main/java/org/bdware/sc/memory/MemoryDumpUtil.java +++ b/src/main/java/org/bdware/sc/memory/MemoryDumpUtil.java @@ -13,7 +13,7 @@ import java.util.zip.GZIPOutputStream; public class MemoryDumpUtil { public static final String STATELESS_MEMORY = "statelessContractMemory"; - public static AtomicInteger checkPointCounter; //用于common模式下的检查点计数 + public static AtomicInteger checkPointCounter; // 用于common模式下的检查点计数 NashornScriptEngine engine; String dumpContent; MemoryDump memoryDump = null; @@ -30,11 +30,12 @@ public class MemoryDumpUtil { GZIPInputStream gzin = new GZIPInputStream(fileout); reader = new ObjectInputStream(gzin); MemoryDump memoryDump = new MemoryDump(); - //memoryDump.objects = (Map) reader.readObject(); + // memoryDump.objects = (Map) reader.readObject(); memoryDump = (MemoryDump) reader.readObject(); reader.close(); String ret = JsonUtil.toPrettyJson(memoryDump.objects); - ret += ("" + memoryDump.jseDump.invokeID + ";" + memoryDump.jseDump.ranSeed + ";" + memoryDump.jseDump.numsOfCopies + ""); + ret += ("" + memoryDump.jseDump.invokeID + ";" + memoryDump.jseDump.ranSeed + + ";" + memoryDump.jseDump.numsOfCopies + ""); return ret; } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); @@ -43,23 +44,14 @@ public class MemoryDumpUtil { } /* - public static String getContentFromFile2(String path) { - File file = new File(path); - ObjectInputStream reader; - try { - FileInputStream fileout = new FileInputStream(file); - reader = new ObjectInputStream(fileout); - String ret = (String)reader.readObject(); - reader.close(); - return ret; - } catch (IOException | ClassNotFoundException e) { - e.printStackTrace(); - } - return null; - } - */ + * public static String getContentFromFile2(String path) { File file = new File(path); + * ObjectInputStream reader; try { FileInputStream fileout = new FileInputStream(file); reader = + * new ObjectInputStream(fileout); String ret = (String)reader.readObject(); reader.close(); + * return ret; } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } return + * null; } + */ - //stateful 表示合约是有/无状态合约 + // stateful 表示合约是有/无状态合约 public String dumpMemory(String path, boolean stateful) { synchronized (engine) { String ret; @@ -73,16 +65,21 @@ public class MemoryDumpUtil { long id = memoryDump.allocate(obj); root.addField(key, id); } - memoryDump.jseDump = new JSEDump(JavaScriptEntry.invokeID, Long.parseLong(JavaScriptEntry.currentSyncUtil.contractID), JavaScriptEntry.numOfCopies); + memoryDump.jseDump = new JSEDump(JavaScriptEntry.invokeID, + Long.parseLong(JavaScriptEntry.currentSyncUtil.contractID), + JavaScriptEntry.numOfCopies); ret = JsonUtil.toPrettyJson(memoryDump.objects); - } else { //无状态合约 - memoryDump.jseDump = new JSEDump(JavaScriptEntry.invokeID, Long.parseLong(JavaScriptEntry.currentSyncUtil.contractID), JavaScriptEntry.numOfCopies); + } else { // 无状态合约 + memoryDump.jseDump = new JSEDump(JavaScriptEntry.invokeID, + Long.parseLong(JavaScriptEntry.currentSyncUtil.contractID), + JavaScriptEntry.numOfCopies); memoryDump.objects.clear(); ret = JsonUtil.toPrettyJson(memoryDump.objects); } ret += "--seperate--"; - ret += (memoryDump.jseDump.invokeID + ";" + memoryDump.jseDump.ranSeed + ";" + memoryDump.jseDump.numsOfCopies); + ret += (memoryDump.jseDump.invokeID + ";" + memoryDump.jseDump.ranSeed + ";" + + memoryDump.jseDump.numsOfCopies); if (path == null || path.equals("")) { return ret; @@ -90,13 +87,14 @@ public class MemoryDumpUtil { File mem = new File(path); File parent = mem.getParentFile(); - if (!parent.exists()) parent.mkdirs(); + if (!parent.exists()) + parent.mkdirs(); ObjectOutputStream writer; try { FileOutputStream fileout = new FileOutputStream(mem); GZIPOutputStream out = new GZIPOutputStream(fileout); writer = new ObjectOutputStream(out); - //writer.writeObject(memoryDump.objects); + // writer.writeObject(memoryDump.objects); writer.writeObject(memoryDump); writer.flush(); writer.close(); @@ -112,44 +110,25 @@ public class MemoryDumpUtil { /* - public String dumpMemory(String path) { - memoryDump = new MemoryDump(); - Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE); - System.out.println("[MemoryDumpUtil] bindings size=" + bindings.size()); - MemoryJSObject root = memoryDump.getRoot(); - for (String key : bindings.keySet()) { - System.out.println("[MemoryDumpUtil] dumpMemory " + key); - Object obj = bindings.get(key); - long id = memoryDump.allocate(obj); - root.addField(key, id); - - System.out.println("[root addFiled] key=" + key + " id=" + id); - } - String ret = JsonUtil.toPrettyJson(memoryDump); - dumpContent = ret; - - if(path == null || path.equals("")) { - return ret; - } - - File mem = new File(path); - File parent = mem.getParentFile(); - if (!parent.exists()) - parent.mkdirs(); - ObjectOutputStream writer; - try { - FileOutputStream fileout = new FileOutputStream(mem); - writer = new ObjectOutputStream(fileout); - writer.writeObject(dumpContent); - - writer.flush(); - writer.close(); - } catch (IOException e) { - e.printStackTrace(); - } - - memoryDump = null; - return ret; - } - */ + * public String dumpMemory(String path) { memoryDump = new MemoryDump(); Bindings bindings = + * engine.getBindings(ScriptContext.ENGINE_SCOPE); + * System.out.println("[MemoryDumpUtil] bindings size=" + bindings.size()); MemoryJSObject root + * = memoryDump.getRoot(); for (String key : bindings.keySet()) { + * System.out.println("[MemoryDumpUtil] dumpMemory " + key); Object obj = bindings.get(key); + * long id = memoryDump.allocate(obj); root.addField(key, id); + * + * System.out.println("[root addFiled] key=" + key + " id=" + id); } String ret = + * JsonUtil.toPrettyJson(memoryDump); dumpContent = ret; + * + * if(path == null || path.equals("")) { return ret; } + * + * File mem = new File(path); File parent = mem.getParentFile(); if (!parent.exists()) + * parent.mkdirs(); ObjectOutputStream writer; try { FileOutputStream fileout = new + * FileOutputStream(mem); writer = new ObjectOutputStream(fileout); + * writer.writeObject(dumpContent); + * + * writer.flush(); writer.close(); } catch (IOException e) { e.printStackTrace(); } + * + * memoryDump = null; return ret; } + */ } diff --git a/src/main/java/org/bdware/sc/memory/MemoryFunctionObject.java b/src/main/java/org/bdware/sc/memory/MemoryFunctionObject.java index 996f261..8c3e793 100644 --- a/src/main/java/org/bdware/sc/memory/MemoryFunctionObject.java +++ b/src/main/java/org/bdware/sc/memory/MemoryFunctionObject.java @@ -4,16 +4,16 @@ import java.util.HashMap; import java.util.Map; public class MemoryFunctionObject extends MemoryObject { - private static final long serialVersionUID = 5169037078273981613L; - Map fields; - - public MemoryFunctionObject(long id) { - super(id); - fields = new HashMap<>(); - type = MOType.JSFunction; - } - - public void addField(String key, long id) { - fields.put(key, id); - } -} \ No newline at end of file + private static final long serialVersionUID = 5169037078273981613L; + Map fields; + + public MemoryFunctionObject(long id) { + super(id); + fields = new HashMap<>(); + type = MOType.JSFunction; + } + + public void addField(String key, long id) { + fields.put(key, id); + } +} diff --git a/src/main/java/org/bdware/sc/memory/MemoryJSObject.java b/src/main/java/org/bdware/sc/memory/MemoryJSObject.java index 25ea3f8..a025aac 100644 --- a/src/main/java/org/bdware/sc/memory/MemoryJSObject.java +++ b/src/main/java/org/bdware/sc/memory/MemoryJSObject.java @@ -1,19 +1,18 @@ package org.bdware.sc.memory; import java.util.LinkedHashMap; -import java.util.Map; public class MemoryJSObject extends MemoryObject { - private static final long serialVersionUID = -2290414347562477503L; - LinkedHashMap fields; + private static final long serialVersionUID = -2290414347562477503L; + LinkedHashMap fields; - public MemoryJSObject(long id) { - super(id); - fields = new LinkedHashMap<>(); - type = MOType.JSObject; - } + public MemoryJSObject(long id) { + super(id); + fields = new LinkedHashMap<>(); + type = MOType.JSObject; + } - public void addField(String key, long id) { - fields.put(key, id); - } -} \ No newline at end of file + public void addField(String key, long id) { + fields.put(key, id); + } +} diff --git a/src/main/java/org/bdware/sc/memory/MemoryRecoverUtil.java b/src/main/java/org/bdware/sc/memory/MemoryRecoverUtil.java index 5034ea0..6f6c648 100644 --- a/src/main/java/org/bdware/sc/memory/MemoryRecoverUtil.java +++ b/src/main/java/org/bdware/sc/memory/MemoryRecoverUtil.java @@ -28,7 +28,7 @@ public class MemoryRecoverUtil { this.resource = r; } - //支持传入memory文件路径或者直接是memory的字符串 + // 支持传入memory文件路径或者直接是memory的字符串 public void loadMemory(String path, boolean stateful) { synchronized (engine) { File file = new File(path); @@ -39,9 +39,9 @@ public class MemoryRecoverUtil { GZIPInputStream gzin = new GZIPInputStream(fileout); ObjectInputStream reader = new ObjectInputStream(gzin); -// MemoryDump temp = new MemoryDump(); -// temp.objects = (Map) reader.readObject(); -// String content = JsonUtil.toPrettyJson(temp); + // MemoryDump temp = new MemoryDump(); + // temp.objects = (Map) reader.readObject(); + // String content = JsonUtil.toPrettyJson(temp); MemoryDump temp = (MemoryDump) reader.readObject(); String content = JsonUtil.toPrettyJson(temp.objects); @@ -67,7 +67,7 @@ public class MemoryRecoverUtil { } } - //memoryDump = MemoryDump.loadFromStr(content); + // memoryDump = MemoryDump.loadFromStr(content); if (stateful) { memoryDump = temp; } @@ -75,7 +75,7 @@ public class MemoryRecoverUtil { } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } - } else { //直接传入的是字符串 + } else { // 直接传入的是字符串 String[] strs = path.split("--seperate--"); String content = strs[0]; String jse = strs[1]; @@ -103,7 +103,7 @@ public class MemoryRecoverUtil { } } - if (stateful) { //有状态合约 + if (stateful) { // 有状态合约 memoryDump = MemoryDump.loadFromStr(content); } } @@ -114,7 +114,8 @@ public class MemoryRecoverUtil { ScriptObjectMirror global = (ScriptObjectMirror) objects.get(0L); Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE); for (Object key : global.keySet()) { - if (global.get(key) != null) bindings.put((String) key, global.get(key)); + if (global.get(key) != null) + bindings.put((String) key, global.get(key)); } } this.memoryDump = null; @@ -127,34 +128,20 @@ public class MemoryRecoverUtil { } /* - public void loadMemory(String path) { - try { - String memory; - File mem = new File(path); - FileInputStream fileout = new FileInputStream(mem); - ObjectInputStream reader = new ObjectInputStream(fileout); - loadContent = (String) reader.readObject(); - // System.out.println("[MemoryRecoverUtil] loadContent : \n" + loadContent); - reader.close(); - memoryDump = MemoryDump.loadFromStr(loadContent); - - - String ret = JsonUtil.toPrettyJson(memoryDump); - MemoryJSObject root = memoryDump.getRoot(); - Map objects = memoryDump.recreateObject(); - ScriptObjectMirror global = (ScriptObjectMirror) objects.get(0L); - Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE); - for (Object key : global.keySet()) { - if (global.get(key) != null) - bindings.put((String) key, global.get(key)); - } - this.memoryDump = null; - if (resource != null) - ((Invocable) engine).invokeFunction("defineProp", "Resources", resource); - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - */ + * public void loadMemory(String path) { try { String memory; File mem = new File(path); + * FileInputStream fileout = new FileInputStream(mem); ObjectInputStream reader = new + * ObjectInputStream(fileout); loadContent = (String) reader.readObject(); // + * System.out.println("[MemoryRecoverUtil] loadContent : \n" + loadContent); reader.close(); + * memoryDump = MemoryDump.loadFromStr(loadContent); + * + * + * String ret = JsonUtil.toPrettyJson(memoryDump); MemoryJSObject root = memoryDump.getRoot(); + * Map objects = memoryDump.recreateObject(); ScriptObjectMirror global = + * (ScriptObjectMirror) objects.get(0L); Bindings bindings = + * engine.getBindings(ScriptContext.ENGINE_SCOPE); for (Object key : global.keySet()) { if + * (global.get(key) != null) bindings.put((String) key, global.get(key)); } this.memoryDump = + * null; if (resource != null) ((Invocable) engine).invokeFunction("defineProp", "Resources", + * resource); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } + * } + */ } diff --git a/src/main/java/org/bdware/sc/redo/TransRecordUtil.java b/src/main/java/org/bdware/sc/redo/TransRecordUtil.java index 530872b..1bf2fe4 100644 --- a/src/main/java/org/bdware/sc/redo/TransRecordUtil.java +++ b/src/main/java/org/bdware/sc/redo/TransRecordUtil.java @@ -11,11 +11,12 @@ import java.util.Map; import java.util.TreeMap; public class TransRecordUtil { - public static final int RESERVED = 20; //最近的多少次records内存也保存,如果一个节点距离集群当前的seq相差超过这个值,就不从本地恢复,让别的节点现场dump - public static final int DUMP_PERIOD = 50; //每满50次记录,就记录一次全量状态,清一次trans记录 + public static final int RESERVED = 20; // 最近的多少次records内存也保存,如果一个节点距离集群当前的seq相差超过这个值,就不从本地恢复,让别的节点现场dump + public static final int DUMP_PERIOD = 50; // 每满50次记录,就记录一次全量状态,清一次trans记录 private static final Logger LOGGER = LogManager.getLogger(TransRecordUtil.class); - public Map cacheTransRecords = new TreeMap(); //TODO 认为其中records一定是seq连续的,否则可能有问题? - //public PriorityQueue transRecords = new PriorityQueue(); + public Map cacheTransRecords = new TreeMap(); // TODO + // 认为其中records一定是seq连续的,否则可能有问题? + // public PriorityQueue transRecords = new PriorityQueue(); public TransRecord currentTransRecord; SyncMechUtil syncUtil; DesktopEngine engine; @@ -30,20 +31,20 @@ public class TransRecordUtil { fileName = path; } - //每次事务开始时初始化 + // 每次事务开始时初始化 public void startNext(String fun, JsonElement arg, int sequence) { - //logger.debug("TransRecordUtil 开始记录事务"); + // logger.debug("TransRecordUtil 开始记录事务"); currentTransRecord = new TransRecord(fun, arg, sequence); } public void startNext(String fun, JsonElement arg) { - //logger.debug("TransRecordUtil 开始记录事务"); + // logger.debug("TransRecordUtil 开始记录事务"); currentTransRecord = new TransRecord(fun, arg); } - //每次事务结束时记录 + // 每次事务结束时记录 public synchronized void eachFinish() { - //logger.debug("TransRecordUtil 记录完一个事务 \n" + currentTransRecord.toString()); + // logger.debug("TransRecordUtil 记录完一个事务 \n" + currentTransRecord.toString()); cacheTransRecords.put(currentTransRecord.seq, currentTransRecord); if (cacheTransRecords.size() == RESERVED) { int temp = 0; @@ -54,10 +55,10 @@ public class TransRecordUtil { cacheTransRecords.remove(temp); } - appendTransFile(currentTransRecord); //执行前已经定序了,所以trans其实没必要定序 + appendTransFile(currentTransRecord); // 执行前已经定序了,所以trans其实没必要定序 } - //追加写入最后一个TransRecord + // 追加写入最后一个TransRecord public void appendTransFile(TransRecord record) { syncUtil.filedTrans.getAndIncrement(); @@ -78,7 +79,7 @@ public class TransRecordUtil { e.printStackTrace(); } - //自动触发检查点 + // 自动触发检查点 if (syncUtil.filedTrans.get() == DUMP_PERIOD) { LOGGER.info("自动触发检查点 DUMP_PERIOD=" + DUMP_PERIOD); file = new File(syncUtil.memoryDir); @@ -106,7 +107,7 @@ public class TransRecordUtil { } } - //sync文件删除第2,3行 + // sync文件删除第2,3行 file = new File(syncUtil.syncDir + "/" + syncUtil.syncFileName); String firstLine = ""; try { @@ -143,7 +144,7 @@ public class TransRecordUtil { } } - //record executeContract in current trans + // record executeContract in current trans public void recordExecutes(String k, String v) { if (currentTransRecord != null) { currentTransRecord.executes.put(k, v); @@ -155,13 +156,13 @@ public class TransRecordUtil { public String getCachedTransRecords(int start) { StringBuilder str = new StringBuilder(); - //先查看有没有第1个 + // 先查看有没有第1个 if (!cacheTransRecords.containsKey(start)) return ""; synchronized (cacheTransRecords) { int temp = -1; - int j = start - 1;//确保有从start开始的连续trans,j记录上一个i值 + int j = start - 1;// 确保有从start开始的连续trans,j记录上一个i值 for (Integer i : cacheTransRecords.keySet()) { if (i >= start) { if (i == (j + 1)) { diff --git a/src/main/java/org/bdware/sc/redo/TransRecoverUtil.java b/src/main/java/org/bdware/sc/redo/TransRecoverUtil.java index 5b5f4bf..ffdafa6 100644 --- a/src/main/java/org/bdware/sc/redo/TransRecoverUtil.java +++ b/src/main/java/org/bdware/sc/redo/TransRecoverUtil.java @@ -34,7 +34,7 @@ public class TransRecoverUtil { while ((line = br.readLine()) != null) { String[] strs = line.split(";"); String arg; - if (strs.length < 5) { //调用记录参数为空 + if (strs.length < 5) { // 调用记录参数为空 arg = null; } else { arg = strs[4]; @@ -46,7 +46,8 @@ public class TransRecoverUtil { System.out.println("恢复时加入 " + cur_read.toString()); } if (strs[1].equals("true")) - cur_read = new TransRecord(strs[3], JsonUtil.parseString(arg), Integer.parseInt(strs[2])); + cur_read = new TransRecord(strs[3], JsonUtil.parseString(arg), + Integer.parseInt(strs[2])); else cur_read = new TransRecord(strs[3], JsonUtil.parseString(arg)); } else { @@ -83,7 +84,8 @@ public class TransRecoverUtil { ac.seq = curRecoverRecord.seq; LOGGER.info("[TransRecordUtil] redo 重新执行事务 " + ac.seq); } - System.out.println("[TransRecoverUtil] recover " + ac.needSeq + " " + ac.seq + " " + ac.getAction() + " " + ac.getArg()); + System.out.println("[TransRecoverUtil] recover " + ac.needSeq + " " + ac.seq + " " + + ac.getAction() + " " + ac.getArg()); ContractResult result = engine.executeContract(ac); @@ -95,8 +97,8 @@ public class TransRecoverUtil { } } - //TODO + // TODO public void recoverFromATransRecord(TransRecord record) { } -} \ No newline at end of file +} diff --git a/src/main/java/org/bdware/sc/server/DoipClusterServer.java b/src/main/java/org/bdware/sc/server/DoipClusterServer.java index dad39d2..17e45ae 100644 --- a/src/main/java/org/bdware/sc/server/DoipClusterServer.java +++ b/src/main/java/org/bdware/sc/server/DoipClusterServer.java @@ -42,7 +42,8 @@ public class DoipClusterServer extends DoipServerImpl { return instance; } - public static int startDoipServer(int startPort, JsonElement otherConfigs) throws InterruptedException { + public static int startDoipServer(int startPort, JsonElement otherConfigs) + throws InterruptedException { try { int ret = DoipLocalSingleton.run(startPort, otherConfigs); return ret; @@ -56,7 +57,10 @@ public class DoipClusterServer extends DoipServerImpl { repoIrpClient = new AuditIrpClient(config); EndpointInfo info = repoIrpClient.getEndpointInfo(); if (info == null) { - String content = "{\"date\":\"2022-1-13\",\"name\":\"testrepoforaibd\",\"doId\":\"bdware.test.local/Repo\",\"address\":\"tcp://127.0.0.1:" + defaultDOIPServerPort + "\",\"status\":\"已审核\",\"protocol\":\"DOIP\",\"pubKey\":\"empty\",\"version\":\"2.1\"}"; + String content = + "{\"date\":\"2022-1-13\",\"name\":\"testrepoforaibd\",\"doId\":\"bdware.test.local/Repo\",\"address\":\"tcp://127.0.0.1:" + + defaultDOIPServerPort + + "\",\"status\":\"已审核\",\"protocol\":\"DOIP\",\"pubKey\":\"empty\",\"version\":\"2.1\"}"; info = EndpointInfo.fromJson(content); } List infos = new ArrayList<>(); @@ -65,7 +69,8 @@ public class DoipClusterServer extends DoipServerImpl { } catch (Exception e) { e.printStackTrace(); } - DoipServiceInfo ret = new DoipServiceInfo(info.getDoId(), info.getPubKey(), defaultRepoType, infos); + DoipServiceInfo ret = + new DoipServiceInfo(info.getDoId(), info.getPubKey(), defaultRepoType, infos); return ret; } @@ -91,12 +96,16 @@ public class DoipClusterServer extends DoipServerImpl { JsonObject functions = new JsonObject(); // 维护RouteInfo,将RouteInfo和doipOperationName的映射关系,以及所有Router中用得到的函数都维护好 - //TODO 移除这部分逻辑? + // TODO 移除这部分逻辑? cn.maintainRouteJoinInfo(methodRouteInfoMap, methodJoinInfoMap, functions); - if (clusterInfo != null) repoHandleValues.add("clusterInfo", clusterInfo); - if (functions.size() > 0) repoHandleValues.add("functions", functions); - if (methodRouteInfoMap.size() > 0) repoHandleValues.add("routeInfo", methodRouteInfoMap); - if (methodJoinInfoMap.size() > 0) repoHandleValues.add("joinInfo", methodJoinInfoMap); + if (clusterInfo != null) + repoHandleValues.add("clusterInfo", clusterInfo); + if (functions.size() > 0) + repoHandleValues.add("functions", functions); + if (methodRouteInfoMap.size() > 0) + repoHandleValues.add("routeInfo", methodRouteInfoMap); + if (methodJoinInfoMap.size() > 0) + repoHandleValues.add("joinInfo", methodJoinInfoMap); repoInfo.handleValues.addProperty("cluster", repoHandleValues.toString()); String updateRepoInfoRes = repoIrpClient.reRegister(repoInfo); if (updateRepoInfoRes.equals("success")) { @@ -104,7 +113,8 @@ public class DoipClusterServer extends DoipServerImpl { } else if (updateRepoInfoRes.equals("failed")) { LOGGER.error("Failed to update cluster info to router"); } else { - LOGGER.warn("Oops...The result of updating clusterInfo to the router is " + updateRepoInfoRes); + LOGGER.warn("Oops...The result of updating clusterInfo to the router is " + + updateRepoInfoRes); } } diff --git a/src/main/java/org/bdware/sc/server/DoipLocalSingleton.java b/src/main/java/org/bdware/sc/server/DoipLocalSingleton.java index 663e734..6afe465 100644 --- a/src/main/java/org/bdware/sc/server/DoipLocalSingleton.java +++ b/src/main/java/org/bdware/sc/server/DoipLocalSingleton.java @@ -100,7 +100,8 @@ public class DoipLocalSingleton { ResultChecker checker = new ResultChecker(); server.start(checker); checker.waitForResult(1000); - if (checker.port > 0) return port; + if (checker.port > 0) + return port; return -1; } diff --git a/src/main/java/org/bdware/sc/syncMech/EstimateUtil.java b/src/main/java/org/bdware/sc/syncMech/EstimateUtil.java index bb2e992..69a214d 100644 --- a/src/main/java/org/bdware/sc/syncMech/EstimateUtil.java +++ b/src/main/java/org/bdware/sc/syncMech/EstimateUtil.java @@ -3,9 +3,9 @@ package org.bdware.sc.syncMech; public class EstimateUtil { public static final String DUMP_TIME = "dumpTime"; public static final String DUMP_TIMES = "dumpTimes"; - long dumpTime; //用时 - long dumpTimes; //次数 - long dumpFile; //状态大小,dumpFile的次数也是dumpTimes + long dumpTime; // 用时 + long dumpTimes; // 次数 + long dumpFile; // 状态大小,dumpFile的次数也是dumpTimes public static final String LOADMEMORY_TIME = "loadMemoryTime"; public static final String LOADMEMORY_TIMES = "loadMemoryTimes"; diff --git a/src/main/java/org/bdware/sc/syncMech/SyncRecord.java b/src/main/java/org/bdware/sc/syncMech/SyncRecord.java index 85ca93f..a0d510c 100644 --- a/src/main/java/org/bdware/sc/syncMech/SyncRecord.java +++ b/src/main/java/org/bdware/sc/syncMech/SyncRecord.java @@ -2,43 +2,43 @@ package org.bdware.sc.syncMech; import java.io.Serializable; -public class SyncRecord implements Serializable{ - SyncType type; - String fileName; //eg:/memory/2020-... +public class SyncRecord implements Serializable { + SyncType type; + String fileName; // eg:/memory/2020-... - public SyncRecord(SyncType t) { - this.type = t; - } + public SyncRecord(SyncType t) { + this.type = t; + } - public SyncRecord(SyncType t,String str){ - type = t; - fileName = str; - } + public SyncRecord(SyncType t, String str) { + type = t; + fileName = str; + } - public void setType(SyncType ty) { - this.type = ty; - } + public void setType(SyncType ty) { + this.type = ty; + } - public SyncType getType() { - return this.type; - } + public SyncType getType() { + return this.type; + } - public void setFileName(String file) { - this.fileName = file; - } + public void setFileName(String file) { + this.fileName = file; + } - public String getFileName() { - return this.fileName; - } + public String getFileName() { + return this.fileName; + } - public String getContent() { - StringBuilder str = new StringBuilder(); - str.append(type.toString() + ";" + fileName); - return str.toString(); - } + public String getContent() { + StringBuilder str = new StringBuilder(); + str.append(type.toString() + ";" + fileName); + return str.toString(); + } - public static SyncRecord loadFromString(String str){ - String[] strs = str.split(";"); - return new SyncRecord(SyncType.convert(strs[0]),strs[1]); - } + public static SyncRecord loadFromString(String str) { + String[] strs = str.split(";"); + return new SyncRecord(SyncType.convert(strs[0]), strs[1]); + } } diff --git a/src/main/java/org/bdware/sc/syncMech/SyncType.java b/src/main/java/org/bdware/sc/syncMech/SyncType.java index e07fda7..ac3959e 100644 --- a/src/main/java/org/bdware/sc/syncMech/SyncType.java +++ b/src/main/java/org/bdware/sc/syncMech/SyncType.java @@ -2,19 +2,19 @@ package org.bdware.sc.syncMech; import java.io.Serializable; -public enum SyncType implements Serializable{ - Memory, Trace, Trans; +public enum SyncType implements Serializable { + Memory, Trace, Trans; - public static SyncType convert(String str){ - switch (str){ - case "Memory": - return Memory; - case "Trace": - return Trace; - case "Trans": - return Trans; - default: - return null; - } - } -} \ No newline at end of file + public static SyncType convert(String str) { + switch (str) { + case "Memory": + return Memory; + case "Trace": + return Trace; + case "Trans": + return Trans; + default: + return null; + } + } +} diff --git a/src/main/java/org/bdware/sc/trace/JS.java b/src/main/java/org/bdware/sc/trace/JS.java index eb6d3e6..73c44ff 100644 --- a/src/main/java/org/bdware/sc/trace/JS.java +++ b/src/main/java/org/bdware/sc/trace/JS.java @@ -2,6 +2,6 @@ package org.bdware.sc.trace; import java.io.Serializable; -public class JS implements Serializable{ +public class JS implements Serializable { -} \ No newline at end of file +} diff --git a/src/main/java/org/bdware/sc/trace/JSArray.java b/src/main/java/org/bdware/sc/trace/JSArray.java index fd1f235..c1695c0 100644 --- a/src/main/java/org/bdware/sc/trace/JSArray.java +++ b/src/main/java/org/bdware/sc/trace/JSArray.java @@ -2,9 +2,9 @@ package org.bdware.sc.trace; import java.io.Serializable; -public class JSArray extends JSScript implements Serializable{ - - public JSArray(int id) { - this.objID = id; - } +public class JSArray extends JSScript implements Serializable { + + public JSArray(int id) { + this.objID = id; + } } diff --git a/src/main/java/org/bdware/sc/trace/JSNull.java b/src/main/java/org/bdware/sc/trace/JSNull.java index 1740fd1..89aa970 100644 --- a/src/main/java/org/bdware/sc/trace/JSNull.java +++ b/src/main/java/org/bdware/sc/trace/JSNull.java @@ -2,6 +2,6 @@ package org.bdware.sc.trace; import java.io.Serializable; -public class JSNull extends JS implements Serializable{ +public class JSNull extends JS implements Serializable { } diff --git a/src/main/java/org/bdware/sc/trace/JSObject.java b/src/main/java/org/bdware/sc/trace/JSObject.java index e7d04fa..b2ab28f 100644 --- a/src/main/java/org/bdware/sc/trace/JSObject.java +++ b/src/main/java/org/bdware/sc/trace/JSObject.java @@ -3,12 +3,11 @@ package org.bdware.sc.trace; import java.io.Serializable; /* - * js中对象 - * ScriptObject JO + * js中对象 ScriptObject JO */ -public class JSObject extends JSScript implements Serializable{ - - public JSObject(int id) { - this.objID = id; - } -} \ No newline at end of file +public class JSObject extends JSScript implements Serializable { + + public JSObject(int id) { + this.objID = id; + } +} diff --git a/src/main/java/org/bdware/sc/trace/JSScript.java b/src/main/java/org/bdware/sc/trace/JSScript.java index dd560b7..93841d9 100644 --- a/src/main/java/org/bdware/sc/trace/JSScript.java +++ b/src/main/java/org/bdware/sc/trace/JSScript.java @@ -2,10 +2,10 @@ package org.bdware.sc.trace; import java.io.Serializable; -public class JSScript extends JS implements Serializable{ - int objID; - - public int getObjID() { - return this.objID; - } -} \ No newline at end of file +public class JSScript extends JS implements Serializable { + int objID; + + public int getObjID() { + return this.objID; + } +} diff --git a/src/main/java/org/bdware/sc/trace/JSUndifined.java b/src/main/java/org/bdware/sc/trace/JSUndifined.java index 3d6f1e2..04f995d 100644 --- a/src/main/java/org/bdware/sc/trace/JSUndifined.java +++ b/src/main/java/org/bdware/sc/trace/JSUndifined.java @@ -2,6 +2,6 @@ package org.bdware.sc.trace; import java.io.Serializable; -public class JSUndifined extends JS implements Serializable{ +public class JSUndifined extends JS implements Serializable { } diff --git a/src/main/java/org/bdware/sc/trace/MethodInvokePrinter.java b/src/main/java/org/bdware/sc/trace/MethodInvokePrinter.java index 1151a62..1aca5c4 100644 --- a/src/main/java/org/bdware/sc/trace/MethodInvokePrinter.java +++ b/src/main/java/org/bdware/sc/trace/MethodInvokePrinter.java @@ -9,69 +9,70 @@ import wrp.jdk.nashorn.internal.runtime.TraceMethod; public class MethodInvokePrinter implements TraceMethod { - PrintStream out; + PrintStream out; - public void printObject(final Object arg) { - if (arg instanceof ScriptObject) { - final ScriptObject object = (ScriptObject) arg; + public void printObject(final Object arg) { + if (arg instanceof ScriptObject) { + final ScriptObject object = (ScriptObject) arg; - boolean isFirst = true; - final Set keySet = object.keySet(); + boolean isFirst = true; + final Set keySet = object.keySet(); - if (keySet.isEmpty()) { - out.print(ScriptRuntime.safeToString(arg)); - } else { - out.print("{ "); + if (keySet.isEmpty()) { + out.print(ScriptRuntime.safeToString(arg)); + } else { + out.print("{ "); - for (final Object key : keySet) { - if (!isFirst) { - out.print(", "); - } + for (final Object key : keySet) { + if (!isFirst) { + out.print(", "); + } - out.print(key); - out.print(":"); + out.print(key); + out.print(":"); - final Object value = object.get(key); + final Object value = object.get(key); - if (value instanceof ScriptObject) { - out.print("..."); - } else { - printObject(value); - } + if (value instanceof ScriptObject) { + out.print("..."); + } else { + printObject(value); + } - isFirst = false; - } + isFirst = false; + } - out.print(" }"); - } - } else { - out.print(ScriptRuntime.safeToString(arg)); - } - } + out.print(" }"); + } + } else { + out.print(ScriptRuntime.safeToString(arg)); + } + } - public void tracePrint(final String tag, int pc, String methodName, final Object[] args, final Object result) { - // boolean isVoid = type().returnType() == void.class; - out.print(tag); - out.print(methodName + "_" + pc + "("); - if (args.length > 0) { - printObject(args[0]); - for (int i = 1; i < args.length; i++) { - final Object arg = args[i]; - out.print(", "); + public void tracePrint(final String tag, int pc, String methodName, final Object[] args, + final Object result) { + // boolean isVoid = type().returnType() == void.class; + out.print(tag); + out.print(methodName + "_" + pc + "("); + if (args.length > 0) { + printObject(args[0]); + for (int i = 1; i < args.length; i++) { + final Object arg = args[i]; + out.print(", "); - if (!(arg instanceof ScriptObject && ((ScriptObject) arg).isScope())) { - printObject(arg); - } else { - out.print("SCOPE"); - } - } - } - out.print(")"); - if (tag.equals("EXIT ")) { - out.print(" --> "); - printObject(result); - } - out.println(); - } + if (!(arg instanceof ScriptObject && ((ScriptObject) arg).isScope())) { + printObject(arg); + } else { + out.print("SCOPE"); + } + } + } + out.print(")"); + if (tag.equals("EXIT ")) { + out.print(" --> "); + printObject(result); + } + out.println(); + } } diff --git a/src/main/java/org/bdware/sc/trace/ProgramPointCounter.java b/src/main/java/org/bdware/sc/trace/ProgramPointCounter.java index ee1c031..fa84931 100644 --- a/src/main/java/org/bdware/sc/trace/ProgramPointCounter.java +++ b/src/main/java/org/bdware/sc/trace/ProgramPointCounter.java @@ -24,17 +24,11 @@ public class ProgramPointCounter extends ContractProcess.Logger implements Trace int functionIndex = 0; public long gasValue = 0; - public ProgramPointCounter( - ByteArrayOutputStream bo, - ContractProcess cp, - long gasLimit, - int functionIndex, - long gasValue, - long extraGas, - String action, + public ProgramPointCounter(ByteArrayOutputStream bo, ContractProcess cp, long gasLimit, + int functionIndex, long gasValue, long extraGas, String action, HashMap countMap) { super(new PrintStream(bo), cp); - // out = System.out; + // out = System.out; this.bo = bo; globalAction = action; this.gasLimit = gasLimit - extraGas; @@ -50,53 +44,34 @@ public class ProgramPointCounter extends ContractProcess.Logger implements Trace public String getOutputStr() { return bo.toString(); } + /* - private void printObject(final Object arg) { - if (simple) return; - if (arg instanceof ScriptObject) { - final ScriptObject object = (ScriptObject) arg; - - boolean isFirst = true; - final Set keySet = object.keySet(); - // System.out.println("[keySet:]"); - if (keySet.isEmpty()) { - out.print(ScriptRuntime.safeToString(arg)); - } else { - out.print("{ "); - - for (final Object key : keySet) { - if (!isFirst) { - out.print(", "); - } - - out.print(key); - out.print(":"); - - final Object value = object.get(key); - - if (value instanceof ScriptObject) { - out.print("..."); - } else { - printObject(value); - } - - isFirst = false; - } - - out.print(" }"); - } - } else { - out.print(ScriptRuntime.safeToString(arg)); - } - } - */ + * private void printObject(final Object arg) { if (simple) return; if (arg instanceof + * ScriptObject) { final ScriptObject object = (ScriptObject) arg; + * + * boolean isFirst = true; final Set keySet = object.keySet(); // + * System.out.println("[keySet:]"); if (keySet.isEmpty()) { + * out.print(ScriptRuntime.safeToString(arg)); } else { out.print("{ "); + * + * for (final Object key : keySet) { if (!isFirst) { out.print(", "); } + * + * out.print(key); out.print(":"); + * + * final Object value = object.get(key); + * + * if (value instanceof ScriptObject) { out.print("..."); } else { printObject(value); } + * + * isFirst = false; } + * + * out.print(" }"); } } else { out.print(ScriptRuntime.safeToString(arg)); } } + */ boolean initEnter = false; Stack pcStack = new Stack<>(); @Override - public void tracePrint( - final String tag, int pc, String methodName, final Object[] args, final Object result) { - // System.out.println("!@#$%^&*" + tag + pc + methodName + args + result); + public void tracePrint(final String tag, int pc, String methodName, final Object[] args, + final Object result) { + // System.out.println("!@#$%^&*" + tag + pc + methodName + args + result); if (!initEnter) { // System.out.println("[functionIndex]:" + countMap.get(String.valueOf(functionIndex))); compareValue(String.valueOf(functionIndex)); @@ -119,29 +94,29 @@ public class ProgramPointCounter extends ContractProcess.Logger implements Trace pcStack.push(pc); // System.out.println("入栈" + pcStack); } - // out.print("[ProgramPointCounter] " + tag); - // out.print(methodName + "_" + pc + "("); + // out.print("[ProgramPointCounter] " + tag); + // out.print(methodName + "_" + pc + "("); - // if (args.length > 0) { - // printObject(args[0]); - // for (int i = 1; i < args.length; i++) { - // final Object arg = args[i]; - // out.print(", "); - // if (!(arg instanceof ScriptObject && ((ScriptObject) arg).isScope())) { - // printObject(arg); - // } else { - // out.print("SCOPE"); - // } - // } - // } - // out.print(")"); - // if (tag.equals("EXIT ")) { - // out.print(" --> "); - // printObject(result); - // System.out.println("[result:]" + result); - // } - // out.println(); - // System.out.println("[Fee剩余]" + gasValue); + // if (args.length > 0) { + // printObject(args[0]); + // for (int i = 1; i < args.length; i++) { + // final Object arg = args[i]; + // out.print(", "); + // if (!(arg instanceof ScriptObject && ((ScriptObject) arg).isScope())) { + // printObject(arg); + // } else { + // out.print("SCOPE"); + // } + // } + // } + // out.print(")"); + // if (tag.equals("EXIT ")) { + // out.print(" --> "); + // printObject(result); + // System.out.println("[result:]" + result); + // } + // out.println(); + // System.out.println("[Fee剩余]" + gasValue); } public long cost = 0; @@ -192,15 +167,15 @@ public class ProgramPointCounter extends ContractProcess.Logger implements Trace } } } - // System.out.println("[Gas剩余]" + gasValue); - // System.out.println("[执行消耗Gas]" + cost); - // System.out.println("[额外Gas]" + extraGas); - // System.out.println( - // "[ProgramPointCounter] {\"traceMark\":" - // + traceMarkValue - // + "\"val\":" - // + val - // + "}"); + // System.out.println("[Gas剩余]" + gasValue); + // System.out.println("[执行消耗Gas]" + cost); + // System.out.println("[额外Gas]" + extraGas); + // System.out.println( + // "[ProgramPointCounter] {\"traceMark\":" + // + traceMarkValue + // + "\"val\":" + // + val + // + "}"); } catch (IllegalStateException e) { throw e; } diff --git a/src/main/java/org/bdware/sc/trace/Trace.java b/src/main/java/org/bdware/sc/trace/Trace.java index 70384a1..ad84988 100644 --- a/src/main/java/org/bdware/sc/trace/Trace.java +++ b/src/main/java/org/bdware/sc/trace/Trace.java @@ -2,10 +2,10 @@ package org.bdware.sc.trace; import java.io.Serializable; -public class Trace implements Serializable{ - - public String traceContent() { - return "Trace Content"; - } +public class Trace implements Serializable { + + public String traceContent() { + return "Trace Content"; + } } diff --git a/src/main/java/org/bdware/sc/trace/TraceDone.java b/src/main/java/org/bdware/sc/trace/TraceDone.java index 9c499b1..68b745f 100644 --- a/src/main/java/org/bdware/sc/trace/TraceDone.java +++ b/src/main/java/org/bdware/sc/trace/TraceDone.java @@ -3,18 +3,18 @@ package org.bdware.sc.trace; import java.io.Serializable; public class TraceDone extends Trace implements Serializable { - int id; + int id; - public TraceDone(int id) { - this.id = id; - } - - public int getID() { - return id; - } + public TraceDone(int id) { + this.id = id; + } - @Override - public String traceContent() { - return "[TraceDone] allocID:" + id + "\n"; - } -} \ No newline at end of file + public int getID() { + return id; + } + + @Override + public String traceContent() { + return "[TraceDone] allocID:" + id + "\n"; + } +} diff --git a/src/main/java/org/bdware/sc/trace/TraceInitArray.java b/src/main/java/org/bdware/sc/trace/TraceInitArray.java index 56e781b..360bc6d 100644 --- a/src/main/java/org/bdware/sc/trace/TraceInitArray.java +++ b/src/main/java/org/bdware/sc/trace/TraceInitArray.java @@ -7,45 +7,45 @@ import java.util.List; /* * 数组创建时有初始值,进行初始化 */ -public class TraceInitArray extends Trace implements Serializable{ - int arrayId; //被初始化的数组id - List keys = new ArrayList(); //数组下标可能是整数,对象名等 - List values = new ArrayList(); //数组内内容 - - public TraceInitArray(int id) { - this.arrayId = id; - keys = new ArrayList(); - values = new ArrayList(); - } - - public void put(Object key,Object value) { - keys.add(key); - values.add(value); - } - - public int getLength() { - return values.size(); - } - - public int getArrayId() { - return this.arrayId; - } - - public Object getKey(int i) { - return keys.get(i); - } - - public Object getValue(int i) { - return values.get(i); - } - - @Override - public String traceContent(){ - StringBuilder str = new StringBuilder(); - str.append("[TraceInitArray]\n"); - str.append("ArrayID=" + arrayId + "\n"); - for(int i = 0;i < values.size();i++) - str.append("key=" + keys.get(i) + ";value=" + values.get(i) + "\n"); - return str.toString(); - } +public class TraceInitArray extends Trace implements Serializable { + int arrayId; // 被初始化的数组id + List keys = new ArrayList(); // 数组下标可能是整数,对象名等 + List values = new ArrayList(); // 数组内内容 + + public TraceInitArray(int id) { + this.arrayId = id; + keys = new ArrayList(); + values = new ArrayList(); + } + + public void put(Object key, Object value) { + keys.add(key); + values.add(value); + } + + public int getLength() { + return values.size(); + } + + public int getArrayId() { + return this.arrayId; + } + + public Object getKey(int i) { + return keys.get(i); + } + + public Object getValue(int i) { + return values.get(i); + } + + @Override + public String traceContent() { + StringBuilder str = new StringBuilder(); + str.append("[TraceInitArray]\n"); + str.append("ArrayID=" + arrayId + "\n"); + for (int i = 0; i < values.size(); i++) + str.append("key=" + keys.get(i) + ";value=" + values.get(i) + "\n"); + return str.toString(); + } } diff --git a/src/main/java/org/bdware/sc/trace/TraceInitObject.java b/src/main/java/org/bdware/sc/trace/TraceInitObject.java index b0731b2..c9631c8 100644 --- a/src/main/java/org/bdware/sc/trace/TraceInitObject.java +++ b/src/main/java/org/bdware/sc/trace/TraceInitObject.java @@ -2,28 +2,28 @@ package org.bdware.sc.trace; import java.io.Serializable; -public class TraceInitObject extends Trace implements Serializable{ - int id; - int id2; - - public TraceInitObject(int id,int id2) { - this.id = id; - this.id2 = id2; - } - - public int getId() { - return id; - } - - public int getId2() { - return id2; - } - - @Override - public String traceContent() { - StringBuilder str = new StringBuilder(); - str.append("[TraceInitObject]\n"); - str.append("id=" + id + " ; id2=" + id2 + "\n"); - return str.toString(); - } +public class TraceInitObject extends Trace implements Serializable { + int id; + int id2; + + public TraceInitObject(int id, int id2) { + this.id = id; + this.id2 = id2; + } + + public int getId() { + return id; + } + + public int getId2() { + return id2; + } + + @Override + public String traceContent() { + StringBuilder str = new StringBuilder(); + str.append("[TraceInitObject]\n"); + str.append("id=" + id + " ; id2=" + id2 + "\n"); + return str.toString(); + } } diff --git a/src/main/java/org/bdware/sc/trace/TraceRecord.java b/src/main/java/org/bdware/sc/trace/TraceRecord.java index 0443332..f397814 100644 --- a/src/main/java/org/bdware/sc/trace/TraceRecord.java +++ b/src/main/java/org/bdware/sc/trace/TraceRecord.java @@ -4,35 +4,35 @@ import java.io.Serializable; import java.util.ArrayList; import java.util.List; -public class TraceRecord implements Serializable{ - private static final long serialVersionUID = 34643133713102276L; +public class TraceRecord implements Serializable { + private static final long serialVersionUID = 34643133713102276L; - public List traces; - - public TraceRecord() { - traces = new ArrayList(); - } - - - @Override - public String toString() { - StringBuilder str = new StringBuilder(); - for(int i = 0;i < traces.size();i++) { - //str.append("com/yancloud/sc/trace" + i + ":\n"); - str.append(traces.get(i).traceContent() + "\n"); - } - return str.toString(); - } - - - /* - * 加入一条新的trace - */ - public void record(Trace trace) { - traces.add(trace); - } - - public int length() { - return traces.size(); - } -} \ No newline at end of file + public List traces; + + public TraceRecord() { + traces = new ArrayList(); + } + + + @Override + public String toString() { + StringBuilder str = new StringBuilder(); + for (int i = 0; i < traces.size(); i++) { + // str.append("com/yancloud/sc/trace" + i + ":\n"); + str.append(traces.get(i).traceContent() + "\n"); + } + return str.toString(); + } + + + /* + * 加入一条新的trace + */ + public void record(Trace trace) { + traces.add(trace); + } + + public int length() { + return traces.size(); + } +} diff --git a/src/main/java/org/bdware/sc/trace/TraceRecordUtil.java b/src/main/java/org/bdware/sc/trace/TraceRecordUtil.java index 467e685..e35632f 100644 --- a/src/main/java/org/bdware/sc/trace/TraceRecordUtil.java +++ b/src/main/java/org/bdware/sc/trace/TraceRecordUtil.java @@ -2,7 +2,6 @@ package org.bdware.sc.trace; import java.io.*; import java.util.ArrayList; -import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; import java.util.zip.GZIPInputStream; @@ -24,226 +23,227 @@ import wrp.jdk.nashorn.internal.runtime.TraceSetupScriptObject; import wrp.jdk.nashorn.internal.runtime.arrays.ArrayData; public class TraceRecordUtil { - SyncMechUtil syncUtil; - DesktopEngine engine; + SyncMechUtil syncUtil; + DesktopEngine engine; - public ArrayList traceRecords; + public ArrayList traceRecords; - public TraceRecord currentTraceRecord; //当前事务中所有非TraceSet的trace - public Map currentArrayMap; - public Map currentMap; //当前事务中所有TraceSet的精简记录 + public TraceRecord currentTraceRecord; // 当前事务中所有非TraceSet的trace + public Map currentArrayMap; + public Map currentMap; // 当前事务中所有TraceSet的精简记录 - public TraceRecordUtil(DesktopEngine de,SyncMechUtil sync) { - this.engine = de; - this.syncUtil = sync; - } + public TraceRecordUtil(DesktopEngine de, SyncMechUtil sync) { + this.engine = de; + this.syncUtil = sync; + } - public ArrayList getTraceRecords() { - if (traceRecords == null) { - return null; - } - return this.traceRecords; - } + public ArrayList getTraceRecords() { + if (traceRecords == null) { + return null; + } + return this.traceRecords; + } - public TraceRecord getTraceRecord(int c) { - if (traceRecords == null) - return null; - else if (traceRecords.size() <= c) - return null; - else - return traceRecords.get(c); - } + public TraceRecord getTraceRecord(int c) { + if (traceRecords == null) + return null; + else if (traceRecords.size() <= c) + return null; + else + return traceRecords.get(c); + } - public String getTraceRecordsContent() { - StringBuilder str = new StringBuilder(); + public String getTraceRecordsContent() { + StringBuilder str = new StringBuilder(); - for (int i = 0; i < traceRecords.size(); i++) { - str.append("No." + i + "record : \n" + traceRecords.get(i).toString()); - } + for (int i = 0; i < traceRecords.size(); i++) { + str.append("No." + i + "record : \n" + traceRecords.get(i).toString()); + } - return str.toString(); - } + return str.toString(); + } - public static String getTraceRecordsByFile(String fileName) { - ArrayList traceRecords = null; - File file = new File(fileName); - ObjectInputStream reader; - try { - FileInputStream fileout = new FileInputStream(file); - GZIPInputStream gzin = new GZIPInputStream(fileout); - reader = new ObjectInputStream(gzin); - traceRecords = (ArrayList) reader.readObject(); - reader.close(); - } catch (IOException | ClassNotFoundException e) { - e.printStackTrace(); - } + public static String getTraceRecordsByFile(String fileName) { + ArrayList traceRecords = null; + File file = new File(fileName); + ObjectInputStream reader; + try { + FileInputStream fileout = new FileInputStream(file); + GZIPInputStream gzin = new GZIPInputStream(fileout); + reader = new ObjectInputStream(gzin); + traceRecords = (ArrayList) reader.readObject(); + reader.close(); + } catch (IOException | ClassNotFoundException e) { + e.printStackTrace(); + } - StringBuilder str = new StringBuilder(); - for (int i = 0; i < traceRecords.size(); i++) { - str.append("No." + i + "record : \n" + traceRecords.get(i).toString()); - } - return str.toString(); - } + StringBuilder str = new StringBuilder(); + for (int i = 0; i < traceRecords.size(); i++) { + str.append("No." + i + "record : \n" + traceRecords.get(i).toString()); + } + return str.toString(); + } - //添加简化过的TraceSet - public void addSmpSet() { - currentTraceRecord.traces.addAll(currentArrayMap.values()); - currentTraceRecord.traces.addAll(currentMap.values()); - } + // 添加简化过的TraceSet + public void addSmpSet() { + currentTraceRecord.traces.addAll(currentArrayMap.values()); + currentTraceRecord.traces.addAll(currentMap.values()); + } - public void saveTraceRecords(String fileName) { - if (traceRecords == null) { - System.out.println("[saveTraceRecords] traceRecords is null,can't save traceRecords to fill!"); - } + public void saveTraceRecords(String fileName) { + if (traceRecords == null) { + System.out.println( + "[saveTraceRecords] traceRecords is null,can't save traceRecords to fill!"); + } - File traceFile = new File(fileName + ".trace"); // trace文件名中不带时间 - File parent = traceFile.getParentFile(); - if (!parent.exists()) - parent.mkdirs(); + File traceFile = new File(fileName + ".trace"); // trace文件名中不带时间 + File parent = traceFile.getParentFile(); + if (!parent.exists()) + parent.mkdirs(); - ObjectOutputStream writer; - try { - FileOutputStream fileout = new FileOutputStream(traceFile); - GZIPOutputStream out = new GZIPOutputStream(fileout); - writer = new ObjectOutputStream(out); - writer.writeObject(traceRecords); - writer.flush(); - writer.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } + ObjectOutputStream writer; + try { + FileOutputStream fileout = new FileOutputStream(traceFile); + GZIPOutputStream out = new GZIPOutputStream(fileout); + writer = new ObjectOutputStream(out); + writer.writeObject(traceRecords); + writer.flush(); + writer.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } - public void startRecordTrace() { - this.traceRecords = new ArrayList(); -// recording = true; + public void startRecordTrace() { + this.traceRecords = new ArrayList(); + // recording = true; - //set behavior - Context.TRACESETBEHAVIOR = new TraceSetBehavior() { + // set behavior + Context.TRACESETBEHAVIOR = new TraceSetBehavior() { - @Override - public void trace(CallSiteDescriptor desc, Object... args) { - int id = ((ScriptObject) args[0]).getObjectID(); - Object key = null; - Object value = null; + @Override + public void trace(CallSiteDescriptor desc, Object... args) { + int id = ((ScriptObject) args[0]).getObjectID(); + Object key = null; + Object value = null; - if (args.length == 3) { - key = JSType.toPrimitive(args[1]); - value = args[2]; - } else if (args.length == 2) { - key = desc.getName().split(":")[2]; - value = args[1]; - } + if (args.length == 3) { + key = JSType.toPrimitive(args[1]); + value = args[2]; + } else if (args.length == 2) { + key = desc.getName().split(":")[2]; + value = args[1]; + } - if (value instanceof Integer || value instanceof String || value instanceof Double) - ; - else if (value instanceof ConsString) - value = value.toString(); - else - value = produceJS(value); + if (value instanceof Integer || value instanceof String || value instanceof Double) + ; + else if (value instanceof ConsString) + value = value.toString(); + else + value = produceJS(value); - currentMap.put(new TraceSetIdentifier(id,key),new TraceSet(id,key,value)); - } - }; + currentMap.put(new TraceSetIdentifier(id, key), new TraceSet(id, key, value)); + } + }; - //setup script object - Context.TRACESETUPSCRIPTOBJECT = new TraceSetupScriptObject() { + // setup script object + Context.TRACESETUPSCRIPTOBJECT = new TraceSetupScriptObject() { - @Override - public void trace(ScriptObject arg, PropertyMap map) { - /* - * TraceSetup tracesetup = new TraceSetup((JSScript)produceJS(arg)); - * traceRecord.record(tracesetup); - */ + @Override + public void trace(ScriptObject arg, PropertyMap map) { + /* + * TraceSetup tracesetup = new TraceSetup((JSScript)produceJS(arg)); + * traceRecord.record(tracesetup); + */ - TraceSetup tracesetup = new TraceSetup((JSScript) produceJS(arg)); + TraceSetup tracesetup = new TraceSetup((JSScript) produceJS(arg)); - /* - * System.out.println("[startRecord] setupScriptObject argID=" + - * arg.getObjectID() + " map=" + map.size()); Iterator keys = - * arg.propertyIterator(); while(keys.hasNext()) { String key = keys.next(); - * System.out.println(key + " ; " + - * map.findProperty(key).getClass().getCanonicalName()); - * tracesetup.add(map.findProperty(key)); } - */ + /* + * System.out.println("[startRecord] setupScriptObject argID=" + arg.getObjectID() + + * " map=" + map.size()); Iterator keys = arg.propertyIterator(); + * while(keys.hasNext()) { String key = keys.next(); System.out.println(key + " ; " + * + map.findProperty(key).getClass().getCanonicalName()); + * tracesetup.add(map.findProperty(key)); } + */ - currentTraceRecord.record(tracesetup); - } - }; + currentTraceRecord.record(tracesetup); + } + }; - //setup array - Context.TRACESETUPARRAY = new TraceSetupArray() { - @Override - public void trace(ArrayData arg, int id) { - TraceInitArray traceinitarray = new TraceInitArray(id); - for (int i = 0; i < arg.length(); i++) { - Object obj = arg.getObject(i); + // setup array + Context.TRACESETUPARRAY = new TraceSetupArray() { + @Override + public void trace(ArrayData arg, int id) { + TraceInitArray traceinitarray = new TraceInitArray(id); + for (int i = 0; i < arg.length(); i++) { + Object obj = arg.getObject(i); - if (obj instanceof Integer || obj instanceof String || obj instanceof Double) - ; - else if (obj instanceof ConsString) - obj = obj.toString(); - else - obj = produceJS(obj); + if (obj instanceof Integer || obj instanceof String || obj instanceof Double) + ; + else if (obj instanceof ConsString) + obj = obj.toString(); + else + obj = produceJS(obj); - traceinitarray.put(i, obj); - } - currentArrayMap.put(traceinitarray.getArrayId(), traceinitarray); - } - }; + traceinitarray.put(i, obj); + } + currentArrayMap.put(traceinitarray.getArrayId(), traceinitarray); + } + }; - /* - * Context.TRACESETGLOBALOBJECTPROTO = new TraceSetGlobalObjectProto() { - * - * @Override public void trace(int id, int id2,List keys,List - * values) { System.out.println("[startRecord] set global object proto id=" + id - * + " id2=" + id2 + " size=" + keys.size()); - * - * - * - * TraceInitObject traceinitobject = new TraceInitObject(id,id2); for(int i = - * 0;i < keys.size();i++) { System.out.println("i=" + i + " key=" + keys.get(i) - * + " value=" + values.get(i)); } currentTraceRecord.record(traceinitobject); } - * - * }; - */ - } + /* + * Context.TRACESETGLOBALOBJECTPROTO = new TraceSetGlobalObjectProto() { + * + * @Override public void trace(int id, int id2,List keys,List values) { + * System.out.println("[startRecord] set global object proto id=" + id + " id2=" + id2 + + * " size=" + keys.size()); + * + * + * + * TraceInitObject traceinitobject = new TraceInitObject(id,id2); for(int i = 0;i < + * keys.size();i++) { System.out.println("i=" + i + " key=" + keys.get(i) + " value=" + + * values.get(i)); } currentTraceRecord.record(traceinitobject); } + * + * }; + */ + } - public void stopRecordTrace() { - Context.TRACESETBEHAVIOR = null; - Context.TRACESETUPARRAY = null; - Context.TRACESETUPSCRIPTOBJECT = null; -// recording = false; - } + public void stopRecordTrace() { + Context.TRACESETBEHAVIOR = null; + Context.TRACESETUPARRAY = null; + Context.TRACESETUPSCRIPTOBJECT = null; + // recording = false; + } - public static JS produceJS(Object arg) { - if (arg == null) { - return new JSNull(); - } else if (arg instanceof NativeArray) { - JSArray arr = new JSArray(((ScriptObject) arg).getObjectID()); - return arr; - } else if (arg instanceof ScriptObject) { - JSObject obj = new JSObject(((ScriptObject) arg).getObjectID()); - return obj; - } else if (arg instanceof wrp.jdk.nashorn.internal.runtime.Undefined) { - return new JSUndifined(); - } else { - System.out.println("[produceJS] arg encounter new type!" + arg.toString() + " " + arg.getClass()); - return new JS(); - } - } + public static JS produceJS(Object arg) { + if (arg == null) { + return new JSNull(); + } else if (arg instanceof NativeArray) { + JSArray arr = new JSArray(((ScriptObject) arg).getObjectID()); + return arr; + } else if (arg instanceof ScriptObject) { + JSObject obj = new JSObject(((ScriptObject) arg).getObjectID()); + return obj; + } else if (arg instanceof wrp.jdk.nashorn.internal.runtime.Undefined) { + return new JSUndifined(); + } else { + System.out.println( + "[produceJS] arg encounter new type!" + arg.toString() + " " + arg.getClass()); + return new JS(); + } + } - //每次事务开始时初始化 - public void startNext() { - currentTraceRecord = new TraceRecord(); - currentArrayMap = new LinkedHashMap(); - currentMap = new LinkedHashMap(); - } + // 每次事务开始时初始化 + public void startNext() { + currentTraceRecord = new TraceRecord(); + currentArrayMap = new LinkedHashMap(); + currentMap = new LinkedHashMap(); + } - //每次事务结束时记录 - public void eachFinish() { - addSmpSet(); - currentTraceRecord.record(new TraceDone(ScriptObject.getAllocID())); - traceRecords.add(currentTraceRecord); - } -} \ No newline at end of file + // 每次事务结束时记录 + public void eachFinish() { + addSmpSet(); + currentTraceRecord.record(new TraceDone(ScriptObject.getAllocID())); + traceRecords.add(currentTraceRecord); + } +} diff --git a/src/main/java/org/bdware/sc/trace/TraceRecoverUtil.java b/src/main/java/org/bdware/sc/trace/TraceRecoverUtil.java index ed7117f..5e7beb4 100644 --- a/src/main/java/org/bdware/sc/trace/TraceRecoverUtil.java +++ b/src/main/java/org/bdware/sc/trace/TraceRecoverUtil.java @@ -50,9 +50,12 @@ public class TraceRecoverUtil { } public TraceRecord getTraceRecord(int c) { - if (traceRecords == null) return null; - else if (traceRecords.size() <= c) return null; - else return traceRecords.get(c); + if (traceRecords == null) + return null; + else if (traceRecords.size() <= c) + return null; + else + return traceRecords.get(c); } public String getTraceRecordsContent() { @@ -86,7 +89,8 @@ public class TraceRecoverUtil { for (String key : bindings.keySet()) { Object obj = bindings.get(key); - if (obj instanceof ScriptObjectMirror) initRecoverMap((ScriptObjectMirror) obj); + if (obj instanceof ScriptObjectMirror) + initRecoverMap((ScriptObjectMirror) obj); } Context.setGlobal(JavaScriptEntry.getEngineGlobal()); } @@ -118,7 +122,7 @@ public class TraceRecoverUtil { } else if (trace instanceof TraceDone) { // System.out.println("[DesktopEngine] recover TraceDone, before set:" + // ScriptObject.getAllocID() - // + " -->" + ((TraceDone) trace).getID()); + // + " -->" + ((TraceDone) trace).getID()); ScriptObject.setAllocID(((TraceDone) trace).getID()); } else if (trace instanceof TraceInitArray) { recoverInitArray((TraceInitArray) trace); @@ -137,31 +141,25 @@ public class TraceRecoverUtil { return "[recoverFromTraceRecord] recover all"; } + /* * 通过traceRecord进行恢复 从当前状态恢复到第c次执行之后的状态 */ public String recoverFromTraceRecord(int c) { int oldflag = recoverFlag; - if (recoverFlag < 0) recoverInit(); + if (recoverFlag < 0) + recoverInit(); if (recoverFlag > c) { - System.out.println( - "[recoverFromTraceRecord] recoverFlag now is " - + recoverFlag - + " ,can't recover to " - + c - + " !"); + System.out.println("[recoverFromTraceRecord] recoverFlag now is " + recoverFlag + + " ,can't recover to " + c + " !"); return "recover from trace failed!"; } if (c >= traceRecords.size()) { - System.out.println( - "[recoverFromTraceRecord] traceRecords' size now is " - + traceRecords.size() - + " ,can't recover to " - + c - + " !"); + System.out.println("[recoverFromTraceRecord] traceRecords' size now is " + + traceRecords.size() + " ,can't recover to " + c + " !"); return "recover from trace failed!"; } @@ -187,7 +185,7 @@ public class TraceRecoverUtil { } else if (trace instanceof TraceDone) { // System.out.println("[DesktopEngine] recover TraceDone, before set:" + // ScriptObject.getAllocID() - // + " -->" + ((TraceDone) trace).getID()); + // + " -->" + ((TraceDone) trace).getID()); ScriptObject.setAllocID(((TraceDone) trace).getID()); } else if (trace instanceof TraceInitArray) { recoverInitArray((TraceInitArray) trace); @@ -217,10 +215,8 @@ public class TraceRecoverUtil { if (trace.getObj() instanceof JSArray) { id = ((JSArray) trace.getObj()).getObjID(); - obj = - (ScriptObjectMirror) - ScriptObjectMirror.wrap( - Global.allocate(new int[0]), engine.getDesktopGlobal()); + obj = (ScriptObjectMirror) ScriptObjectMirror.wrap(Global.allocate(new int[0]), + engine.getDesktopGlobal()); obj.setObjectID(id); } else if (trace.getObj() instanceof JSObject) { @@ -233,9 +229,8 @@ public class TraceRecoverUtil { * System.out.println("[recover setup JSObject] : "); id = ((JSObject) * trace.getObj()).getObjID(); * - * PropertyMap map = PropertyMap.newMap(); for(int i = 0;i < - * trace.proLength();i++) { map.addProperty(trace.get(i)); } ScriptObject so = - * new JO(map); + * PropertyMap map = PropertyMap.newMap(); for(int i = 0;i < trace.proLength();i++) { + * map.addProperty(trace.get(i)); } ScriptObject so = new JO(map); * * * @@ -279,12 +274,14 @@ public class TraceRecoverUtil { // 目前JSArray和JSObject一样,可能可以简化 else if (value instanceof JSArray) value = getScriptObjectMirrorById(((JSArray) value).getObjID()); - else if (value instanceof String || value instanceof Integer || value instanceof Double) ; + else if (value instanceof String || value instanceof Integer || value instanceof Double) + ; else if (value instanceof JSNull) // 数组trace中可能存在null和Undifined类型,赋值时跳过 - return; + return; else if (value instanceof JSUndifined) { return; - } else System.out.println("[recoverSet] encounter new value type!" + value.toString()); + } else + System.out.println("[recoverSet] encounter new value type!" + value.toString()); // 修改ScriptObjectMirror中的setMemeber,使它支持所有类型 owner.setMember2(key, value); @@ -294,8 +291,10 @@ public class TraceRecoverUtil { * 将bindings中的变量放入recoverMap 对于函数的scope中的对象以及对象中的对象的情况可以通过递归将所有需要的都放入recoverMap中 */ private void initRecoverMap(ScriptObjectMirror obj) { - if (obj == null) return; - if (recoverMap.containsKey(obj.getObjectID())) return; + if (obj == null) + return; + if (recoverMap.containsKey(obj.getObjectID())) + return; recoverMap.put(obj.getObjectID(), obj); // 全局变量从bindings中获得 @@ -309,10 +308,8 @@ public class TraceRecoverUtil { if (svalue.isFunction()) { ScriptFunction sf = (ScriptFunction) svalue.getScriptObject(); ScriptObject s = sf.getScope(); - ScriptObjectMirror obj2 = - (ScriptObjectMirror) - ScriptObjectMirror.wrap( - s, JavaScriptEntry.getEngineGlobal()); + ScriptObjectMirror obj2 = (ScriptObjectMirror) ScriptObjectMirror.wrap(s, + JavaScriptEntry.getEngineGlobal()); initRecoverMap(obj2); } } @@ -326,7 +323,8 @@ public class TraceRecoverUtil { ScriptObjectMirror so = null; // 从recoverMap中获得 - if (recoverMap.containsKey(id)) return recoverMap.get(id); + if (recoverMap.containsKey(id)) + return recoverMap.get(id); System.out.println("[getScriptObjectMirrorById] can't find the ScriptObjectMirror by id!"); return so; diff --git a/src/main/java/org/bdware/sc/trace/TraceSet.java b/src/main/java/org/bdware/sc/trace/TraceSet.java index eb741f0..3ee1878 100644 --- a/src/main/java/org/bdware/sc/trace/TraceSet.java +++ b/src/main/java/org/bdware/sc/trace/TraceSet.java @@ -3,51 +3,50 @@ package org.bdware.sc.trace; import java.io.Serializable; /* - * 记录nashorn中setElem和setProp的trace - * 自定义对象和数组的set不同 + * 记录nashorn中setElem和setProp的trace 自定义对象和数组的set不同 */ -public class TraceSet extends Trace implements Serializable{ - int owner; //该对象的id - Object key; - Object value; - - public TraceSet(int id,Object k,Object v) { - owner = id; - key = k; - value = v; - } - - public void setOwner(int owner) { - this.owner = owner; - } - - public void setKey(Object k) { - this.key = k; - } - - public void setValue(Object v) { - this.value = v; - } - - public int getOwner() { - return this.owner; - } - - public Object getKey() { - return this.key; - } - - public Object getValue() { - return this.value; - } - - @Override - public String traceContent(){ - StringBuilder str = new StringBuilder(); - str.append("[TraceSet]\n"); - str.append("owner=" + owner + "\n"); - str.append("key=" + key.getClass() + " " + key.toString() + "\n"); - str.append("value=" + value.getClass() + " " + value.toString() + "\n"); - return str.toString(); - } -} \ No newline at end of file +public class TraceSet extends Trace implements Serializable { + int owner; // 该对象的id + Object key; + Object value; + + public TraceSet(int id, Object k, Object v) { + owner = id; + key = k; + value = v; + } + + public void setOwner(int owner) { + this.owner = owner; + } + + public void setKey(Object k) { + this.key = k; + } + + public void setValue(Object v) { + this.value = v; + } + + public int getOwner() { + return this.owner; + } + + public Object getKey() { + return this.key; + } + + public Object getValue() { + return this.value; + } + + @Override + public String traceContent() { + StringBuilder str = new StringBuilder(); + str.append("[TraceSet]\n"); + str.append("owner=" + owner + "\n"); + str.append("key=" + key.getClass() + " " + key.toString() + "\n"); + str.append("value=" + value.getClass() + " " + value.toString() + "\n"); + return str.toString(); + } +} diff --git a/src/main/java/org/bdware/sc/trace/TraceSetIdentifier.java b/src/main/java/org/bdware/sc/trace/TraceSetIdentifier.java index 08bae52..a317f13 100644 --- a/src/main/java/org/bdware/sc/trace/TraceSetIdentifier.java +++ b/src/main/java/org/bdware/sc/trace/TraceSetIdentifier.java @@ -1,61 +1,64 @@ package org.bdware.sc.trace; public class TraceSetIdentifier { - int owner; - Object key; - - public TraceSetIdentifier(int id,Object k) { - this.owner = id; - this.key = k; - } - - @Override - public boolean equals(Object obj) { - if(obj == null && this == null) - return true; - else if(obj == null && this != null) - return false; - else if(obj != null && this == null) - return false; - - if(this == obj) - return true; - - TraceSetIdentifier obj2 = (TraceSetIdentifier)obj; - - if(this.owner != obj2.owner) - return false; - - if(this.key instanceof String && obj2.key instanceof String && obj2.key.equals(this.key)) - return true; - else if(this.key instanceof Double && obj2.key instanceof Double && obj2.key.equals(this.key)) - return true; - else - System.out.println("[TraceSetIdentifier] error : encounter new key type : " + this.key.getClass().getName()); - - return false; - } - - @Override - public int hashCode() { - int result = 17,temp = 0; - result = 31 * result + owner; - - if(key instanceof String) { - String k = (String)key; - result = result * 31 + k.hashCode(); - result = result * 31 + temp; - }else if(key instanceof Double) { - double t = (Double)key; - temp = (int)t; - result = result * 31 + temp; - }else { - System.out.println("[TraceSetIdentifier] error : encounter new key type : " + this.key.getClass().getName()); - } - - if(result < 0) - result = -result; - - return result; - } + int owner; + Object key; + + public TraceSetIdentifier(int id, Object k) { + this.owner = id; + this.key = k; + } + + @Override + public boolean equals(Object obj) { + if (obj == null && this == null) + return true; + else if (obj == null && this != null) + return false; + else if (obj != null && this == null) + return false; + + if (this == obj) + return true; + + TraceSetIdentifier obj2 = (TraceSetIdentifier) obj; + + if (this.owner != obj2.owner) + return false; + + if (this.key instanceof String && obj2.key instanceof String && obj2.key.equals(this.key)) + return true; + else if (this.key instanceof Double && obj2.key instanceof Double + && obj2.key.equals(this.key)) + return true; + else + System.out.println("[TraceSetIdentifier] error : encounter new key type : " + + this.key.getClass().getName()); + + return false; + } + + @Override + public int hashCode() { + int result = 17, temp = 0; + result = 31 * result + owner; + + if (key instanceof String) { + String k = (String) key; + result = result * 31 + k.hashCode(); + result = result * 31 + temp; + } else if (key instanceof Double) { + double t = (Double) key; + temp = (int) t; + result = result * 31 + temp; + } else { + System.out.println("[TraceSetIdentifier] error : encounter new key type : " + + this.key.getClass().getName()); + } + + if (result < 0) + result = -result; + + return result; + } } diff --git a/src/main/java/org/bdware/sc/trace/TraceSetup.java b/src/main/java/org/bdware/sc/trace/TraceSetup.java index 7675f33..16047e7 100644 --- a/src/main/java/org/bdware/sc/trace/TraceSetup.java +++ b/src/main/java/org/bdware/sc/trace/TraceSetup.java @@ -9,46 +9,48 @@ import wrp.jdk.nashorn.internal.runtime.Property; /* * 记录nashorn中ScriptObject创建的trace */ -public class TraceSetup extends Trace implements Serializable{ - JSScript obj; //被创建的ScriptObject - List properties; - - public TraceSetup(JSScript o) { - this.obj = o; - properties = new ArrayList(); - } - - public JSScript getObj() { - return this.obj; - } - - public void add(Property p) { - properties.add(p); - } - - public Property get(int i) { - return properties.get(i); - } - - public int proLength() { - return properties.size(); - } - - @Override - public String traceContent(){ - StringBuilder str = new StringBuilder(); - str.append("[TraceSetup]\n"); - if(obj instanceof JSObject) - str.append(((JSObject)obj).getObjID() + "," + ((JSObject)obj).getClass()+ " " + /*map.toString() +*/ "\n"); - else if(obj instanceof JSArray) - str.append(((JSArray)obj).getObjID() + "," + ((JSArray)obj).getClass() + " " +/* map.toString() + */"\n"); - - if(properties.size() > 0) { - str.append("properties : " + "\n"); - for(int i = 0;i < properties.size();i++) - str.append(properties.get(i) + "\n"); - } - - return str.toString(); - } -} \ No newline at end of file +public class TraceSetup extends Trace implements Serializable { + JSScript obj; // 被创建的ScriptObject + List properties; + + public TraceSetup(JSScript o) { + this.obj = o; + properties = new ArrayList(); + } + + public JSScript getObj() { + return this.obj; + } + + public void add(Property p) { + properties.add(p); + } + + public Property get(int i) { + return properties.get(i); + } + + public int proLength() { + return properties.size(); + } + + @Override + public String traceContent() { + StringBuilder str = new StringBuilder(); + str.append("[TraceSetup]\n"); + if (obj instanceof JSObject) + str.append(((JSObject) obj).getObjID() + "," + ((JSObject) obj).getClass() + " " + + /* map.toString() + */ "\n"); + else if (obj instanceof JSArray) + str.append(((JSArray) obj).getObjID() + "," + ((JSArray) obj).getClass() + " " + + /* map.toString() + */"\n"); + + if (properties.size() > 0) { + str.append("properties : " + "\n"); + for (int i = 0; i < properties.size(); i++) + str.append(properties.get(i) + "\n"); + } + + return str.toString(); + } +} diff --git a/src/test/data-mask/maskingJobs/DataXTest.java b/src/test/data-mask/maskingJobs/DataXTest.java index 253fada..3908e56 100644 --- a/src/test/data-mask/maskingJobs/DataXTest.java +++ b/src/test/data-mask/maskingJobs/DataXTest.java @@ -13,12 +13,14 @@ public class DataXTest { File directory = new File(""); String path = directory.getCanonicalPath(); try { - BufferedReader bf = new BufferedReader(new FileReader("./src/test/data-mask/maskingJobs/config.json")); - while((s = bf.readLine()) != null) { + BufferedReader bf = new BufferedReader( + new FileReader("./src/test/data-mask/maskingJobs/config.json")); + while ((s = bf.readLine()) != null) { content.append(s.trim()); } s = content.toString(); - } catch (Exception e) {} + } catch (Exception e) { + } String ans = mj.getMaskedData(s).getAsString(); System.out.println(ans); } diff --git a/src/test/java/org/bdware/analysis/DataBaseProcess.java b/src/test/java/org/bdware/analysis/DataBaseProcess.java index 6745eec..a641b31 100644 --- a/src/test/java/org/bdware/analysis/DataBaseProcess.java +++ b/src/test/java/org/bdware/analysis/DataBaseProcess.java @@ -2,42 +2,43 @@ package org.bdware.analysis; public class DataBaseProcess { public static void main(String[] args) { -// long timeJava; -// try { -// // start a process before -// long startJava = System.currentTimeMillis(); -// // authentication: user, dbname, pwd -// MongoCredential credential = MongoCredential.createCredential("yancloud-dbuser", "yancloud", -// "yancloud-112".toCharArray()); -// // connect to mongodb server -// ServerAddress addr = new ServerAddress("39.106.118.0", 27017); -// List credentials = new ArrayList(); -// credentials.add(credential); -// // connect to mongodb database -// // MongoClient mongoClient = new MongoClient(addr, -// // Arrays.asList(credential)); -// MongoClient mongoClient = new MongoClient(addr, credentials); -// MongoDatabase mongoDatabase = mongoClient.getDatabase("yancloud"); -// // list all the documents of collection -// MongoCollection collection = mongoDatabase.getCollection("containers"); -// FindIterable findIterable = collection.find(); -// MongoCursor mongoCursor = findIterable.iterator(); -// StringBuilder stringBuilder = new StringBuilder(); -// while (mongoCursor.hasNext()) { -// stringBuilder.append(mongoCursor.next().toJson()); -// } -// System.out.println(stringBuilder); -// timeJava = System.currentTimeMillis() - startJava; -// System.out.println("DataBaseTest of Java: " + timeJava + "\n\t"); -// mongoClient.close(); -// mongoClient = null; -// -// /* -// * findIterable.forEach(new Block() { public void apply(Document _doc) -// * { System.out.println(_doc.toJson()); } }); -// */ -// } catch (Exception e) { -// e.printStackTrace(); -// } + // long timeJava; + // try { + // // start a process before + // long startJava = System.currentTimeMillis(); + // // authentication: user, dbname, pwd + // MongoCredential credential = MongoCredential.createCredential("yancloud-dbuser", + // "yancloud", + // "yancloud-112".toCharArray()); + // // connect to mongodb server + // ServerAddress addr = new ServerAddress("39.106.118.0", 27017); + // List credentials = new ArrayList(); + // credentials.add(credential); + // // connect to mongodb database + // // MongoClient mongoClient = new MongoClient(addr, + // // Arrays.asList(credential)); + // MongoClient mongoClient = new MongoClient(addr, credentials); + // MongoDatabase mongoDatabase = mongoClient.getDatabase("yancloud"); + // // list all the documents of collection + // MongoCollection collection = mongoDatabase.getCollection("containers"); + // FindIterable findIterable = collection.find(); + // MongoCursor mongoCursor = findIterable.iterator(); + // StringBuilder stringBuilder = new StringBuilder(); + // while (mongoCursor.hasNext()) { + // stringBuilder.append(mongoCursor.next().toJson()); + // } + // System.out.println(stringBuilder); + // timeJava = System.currentTimeMillis() - startJava; + // System.out.println("DataBaseTest of Java: " + timeJava + "\n\t"); + // mongoClient.close(); + // mongoClient = null; + // + // /* + // * findIterable.forEach(new Block() { public void apply(Document _doc) + // * { System.out.println(_doc.toJson()); } }); + // */ + // } catch (Exception e) { + // e.printStackTrace(); + // } } } diff --git a/src/test/java/org/bdware/analysis/dynamic/NaiveDynamicTaintAnalysisTest.java b/src/test/java/org/bdware/analysis/dynamic/NaiveDynamicTaintAnalysisTest.java index 85e423c..e02e6bf 100644 --- a/src/test/java/org/bdware/analysis/dynamic/NaiveDynamicTaintAnalysisTest.java +++ b/src/test/java/org/bdware/analysis/dynamic/NaiveDynamicTaintAnalysisTest.java @@ -26,7 +26,7 @@ public class NaiveDynamicTaintAnalysisTest extends NaiveDynamicTaintAnalysis { YJSCompiler compiler = new YJSCompiler(); contractNode = compiler.compile(new FileInputStream(path), null); DesktopEngine engine = new DesktopEngine(); - engine.loadContract(null,contractNode, false); + engine.loadContract(null, contractNode, false); Map clzs = engine.dumpClass(); Map methods = new HashMap<>(); for (byte[] clz : clzs.values()) { diff --git a/src/test/java/org/bdware/analysis/dynamic/test/FSAnalysisTest.java b/src/test/java/org/bdware/analysis/dynamic/test/FSAnalysisTest.java index 9892c82..4b57c89 100644 --- a/src/test/java/org/bdware/analysis/dynamic/test/FSAnalysisTest.java +++ b/src/test/java/org/bdware/analysis/dynamic/test/FSAnalysisTest.java @@ -8,8 +8,8 @@ public class FSAnalysisTest { public static void main(String[] args) throws Exception { Contract contract = new Contract(); FSAnalysis.isDebug = true; - String content = - FileUtil.getFileContent("/Users/hulingxuan/git/SmartContract/contractExamples/fsanlysis.yjs"); + String content = FileUtil.getFileContent( + "/Users/hulingxuan/git/SmartContract/contractExamples/fsanlysis.yjs"); contract.setScript(null == content ? "" : content); FSAnalysis.staticVerify(contract); } diff --git a/src/test/java/org/bdware/analysis/example/MultiSourceTaintAyalysisTest.java b/src/test/java/org/bdware/analysis/example/MultiSourceTaintAyalysisTest.java index 577ed8e..268ae44 100644 --- a/src/test/java/org/bdware/analysis/example/MultiSourceTaintAyalysisTest.java +++ b/src/test/java/org/bdware/analysis/example/MultiSourceTaintAyalysisTest.java @@ -41,14 +41,12 @@ public class MultiSourceTaintAyalysisTest extends MultiSourceTaintAnalysis { YJSCompiler compiler = new YJSCompiler(); // contractNode = compiler.compile(new FileInputStream(path), "contract_main.yjs"); - // contractNode = compiler.compile(new FileInputStream(path), "DORepo.yjs"); - contractNode = - compiler.compile( - new ZipFile( - "/Users/huaqiancai/BDWare/BDContract/front-agent/./BDWareProjectDir/publicCompiled/StaticAnalysisExample_2020-07-09-00.ypk")) - .mergeContractNode(); + // contractNode = compiler.compile(new FileInputStream(path), "DORepo.yjs"); + contractNode = compiler.compile(new ZipFile( + "/Users/huaqiancai/BDWare/BDContract/front-agent/./BDWareProjectDir/publicCompiled/StaticAnalysisExample_2020-07-09-00.ypk")) + .mergeContractNode(); DesktopEngine engine = new DesktopEngine(); // engine.loadJar(zf); - ContractResult result2 = engine.loadContract(null,contractNode, false); + ContractResult result2 = engine.loadContract(null, contractNode, false); System.out.println("----LoadResult------"); System.out.println(new Gson().toJson(result2)); Map clzs = engine.dumpClass(); @@ -74,9 +72,7 @@ public class MultiSourceTaintAyalysisTest extends MultiSourceTaintAnalysis { // DependencyAnalysis // if(fn.functionName.equals("depTest")) - // depAnalysis(cfg); - - + // depAnalysis(cfg); @@ -95,7 +91,8 @@ public class MultiSourceTaintAyalysisTest extends MultiSourceTaintAnalysis { List ids = map.get(i); frontCF.addBB(bb, decompiled, ids, cfg); Set suc = cfg.getSucBlocks(bb); - for (BasicBlock sucBB : suc) frontCF.addEdge(bb, sucBB); + for (BasicBlock sucBB : suc) + frontCF.addEdge(bb, sucBB); } TaintBB bb = cfg.getLastBlock(); cfg.printSelf(); @@ -103,7 +100,8 @@ public class MultiSourceTaintAyalysisTest extends MultiSourceTaintAnalysis { // if(fn.functionName.equals("setDBInfo")) // cfg.printSelf(); - if (bb != null) result.put(fn.functionName, bb.getResultWithTaintBit()); + if (bb != null) + result.put(fn.functionName, bb.getResultWithTaintBit()); System.out.println("[ContractManager] verifyDone:" + fn.functionName); } } diff --git a/src/test/java/org/bdware/analysis/example/NaiveTaintAnalysisTest.java b/src/test/java/org/bdware/analysis/example/NaiveTaintAnalysisTest.java index 3440378..f9cafa1 100644 --- a/src/test/java/org/bdware/analysis/example/NaiveTaintAnalysisTest.java +++ b/src/test/java/org/bdware/analysis/example/NaiveTaintAnalysisTest.java @@ -26,7 +26,7 @@ public class NaiveTaintAnalysisTest extends NaiveTaintAnalysis { YJSCompiler compiler = new YJSCompiler(); contractNode = compiler.compile(new FileInputStream(path), null); DesktopEngine engine = new DesktopEngine(); - engine.loadContract(null,contractNode, false); + engine.loadContract(null, contractNode, false); Map clzs = engine.dumpClass(); Map methods = new HashMap<>(); for (byte[] clz : clzs.values()) { @@ -54,25 +54,25 @@ public class NaiveTaintAnalysisTest extends NaiveTaintAnalysis { cfg.printSelf(); } } - // String clzPath = + // String clzPath = // "./output/traceTest/wrp/jdk/nashorn/internal/scripts/Script$Recompilation$7$23A$contract_main_yjs7.class"; - // // String clzPath = - // // + // // String clzPath = + // // // "/Users/damei/Documents/Project/DataContract/SmartContract/output/traceTest/wrp/jdk/nashorn/internal/scripts/Script$Recompilation$5$23A$contract_main_yjs5.class"; - // ClassReader cr = new ClassReader(new FileInputStream(clzPath)); - // ClassNode cn = new ClassNode(); - // TaintResult.nLocals = 15; - // TaintResult.nStack = 5; - // cr.accept(cn, ClassReader.EXPAND_FRAMES); - // for (MethodNode mn : cn.methods) { - // if (mn.name.equals("statAge")) { - // System.out.println("[NaiveTaintAnalysis] Matched Method:" + mn.name + mn.desc); - // TaintCFG cfg = new TaintCFG(mn); - // TaintResult.printer.setLabelOrder(cfg.getLabelOrder()); - // NaiveTaintAnalysis analysis = new NaiveTaintAnalysis(cfg); - // analysis.analysis(); - // cfg.printSelf(); - // } - // } + // ClassReader cr = new ClassReader(new FileInputStream(clzPath)); + // ClassNode cn = new ClassNode(); + // TaintResult.nLocals = 15; + // TaintResult.nStack = 5; + // cr.accept(cn, ClassReader.EXPAND_FRAMES); + // for (MethodNode mn : cn.methods) { + // if (mn.name.equals("statAge")) { + // System.out.println("[NaiveTaintAnalysis] Matched Method:" + mn.name + mn.desc); + // TaintCFG cfg = new TaintCFG(mn); + // TaintResult.printer.setLabelOrder(cfg.getLabelOrder()); + // NaiveTaintAnalysis analysis = new NaiveTaintAnalysis(cfg); + // analysis.analysis(); + // cfg.printSelf(); + // } + // } } } diff --git a/src/test/java/org/bdware/analysis/gas/PPCountTest.java b/src/test/java/org/bdware/analysis/gas/PPCountTest.java index b2305a0..fec0c1f 100644 --- a/src/test/java/org/bdware/analysis/gas/PPCountTest.java +++ b/src/test/java/org/bdware/analysis/gas/PPCountTest.java @@ -44,17 +44,16 @@ public class PPCountTest extends PPCount { // flag++; MethodNode mn = methods.get("log"); if (mn != null) { - CFGraph cfg = - new CFGraph(mn) { - @Override - public BasicBlock getBasicBlock(int id) { - return new BasicBlock(id); - } - }; + CFGraph cfg = new CFGraph(mn) { + @Override + public BasicBlock getBasicBlock(int id) { + return new BasicBlock(id); + } + }; cfg.getLabelOrder(); PPCount countFee = new PPCount(cfg, flag); - // countFee.analysis(); + // countFee.analysis(); // cfg.printSelf(); } } @@ -66,11 +65,11 @@ public class PPCountTest extends PPCount { Evaluates feEvaluates = new Evaluates(); System.out.println(feEvaluates.getCallFee()); - // System.out.println(callFunction); - // System.out.println(branchCount); - // System.out.println(BlockInsn); - // System.out.println(ppMap); - // Evaluates feEvaluates = new Evaluates(callFunction); - // System.out.println(feEvaluates.getCallFee()); + // System.out.println(callFunction); + // System.out.println(branchCount); + // System.out.println(BlockInsn); + // System.out.println(ppMap); + // Evaluates feEvaluates = new Evaluates(callFunction); + // System.out.println(feEvaluates.getCallFee()); } } diff --git a/src/test/java/org/bdware/analysis/gas/Test.java b/src/test/java/org/bdware/analysis/gas/Test.java index e0f34bc..3a511f9 100644 --- a/src/test/java/org/bdware/analysis/gas/Test.java +++ b/src/test/java/org/bdware/analysis/gas/Test.java @@ -33,24 +33,24 @@ public class Test { // for (FunctionNode fn : contractNode.getFunctions()) { // funNameList.add(fn.functionName); // } - // CollectAnalysis collection = null; - // for (FunctionNode fn : contractNode.getFunctions()) { - // MethodNode mn = methods.get(fn.functionName); - // if (mn != null) { - // TaintResult.nLocals = mn.maxLocals; - // TaintResult.nStack = mn.maxStack; - // TaintCFG cfg = new TaintCFG(mn); - // // cfg.printSelf(); - // TracedFile tf = new TracedFile(new FileInputStream(tracePath)); - // cfg.getLabelOrder(); - // collection = new CollectAnalysis(cfg, tf); - // collection.analysis(); - // } - // } - // System.out.println(CollectAnalysis.branchCount); - // System.out.println(varInsnCount); - // System.out.println(callFunction); - // Evaluates forecast = new Evaluates(callFunction, varInsnCount, branchCount, "log"); - // System.out.println(forecast.getSum()); + // CollectAnalysis collection = null; + // for (FunctionNode fn : contractNode.getFunctions()) { + // MethodNode mn = methods.get(fn.functionName); + // if (mn != null) { + // TaintResult.nLocals = mn.maxLocals; + // TaintResult.nStack = mn.maxStack; + // TaintCFG cfg = new TaintCFG(mn); + // // cfg.printSelf(); + // TracedFile tf = new TracedFile(new FileInputStream(tracePath)); + // cfg.getLabelOrder(); + // collection = new CollectAnalysis(cfg, tf); + // collection.analysis(); + // } + // } + // System.out.println(CollectAnalysis.branchCount); + // System.out.println(varInsnCount); + // System.out.println(callFunction); + // Evaluates forecast = new Evaluates(callFunction, varInsnCount, branchCount, "log"); + // System.out.println(forecast.getSum()); } } diff --git a/src/test/java/org/bdware/sc/ByteArrayTest.java b/src/test/java/org/bdware/sc/ByteArrayTest.java index 878417d..8c5e6b3 100644 --- a/src/test/java/org/bdware/sc/ByteArrayTest.java +++ b/src/test/java/org/bdware/sc/ByteArrayTest.java @@ -14,8 +14,8 @@ public class ByteArrayTest { String hexStr = ByteUtils.toHexString(data); System.out.println("hashCode:" + Arrays.hashCode(data)); System.out.println("hexStr:" + hexStr); - //hashCode:-606029994 - //hexStr:616263363738373638396464 + // hashCode:-606029994 + // hexStr:616263363738373638396464 } @Test @@ -25,8 +25,8 @@ public class ByteArrayTest { String hexStr = ByteUtils.toHexString(data); System.out.println("hashCode:" + Arrays.hashCode(data)); System.out.println("hexStr:" + hexStr); - //hashCode:1712735702 - //hexStr:6162633637383736383232396464 + // hashCode:1712735702 + // hexStr:6162633637383736383232396464 // } } diff --git a/src/test/java/org/bdware/sc/ExecuteFunctionWithoutLimitTest.java b/src/test/java/org/bdware/sc/ExecuteFunctionWithoutLimitTest.java index d996541..0ab9b20 100644 --- a/src/test/java/org/bdware/sc/ExecuteFunctionWithoutLimitTest.java +++ b/src/test/java/org/bdware/sc/ExecuteFunctionWithoutLimitTest.java @@ -2,7 +2,6 @@ package org.bdware.sc; import com.google.gson.JsonArray; import com.google.gson.JsonObject; -import jdk.nashorn.internal.objects.Global; import org.bdware.sc.bean.Contract; import org.bdware.sc.engine.JSONTool; import org.bdware.sc.util.JsonUtil; @@ -10,8 +9,9 @@ import org.junit.Test; public class ExecuteFunctionWithoutLimitTest { @Test - public void go(){ - String arg = "{\"funcName\":\"getWriteCandidates\",\"funcArgs\":[\"041016acfbc8f4068a24d38a99a1355449ba958f7e905d66617cec9eda311e3001f8d15a4440b6eb61d7b7bd94bb434b30a518623c1593540e7d32eb72fe8088e1\",[\"04a68e0f34cd28484f67cf4e108eb64b8d565d18ecd916e9049151f97d1553872bf8706b3be9e418b7e6136e627d8e81c82a7ce3fd7045ed5382ae3196aa3ac418\",\"041016acfbc8f4068a24d38a99a1355449ba958f7e905d66617cec9eda311e3001f8d15a4440b6eb61d7b7bd94bb434b30a518623c1593540e7d32eb72fe8088e1\",\"04cd228cc3eeec97d912a5158eb25edfcb37947f9934a7645a3311eeeb06f1af05f155f2019ff1edc716d3d10a79a67714284a06ef16d61ccea80aa6816110d157\"],3,{\"doId\":\"abc\",\"body\":\"abcdefg\"}]}"; + public void go() { + String arg = + "{\"funcName\":\"getWriteCandidates\",\"funcArgs\":[\"041016acfbc8f4068a24d38a99a1355449ba958f7e905d66617cec9eda311e3001f8d15a4440b6eb61d7b7bd94bb434b30a518623c1593540e7d32eb72fe8088e1\",[\"04a68e0f34cd28484f67cf4e108eb64b8d565d18ecd916e9049151f97d1553872bf8706b3be9e418b7e6136e627d8e81c82a7ce3fd7045ed5382ae3196aa3ac418\",\"041016acfbc8f4068a24d38a99a1355449ba958f7e905d66617cec9eda311e3001f8d15a4440b6eb61d7b7bd94bb434b30a518623c1593540e7d32eb72fe8088e1\",\"04cd228cc3eeec97d912a5158eb25edfcb37947f9934a7645a3311eeeb06f1af05f155f2019ff1edc716d3d10a79a67714284a06ef16d61ccea80aa6816110d157\"],3,{\"doId\":\"abc\",\"body\":\"abcdefg\"}]}"; JsonObject body = JsonUtil.parseString(arg).getAsJsonObject(); String funcName = body.get("funcName").getAsString(); @@ -19,13 +19,15 @@ public class ExecuteFunctionWithoutLimitTest { Object[] funcArgs = JsonUtil.fromJson(arr, Object[].class); System.out.println(funcArgs); } + @Test - public void go2(){ - ContractProcess instance = new ContractProcess(124,"bac"); + public void go2() { + ContractProcess instance = new ContractProcess(124, "bac"); Contract c = new Contract(); c.setScript("contract abc{}"); instance.setContract(c); - String arg = "{\"funcName\":\"getWriteCandidates\",\"funcArgs\":[\"041016acfbc8f4068a24d38a99a1355449ba958f7e905d66617cec9eda311e3001f8d15a4440b6eb61d7b7bd94bb434b30a518623c1593540e7d32eb72fe8088e1\",[\"04a68e0f34cd28484f67cf4e108eb64b8d565d18ecd916e9049151f97d1553872bf8706b3be9e418b7e6136e627d8e81c82a7ce3fd7045ed5382ae3196aa3ac418\",\"041016acfbc8f4068a24d38a99a1355449ba958f7e905d66617cec9eda311e3001f8d15a4440b6eb61d7b7bd94bb434b30a518623c1593540e7d32eb72fe8088e1\",\"04cd228cc3eeec97d912a5158eb25edfcb37947f9934a7645a3311eeeb06f1af05f155f2019ff1edc716d3d10a79a67714284a06ef16d61ccea80aa6816110d157\"],3,{\"doId\":\"abc\",\"body\":\"abcdefg\"}]}"; + String arg = + "{\"funcName\":\"getWriteCandidates\",\"funcArgs\":[\"041016acfbc8f4068a24d38a99a1355449ba958f7e905d66617cec9eda311e3001f8d15a4440b6eb61d7b7bd94bb434b30a518623c1593540e7d32eb72fe8088e1\",[\"04a68e0f34cd28484f67cf4e108eb64b8d565d18ecd916e9049151f97d1553872bf8706b3be9e418b7e6136e627d8e81c82a7ce3fd7045ed5382ae3196aa3ac418\",\"041016acfbc8f4068a24d38a99a1355449ba958f7e905d66617cec9eda311e3001f8d15a4440b6eb61d7b7bd94bb434b30a518623c1593540e7d32eb72fe8088e1\",\"04cd228cc3eeec97d912a5158eb25edfcb37947f9934a7645a3311eeeb06f1af05f155f2019ff1edc716d3d10a79a67714284a06ef16d61ccea80aa6816110d157\"],3,{\"doId\":\"abc\",\"body\":\"abcdefg\"}]}"; JsonObject body = JsonUtil.parseString(arg).getAsJsonObject(); String funcName = body.get("funcName").getAsString(); JsonArray arr = body.getAsJsonArray("funcArgs"); diff --git a/src/test/java/org/bdware/sc/SM2Test.java b/src/test/java/org/bdware/sc/SM2Test.java index f9eeee1..538fb24 100644 --- a/src/test/java/org/bdware/sc/SM2Test.java +++ b/src/test/java/org/bdware/sc/SM2Test.java @@ -7,15 +7,17 @@ import java.math.BigInteger; public class SM2Test { @Test - public void run(){ - String str = "{publicKey:\"04398dfde44290595cd098cd2f904b36367c69f9011719d43fb0955f823cf1386764769bc7c0a5649dcb316d552998a5c106afd268d9db8b6482ce527544a7bd15\",privateKey:\"82a119ee46db52778182682f11e21980b6de3070b070a2f58e614af66775d6fc\"}"; - String pubKey = "04398dfde44290595cd098cd2f904b36367c69f9011719d43fb0955f823cf1386764769bc7c0a5649dcb316d552998a5c106afd268d9db8b6482ce527544a7bd15"; - // 04398dfde44290595cd098cd2f904b36367c69f9011719d43fb0955f823cf1386764769bc7cOa5649dcb316d552998a5c106afd268d9db8b6482ce527544a7bd15 + public void run() { + String str = + "{publicKey:\"04398dfde44290595cd098cd2f904b36367c69f9011719d43fb0955f823cf1386764769bc7c0a5649dcb316d552998a5c106afd268d9db8b6482ce527544a7bd15\",privateKey:\"82a119ee46db52778182682f11e21980b6de3070b070a2f58e614af66775d6fc\"}"; + String pubKey = + "04398dfde44290595cd098cd2f904b36367c69f9011719d43fb0955f823cf1386764769bc7c0a5649dcb316d552998a5c106afd268d9db8b6482ce527544a7bd15"; + // 04398dfde44290595cd098cd2f904b36367c69f9011719d43fb0955f823cf1386764769bc7cOa5649dcb316d552998a5c106afd268d9db8b6482ce527544a7bd15 String privKey = "82a119ee46db52778182682f11e21980b6de3070b070a2f58e614af66775d6fc"; - // 82a119ee46db52778182682f11e21980b6de3070b070a2f58e614af66775d6fc - SM2KeyPair key = new SM2KeyPair( - SM2KeyPair.publicKeyStr2ECPoint(pubKey), new BigInteger(privKey, 16)); + // 82a119ee46db52778182682f11e21980b6de3070b070a2f58e614af66775d6fc + SM2KeyPair key = new SM2KeyPair(SM2KeyPair.publicKeyStr2ECPoint(pubKey), + new BigInteger(privKey, 16)); System.out.println("hello"); } } diff --git a/src/test/java/org/bdware/sc/blockdb/test/ElasticDBUtilTest.java b/src/test/java/org/bdware/sc/blockdb/test/ElasticDBUtilTest.java index c1814be..1b4d373 100644 --- a/src/test/java/org/bdware/sc/blockdb/test/ElasticDBUtilTest.java +++ b/src/test/java/org/bdware/sc/blockdb/test/ElasticDBUtilTest.java @@ -6,19 +6,19 @@ public class ElasticDBUtilTest { public static void main(String[] args) { Client ci = new Client("127.0.0.1", 18095); System.out.println(ci.clientVersionSync()); -// long start = System.currentTimeMillis(); -// ElasticDBUtil util = new ElasticDBUtil(); -// //System.out.println(util.Create_DB()); -// for (int i=0;i<10000000;i++) { -// Transaction trans = new Transaction(); -// trans.data = ("eee1688b9d431ee448d97ed1dbdb62339b1b68d3"+i).getBytes(); -// trans.hash = ("aee1688b9d431ee448d97ed1dbdb62339"+i).getBytes(); -// util.Put(trans); -// } -// //Map condition = new HashMap<>(); -// //condition.put("data", "hello10"); -// //util.Get(condition); -// long end = System.currentTimeMillis(); -// System.out.println("consume time =>"+(end-start)); + // long start = System.currentTimeMillis(); + // ElasticDBUtil util = new ElasticDBUtil(); + // //System.out.println(util.Create_DB()); + // for (int i=0;i<10000000;i++) { + // Transaction trans = new Transaction(); + // trans.data = ("eee1688b9d431ee448d97ed1dbdb62339b1b68d3"+i).getBytes(); + // trans.hash = ("aee1688b9d431ee448d97ed1dbdb62339"+i).getBytes(); + // util.Put(trans); + // } + // //Map condition = new HashMap<>(); + // //condition.put("data", "hello10"); + // //util.Get(condition); + // long end = System.currentTimeMillis(); + // System.out.println("consume time =>"+(end-start)); } } diff --git a/src/test/java/org/bdware/sc/blockdb/test/SQLiteTest.java b/src/test/java/org/bdware/sc/blockdb/test/SQLiteTest.java index 130203a..4c6a9d5 100644 --- a/src/test/java/org/bdware/sc/blockdb/test/SQLiteTest.java +++ b/src/test/java/org/bdware/sc/blockdb/test/SQLiteTest.java @@ -1,23 +1,25 @@ package org.bdware.sc.blockdb.test; public class SQLiteTest { - public static void main(String[] args) { -// SqliteDBUtil db = SqliteDBUtil.connect("testdb"); -// Block block = db.getBlock("bb68c4a00b251ea350bbb28052eb44b565749586", "480af5c0c74eb471b671edb49bc9db93dc3456e4"); -// DataInputStream in = new DataInputStream(new ByteArrayInputStream(block.blockheader.prevblockID)); -// byte[] b1 = new byte[20]; -// byte[] b2 = new byte[20]; -// byte[] b3 = new byte[20]; -// try { -// in.read(b1); -// in.read(b2); -// in.read(b3); -// } catch (IOException e) { -// // TODO Auto-generated catch block -// e.printStackTrace(); -// } -// System.out.print(HashUtil.byteArray2Str(b1)); -// System.out.println(HashUtil.byteArray2Str(b2)); -// System.out.println(HashUtil.byteArray2Str(b3)); - } + public static void main(String[] args) { + // SqliteDBUtil db = SqliteDBUtil.connect("testdb"); + // Block block = db.getBlock("bb68c4a00b251ea350bbb28052eb44b565749586", + // "480af5c0c74eb471b671edb49bc9db93dc3456e4"); + // DataInputStream in = new DataInputStream(new + // ByteArrayInputStream(block.blockheader.prevblockID)); + // byte[] b1 = new byte[20]; + // byte[] b2 = new byte[20]; + // byte[] b3 = new byte[20]; + // try { + // in.read(b1); + // in.read(b2); + // in.read(b3); + // } catch (IOException e) { + // // TODO Auto-generated catch block + // e.printStackTrace(); + // } + // System.out.print(HashUtil.byteArray2Str(b1)); + // System.out.println(HashUtil.byteArray2Str(b2)); + // System.out.println(HashUtil.byteArray2Str(b3)); + } } diff --git a/src/test/java/org/bdware/sc/boundry/test/ACIndexTest.java b/src/test/java/org/bdware/sc/boundry/test/ACIndexTest.java index 4bd6b57..7e2d87a 100644 --- a/src/test/java/org/bdware/sc/boundry/test/ACIndexTest.java +++ b/src/test/java/org/bdware/sc/boundry/test/ACIndexTest.java @@ -19,7 +19,8 @@ public class ACIndexTest { DesktopEngine engine = new DesktopEngine(); engine.getDesktopGlobal(); Context.setGlobal(JavaScriptEntry.getEngineGlobal()); - ScriptObjectMirror mir = (ScriptObjectMirror) ScriptObjectMirror.wrap(Global.allocate(new int[0]), engine.getDesktopGlobal()); + ScriptObjectMirror mir = (ScriptObjectMirror) ScriptObjectMirror + .wrap(Global.allocate(new int[0]), engine.getDesktopGlobal()); mir.setMember("account", "ac1"); mir.setMember("file", "time"); mir.setMember("dataLength", "32"); @@ -29,7 +30,8 @@ public class ACIndexTest { for (int i = 0; i < 10000000; i++) { mir.setMember("account", "ac1"); mir.setMember("file", "time"); - mir.setMember("content", "61c1dc1d49ccd251b2f73567c776b4607dd36b12fa64e49e7ca53630102d0389"); + mir.setMember("content", + "61c1dc1d49ccd251b2f73567c776b4607dd36b12fa64e49e7ca53630102d0389"); mir.setMember("date", 1578042000 + i); obj = index.manullyIndex(mir); } diff --git a/src/test/java/org/bdware/sc/boundry/test/HttpPostTest.java b/src/test/java/org/bdware/sc/boundry/test/HttpPostTest.java index ba0f93e..e8b8dbe 100644 --- a/src/test/java/org/bdware/sc/boundry/test/HttpPostTest.java +++ b/src/test/java/org/bdware/sc/boundry/test/HttpPostTest.java @@ -35,14 +35,15 @@ public class HttpPostTest { } public static String get24Html(String url) { - ProcessBuilder builder = new ProcessBuilder("curl", url, "-H", "Connection: keep-alive", "-H", - "Cache-Control: max-age=0", "-H", "Origin: http://162.105.138.123", "-H", - "Upgrade-Insecure-Requests: 1", "-H", "Content-Type: application/x-www-form-urlencoded", "-H", + ProcessBuilder builder = new ProcessBuilder("curl", url, "-H", "Connection: keep-alive", + "-H", "Cache-Control: max-age=0", "-H", "Origin: http://162.105.138.123", "-H", + "Upgrade-Insecure-Requests: 1", "-H", + "Content-Type: application/x-www-form-urlencoded", "-H", "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36", "-H", "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3", - "-H", "Referer: http://162.105.138.123/net25/query.asp", "-H", "Accept-Encoding: gzip, deflate", "-H", - "Accept-Language: zh-CN,zh;q=0.9", "-H", + "-H", "Referer: http://162.105.138.123/net25/query.asp", "-H", + "Accept-Encoding: gzip, deflate", "-H", "Accept-Language: zh-CN,zh;q=0.9", "-H", "Cookie: setstring=%CB%CE%CC%E5%2FBLACK%2F4%2F%C1%A5%CA%E9%2FBLUE%2F2%2F%CB%CE%CC%E5%2Fgreen%2F3%2F%C1%A5%CA%E9%2FBLUE%2F2; clientadd=10%2E1%2E12%2E68; serveradd=162%2E105%2E138%2E123; ASPSESSIONIDAQQTRBDT=HHKDBDKBHGLMACLALGIFGKPO", "--compressed"); try { @@ -54,14 +55,16 @@ public class HttpPostTest { } public static String getCatalogHtml(String key) { - ProcessBuilder builder = new ProcessBuilder("curl", "http://162.105.138.123/net25/readns-25.htm", "-H", - "Connection: keep-alive", "-H", "Cache-Control: max-age=0", "-H", "Origin: http://162.105.138.123", - "-H", "Upgrade-Insecure-Requests: 1", "-H", "Content-Type: application/x-www-form-urlencoded", "-H", + ProcessBuilder builder = new ProcessBuilder("curl", + "http://162.105.138.123/net25/readns-25.htm", "-H", "Connection: keep-alive", "-H", + "Cache-Control: max-age=0", "-H", "Origin: http://162.105.138.123", "-H", + "Upgrade-Insecure-Requests: 1", "-H", + "Content-Type: application/x-www-form-urlencoded", "-H", "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36", "-H", "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3", - "-H", "Referer: http://162.105.138.123/net25/query.asp", "-H", "Accept-Encoding: gzip, deflate", "-H", - "Accept-Language: zh-CN,zh;q=0.9", "-H", + "-H", "Referer: http://162.105.138.123/net25/query.asp", "-H", + "Accept-Encoding: gzip, deflate", "-H", "Accept-Language: zh-CN,zh;q=0.9", "-H", "Cookie: setstring=%CB%CE%CC%E5%2FBLACK%2F4%2F%C1%A5%CA%E9%2FBLUE%2F2%2F%CB%CE%CC%E5%2Fgreen%2F3%2F%C1%A5%CA%E9%2FBLUE%2F2; clientadd=10%2E1%2E12%2E68; serveradd=162%2E105%2E138%2E123; ASPSESSIONIDAQQTRBDT=HHKDBDKBHGLMACLALGIFGKPO", "--compressed"); try { @@ -73,14 +76,16 @@ public class HttpPostTest { } public static String query24(String keyword) { - ProcessBuilder builder = new ProcessBuilder("curl", "http://162.105.138.123/net25/readwhole.asp", "-H", - "Connection: keep-alive", "-H", "Cache-Control: max-age=0", "-H", "Origin: http://162.105.138.123", - "-H", "Upgrade-Insecure-Requests: 1", "-H", "Content-Type: application/x-www-form-urlencoded", "-H", + ProcessBuilder builder = new ProcessBuilder("curl", + "http://162.105.138.123/net25/readwhole.asp", "-H", "Connection: keep-alive", "-H", + "Cache-Control: max-age=0", "-H", "Origin: http://162.105.138.123", "-H", + "Upgrade-Insecure-Requests: 1", "-H", + "Content-Type: application/x-www-form-urlencoded", "-H", "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36", "-H", "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3", - "-H", "Referer: http://162.105.138.123/net25/query.asp", "-H", "Accept-Encoding: gzip, deflate", "-H", - "Accept-Language: zh-CN,zh;q=0.9", "-H", + "-H", "Referer: http://162.105.138.123/net25/query.asp", "-H", + "Accept-Encoding: gzip, deflate", "-H", "Accept-Language: zh-CN,zh;q=0.9", "-H", "Cookie: setstring=%CB%CE%CC%E5%2FBLACK%2F4%2F%C1%A5%CA%E9%2FBLUE%2F2%2F%CB%CE%CC%E5%2Fgreen%2F3%2F%C1%A5%CA%E9%2FBLUE%2F2; clientadd=10%2E1%2E12%2E68; serveradd=162%2E105%2E138%2E123; ASPSESSIONIDAQQTRBDT=HHKDBDKBHGLMACLALGIFGKPO", "--data", "query=_keyword&B1=%BC%EC+%CB%F7&colid=2.0&colid=2.1&colid=2.14&colid=2.25&colid=2.34&colid=2.65&colid=2.136&colid=2.138" @@ -95,16 +100,16 @@ public class HttpPostTest { } public static void main(String[] args) throws Exception { -// String data = getCatalogHtml(null); -// FileOutputStream fout = new FileOutputStream("./output/shiji.txt"); -// Document doc = Jsoup.parse(data); -// Element body = doc.select("body").get(0); -// for (int i = 0; i < body.children().size(); i++) { -// Element ele = body.child(i); -// -// if (ele.tagName().equals("span")) -// System.out.println(ele.text()); -// } + // String data = getCatalogHtml(null); + // FileOutputStream fout = new FileOutputStream("./output/shiji.txt"); + // Document doc = Jsoup.parse(data); + // Element body = doc.select("body").get(0); + // for (int i = 0; i < body.children().size(); i++) { + // Element ele = body.child(i); + // + // if (ele.tagName().equals("span")) + // System.out.println(ele.text()); + // } System.out.println(querySection("aa")); } diff --git a/src/test/java/org/bdware/sc/boundry/test/YancloudUtilTest.java b/src/test/java/org/bdware/sc/boundry/test/YancloudUtilTest.java index 61c5c78..91eef26 100644 --- a/src/test/java/org/bdware/sc/boundry/test/YancloudUtilTest.java +++ b/src/test/java/org/bdware/sc/boundry/test/YancloudUtilTest.java @@ -6,7 +6,7 @@ public class YancloudUtilTest { public static void main(String[] arg) { Scanner sc = new Scanner(System.in); String bufLine = ""; - for (String line; (line = sc.nextLine()) != null; ) { + for (String line; (line = sc.nextLine()) != null;) { if (line.length() > 2) { bufLine = line; } @@ -16,10 +16,10 @@ public class YancloudUtilTest { final String data = line; new Thread(() -> { System.out.println(data); - // long start = System.currentTimeMillis(); + // long start = System.currentTimeMillis(); - // String ret = JavaScriptEntry.httpGet(data).substring(0, 100); - // System.out.println(System.currentTimeMillis() - start + " " + + // String ret = JavaScriptEntry.httpGet(data).substring(0, 100); + // System.out.println(System.currentTimeMillis() - start + " " + // ret); }).start(); } diff --git a/src/test/java/org/bdware/sc/boundry/utils/test/LedgerUtilTest.java b/src/test/java/org/bdware/sc/boundry/utils/test/LedgerUtilTest.java index 25e5bdd..91eebf3 100644 --- a/src/test/java/org/bdware/sc/boundry/utils/test/LedgerUtilTest.java +++ b/src/test/java/org/bdware/sc/boundry/utils/test/LedgerUtilTest.java @@ -20,8 +20,9 @@ public class LedgerUtilTest { System.out.println(c.clientVersionSync().getVersion()); System.out.println(c.getLedgersSync().toString()); String from = "0xb60e8dd61c5d32be8058bb8eb970870f07233155"; - LedgerProto.SendTransactionResponse ret = c.sendTransactionSync("bdcontract", CommonProto.TransactionType.MESSAGE, - from, System.currentTimeMillis(), from, "hello".getBytes(StandardCharsets.UTF_8)); + LedgerProto.SendTransactionResponse ret = + c.sendTransactionSync("bdcontract", CommonProto.TransactionType.MESSAGE, from, + System.currentTimeMillis(), from, "hello".getBytes(StandardCharsets.UTF_8)); System.out.println("=====" + HashUtil.byteArray2Str(ret.getHash().toByteArray())); } @@ -29,7 +30,7 @@ public class LedgerUtilTest { public void transToB64() { String str = "1dc589951b10840e097c793764f8ad63ff577ef7"; str = "3c011ab510c5756db9aab1f2ac1a14742b85200f"; - str ="867020d3463126c0f5ea41967100865770ca1873"; + str = "867020d3463126c0f5ea41967100865770ca1873"; str = "3f40a6afcd4e7c7db1ee8a3cf54074be270c6847"; str = "ab7d541b4f320f77fe424082d79d1f4ca2a40f84"; str = "246f5527c3182d162ea8f1c3f5e0be05d9269517"; @@ -37,20 +38,22 @@ public class LedgerUtilTest { str = "dbda443da8a6da3b4703b06250f07f7df3e04d72"; str = "d16da370021447c1c1136f97f9975069b1f22ddb"; str = "448b314de358384a55b9c5d2eae7596cff4e3587"; - str ="a114aa22365c2d61ee1c242c755d82c035783e41"; + str = "a114aa22365c2d61ee1c242c755d82c035783e41"; str = "d9be17b4287ed9a548901ef0e738f65f25dc7041"; - str="e90dbc995add64b26fa483b1b0ad7747b19ad579"; + str = "e90dbc995add64b26fa483b1b0ad7747b19ad579"; byte[] bytes = ByteUtils.fromHexString(str); String hash = ByteUtil.encodeBASE64(bytes); System.out.println(URLEncoder.encode(hash)); } + @Test - public void readData(){ + public void readData() { String hash = "kNkTGrOLKlMiVHiCx/Ik3Tx3DDI="; hash = "mEoVJx4k2L5nhKY6exjtJWmU7RA="; hash = "OTg0YTE1MjcxZTI0ZDhiZTY3ODRhNjNhN2IxOGVkMjU2OTk0ZWQxMA=="; byte[] data = ByteUtil.decodeBASE64(hash); - // - // System.out.println(new String(data)); + // + // System.out.println(new String(data)); } } diff --git a/src/test/java/org/bdware/sc/boundry/utils/test/SQLUtilTest.java b/src/test/java/org/bdware/sc/boundry/utils/test/SQLUtilTest.java index f2c51e7..7821f83 100644 --- a/src/test/java/org/bdware/sc/boundry/utils/test/SQLUtilTest.java +++ b/src/test/java/org/bdware/sc/boundry/utils/test/SQLUtilTest.java @@ -18,7 +18,8 @@ public class SQLUtilTest { // 1.getConnection()方法,连接MySQL数据库!! con = DriverManager.getConnection(url, user, password); - if (!con.isClosed()) System.out.println("Succeeded connecting to the Database!"); + if (!con.isClosed()) + System.out.println("Succeeded connecting to the Database!"); // 2.创建statement类对象,用来执行SQL语句!! Statement statement = con.createStatement(); @@ -31,7 +32,7 @@ public class SQLUtilTest { String id = null; for (int i = 1; i <= metaData.getColumnCount(); i++) { - // int columnType = metaData.getColumnType(i); + // int columnType = metaData.getColumnType(i); System.out.println( metaData.getColumnTypeName(i) + " --> " + metaData.getColumnName(i)); } diff --git a/src/test/java/org/bdware/sc/engine/hook/test/ArgSchemaTest.java b/src/test/java/org/bdware/sc/engine/hook/test/ArgSchemaTest.java index 9118950..ae27aad 100644 --- a/src/test/java/org/bdware/sc/engine/hook/test/ArgSchemaTest.java +++ b/src/test/java/org/bdware/sc/engine/hook/test/ArgSchemaTest.java @@ -8,47 +8,62 @@ import org.junit.Test; public class ArgSchemaTest { @Test - public void test1(){ - JsonObject realData=JsonParser.parseString("{\"data\":[{\"name\":\"zzz\"},{\"name\":\"aaa\"}],\"doId\":1}").getAsJsonObject(); - JsonObject schema=JsonParser.parseString("{\"data\":[{\"name\":\"string\"}],\"doId\":\"number\"}").getAsJsonObject(); + public void test1() { + JsonObject realData = JsonParser + .parseString("{\"data\":[{\"name\":\"zzz\"},{\"name\":\"aaa\"}],\"doId\":1}") + .getAsJsonObject(); + JsonObject schema = + JsonParser.parseString("{\"data\":[{\"name\":\"string\"}],\"doId\":\"number\"}") + .getAsJsonObject(); - ArgSchemaVisitor visitor=new ArgSchemaVisitor(realData); + ArgSchemaVisitor visitor = new ArgSchemaVisitor(realData); visitor.visit(schema); System.out.println(visitor.getStatus()); System.out.println(visitor.getException().toString()); } + @Test - public void test2(){ - JsonObject realData=JsonParser.parseString("{\"data\":[{\"name\":\"zzz\"},{\"name\":\"aaa\"}],\"doId\":\"string\"}").getAsJsonObject(); - JsonObject schema=JsonParser.parseString("{\"data\":[{\"name\":\"string\"}],\"doId\":\"number\"}").getAsJsonObject(); + public void test2() { + JsonObject realData = JsonParser + .parseString( + "{\"data\":[{\"name\":\"zzz\"},{\"name\":\"aaa\"}],\"doId\":\"string\"}") + .getAsJsonObject(); + JsonObject schema = + JsonParser.parseString("{\"data\":[{\"name\":\"string\"}],\"doId\":\"number\"}") + .getAsJsonObject(); - ArgSchemaVisitor visitor=new ArgSchemaVisitor(realData); - visitor.visit(schema); - System.out.println(visitor.getStatus()); - System.out.println(visitor.getException().toString()); - System.out.println(visitor.errorCode); - } - @Test - public void test3(){ - JsonElement realData=JsonParser.parseString("data.json"); - JsonElement schema=JsonParser.parseString("\"string\""); - - - ArgSchemaVisitor visitor=new ArgSchemaVisitor(realData); + ArgSchemaVisitor visitor = new ArgSchemaVisitor(realData); visitor.visit(schema); System.out.println(visitor.getStatus()); System.out.println(visitor.getException().toString()); System.out.println(visitor.errorCode); } - //缺少必选项 @Test - public void test4(){ - JsonObject realData=JsonParser.parseString("{\"data\":[{\"name\":\"zzz\"},{\"name\":\"aaa\"}]}").getAsJsonObject(); - JsonObject schema=JsonParser.parseString("{\"data\":[{\"name\":\"string\"}],\"!doId\":\"string\"}").getAsJsonObject(); - ArgSchemaVisitor visitor=new ArgSchemaVisitor(realData); + public void test3() { + JsonElement realData = JsonParser.parseString("data.json"); + JsonElement schema = JsonParser.parseString("\"string\""); + + + ArgSchemaVisitor visitor = new ArgSchemaVisitor(realData); + visitor.visit(schema); + System.out.println(visitor.getStatus()); + System.out.println(visitor.getException().toString()); + System.out.println(visitor.errorCode); + } + + // 缺少必选项 + @Test + public void test4() { + JsonObject realData = + JsonParser.parseString("{\"data\":[{\"name\":\"zzz\"},{\"name\":\"aaa\"}]}") + .getAsJsonObject(); + JsonObject schema = + JsonParser.parseString("{\"data\":[{\"name\":\"string\"}],\"!doId\":\"string\"}") + .getAsJsonObject(); + ArgSchemaVisitor visitor = new ArgSchemaVisitor(realData); visitor.visit(schema); System.out.println(visitor.getStatus()); System.out.println(visitor.getException()); diff --git a/src/test/java/org/bdware/sc/engine/hook/test/MaskVisitorTest.java b/src/test/java/org/bdware/sc/engine/hook/test/MaskVisitorTest.java index 751a4f5..ea238b2 100644 --- a/src/test/java/org/bdware/sc/engine/hook/test/MaskVisitorTest.java +++ b/src/test/java/org/bdware/sc/engine/hook/test/MaskVisitorTest.java @@ -6,8 +6,10 @@ import org.bdware.sc.engine.hook.MaskVisitor; public class MaskVisitorTest { public static void main(String[] args) { - String mask = "[{\"score\":\"md5\",\"name\":\"fpe\",\"grade\":\"edp\",\"number\":\"aes\",\"info\":{\"age\":\"edp\",\"sex\":\"md5\"}}]"; - String data = "[{\"score\":11,\"name\":\"ccq\",\"grade\":98.5,\"number\":2001210533,\"info\":{\"age\":\"18\",\"sex\":\"m\"}},{\"score\":11,\"name\":\"ccq\",\"grade\":98.5,\"number\":2001210533}]"; + String mask = + "[{\"score\":\"md5\",\"name\":\"fpe\",\"grade\":\"edp\",\"number\":\"aes\",\"info\":{\"age\":\"edp\",\"sex\":\"md5\"}}]"; + String data = + "[{\"score\":11,\"name\":\"ccq\",\"grade\":98.5,\"number\":2001210533,\"info\":{\"age\":\"18\",\"sex\":\"m\"}},{\"score\":11,\"name\":\"ccq\",\"grade\":98.5,\"number\":2001210533}]"; String m1 = "{\"score\":\"edp\",\"name\":\"md5\"}"; String d1 = "{\"score\":\"95\",\"name\":\"zzz\"}"; diff --git a/src/test/java/org/bdware/sc/engine/test/MockUtilTest.java b/src/test/java/org/bdware/sc/engine/test/MockUtilTest.java index 90b2448..c2bc94c 100644 --- a/src/test/java/org/bdware/sc/engine/test/MockUtilTest.java +++ b/src/test/java/org/bdware/sc/engine/test/MockUtilTest.java @@ -13,13 +13,13 @@ public class MockUtilTest { // "{'result':'@datetime'}" "{'result':'@date'}" "{'result':'@time'}" // "{'result':'@cname'}" "{'result':'@name'}" "{'result':'@first'}" "{'result':'@last'}" // "{'result':'@email'}" "{'result':'@ip'}" {'result':'@url'}" - // "{'result':'@province'}" "{'result':'@city'}" "{'result':'@county'}" + // "{'result':'@province'}" "{'result':'@city'}" "{'result':'@county'}" // {'id':'@integer','email':'@email','password':'@string','name':'@name'} // String template="{'result|1-100':1}"; /// - // Object res = mock("'@email'"); + // Object res = mock("'@email'"); // if (res instanceof ScriptObjectMirror) - // System.out.println(JSONTool.copy((ScriptObjectMirror) res)); + // System.out.println(JSONTool.copy((ScriptObjectMirror) res)); // else System.out.println(res); // System.out.println(res); // "{'list|1-5':[{'id|+1':1,'data':'@datetime','nickname': '@cname','email':'@email'}]} @@ -34,8 +34,9 @@ public class MockUtilTest { System.out.println(ele2); Object ret = MockUtil.mock(ele2.toString()); -// System.out.println( -// (JSONTool.copy((jdk.nashorn.api.scripting.ScriptObjectMirror) mock(ele2.toString()))).toString()); + // System.out.println( + // (JSONTool.copy((jdk.nashorn.api.scripting.ScriptObjectMirror) + // mock(ele2.toString()))).toString()); System.out.println(MockUtil.mock("'@string'")); } diff --git a/src/test/java/org/bdware/sc/memory/test/MemoryDumpRecoverTest.java b/src/test/java/org/bdware/sc/memory/test/MemoryDumpRecoverTest.java index 1e4af6e..ea74f1e 100644 --- a/src/test/java/org/bdware/sc/memory/test/MemoryDumpRecoverTest.java +++ b/src/test/java/org/bdware/sc/memory/test/MemoryDumpRecoverTest.java @@ -7,8 +7,8 @@ import java.util.Scanner; public class MemoryDumpRecoverTest { public static void main(String[] args) throws IOException { - //memoryRecover("AppData"); - //memoryRecover("Test"); + // memoryRecover("AppData"); + // memoryRecover("Test"); } public static String file2Str(String file) { @@ -24,45 +24,32 @@ public class MemoryDumpRecoverTest { return sb.toString(); } -/* //仅支持public路径下测试 - public static void memoryRecover(String contractName) { - String name = contractName + "/" + contractName; - - //启动合约 - ContractManager cm2 = new ContractManager(); - cm2.yjsPath = JudgeStatus.yjsPath; - Contract c = new Contract(); - c.setType(ContractType.Sole); - String id = "169412582"; - c.setID(id); - c.setScript(RedoRecoverTest.file2Str("/Users/zhangyixuan/Lab/newProject/BDContract/front-agent/BDWareProjectDir/public/" + name + ".yjs")); - String key = "{\"publicKey\":\"041d7b4818817736b3d64305675f04e9a00a12ce23c72e6740f83785edcffec6bae2c178fbc934f66d72e26f89a2e7a2c966dc3f0a8e415663c9b933af7a2e9ff8\",\"privateKey\":\"18505919022281880113072981827955639221458448578012075254857346196103069175443\"}"; - SM2KeyPair pair = SM2KeyPair.fromJson(key); - c.doSignature(pair); - System.out.println("<<>> : " + cm2.startContractAndRedirect(c, null)); - - - - - //loadMemory - long time1 = System.currentTimeMillis(); - File mem = new File("./cp/recoverTestFiles/memory/" + contractName + ".txt"); - String res = cm2.loadMemory(contractName, mem.getAbsolutePath()); - long time2 = System.currentTimeMillis(); - try { - Thread.sleep(3000); - } catch (InterruptedException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - System.out.println("<<>> : " + res); - double cost = ((double)time2 - (double)time1)/1000; - System.out.println("load memory 用时" + cost + "s"); - - - String content1 = MemoryDumpUtil.getContentFromFile(mem.getAbsolutePath()); - String content2 = cm2.dumpContract(contractName, ""); - System.out.println("load之后dump : \n" + content2 + "\n\n"); - System.out.println(content1.equals(content2)); - }*/ + /* + * //仅支持public路径下测试 public static void memoryRecover(String contractName) { String name = + * contractName + "/" + contractName; + * + * //启动合约 ContractManager cm2 = new ContractManager(); cm2.yjsPath = JudgeStatus.yjsPath; + * Contract c = new Contract(); c.setType(ContractType.Sole); String id = "169412582"; + * c.setID(id); c.setScript(RedoRecoverTest.file2Str( + * "/Users/zhangyixuan/Lab/newProject/BDContract/front-agent/BDWareProjectDir/public/" + name + + * ".yjs")); String key = + * "{\"publicKey\":\"041d7b4818817736b3d64305675f04e9a00a12ce23c72e6740f83785edcffec6bae2c178fbc934f66d72e26f89a2e7a2c966dc3f0a8e415663c9b933af7a2e9ff8\",\"privateKey\":\"18505919022281880113072981827955639221458448578012075254857346196103069175443\"}"; + * SM2KeyPair pair = SM2KeyPair.fromJson(key); c.doSignature(pair); + * System.out.println("<<>> : " + cm2.startContractAndRedirect(c, null)); + * + * + * + * + * //loadMemory long time1 = System.currentTimeMillis(); File mem = new + * File("./cp/recoverTestFiles/memory/" + contractName + ".txt"); String res = + * cm2.loadMemory(contractName, mem.getAbsolutePath()); long time2 = System.currentTimeMillis(); + * try { Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch + * block e.printStackTrace(); } System.out.println("<<>> : " + res); double cost = + * ((double)time2 - (double)time1)/1000; System.out.println("load memory 用时" + cost + "s"); + * + * + * String content1 = MemoryDumpUtil.getContentFromFile(mem.getAbsolutePath()); String content2 = + * cm2.dumpContract(contractName, ""); System.out.println("load之后dump : \n" + content2 + + * "\n\n"); System.out.println(content1.equals(content2)); } + */ } diff --git a/src/test/java/org/bdware/sc/memory/test/MemoryDumpTest.java b/src/test/java/org/bdware/sc/memory/test/MemoryDumpTest.java index af994aa..abc91ab 100644 --- a/src/test/java/org/bdware/sc/memory/test/MemoryDumpTest.java +++ b/src/test/java/org/bdware/sc/memory/test/MemoryDumpTest.java @@ -4,59 +4,40 @@ import java.io.IOException; public class MemoryDumpTest { public static void main(String[] args) throws IOException { - //memoryRecord("Z_Test_pub2","Z_Test_pub2-1"); - //memoryRecord("Test","Test1"); + // memoryRecord("Z_Test_pub2","Z_Test_pub2-1"); + // memoryRecord("Test","Test1"); } -/* //仅支持public路径下测试 - public static void memoryRecord(String contractName,String argsFileName) { - String name = contractName + "/" + contractName; - - //启动合约 - ContractManager cm1 = new ContractManager(); - cm1.yjsPath = JudgeStatus.yjsPath; - Contract c = new Contract(); - c.setType(ContractType.Sole); - String id = "169412582"; - c.setID(id); - c.setScript(RedoRecoverTest.file2Str("/Users/zhangyixuan/Lab/newProject/BDContract/front-agent/BDWareProjectDir/public/" + name + ".yjs")); - String key = "{\"publicKey\":\"041d7b4818817736b3d64305675f04e9a00a12ce23c72e6740f83785edcffec6bae2c178fbc934f66d72e26f89a2e7a2c966dc3f0a8e415663c9b933af7a2e9ff8\",\"privateKey\":\"18505919022281880113072981827955639221458448578012075254857346196103069175443\"}"; - SM2KeyPair pair = SM2KeyPair.fromJson(key); - c.doSignature(pair); - System.out.println("<<>> : " + cm1.startContractAndRedirect(c, null)); - - - - //执行事务 - int count = 0; - ContractRequest cor = new ContractRequest(); - cor.setContractID(contractName); - cor.doSignature(pair); - File file = new File("./cp/recoverTestFiles/affairs/" + contractName + "/" + argsFileName); - try { - FileReader fr = new FileReader(file); - BufferedReader br = new BufferedReader(fr); - String s = null; - while((s=br.readLine()) != null) { - String[] strs = s.split(" "); - cor.setAction(strs[0]); - cor.setArg(strs[1]); - System.out.println("<<> NO." + (++count) + " [Result]" + cm1.execute(cor,null)); - } - } catch (IOException e1) { - // TODO Auto-generated catch block - e1.printStackTrace(); - } - - - //memoryDump - long time1 = System.currentTimeMillis(); - File mem = new File("./cp/recoverTestFiles/memory/" + contractName + ".txt"); - String memory = cm1.dumpContract(contractName,mem.getAbsolutePath()); - System.out.println("memory\n" + memory); - long time2 = System.currentTimeMillis(); - double cost = ((double)time2 - (double)time1)/1000; - System.out.println("共" + count + "项事务,dump memory 用时" + cost + "s"); - }*/ + /* + * //仅支持public路径下测试 public static void memoryRecord(String contractName,String argsFileName) { + * String name = contractName + "/" + contractName; + * + * //启动合约 ContractManager cm1 = new ContractManager(); cm1.yjsPath = JudgeStatus.yjsPath; + * Contract c = new Contract(); c.setType(ContractType.Sole); String id = "169412582"; + * c.setID(id); c.setScript(RedoRecoverTest.file2Str( + * "/Users/zhangyixuan/Lab/newProject/BDContract/front-agent/BDWareProjectDir/public/" + name + + * ".yjs")); String key = + * "{\"publicKey\":\"041d7b4818817736b3d64305675f04e9a00a12ce23c72e6740f83785edcffec6bae2c178fbc934f66d72e26f89a2e7a2c966dc3f0a8e415663c9b933af7a2e9ff8\",\"privateKey\":\"18505919022281880113072981827955639221458448578012075254857346196103069175443\"}"; + * SM2KeyPair pair = SM2KeyPair.fromJson(key); c.doSignature(pair); + * System.out.println("<<>> : " + cm1.startContractAndRedirect(c, null)); + * + * + * + * //执行事务 int count = 0; ContractRequest cor = new ContractRequest(); + * cor.setContractID(contractName); cor.doSignature(pair); File file = new + * File("./cp/recoverTestFiles/affairs/" + contractName + "/" + argsFileName); try { FileReader + * fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); String s = null; + * while((s=br.readLine()) != null) { String[] strs = s.split(" "); cor.setAction(strs[0]); + * cor.setArg(strs[1]); System.out.println("<<> NO." + (++count) + + * " [Result]" + cm1.execute(cor,null)); } } catch (IOException e1) { // TODO Auto-generated + * catch block e1.printStackTrace(); } + * + * + * //memoryDump long time1 = System.currentTimeMillis(); File mem = new + * File("./cp/recoverTestFiles/memory/" + contractName + ".txt"); String memory = + * cm1.dumpContract(contractName,mem.getAbsolutePath()); System.out.println("memory\n" + + * memory); long time2 = System.currentTimeMillis(); double cost = ((double)time2 - + * (double)time1)/1000; System.out.println("共" + count + "项事务,dump memory 用时" + cost + "s"); } + */ } diff --git a/src/test/java/org/bdware/sc/parser/YJSParserTest.java b/src/test/java/org/bdware/sc/parser/YJSParserTest.java index 9e62db9..af3634f 100644 --- a/src/test/java/org/bdware/sc/parser/YJSParserTest.java +++ b/src/test/java/org/bdware/sc/parser/YJSParserTest.java @@ -18,7 +18,8 @@ import java.io.InputStream; public class YJSParserTest { @Test public void elseTest() throws IOException { - InputStream resource = YJSParserTest.class.getClassLoader().getResourceAsStream("module1.yjs"); + InputStream resource = + YJSParserTest.class.getClassLoader().getResourceAsStream("module1.yjs"); JavaScriptLexer lexer; assert resource != null; lexer = new JavaScriptLexer(CharStreams.fromStream(resource)); @@ -37,7 +38,8 @@ public class YJSParserTest { @Test public void functionTest() throws IOException { - InputStream resource = YJSParserTest.class.getClassLoader().getResourceAsStream("function.yjs"); + InputStream resource = + YJSParserTest.class.getClassLoader().getResourceAsStream("function.yjs"); JavaScriptLexer lexer; assert resource != null; lexer = new JavaScriptLexer(CharStreams.fromStream(resource)); @@ -50,7 +52,8 @@ public class YJSParserTest { parser.addErrorListener(errorListener); parser.addErrorListener(new DiagnosticErrorListener()); YJSParser.ProgramContext tree = parser.program(); - for (YJSParser.ClzOrFunctionDeclarationContext clzOrFunc : tree.contractDeclar().clzOrFunctionDeclaration()) { + for (YJSParser.ClzOrFunctionDeclarationContext clzOrFunc : tree.contractDeclar() + .clzOrFunctionDeclaration()) { System.out.println(clzOrFunc.functionDeclaration().View()); } for (String str : errorListener.getResultList()) { @@ -59,7 +62,7 @@ public class YJSParserTest { } @Test - public void test() throws FileNotFoundException {//测试执行 engine 的 executeContract方法 + public void test() throws FileNotFoundException {// 测试执行 engine 的 executeContract方法 InputStream resource = new FileInputStream( "../front-agent/BDWareProjectDir/public/ARouteExample/ARouteExample.yjs"); YJSCompiler compiler = new YJSCompiler(); diff --git a/src/test/java/org/bdware/sc/test/ExecuteContractTest.java b/src/test/java/org/bdware/sc/test/ExecuteContractTest.java index 2c3f25e..4624f43 100644 --- a/src/test/java/org/bdware/sc/test/ExecuteContractTest.java +++ b/src/test/java/org/bdware/sc/test/ExecuteContractTest.java @@ -21,40 +21,33 @@ public class ExecuteContractTest { public static void main(String[] args) throws Exception { String testDir = "./scripts/test/"; File f = new File(testDir); - if (!f.exists()) f.mkdirs(); - String[] confs = - new String[]{ - "HelloAtMac", - "AnalysisAtMac", - "TFAtMac", - "HelloAtAli", - "AnalysisAtAli", - "TFAtAli" - }; + if (!f.exists()) + f.mkdirs(); + String[] confs = new String[] {"HelloAtMac", "AnalysisAtMac", "TFAtMac", "HelloAtAli", + "AnalysisAtAli", "TFAtAli"}; // http://59.110.5.194:8080/ - String[] urls = - new String[]{ - "http://127.0.0.1:18000/SCIDE/CMManager?action=executeContract&&contractID=HelloAtMac&operation=get&arg=world", - "http://127.0.0.1:18000/SCIDE/CMManager?action=executeContract&&contractID=AppDataContractAtMac&operation=connectDBAndQuery&arg=%7B%22type%22%3A%22takeout%22%2C%22detail%22%3A%22overall%22%2C%22district%22%3A%22%E5%AF%86%E4%BA%91%22%7D&requestID=1576408475257_93", - "http://127.0.0.1:18000/SCIDE/CMManager?action=executeContract&&contractID=ImageMatcherAtMac&operation=testLocal&arg=%2Fimgs%2Fcup.jpg", - "http://59.110.5.194:8080/SCIDE/CMManager?action=executeContract&&contractID=HelloAt59&operation=get&arg=world", - "http://59.110.5.194:8080/SCIDE/CMManager?action=executeContract&&contractID=AppDataContractAt59&operation=connectDBAndQuery&arg=%7B%22type%22%3A%22takeout%22%2C%22detail%22%3A%22overall%22%2C%22district%22%3A%22%E5%AF%86%E4%BA%91%22%7D&requestID=1576408475257_93", - "http://59.110.5.194:8080/SCIDE/CMManager?action=executeContract&&contractID=ImageMatcherAt59&operation=testLocal&arg=%2Fimgs%2Fcup.jpg" - }; + String[] urls = new String[] { + "http://127.0.0.1:18000/SCIDE/CMManager?action=executeContract&&contractID=HelloAtMac&operation=get&arg=world", + "http://127.0.0.1:18000/SCIDE/CMManager?action=executeContract&&contractID=AppDataContractAtMac&operation=connectDBAndQuery&arg=%7B%22type%22%3A%22takeout%22%2C%22detail%22%3A%22overall%22%2C%22district%22%3A%22%E5%AF%86%E4%BA%91%22%7D&requestID=1576408475257_93", + "http://127.0.0.1:18000/SCIDE/CMManager?action=executeContract&&contractID=ImageMatcherAtMac&operation=testLocal&arg=%2Fimgs%2Fcup.jpg", + "http://59.110.5.194:8080/SCIDE/CMManager?action=executeContract&&contractID=HelloAt59&operation=get&arg=world", + "http://59.110.5.194:8080/SCIDE/CMManager?action=executeContract&&contractID=AppDataContractAt59&operation=connectDBAndQuery&arg=%7B%22type%22%3A%22takeout%22%2C%22detail%22%3A%22overall%22%2C%22district%22%3A%22%E5%AF%86%E4%BA%91%22%7D&requestID=1576408475257_93", + "http://59.110.5.194:8080/SCIDE/CMManager?action=executeContract&&contractID=ImageMatcherAt59&operation=testLocal&arg=%2Fimgs%2Fcup.jpg"}; - // String[] urls = new String[] { + // String[] urls = new String[] { // - // "http://127.0.0.1:18000/SCIDE/CMManager?action=executeContract&&contractID=HelloAt59&operation=get&arg=world", + // "http://127.0.0.1:18000/SCIDE/CMManager?action=executeContract&&contractID=HelloAt59&operation=get&arg=world", // - // "http://127.0.0.1:18000/SCIDE/CMManager?action=executeContract&&contractID=AppDataContractAt59&operation=connectDBAndQuery&arg=%7B%22type%22%3A%22takeout%22%2C%22detail%22%3A%22overall%22%2C%22district%22%3A%22%E5%AF%86%E4%BA%91%22%7D&requestID=1576408475257_93", + // "http://127.0.0.1:18000/SCIDE/CMManager?action=executeContract&&contractID=AppDataContractAt59&operation=connectDBAndQuery&arg=%7B%22type%22%3A%22takeout%22%2C%22detail%22%3A%22overall%22%2C%22district%22%3A%22%E5%AF%86%E4%BA%91%22%7D&requestID=1576408475257_93", // - // "http://127.0.0.1:18000/SCIDE/CMManager?action=executeContract&&contractID=ImageMatcherAt59&operation=testLocal&arg=%2Fimgs%2Fcup.jpg", + // "http://127.0.0.1:18000/SCIDE/CMManager?action=executeContract&&contractID=ImageMatcherAt59&operation=testLocal&arg=%2Fimgs%2Fcup.jpg", // - // "http://59.110.5.194:8080/SCIDE/CMManager?action=executeContract&&contractID=HelloAtMac&operation=get&arg=world", + // "http://59.110.5.194:8080/SCIDE/CMManager?action=executeContract&&contractID=HelloAtMac&operation=get&arg=world", // - // "http://59.110.5.194:8080/SCIDE/CMManager?action=executeContract&&contractID=AppDataContractAtMac&operation=connectDBAndQuery&arg=%7B%22type%22%3A%22takeout%22%2C%22detail%22%3A%22overall%22%2C%22district%22%3A%22%E5%AF%86%E4%BA%91%22%7D&requestID=1576408475257_93", + // "http://59.110.5.194:8080/SCIDE/CMManager?action=executeContract&&contractID=AppDataContractAtMac&operation=connectDBAndQuery&arg=%7B%22type%22%3A%22takeout%22%2C%22detail%22%3A%22overall%22%2C%22district%22%3A%22%E5%AF%86%E4%BA%91%22%7D&requestID=1576408475257_93", // - // "http://59.110.5.194:8080/SCIDE/CMManager?action=executeContract&&contractID=ImageMatcherAtMac&operation=testLocal&arg=%2Fimgs%2Fcup.jpg" }; + // "http://59.110.5.194:8080/SCIDE/CMManager?action=executeContract&&contractID=ImageMatcherAtMac&operation=testLocal&arg=%2Fimgs%2Fcup.jpg" + // }; String output = "./scripts/test/executeContract_cross.txt"; FileOutputStream fout = new FileOutputStream(output, true); @@ -62,7 +55,7 @@ public class ExecuteContractTest { test(confs[i], urls[i], new PrintStream(fout)); // new PrintStream(fout) } int pre = 0; - for (; ; ) { + for (;;) { Thread.sleep(1000); int curr = c.busy.get() + c.success.get(); System.out.println(new Gson().toJson(c) + " --> " + (curr - pre) + " " + curr); @@ -82,10 +75,10 @@ public class ExecuteContractTest { // System.out.println("http result:" + r.response); if (r.get("response").toString().contains("busy")) c.busy.incrementAndGet(); - else c.success.incrementAndGet(); + else + c.success.incrementAndGet(); JsonObject jo = - JsonParser.parseString(r.get("response").toString()) - .getAsJsonObject(); + JsonParser.parseString(r.get("response").toString()).getAsJsonObject(); int exeTime = jo.get("executeTime").getAsInt(); out.println(conf + "\t" + exeTime); }); diff --git a/src/test/java/org/bdware/sc/test/PermissionStubDocGenerator.java b/src/test/java/org/bdware/sc/test/PermissionStubDocGenerator.java index d8e468c..ceff98b 100644 --- a/src/test/java/org/bdware/sc/test/PermissionStubDocGenerator.java +++ b/src/test/java/org/bdware/sc/test/PermissionStubDocGenerator.java @@ -36,11 +36,9 @@ public class PermissionStubDocGenerator { List methodList = Arrays.asList(methods); methodList.sort(Comparator.comparing(Method::getName)); System.out.println("## " + stub.permission() + "util\n"); - System.out.printf( - "可以使用@Permission(\"%s\")来引入%s对象。%n", - stub.permission(), stub.permission() + "util"); - System.out.printf( - "```\n@Permission(\"%s\")\ncontract %sExample{\n ...\n}\n```\n%n", + System.out.printf("可以使用@Permission(\"%s\")来引入%s对象。%n", stub.permission(), + stub.permission() + "util"); + System.out.printf("```\n@Permission(\"%s\")\ncontract %sExample{\n ...\n}\n```\n%n", stub.permission(), stub.permission()); for (Method m : methods) { @@ -52,15 +50,14 @@ public class PermissionStubDocGenerator { System.out.println("|---|---|---|"); int i = 1; for (Parameter p : parameters) { - System.out.printf( - "| %d | %s | %s |%n", - i, p.getName(), p.getType().getCanonicalName()); + System.out.printf("| %d | %s | %s |%n", i, p.getName(), + p.getType().getCanonicalName()); i++; } System.out.println("\n#### 使用示例\n"); System.out.println("```javascript"); - System.out.println( - "var ret = " + stub.permission() + "Util." + m.getName() + "();"); + System.out + .println("var ret = " + stub.permission() + "Util." + m.getName() + "();"); System.out.println("```"); } } diff --git a/src/test/java/org/bdware/sc/test/StartContractTest.java b/src/test/java/org/bdware/sc/test/StartContractTest.java index bcd8d62..d14c5b2 100644 --- a/src/test/java/org/bdware/sc/test/StartContractTest.java +++ b/src/test/java/org/bdware/sc/test/StartContractTest.java @@ -13,26 +13,18 @@ public class StartContractTest { public static void main(String[] args) throws Exception { String testDir = "./scripts/test/"; File f = new File(testDir); - if (!f.exists()) f.mkdirs(); - String[] confs = - new String[] { - "HelloAtMac", - "AnalysisAtMac", - "TFAtMac", - "HelloAtAli", - "AnalysisAtAli", - "TFAtAli" - }; + if (!f.exists()) + f.mkdirs(); + String[] confs = new String[] {"HelloAtMac", "AnalysisAtMac", "TFAtMac", "HelloAtAli", + "AnalysisAtAli", "TFAtAli"}; // http://59.110.5.194:8080/ - String[] urls = - new String[] { - "http://127.0.0.1:18000/SCIDE/CMManager?action=startContractBatched&fileList=[A2.yjs]", - "http://127.0.0.1:18000/SCIDE/CMManager?action=startContractBatched&fileList=[EleAnalysis.yjs]", - "http://127.0.0.1:18000/SCIDE/CMManager?action=startContract&owner=04266aa345154d8e224990e33dd47a3203dd927d8c4f6420f63fbab063d9ecb8c4d58751e81940f2092dc4609de22a1980f4d8df39730ba0ddd453e3fa3b7b2a6b&requestID=1576403183009&contractid=Hello&script=empty&path=/Tensorflow/imagematch.yjs&signature=815490e34a3c85bcfe54a740f91e8739cfd6e5daa6c73084de758705e754a43bb245c5c23ff9f46c2a99cc0d627d4cf1da583e20073e85455f6cb10c63bef86b", - "http://59.110.5.194:8080/SCIDE/CMManager?action=startContractBatched&fileList=[A2.yjs]", - "http://59.110.5.194:8080/SCIDE/CMManager?action=startContractBatched&fileList=[EleAnalysis.yjs]", - "http://59.110.5.194:8080/SCIDE/CMManager?action=startContract&owner=04266aa345154d8e224990e33dd47a3203dd927d8c4f6420f63fbab063d9ecb8c4d58751e81940f2092dc4609de22a1980f4d8df39730ba0ddd453e3fa3b7b2a6b&requestID=1576403183009&contractid=Hello&script=empty&path=/Tensorflow/imagematch.yjs&signature=815490e34a3c85bcfe54a740f91e8739cfd6e5daa6c73084de758705e754a43bb245c5c23ff9f46c2a99cc0d627d4cf1da583e20073e85455f6cb10c63bef86b" - }; + String[] urls = new String[] { + "http://127.0.0.1:18000/SCIDE/CMManager?action=startContractBatched&fileList=[A2.yjs]", + "http://127.0.0.1:18000/SCIDE/CMManager?action=startContractBatched&fileList=[EleAnalysis.yjs]", + "http://127.0.0.1:18000/SCIDE/CMManager?action=startContract&owner=04266aa345154d8e224990e33dd47a3203dd927d8c4f6420f63fbab063d9ecb8c4d58751e81940f2092dc4609de22a1980f4d8df39730ba0ddd453e3fa3b7b2a6b&requestID=1576403183009&contractid=Hello&script=empty&path=/Tensorflow/imagematch.yjs&signature=815490e34a3c85bcfe54a740f91e8739cfd6e5daa6c73084de758705e754a43bb245c5c23ff9f46c2a99cc0d627d4cf1da583e20073e85455f6cb10c63bef86b", + "http://59.110.5.194:8080/SCIDE/CMManager?action=startContractBatched&fileList=[A2.yjs]", + "http://59.110.5.194:8080/SCIDE/CMManager?action=startContractBatched&fileList=[EleAnalysis.yjs]", + "http://59.110.5.194:8080/SCIDE/CMManager?action=startContract&owner=04266aa345154d8e224990e33dd47a3203dd927d8c4f6420f63fbab063d9ecb8c4d58751e81940f2092dc4609de22a1980f4d8df39730ba0ddd453e3fa3b7b2a6b&requestID=1576403183009&contractid=Hello&script=empty&path=/Tensorflow/imagematch.yjs&signature=815490e34a3c85bcfe54a740f91e8739cfd6e5daa6c73084de758705e754a43bb245c5c23ff9f46c2a99cc0d627d4cf1da583e20073e85455f6cb10c63bef86b"}; String output = "./scripts/test/startContract.txt"; FileOutputStream fout = new FileOutputStream(output, true); diff --git a/src/test/java/org/bdware/sc/test/setMaskTest.java b/src/test/java/org/bdware/sc/test/setMaskTest.java index 454b01c..7d1d70a 100644 --- a/src/test/java/org/bdware/sc/test/setMaskTest.java +++ b/src/test/java/org/bdware/sc/test/setMaskTest.java @@ -5,140 +5,145 @@ import com.google.gson.JsonParser; import org.bdware.sc.http.HttpUtil; import org.junit.Test; -import java.io.File; -import java.net.URLEncoder; -import java.util.HashMap; -import java.util.Map; -import java.io.File; -import java.io.IOException; -import java.io.StringWriter; -import java.net.URLEncoder; import java.sql.*; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; + public class setMaskTest { @Test - public void test1(){ + public void test1() { - String url="http://127.0.0.1:21030/SCIDE/CMManager?action=setMask"; - String contractID="Hello1"; - String operation="hello1"; - String maskInfo="\"md5\""; - maskInfo=""; - String pubkey="04c111cde30c257eb9d891653e2a25eb15df5239dd9882389e8daeffd4b16f65f30b30688b58d3e2eaf25ce5a529a601e4924581b55cedf78d94d17864b73c51a7"; + String url = "http://127.0.0.1:21030/SCIDE/CMManager?action=setMask"; + String contractID = "Hello1"; + String operation = "hello1"; + String maskInfo = "\"md5\""; + maskInfo = ""; + String pubkey = + "04c111cde30c257eb9d891653e2a25eb15df5239dd9882389e8daeffd4b16f65f30b30688b58d3e2eaf25ce5a529a601e4924581b55cedf78d94d17864b73c51a7"; System.out.println(url); - String sign="30440220138c8ae1956f6af19ddb24b9a0fe5cf9cc4f8ecd25537a8b5c4dcd5f30a4f985022026475279687ccdfb112cbcc307e573333d80f45386281def4df54b8b7532ed79"; - //url+="&contractID="+contractID+"&operation="+operation+"&maskInfo="+maskInfo+"&pubkey="+pubkey+"&signature="+sign; - url+="&contractID="+contractID+"&operation="+operation+"&maskInfo="+maskInfo; - //url="http://127.0.0.1:21030/SCIDE/CMManager?action=setMask&&pubkey=04c111cde30c257eb9d891653e2a25eb15df5239dd9882389e8daeffd4b16f65f30b30688b58d3e2eaf25ce5a529a601e4924581b55cedf78d94d17864b73c51a7&signature=304602210082f33a4cdbf483428c701753eb08ca430f855d52df18547a367fe14ee638761f022100cab7fa189b03248b54f3cb679d7df6c2548f6743256cad6ee772c532bb34bdf9"; - //url="http://127.0.0.1:21030/SCIDE/CMManager?action=ping"; - //url="http://127.0.0.1:21030/SCIDE/CMManager?action=setMask"+"&contractID="+contractID+"&maskInfo="+maskInfo; - //url="http://127.0.0.1:21030/SCIDE/CMManager?action=ping"; + String sign = + "30440220138c8ae1956f6af19ddb24b9a0fe5cf9cc4f8ecd25537a8b5c4dcd5f30a4f985022026475279687ccdfb112cbcc307e573333d80f45386281def4df54b8b7532ed79"; + // url+="&contractID="+contractID+"&operation="+operation+"&maskInfo="+maskInfo+"&pubkey="+pubkey+"&signature="+sign; + url += "&contractID=" + contractID + "&operation=" + operation + "&maskInfo=" + maskInfo; + // url="http://127.0.0.1:21030/SCIDE/CMManager?action=setMask&&pubkey=04c111cde30c257eb9d891653e2a25eb15df5239dd9882389e8daeffd4b16f65f30b30688b58d3e2eaf25ce5a529a601e4924581b55cedf78d94d17864b73c51a7&signature=304602210082f33a4cdbf483428c701753eb08ca430f855d52df18547a367fe14ee638761f022100cab7fa189b03248b54f3cb679d7df6c2548f6743256cad6ee772c532bb34bdf9"; + // url="http://127.0.0.1:21030/SCIDE/CMManager?action=ping"; + // url="http://127.0.0.1:21030/SCIDE/CMManager?action=setMask"+"&contractID="+contractID+"&maskInfo="+maskInfo; + // url="http://127.0.0.1:21030/SCIDE/CMManager?action=ping"; System.out.println(url); System.out.println(HttpUtil.httpGet(url.toString())); String resp = HttpUtil.httpGet(url.toString()).get("response").toString(); System.out.println(resp); } - @Test - public void testSetMock(){ - String url="http://127.0.0.1:21030/SCIDE/CMManager?action=setMock"; - String contractID="Hello1"; - String operation="hello1"; - String mockInfo="\"@string\""; - //String mockInfo=""; - String pubkey="04c111cde30c257eb9d891653e2a25eb15df5239dd9882389e8daeffd4b16f65f30b30688b58d3e2eaf25ce5a529a601e4924581b55cedf78d94d17864b73c51a7"; + @Test + public void testSetMock() { + + String url = "http://127.0.0.1:21030/SCIDE/CMManager?action=setMock"; + String contractID = "Hello1"; + String operation = "hello1"; + String mockInfo = "\"@string\""; + // String mockInfo=""; + String pubkey = + "04c111cde30c257eb9d891653e2a25eb15df5239dd9882389e8daeffd4b16f65f30b30688b58d3e2eaf25ce5a529a601e4924581b55cedf78d94d17864b73c51a7"; System.out.println(url); - String sign="30440220138c8ae1956f6af19ddb24b9a0fe5cf9cc4f8ecd25537a8b5c4dcd5f30a4f985022026475279687ccdfb112cbcc307e573333d80f45386281def4df54b8b7532ed79"; - //url+="&contractID="+contractID+"&operation="+operation+"&maskInfo="+maskInfo+"&pubkey="+pubkey+"&signature="+sign; - url+="&contractID="+contractID+"&operation="+operation+"&mockInfo="+mockInfo; - //url="http://127.0.0.1:21030/SCIDE/CMManager?action=setMask&&pubkey=04c111cde30c257eb9d891653e2a25eb15df5239dd9882389e8daeffd4b16f65f30b30688b58d3e2eaf25ce5a529a601e4924581b55cedf78d94d17864b73c51a7&signature=304602210082f33a4cdbf483428c701753eb08ca430f855d52df18547a367fe14ee638761f022100cab7fa189b03248b54f3cb679d7df6c2548f6743256cad6ee772c532bb34bdf9"; - //url="http://127.0.0.1:21030/SCIDE/CMManager?action=ping"; - //url="http://127.0.0.1:21030/SCIDE/CMManager?action=setMask"+"&contractID="+contractID+"&maskInfo="+maskInfo; - //url="http://127.0.0.1:21030/SCIDE/CMManager?action=ping"; + String sign = + "30440220138c8ae1956f6af19ddb24b9a0fe5cf9cc4f8ecd25537a8b5c4dcd5f30a4f985022026475279687ccdfb112cbcc307e573333d80f45386281def4df54b8b7532ed79"; + // url+="&contractID="+contractID+"&operation="+operation+"&maskInfo="+maskInfo+"&pubkey="+pubkey+"&signature="+sign; + url += "&contractID=" + contractID + "&operation=" + operation + "&mockInfo=" + mockInfo; + // url="http://127.0.0.1:21030/SCIDE/CMManager?action=setMask&&pubkey=04c111cde30c257eb9d891653e2a25eb15df5239dd9882389e8daeffd4b16f65f30b30688b58d3e2eaf25ce5a529a601e4924581b55cedf78d94d17864b73c51a7&signature=304602210082f33a4cdbf483428c701753eb08ca430f855d52df18547a367fe14ee638761f022100cab7fa189b03248b54f3cb679d7df6c2548f6743256cad6ee772c532bb34bdf9"; + // url="http://127.0.0.1:21030/SCIDE/CMManager?action=ping"; + // url="http://127.0.0.1:21030/SCIDE/CMManager?action=setMask"+"&contractID="+contractID+"&maskInfo="+maskInfo; + // url="http://127.0.0.1:21030/SCIDE/CMManager?action=ping"; System.out.println(url); System.out.println(HttpUtil.httpGet(url.toString())); String resp = HttpUtil.httpGet(url.toString()).get("response").toString(); System.out.println(resp); } - @Test - public void testGetMock(){ - String url="http://127.0.0.1:21030/SCIDE/CMManager?action=getMock"; - String contractID="Hello1"; - String operation="hello1"; - String mockInfo="@string"; - String pubkey="04c111cde30c257eb9d891653e2a25eb15df5239dd9882389e8daeffd4b16f65f30b30688b58d3e2eaf25ce5a529a601e4924581b55cedf78d94d17864b73c51a7"; + @Test + public void testGetMock() { + + String url = "http://127.0.0.1:21030/SCIDE/CMManager?action=getMock"; + String contractID = "Hello1"; + String operation = "hello1"; + String mockInfo = "@string"; + String pubkey = + "04c111cde30c257eb9d891653e2a25eb15df5239dd9882389e8daeffd4b16f65f30b30688b58d3e2eaf25ce5a529a601e4924581b55cedf78d94d17864b73c51a7"; System.out.println(url); - String sign="30440220138c8ae1956f6af19ddb24b9a0fe5cf9cc4f8ecd25537a8b5c4dcd5f30a4f985022026475279687ccdfb112cbcc307e573333d80f45386281def4df54b8b7532ed79"; - //url+="&contractID="+contractID+"&operation="+operation+"&maskInfo="+maskInfo+"&pubkey="+pubkey+"&signature="+sign; - url+="&contractID="+contractID+"&operation="+operation; - //url="http://127.0.0.1:21030/SCIDE/CMManager?action=setMask&&pubkey=04c111cde30c257eb9d891653e2a25eb15df5239dd9882389e8daeffd4b16f65f30b30688b58d3e2eaf25ce5a529a601e4924581b55cedf78d94d17864b73c51a7&signature=304602210082f33a4cdbf483428c701753eb08ca430f855d52df18547a367fe14ee638761f022100cab7fa189b03248b54f3cb679d7df6c2548f6743256cad6ee772c532bb34bdf9"; - //url="http://127.0.0.1:21030/SCIDE/CMManager?action=ping"; - //url="http://127.0.0.1:21030/SCIDE/CMManager?action=setMask"+"&contractID="+contractID+"&maskInfo="+maskInfo; - //url="http://127.0.0.1:21030/SCIDE/CMManager?action=ping"; + String sign = + "30440220138c8ae1956f6af19ddb24b9a0fe5cf9cc4f8ecd25537a8b5c4dcd5f30a4f985022026475279687ccdfb112cbcc307e573333d80f45386281def4df54b8b7532ed79"; + // url+="&contractID="+contractID+"&operation="+operation+"&maskInfo="+maskInfo+"&pubkey="+pubkey+"&signature="+sign; + url += "&contractID=" + contractID + "&operation=" + operation; + // url="http://127.0.0.1:21030/SCIDE/CMManager?action=setMask&&pubkey=04c111cde30c257eb9d891653e2a25eb15df5239dd9882389e8daeffd4b16f65f30b30688b58d3e2eaf25ce5a529a601e4924581b55cedf78d94d17864b73c51a7&signature=304602210082f33a4cdbf483428c701753eb08ca430f855d52df18547a367fe14ee638761f022100cab7fa189b03248b54f3cb679d7df6c2548f6743256cad6ee772c532bb34bdf9"; + // url="http://127.0.0.1:21030/SCIDE/CMManager?action=ping"; + // url="http://127.0.0.1:21030/SCIDE/CMManager?action=setMask"+"&contractID="+contractID+"&maskInfo="+maskInfo; + // url="http://127.0.0.1:21030/SCIDE/CMManager?action=ping"; System.out.println(url); System.out.println(HttpUtil.httpGet(url.toString())); String resp = HttpUtil.httpGet(url.toString()).get("response").toString(); System.out.println(resp); } - @Test - public void test3(){ - String url="http://127.0.0.1:21030/SCIDE/CMManager?action=setMask"; - String contractID="Hello"; - String operation="hello3"; - String maskInfo=""; - String pubkey="04c111cde30c257eb9d891653e2a25eb15df5239dd9882389e8daeffd4b16f65f30b30688b58d3e2eaf25ce5a529a601e4924581b55cedf78d94d17864b73c51a7"; + @Test + public void test3() { + + String url = "http://127.0.0.1:21030/SCIDE/CMManager?action=setMask"; + String contractID = "Hello"; + String operation = "hello3"; + String maskInfo = ""; + String pubkey = + "04c111cde30c257eb9d891653e2a25eb15df5239dd9882389e8daeffd4b16f65f30b30688b58d3e2eaf25ce5a529a601e4924581b55cedf78d94d17864b73c51a7"; System.out.println(url); - String sign="30440220138c8ae1956f6af19ddb24b9a0fe5cf9cc4f8ecd25537a8b5c4dcd5f30a4f985022026475279687ccdfb112cbcc307e573333d80f45386281def4df54b8b7532ed79"; - //url+="&contractID="+contractID+"&operation="+operation+"&maskInfo="+maskInfo+"&pubkey="+pubkey+"&signature="+sign; - url+="&contractID="+contractID+"&operation="+operation+"&maskInfo="+maskInfo; - //url="http://127.0.0.1:21030/SCIDE/CMManager?action=setMask&&pubkey=04c111cde30c257eb9d891653e2a25eb15df5239dd9882389e8daeffd4b16f65f30b30688b58d3e2eaf25ce5a529a601e4924581b55cedf78d94d17864b73c51a7&signature=304602210082f33a4cdbf483428c701753eb08ca430f855d52df18547a367fe14ee638761f022100cab7fa189b03248b54f3cb679d7df6c2548f6743256cad6ee772c532bb34bdf9"; - //url="http://127.0.0.1:21030/SCIDE/CMManager?action=ping"; - //url="http://127.0.0.1:21030/SCIDE/CMManager?action=setMask"+"&contractID="+contractID+"&maskInfo="+maskInfo; - //url="http://127.0.0.1:21030/SCIDE/CMManager?action=ping"; + String sign = + "30440220138c8ae1956f6af19ddb24b9a0fe5cf9cc4f8ecd25537a8b5c4dcd5f30a4f985022026475279687ccdfb112cbcc307e573333d80f45386281def4df54b8b7532ed79"; + // url+="&contractID="+contractID+"&operation="+operation+"&maskInfo="+maskInfo+"&pubkey="+pubkey+"&signature="+sign; + url += "&contractID=" + contractID + "&operation=" + operation + "&maskInfo=" + maskInfo; + // url="http://127.0.0.1:21030/SCIDE/CMManager?action=setMask&&pubkey=04c111cde30c257eb9d891653e2a25eb15df5239dd9882389e8daeffd4b16f65f30b30688b58d3e2eaf25ce5a529a601e4924581b55cedf78d94d17864b73c51a7&signature=304602210082f33a4cdbf483428c701753eb08ca430f855d52df18547a367fe14ee638761f022100cab7fa189b03248b54f3cb679d7df6c2548f6743256cad6ee772c532bb34bdf9"; + // url="http://127.0.0.1:21030/SCIDE/CMManager?action=ping"; + // url="http://127.0.0.1:21030/SCIDE/CMManager?action=setMask"+"&contractID="+contractID+"&maskInfo="+maskInfo; + // url="http://127.0.0.1:21030/SCIDE/CMManager?action=ping"; System.out.println(url); System.out.println(HttpUtil.httpGet(url.toString())); String resp = HttpUtil.httpGet(url.toString()).get("response").toString(); System.out.println(resp); } + @Test - public void test2(){ - String s1="{\"score\":\"md5\",\"name\":\"aes\"}"; - JsonElement je1=JsonParser.parseString("{\"score\":\"md5\",\"name\":\"aes\"}"); + public void test2() { + String s1 = "{\"score\":\"md5\",\"name\":\"aes\"}"; + JsonElement je1 = JsonParser.parseString("{\"score\":\"md5\",\"name\":\"aes\"}"); System.out.println(je1); } + @Test - public void testHangUp(){ - String url="http://127.0.0.1:21030/SCIDE/CMManager?action="; - String contractID="Hello"; - String urlHangUp=url+"hangUpContractProcess&contractID="+contractID; - String urlResume=url+"resumeContractProcess&contractID="+contractID; + public void testHangUp() { + String url = "http://127.0.0.1:21030/SCIDE/CMManager?action="; + String contractID = "Hello"; + String urlHangUp = url + "hangUpContractProcess&contractID=" + contractID; + String urlResume = url + "resumeContractProcess&contractID=" + contractID; System.out.println(urlHangUp); String resp1 = HttpUtil.httpGet(urlHangUp.toString()).get("response").toString(); System.out.println(resp1); - //String operation="hello3"; + // String operation="hello3"; } + @Test - public void testResume(){ - String url="http://127.0.0.1:21030/SCIDE/CMManager?action="; - String contractID="Hello"; - String urlHangUp=url+"hangUpContractProcess&contractID="+contractID; - String urlResume=url+"resumeContractProcess&contractID="+contractID; + public void testResume() { + String url = "http://127.0.0.1:21030/SCIDE/CMManager?action="; + String contractID = "Hello"; + String urlHangUp = url + "hangUpContractProcess&contractID=" + contractID; + String urlResume = url + "resumeContractProcess&contractID=" + contractID; System.out.println(urlResume); String resp2 = HttpUtil.httpGet(urlResume.toString()).get("response").toString(); System.out.println(resp2); - //String operation="hello3"; + // String operation="hello3"; } + @Test - public void testMysql(){ - String contractID="AAA12BMySQL"; - String url="http://127.0.0.1:21030/SCIDE/CMManager?action=getMask"+"&contractID="+contractID+"&requestID=1"; + public void testMysql() { + String contractID = "AAA12BMySQL"; + String url = "http://127.0.0.1:21030/SCIDE/CMManager?action=getMask" + "&contractID=" + + contractID + "&requestID=1"; System.out.println(url); String resp = HttpUtil.httpGet(url.toString()).get("response").toString(); diff --git a/src/test/java/org/bdware/sc/test/synctest/JudgeStatus.java b/src/test/java/org/bdware/sc/test/synctest/JudgeStatus.java index 6f6e799..bf3ecf7 100644 --- a/src/test/java/org/bdware/sc/test/synctest/JudgeStatus.java +++ b/src/test/java/org/bdware/sc/test/synctest/JudgeStatus.java @@ -21,7 +21,7 @@ public class JudgeStatus { System.out.println(content2 + "\n\n"); - if(content1.equals(content2)) + if (content1.equals(content2)) System.out.println("Success!"); else System.out.println("Failed!"); diff --git a/src/test/java/org/bdware/sc/test/synctest/PrintTraceRecords.java b/src/test/java/org/bdware/sc/test/synctest/PrintTraceRecords.java index 77db8f6..b7a89e0 100644 --- a/src/test/java/org/bdware/sc/test/synctest/PrintTraceRecords.java +++ b/src/test/java/org/bdware/sc/test/synctest/PrintTraceRecords.java @@ -1,18 +1,18 @@ package org.bdware.sc.test.synctest; -import org.bdware.sc.trace.TraceRecordUtil; public class PrintTraceRecords { public static void main(String[] args) { -// String path = "/Users/zhangyixuan/Lab/new/BDContract/front-agent/BDWareProjectDir/trace/"; -// String contractName = "Test"; -// path += (contractName + "/"); -// -// String path1 = ""; -// -// String content = TraceRecordUtil.getTraceRecordsByFile(path + path1); -// System.out.println(content); + // String path = + // "/Users/zhangyixuan/Lab/new/BDContract/front-agent/BDWareProjectDir/trace/"; + // String contractName = "Test"; + // path += (contractName + "/"); + // + // String path1 = ""; + // + // String content = TraceRecordUtil.getTraceRecordsByFile(path + path1); + // System.out.println(content); } } diff --git a/src/test/java/org/bdware/sc/test/synctest/PrintTransRecords.java b/src/test/java/org/bdware/sc/test/synctest/PrintTransRecords.java index e85fd3a..f59cc1d 100644 --- a/src/test/java/org/bdware/sc/test/synctest/PrintTransRecords.java +++ b/src/test/java/org/bdware/sc/test/synctest/PrintTransRecords.java @@ -1,18 +1,17 @@ package org.bdware.sc.test.synctest; -import org.bdware.sc.redo.TransRecordUtil; -import org.bdware.sc.trace.TraceRecordUtil; public class PrintTransRecords { public static void main(String[] args) { -// String path = "/Users/zhangyixuan/Lab/new/BDContract/front-agent/BDWareProjectDir/trans/"; -// String contractName = "Test"; -// path += (contractName + "/"); -// -// String path1 = ""; -// -// String content = TransRecordUtil.getTransRecordsByFile(path + path1); -// System.out.println(content); + // String path = + // "/Users/zhangyixuan/Lab/new/BDContract/front-agent/BDWareProjectDir/trans/"; + // String contractName = "Test"; + // path += (contractName + "/"); + // + // String path1 = ""; + // + // String content = TransRecordUtil.getTransRecordsByFile(path + path1); + // System.out.println(content); } } diff --git a/src/test/java/org/bdware/sc/test/synctest/RedoRecordTest.java b/src/test/java/org/bdware/sc/test/synctest/RedoRecordTest.java index 45e5982..bbb9834 100644 --- a/src/test/java/org/bdware/sc/test/synctest/RedoRecordTest.java +++ b/src/test/java/org/bdware/sc/test/synctest/RedoRecordTest.java @@ -3,65 +3,70 @@ package org.bdware.sc.test.synctest; public class RedoRecordTest { public static void main(String[] args) { - transRecord("Z_Test_pub1","Z_Test_pub1-1"); + transRecord("Z_Test_pub1", "Z_Test_pub1-1"); } - //仅支持public路径下测试 - public static void transRecord(String contractName,String argsFileName) { -// String name = contractName + "/" + contractName; -// -// //启动合约 -// ContractManager cm1 = new ContractManager(); -// cm1.yjsPath = JudgeStatus.yjsPath; -// Contract c = new Contract(); -// c.setType(ContractType.Sole); -// String id = "169412582"; -// c.setID(id); -// c.setScript(RedoRecoverTest.file2Str("/Users/zhangyixuan/Lab/BDWareHttp/BDWareProjectDir/public/" + name + ".yjs")); -// String key = "{\"publicKey\":\"041d7b4818817736b3d64305675f04e9a00a12ce23c72e6740f83785edcffec6bae2c178fbc934f66d72e26f89a2e7a2c966dc3f0a8e415663c9b933af7a2e9ff8\",\"privateKey\":\"18505919022281880113072981827955639221458448578012075254857346196103069175443\"}"; -// SM2KeyPair pair = SM2KeyPair.fromJson(key); -// c.doSignature(pair); -// System.out.println("<<>> : " + cm1.startContractAndRedirect(c, null)); -// cm1.engineStartTransRecord(contractName); -// -// -// //执行事务 -// int count = 0; -// ContractRequest cor = new ContractRequest(); -// cor.setContractID(contractName); -// cor.doSignature(pair); -// File file = new File("./cp/recoverTestFiles/affairs/" + contractName + "/" + argsFileName); -// try { -// FileReader fr = new FileReader(file); -// BufferedReader br = new BufferedReader(fr); -// String s = null; -// while((s=br.readLine()) != null) { -// String[] strs = s.split(" "); -// cor.setAction(strs[0]); -// cor.setArg(strs[1]); -// System.out.println("<<> NO." + (++count) + " [Result]" + cm1.execute(cor)); -// } -// } catch (IOException e1) { -// // TODO Auto-generated catch block -// e1.printStackTrace(); -// } -// -// -// -// -// //记录最终状态 -// cm1.saveTransRecords(contractName,"./cp/recoverTestFiles/trans/" + contractName); -// System.out.println("<<>> : \n" + cm1.engineTransRecords2String(contractName)); -// -// -// File mem = new File("./cp/recoverTestFiles/memory/" + contractName + ".txt"); -// String memory1 = cm1.dumpContract(contractName,mem.getAbsolutePath()); -// if(memory1.equals("{\"status\":\"Error\",\"result\":\"Timeout!\"}")) { -// System.out.println("<<>> failed"); -// return; -// } -// System.out.println("<<>>" + memory1); -// System.out.println("共" +count + "项事务记录"); + // 仅支持public路径下测试 + public static void transRecord(String contractName, String argsFileName) { + // String name = contractName + "/" + contractName; + // + // //启动合约 + // ContractManager cm1 = new ContractManager(); + // cm1.yjsPath = JudgeStatus.yjsPath; + // Contract c = new Contract(); + // c.setType(ContractType.Sole); + // String id = "169412582"; + // c.setID(id); + // c.setScript(RedoRecoverTest.file2Str("/Users/zhangyixuan/Lab/BDWareHttp/BDWareProjectDir/public/" + // + name + ".yjs")); + // String key = + // "{\"publicKey\":\"041d7b4818817736b3d64305675f04e9a00a12ce23c72e6740f83785edcffec6bae2c178fbc934f66d72e26f89a2e7a2c966dc3f0a8e415663c9b933af7a2e9ff8\",\"privateKey\":\"18505919022281880113072981827955639221458448578012075254857346196103069175443\"}"; + // SM2KeyPair pair = SM2KeyPair.fromJson(key); + // c.doSignature(pair); + // System.out.println("<<>> : " + cm1.startContractAndRedirect(c, null)); + // cm1.engineStartTransRecord(contractName); + // + // + // //执行事务 + // int count = 0; + // ContractRequest cor = new ContractRequest(); + // cor.setContractID(contractName); + // cor.doSignature(pair); + // File file = new File("./cp/recoverTestFiles/affairs/" + contractName + "/" + + // argsFileName); + // try { + // FileReader fr = new FileReader(file); + // BufferedReader br = new BufferedReader(fr); + // String s = null; + // while((s=br.readLine()) != null) { + // String[] strs = s.split(" "); + // cor.setAction(strs[0]); + // cor.setArg(strs[1]); + // System.out.println("<<> NO." + (++count) + " [Result]" + + // cm1.execute(cor)); + // } + // } catch (IOException e1) { + // // TODO Auto-generated catch block + // e1.printStackTrace(); + // } + // + // + // + // + // //记录最终状态 + // cm1.saveTransRecords(contractName,"./cp/recoverTestFiles/trans/" + contractName); + // System.out.println("<<>> : \n" + + // cm1.engineTransRecords2String(contractName)); + // + // + // File mem = new File("./cp/recoverTestFiles/memory/" + contractName + ".txt"); + // String memory1 = cm1.dumpContract(contractName,mem.getAbsolutePath()); + // if(memory1.equals("{\"status\":\"Error\",\"result\":\"Timeout!\"}")) { + // System.out.println("<<>> failed"); + // return; + // } + // System.out.println("<<>>" + memory1); + // System.out.println("共" +count + "项事务记录"); } } diff --git a/src/test/java/org/bdware/sc/test/synctest/RedoRecoverTest.java b/src/test/java/org/bdware/sc/test/synctest/RedoRecoverTest.java index a16425e..0b14b9d 100644 --- a/src/test/java/org/bdware/sc/test/synctest/RedoRecoverTest.java +++ b/src/test/java/org/bdware/sc/test/synctest/RedoRecoverTest.java @@ -7,84 +7,91 @@ import java.util.Scanner; public class RedoRecoverTest { public static void main(String[] args) throws IOException { - transRecover("Z_Test_pub1","Z_Test_pub1-1"); + transRecover("Z_Test_pub1", "Z_Test_pub1-1"); } - public static void transRecover(String contractName,String argsFileName) { -// //获得之前的状态 -// ObjectInputStream reader; -// String memory = null; -// try { -// FileInputStream fileout = new FileInputStream("./cp/recoverTestFiles/memory/" + contractName + ".txt"); -// GZIPInputStream gzin = new GZIPInputStream(fileout); -// reader = new ObjectInputStream(gzin); -// MemoryDump md = new MemoryDump(); -// md.setObjects((Map) reader.readObject());; -// reader.close(); -// memory = new GsonBuilder().setPrettyPrinting().create().toJson(md); -// } catch (IOException | ClassNotFoundException e) { -// e.printStackTrace(); -// } -// -// -// -// -// //读取文件中内容; -// int count = 0; -// try { -// File file = new File("./cp/recoverTestFiles/affairs/" + contractName + "/" + argsFileName); -// FileReader fr = new FileReader(file); -// BufferedReader br = new BufferedReader(fr); -// String s = null; -// while((s=br.readLine()) != null) { -// count++; -// } -// } catch (IOException e) { -// // TODO Auto-generated catch block -// e.printStackTrace(); -// } -// -// -// -// //启动 -// String name = contractName + "/" + contractName; -// ContractManager cm2 = new ContractManager(); -// cm2.yjsPath = JudgeStatus.yjsPath; -// Contract c = new Contract(); -// c.setType(ContractType.Sole); -// String id = "169412583"; -// c.setID(id); -// c.setScript(MemoryDumpRecoverTest .file2Str("/Users/zhangyixuan/Lab/newProject/BDContract/front-agent/BDWareProjectDir/public/" + name + ".yjs")); -// String key = "{\"publicKey\":\"041d7b4818817736b3d64305675f04e9a00a12ce23c72e6740f83785edcffec6bae2c178fbc934f66d72e26f89a2e7a2c966dc3f0a8e415663c9b933af7a2e9ff8\",\"privateKey\":\"18505919022281880113072981827955639221458448578012075254857346196103069175443\"}"; -// SM2KeyPair pair = SM2KeyPair.fromJson(key); -// c.doSignature(pair); -// System.out.println("Start a contract : " + cm2.startContractAndRedirect(c, System.out)); -// -// -// //recover -// long time1 = System.currentTimeMillis(); -// String traceFile = "./cp/recoverTestFiles/trans/" + contractName + ".trans"; -// cm2.setTransRecords(contractName,traceFile); -// cm2.recoverFromTransRecord(contractName,(count - 1) + ""); -// long time2 = System.currentTimeMillis(); -// double cost = ((double)time2 - (double)time1)/1000; -// -// -// //对比 -// String memory2 = cm2.dumpContract(contractName,""); -// System.out.println("<<>>" + memory2); -// if(memory.equals(memory2)) { -// System.out.println("####################\n####################\n####################"); -// System.out.println("Trans count = " + count + " Contract " + contractName + " recover success!"); -// System.out.println("####################\n####################\n####################"); -// } -// else { -// System.out.println("!!!!!!!!!!!!!!!!!!!!!\n!!!!!!!!!!!!!!!!!!!!!\n!!!!!!!!!!!!!!!!!!!!!"); -// System.out.println("Trans count = " + count + " Contract " + contractName + " recover failed!"); -// System.out.println("!!!!!!!!!!!!!!!!!!!!!\n!!!!!!!!!!!!!!!!!!!!!\n!!!!!!!!!!!!!!!!!!!!!"); -// -// } -// System.out.println("共" + count + "项事务恢复,用时" + cost + "s"); + public static void transRecover(String contractName, String argsFileName) { + // //获得之前的状态 + // ObjectInputStream reader; + // String memory = null; + // try { + // FileInputStream fileout = new FileInputStream("./cp/recoverTestFiles/memory/" + + // contractName + ".txt"); + // GZIPInputStream gzin = new GZIPInputStream(fileout); + // reader = new ObjectInputStream(gzin); + // MemoryDump md = new MemoryDump(); + // md.setObjects((Map) reader.readObject());; + // reader.close(); + // memory = new GsonBuilder().setPrettyPrinting().create().toJson(md); + // } catch (IOException | ClassNotFoundException e) { + // e.printStackTrace(); + // } + // + // + // + // + // //读取文件中内容; + // int count = 0; + // try { + // File file = new File("./cp/recoverTestFiles/affairs/" + contractName + "/" + + // argsFileName); + // FileReader fr = new FileReader(file); + // BufferedReader br = new BufferedReader(fr); + // String s = null; + // while((s=br.readLine()) != null) { + // count++; + // } + // } catch (IOException e) { + // // TODO Auto-generated catch block + // e.printStackTrace(); + // } + // + // + // + // //启动 + // String name = contractName + "/" + contractName; + // ContractManager cm2 = new ContractManager(); + // cm2.yjsPath = JudgeStatus.yjsPath; + // Contract c = new Contract(); + // c.setType(ContractType.Sole); + // String id = "169412583"; + // c.setID(id); + // c.setScript(MemoryDumpRecoverTest + // .file2Str("/Users/zhangyixuan/Lab/newProject/BDContract/front-agent/BDWareProjectDir/public/" + // + name + ".yjs")); + // String key = + // "{\"publicKey\":\"041d7b4818817736b3d64305675f04e9a00a12ce23c72e6740f83785edcffec6bae2c178fbc934f66d72e26f89a2e7a2c966dc3f0a8e415663c9b933af7a2e9ff8\",\"privateKey\":\"18505919022281880113072981827955639221458448578012075254857346196103069175443\"}"; + // SM2KeyPair pair = SM2KeyPair.fromJson(key); + // c.doSignature(pair); + // System.out.println("Start a contract : " + cm2.startContractAndRedirect(c, System.out)); + // + // + // //recover + // long time1 = System.currentTimeMillis(); + // String traceFile = "./cp/recoverTestFiles/trans/" + contractName + ".trans"; + // cm2.setTransRecords(contractName,traceFile); + // cm2.recoverFromTransRecord(contractName,(count - 1) + ""); + // long time2 = System.currentTimeMillis(); + // double cost = ((double)time2 - (double)time1)/1000; + // + // + // //对比 + // String memory2 = cm2.dumpContract(contractName,""); + // System.out.println("<<>>" + memory2); + // if(memory.equals(memory2)) { + // System.out.println("####################\n####################\n####################"); + // System.out.println("Trans count = " + count + " Contract " + contractName + " recover + // success!"); + // System.out.println("####################\n####################\n####################"); + // } + // else { + // System.out.println("!!!!!!!!!!!!!!!!!!!!!\n!!!!!!!!!!!!!!!!!!!!!\n!!!!!!!!!!!!!!!!!!!!!"); + // System.out.println("Trans count = " + count + " Contract " + contractName + " recover + // failed!"); + // System.out.println("!!!!!!!!!!!!!!!!!!!!!\n!!!!!!!!!!!!!!!!!!!!!\n!!!!!!!!!!!!!!!!!!!!!"); + // + // } + // System.out.println("共" + count + "项事务恢复,用时" + cost + "s"); } diff --git a/src/test/java/org/bdware/sc/test/synctest/TraceRecordTest.java b/src/test/java/org/bdware/sc/test/synctest/TraceRecordTest.java index 03b2345..20a5784 100644 --- a/src/test/java/org/bdware/sc/test/synctest/TraceRecordTest.java +++ b/src/test/java/org/bdware/sc/test/synctest/TraceRecordTest.java @@ -5,69 +5,75 @@ import java.io.IOException; public class TraceRecordTest { public static void main(String[] args) throws IOException { - traceRecord("Test","Test1"); - //traceRecord("BDCoin","BDCoin1"); - //traceRecord("AppData","AppData-3"); - //traceRecord("AppData","AppData-7"); - //traceRecord("Z_Test_pub2","Z_Test_pub2-1"); + traceRecord("Test", "Test1"); + // traceRecord("BDCoin","BDCoin1"); + // traceRecord("AppData","AppData-3"); + // traceRecord("AppData","AppData-7"); + // traceRecord("Z_Test_pub2","Z_Test_pub2-1"); } - //仅支持public路径下测试 - public static void traceRecord(String contractName,String argsFileName) { -// String name = contractName + "/" + contractName; -// -// //启动合约 -// ContractManager cm1 = new ContractManager(); -// cm1.yjsPath = JudgeStatus.yjsPath; -// Contract c = new Contract(); -// c.setType(ContractType.Sole); -// String id = "169412582"; -// c.setID(id); -// c.setScript(RedoRecoverTest.file2Str("/Users/zhangyixuan/Lab/newProject/BDContract/front-agent/BDWareProjectDir/public/" + name + ".yjs")); -// String key = "{\"publicKey\":\"041d7b4818817736b3d64305675f04e9a00a12ce23c72e6740f83785edcffec6bae2c178fbc934f66d72e26f89a2e7a2c966dc3f0a8e415663c9b933af7a2e9ff8\",\"privateKey\":\"18505919022281880113072981827955639221458448578012075254857346196103069175443\"}"; -// SM2KeyPair pair = SM2KeyPair.fromJson(key); -// c.doSignature(pair); -// System.out.println("<<>> : " + cm1.startContractAndRedirect(c, System.out)); -// cm1.startSync(contractName); -// cm1.changeSyncType(contractName, "Trace"); -// -// -// //执行事务 -// int count = 0; -// ContractRequest cor = new ContractRequest(); -// cor.setContractID(contractName); -// cor.doSignature(pair); -// File file = new File("./cp/recoverTestFiles/affairs/" + contractName + "/" + argsFileName); -// try { -// FileReader fr = new FileReader(file); -// BufferedReader br = new BufferedReader(fr); -// String s = null; -// while((s=br.readLine()) != null) { -// String[] strs = s.split(" "); -// cor.setAction(strs[0]); -// cor.setArg(strs[1]); -// System.out.println("<<> NO." + (++count) + " [Result]" + cm1.execute(cor)); -// } -// } catch (IOException e1) { -// // TODO Auto-generated catch block -// e1.printStackTrace(); -// } -// -// -// -// -// //记录最终状态 -// cm1.saveTraceRecords(contractName,"./cp/recoverTestFiles/trace/" + contractName); -// System.out.println("<<>> : \n" + cm1.engineTraceRecords2String(contractName)); -// -// -// File mem = new File("./cp/recoverTestFiles/memory/" + contractName + ".txt"); -// String memory1 = cm1.dumpContract(contractName,mem.getAbsolutePath()); -// if(memory1.equals("{\"status\":\"Error\",\"result\":\"Timeout!\"}")) { -// System.out.println("<<>> failed"); -// return; -// } -// System.out.println("<<>>" + memory1); -// System.out.println("共" +count + "项事务记录"); + // 仅支持public路径下测试 + public static void traceRecord(String contractName, String argsFileName) { + // String name = contractName + "/" + contractName; + // + // //启动合约 + // ContractManager cm1 = new ContractManager(); + // cm1.yjsPath = JudgeStatus.yjsPath; + // Contract c = new Contract(); + // c.setType(ContractType.Sole); + // String id = "169412582"; + // c.setID(id); + // c.setScript(RedoRecoverTest.file2Str("/Users/zhangyixuan/Lab/newProject/BDContract/front-agent/BDWareProjectDir/public/" + // + name + ".yjs")); + // String key = + // "{\"publicKey\":\"041d7b4818817736b3d64305675f04e9a00a12ce23c72e6740f83785edcffec6bae2c178fbc934f66d72e26f89a2e7a2c966dc3f0a8e415663c9b933af7a2e9ff8\",\"privateKey\":\"18505919022281880113072981827955639221458448578012075254857346196103069175443\"}"; + // SM2KeyPair pair = SM2KeyPair.fromJson(key); + // c.doSignature(pair); + // System.out.println("<<>> : " + cm1.startContractAndRedirect(c, + // System.out)); + // cm1.startSync(contractName); + // cm1.changeSyncType(contractName, "Trace"); + // + // + // //执行事务 + // int count = 0; + // ContractRequest cor = new ContractRequest(); + // cor.setContractID(contractName); + // cor.doSignature(pair); + // File file = new File("./cp/recoverTestFiles/affairs/" + contractName + "/" + + // argsFileName); + // try { + // FileReader fr = new FileReader(file); + // BufferedReader br = new BufferedReader(fr); + // String s = null; + // while((s=br.readLine()) != null) { + // String[] strs = s.split(" "); + // cor.setAction(strs[0]); + // cor.setArg(strs[1]); + // System.out.println("<<> NO." + (++count) + " [Result]" + + // cm1.execute(cor)); + // } + // } catch (IOException e1) { + // // TODO Auto-generated catch block + // e1.printStackTrace(); + // } + // + // + // + // + // //记录最终状态 + // cm1.saveTraceRecords(contractName,"./cp/recoverTestFiles/trace/" + contractName); + // System.out.println("<<>> : \n" + + // cm1.engineTraceRecords2String(contractName)); + // + // + // File mem = new File("./cp/recoverTestFiles/memory/" + contractName + ".txt"); + // String memory1 = cm1.dumpContract(contractName,mem.getAbsolutePath()); + // if(memory1.equals("{\"status\":\"Error\",\"result\":\"Timeout!\"}")) { + // System.out.println("<<>> failed"); + // return; + // } + // System.out.println("<<>>" + memory1); + // System.out.println("共" +count + "项事务记录"); } } diff --git a/src/test/java/org/bdware/sc/test/synctest/TraceRecoverTest.java b/src/test/java/org/bdware/sc/test/synctest/TraceRecoverTest.java index 6cca439..e056185 100644 --- a/src/test/java/org/bdware/sc/test/synctest/TraceRecoverTest.java +++ b/src/test/java/org/bdware/sc/test/synctest/TraceRecoverTest.java @@ -5,75 +5,81 @@ import java.io.IOException; public class TraceRecoverTest { public static void main(String[] args) throws IOException { - traceRecover("Test","Test1"); - //traceRecover("BDCoin","BDCoin1"); - //traceRecover("Z_Test_pub2","Z_Test_pub2-1"); - //traceRecover("AppData","AppData-3"); - //traceRecover("AppData","AppData-7"); + traceRecover("Test", "Test1"); + // traceRecover("BDCoin","BDCoin1"); + // traceRecover("Z_Test_pub2","Z_Test_pub2-1"); + // traceRecover("AppData","AppData-3"); + // traceRecover("AppData","AppData-7"); } - public static void traceRecover(String contractName,String argsFileName) { -// //获得之前的状态 -// File f = new File("./cp/recoverTestFiles/memory/" + contractName + ".txt"); -// String memory = MemoryDumpUtil.getContentFromFile(f.getAbsolutePath()); -// -// -// -// //读取文件中内容; -// int count = 0; -// try { -// File file = new File("./cp/recoverTestFiles/affairs/" + contractName + "/" + argsFileName); -// FileReader fr = new FileReader(file); -// BufferedReader br = new BufferedReader(fr); -// String s = null; -// while((s=br.readLine()) != null) { -// count++; -// } -// } catch (IOException e) { -// // TODO Auto-generated catch block -// e.printStackTrace(); -// } -// -// -// -// //启动 -// String name = contractName + "/" + contractName; -// ContractManager cm2 = new ContractManager(); -// cm2.yjsPath = JudgeStatus.yjsPath; -// Contract c = new Contract(); -// c.setType(ContractType.Sole); -// String id = "169412583"; -// c.setID(id); -// c.setScript(MemoryDumpRecoverTest.file2Str("/Users/zhangyixuan/Lab/newProject/BDContract/front-agent/BDWareProjectDir/public/" + name + ".yjs")); -// String key = "{\"publicKey\":\"041d7b4818817736b3d64305675f04e9a00a12ce23c72e6740f83785edcffec6bae2c178fbc934f66d72e26f89a2e7a2c966dc3f0a8e415663c9b933af7a2e9ff8\",\"privateKey\":\"18505919022281880113072981827955639221458448578012075254857346196103069175443\"}"; -// SM2KeyPair pair = SM2KeyPair.fromJson(key); -// c.doSignature(pair); -// System.out.println("<<>> : " + cm2.startContractAndRedirect(c, System.out)); -// -// -// //recover -// long time1 = System.currentTimeMillis(); -// String traceFile = "./cp/recoverTestFiles/trace/" + contractName + ".trace"; -// cm2.setTraceRecords(contractName,traceFile); -// cm2.recoverFromTraceRecord(contractName,(count - 1) + ""); -// long time2 = System.currentTimeMillis(); -// double cost = ((double)time2 - (double)time1)/1000; -// -// -// //对比 -// String memory2 = cm2.dumpContract(contractName,""); -// System.out.println("<<>>" + memory2); -// if(memory.equals(memory2)) { -// System.out.println("####################\n####################\n####################"); -// System.out.println("Trans count = " + count + " Contract " + contractName + " recover success!"); -// System.out.println("####################\n####################\n####################"); -// } -// else { -// System.out.println("!!!!!!!!!!!!!!!!!!!!!\n!!!!!!!!!!!!!!!!!!!!!\n!!!!!!!!!!!!!!!!!!!!!"); -// System.out.println("Trans count = " + count + " Contract " + contractName + " recover failed!"); -// System.out.println("!!!!!!!!!!!!!!!!!!!!!\n!!!!!!!!!!!!!!!!!!!!!\n!!!!!!!!!!!!!!!!!!!!!"); -// -// } -// System.out.println("共" + count + "项事务恢复,用时" + cost + "s"); + public static void traceRecover(String contractName, String argsFileName) { + // //获得之前的状态 + // File f = new File("./cp/recoverTestFiles/memory/" + contractName + ".txt"); + // String memory = MemoryDumpUtil.getContentFromFile(f.getAbsolutePath()); + // + // + // + // //读取文件中内容; + // int count = 0; + // try { + // File file = new File("./cp/recoverTestFiles/affairs/" + contractName + "/" + + // argsFileName); + // FileReader fr = new FileReader(file); + // BufferedReader br = new BufferedReader(fr); + // String s = null; + // while((s=br.readLine()) != null) { + // count++; + // } + // } catch (IOException e) { + // // TODO Auto-generated catch block + // e.printStackTrace(); + // } + // + // + // + // //启动 + // String name = contractName + "/" + contractName; + // ContractManager cm2 = new ContractManager(); + // cm2.yjsPath = JudgeStatus.yjsPath; + // Contract c = new Contract(); + // c.setType(ContractType.Sole); + // String id = "169412583"; + // c.setID(id); + // c.setScript(MemoryDumpRecoverTest.file2Str("/Users/zhangyixuan/Lab/newProject/BDContract/front-agent/BDWareProjectDir/public/" + // + name + ".yjs")); + // String key = + // "{\"publicKey\":\"041d7b4818817736b3d64305675f04e9a00a12ce23c72e6740f83785edcffec6bae2c178fbc934f66d72e26f89a2e7a2c966dc3f0a8e415663c9b933af7a2e9ff8\",\"privateKey\":\"18505919022281880113072981827955639221458448578012075254857346196103069175443\"}"; + // SM2KeyPair pair = SM2KeyPair.fromJson(key); + // c.doSignature(pair); + // System.out.println("<<>> : " + cm2.startContractAndRedirect(c, + // System.out)); + // + // + // //recover + // long time1 = System.currentTimeMillis(); + // String traceFile = "./cp/recoverTestFiles/trace/" + contractName + ".trace"; + // cm2.setTraceRecords(contractName,traceFile); + // cm2.recoverFromTraceRecord(contractName,(count - 1) + ""); + // long time2 = System.currentTimeMillis(); + // double cost = ((double)time2 - (double)time1)/1000; + // + // + // //对比 + // String memory2 = cm2.dumpContract(contractName,""); + // System.out.println("<<>>" + memory2); + // if(memory.equals(memory2)) { + // System.out.println("####################\n####################\n####################"); + // System.out.println("Trans count = " + count + " Contract " + contractName + " recover + // success!"); + // System.out.println("####################\n####################\n####################"); + // } + // else { + // System.out.println("!!!!!!!!!!!!!!!!!!!!!\n!!!!!!!!!!!!!!!!!!!!!\n!!!!!!!!!!!!!!!!!!!!!"); + // System.out.println("Trans count = " + count + " Contract " + contractName + " recover + // failed!"); + // System.out.println("!!!!!!!!!!!!!!!!!!!!!\n!!!!!!!!!!!!!!!!!!!!!\n!!!!!!!!!!!!!!!!!!!!!"); + // + // } + // System.out.println("共" + count + "项事务恢复,用时" + cost + "s"); } } diff --git a/src/test/java/org/bdware/sc/test/synctest/TransRecordTest.java b/src/test/java/org/bdware/sc/test/synctest/TransRecordTest.java index a0d49c7..39cbdab 100644 --- a/src/test/java/org/bdware/sc/test/synctest/TransRecordTest.java +++ b/src/test/java/org/bdware/sc/test/synctest/TransRecordTest.java @@ -8,33 +8,33 @@ public class TransRecordTest { static TransRecord currentTransRecord = new TransRecord("myFun", new JsonPrimitive("myArg")); public static void main(String[] args) { -// currentTransRecord.executes.put("k1","v1"); -// currentTransRecord.executes.put("k2","v2"); -// currentTransRecord.executes.put("k3","v3"); -// -// System.out.println("发送一个TransRecord\n" + currentTransRecord.toString()); -// -// String str1 = currentTransRecord.transferToString(); -// -// try { -// byte[] bytes = str1.getBytes("UTF-8"); -// -// try { -// String re = new String(bytes, "UTF-8"); -// TransRecord transRecord = TransRecord.loadFromStr(re); -// -// System.out.println("收到一个TransRecord\n" + transRecord.toString()); -// -// } catch (UnsupportedEncodingException e) { -// e.printStackTrace(); -// } -// -// -// -// -// } catch (Exception e) { -// e.printStackTrace(); -// } + // currentTransRecord.executes.put("k1","v1"); + // currentTransRecord.executes.put("k2","v2"); + // currentTransRecord.executes.put("k3","v3"); + // + // System.out.println("发送一个TransRecord\n" + currentTransRecord.toString()); + // + // String str1 = currentTransRecord.transferToString(); + // + // try { + // byte[] bytes = str1.getBytes("UTF-8"); + // + // try { + // String re = new String(bytes, "UTF-8"); + // TransRecord transRecord = TransRecord.loadFromStr(re); + // + // System.out.println("收到一个TransRecord\n" + transRecord.toString()); + // + // } catch (UnsupportedEncodingException e) { + // e.printStackTrace(); + // } + // + // + // + // + // } catch (Exception e) { + // e.printStackTrace(); + // } } } diff --git a/src/test/java/org/bdware/sc/test/transformerTest.java b/src/test/java/org/bdware/sc/test/transformerTest.java index 2015ea1..e71b99b 100644 --- a/src/test/java/org/bdware/sc/test/transformerTest.java +++ b/src/test/java/org/bdware/sc/test/transformerTest.java @@ -1,18 +1,17 @@ package org.bdware.sc.test; -import com.alibaba.datax.transport.transformer.MD5Transformer; + import com.alibaba.datax.transport.transformer.maskingMethods.irreversibleInterference.*; import org.junit.Test; public class transformerTest { @Test - public void testMD5(){ + public void testMD5() { MD5EncryptionImpl masker = new MD5EncryptionImpl(); - try{ + try { String result = masker.execute("aaa"); System.out.println(result); - } - catch (Exception e){ + } catch (Exception e) { System.out.println(e); } }