use of com.jd.httpservice.agent.ServiceEndpoint in project jdchain-core by blockchain-jd-com.
the class BlockSyncService method getConsensusNodeManagerInfo.
private ServiceEndpoint getConsensusNodeManagerInfo(Endpoint remoteEndpoint) {
try {
QueryManagerInfoRequest request = new QueryManagerInfoRequest();
RpcResponse response = (RpcResponse) rpcClient.invokeSync(remoteEndpoint, request, requestTimeoutMs);
QueryManagerInfoRequestProcessor.ManagerInfoResponse infoResponse = QueryManagerInfoRequestProcessor.ManagerInfoResponse.fromBytes(response.getResult());
return new ServiceEndpoint(remoteEndpoint.getIp(), infoResponse.getManagerPort(), infoResponse.isManagerSSLEnabled());
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
}
return null;
}
use of com.jd.httpservice.agent.ServiceEndpoint in project jdchain-core by blockchain-jd-com.
the class ManagementController method replayTransaction.
private boolean replayTransaction(LedgerRepository ledgerRepository, ParticipantNode node, ServiceEndpoint endpoint) {
long height = ledgerRepository.retrieveLatestBlock().getHeight();
HashDigest ledgerHash = ledgerRepository.retrieveLatestBlock().getLedgerHash();
TransactionBatchResultHandle handle = null;
OperationHandleRegisteration opReg = new DefaultOperationHandleRegisteration();
try (ServiceConnection httpConnection = ServiceConnectionManager.connect(endpoint)) {
HttpBlockchainBrowserService queryService = HttpServiceAgent.createService(HttpBlockchainBrowserService.class, httpConnection, null);
while (true) {
boolean getout = false;
TransactionBatchProcessor batchProcessor = new TransactionBatchProcessor(ledgerRepository, opReg);
try {
height++;
long remoteLatestBlockHeight = queryService.getLedger(ledgerHash).getLatestBlockHeight();
// fix endpoint write block to database slow bug, that lead to active node failed!
int count = 0;
while ((remoteLatestBlockHeight < height) && (count < 600)) {
Thread.sleep(1000);
remoteLatestBlockHeight = queryService.getLedger(ledgerHash).getLatestBlockHeight();
count++;
}
if (remoteLatestBlockHeight < height) {
throw new IllegalStateException("Remote endpoint block height exception!");
}
LedgerBlock block = queryService.getBlock(ledgerHash, height);
// 获取区块内的增量交易
List<LedgerTransaction> transactions = getAdditionalTransactions(queryService, ledgerHash, (int) height);
try {
for (LedgerTransaction ledgerTransaction : transactions) {
batchProcessor.schedule(ledgerTransaction.getRequest());
Operation[] operations = ledgerTransaction.getRequest().getTransactionContent().getOperations();
for (Operation op : operations) {
if (op instanceof ParticipantStateUpdateOperation) {
ParticipantStateUpdateOperation psop = (ParticipantStateUpdateOperation) op;
if (psop.getParticipantID().getPubKey().equals(node.getPubKey())) {
getout = true;
}
}
}
}
} catch (BlockRollbackException e) {
batchProcessor.cancel(LEDGER_ERROR);
continue;
}
LedgerEditor.TIMESTAMP_HOLDER.set(block.getTimestamp());
handle = batchProcessor.prepare();
if (!(handle.getBlock().getHash().toBase58().equals(block.getHash().toBase58()))) {
LOGGER.error("replayTransaction, transactions replay result is inconsistent at height {}", height);
throw new IllegalStateException("checkLedgerDiff, transactions replay, block hash result is inconsistent!");
}
handle.commit();
LOGGER.debug("replayTransaction, transactions replay result is consistent at height {}", height);
if (getout) {
return true;
}
} catch (Exception e) {
handle.cancel(LEDGER_ERROR);
throw new IllegalStateException("replayTransaction, transactions replay failed!", e);
}
}
}
}
use of com.jd.httpservice.agent.ServiceEndpoint in project jdchain-core by blockchain-jd-com.
the class ManagementController method activateParticipant.
/**
* 激活参与方; <br>
* <p>
* 此方法假设当前节点是一个新建但尚未加入共识网络的共识节点, 通过此方法接收一笔用于实现管理操作的交易;
*
* <p>
* <p>
* 此方法接收到交易之后,先把交易提交到已有的共识网络执行; <br>
* <p>
* 如果交易通过验证并执行成功,则将交易在本地的账本中以本地方式执行; <br>
* <p>
* 如果执行之后的新区块一致,则提交本地区块;
*
* <p>
* 如果操作中涉及到共识参与方的共识参数变化,将触发将此节点的共识拓扑改变的操作;
*
* @param base58LedgerHash base58格式的账本哈希;
* @param consensusHost 激活参与方的共识Ip
* @param consensusPort 激活参与方的共识Port
* @param consensusSecure 激活参与方的共识服务是否开启安全连接
* @param remoteManageHost 提供完备数据库的共识节点管理IP
* @param remoteManagePort 提供完备数据库的共识节点管理Port
* @param remoteManageSecure 提供完备数据库的共识节点管理服务是否开启安全连接
* @return
*/
@RequestMapping(path = "/delegate/activeparticipant", method = RequestMethod.POST)
public WebResponse activateParticipant(@RequestParam("ledgerHash") String base58LedgerHash, @RequestParam("consensusHost") String consensusHost, @RequestParam("consensusPort") int consensusPort, @RequestParam(name = "consensusSecure", required = false, defaultValue = "false") boolean consensusSecure, @RequestParam("remoteManageHost") String remoteManageHost, @RequestParam("remoteManagePort") int remoteManagePort, @RequestParam(name = "remoteManageSecure", required = false, defaultValue = "false") boolean remoteManageSecure, @RequestParam(name = "shutdown", required = false, defaultValue = "false") boolean shutdown) {
try {
HashDigest ledgerHash = Crypto.resolveAsHashDigest(Base58Utils.decode(base58LedgerHash));
if (ledgerKeypairs.get(ledgerHash) == null) {
return WebResponse.createFailureResult(-1, "ledger hash not exist!");
}
ServiceEndpoint remoteEndpoint = new ServiceEndpoint(new NetworkAddress(remoteManageHost, remoteManagePort, remoteManageSecure));
remoteEndpoint.setSslSecurity(bindingConfigs.get(ledgerHash).getSslSecurity());
LedgerRepository ledgerRepo = (LedgerRepository) ledgerQuerys.get(ledgerHash);
WebResponse webResponse = checkLedgerDiff(ledgerHash, ledgerRepo, ledgerRepo.retrieveLatestBlock(), remoteEndpoint);
if (!(webResponse.isSuccess())) {
return webResponse;
}
LedgerAdminInfo ledgerAdminInfo = ledgerRepo.getAdminInfo(ledgerRepo.retrieveLatestBlock());
String currentProvider = ledgerAdminInfo.getSettings().getConsensusProvider();
IParticipantManagerService participantService = consensusServiceFactory.getService(currentProvider);
if (participantService == null || !participantService.supportManagerParticipant()) {
return WebResponse.createFailureResult(-1, "not support operation");
}
ParticipantContext context = ParticipantContext.buildContext(ledgerHash, ledgerRepo, ledgerAdminInfo, currentProvider, participantService, bindingConfigs.get(ledgerHash).getSslSecurity());
context.setProperty(ParticipantContext.HASH_ALG_PROP, ledgerCryptoSettings.get(ledgerHash).getHashAlgorithm());
context.setProperty(ParticipantContext.ENDPOINT_SIGNER_PROP, new AsymmetricKeypair(ledgerKeypairs.get(ledgerHash).getPubKey(), ledgerKeypairs.get(ledgerHash).getPrivKey()));
// 检查节点信息
ParticipantNode addNode = getCurrentNode(ledgerAdminInfo, ledgerCurrNodes.get(ledgerHash).getAddress().toString());
NodeSettings nodeSettings = getConsensusNodeSettings(ledgerQuerys.values(), consensusHost, consensusPort);
if (nodeSettings != null) {
if (!BytesUtils.equals(addNode.getPubKey().toBytes(), nodeSettings.getPubKey().toBytes())) {
return WebResponse.createFailureResult(-1, String.format("%s:%d already occupied!", consensusHost, consensusPort, addNode.getAddress().toBase58()));
} else {
LOGGER.info("participant {}:{} exists and status is CONSENSUS!", consensusHost, consensusPort);
// 节点存在且状态为激活,返回成功
return WebResponse.createSuccessResult(null);
}
}
if (addNode.getParticipantNodeState() == ParticipantNodeState.CONSENSUS) {
LOGGER.info("participant {}:{} already in CONSENSUS state!!", consensusHost, consensusPort);
return WebResponse.createSuccessResult("participant already in CONSENSUS state!");
}
NetworkAddress addConsensusNodeAddress = new NetworkAddress(consensusHost, consensusPort, consensusSecure);
remoteEndpoint.setSslSecurity(bindingConfigs.get(ledgerHash).getSslSecurity());
LOGGER.info("active participant {}:{}!", consensusHost, consensusPort);
return activeParticipant(context, addNode, addConsensusNodeAddress, shutdown, remoteEndpoint);
} catch (Exception e) {
LOGGER.error(String.format("activate participant %s:%d failed!", consensusHost, consensusPort), e);
return WebResponse.createFailureResult(-1, "activate participant failed! " + e.getMessage());
} finally {
ParticipantContext.clear();
}
}
use of com.jd.httpservice.agent.ServiceEndpoint in project jdchain-core by blockchain-jd-com.
the class HttpInitConsensServiceFactory method connect.
@Override
public LedgerInitConsensusService connect(NetworkAddress endpointAddress) {
ServiceEndpoint endpoint = new ServiceEndpoint(endpointAddress);
endpoint.setSslSecurity(new SSLSecurity());
LedgerInitConsensusService initConsensus = HttpServiceAgent.createService(LedgerInitConsensusService.class, endpoint);
return initConsensus;
}
use of com.jd.httpservice.agent.ServiceEndpoint in project jdchain-core by blockchain-jd-com.
the class ParticipantInactive method active.
public WebResponse active() throws Exception {
String url = (secure ? "https://" : "http://") + host + ":" + port + "/management/delegate/activeparticipant";
HttpPost httpPost = new HttpPost(url);
List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
params.add(new BasicNameValuePair("ledgerHash", ledger));
params.add(new BasicNameValuePair("consensusHost", host));
params.add(new BasicNameValuePair("consensusPort", consensusPort + ""));
params.add(new BasicNameValuePair("consensusStorage", consensusStorage));
params.add(new BasicNameValuePair("consensusSecure", consensusSecure + ""));
params.add(new BasicNameValuePair("remoteManageHost", synHost));
params.add(new BasicNameValuePair("remoteManagePort", synPort + ""));
params.add(new BasicNameValuePair("remoteManageSecure", synSecure + ""));
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);
}
Aggregations