import { logger } from '../util' ; import { NetworkCryptoConfigPeerOrgType , NetworkCreatePeerOrgType , NetworkPeerPortType } from '../model/type/network.type' ; import CryptoConfigYaml from '../model/yaml/network/cryptoConfigYaml' ; import PeerDockerComposeYaml from '../model/yaml/docker-compose/peerDockerComposeYaml' ; import PeerInstance from '../instance/peer' ; import ConnectionProfileYaml from '../model/yaml/network/connectionProfileYaml' ; import { InstanceTypeEnum } from '../instance/bdkFile' ; import ConfigtxYaml from '../model/yaml/network/configtx' ; import FabricTools from '../instance/fabricTools' ; import Channel from './channel' ; import { ChannelCreateChannelConfigComputeType } from '../model/type/channel.type' ; import { PeerUpType , PeerDownType , PeerAddType , PeerAddOrgToChannelType , PeerAddOrgToSystemChannelType } from '../model/type/peer.type' ; import { InfraRunnerResultType } from '../instance/infra/InfraRunner.interface' ; import { OrgPeerCreateType } from '../model/type/org.type' ; import { AbstractService } from './Service.abstract' ; import Org from './org' ; export default class Peer extends AbstractService { description public async up ( dto ) { logger . debug ( `Peer up: ${ dto . peerHostname }` ) ; return await ( new PeerInstance ( dto . peerHostname , this . config , this . infra ) ) . up ( ) ; } description public async down ( dto ) { logger . debug ( `Peer down: ${ dto . peerHostname }` ) ; return await ( new PeerInstance ( dto . peerHostname , this . config , this . infra ) . down ( ) ) ; } description returns public async cryptogen ( dto ) { const { peerOrgs } = dto ; logger . debug ( 'Peer create cryptogen' ) ; const cryptoConfigYaml = new CryptoConfigYaml ( ) ; const peerOrgCryptoConfigYaml = this . createCryptoConfigYaml ( peerOrgs ) ; if ( peerOrgCryptoConfigYaml && peerOrgCryptoConfigYaml . value . PeerOrgs ) { cryptoConfigYaml . setPeerOrgs ( peerOrgCryptoConfigYaml . value . PeerOrgs ) ; } this . bdkFile . createCryptoConfigYaml ( cryptoConfigYaml ) ; await ( new FabricTools ( this . config , this . infra ) ) . cryptogenGenerateCryptoConfig ( ) ; } description returns public async createPeerOrgConfigtxJSON ( dto ) { const { peerOrgs } = dto ; for ( const peerOrg of peerOrgs ) { logger . debug ( `Peer create configtx: ${ peerOrg . name }` ) ; const configtxYaml = new ConfigtxYaml ( ) ; const newOrg = configtxYaml . addPeerOrg ( { name : peerOrg . name , mspDir : `${ this . config . infraConfig . dockerPath }/peerOrganizations/${ peerOrg . domain }/msp` , domain : peerOrg . domain , anchorPeers : [ { hostname : `${ this . config . hostname }.${ this . config . orgDomainName }` , port : peerOrg ?. ports ?. [ + ( this . config . hostname . slice ( 4 , 0 ) ) ] ?. port } ] , } ) ; this . bdkFile . createConfigtxPeerOrg ( newOrg ) ; await ( new Org ( this . config , this . infra ) ) . createOrgDefinitionJson ( peerOrg . name , configtxYaml ) ; } } description returns public copyTLSCa ( dto ) { const { peerOrgs } = dto ; peerOrgs . forEach ( ( peerOrg ) => { logger . debug ( `Peer create copyTLSCa: ${ peerOrg . name }` ) ; for ( let i = 0 ; i < peerOrg . peerCount ; i ++ ) { this . bdkFile . copyPeerOrgTLSCa ( `peer${ i }` , peerOrg . domain ) ; } } ) ; } description returns public createConnectionProfileYaml ( dto ) { const { peerOrgs } = dto ; peerOrgs . forEach ( ( peerOrg ) => { logger . debug ( `Peer create connection config: ${ peerOrg . name }` ) ; const connectionProfileYaml = new ConnectionProfileYaml ( ) ; connectionProfileYaml . setName ( `${ this . config . networkName }-${ peerOrg . name }` ) ; connectionProfileYaml . setClientOrganization ( peerOrg . name ) ; for ( let i = 0 ; i < peerOrg . peerCount ; i ++ ) { connectionProfileYaml . addPeer ( peerOrg . name , `peer${ i }.${ peerOrg . domain }` , this . bdkFile . getPeerOrgTlsCertString ( i , peerOrg . domain ) , peerOrg . ports ?. [ i ] ?. port ) ; } this . bdkFile . createConnectionFile ( peerOrg . name , peerOrg . domain , connectionProfileYaml ) ; } ) ; } description returns public add ( dto ) { logger . debug ( 'Peer add' ) ; this . createPeerOrgDockerComposeYaml ( dto . orgName || this . config . orgName , dto . orgDomain || this . config . orgDomainName , dto . peerCount , dto . ports ) ; } description returns public createDockerCompose ( dto ) { const { peerOrgs } = dto ; peerOrgs . forEach ( ( peerOrg ) => { logger . debug ( `Peer create docker-compose: ${ peerOrg . name }` ) ; this . createPeerOrgDockerComposeYaml ( peerOrg . name , peerOrg . domain , peerOrg . peerCount , peerOrg . ports ) ; } ) ; } description param peerName param peerDomain param peerCount param ports - [ NetworkPeerPortType array] peer org 中 port 設定
     *  returns public createPeerOrgDockerComposeYaml ( peerName , peerDomain , peerCount , ports ? ) { for ( let i = 0 ; i < peerCount ; i ++ ) { const bootstrapPeerNumber = ( i + 1 ) % peerCount ; const peerDockerComposeYaml = new PeerDockerComposeYaml ( ) ; peerDockerComposeYaml . addNetwork ( this . config . networkName , { name : this . config . networkName , external : true } ) ; peerDockerComposeYaml . addPeer ( this . config , peerName , peerDomain , i , bootstrapPeerNumber , ports ?. [ bootstrapPeerNumber ] ?. port , ports ?. [ i ] ?. port , ports ?. [ i ] ?. operationPort , ports ?. [ i ] ?. isPublishPort , ports ?. [ i ] ?. isPublishOperationPort ) ; this . bdkFile . createDockerComposeYaml ( `peer${ i }.${ peerDomain }` , peerDockerComposeYaml ) ; this . bdkFile . createOrgConfigEnv ( `peer-peer${ i }.${ peerDomain }` , peerDockerComposeYaml . getPeerOrgEnv ( this . config , peerName , i , peerDomain , ports ?. [ i ] ?. port ) ) ; } } description returns public createCryptoConfigYaml ( cryptoConfigPeerOrg ) { const cryptConfigYaml = new CryptoConfigYaml ( ) ; cryptoConfigPeerOrg . forEach ( x => cryptConfigYaml . addPeerOrg ( { Name : x . name , Domain : x . domain , EnableNodeOUs : x . enableNodeOUs , Template : { Count : x . peerCount , } , Users : { Count : x . userCount , } , } ) ) ; return cryptoConfigPeerOrg . length === 0 ? null : cryptConfigYaml ; } description public async addOrgToChannel ( dto ) { await this . addOrgToChannelSteps ( ) . fetchChannelConfig ( dto ) ; await this . addOrgToChannelSteps ( ) . computeUpdateConfigTx ( dto ) ; } ignore public addOrgToChannelSteps ( ) { return { fetchChannelConfig : async ( dto ) : Promise < InfraRunnerResultType > => { logger . debug ( 'add org to channel step1 (fetchChannelConfig)' ) ; return await ( new Channel ( this . config , this . infra ) ) . fetchChannelConfig ( dto . channelName , this . config . orgType ) ; } , computeUpdateConfigTx : async ( dto ) => { logger . debug ( 'add org to channel step2 (orgConfigComputeUpdateAndSignConfigTx)' ) ; const { channelName , orgName } = dto ; const configBlock = await ( new Channel ( this . config , this . infra ) ) . getConfigBlock ( channelName ) ; this . bdkFile . createChannelConfigJson ( channelName , Channel . channelConfigFileName ( channelName ) . originalFileName , JSON . stringify ( configBlock ) ) ; const newOrg = JSON . parse ( this . bdkFile . getOrgConfigJson ( orgName ) ) ; configBlock . channel_group . groups . Application . groups = { ... configBlock . channel_group . groups . Application . groups , [ orgName ] : newOrg , } ; this . bdkFile . createChannelConfigJson ( channelName , Channel . channelConfigFileName ( channelName ) . modifiedFileName , JSON . stringify ( configBlock ) ) ; const channelCreateChannelConfigUpdate = { channelName , } ; return await ( new Channel ( this . config , this . infra ) ) . createChannelConfigSteps ( ) . computeUpdateConfigTx ( channelCreateChannelConfigUpdate ) ; } , } ; } description public async addOrgToSystemChannel ( dto ) { await this . addOrgToSystemChannelSteps ( ) . fetchChannelConfig ( dto ) ; await this . addOrgToSystemChannelSteps ( ) . computeUpdateConfigTx ( dto ) ; } ignore public addOrgToSystemChannelSteps ( ) { return { fetchChannelConfig : async ( dto ) : Promise < InfraRunnerResultType > => { logger . debug ( 'add org to system channel step1 (fetchChannelConfig)' ) ; return await ( new Channel ( this . config , this . infra ) ) . fetchChannelConfig ( dto . channelName , this . config . orgType , dto . orderer ) ; } , computeUpdateConfigTx : async ( dto ) => { logger . debug ( 'add org to system channel step2 (orgConfigComputeUpdateAndSignConfigTx)' ) ; const { channelName , orgName } = dto ; const configBlock = await ( new Channel ( this . config , this . infra ) ) . getConfigBlock ( channelName ) ; this . bdkFile . createChannelConfigJson ( channelName , Channel . channelConfigFileName ( channelName ) . originalFileName , JSON . stringify ( configBlock ) ) ; const newOrg = JSON . parse ( this . bdkFile . getOrgConfigJson ( orgName ) ) ; configBlock . channel_group . groups . Consortiums . groups . AllOrganizationsConsortium . groups = { ... configBlock . channel_group . groups . Consortiums . groups . AllOrganizationsConsortium . groups , [ orgName ] : newOrg , } ; this . bdkFile . createChannelConfigJson ( channelName , Channel . channelConfigFileName ( channelName ) . modifiedFileName , JSON . stringify ( configBlock ) ) ; const channelCreateChannelConfigUpdate = { channelName , } ; return await ( new Channel ( this . config , this . infra ) ) . createChannelConfigSteps ( ) . computeUpdateConfigTx ( channelCreateChannelConfigUpdate ) ; } , } ; } description returns public getPeerAddressList ( ) { const peerList = this . bdkFile . getDockerComposeList ( ) . peer ; return ( ( ) => { return peerList . map ( ( peer ) => { const peerDockerCompose = new PeerDockerComposeYaml ( this . bdkFile . getDockerComposeYaml ( peer , InstanceTypeEnum . peer ) ) ; return Object . values ( peerDockerCompose . value . services ) . map ( peerService => peerService . environment ?. find ( env => /CORE_PEER_ADDRESS=/ . test ( env ) ) ?. split ( '=' ) ?. [ 1 ] || '' ) . filter ( x => x ) [ 0 ] ; } ) ; } ) ( ) ; } }