feat(cp): update event mechanism

support local event in contract process
This commit is contained in:
Frank.R.Wu 2021-10-31 23:10:43 +08:00
parent a7be50024e
commit d70490b5fb
4 changed files with 40 additions and 33 deletions

View File

@ -26,7 +26,7 @@ sourceSets {
dependencies { dependencies {
implementation project(":common") implementation project(":common")
implementation project(":MockJava") implementation project(":mockjava")
implementation 'com.atlassian.commonmark:commonmark:0.17.0' implementation 'com.atlassian.commonmark:commonmark:0.17.0'
implementation 'com.idealista:format-preserving-encryption:1.0.0' implementation 'com.idealista:format-preserving-encryption:1.0.0'

View File

@ -637,12 +637,7 @@ public class JavaScriptEntry {
topic_handlers.remove(topic); topic_handlers.remove(topic);
ContractProcess.instance.unSubscribe(handler); ContractProcess.instance.unSubscribe(handler);
} }
REvent msg = REvent msg = new REvent(topic, UNSUBSCRIBE, content, reqID);
new REvent(
topic,
UNSUBSCRIBE,
content,
reqID);
msgList.add(msg); msgList.add(msg);
} }

View File

@ -31,7 +31,9 @@ public class RocksDBUtil {
lockFile.delete(); lockFile.delete();
if (readOnly) { if (readOnly) {
rocksDB = RocksDB.openReadOnly(options, path); rocksDB = RocksDB.openReadOnly(options, path);
} else rocksDB = RocksDB.open(options, path); } else {
rocksDB = RocksDB.open(options, path);
}
} catch (RocksDBException e) { } catch (RocksDBException e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -27,6 +27,7 @@ import org.bdware.sc.event.REvent.REventSemantics;
import org.bdware.sc.node.*; import org.bdware.sc.node.*;
import org.bdware.sc.syncMech.SyncType; import org.bdware.sc.syncMech.SyncType;
import org.bdware.sc.trace.ProgramPointCounter; import org.bdware.sc.trace.ProgramPointCounter;
import org.bdware.sc.util.HashUtil;
import org.bdware.sc.util.JsonUtil; import org.bdware.sc.util.JsonUtil;
import org.objectweb.asm.ClassReader; import org.objectweb.asm.ClassReader;
import org.objectweb.asm.tree.MethodNode; import org.objectweb.asm.tree.MethodNode;
@ -259,31 +260,13 @@ public class DesktopEngine extends JSEngine {
e.printStackTrace(); e.printStackTrace();
return new ContractResult(Status.Error, new JsonPrimitive(bo.toString())); return new ContractResult(Status.Error, new JsonPrimitive(bo.toString()));
} }
LOGGER.debug(JsonUtil.toJson(contractNode.events)); LOGGER.debug(JsonUtil.toJson(contractNode.events) + "\n\t" + JsonUtil.toJson(contractNode.logs));
for (String event : contractNode.events.keySet()) { for (String topic : contractNode.events.keySet()) {
try { compileEventFunction(topic, topic, contractNode.events.get(topic));
String str;
REventSemantics semantics = contractNode.events.get(event);
if (REventSemantics.AT_LEAST_ONCE.equals(semantics)) {
str =
String.format(
"function %s(arg) { YancloudUtil.pubEvent(\"%s\", arg); }",
event, event);
} else {
str =
String.format(
"function %s(arg) { YancloudUtil.pubEventConstraint(\"%s\", arg, \"%s\"); }",
event, event, semantics.name());
}
compileFunction(str, false);
str =
String.format(
"function %ss(arg0, arg1) { YancloudUtil.pubEventConstraint(\"%s\", arg0, arg1); }",
event, event);
compileFunction(str, false);
} catch (ScriptException e) {
e.printStackTrace();
} }
for (String log : contractNode.logs.keySet()) {
String topic = HashUtil.sha3(contract.getID() + log);
compileEventFunction(log, topic, contractNode.logs.get(log));
} }
for (ClassNode cn : contractNode.getClzs()) { for (ClassNode cn : contractNode.getClzs()) {
try { try {
@ -302,6 +285,33 @@ public class DesktopEngine extends JSEngine {
return cResult; return cResult;
} }
private void compileEventFunction(String name, String topic, REventSemantics semantics) {
try {
String str;
if (REventSemantics.AT_LEAST_ONCE.equals(semantics)) {
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());
}
compileFunction(str, false);
LOGGER.debug("compile function " + name + " success!");
str =
String.format(
"function %ss(arg0, arg1) { YancloudUtil.pubEventConstraint(\"%s\", arg0, arg1); }",
name, topic);
compileFunction(str, false);
LOGGER.debug("compile function " + name + "s success!");
} catch (ScriptException e) {
e.printStackTrace();
}
}
// /** // /**
// * Load a contract into contract engine // * Load a contract into contract engine
// * // *