Hyperledger Fabric,Debug, orderer

To debug the fabric source code, let's debug the orderer node first.

Prepare in advance

ubuntu system (I am a dual system of win10 and ubuntu)

Fabric that can run (I use version 1.2)

IDE:goland

Debug file preparation

Let me talk about where my fabric is: /home/xxx/go/src/github.com/hyperledger/fabric

1. Put all the files we need for debugging in one file. Create a new directory work_dir under /fabric

cd go/src/github.com/hyperledger/fabric
mkdir work_dir

2.1. Copy the required configuration, bin tool, and shell file to work_dir

Let's show everyone the final look

Now start copying bit by bit:

Copy the contents of the 4 folders /bin, /chaincode, /config, and /first-network under fabric/scripts/fabric-samples to the corresponding folders under /fabric/work_dir.

2.2 Verify the copied configuration and tools

cd 
cd go/src/github.com/hyperledger/fabric/work_dir/first-network
./byfn.sh down
./byfn.sh up

Can print out the result as normal, that’s right

The environment variables in the configuration file are migrated to the IDE

1. Select main.go of orderer in ide

Click: Run->Debug->Edit Configurations

edit:

Run Kind: Package
Package path: github.com/hyperledger/fabric/orderer
Output directory: /home/xxx/go/src/github.com/hyperledger/fabric/work_dir/bin
Working directory: /home/xxx/go/src/github.com/hyperledger/fabric/work_dir/first-network

Environment:

When running the fabric normally, the environment variables are all written in the yaml file of docker-compose, there are three in total. Byfn uses docker-compose-cli.yaml, base/docker-compose-base.yaml, base/peer-base.yaml.

The environment variables of orderer are all in base/docker-compose-base.yaml

The environment variable paths in the yaml file are all mapped paths. Replace the part after the colon with the part before each colon in volumes, and remember to add first-network to the new path. The modified content is as follows (don't need to move the yaml file, copy it out and change it, we will paste it into the ide later)

Also add a FABRIC_CFG_PATH=../config/ (no error will be reported)

ORDERER_GENERAL_LOGLEVEL=INFO
ORDERER_GENERAL_LISTENADDRESS=0.0.0.0
ORDERER_GENERAL_GENESISMETHOD=file
ORDERER_GENERAL_GENESISFILE=../first-network/channel-artifacts/genesis.block
ORDERER_GENERAL_LOCALMSPID=OrdererMSP
ORDERER_GENERAL_LOCALMSPDIR=../first-network/crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/msp
ORDERER_GENERAL_TLS_ENABLED=true
ORDERER_GENERAL_TLS_PRIVATEKEY=../first-network/crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/tls/server.key
ORDERER_GENERAL_TLS_CERTIFICATE=../first-network/crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/tls/server.crt
ORDERER_GENERAL_TLS_ROOTCAS=[../first-network/crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/tls/ca.crt]
FABRIC_CFG_PATH=../config/   #注意这行,原来没有,debug要加上

Then copy these to the place where we just set the debug configuration in the ide (Run->Debug->Edit Configurations), click directly on the small picture (paste) behind Environment, and the environment variables will be written in.

Change orderer's config

vi work_dir/config/orderer.yaml

Find FileLedger.Location. It turned out to be the mapped path, changed to. . . /fabric/work_dir/orderer as shown above

改docker-compose-cli.yaml

Comment out the orderer part

Also note the orderer part of depends_on of service.cli

Then, add an orderer's external ip mapping extra_hosts to all other container parts in this file, like this:

Check your own ip like this: enter ifconfig, or ip a, or hostname -I in the terminal (this is the most convenient, the output is very short, the first is the desired ip)

Start the network

Clean up the previous network first

cd work_dir/first-network
./byfn.sh down    # 清理orderer的FileLedger目录,每次网络重置都需要做这步,orderer的一些内容脚本是清不掉的
rm -rf ../orderer/*     
./byfn generate

docker-compose -f docker-compose-cli.yaml up -d  # 启动容器

Start debugging

Interrupt the point in the source code and click the green bug debug. Can see filepath=. . . That's it. Like the picture below.

Then we go to the cli terminal (docker exec -it cli bash) to write a peer command

cd work_dir/first-network
docker exec -it cli bash
# 发送channel创建请求
export CHANNEL_NAME=mychannel
peer channel create -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/channel.tx --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

You can see the value at the ide interruption point.

success! ! ! I will debug the peer in two days

Guess you like

Origin blog.csdn.net/wwqcherry/article/details/105925919