Search in sources :

Example 1 with ParticipantContext

use of com.jd.blockchain.peer.service.ParticipantContext in project jdchain-core by blockchain-jd-com.

the class ManagementController method updateParticipant.

/**
 * 更新参与方共识配置(IP,端口,是否开启安全连接); <br>
 *
 * @param base58LedgerHash base58格式的账本哈希;
 * @param consensusHost    待更新参与方的共识Ip
 * @param consensusPort    待更新参与方的共识Port
 * @param consensusSecure  待更新参与方的共识服务是否开启安全连接
 * @return
 */
@RequestMapping(path = "/delegate/updateparticipant", method = RequestMethod.POST)
public WebResponse updateParticipant(@RequestParam("ledgerHash") String base58LedgerHash, @RequestParam("consensusHost") String consensusHost, @RequestParam("consensusPort") int consensusPort, @RequestParam(name = "consensusSecure", required = false, defaultValue = "false") boolean consensusSecure, @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!");
        }
        LedgerRepository ledgerRepo = (LedgerRepository) ledgerQuerys.get(ledgerHash);
        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 updateNode = getCurrentNode(ledgerAdminInfo, ledgerCurrNodes.get(ledgerHash).getAddress().toString());
        NodeSettings nodeSettings = getConsensusNodeSettings(ledgerQuerys.values(), consensusHost, consensusPort);
        if (nodeSettings != null) {
            if (!BytesUtils.equals(updateNode.getPubKey().toBytes(), nodeSettings.getPubKey().toBytes())) {
                return WebResponse.createFailureResult(-1, String.format("%s:%d already occupied!", consensusHost, consensusPort, updateNode.getAddress().toBase58()));
            } else {
                LOGGER.info("participant {}:{} exists and status is CONSENSUS!", consensusHost, consensusPort);
                // 节点存在且状态为激活,返回成功
                return WebResponse.createSuccessResult(null);
            }
        }
        if (updateNode.getParticipantNodeState() != ParticipantNodeState.CONSENSUS) {
            return WebResponse.createFailureResult(-1, "participant not in CONSENSUS state!");
        }
        NetworkAddress updateConsensusNodeAddress = new NetworkAddress(consensusHost, consensusPort, consensusSecure);
        LOGGER.info("update participant {}:{}", consensusHost, consensusPort);
        return updateParticipant(context, updateNode, updateConsensusNodeAddress, shutdown);
    } catch (Exception e) {
        LOGGER.error(String.format("update participant %s:%d failed!", consensusHost, consensusPort), e);
        return WebResponse.createFailureResult(-1, "update participant failed! " + e.getMessage());
    } finally {
        ParticipantContext.clear();
    }
}
Also used : BftsmartNodeSettings(com.jd.blockchain.consensus.bftsmart.BftsmartNodeSettings) NetworkAddress(utils.net.NetworkAddress) IParticipantManagerService(com.jd.blockchain.peer.service.IParticipantManagerService) ParticipantContext(com.jd.blockchain.peer.service.ParticipantContext) BusinessException(utils.BusinessException)

Example 2 with ParticipantContext

use of com.jd.blockchain.peer.service.ParticipantContext in project jdchain-core by blockchain-jd-com.

the class ManagementController method deActivateParticipant.

/**
 * 代理交易; <br>
 * <p>
 * 此方法假设当前节点是一个待移除的共识节点, 通过此方法接收一笔用于实现管理操作的交易;
 *
 * <p>
 * <p>
 * 此方法接收到交易之后,先把交易提交到已有的共识网络执行,这个已有网络包括本节点; <br>
 *
 * <p>
 * 如果操作中涉及到共识参与方的共识参数变化,将触发将此节点的共识拓扑改变的操作;
 *
 * @param base58LedgerHash   base58格式的账本哈希;
 * @param participantAddress 待移除参与方的地址
 * @return
 */
@RequestMapping(path = "/delegate/deactiveparticipant", method = RequestMethod.POST)
public WebResponse deActivateParticipant(@RequestParam("ledgerHash") String base58LedgerHash, @RequestParam("participantAddress") String participantAddress) {
    try {
        HashDigest ledgerHash = Crypto.resolveAsHashDigest(Base58Utils.decode(base58LedgerHash));
        // 进行一系列安全检查
        if (ledgerQuerys.get(ledgerHash) == null) {
            return WebResponse.createFailureResult(-1, "input ledgerhash not exist!");
        }
        if (!ledgerCurrNodes.get(ledgerHash).getAddress().toBase58().equals(participantAddress)) {
            return WebResponse.createFailureResult(-1, "deactive participant not me!");
        }
        LedgerRepository ledgerRepo = (LedgerRepository) ledgerQuerys.get(ledgerHash);
        LedgerAdminInfo ledgerAdminInfo = ledgerRepo.getAdminInfo();
        String currentProvider = ledgerAdminInfo.getSettings().getConsensusProvider();
        IParticipantManagerService participantService = consensusServiceFactory.getService(currentProvider);
        if (participantService == null || !participantService.supportManagerParticipant()) {
            return WebResponse.createFailureResult(-1, "not support operation");
        }
        // 已经是DEACTIVATED状态
        ParticipantNode node = getCurrentNode(ledgerAdminInfo, participantAddress);
        if (node.getParticipantNodeState() == ParticipantNodeState.DEACTIVATED) {
            return WebResponse.createSuccessResult(null);
        }
        // 已经处于最小节点数环境的共识网络,不能再执行去激活操作
        List<NodeSettings> origConsensusNodes = SearchOrigConsensusNodes(ledgerRepo);
        if (origConsensusNodes.size() <= participantService.minConsensusNodes()) {
            return WebResponse.createFailureResult(-1, "in minimum number of nodes scenario, deactive op is not allowed!");
        }
        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()));
        Properties customProperties = participantService.getCustomProperties(context);
        // 由本节点准备交易
        TransactionRequest txRequest = prepareDeActiveTx(ledgerHash, node, customProperties);
        // 为交易添加本节点的签名信息,防止无法通过安全策略检查
        txRequest = addNodeSigner(txRequest);
        // 连接原有的共识网络,把交易提交到目标账本的原有共识网络进行共识,即在原有共识网络中执行参与方的去激活操作,这个原有网络包括本节点
        TransactionResponse txResponse = participantService.submitNodeStateChangeTx(context, node.getId(), txRequest, origConsensusNodes);
        if (!txResponse.isSuccess()) {
            return WebResponse.createFailureResult(-1, "commit tx to orig consensus, tx execute failed, please retry deactivate participant!");
        }
        // 保证原有共识网络账本状态与共识协议的视图更新信息一致
        WebResponse response = participantService.applyConsensusGroupNodeChange(context, ledgerCurrNodes.get(ledgerHash), null, origConsensusNodes, ParticipantUpdateType.DEACTIVE);
        ledgerPeers.get(ledgerHash).stop();
        LOGGER.info("updateView success!");
        return response;
    } catch (Exception e) {
        return WebResponse.createFailureResult(-1, "deactivate participant failed!" + e);
    } finally {
        ParticipantContext.clear();
    }
}
Also used : WebResponse(com.jd.httpservice.utils.web.WebResponse) IParticipantManagerService(com.jd.blockchain.peer.service.IParticipantManagerService) ParticipantContext(com.jd.blockchain.peer.service.ParticipantContext) BusinessException(utils.BusinessException) BftsmartNodeSettings(com.jd.blockchain.consensus.bftsmart.BftsmartNodeSettings)

Example 3 with ParticipantContext

use of com.jd.blockchain.peer.service.ParticipantContext 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();
    }
}
Also used : WebResponse(com.jd.httpservice.utils.web.WebResponse) IParticipantManagerService(com.jd.blockchain.peer.service.IParticipantManagerService) ParticipantContext(com.jd.blockchain.peer.service.ParticipantContext) BusinessException(utils.BusinessException) BftsmartNodeSettings(com.jd.blockchain.consensus.bftsmart.BftsmartNodeSettings) NetworkAddress(utils.net.NetworkAddress) ServiceEndpoint(com.jd.httpservice.agent.ServiceEndpoint)

Aggregations

BftsmartNodeSettings (com.jd.blockchain.consensus.bftsmart.BftsmartNodeSettings)3 IParticipantManagerService (com.jd.blockchain.peer.service.IParticipantManagerService)3 ParticipantContext (com.jd.blockchain.peer.service.ParticipantContext)3 BusinessException (utils.BusinessException)3 WebResponse (com.jd.httpservice.utils.web.WebResponse)2 NetworkAddress (utils.net.NetworkAddress)2 ServiceEndpoint (com.jd.httpservice.agent.ServiceEndpoint)1