merge dengshuang-feature

refactor: NetworkManager
This commit is contained in:
CaiHQ 2021-11-21 23:20:44 +08:00
parent ca20ea58f5
commit 6bf6343c50
5 changed files with 35 additions and 33 deletions

View File

@ -3,11 +3,11 @@ package org.bdware.sc;
import org.bdware.sc.bean.ContractRequest; import org.bdware.sc.bean.ContractRequest;
import org.bdware.sc.conn.ResultCallback; import org.bdware.sc.conn.ResultCallback;
public interface MasterStub { public interface AgentPeerManagerIntf {
void transferToOtherNode(String pubKey, String contractID); void transferToOtherNode(String pubKey, String contractID);
void executeByOtherNodeAsync(String pubKey, ContractRequest c, ResultCallback cb); void executeByOtherNodeAsync(String pubKey, ContractRequest c, ResultCallback cb);
boolean hasConnection(String pubKey); boolean hasAgentConnection(String pubKey);
} }

View File

@ -144,15 +144,13 @@ public class ContractClient {
new TypeToken<Set<String>>() { new TypeToken<Set<String>>() {
}.getType()); }.getType());
// LOGGER.info("initProps ---- position-----4"); // LOGGER.info("initProps ---- position-----4");
contractMeta.exportedFunctions = new HashMap<>(); contractMeta.exportedFunctions =
List<FunctionDesp> exportedFunctions =
JsonUtil.fromJson( JsonUtil.fromJson(
get.syncGet("", "getExportedFunctions", ""), get.syncGet("", "getExportedFunctions", ""),
new TypeToken<List<FunctionDesp>>() { new TypeToken<List<FunctionDesp>>() {
}.getType()); }.getType());
contractMeta.logDetail = new HashMap<>(); contractMeta.logDetail = new HashMap<>();
for (FunctionDesp func : exportedFunctions) { for (FunctionDesp func : contractMeta.exportedFunctions) {
contractMeta.exportedFunctions.put(func.functionName, func);
StringBuilder str = new StringBuilder(); StringBuilder str = new StringBuilder();
for (AnnotationNode anno : func.annotations) { for (AnnotationNode anno : func.annotations) {
if (anno.getType().equals("LogType")) { if (anno.getType().equals("LogType")) {

View File

@ -93,7 +93,7 @@ public class ContractManager {
protected final EventBroker eventBroker; protected final EventBroker eventBroker;
public NodeCenterConn nodeCenterConn; public NodeCenterConn nodeCenterConn;
public MasterStub masterStub; public AgentPeerManagerIntf masterStub;
public ChainOpener chainOpener; public ChainOpener chainOpener;
public ContractStatusRecorder statusRecorder; public ContractStatusRecorder statusRecorder;
public MultiContractRecorder multiContractRecorder; public MultiContractRecorder multiContractRecorder;
@ -1297,23 +1297,22 @@ public class ContractManager {
if (client.contractMeta.sigRequired) { if (client.contractMeta.sigRequired) {
if (!request.verifySignature()) { if (!request.verifySignature()) {
cr = new ContractResult(Status.Error, new JsonPrimitive("sign verified failed")); cr = new ContractResult(Status.Error, new JsonPrimitive("sign verified failed"));
rcb.onResult(JsonUtil.parseObject(cr)); rcb.onResult(JsonUtil.parseObjectAsJsonObject(cr));
return; return;
} }
} }
client.times++; client.times++;
client.contractStatus = ContractStatus.Executing; client.contractStatus = ContractStatus.Executing;
ResultCallback acb; ResultCallback acb;
// TODO acb bu dei jin //use to write to chian
acb = acb =
new ResultCallback() { new ResultCallback() {
@Override @Override
public void onResult(String result) { public void onResult(String result) {
//TODO????
client.traffic += result.length(); client.traffic += result.length();
client.contractStatus = ContractStatus.Executed; client.contractStatus = ContractStatus.Executed;
JsonObject finalRet = JsonUtil.parseString(result); JsonObject finalRet = JsonUtil.parseStringAsJsonObject(result);
rcb.onResult(finalRet); rcb.onResult(finalRet);
if (finalRet != null) { if (finalRet != null) {
@ -1368,7 +1367,7 @@ public class ContractManager {
client.traffic += result.length(); client.traffic += result.length();
if (client.getContractCopies() == 1) { if (client.getContractCopies() == 1) {
extractEventsFromContractResult(ocb, JsonUtil.parseString(result), client, request, start); extractEventsFromContractResult(ocb, JsonUtil.parseStringAsJsonObject(result), client, request, start);
} }
chainOpener.writeContractResultToLocalAndLedger( chainOpener.writeContractResultToLocalAndLedger(
result, client, request, ocb, start, System.currentTimeMillis() - start); result, client, request, ocb, start, System.currentTimeMillis() - start);
@ -1464,14 +1463,18 @@ public class ContractManager {
ResultCallback eventExtractor = new ResultCallback() { ResultCallback eventExtractor = new ResultCallback() {
@Override @Override
public void onResult(String ret){ public void onResult(String ret){
JsonObject result = JsonUtil.parseString(ret); JsonObject result = JsonUtil.parseStringAsJsonObject(ret);
this.onResult(result);
}
@Override
public void onResult(JsonObject result) {
ContractManager.instance.extractEventsFromContractResult( ContractManager.instance.extractEventsFromContractResult(
null, result, client, cr, start); null, result, client, cr, start);
LOGGER.debug( LOGGER.debug(
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
.format(new Date(System.currentTimeMillis())) .format(new Date(System.currentTimeMillis()))
+ meta.contractExecutor + " 结果是 " + meta.contractExecutor + " 结果是 "
+ ret + result.toString()
+ "\n"); + "\n");
rcb.onResult(result); rcb.onResult(result);
} }
@ -1505,7 +1508,7 @@ public class ContractManager {
// LOGGER.info("查看合约 " + cr.getContractID() + " 的master为 " + pubKey); // LOGGER.info("查看合约 " + cr.getContractID() + " 的master为 " + pubKey);
if (!masterStub.hasConnection(pubKey)) { if (!masterStub.hasAgentConnection(pubKey)) {
pubKey = nodeCenterConn.reRouteContract(cr.getContractID()); pubKey = nodeCenterConn.reRouteContract(cr.getContractID());
LOGGER.info("查看合约 " + cr.getContractID() + " 的master为 " + pubKey); LOGGER.info("查看合约 " + cr.getContractID() + " 的master为 " + pubKey);
} }
@ -1541,7 +1544,7 @@ public class ContractManager {
new JsonPrimitive( new JsonPrimitive(
"Contract " + cr.getContractID() + " doesn't exists!!")); "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) { if (null != client) {
try { try {
String ret = client.get.syncGet("", "suicide", ""); String ret = client.get.syncGet("", "suicide", "");
JsonObject jo = JsonUtil.parseString(ret); JsonObject jo = JsonUtil.parseStringAsJsonObject(ret);
if (jo.has("cleanSub")) { if (jo.has("cleanSub")) {
REvent msg = REvent msg =
new REvent( new REvent(

View File

@ -9,7 +9,10 @@ import org.bdware.sc.node.YjsType;
import org.bdware.server.trustedmodel.ContractExecutor; import org.bdware.server.trustedmodel.ContractExecutor;
import org.bdware.server.trustedmodel.SingleNodeExecutor; 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 class ContractMeta implements IDSerializable {
public Contract contract; public Contract contract;
@ -18,7 +21,7 @@ public class ContractMeta implements IDSerializable {
String id; String id;
boolean isDebug; boolean isDebug;
Map<String, REvent.REventSemantics> declaredEvents; Map<String, REvent.REventSemantics> declaredEvents;
Map<String, FunctionDesp> exportedFunctions; List<FunctionDesp> exportedFunctions;
Map<String, String> logDetail; Map<String, String> logDetail;
List<AnnotationNode> annotations; List<AnnotationNode> annotations;
Set<String> dependentContracts; Set<String> dependentContracts;
@ -59,8 +62,8 @@ public class ContractMeta implements IDSerializable {
return annotations; return annotations;
} }
public Collection<FunctionDesp> getExportedFunctions() { public List<FunctionDesp> getExportedFunctions() {
return null == exportedFunctions ? null : exportedFunctions.values(); return exportedFunctions;
} }
public Set<String> getDependentContracts() { public Set<String> getDependentContracts() {
@ -98,22 +101,19 @@ public class ContractMeta implements IDSerializable {
} }
public FunctionDesp getExportedFunction(String action) { public FunctionDesp getExportedFunction(String action) {
if (funCache == null) { if (funCache == null) funCache = new HashMap<>();
funCache = new HashMap<>();
}
FunctionDesp desp = funCache.get(action); FunctionDesp desp = funCache.get(action);
if (desp != null) { if (desp != null) return desp;
return desp;
}
desp = seekFunction(action); desp = seekFunction(action);
if (null != desp) { if (desp != null) funCache.put(action, desp);
funCache.put(action, desp);
}
return desp; return desp;
} }
public FunctionDesp seekFunction(String action) { private FunctionDesp seekFunction(String action) {
return null == exportedFunctions ? null : exportedFunctions.get(action); for (FunctionDesp desp : exportedFunctions) {
if (desp != null && desp.functionName.equals(action)) return desp;
}
return null;
} }
public void setContract(Contract c) { public void setContract(Contract c) {

View File

@ -6,4 +6,5 @@ import org.bdware.sc.conn.ResultCallback;
public interface ContractExecutor { public interface ContractExecutor {
void execute(String requestID, ContractRequest req, ResultCallback rcb, OnHashCallback hcb); void execute(String requestID, ContractRequest req, ResultCallback rcb, OnHashCallback hcb);
} }