# 私链搭建（Ubuntu）

## 安装需要的软件

```bash
#ubuntu18
sudo apt-get install curl docker docker-compose golang nodejs npm python
```

| 软件             | 版本          |
| -------------- | ----------- |
| docker         | 17.06.2-ce+ |
| docker-compose | 1.14.0+     |
| go             | 1.9.x+      |
| nodejs         | 8.9.x       |
| bon            | 5.6.0       |
| python         | 2.7.x       |

## 设置环境变量

```bash
#~/.bashrc
export GOPATH=$HOME/gopath
export PATH=$PATH:$GOPATH/bin
```

## 准备环境

```bash
#下载脚本
wget https://raw.githubusercontent.com/hyperledger/fabric/master/scripts/bootstrap.sh

#运行脚本，可以考虑将里面的docker命令前增加su
./bootstrap.sh
```

## 运行私链

```bash
cd fabric-samples/first-network

#初始化配置
./byfn.sh -m generate

#这个脚本，做了以下事情：
#创建证书
#生成第一个区块
#生成MSP的锚节点
#启动管道mychannel
#管道包括了两个组织，org1和org2
#org1有两个节点peer0,peer1
#org2有两个节点peer0,peer1
#然后在org1和org2的peer0节点部署了智能合约
#查询合约
#合约转账
#查询合约
sudo ./byfn.sh -m up
##### Generate certificates using cryptogen tool #########
#########  Generating Orderer Genesis block ##############
#######    Generating anchor peer update for Org1MSP   ##########
#######    Generating anchor peer update for Org2MSP   ##########
===================== Channel "mychannel" is created successfully =====================
===================== peer0.org1 joined on the channel "mychannel" =====================
===================== peer1.org1 joined on the channel "mychannel" =====================
===================== peer0.org2 joined on the channel "mychannel" =====================
===================== peer1.org2 joined on the channel "mychannel" =====================
===================== Anchor peers for org "Org1MSP" on "mychannel" is updated successfully =====================
===================== Anchor peers for org "Org2MSP" on "mychannel" is updated successfully =====================
===================== Chaincode is installed on peer0.org1 =====================
===================== Chaincode is installed on peer0.org2 =====================
===================== Chaincode Instantiation on peer0.org2 on channel 'mychannel' is successful =====================
===================== Querying on peer0.org1 on channel 'mychannel'... =====================
Query Result: 100
===================== Query on peer0.org1 on channel 'mychannel' is successful =====================
===================== Invoke transaction on peer0.org1 on channel 'mychannel' is successful =====================
===================== Chaincode is installed on peer1.org2 =====================
===================== Querying on peer1.org2 on channel 'mychannel'... =====================
Query Result: 90
===================== Query on peer1.org2 on channel 'mychannel' is successful =====================
========= All GOOD, BYFN execution completed ===========
```

## 查看容器列表

```bash
sudo docker ps
CONTAINER ID        IMAGE                                                                                                  COMMAND                  CREATED             STATUS              PORTS                                              NAMES
92b34725eb84        dev-peer1.org2.example.com-mycc-1.0-26c2ef32838554aac4f7ad6f100aca865e87959c9a126e86d764c8d01f8346ab   "chaincode -peer.add…"   14 minutes ago      Up 14 minutes                                                          dev-peer1.org2.example.com-mycc-1.0
fc09cb911752        dev-peer0.org1.example.com-mycc-1.0-384f11f484b9302df90b453200cfb25174305fce8f53f4e94d45ee3b6cab0ce9   "chaincode -peer.add…"   15 minutes ago      Up 15 minutes                                                          dev-peer0.org1.example.com-mycc-1.0
bed1373228f0        dev-peer0.org2.example.com-mycc-1.0-15b571b3ce849066b7ec74497da3b27e54e0df1345daff3951b94245ce09c42b   "chaincode -peer.add…"   15 minutes ago      Up 15 minutes                                                          dev-peer0.org2.example.com-mycc-1.0
bfa16375cae2        hyperledger/fabric-tools:latest                                                                        "/bin/bash"              16 minutes ago      Up 16 minutes                                                          cli
97bbdad5a204        hyperledger/fabric-orderer:latest                                                                      "orderer"                16 minutes ago      Up 16 minutes       0.0.0.0:7050->7050/tcp                             orderer.example.com
9743b8716888        hyperledger/fabric-peer:latest                                                                         "peer node start"        16 minutes ago      Up 16 minutes       0.0.0.0:10051->7051/tcp, 0.0.0.0:10053->7053/tcp   peer1.org2.example.com
d3ea4a45d1f3        hyperledger/fabric-peer:latest                                                                         "peer node start"        16 minutes ago      Up 16 minutes       0.0.0.0:8051->7051/tcp, 0.0.0.0:8053->7053/tcp     peer1.org1.example.com
514337d43df4        hyperledger/fabric-peer:latest                                                                         "peer node start"        16 minutes ago      Up 16 minutes       0.0.0.0:7051->7051/tcp, 0.0.0.0:7053->7053/tcp     peer0.org1.example.com
3ced253d6808        hyperledger/fabric-peer:latest              
```

## 查看交易细节

```bash
sudo docker logs dev-peer0.org2.example.com-mycc-1.0
ex02 Init
Aval = 100, Bval = 200

sudo docker logs dev-peer0.org1.example.com-mycc-1.0
ex02 Invoke
Query Response:{"Name":"a","Amount":"100"}
ex02 Invoke
Aval = 90, Bval = 210

sudo docker logs dev-peer1.org2.example.com-mycc-1.0
ex02 Invoke
Query Response:{"Name":"a","Amount":"90"}
```

## 清理工作

```bash
sudo ./byfn.sh -m down
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://neochain.gitbook.io/project/fabric/si-lian-da-jian-ubuntu.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
