upgrade annotation type

upgrade bdledger sdk
This commit is contained in:
CaiHQ 2022-11-15 11:37:08 +08:00
parent 9317385bd9
commit 07174bb3b4
14 changed files with 91 additions and 85 deletions

View File

@ -6,7 +6,7 @@ plugins {
}
group = "org.bdware.sc"
version = "1.6.91"
version = "1.7.1"
repositories {
mavenCentral()
mavenLocal()
@ -61,8 +61,8 @@ jar {
// uncomment this when publish,
//while develop at local use "false"
configurations.runtimeClasspath.filter {
// it.getAbsolutePath().contains("/lib/")
false
it.getAbsolutePath().contains("/lib/")
// false
}.collect {
it.isDirectory() ? it : zipTree(it)
}

Binary file not shown.

View File

@ -510,12 +510,14 @@ public class ContractProcess {
engine = new DesktopEngine(zipBundle.getManifest(), zipPath, contract);
engine.loadJar(zf);
engine.registerResource(new Resources(zf, engine.getClassLoad()));
String retStr =
JsonUtil.toJson(
engine.loadContract(contract, cn, cn.getInstrumentBranch()));
invokeOnCreate(contract.getCreateParam());
LOGGER.debug("result: " + retStr);
return retStr;
ContractResult result = engine.loadContract(contract, cn, cn.getInstrumentBranch());
JsonObject jo = new JsonObject();
ContractResult onCreate = invokeOnCreate(contract.getCreateParam());
jo.add("onCreate", JsonUtil.parseObject(onCreate));
jo.add("loadContract", JsonUtil.parseObject(result));
jo.addProperty("status", result.status.merge(onCreate.status).toString());
LOGGER.debug("result: " + jo.toString());
return jo.toString();
} else {
contract.setScript(FileUtil.getFileContent(zipPath));
return setContract(contract);
@ -633,10 +635,15 @@ public class ContractProcess {
handleLog();
LOGGER.info("load script, contract:" + JsonUtil.toJson(contract.getScriptStr()));
LOGGER.info("load cn:" + JsonUtil.toJson(cn));
String ret =
JsonUtil.toJson(engine.loadContract(contract, cn, cn.getInstrumentBranch()));
invokeOnCreate(contract.getCreateParam());
return ret;
ContractResult ret =
engine.loadContract(contract, cn, cn.getInstrumentBranch());
ContractResult onCreate = invokeOnCreate(contract.getCreateParam());
JsonObject jo = new JsonObject();
jo.add("onCreate", JsonUtil.parseObject(onCreate));
jo.add("loadContract", JsonUtil.parseObject(ret));
jo.addProperty("status", ret.status.merge(onCreate.status).toString());
LOGGER.debug("result: " + jo.toString());
return jo.toString();
} catch (Exception e) {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
e.printStackTrace(new PrintStream(bo));
@ -688,7 +695,7 @@ public class ContractProcess {
return r.totalMemory() - r.freeMemory();
}
private void invokeOnCreate(JsonElement arg) {
private ContractResult invokeOnCreate(JsonElement arg) {
long start = System.currentTimeMillis();
logIndex = new TimeSerialIndex("./ContractDB/" + cn.getContractName() + ".index");
LOGGER.debug("timeSerialIndex: " + (System.currentTimeMillis() - start));
@ -728,7 +735,7 @@ public class ContractProcess {
onCreate.setRequesterDOI("empty");
}
FunctionNode funNode = cn.getFunction("onCreate");
invoke(start, onCreate, funNode);
return invoke(onCreate, funNode);
}
public void resetContractName(String name) {
@ -736,8 +743,7 @@ public class ContractProcess {
cn.resetContractName(name);
}
private void invokeOnRecover(JsonElement arg) {
long start = System.currentTimeMillis();
private ContractResult invokeOnRecover(JsonElement arg) {
ContractRequest onRecover = new ContractRequest();
onRecover.setAction("onRecover");
if (arg == null)
@ -753,26 +759,18 @@ public class ContractProcess {
onRecover.setRequesterDOI("empty");
}
FunctionNode funNode = cn.getFunction("onRecover");
invoke(start, onRecover, funNode);
return invoke(onRecover, funNode);
}
private void invoke(long start, ContractRequest onRecover, FunctionNode funNode) {
// start = System.currentTimeMillis();
private ContractResult invoke(ContractRequest onRecover, FunctionNode funNode) {
if (funNode != null) {
LOGGER.debug(
"getFunction:" + (System.currentTimeMillis() - start) + " " + funNode.functionName);
long start1 = System.currentTimeMillis();
funNode.setIsExport(true);
ContractResult result = engine.executeContract(onRecover);
LOGGER.info("invoke onCreate, result:" + new Gson().toJson(result));
LOGGER.debug("executeOnCreate:" + (System.currentTimeMillis() - start1));
start1 = System.currentTimeMillis();
funNode.setIsExport(false);
engine.getTracePS().clean();
LOGGER.debug("clearTrace:" + (System.currentTimeMillis() - start1));
// start1 = System.currentTimeMillis();
return result;
}
return new ContractResult(Status.Success, new JsonPrimitive("no funNode found"));
}
// public String executeBundle(ContractZipBundle czb, String arg) {

View File

@ -429,7 +429,7 @@ public class DesktopEngine extends JSEngine {
if (methodHandle.getClass() != Class.forName("java.lang.invoke.DirectMethodHandle")) {
Field argL0 = methodHandle.getClass().getDeclaredField("argL0");
argL0.setAccessible(true);
methodHandle = (MethodHandle) argL0.get(methodHandle);
methodHandle = (MethodHandle) argL0.get(methodHandle);
}
Field memberNameField = methodHandle.getClass().getDeclaredField("member");
@ -611,7 +611,7 @@ public class DesktopEngine extends JSEngine {
// long start = System.currentTimeMillis();
for (AnnotationHook handler : fun.beforeExecutionAnnotations()) {
ret = handler.handle(input, this, ret);
ret = handler.handle(this, new ArgPacks(input, input.getArg(), ret));
}
// actually invoke!
if (ret == null) {
@ -628,7 +628,7 @@ public class DesktopEngine extends JSEngine {
//Mask在after裏面
//System.out.println("afterHook"+contract.Mask);
ret = handler.handle(input, this, ret);
ret = handler.handle(this, new ArgPacks(input, input.getArg(), ret));
}
return ret;
}

View File

@ -10,6 +10,7 @@ import org.bdware.sc.bean.ContractRequest;
import org.bdware.sc.boundry.ScriptReturnException;
import org.bdware.sc.node.AnnotationHook;
import org.bdware.sc.node.AnnotationNode;
import org.bdware.sc.node.ArgPacks;
import org.bdware.sc.node.FunctionNode;
@YJSAnnotation(name = "ArgSchema")
@ -33,7 +34,8 @@ public class ArgSchemaHandler implements AnnotationHook {
}
@Override
public Object handle(ContractRequest input, JSEngine Engine, Object ret) throws ScriptReturnException {
public ArgPacks handle(JSEngine Engine, ArgPacks argPacks) throws ScriptReturnException {
ContractRequest input = argPacks.request;
JsonElement je = input.getArg();
ArgSchemaVisitor visitor;
if (je.isJsonObject())
@ -44,6 +46,7 @@ public class ArgSchemaHandler implements AnnotationHook {
visitor = new ArgSchemaVisitor(obj);
//IMPORTANT automatically convert arg type here
input.setArg(obj);
argPacks.arg = obj;
} catch (Exception e) {
e.printStackTrace();
JsonObject jo = new JsonObject();
@ -65,6 +68,6 @@ public class ArgSchemaHandler implements AnnotationHook {
jo.addProperty("code", visitor.errorCode);
throw new ScriptReturnException(jo);
}
return ret;
return argPacks;
}
}

View File

@ -31,7 +31,6 @@ public class ArgSchemaVisitor extends JsonVisitor {
if (jo.has(key.substring(1))) {
ArgSchemaVisitor visitor = new ArgSchemaVisitor(jo.get(key.substring(1)));
visitor.visit(schema.get(key));
jo.add(key, visitor.get());
if (!visitor.status) {
errorCode += visitor.errorCode;
message += visitor.message;
@ -46,13 +45,11 @@ public class ArgSchemaVisitor extends JsonVisitor {
if (jo.has(key)) {
ArgSchemaVisitor visitor = new ArgSchemaVisitor(jo.get(key));
visitor.visit(schema.get(key));
jo.add(key, visitor.get());
if (!visitor.status) {
message += visitor.message;
errorCode += visitor.errorCode;
}
status &= visitor.status;
}
}
}
@ -68,11 +65,11 @@ public class ArgSchemaVisitor extends JsonVisitor {
public JsonVisitor visitJsonArray(JsonArray ele) {
if (root.isJsonArray()) {
JsonArray array = root.getAsJsonArray();
//message = new JsonArray();
for (int i = 0; i < array.size(); i++) {
ArgSchemaVisitor visitor = new ArgSchemaVisitor(array.get(i));
visitor.visit(ele.get(0));
message += visitor.message;
status &= visitor.status;
}
}
return this;
@ -95,9 +92,6 @@ public class ArgSchemaVisitor extends JsonVisitor {
if (root.isJsonPrimitive() && root.getAsJsonPrimitive().isString()) {
return this;
} else {
// message=new JsonObject();
// message.getAsJsonObject().addProperty("msg","xxx");
// message.getAsJsonObject().addProperty("code","1000");
message = "[Type error] The value (" + root.getAsJsonPrimitive().getAsString() + ") should be string";
status = false;
errorCode = 1001;
@ -145,12 +139,6 @@ public class ArgSchemaVisitor extends JsonVisitor {
System.out.println(e);
}
}
//String result = masker.execute(primitive.toString());
//System.out.println(result);
//root = new JsonPrimitive(root.getAsString().substring(0, 2));
//https://github.com/guohf/DataX-Masking
return this;
}

View File

@ -5,6 +5,7 @@ import org.bdware.sc.bean.ContractRequest;
import org.bdware.sc.engine.ConfidentialContractUtil;
import org.bdware.sc.engine.DesktopEngine;
import org.bdware.sc.node.AnnotationHook;
import org.bdware.sc.node.ArgPacks;
import org.bdware.sc.node.FunctionNode;
import wrp.jdk.nashorn.api.scripting.ScriptObjectMirror;
@ -16,19 +17,20 @@ public class ConfidentialHandler implements AnnotationHook {
}
@Override
public Object handle(ContractRequest input, JSEngine engine, Object ret) {
public ArgPacks handle(JSEngine engine, ArgPacks argPacks) {
try {
ContractRequest input = argPacks.request;
DesktopEngine desktopEngine = (DesktopEngine) engine;
ConfidentialContractUtil.copyTemplateToDestination(input);
ScriptObjectMirror globalVars = (ScriptObjectMirror) desktopEngine.get("Global");
ConfidentialContractUtil.dumpScriptAndStates(
desktopEngine.engine, fun, input, globalVars);
// run in SGX instead of Nashorn if function has @Confidential annotation
ret = ConfidentialContractUtil.executeConfidentialContract(input);
return ret;
argPacks.ret = ConfidentialContractUtil.executeConfidentialContract(input);
return argPacks;
} catch (Exception e) {
e.printStackTrace();
}
return ret;
return argPacks;
}
}

View File

@ -6,9 +6,9 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bdware.sc.ContractResult;
import org.bdware.sc.JSEngine;
import org.bdware.sc.bean.ContractRequest;
import org.bdware.sc.boundry.JavaScriptEntry;
import org.bdware.sc.node.AnnotationHook;
import org.bdware.sc.node.ArgPacks;
import org.bdware.sc.node.FunctionNode;
import org.bdware.sc.util.JsonUtil;
import org.paillier.PaillierKeyPair;
@ -25,7 +25,7 @@ public class HomomorphicDecryptHandler implements AnnotationHook {
}
@Override
public Object handle(ContractRequest input, JSEngine engine, Object ret) {
public ArgPacks handle(JSEngine engine, ArgPacks argPacks) {
try {
// GetHomArgs args =
// new GetHomArgs(
@ -45,18 +45,18 @@ public class HomomorphicDecryptHandler implements AnnotationHook {
RSAPrivateKey privkey = (RSAPrivateKey) PaillierKeyPair.pemToPrivateKey(privKeyStr);
LOGGER.info("HomomorphicEncryptHandler--------------------------------3: " + privkey);
HomoVisitor.privateKey = privkey;
ret = getDecryptResult(homoDecryptConf, JsonUtil.parseObject(ret));
if (null != ret) {
return ret;
argPacks.ret = getDecryptResult(homoDecryptConf, JsonUtil.parseObject(argPacks.ret));
if (null != argPacks.ret) {
return argPacks;
}
ret = new JsonObject();
argPacks.ret = new JsonObject();
}
return ret;
return argPacks;
// return ret;
} catch (Exception e) {
e.printStackTrace();
}
return ret;
return argPacks;
}
private Object getDecryptResult(JsonElement homoDecryptConf, JsonElement data) {

View File

@ -9,6 +9,7 @@ import org.bdware.sc.JSEngine;
import org.bdware.sc.bean.ContractRequest;
import org.bdware.sc.boundry.JavaScriptEntry;
import org.bdware.sc.node.AnnotationHook;
import org.bdware.sc.node.ArgPacks;
import org.bdware.sc.node.FunctionNode;
import org.bdware.sc.util.JsonUtil;
import org.paillier.PaillierKeyPair;
@ -25,15 +26,16 @@ public class HomomorphicEncryptHandler implements AnnotationHook {
}
@Override
public Object handle(ContractRequest input, JSEngine engine, Object ret) {
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());
JsonElement response = (JsonElement) ret;
JsonElement response = (JsonElement) argPacks.ret;
JsonElement homoEncryptConf = this.fun.getHomoEncryptConf();
if (homoEncryptConf != null && !homoEncryptConf.isJsonNull()) {
String res =
@ -54,20 +56,20 @@ public class HomomorphicEncryptHandler implements AnnotationHook {
// if (homoEncryptConf.getAsJsonPrimitive().isString())
// homoEncryptConf = JsonParser.parseString(homoEncryptConf.getAsString());
LOGGER.info("HomomorphicEncryptHandler--------------------------------6: " + homoEncryptConf);
LOGGER.info("HomomorphicEncryptHandler--------------------------------7: " + ret);
LOGGER.info("HomomorphicEncryptHandler--------------------------------8: " + ret.toString());
LOGGER.info("HomomorphicEncryptHandler--------------------------------7: " + argPacks.ret);
LOGGER.info("HomomorphicEncryptHandler--------------------------------8: " + argPacks.ret.toString());
// LOGGER.info("HomomorphicEncryptHandler--------------------------------9: " + JsonUtil.toJson(ret));
ret = getEncryptResult(homoEncryptConf, response);
if (ret != null) {
return ret;
argPacks.ret = getEncryptResult(homoEncryptConf, response);
if (argPacks.ret != null) {
return argPacks;
}
ret = new JsonObject();
argPacks.ret = new JsonObject();
}
return ret;
return argPacks;
} catch (Exception e) {
e.printStackTrace();
}
return ret;
return argPacks;
}
private Object getEncryptResult(JsonElement homoEncryptConf, JsonElement data) {

View File

@ -9,13 +9,16 @@ import org.bdware.sc.bean.ContractRequest;
import org.bdware.sc.bean.ProjectConfig;
import org.bdware.sc.engine.DesktopEngine;
import org.bdware.sc.node.AnnotationHook;
import org.bdware.sc.node.ArgPacks;
public class MaskHandler implements AnnotationHook {
private static final Logger LOGGER = LogManager.getLogger(MaskHandler.class);
@Override
public Object handle(ContractRequest input, JSEngine Engine, Object ret) {
public ArgPacks handle(JSEngine Engine, ArgPacks argPacks) {
try {
ContractRequest input = argPacks.request;
Object ret = argPacks.ret;
DesktopEngine desktopEngine = (DesktopEngine) Engine;
ProjectConfig projectConfig = desktopEngine.getProjectConfig();
JsonElement maskConf = projectConfig.getMask(input.getAction());
@ -35,14 +38,17 @@ public class MaskHandler implements AnnotationHook {
ret = visitor.get();
LOGGER.debug(maskConf);
if (null != ret) {
return ret;
argPacks.ret = ret;
return argPacks;
}
ret = JsonParser.parseString("");
}
return ret;
argPacks.ret = ret;
return argPacks;
} catch (Exception e) {
// e.printStackTrace();
return ret;
return argPacks;
}
}

View File

@ -6,11 +6,13 @@ import org.bdware.sc.bean.ContractRequest;
import org.bdware.sc.bean.ProjectConfig;
import org.bdware.sc.engine.DesktopEngine;
import org.bdware.sc.node.AnnotationHook;
import org.bdware.sc.node.ArgPacks;
public class MockTemplateHandler implements AnnotationHook {
@Override
public Object handle(ContractRequest request, JSEngine engine, Object ret) {
public ArgPacks handle(JSEngine engine, ArgPacks argPacks) {
try {
ContractRequest request = argPacks.request;
if (request.fromDebug()) {
System.out.println(request.getAction());
DesktopEngine desktopEngine = (DesktopEngine) engine;
@ -19,13 +21,14 @@ public class MockTemplateHandler implements AnnotationHook {
if (template != null && template.length() > 0) {
System.out.println(template);
MockUtil Mock = new MockUtil();
return Mock.mock(template).toString();
} else return ret; //When mock config is null defined just ignore.
argPacks.ret = Mock.mock(template).toString();
return argPacks;
} else return argPacks; //When mock config is null defined just ignore.
}
} catch (Exception e) {
e.printStackTrace();
} finally {
return ret;
return argPacks;
}
}
}

View File

@ -1,13 +1,14 @@
package org.bdware.sc.engine.hook;
import org.bdware.sc.JSEngine;
import org.bdware.sc.bean.ContractRequest;
import org.bdware.sc.engine.JSONTool;
import org.bdware.sc.node.AnnotationHook;
import org.bdware.sc.node.ArgPacks;
public class ObjToJsonHandler implements AnnotationHook {
@Override
public Object handle(ContractRequest input, JSEngine desktopEngine, Object ret) {
return JSONTool.convertMirrorToJson(ret);
public ArgPacks handle(JSEngine desktopEngine, ArgPacks argPacks) {
argPacks.ret = JSONTool.convertMirrorToJson(argPacks.ret);
return argPacks;
}
}

View File

@ -4,6 +4,7 @@ import org.bdware.sc.JSEngine;
import org.bdware.sc.bean.ContractRequest;
import org.bdware.sc.engine.DesktopEngine;
import org.bdware.sc.node.AnnotationHook;
import org.bdware.sc.node.ArgPacks;
import org.commonmark.node.FencedCodeBlock;
import org.commonmark.node.Heading;
import org.commonmark.node.Node;
@ -56,12 +57,14 @@ public class ReadMeHandler implements AnnotationHook {
}
@Override
public Object handle(ContractRequest input, JSEngine engine, Object ret) {
public ArgPacks handle(JSEngine engine, ArgPacks argPacks) {
DesktopEngine desktopEngine = (DesktopEngine) engine;
if (input.fromDebug() && (ret == null || ret.equals("emptyMock"))) {
ret = getReadMeData(desktopEngine, input);
System.out.println(ret);
ContractRequest input = argPacks.request;
if (input.fromDebug() && (argPacks.ret == null || argPacks.ret.equals("emptyMock"))) {
argPacks.ret = getReadMeData(desktopEngine, input);
System.out.println(argPacks.ret);
}
return ret;
return argPacks;
}
}