Search in sources :

Example 1 with LedgerStateManager

use of com.jd.blockchain.peer.consensus.LedgerStateManager in project jdchain-core by blockchain-jd-com.

the class ManagementController method setupServer.

// 视图更新完成,启动共识节点
private void setupServer(LedgerRepository ledgerRepository, boolean shutdown) {
    try {
        HashDigest ledgerHash = ledgerRepository.getHash();
        // 关闭旧的server
        NodeServer server = ledgerPeers.get(ledgerHash);
        if (null != server) {
            LOGGER.info("stop old server");
            server.stop();
        }
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            LOGGER.error("sleep InterruptedException", e);
        }
        if (shutdown) {
            LOGGER.info("shutdown server in this ip");
            return;
        }
        ParticipantNode currentNode = ledgerCurrNodes.get(ledgerHash);
        LedgerAdminInfo ledgerAdminAccount = ledgerRepository.getAdminInfo(ledgerRepository.getBlock(ledgerRepository.retrieveLatestBlockHeight()));
        // load provider;
        ConsensusProvider provider = getProvider(ledgerAdminAccount);
        // load consensus setting;
        ConsensusViewSettings csSettings = getConsensusSetting(ledgerAdminAccount);
        ServerSettings serverSettings = provider.getServerFactory().buildServerSettings(ledgerHash.toBase58(), csSettings, currentNode.getAddress().toBase58(), bindingConfigs.get(ledgerHash).getSslSecurity(), bindingConfigs.get(ledgerHash).getExtraProperties());
        ((LedgerStateManager) consensusStateManager).setLatestStateId(ledgerRepository.retrieveLatestBlockHeight());
        Storage consensusRuntimeStorage = getConsensusRuntimeStorage(ledgerHash);
        server = provider.getServerFactory().setupServer(serverSettings, consensusMessageHandler, consensusStateManager, consensusRuntimeStorage);
        ledgerPeers.put(ledgerHash, server);
        runRealm(server);
        LOGGER.info("setupServer success!");
    } catch (Exception e) {
        throw new StartServerException("start server fail exception", e);
    }
}
Also used : BftsmartConsensusViewSettings(com.jd.blockchain.consensus.bftsmart.BftsmartConsensusViewSettings) LedgerStateManager(com.jd.blockchain.peer.consensus.LedgerStateManager) Storage(utils.io.Storage) BusinessException(utils.BusinessException)

Example 2 with LedgerStateManager

use of com.jd.blockchain.peer.consensus.LedgerStateManager in project jdchain-core by blockchain-jd-com.

the class ManagementController method setConfig.

@Override
public NodeServer setConfig(BindingConfig bindingConfig, HashDigest ledgerHash) {
    // LedgerBindingConfig.BindingConfig bindingConfig = config.getLedger(ledgerHash);
    LedgerQuery ledgerRepository = null;
    NodeServer server = null;
    ParticipantNode currentNode = null;
    LedgerAdminInfo ledgerAdminAccount = null;
    try {
        DbConnection dbConnNew = connFactory.connect(bindingConfig.getDbConnection().getUri(), bindingConfig.getDbConnection().getPassword());
        ledgerRepository = ledgerManager.register(ledgerHash, dbConnNew.getStorageService(), bindingConfig.getDataStructure());
        ledgerAdminAccount = ledgerRepository.getAdminInfo();
        ConsensusProvider provider = getProvider(ledgerAdminAccount);
        // load consensus setting;
        ConsensusViewSettings csSettings = getConsensusSetting(ledgerAdminAccount);
        for (ParticipantNode participantNode : ledgerAdminAccount.getParticipants()) {
            LOGGER.debug("[!!!] ParticipantNode.getAddress().toString()= {}", participantNode.getAddress().toString());
            if (participantNode.getAddress().toString().equals(bindingConfig.getParticipant().getAddress())) {
                currentNode = participantNode;
                break;
            }
        }
        if (currentNode == null) {
            throw new IllegalArgumentException("Current node is not found from the participant settings of ledger[" + ledgerHash.toBase58() + "]!");
        }
        LedgerMetadata_V2 metadata = ledgerRepository.getAdminInfo().getMetadata();
        ledgerIdMode.put(ledgerHash, null != metadata.getIdentityMode() ? metadata.getIdentityMode() : IdentityMode.KEYPAIR);
        if (metadata.getIdentityMode() == IdentityMode.CA) {
            X509Certificate peerCA = CertificateUtils.parseCertificate(ledgerRepository.getUserAccountSet().getAccount(currentNode.getAddress()).getCertificate());
            X509Certificate[] issuers = CertificateUtils.findIssuers(peerCA, CertificateUtils.parseCertificates(metadata.getLedgerCertificates()));
            // 校验根证书
            Arrays.stream(issuers).forEach(issuer -> CertificateUtils.checkCACertificate(issuer));
            CertificateUtils.checkValidityAny(issuers);
            // 校验节点证书
            CertificateUtils.checkCertificateRole(peerCA, CertificateRole.PEER);
            CertificateUtils.checkValidity(peerCA);
        }
        // 处于ACTIVED状态的参与方才会创建共识节点服务
        if (currentNode.getParticipantNodeState() == ParticipantNodeState.CONSENSUS) {
            ServerSettings serverSettings = provider.getServerFactory().buildServerSettings(ledgerHash.toBase58(), csSettings, currentNode.getAddress().toBase58(), bindingConfig.getSslSecurity(), bindingConfig.getExtraProperties());
            ((LedgerStateManager) consensusStateManager).setLatestStateId(ledgerRepository.retrieveLatestBlockHeight());
            Storage consensusRuntimeStorage = getConsensusRuntimeStorage(ledgerHash);
            server = provider.getServerFactory().setupServer(serverSettings, consensusMessageHandler, consensusStateManager, consensusRuntimeStorage);
            ledgerPeers.put(ledgerHash, server);
        }
        ledgerQuerys.put(ledgerHash, ledgerRepository);
        ledgerCurrNodes.put(ledgerHash, currentNode);
        ledgerCryptoSettings.put(ledgerHash, ledgerAdminAccount.getSettings().getCryptoSetting());
        ledgerKeypairs.put(ledgerHash, loadIdentity(currentNode, bindingConfig));
        bindingConfigs.put(ledgerHash, bindingConfig);
    } catch (Exception e) {
        ledgerManager.unregister(ledgerHash);
        throw e;
    }
    return server;
}
Also used : DbConnection(com.jd.blockchain.storage.service.DbConnection) X509Certificate(java.security.cert.X509Certificate) BusinessException(utils.BusinessException) BftsmartConsensusViewSettings(com.jd.blockchain.consensus.bftsmart.BftsmartConsensusViewSettings) LedgerStateManager(com.jd.blockchain.peer.consensus.LedgerStateManager) Storage(utils.io.Storage)

Aggregations

BftsmartConsensusViewSettings (com.jd.blockchain.consensus.bftsmart.BftsmartConsensusViewSettings)2 LedgerStateManager (com.jd.blockchain.peer.consensus.LedgerStateManager)2 BusinessException (utils.BusinessException)2 Storage (utils.io.Storage)2 DbConnection (com.jd.blockchain.storage.service.DbConnection)1 X509Certificate (java.security.cert.X509Certificate)1