From fa729d80a0b3179fdc267a08b611ab5839d4ce49 Mon Sep 17 00:00:00 2001 From: CaiHQ Date: Thu, 7 Apr 2022 15:48:39 +0800 Subject: [PATCH] feat: support onCreateParams --- .../java/org/bdware/sc/ContractProcess.java | 41 +++++++++++-------- .../java/org/bdware/sc/ByteArrayTest.java | 32 +++++++++++++++ src/test/java/org/bdware/sc/SM2Test.java | 21 ++++++++++ 3 files changed, 76 insertions(+), 18 deletions(-) create mode 100644 src/test/java/org/bdware/sc/ByteArrayTest.java create mode 100644 src/test/java/org/bdware/sc/SM2Test.java diff --git a/src/main/java/org/bdware/sc/ContractProcess.java b/src/main/java/org/bdware/sc/ContractProcess.java index bfe3d4d..3a061b6 100644 --- a/src/main/java/org/bdware/sc/ContractProcess.java +++ b/src/main/java/org/bdware/sc/ContractProcess.java @@ -1,6 +1,6 @@ package org.bdware.sc; -import com.google.gson.Gson; +import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonPrimitive; import com.google.gson.JsonSyntaxException; @@ -520,7 +520,7 @@ public class ContractProcess { String retStr = JsonUtil.toJson( engine.loadContract(contract, cn, cn.getInstrumentBranch())); - invokeOnCreate(); + invokeOnCreate(contract.getCreateParam()); LOGGER.debug("result: " + retStr); return retStr; } else { @@ -559,8 +559,8 @@ public class ContractProcess { fun.appendBeforeInvokeHandler(new ConfidentialHandler(fun)); } - ArgSchemaHandler argSchemaHandler = createHandlerIfExist(fun,fun.annotations,ArgSchemaHandler.class); - if(argSchemaHandler!=null){ + ArgSchemaHandler argSchemaHandler = createHandlerIfExist(fun, fun.annotations, ArgSchemaHandler.class); + if (argSchemaHandler != null) { fun.appendBeforeInvokeHandler(argSchemaHandler); } @@ -589,15 +589,15 @@ public class ContractProcess { } } - T createHandlerIfExist(FunctionNode function,List annotations, Class clz) { + T createHandlerIfExist(FunctionNode function, List annotations, Class clz) { YJSAnnotation annotation = clz.getAnnotation(YJSAnnotation.class); - if (annotation==null) return null; - if (annotations==null) return null; - for (AnnotationNode node: annotations){ - if (annotation.name().equals(node.getType())){ + if (annotation == null) return null; + if (annotations == null) return null; + for (AnnotationNode node : annotations) { + if (annotation.name().equals(node.getType())) { try { - Method m = clz.getDeclaredMethod("fromAnnotationNode",FunctionNode.class,AnnotationNode.class); - T result = (T) m.invoke(null,function, node); + Method m = clz.getDeclaredMethod("fromAnnotationNode", FunctionNode.class, AnnotationNode.class); + T result = (T) m.invoke(null, function, node); return result; } catch (Exception e) { e.printStackTrace(); @@ -640,8 +640,7 @@ public class ContractProcess { handleLog(); String ret = JsonUtil.toJson(engine.loadContract(contract, cn, cn.getInstrumentBranch())); - invokeOnCreate(); - + invokeOnCreate(contract.getCreateParam()); return ret; } catch (Exception e) { ByteArrayOutputStream bo = new ByteArrayOutputStream(); @@ -694,7 +693,7 @@ public class ContractProcess { return r.totalMemory() - r.freeMemory(); } - private void invokeOnCreate() { + private void invokeOnCreate(JsonElement arg) { long start = System.currentTimeMillis(); logIndex = new TimeSerialIndex("./ContractDB/" + cn.getContractName() + ".index"); LOGGER.debug("timeSerialIndex: " + (System.currentTimeMillis() - start)); @@ -719,7 +718,10 @@ public class ContractProcess { JavaScriptEntry.isDebug = contract.isDebug(); ContractRequest onCreate = new ContractRequest(); onCreate.setAction("onCreate"); - onCreate.setArg("null"); + if (arg == null) + onCreate.setArg("null"); + else + onCreate.setArg(arg); onCreate.setRequester(contract.getOwner()); if (contract.getDoipFlag() && null != contract.getDOI() && !contract.getDOI().isEmpty()) { onCreate.setRequesterDOI(contract.getDOI()); @@ -730,11 +732,14 @@ public class ContractProcess { invoke(start, onCreate, funNode); } - private void invokeOnRecover() { + private void invokeOnRecover(JsonElement arg) { long start = System.currentTimeMillis(); ContractRequest onRecover = new ContractRequest(); onRecover.setAction("onRecover"); - onRecover.setArg("null"); + if (arg == null) + onRecover.setArg("null"); + else + onRecover.setArg(arg); onRecover.setRequester(contract.getOwner()); if (contract.getDoipFlag() && (contract.getDOI() != null) @@ -1062,7 +1067,7 @@ public class ContractProcess { public String loadMemoryDump(String path) { String str = engine.syncUtil.loadMemoryDump(path, contract.getStateful()); - invokeOnRecover(); + invokeOnRecover(contract.getCreateParam()); return str; } diff --git a/src/test/java/org/bdware/sc/ByteArrayTest.java b/src/test/java/org/bdware/sc/ByteArrayTest.java new file mode 100644 index 0000000..878417d --- /dev/null +++ b/src/test/java/org/bdware/sc/ByteArrayTest.java @@ -0,0 +1,32 @@ +package org.bdware.sc; + +import org.bouncycastle.pqc.math.linearalgebra.ByteUtils; +import org.junit.Test; + +import java.nio.charset.StandardCharsets; +import java.util.Arrays; + +public class ByteArrayTest { + @Test + public void abc() { + String str = "abc6787689dd"; + byte[] data = str.getBytes(StandardCharsets.UTF_8); + String hexStr = ByteUtils.toHexString(data); + System.out.println("hashCode:" + Arrays.hashCode(data)); + System.out.println("hexStr:" + hexStr); + //hashCode:-606029994 + //hexStr:616263363738373638396464 + } + + @Test + public void abc2() { + String str = "abc678768229dd"; + byte[] data = str.getBytes(StandardCharsets.UTF_8); + String hexStr = ByteUtils.toHexString(data); + System.out.println("hashCode:" + Arrays.hashCode(data)); + System.out.println("hexStr:" + hexStr); + //hashCode:1712735702 + //hexStr:6162633637383736383232396464 + // + } +} diff --git a/src/test/java/org/bdware/sc/SM2Test.java b/src/test/java/org/bdware/sc/SM2Test.java new file mode 100644 index 0000000..f9eeee1 --- /dev/null +++ b/src/test/java/org/bdware/sc/SM2Test.java @@ -0,0 +1,21 @@ +package org.bdware.sc; + +import org.junit.Test; +import org.zz.gmhelper.SM2KeyPair; + +import java.math.BigInteger; + +public class SM2Test { + @Test + 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)); + System.out.println("hello"); + } +}