From d70490b5fb2079c4e176cc7274965db1ea517276 Mon Sep 17 00:00:00 2001 From: "Frank.R.Wu" Date: Sun, 31 Oct 2021 23:10:43 +0800 Subject: [PATCH] feat(cp): update event mechanism support local event in contract process --- build.gradle | 2 +- .../bdware/sc/boundry/JavaScriptEntry.java | 7 +-- .../bdware/sc/boundry/utils/RocksDBUtil.java | 4 +- .../org/bdware/sc/engine/DesktopEngine.java | 60 +++++++++++-------- 4 files changed, 40 insertions(+), 33 deletions(-) diff --git a/build.gradle b/build.gradle index 726c2ae..190d9fb 100644 --- a/build.gradle +++ b/build.gradle @@ -26,7 +26,7 @@ sourceSets { dependencies { implementation project(":common") - implementation project(":MockJava") + implementation project(":mockjava") implementation 'com.atlassian.commonmark:commonmark:0.17.0' implementation 'com.idealista:format-preserving-encryption:1.0.0' diff --git a/src/main/java/org/bdware/sc/boundry/JavaScriptEntry.java b/src/main/java/org/bdware/sc/boundry/JavaScriptEntry.java index d846c83..37e67cf 100644 --- a/src/main/java/org/bdware/sc/boundry/JavaScriptEntry.java +++ b/src/main/java/org/bdware/sc/boundry/JavaScriptEntry.java @@ -637,12 +637,7 @@ public class JavaScriptEntry { topic_handlers.remove(topic); ContractProcess.instance.unSubscribe(handler); } - REvent msg = - new REvent( - topic, - UNSUBSCRIBE, - content, - reqID); + REvent msg = new REvent(topic, UNSUBSCRIBE, content, reqID); msgList.add(msg); } 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 ac0bad1..48b1159 100644 --- a/src/main/java/org/bdware/sc/boundry/utils/RocksDBUtil.java +++ b/src/main/java/org/bdware/sc/boundry/utils/RocksDBUtil.java @@ -31,7 +31,9 @@ public class RocksDBUtil { lockFile.delete(); if (readOnly) { rocksDB = RocksDB.openReadOnly(options, path); - } else rocksDB = RocksDB.open(options, path); + } else { + rocksDB = RocksDB.open(options, path); + } } catch (RocksDBException e) { e.printStackTrace(); } diff --git a/src/main/java/org/bdware/sc/engine/DesktopEngine.java b/src/main/java/org/bdware/sc/engine/DesktopEngine.java index 4a6306c..be7c44e 100644 --- a/src/main/java/org/bdware/sc/engine/DesktopEngine.java +++ b/src/main/java/org/bdware/sc/engine/DesktopEngine.java @@ -27,6 +27,7 @@ import org.bdware.sc.event.REvent.REventSemantics; import org.bdware.sc.node.*; import org.bdware.sc.syncMech.SyncType; import org.bdware.sc.trace.ProgramPointCounter; +import org.bdware.sc.util.HashUtil; import org.bdware.sc.util.JsonUtil; import org.objectweb.asm.ClassReader; import org.objectweb.asm.tree.MethodNode; @@ -259,31 +260,13 @@ public class DesktopEngine extends JSEngine { e.printStackTrace(); return new ContractResult(Status.Error, new JsonPrimitive(bo.toString())); } - LOGGER.debug(JsonUtil.toJson(contractNode.events)); - for (String event : contractNode.events.keySet()) { - try { - 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(); - } + 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)); + } + 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()) { try { @@ -302,6 +285,33 @@ public class DesktopEngine extends JSEngine { 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 // *