feat: support onCreateParams

This commit is contained in:
CaiHQ 2022-04-07 15:48:39 +08:00
parent 4b96899653
commit fa729d80a0
3 changed files with 76 additions and 18 deletions

View File

@ -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 extends AnnotationHook> T createHandlerIfExist(FunctionNode function,List<AnnotationNode> annotations, Class<T> clz) {
<T extends AnnotationHook> T createHandlerIfExist(FunctionNode function, List<AnnotationNode> annotations, Class<T> 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;
}

View File

@ -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
//
}
}

View File

@ -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");
}
}