From 6bf6343c5006a4f809c7fa6e5f02e8bd4dd2b340 Mon Sep 17 00:00:00 2001 From: CaiHQ Date: Sun, 21 Nov 2021 23:20:44 +0800 Subject: [PATCH] merge dengshuang-feature refactor: NetworkManager --- ...terStub.java => AgentPeerManagerIntf.java} | 4 +-- .../java/org/bdware/sc/ContractClient.java | 6 ++-- .../java/org/bdware/sc/ContractManager.java | 27 +++++++++-------- src/main/java/org/bdware/sc/ContractMeta.java | 30 +++++++++---------- .../server/trustedmodel/ContractExecutor.java | 1 + 5 files changed, 35 insertions(+), 33 deletions(-) rename src/main/java/org/bdware/sc/{MasterStub.java => AgentPeerManagerIntf.java} (75%) diff --git a/src/main/java/org/bdware/sc/MasterStub.java b/src/main/java/org/bdware/sc/AgentPeerManagerIntf.java similarity index 75% rename from src/main/java/org/bdware/sc/MasterStub.java rename to src/main/java/org/bdware/sc/AgentPeerManagerIntf.java index 293fd12..2f25058 100644 --- a/src/main/java/org/bdware/sc/MasterStub.java +++ b/src/main/java/org/bdware/sc/AgentPeerManagerIntf.java @@ -3,11 +3,11 @@ package org.bdware.sc; import org.bdware.sc.bean.ContractRequest; import org.bdware.sc.conn.ResultCallback; -public interface MasterStub { +public interface AgentPeerManagerIntf { void transferToOtherNode(String pubKey, String contractID); void executeByOtherNodeAsync(String pubKey, ContractRequest c, ResultCallback cb); - boolean hasConnection(String pubKey); + boolean hasAgentConnection(String pubKey); } diff --git a/src/main/java/org/bdware/sc/ContractClient.java b/src/main/java/org/bdware/sc/ContractClient.java index 8495f71..04464e5 100644 --- a/src/main/java/org/bdware/sc/ContractClient.java +++ b/src/main/java/org/bdware/sc/ContractClient.java @@ -144,15 +144,13 @@ public class ContractClient { new TypeToken>() { }.getType()); // LOGGER.info("initProps ---- position-----4"); - contractMeta.exportedFunctions = new HashMap<>(); - List exportedFunctions = + contractMeta.exportedFunctions = JsonUtil.fromJson( get.syncGet("", "getExportedFunctions", ""), new TypeToken>() { }.getType()); contractMeta.logDetail = new HashMap<>(); - for (FunctionDesp func : exportedFunctions) { - contractMeta.exportedFunctions.put(func.functionName, func); + for (FunctionDesp func : contractMeta.exportedFunctions) { StringBuilder str = new StringBuilder(); for (AnnotationNode anno : func.annotations) { if (anno.getType().equals("LogType")) { diff --git a/src/main/java/org/bdware/sc/ContractManager.java b/src/main/java/org/bdware/sc/ContractManager.java index 21a2bf0..04fa9ac 100644 --- a/src/main/java/org/bdware/sc/ContractManager.java +++ b/src/main/java/org/bdware/sc/ContractManager.java @@ -93,7 +93,7 @@ public class ContractManager { protected final EventBroker eventBroker; public NodeCenterConn nodeCenterConn; - public MasterStub masterStub; + public AgentPeerManagerIntf masterStub; public ChainOpener chainOpener; public ContractStatusRecorder statusRecorder; public MultiContractRecorder multiContractRecorder; @@ -1297,23 +1297,22 @@ public class ContractManager { if (client.contractMeta.sigRequired) { if (!request.verifySignature()) { cr = new ContractResult(Status.Error, new JsonPrimitive("sign verified failed")); - rcb.onResult(JsonUtil.parseObject(cr)); + rcb.onResult(JsonUtil.parseObjectAsJsonObject(cr)); return; } } client.times++; client.contractStatus = ContractStatus.Executing; ResultCallback acb; - // TODO acb bu dei jin + //use to write to chian acb = new ResultCallback() { @Override public void onResult(String result) { - //TODO???? client.traffic += result.length(); client.contractStatus = ContractStatus.Executed; - JsonObject finalRet = JsonUtil.parseString(result); + JsonObject finalRet = JsonUtil.parseStringAsJsonObject(result); rcb.onResult(finalRet); if (finalRet != null) { @@ -1368,7 +1367,7 @@ public class ContractManager { client.traffic += result.length(); if (client.getContractCopies() == 1) { - extractEventsFromContractResult(ocb, JsonUtil.parseString(result), client, request, start); + extractEventsFromContractResult(ocb, JsonUtil.parseStringAsJsonObject(result), client, request, start); } chainOpener.writeContractResultToLocalAndLedger( result, client, request, ocb, start, System.currentTimeMillis() - start); @@ -1463,15 +1462,19 @@ public class ContractManager { long start = System.currentTimeMillis(); ResultCallback eventExtractor = new ResultCallback() { @Override - public void onResult(String ret) { - JsonObject result = JsonUtil.parseString(ret); + public void onResult(String ret){ + JsonObject result = JsonUtil.parseStringAsJsonObject(ret); + this.onResult(result); + } + @Override + public void onResult(JsonObject result) { ContractManager.instance.extractEventsFromContractResult( null, result, client, cr, start); LOGGER.debug( new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") .format(new Date(System.currentTimeMillis())) + meta.contractExecutor + " 结果是 " - + ret + + result.toString() + "\n"); rcb.onResult(result); } @@ -1505,7 +1508,7 @@ public class ContractManager { // LOGGER.info("查看合约 " + cr.getContractID() + " 的master为 " + pubKey); - if (!masterStub.hasConnection(pubKey)) { + if (!masterStub.hasAgentConnection(pubKey)) { pubKey = nodeCenterConn.reRouteContract(cr.getContractID()); LOGGER.info("查看合约 " + cr.getContractID() + " 的master为 " + pubKey); } @@ -1541,7 +1544,7 @@ public class ContractManager { new JsonPrimitive( "Contract " + cr.getContractID() + " doesn't exists!!")); } - rcb.onResult(JsonUtil.parseObject(result)); + rcb.onResult(JsonUtil.parseObjectAsJsonObject(result)); } @@ -2159,7 +2162,7 @@ public class ContractManager { if (null != client) { try { String ret = client.get.syncGet("", "suicide", ""); - JsonObject jo = JsonUtil.parseString(ret); + JsonObject jo = JsonUtil.parseStringAsJsonObject(ret); if (jo.has("cleanSub")) { REvent msg = new REvent( diff --git a/src/main/java/org/bdware/sc/ContractMeta.java b/src/main/java/org/bdware/sc/ContractMeta.java index d96eb12..9c30bbe 100644 --- a/src/main/java/org/bdware/sc/ContractMeta.java +++ b/src/main/java/org/bdware/sc/ContractMeta.java @@ -9,7 +9,10 @@ import org.bdware.sc.node.YjsType; import org.bdware.server.trustedmodel.ContractExecutor; import org.bdware.server.trustedmodel.SingleNodeExecutor; -import java.util.*; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; public class ContractMeta implements IDSerializable { public Contract contract; @@ -18,7 +21,7 @@ public class ContractMeta implements IDSerializable { String id; boolean isDebug; Map declaredEvents; - Map exportedFunctions; + List exportedFunctions; Map logDetail; List annotations; Set dependentContracts; @@ -59,8 +62,8 @@ public class ContractMeta implements IDSerializable { return annotations; } - public Collection getExportedFunctions() { - return null == exportedFunctions ? null : exportedFunctions.values(); + public List getExportedFunctions() { + return exportedFunctions; } public Set getDependentContracts() { @@ -98,22 +101,19 @@ public class ContractMeta implements IDSerializable { } public FunctionDesp getExportedFunction(String action) { - if (funCache == null) { - funCache = new HashMap<>(); - } + if (funCache == null) funCache = new HashMap<>(); FunctionDesp desp = funCache.get(action); - if (desp != null) { - return desp; - } + if (desp != null) return desp; desp = seekFunction(action); - if (null != desp) { - funCache.put(action, desp); - } + if (desp != null) funCache.put(action, desp); return desp; } - public FunctionDesp seekFunction(String action) { - return null == exportedFunctions ? null : exportedFunctions.get(action); + private FunctionDesp seekFunction(String action) { + for (FunctionDesp desp : exportedFunctions) { + if (desp != null && desp.functionName.equals(action)) return desp; + } + return null; } public void setContract(Contract c) { diff --git a/src/main/java/org/bdware/server/trustedmodel/ContractExecutor.java b/src/main/java/org/bdware/server/trustedmodel/ContractExecutor.java index c9b137d..ba6cce8 100644 --- a/src/main/java/org/bdware/server/trustedmodel/ContractExecutor.java +++ b/src/main/java/org/bdware/server/trustedmodel/ContractExecutor.java @@ -6,4 +6,5 @@ import org.bdware.sc.conn.ResultCallback; public interface ContractExecutor { void execute(String requestID, ContractRequest req, ResultCallback rcb, OnHashCallback hcb); + }