| @@ -5,14 +5,12 @@ import com.jd.blockchain.contract.engine.ContractCode; | |||
| import com.jd.blockchain.runtime.Module; | |||
| import com.jd.blockchain.transaction.ContractType; | |||
| import com.jd.blockchain.utils.Bytes; | |||
| import com.jd.blockchain.utils.serialize.binary.ContractSerializeUtils; | |||
| import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| import org.springframework.util.ReflectionUtils; | |||
| import org.springframework.util.SerializationUtils; | |||
| import java.lang.reflect.Array; | |||
| import java.lang.reflect.Method; | |||
| import java.lang.reflect.Type; | |||
| /** | |||
| * contract code based jvm | |||
| @@ -50,8 +48,8 @@ public class JavaContractCode implements ContractCode { | |||
| codeModule.execute(new ContractExecution()); | |||
| } | |||
| private Object resolveArgs(byte[] args) { | |||
| return SerializationUtils.deserialize(args); | |||
| private Object resolveArgs(byte[] args, Method method) { | |||
| return ContractSerializeUtils.deserializeMethodParam(args,method); | |||
| } | |||
| class ContractExecution implements Runnable { | |||
| @@ -75,7 +73,7 @@ public class JavaContractCode implements ContractCode { | |||
| // 反序列化参数; | |||
| Method handleMethod = ContractType.resolve(myClass).getHandleMethod(contractEventContext.getEvent()); | |||
| Object args = resolveArgs(contractEventContext.getArgs()); | |||
| Object args = resolveArgs(contractEventContext.getArgs(), handleMethod); | |||
| Object[] params = null; | |||
| if(args.getClass().isArray()){ | |||
| @@ -1,11 +1,11 @@ | |||
| package com.jd.blockchain.transaction; | |||
| import com.jd.blockchain.utils.Bytes; | |||
| import com.jd.blockchain.utils.serialize.binary.ContractSerializeUtils; | |||
| import java.lang.reflect.InvocationHandler; | |||
| import java.lang.reflect.Method; | |||
| import com.jd.blockchain.utils.Bytes; | |||
| import com.jd.blockchain.utils.serialize.binary.BinarySerializeUtils; | |||
| public class ContractInvocationProxy implements InvocationHandler { | |||
| // private String contractMessage; | |||
| @@ -39,15 +39,15 @@ public class ContractInvocationProxy implements InvocationHandler { | |||
| // hashCode 方法; | |||
| } | |||
| // 合约方法; | |||
| byte[] argBytes = serializeArgs(args); | |||
| byte[] argBytes = serializeArgs(args,method); | |||
| sendOpBuilder.send(contractAddress, event, argBytes); | |||
| // TODO: 暂时未考虑有返回值的情况; | |||
| return null; | |||
| } | |||
| private byte[] serializeArgs(Object[] args) { | |||
| private byte[] serializeArgs(Object[] args, Method method) throws Exception { | |||
| // TODO 根据方法参数的定义序列化参数; | |||
| return BinarySerializeUtils.serialize(args); | |||
| return ContractSerializeUtils.serializeMethodParam(args,method); | |||
| } | |||
| } | |||
| @@ -0,0 +1,51 @@ | |||
| package com.jd.blockchain.utils.serialize.binary; | |||
| import com.jd.blockchain.utils.ArrayUtils; | |||
| import com.jd.blockchain.utils.io.BytesUtils; | |||
| import org.springframework.util.SerializationUtils; | |||
| import java.lang.reflect.Method; | |||
| import java.math.BigDecimal; | |||
| /** | |||
| * @author zhaogw | |||
| * date 2019/5/16 18:05 | |||
| */ | |||
| public class ContractSerializeUtils { | |||
| public static final Class[] confirmedType = {Integer.class, Long.class, Double.class, | |||
| int.class,long.class,double.class,String.class, BigDecimal.class}; | |||
| /** | |||
| * valid then parse the Object by Method params; | |||
| * @param object | |||
| * @param method | |||
| * @return | |||
| */ | |||
| public static byte[] serializeMethodParam(Object object,Method method) { | |||
| if (object == null) { | |||
| return BytesUtils.EMPTY_BYTES; | |||
| } | |||
| Class<?>[] classType = method.getParameterTypes(); | |||
| for(Class<?> curClass : classType){ | |||
| if(!ArrayUtils.asList(confirmedType).contains(curClass)){ | |||
| throw new IllegalArgumentException("not support this type="+curClass.toString()); | |||
| } | |||
| } | |||
| return SerializationUtils.serialize(object); | |||
| } | |||
| public static Object deserializeMethodParam(byte[] bytes,Method method) { | |||
| if (bytes == null) { | |||
| return null; | |||
| } | |||
| Class<?>[] classType = method.getParameterTypes(); | |||
| for(Class<?> curClass : classType){ | |||
| if(!ArrayUtils.asList(confirmedType).contains(curClass)){ | |||
| throw new IllegalArgumentException("not support this type="+curClass.toString()); | |||
| } | |||
| } | |||
| return SerializationUtils.deserialize(bytes); | |||
| } | |||
| } | |||