use of com.jd.httpservice.utils.web.WebResponse in project jdchain-core by blockchain-jd-com.
the class ParticipantInactive method update.
public WebResponse update() throws Exception {
String url = (secure ? "https://" : "http://") + host + ":" + port + "/management/delegate/updateparticipant";
HttpPost httpPost = new HttpPost(url);
List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
params.add(new BasicNameValuePair("ledgerHash", ledger));
params.add(new BasicNameValuePair("consensusHost", consensusHost));
params.add(new BasicNameValuePair("consensusPort", consensusPort + ""));
params.add(new BasicNameValuePair("consensusStorage", consensusStorage + ""));
params.add(new BasicNameValuePair("consensusSecure", consensusSecure + ""));
params.add(new BasicNameValuePair("shutdown", shutdown + ""));
httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
ServiceEndpoint endpoint = new ServiceEndpoint(host, port, secure);
if (secure) {
GmSSLProvider.enableGMSupport(participant.getSSLSecurity().getProtocol());
endpoint.setSslSecurity(participant.getSSLSecurity());
} else {
endpoint.setSslSecurity(new SSLSecurity());
}
HttpResponse response = ServiceConnectionManager.buildHttpClient(endpoint).execute(httpPost);
return (WebResponse) new JsonResponseConverter(WebResponse.class).getResponse(null, response.getEntity().getContent(), null);
}
use of com.jd.httpservice.utils.web.WebResponse in project jdchain-core by blockchain-jd-com.
the class ManagementController method updateParticipant.
private WebResponse updateParticipant(ParticipantContext context, ParticipantNode node, NetworkAddress updateConsensusNodeAddress, boolean shutdown) {
HashDigest ledgerHash = context.ledgerHash();
LedgerRepository ledgerRepo = context.ledgerRepo();
IParticipantManagerService participantService = context.participantService();
Properties customProperties = participantService.getCustomProperties(context);
// 由本节点准备交易
TransactionRequest txRequest = prepareUpdateTx(ledgerHash, node, updateConsensusNodeAddress, customProperties);
// 为交易添加本节点的签名信息,防止无法通过安全策略检查
txRequest = addNodeSigner(txRequest);
List<NodeSettings> origConsensusNodes = SearchOrigConsensusNodes(ledgerRepo);
// 连接原有的共识网络,把交易提交到目标账本的原有共识网络进行共识,即在原有共识网络中执行新参与方的状态激活操作
TransactionResponse remoteTxResponse = participantService.submitNodeStateChangeTx(context, node.getId(), txRequest, origConsensusNodes);
if (!remoteTxResponse.isSuccess()) {
return WebResponse.createFailureResult(-1, "commit tx to orig consensus, tx execute failed, please retry activate participant!");
}
// 保证原有共识网络账本状态与共识协议的视图更新信息一致
try {
waitUtilReachHeight(remoteTxResponse.getBlockHeight(), ledgerRepo);
if (participantService.startServerBeforeApplyNodeChange()) {
setupServer(ledgerRepo, false);
}
WebResponse webResponse = participantService.applyConsensusGroupNodeChange(context, ledgerCurrNodes.get(ledgerHash), updateConsensusNodeAddress, origConsensusNodes, ParticipantUpdateType.UPDATE);
if (!webResponse.isSuccess()) {
return webResponse;
}
} catch (Exception e) {
LOGGER.error("updateView exception!", e);
return WebResponse.createFailureResult(-1, "commit tx to orig consensus, tx execute succ but view update failed, please restart all nodes and copy database for new participant node!");
}
if (!participantService.startServerBeforeApplyNodeChange()) {
setupServer(ledgerRepo, shutdown);
}
return WebResponse.createSuccessResult(null);
}
use of com.jd.httpservice.utils.web.WebResponse in project jdchain-core by blockchain-jd-com.
the class ManagementController method activeParticipant.
private WebResponse activeParticipant(ParticipantContext context, ParticipantNode node, NetworkAddress addConsensusNodeAddress, boolean shutdown, ServiceEndpoint remoteEndpoint) {
HashDigest ledgerHash = context.ledgerHash();
LedgerRepository ledgerRepo = context.ledgerRepo();
IParticipantManagerService participantService = context.participantService();
Properties customProperties = participantService.getCustomProperties(context);
// 由本节点准备交易
TransactionRequest txRequest = prepareActiveTx(ledgerHash, node, addConsensusNodeAddress, customProperties);
// 为交易添加本节点的签名信息,防止无法通过安全策略检查
txRequest = addNodeSigner(txRequest);
List<NodeSettings> origConsensusNodes = SearchOtherOrigConsensusNodes(ledgerRepo, node);
// 连接原有的共识网络,把交易提交到目标账本的原有共识网络进行共识,即在原有共识网络中执行新参与方的状态激活操作
TransactionResponse remoteTxResponse = participantService.submitNodeStateChangeTx(context, node.getId(), txRequest, origConsensusNodes);
if (remoteTxResponse.isSuccess() && replayTransaction(ledgerRepo, node, remoteEndpoint)) {
try {
if (participantService.startServerBeforeApplyNodeChange()) {
setupServer(ledgerRepo, false);
}
// 使用共识原语变更节点
WebResponse webResponse = participantService.applyConsensusGroupNodeChange(context, ledgerCurrNodes.get(ledgerHash), addConsensusNodeAddress, origConsensusNodes, ParticipantUpdateType.ACTIVE);
if (!webResponse.isSuccess()) {
return webResponse;
}
if (!participantService.startServerBeforeApplyNodeChange()) {
setupServer(ledgerRepo, shutdown);
}
return webResponse;
} catch (Exception e) {
return WebResponse.createFailureResult(-1, "commit tx to orig consensus, tx execute succ but view update failed, please restart all nodes and copy database for new participant node!");
}
}
return WebResponse.createFailureResult(remoteTxResponse.getExecutionState().CODE, remoteTxResponse.getExecutionState().toString());
}
use of com.jd.httpservice.utils.web.WebResponse in project jdchain-core by blockchain-jd-com.
the class ManagementController method syncBlock.
/**
* 区块同步:
* 从指定节点同步最新区块信息,调用此接口会执行NodeServer重建
*
* @param ledgerHash 账本
* @param syncHost 同步节点IP
* @param syncPort 同步节点端口
* @return
*/
@RequestMapping(path = "/block/sync", method = RequestMethod.POST)
public WebResponse syncBlock(@RequestParam("ledgerHash") String ledgerHash, @RequestParam("syncHost") String syncHost, @RequestParam("syncPort") int syncPort, @RequestParam(name = "syncSecure", required = false, defaultValue = "false") boolean syncSecure, @RequestParam(name = "restart", required = false, defaultValue = "false") boolean restart) {
try {
HashDigest ledger = Crypto.resolveAsHashDigest(Base58Utils.decode(ledgerHash));
if (!ledgerKeypairs.containsKey(ledger)) {
return WebResponse.createFailureResult(-1, "input ledger hash not exist!");
}
LedgerRepository ledgerRepo = (LedgerRepository) ledgerQuerys.get(ledger);
LedgerBlock ledgerLatestBlock = ledgerRepo.retrieveLatestBlock();
LedgerAdminInfo ledgerAdminInfo = ledgerRepo.getAdminInfo(ledgerLatestBlock);
// 目前仅支持BFT-SMaRt
if (ledgerAdminInfo.getSettings().getConsensusProvider().equals(ConsensusTypeEnum.BFTSMART.getProvider())) {
// 检查本地节点与远端节点在库上是否存在差异,有差异的进行差异交易重放
ServiceEndpoint endpoint = new ServiceEndpoint(new NetworkAddress(syncHost, syncPort, syncSecure));
SSLSecurity sslSecurity = bindingConfigs.get(ledger).getSslSecurity();
endpoint.setSslSecurity(sslSecurity);
WebResponse webResponse = checkLedgerDiff(ledger, ledgerRepo, ledgerLatestBlock, endpoint);
if (!webResponse.isSuccess()) {
return webResponse;
}
// 重建 NodeServer
if (restart) {
setupServer(ledgerRepo, false);
}
LOGGER.info("sync block success!");
return WebResponse.createSuccessResult(null);
} else {
return WebResponse.createSuccessResult(null);
}
} catch (Exception e) {
LOGGER.error("sync block failed!", e);
return WebResponse.createFailureResult(-1, "sync block failed! " + e.getMessage());
}
}
use of com.jd.httpservice.utils.web.WebResponse in project jdchain-core by blockchain-jd-com.
the class DataSearchController method dataQuery.
private WebResponse dataQuery(String uri, HashDigest ledgerHash, String keyword) {
if (StringUtils.isEmpty(keyword)) {
return WebResponse.createFailureResult(new WebResponse.ErrorMessage(-1, "empty keywords"));
}
String ledger = ledgerHash.toString();
String type = uri.substring(uri.indexOf(ledger) + ledger.length(), uri.lastIndexOf("search"));
Object data = null;
switch(type) {
case // 综合查询
"/all/":
data = dataSearchService.searchAll(ledgerHash, keyword);
break;
case // 数据账户数
"/accounts/count/":
data = dataSearchService.searchDataAccountCount(ledgerHash, keyword);
break;
case // 数据账户
"/accounts/":
DataAccountInfo dataAccount = dataSearchService.searchDataAccount(ledgerHash, keyword);
data = null != dataAccount ? new DataAccountInfo[] { dataAccount } : new DataAccountInfo[] {};
break;
case // 事件账户账户数
"/events/accounts/count/":
data = dataSearchService.searchEventAccountCount(ledgerHash, keyword);
break;
case // 事件账户
"/events/accounts/":
BlockchainIdentity eventAccount = dataSearchService.searchEventAccount(ledgerHash, keyword);
data = null != eventAccount ? new BlockchainIdentity[] { eventAccount } : new BlockchainIdentity[] {};
break;
case // 合约账户数
"/contracts/count/":
data = dataSearchService.searchContractAccountCount(ledgerHash, keyword);
break;
case // 合约
"/contracts/":
ContractInfo contract = dataSearchService.searchContractAccount(ledgerHash, keyword);
data = null != contract ? new ContractInfo[] { contract } : new ContractInfo[] {};
break;
case // 用户账户数
"/users/count/":
data = dataSearchService.searchUserCount(ledgerHash, keyword);
break;
case // 用户
"/users/":
UserInfo user = dataSearchService.searchUser(ledgerHash, keyword);
data = null != user ? new UserInfo[] { user } : new UserInfo[] {};
break;
default:
return WebResponse.createFailureResult(new WebResponse.ErrorMessage(-1, "404"));
}
return WebResponse.createSuccessResult(data);
}
Aggregations