Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace 'config-yaml/configtxOrgs.json' with 'config-yaml/orgs/[peer/orderer]-[orgName].json #17

Merged
merged 3 commits into from
Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 31 additions & 8 deletions src/instance/bdkFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import YAML from 'js-yaml'
import { parse, stringify } from 'envfile'
import CryptoConfigYaml from '../model/yaml/network/cryptoConfigYaml'
import ConnectionConfigYaml from '../model/yaml/network/connectionConfigYaml'
import ConfigtxYaml, { ConfigtxOrgs } from '../model/yaml/network/configtx'
import ConfigtxYaml, { ConfigtxOrgs, OrdererOrganizationInterface, PeerOrganizationInterface } from '../model/yaml/network/configtx'
import { ConfigEnvType } from '../model/type/config.type'
import OrdererDockerComposeYaml from '../model/yaml/docker-compose/ordererDockerComposeYaml'
import PeerDockerComposeYaml from '../model/yaml/docker-compose/peerDockerComposeYaml'
Expand Down Expand Up @@ -63,6 +63,10 @@ export default class BdkFile {
fs.mkdirSync(`${this.bdkPath}/config-yaml`, { recursive: true })
}

private createConfigYamlOrgsFolder () {
fs.mkdirSync(`${this.bdkPath}/config-yaml/orgs`, { recursive: true })
}

private createTlsFolder (orgDomainName: string) {
fs.mkdirSync(`${this.bdkPath}/tlsca/${orgDomainName}`, { recursive: true })
}
Expand All @@ -81,17 +85,36 @@ export default class BdkFile {
fs.writeFileSync(`${this.bdkPath}/config-yaml/configtx.yaml`, configtxYaml.getYamlString())
}

public createConfigtxOrgs (configtxOrgs: ConfigtxOrgs) {
this.createConfigYamlFolder()
fs.writeFileSync(`${this.bdkPath}/config-yaml/configtxOrgs.json`, JSON.stringify(configtxOrgs))
public createConfigtxPeerOrg (peerOrg: PeerOrganizationInterface) {
this.createConfigYamlOrgsFolder()
fs.writeFileSync(`${this.bdkPath}/config-yaml/orgs/peer-${peerOrg.Name}.json`, JSON.stringify(peerOrg))
}

public createConfigtxOrdererOrg (OrdererOrg: OrdererOrganizationInterface) {
this.createConfigYamlOrgsFolder()
fs.writeFileSync(`${this.bdkPath}/config-yaml/orgs/orderer-${OrdererOrg.Name}.json`, JSON.stringify(OrdererOrg))
}

public getOrdererServerCertToBase64 (hostname: string, domain: string) {
return fs.readFileSync(`${this.bdkPath}/ordererOrganizations/${domain}/orderers/${hostname}.${domain}/tls/server.crt`).toString('base64')
}

public getConfigtxOrgs (): ConfigtxOrgs {
return JSON.parse(fs.readFileSync(`${this.bdkPath}/config-yaml/configtxOrgs.json`).toString())
const configtxOrgs: ConfigtxOrgs = {
ordererOrgs: {},
peerOrgs: {},
}
fs.readdirSync(`${this.bdkPath}/config-yaml/orgs`).forEach((filename: string) => {
const peerOrg = filename.match(/(?<=^peer-).*(?=\.json$)/)?.[0]
const ordererOrg = filename.match(/(?<=^orderer-).*(?=\.json$)/)?.[0]
if (ordererOrg) {
configtxOrgs.ordererOrgs[ordererOrg] = JSON.parse(fs.readFileSync(`${this.bdkPath}/config-yaml/orgs/${filename}`).toString())
} else if (peerOrg) {
configtxOrgs.peerOrgs[peerOrg] = JSON.parse(fs.readFileSync(`${this.bdkPath}/config-yaml/orgs/${filename}`).toString())
}
},
)
return configtxOrgs
}

public copyOrdererOrgTLSCa (hostname: string, domain: string) {
Expand Down Expand Up @@ -184,17 +207,17 @@ export default class BdkFile {
fs.mkdirSync(`${this.bdkPath}/chaincode`, { recursive: true })
}

public createOrgConfigJson (name: string, orgJson: string) {
public createOrgDefinitionJson (name: string, orgJson: string) {
fs.mkdirSync(`${this.bdkPath}/org-json`, { recursive: true })
fs.writeFileSync(`${this.bdkPath}/org-json/${name}.json`, orgJson)
}

public createOrdererOrgConsenter (name: string, consenterJson: string) {
public createOrdererOrgConsenterJson (name: string, consenterJson: string) {
fs.mkdirSync(`${this.bdkPath}/org-json`, { recursive: true })
fs.writeFileSync(`${this.bdkPath}/org-json/${name}-consenter.json`, consenterJson)
}

public createExportOrgConfigJson (exportOrgJson: OrgJsonType, file: string) {
public createExportOrgDefinitionJson (exportOrgJson: OrgJsonType, file: string) {
const splitFilePath = file.split('/', 3)
for (let i = 0; i < splitFilePath.length - 1; i++) {
fs.mkdirSync(`${splitFilePath[i]}`, { recursive: true })
Expand Down
2 changes: 1 addition & 1 deletion src/instance/fabricTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export default class FabricTools extends AbstractInstance {
options)
}

public async createNewOrgConfigTx (orgName: string, options?: OptionsType): Promise<DockerResultType> {
public async printOrgDefinitionJson (orgName: string, options?: OptionsType): Promise<DockerResultType> {
const result = await this.infraRunCommand([
'configtxgen',
'-printOrg', `${orgName}`,
Expand Down
25 changes: 12 additions & 13 deletions src/model/prompts/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import Channel from '../../service/channel'
import { Config } from '../../config'
import Chaincode from '../../service/chaincode'
import { logger } from '../../util'
import { ConfigtxOrgs } from '../yaml/network/configtx'

export const joinedChannelChoice = async (channel: Channel): Promise<Choice[]> => {
const listJoinedChannelResult = await channel.listJoinedChannel()
Expand Down Expand Up @@ -44,18 +43,13 @@ export const getChaincodeList = (config: Config): {name: string; version: number
: []
}

const getConfigtxOrgsJson = (config: Config): ConfigtxOrgs => {
try {
const hostBasePath = `${config.infraConfig.bdkPath}/${config.networkName}`
return fs.existsSync(`${hostBasePath}/config-yaml/configtxOrgs.json`) ? JSON.parse(fs.readFileSync(`${hostBasePath}/config-yaml/configtxOrgs.json`).toString()) : { ordererOrgs: {}, peerOrgs: {} }
} catch {
return { ordererOrgs: {}, peerOrgs: {} }
}
}
export const getOrdererList = (config: Config): string[] => {
try {
const configtxOrgsJson = getConfigtxOrgsJson(config)
return Object.values(configtxOrgsJson.ordererOrgs).map(x => x.OrdererEndpoints).reduce((prev, curr) => (prev.concat(curr)), [])
const hostBasePath = `${config.infraConfig.bdkPath}/${config.networkName}`
const ordererOrgs = fs.readdirSync(`${hostBasePath}/config-yaml/orgs`)
.filter((filename: string) => (/^orderer-.*\.json$/.test(filename)))
.map((filename: string) => (JSON.parse(fs.readFileSync(`${hostBasePath}/config-yaml/orgs/${filename}`).toString())))
return ordererOrgs.map(x => x.OrdererEndpoints).reduce((prev, curr) => (prev.concat(curr)), [])
} catch {
return []
}
Expand All @@ -72,8 +66,13 @@ export const getChannelList = (config: Config): string[] => {

export const getOrgNames = (config: Config): string[] => {
try {
const configtxOrgsJson = getConfigtxOrgsJson(config)
return Object.keys(configtxOrgsJson.peerOrgs)
const hostBasePath = `${config.infraConfig.bdkPath}/${config.networkName}`
const orgs: string[] = []
fs.readdirSync(`${hostBasePath}/config-yaml/orgs`).forEach(fileName => {
const org = fileName.match(/(?<=^peer-).*(?=\.json$)/)?.[0]
org && orgs.push(org)
})
return orgs
} catch {
return []
}
Expand Down
13 changes: 5 additions & 8 deletions src/model/yaml/network/configtx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ interface OrganizationInterface {
ID: string // ID to load the MSP definition as
MSPDir: string // MSPDir is the filesystem path which contains the MSP configuration
}
interface OrdererOrganizationInterface extends OrganizationInterface {
export interface OrdererOrganizationInterface extends OrganizationInterface {
Policies: { // Policies defines the set of policies at this level of the config tree. For organization policies, their canonical path is usually /Channel/<Application|Orderer>/<OrgName>/<PolicyName>
Readers: PolicyInterface
Writers: PolicyInterface
Admins: PolicyInterface
}
OrdererEndpoints: string[]
}
interface PeerOrganizationInterface extends OrganizationInterface {
export interface PeerOrganizationInterface extends OrganizationInterface {
Policies: { // Policies defines the set of policies at this level of the config tree. For organization policies, their canonical path is usually /Channel/<Application|Orderer>/<OrgName>/<PolicyName>
Readers: PolicyInterface
Writers: PolicyInterface
Expand Down Expand Up @@ -270,6 +270,7 @@ class ConfigtxYaml extends BdkYaml<ConfigtxInterface> {

this.ordererOrgs[payload.name] = newOrdererOrg
this.value.Organizations.push(newOrdererOrg)
return newOrdererOrg
}

public addPeerOrg (payload: { name: string; mspDir: string; domain: string; anchorPeers: {hostname: string; port?: number}[]}) {
Expand All @@ -281,7 +282,7 @@ class ConfigtxYaml extends BdkYaml<ConfigtxInterface> {
// ports -> [7051, 7151]
const newPeerOrg: PeerOrganizationInterface = {
Name: payload.name,
ID: `${payload.name}`,
ID: payload.name,
MSPDir: payload.mspDir,
Policies: {
Readers: {
Expand All @@ -306,6 +307,7 @@ class ConfigtxYaml extends BdkYaml<ConfigtxInterface> {

this.peerOrgs[payload.name] = newPeerOrg
this.value.Organizations.push(newPeerOrg)
return newPeerOrg
}

public addSystemChannelProfile (payload: { name: string; etcdRaftConsenters: EtcdRaftConsentersInterface[]; ordererOrgs: string[]; consortiums: { [cousortiumName: string]: string[] }; batchTimeout?: string; BatchSize?: BatchSizeInterface }) {
Expand Down Expand Up @@ -373,11 +375,6 @@ class ConfigtxYaml extends BdkYaml<ConfigtxInterface> {
}
}

public exportOrgs () {
const configtxOrgs: ConfigtxOrgs = { ordererOrgs: this.ordererOrgs, peerOrgs: this.peerOrgs }
return configtxOrgs
}

public importOrgs (data: ConfigtxOrgs) {
this.ordererOrgs = data.ordererOrgs
this.peerOrgs = data.peerOrgs
Expand Down
14 changes: 0 additions & 14 deletions src/service/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,20 +272,6 @@ export default class Channel extends AbstractService {
}
}

/**
* @description 由 config.yaml 建立 peer org 的 json 檔案
* @param orgName - peer org 的名稱
* @returns 在 blockchain network 資料夾底下 org-json/[peer org 名稱].json 檔案
*/
// create new org configtx yaml
public async createNewOrgConfigTx (orgName: string) {
logger.info(`[*] Generate ${orgName} config json file: configtxgen ${this.config.infraConfig.bdkPath}/${this.config.networkName}/${orgName}/${orgName}.json`)

const orgJson = (await (new FabricTools(this.config, this.infra)).createNewOrgConfigTx(orgName)).stdout.match(/{.*}/s)?.[0] || ''

this.bdkFile.createOrgConfigJson(orgName, orgJson)
}

/**
* fetch channel config to artifact/${channelName}/${channelName}_config_bock.pb
*/
Expand Down
7 changes: 4 additions & 3 deletions src/service/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,14 @@ export default class Network extends AbstractService {
const etcdRaftConsenters: { Host: string; Port: number; ClientTLSCert: string; ServerTLSCert: string }[] = []

dto.ordererOrgs.forEach(ordererOrg => {
configtxYaml.addOrdererOrg({
const newOrg = configtxYaml.addOrdererOrg({
name: ordererOrg.name,
mspDir: `${this.config.infraConfig.dockerPath}/ordererOrganizations/${ordererOrg.domain}/msp`,
domain: ordererOrg.domain,
hostname: ordererOrg.hostname,
ports: ordererOrg.ports?.map(x => x.port),
})
this.bdkFile.createConfigtxOrdererOrg(newOrg)

ordererOrg.hostname.forEach((hostname, i) => {
etcdRaftConsenters.push({
Expand All @@ -219,12 +220,13 @@ export default class Network extends AbstractService {
})

dto.peerOrgs.forEach(peerOrg => {
configtxYaml.addPeerOrg({
const newOrg = configtxYaml.addPeerOrg({
name: peerOrg.name,
mspDir: `${this.config.infraConfig.dockerPath}/peerOrganizations/${peerOrg.domain}/msp`,
domain: peerOrg.domain,
anchorPeers: [{ hostname: `peer0.${peerOrg.domain}`, port: peerOrg?.ports?.[0]?.port }],
})
this.bdkFile.createConfigtxPeerOrg(newOrg)
})

configtxYaml.addSystemChannelProfile({
Expand All @@ -237,7 +239,6 @@ export default class Network extends AbstractService {
})

this.bdkFile.createConfigtx(configtxYaml)
this.bdkFile.createConfigtxOrgs(configtxYaml.exportOrgs())

return configtxYaml
}
Expand Down
12 changes: 5 additions & 7 deletions src/service/orderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { OrdererAddType, ConsenterType, OrdererUpType, OrdererAddOrgToChannelTyp
import { InfraRunnerResultType } from '../instance/infra/InfraRunner.interface'
import { OrgOrdererCreateType } from '../model/type/org.type'
import { AbstractService } from './Service.abstract'
import Org from './org'

export default class Orderer extends AbstractService {
/**
Expand Down Expand Up @@ -62,16 +63,17 @@ export default class Orderer extends AbstractService {

const ports = ordererOrg.ports?.map(port => port.port)

configtxYaml.addOrdererOrg({
const newOrg = configtxYaml.addOrdererOrg({
name: ordererOrg.name,
mspDir: `${this.config.infraConfig.dockerPath}/ordererOrganizations/${ordererOrg.domain}/msp`,
domain: ordererOrg.domain,
hostname: ordererOrg.hostname,
ports,
})
this.bdkFile.createConfigtxOrdererOrg(newOrg)
await (new Org(this.config, this.infra)).createOrgDefinitionJson(ordererOrg.name, configtxYaml)

const ordererOrgConsenter: ConsenterType[] = []

ordererOrg.hostname.forEach((hostname, index) => {
const serverCertBase64 = this.bdkFile.getOrdererServerCertToBase64(hostname, ordererOrg.domain)

Expand All @@ -82,11 +84,7 @@ export default class Orderer extends AbstractService {
serverTlsCert: serverCertBase64,
})
})

this.bdkFile.createConfigtx(configtxYaml)
this.bdkFile.createOrdererOrgConsenter(ordererOrg.name, JSON.stringify(ordererOrgConsenter))

await (new Channel(this.config, this.infra)).createNewOrgConfigTx(ordererOrg.name)
this.bdkFile.createOrdererOrgConsenterJson(ordererOrg.name, JSON.stringify(ordererOrgConsenter))
}
}

Expand Down
21 changes: 19 additions & 2 deletions src/service/org.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { logger } from '../util'
import { OrgJsonType } from '../model/type/org.type'
import { AbstractService } from './Service.abstract'
import FabricTools from '../instance/fabricTools'
import ConfigtxYaml from '../model/yaml/network/configtx'

export default class Org extends AbstractService {
/**
Expand All @@ -10,7 +12,7 @@ export default class Org extends AbstractService {
data.forEach(org => {
logger.info(`[*] Import org config: ${org.name}`)

this.bdkFile.createOrgConfigJson(org.name, org.json)
this.bdkFile.createOrgDefinitionJson(org.name, org.json)
})
}

Expand All @@ -25,6 +27,21 @@ export default class Org extends AbstractService {
json: orgJson,
}

this.bdkFile.createExportOrgConfigJson(exportPeerOrgJson, path)
this.bdkFile.createExportOrgDefinitionJson(exportPeerOrgJson, path)
}

/**
* @description 由 config.yaml 建立 peer org 的 json 檔案
* @param orgName - peer org 的名稱
* @returns 在 blockchain network 資料夾底下 org-json/[peer org 名稱].json 檔案
*/
// create new org configtx yaml
public async createOrgDefinitionJson (orgName: string, configtxYaml: ConfigtxYaml) {
logger.info(`[*] Generate ${orgName} config json file: configtxgen ${this.config.infraConfig.bdkPath}/${this.config.networkName}/org-json/${orgName}.json`)

this.bdkFile.createConfigtx(configtxYaml)
const orgJson = (await (new FabricTools(this.config, this.infra)).printOrgDefinitionJson(orgName)).stdout.match(/{.*}/s)?.[0] || ''

this.bdkFile.createOrgDefinitionJson(orgName, orgJson)
}
}
9 changes: 4 additions & 5 deletions src/service/peer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { PeerUpType, PeerDownType, PeerAddType, PeerAddOrgToChannelType, PeerApp
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 {
/**
Expand Down Expand Up @@ -63,16 +64,14 @@ export default class Peer extends AbstractService {
for (const peerOrg of peerOrgs) {
logger.info(`[*] Peer create configtx: ${peerOrg.name}`)

configtxYaml.addPeerOrg({
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.createConfigtx(configtxYaml)

await (new Channel(this.config, this.infra)).createNewOrgConfigTx(peerOrg.name)
this.bdkFile.createConfigtxPeerOrg(newOrg)
await (new Org(this.config, this.infra)).createOrgDefinitionJson(peerOrg.name, configtxYaml)
}
}

Expand Down
6 changes: 5 additions & 1 deletion test/service/network.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@
// await network.create(networkCreateDto)
// let files_should_exist = [
// 'config-yaml/configtx.yaml',
// 'config-yaml/configtxOrgs.json',
// 'config-yaml/orgs/peer-Ben.json',
// 'config-yaml/orgs/peer-Grace.json',
// 'config-yaml/orgs/peer-Eugene.json',
// 'config-yaml/orgs/orderer-BenOrderer.json',
// 'config-yaml/orgs/orderer-GraceOrderer.json',
// 'config-yaml/crypto-config.yaml',
// 'docker-compose/docker-compose-orderer-orderer0.ben.cathaybc.com.yaml',
// 'docker-compose/docker-compose-orderer-orderer1.ben.cathaybc.com.yaml',
Expand Down