| @@ -1,21 +1,16 @@ | |||
| package com.jd.blockchain.contract.jvm; | |||
| import java.lang.annotation.Annotation; | |||
| import java.lang.reflect.Method; | |||
| import java.util.HashMap; | |||
| import java.util.Map; | |||
| import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| import org.springframework.util.ReflectionUtils; | |||
| import com.jd.blockchain.contract.ContractEvent; | |||
| import com.jd.blockchain.contract.ContractEventContext; | |||
| import com.jd.blockchain.contract.engine.ContractCode; | |||
| import com.jd.blockchain.runtime.Module; | |||
| import com.jd.blockchain.transaction.ContractType; | |||
| import com.jd.blockchain.utils.BaseConstant; | |||
| import com.jd.blockchain.utils.Bytes; | |||
| import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| import org.springframework.util.ReflectionUtils; | |||
| import org.springframework.util.SerializationUtils; | |||
| import java.lang.reflect.Method; | |||
| /** | |||
| * contract code based jvm | |||
| @@ -29,7 +24,7 @@ public class JavaContractCode implements ContractCode { | |||
| private long version; | |||
| private ContractEventContext contractEventContext; | |||
| private ContractType contractType; | |||
| private ContractType contractType ; | |||
| public JavaContractCode(Bytes address, long version, Module codeModule) { | |||
| this.address = address; | |||
| @@ -53,9 +48,8 @@ public class JavaContractCode implements ContractCode { | |||
| codeModule.execute(new ContractExecution()); | |||
| } | |||
| private Object[] resolveArgs(byte[] args) { | |||
| // TODO Auto-generated method stub | |||
| return null; | |||
| private Object resolveArgs(byte[] args) { | |||
| return SerializationUtils.deserialize(args); | |||
| } | |||
| class ContractExecution implements Runnable { | |||
| @@ -78,8 +72,8 @@ public class JavaContractCode implements ContractCode { | |||
| startTime = System.currentTimeMillis(); | |||
| // 反序列化参数; | |||
| Method handleMethod = contractType.getHandleMethod(contractEventContext.getEvent()); | |||
| Object[] args = resolveArgs(contractEventContext.getArgs()); | |||
| Method handleMethod = ContractType.resolve(myClass).getHandleMethod(contractEventContext.getEvent()); | |||
| Object args = resolveArgs(contractEventContext.getArgs()); | |||
| ReflectionUtils.invokeMethod(handleMethod, contractMainClassObj, args); | |||
| @@ -1,20 +1,19 @@ | |||
| package com.jd.blockchain.transaction; | |||
| import com.jd.blockchain.contract.Contract; | |||
| import com.jd.blockchain.contract.ContractEvent; | |||
| import com.jd.blockchain.utils.BaseConstant; | |||
| import com.jd.blockchain.utils.Bytes; | |||
| import java.lang.annotation.Annotation; | |||
| import java.lang.reflect.Method; | |||
| import java.lang.reflect.Proxy; | |||
| import java.util.HashMap; | |||
| import java.util.Map; | |||
| import com.jd.blockchain.contract.ContractEvent; | |||
| import com.jd.blockchain.contract.ContractException; | |||
| import com.jd.blockchain.utils.BaseConstant; | |||
| import com.jd.blockchain.utils.Bytes; | |||
| import org.apache.http.annotation.Contract; | |||
| public class ContractInvocationProxyBuilder { | |||
| private Map<Class<?>, ContractType> contractTypes; | |||
| private Map<Class<?>, ContractType> contractTypes = new HashMap<Class<?>, ContractType>(); | |||
| public <T> T create(String address, Class<T> contractIntf, ContractEventSendOperationBuilder contractEventBuilder) { | |||
| return create(Bytes.fromBase58(address), contractIntf, contractEventBuilder); | |||
| @@ -52,7 +51,9 @@ public class ContractInvocationProxyBuilder { | |||
| // TODO 检查返回值类型; | |||
| return ContractType.resolve(contractIntf); | |||
| ContractType contractType1 = ContractType.resolve(contractIntf); | |||
| contractTypes.put(contractIntf,contractType1); | |||
| return contractType1; | |||
| } | |||
| @@ -12,9 +12,9 @@ public class ContractType { | |||
| private String name; | |||
| private SortedMap<String, Method> events; | |||
| private SortedMap<String, Method> events = Collections.synchronizedSortedMap(new TreeMap<>()); | |||
| private SortedMap<Method, String> handleMethods; | |||
| private Map<Method, String> handleMethods = new HashMap<>();; | |||
| /** | |||
| * 返回声明的所有事件; | |||
| @@ -25,20 +25,24 @@ public class SDKDemo_Contract { | |||
| public static BlockchainKeypair CLIENT_CERT = BlockchainKeyGenerator.getInstance().generate("ED25519"); | |||
| public static void main(String[] args) { | |||
| demoContract(); | |||
| } | |||
| /** | |||
| * 演示合约执行的过程; | |||
| */ | |||
| public static void demoContract() { | |||
| // 账本地址; | |||
| String ledgerAddress = "6GgNS3YgtxvZDBMvHEoqDiNZvWdiJ3MMpvRS9kL4DYwr4"; | |||
| String ledgerAddress = "j5rpuGWVxSuUbU3gK7MDREfui797AjfdHzvAMiSaSzydu7"; | |||
| // 节点地址列表; | |||
| // NetworkAddress[] peerAddrs = { new NetworkAddress("192.168.10.10", 8080), | |||
| // new NetworkAddress("192.168.10.11", 8080), new NetworkAddress("192.168.10.12", 8080), | |||
| // new NetworkAddress("192.168.10.13", 8080) }; | |||
| // 创建服务代理; | |||
| final String GATEWAY_IP = "192.168.151.39"; | |||
| final int GATEWAY_PORT = 80; | |||
| final String GATEWAY_IP = "localhost"; | |||
| final int GATEWAY_PORT = 11000; | |||
| final boolean SECURE = false; | |||
| GatewayServiceFactory serviceFactory = GatewayServiceFactory.connect(GATEWAY_IP, GATEWAY_PORT, SECURE, | |||
| CLIENT_CERT); | |||
| @@ -52,13 +56,13 @@ public class SDKDemo_Contract { | |||
| // 一个贸易账户,贸易结算后的利润将通过一个合约账户来执行利润分配; | |||
| // 合约账户被设置为通用的账户,不具备对贸易结算账户的直接权限; | |||
| // 只有当前交易发起人具备对贸易账户的直接权限,当交易发起人对交易进行签名之后,权限被间接传递给合约账户; | |||
| String commerceAccount = "GGhhreGeasdfasfUUfehf9932lkae99ds66jf=="; | |||
| String commerceAccount = "LdeP13gKE6319LvYPyWAT4UXr2brvpitPRBN1"; | |||
| // 处理利润分成的通用业务逻辑的合约账户; | |||
| String profitDistributionContract = "AAdfe4346fHhefe34fwf343kaeER4678RT=="; | |||
| String profitDistributionContract = "LdeP13gKE6319LvYPyWAT4UXr2brvpitPRBN1"; | |||
| // 收益人账户; | |||
| String receiptorAccount1 = "MMMEy902jkjjJJDkshreGeasdfassdfajjf=="; | |||
| String receiptorAccount2 = "Kjfe8832hfa9jjjJJDkshrFjksjdlkfj93F=="; | |||
| String receiptorAccount1 = "LdeP13gKE6319LvYPyWAT4UXr2brvpitPRBN1"; | |||
| String receiptorAccount2 = "LdeP13gKE6319LvYPyWAT4UXr2brvpitPRBN1"; | |||
| // 资产编码; | |||
| String assetKey = "RMB-ASSET"; | |||
| // 此次待分配利润; | |||
| @@ -22,7 +22,7 @@ public class BaseConstant { | |||
| public static final String SYS_CONTRACT_PROPS_NAME = "sys-contract.properties"; | |||
| public static final String CONTRACT_MAIN_CLASS_KEY = "contract"; | |||
| public static final String CONTRACT_EVENT_PREFIX="@com.jd.blockchain.contract.model.ContractEvent(name="; | |||
| public static final String CONTRACT_EVENT_PREFIX="@com.jd.blockchain.contract.ContractEvent(name="; | |||
| // 编译时引用包黑名单 | |||
| public static final String PACKAGE_BLACKLIST = "BLACKLIST"; | |||