Skip to content

Commit

Permalink
bdk org peer add-system-channel (#18)
Browse files Browse the repository at this point in the history
* fetchChannelConfig, fetchChannelBlock0, fetchChannelNewestBlock use same input

* refactor peer/orderer add: use 'getConfigBlock'

* add peer to system channel

* update EXAMPLE.md

* update help document
  • Loading branch information
kth-tw authored Dec 13, 2021
1 parent 5d22d22 commit 5d29707
Show file tree
Hide file tree
Showing 11 changed files with 305 additions and 94 deletions.
29 changes: 29 additions & 0 deletions cicd/test_script/crypto-gen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,35 @@ bdk chaincode query -C ryan -n ${CHAINCODE_NAME} -f QueryCar -a CAR_EUGENE_PEER0

# ==================================================================

# [BenOrderer] Eric org add in system-channel
export_env 'orderer' 'BenOrderer' ${ORDERER_ORG_DOMAIN_BEN} 'orderer0'
bdk org peer add-system-channel -o orderer0.${ORDERER_ORG_DOMAIN_BEN}:7050 -n Eric
sleep 2
bdk org orderer approve -c ${SYSTEM_CHANNEL_NAME}
sleep 2

# [GraceOrderer] Orderer org approve in system-channel
export_env 'orderer' 'GraceOrderer' ${ORDERER_ORG_DOMAIN_GRACE} 'orderer0'
bdk org orderer approve -c ${SYSTEM_CHANNEL_NAME}
sleep 2

# [BenOrderer] Orderer org update in system-channel
export_env 'orderer' 'BenOrderer' ${ORDERER_ORG_DOMAIN_BEN} 'orderer0'
bdk org orderer update -o orderer0.${ORDERER_ORG_DOMAIN_BEN}:7050 -c ${SYSTEM_CHANNEL_NAME}
sleep 2

# [Eric] Create channel
export_env 'peer' 'Eric' ${PEER_ORG_DOMAIN_ERIC} 'peer0'
bdk channel create -n ryan87 --orderer orderer0.${ORDERER_ORG_DOMAIN_BEN}:7050 -o Ben -o Grace -o Eugene -o Eric --channelAdminPolicyStyle "Any-Member-in-Channel"
sleep 2

bdk channel join -n ryan87 --orderer orderer1.${ORDERER_ORG_DOMAIN_BEN}:7150
bdk channel update-anchorpeer -n ryan87 --orderer orderer1.${ORDERER_ORG_DOMAIN_BEN}:7150 -p 7051

# ==================================================================



# [EricOrderer] Orderer org create
bdk org orderer create -f cicd/test_script/org-orderer-create.json --genesis-file-name newest_genesis --create-full

Expand Down
48 changes: 38 additions & 10 deletions docs/EXAMPLE-EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,11 +295,6 @@ bdk chaincode invoke -C test -n fabcar -f CreateCar -a CAR_ORG2_PEER0 -a BMW -a
# Query data
bdk chaincode query -C test -n fabcar -f QueryCar -a CAR_ORG2_PEER0
```
<!--
=================================
|| Translated Up to This Point ||
=================================
-->

## Add New Channel

Expand Down Expand Up @@ -347,7 +342,6 @@ bdk channel join -n test --orderer orderer0.example.com:7050

Update settings for Org1 and Org2 on the application channel named *test*. Note that when updating the settings in *~/.bdk/.env*, *BDK_ORG_NAME* and *BDK_ORG_DOMAIN* have to be editted as well.


```bash
# export BDK_ORG_NAME='Org1'
# export BDK_ORG_DOMAIN='org1.example.com'
Expand All @@ -368,7 +362,6 @@ bdk channel update-anchorpeer -n test --orderer orderer0.example.com:7050

First, we need to prepare a file named *org-peer-create.json*, with the required variables. We then use `cryptogen` to generate the certificates and keys required for the peer organization. Next, we add the Org3(the new peer org) to the settings file for the application channel. Last, we start the peers in Org3 and add it to the `test` channel. Test transactions and queries should be successful at this point.


### Prepare settings files

*org-peer-create.json* contains parameters required for generating *configtx.yaml*, *crypto-config.yaml*, and *docker-compose.yaml*. Place the file in the current working directory.
Expand Down Expand Up @@ -457,7 +450,42 @@ Start Org3 peer containers
bdk peer up -n peer0.org3.example.com -n peer1.org3.example.com
```

### Step 4:Add Org3 to channel
### Step 4: Add Org3 to system-channel
Add Org3 to system channel with Org1Orderer

```bash
# export BDK_ORG_TYPE='orderer'
# export BDK_ORG_NAME='Org1Orderer'
# export BDK_ORG_DOMAIN='org1.example.com'
# export BDK_HOSTNAME='orderer0'

bdk org peer add-system-channel -o orderer0.org1.example.com:7050 -n Eric
bdk org orderer approve -c system-channel
```

Approve system-channel change with Org1Orderer
```bash
# export BDK_ORG_TYPE='orderer'
# export BDK_ORG_NAME='Org2Orderer'
# export BDK_ORG_DOMAIN='org2.example.com'
# export BDK_HOSTNAME='orderer0'

bdk org peer add-system-channel -o orderer0.org1.example.com:7050 -n Eric
bdk org orderer approve -c system-channel
```

update system-channel with Org1Orderer

```bash
# export BDK_ORG_TYPE='orderer'
# export BDK_ORG_NAME='Org1Orderer'
# export BDK_ORG_DOMAIN='org1.example.com'
# export BDK_HOSTNAME='orderer0'

bdk org orderer update -o orderer0.org1.example.com:7050 -c system-channel
```

### Step 5:Add Org3 to channel

Add Org3 to the application channel named *test*. Since each peer is added individually to the application channel, changes to variables *BDK_ORG_NAME*, *BDK_ORG_DOMAIN*, and *BDK_HOSTNAME* in *~/.bdk/.env* are required every time.

Expand All @@ -475,7 +503,7 @@ bdk channel join -n test --orderer orderer0.example.com:7050
bdk channel join -n test --orderer orderer0.example.com:7050
```

### Step 5:Deploy chaincode on Org3
### Step 6:Deploy chaincode on Org3

Install and approve the chaincode named fabcar_1. Since we are using the blockchain network from before, we only need to do `peer chaincode lifecycle approveformyorg` up to this step. We can use the `-a` parameter to restrict the deployment of the chaincode lifecycle, and require the chaincode to be initialized with parameter ``-I`. We then install the chaincode named fabcar_1 on peer1 of Org3.

Expand All @@ -495,7 +523,7 @@ bdk chaincode deploy -C test -l fabcar_1 -I -a --orderer orderer0.example.com:70
bdk chaincode install -l fabcar_1
```

### Step 6:Initiate and query a transaction on Org3
### Step 7:Initiate and query a transaction on Org3

Initiate a chaincode transaction on fabcar_1 with `bdk chaincode invoke`. We use the `-f` parameter to select the function used to initialize the chaincode, and `-a` parameter to pass in the variables for the chaincode function. We can then query the chaincode with `bdk chaincode query`.

Expand Down
41 changes: 38 additions & 3 deletions docs/EXAMPLE.md
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,42 @@ bdk org peer add -o orderer0.org1.example.com:7050 -c test -n Eric
bdk peer up -n peer0.org3.example.com -n peer1.org3.example.com
```

### Step 4:Org3 加入 Channel
### Step 4:Org3 加入 system-channel
由 Org1Orderer 組織身份將 Org3 加入 System Channel 並同意

```bash
# export BDK_ORG_TYPE='orderer'
# export BDK_ORG_NAME='Org1Orderer'
# export BDK_ORG_DOMAIN='org1.example.com'
# export BDK_HOSTNAME='orderer0'

bdk org peer add-system-channel -o orderer0.org1.example.com:7050 -n Eric
bdk org orderer approve -c system-channel
```

由 Org2Orderer 組織身份同意 system-channel 的更新
```bash
# export BDK_ORG_TYPE='orderer'
# export BDK_ORG_NAME='Org2Orderer'
# export BDK_ORG_DOMAIN='org2.example.com'
# export BDK_HOSTNAME='orderer0'

bdk org peer add-system-channel -o orderer0.org1.example.com:7050 -n Eric
bdk org orderer approve -c system-channel
```

由 Org1Orderer 組織身份更新 system-channel

```bash
# export BDK_ORG_TYPE='orderer'
# export BDK_ORG_NAME='Org1Orderer'
# export BDK_ORG_DOMAIN='org1.example.com'
# export BDK_HOSTNAME='orderer0'

bdk org orderer update -o orderer0.org1.example.com:7050 -c system-channel
```

### Step 5:Org3 加入 Channel

Org1 加入名稱為 *test* 的 Application Channel,由於加入 Application Chanel 是以 Peer 單位加入,所以每次加入都要記得更改在 *~/.bdk/.env**BDK_ORG_NAME 、 BDK_ORG_DOMAIN**BDK_HOSTNAME* 的設定

Expand All @@ -468,7 +503,7 @@ bdk channel join -n test --orderer orderer0.example.com:7050
bdk channel join -n test --orderer orderer0.example.com:7050
```

### Step 5:Org3 部署 Chaincode
### Step 6:Org3 部署 Chaincode

安裝並且同意標籤名稱為 fabcar_1 的 Chaincode,由於使用前面的 Blockchain network,所以此次只做到 `peer chaincode lifecycle approveformyorg`,使用 `-a` 來限制 Lifecycle chaincode 的部署 Chaincode 步驟只做到 `peer chaincode lifecycle approveformyorg` ,使用 `-I` 來標示此次 Chaincode 需要初始化才能使用,之後在 Org3 的 peer1 安裝名稱為 fabcar_1 的 Chaincode

Expand All @@ -488,7 +523,7 @@ bdk chaincode deploy -C test -l fabcar_1 -I -a --orderer orderer0.example.com:70
bdk chaincode install -l fabcar_1
```

### Step 6:Org3 發起交易且查詢
### Step 7:Org3 發起交易且查詢

使用 `bdk chaincode invoke` 和名稱為 fabcar_1 的 Chaincode 發起交易,使用 `-f` 選擇 Chaincode 上初始化的 function,使用 `-a` 輸入 Chainocde function 所需要的參數,之後可以使用 `bdk chaincode query` 和 Chaincode 查詢資訊

Expand Down
71 changes: 71 additions & 0 deletions src/command/org/peer/add-system-channel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import config from '../../../config'
import prompts from 'prompts'
import { Arguments, Argv } from 'yargs'
import { onCancel, ParamsError } from '../../../util'
import Peer from '../../../service/peer'
import { getOrdererList } from '../../../model/prompts/util'

export const command = 'add-system-channel'

export const desc = '加入新 Peer org 在 System Channel 中'

interface OptType {
interactive: boolean
orderer: string
channelName: string
peerOrgName: string
}

const ordererList = getOrdererList(config)

export const builder = (yargs: Argv<OptType>) => {
return yargs
.example('bdk org peer add-system-channel --interactive', 'Cathay BDK 互動式問答')
.example('bdk org peer add-system-channel --orderer orderer0.example.com:7050 --peer-org-name Org1', '使用 orderer0.example.com:7050 將 Org1 加入 System channel')
.option('interactive', { type: 'boolean', description: '是否使用 Cathay BDK 互動式問答', alias: 'i' })
.option('peer-org-name', { type: 'string', description: '欲加入 Channel 中 Peer Org 的名稱', alias: 'n' })
.option('orderer', { type: 'string', choices: ordererList, description: '選擇使用的 Orderer', alias: 'o' })
}

export const handler = async (argv: Arguments<OptType>) => {
const peer = new Peer(config)

const peerOrgName: string = await (async () => {
if (argv.interactive) {
return (await prompts([
{
type: 'text',
name: 'name',
message: 'What is peer org name?',
initial: 'Test',
},
], { onCancel })).name
} else if (argv.peerOrgName) {
return argv.peerOrgName
} else {
throw new ParamsError('Invalid params: Required parameter <peer-org-name> missing')
}
})()

const orderer: string = await (async () => {
if (argv.interactive) {
return (await prompts([
{
type: 'select',
name: 'orderer',
message: 'Ordering service endpoint',
choices: ordererList.map(x => ({
title: x,
value: x,
})),
},
], { onCancel })).orderer
} else if (argv.orderer) {
return argv.orderer
} else {
throw new ParamsError('Invalid params: Required parameter <orderer> missing')
}
})()

await peer.addOrgToSystemChannel({ channelName: 'system-channel', orgName: peerOrgName, orderer })
}
2 changes: 1 addition & 1 deletion src/command/org/peer/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const ordererList = getOrdererList(config)
export const builder = (yargs: Argv<OptType>) => {
return yargs
.example('bdk org peer update --interactive', 'Cathay BDK 互動式問答')
.example('bdk org peer update --peer peer0.example.com:7050 --channel-name test', '使用 peer0.example.com:7050 在名稱 test 的 channel 更新')
.example('bdk org peer update --orderer orderer0.example.com:7050 --channel-name test', '使用 orderer0.example.com:7050 在名稱 test 的 channel 更新')
.option('interactive', { type: 'boolean', description: '是否使用 Cathay BDK 互動式問答', alias: 'i' })
.option('orderer', { type: 'string', choices: ordererList, description: '選擇使用的 Orderer', alias: 'o' })
.option('channel-name', { type: 'string', choices: channelList, description: 'Channel 的名稱', alias: 'c' })
Expand Down
Loading

0 comments on commit 5d29707

Please sign in to comment.