support debug run

This commit is contained in:
CaiHQ 2021-11-02 12:58:35 +08:00
parent 32c882ec07
commit bb6c816119
6 changed files with 118 additions and 7 deletions

4
.gitignore vendored
View File

@ -1,2 +1,6 @@
/recoverTestFiles/ /recoverTestFiles/
/build/ /build/
debugconf.json
/ContractDB/
/defaultLog/
/log/

View File

@ -8,7 +8,7 @@ mainClassName = 'org.bdware.sc.ContractProcess'
sourceSets { sourceSets {
main { main {
java { java {
srcDirs 'src/main/java', 'src/main/analysis', 'src/main/data-mask' srcDirs 'src/main/java', 'src/main/debugger', 'src/main/data-mask'
} }
resources { resources {
srcDir 'src/main/resources' srcDir 'src/main/resources'

6
debugconf.json.template Normal file
View File

@ -0,0 +1,6 @@
{
"agentHttpAddr": "127.0.0.1:18000",
"script": "/Users/huaqiancai/BDWare/datanet/datanet-gateway-bundle/datanet-gateway-backend/build/gateway.ypk",
"pubKey": "04d1924329f72ced148f6f333fb985ccbaa31b1e3aacf10be5f43d4a4ff5ad88899a005e79e37fc06993e1d66ada8cf8b711cb36f59538bb7d3e39e70fa9360ddd",
"privKey": "589d94ee5688358a1c5c18430dd9c75097ddddebf769f139da36a807911d20f8"
}

View File

@ -0,0 +1,93 @@
package org.bdware.sc.debugger;
import com.google.gson.JsonObject;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.bdware.sc.ContractProcess;
import org.bdware.sc.YJSPacker;
import org.bdware.sc.bean.Contract;
import org.bdware.sc.bean.ContractExecType;
import org.bdware.sc.conn.ResultCallback;
import org.bdware.sc.get.GetMessage;
import org.bdware.sc.http.HttpUtil;
import org.bdware.sc.util.FileUtil;
import org.bdware.sc.util.JsonUtil;
import java.io.File;
import java.util.Map;
public class DebugMain {
static class DebugConfig {
String script;
String agentHttpAddr;
String pubKey;
String privKey;
//AutoAppend
int port;
String cmi;
String dbPath;
int cPort;
Contract contract;
}
static Logger LOGGER = LogManager.getLogger(DebugMain.class);
public static void main(String[] args) {
String content = FileUtil.getFileContent("./debugconf.json");
DebugConfig config = JsonUtil.fromJson(content, DebugConfig.class);
inject(config);
ContractProcess.main(new String[]{"-port=" + config.port, "-cmi=" + config.cmi, "-disablePID"});
ResultCallback printCallback
= new ResultCallback() {
@Override
public void onResult(String str) {
LOGGER.info(str);
}
};
ContractProcess.instance.handler.setDBInfo(wrap("", config.dbPath), printCallback);
ContractProcess.instance.handler.registerMangerPort(wrap("", Integer.valueOf(config.cPort)), printCallback);
ContractProcess.instance.handler.setContractBundle(wrap("", config.contract), printCallback);
String urlFormat = ("http://%s/SCIDE/SCManager?action=reconnectAll&owner=%s");
String url = String.format(urlFormat, config.agentHttpAddr, config.pubKey);
Map<String, Object> resp = HttpUtil.httpGet(url);
String data = (String)resp.get("response");
LOGGER.info(JsonUtil.toPrettyJson(JsonUtil.parseString(data)));
LOGGER.info("start done!");
}
private static void inject(DebugConfig config) {
String urlFormat = ("http://%s/SCIDE/SCManager?action=%s&arg=%s");
String url = String.format(urlFormat, config.agentHttpAddr, "getAgentConfig", "");
Map<String, Object> resp = HttpUtil.httpGet(url);
String data = (String
) resp.get("response");
JsonObject jsonObject = JsonUtil.parseString(data);
config.cmi = jsonObject.get("cmi").getAsString();
config.dbPath = jsonObject.get("dbPath").getAsString();
config.cPort = jsonObject.get("cPort").getAsInt();
config.port = jsonObject.get("port").getAsInt();
JsonObject ownerAndScript = new JsonObject();
String arg = "abc&owner=" + config.pubKey + "&script=" + config.script;
url = String.format(urlFormat, config.agentHttpAddr, "allocateKeyPair", arg);
resp = HttpUtil.httpGet(url);
LOGGER.info(url);
String contractStr = (String) resp.get("response");
LOGGER.info("[ContratStr] " + contractStr);
Contract contract = JsonUtil.fromJson(contractStr, Contract.class);
config.contract = contract;
contract.setType(ContractExecType.Sole);
}
private static GetMessage wrap(String operation, Object arg) {
return wrap(operation, JsonUtil.toJson(arg));
}
private static GetMessage wrap(String operation, String arg) {
GetMessage msg = new GetMessage();
msg.method = operation;
msg.arg = arg;
return msg;
}
}

View File

@ -47,6 +47,7 @@ import javax.script.ScriptContext;
import javax.script.ScriptEngine; import javax.script.ScriptEngine;
import javax.script.ScriptException; import javax.script.ScriptException;
import java.io.*; import java.io.*;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.*; import java.util.*;
import java.util.zip.ZipFile; import java.util.zip.ZipFile;
@ -60,6 +61,7 @@ public class ContractProcess {
public final String cmi; public final String cmi;
private final Set<String> cachedRequests = new HashSet<>(); private final Set<String> cachedRequests = new HashSet<>();
public final ContractHandler handler;
public ServiceServer server; public ServiceServer server;
public DesktopEngine engine; public DesktopEngine engine;
String dbPath; String dbPath;
@ -81,7 +83,7 @@ public class ContractProcess {
private String pid; private String pid;
public ContractProcess(int port, String cmi) { public ContractProcess(int port, String cmi) {
ContractHandler handler = new ContractHandler(this); handler = new ContractHandler(this);
this.server = new ServiceServer(handler, port); this.server = new ServiceServer(handler, port);
this.cmi = cmi; this.cmi = cmi;
} }
@ -89,6 +91,7 @@ public class ContractProcess {
public static void main(String[] args) { public static void main(String[] args) {
int port = 1616; int port = 1616;
String cmi = ""; String cmi = "";
InputStream pidInput = System.in;
for (String arg : args) { for (String arg : args) {
if (arg.startsWith("-port")) { if (arg.startsWith("-port")) {
String portStr = arg.substring(6); String portStr = arg.substring(6);
@ -99,9 +102,11 @@ public class ContractProcess {
cmi = arg.substring(5); cmi = arg.substring(5);
} else if (arg.startsWith("-debug")) { } else if (arg.startsWith("-debug")) {
Configurator.setRootLevel(Level.DEBUG); Configurator.setRootLevel(Level.DEBUG);
} else if (arg.startsWith("-disablePID")) {
pidInput = new ByteArrayInputStream("CP PID:-1".getBytes(StandardCharsets.UTF_8));
} }
} }
Scanner sc = new Scanner(System.in); Scanner sc = new Scanner(pidInput);
for (String str; sc.hasNextLine(); ) { for (String str; sc.hasNextLine(); ) {
str = sc.nextLine(); str = sc.nextLine();
System.out.println("[CP From STDIN] " + str); System.out.println("[CP From STDIN] " + str);

View File

@ -67,7 +67,7 @@ public class YJSCompiler {
final ErrorManager errors = new ErrorManager(werr); final ErrorManager errors = new ErrorManager(werr);
// Set up options. // Set up options.
final Options options = new Options("nashorn", werr); 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 '#' // detect scripting mode by any source's first character being '#'
options.set("persistent.code.cache", true); options.set("persistent.code.cache", true);
options.set("print.code", "true"); options.set("print.code", "true");
@ -104,9 +104,12 @@ public class YJSCompiler {
public ContractZipBundle compile(ZipFile zf) throws Exception { public ContractZipBundle compile(ZipFile zf) throws Exception {
ContractZipBundle czb = new ContractZipBundle(); ContractZipBundle czb = new ContractZipBundle();
ZipEntry manifest = zf.getEntry("/manifest.json"); ZipEntry manifest = zf.getEntry("/manifest.json");
if (null == manifest) {
manifest = zf.getEntry("manifest.json");
if (null == manifest) { if (null == manifest) {
throw new IllegalStateException("manifest.json is not exists!"); throw new IllegalStateException("manifest.json is not exists!");
} }
}
InputStream manifestInput = zf.getInputStream(manifest); InputStream manifestInput = zf.getInputStream(manifest);
// Gson gson = new GsonBuilder().registerTypeAdapter(Contract.Type.class, // Gson gson = new GsonBuilder().registerTypeAdapter(Contract.Type.class,
// typeAdapter) // typeAdapter)