如何搭建一个Corda网络

在查看此文档之前,先查看如何开发一个CorDapp

参考文档:NMS的FAQ.md文件

1、网络服务启动,docker版的网络服务启动:

1
docker run --name=network-map -e NMS_ROOT_CA_FILE_PATH="" -p 8080:8080 cordite/network-map:latest

2、启动后打开接口文档用于验证启动是否成功

http://localhost:8080/swagger/#/

image-20201207175926945

3、build和配置CorDapp

build之前修改build.gradle的配置,修改cordapp节点不给cordapp签名

1
2
3
4
5
6
cordapp {
signing {
enabled false
}
// 其他配置
}

运行build命令

1
./gradlew clean deployNodes

修改配置删除自动生成的key等文件

1
2
3
4
5
6
7
8
9
pushd build/nodes
for N in */; do
echo 'compatibilityZoneURL="http://localhost:8080"' >> $N/node.conf
echo 'devModeOptions.allowCompatibilityZone=true' >> $N/node.conf
pushd $N
rm -rf network-parameters nodeInfo-* persistence.mv.db certificates additional-node-infos
popd
done
popd

4、把节点注册到网络

下载网络的truststore

1
curl http://localhost:8080/network-map/truststore -o ~/tmp/network-truststore.jks

每个节点都初始化注册

1
2
3
4
5
6
7
pushd build/nodes
for N in */; do
pushd $N
java -jar corda.jar --initial-registration --network-root-truststore ~/tmp/network-truststore.jks --network-root-truststore-password trustpass
popd
done
popd

5、指定Notary节点

启动Notary节点

进到Notary节点的目录下执行,如果没有权限

1
java -jar corda.jar

注意:如果报错:Unable to create logging directory /Users/apple/code/open-source/blockchain/my-corda/logs. Node will now shutdown.说明没有权限,在命令前加sudo即可

指定Notary节点

1、登陆NMS(network-map-service)并获取token信息

1
TOKEN=`curl -X POST "http://localhost:8080//admin/api/login" -H  "accept: text/plain" -H  "Content-Type: application/json" -d "{  \"user\": \"sa\",  \"password\": \"admin\"}"`

2、上传notary

1
2
3
4
pushd build/nodes/Notary
NODEINFO=`ls nodeInfo*`
curl -X POST -H "Authorization: Bearer $TOKEN" -H "accept: text/plain" -H "Content-Type: application/octet-stream" --data-binary @$NODEINFO http://localhost:8080//admin/api/notaries/validating
popd

在执行上面的命令时,注意自己当前的所在的目录,如果已经在build/nodes/Notary目录下需要退出到bootcamp-cordapp目录

6、停止Notary节点

在notary节点的shell命令行执行 bye

7、修改notary节点的validating

因为在第5步指定Notary的时候调用的是validating接口,所以确认一下notary的配置是的validating是否为true,如下

1
2
3
notary {
validating=true
}

如果配置的是validating=false则会报异常:
[ERROR] 16:25:22+0800 [main] internal.NodeStartupLogging. - Exception during node startup: There is a discrepancy in the configured notary type and the one advertised in the network parameters - shutting down. Configured as validating: false. Advertised as validating: true [errorCode=r8le54, moreInformationAt=https://errors.corda.net/OS/4.3/r8le54]

8、删除Notary节点的network-parameters文件

进入到notary节点的目录,删除network-parameters文件

9、启动notary节点和其他节点

进入到各个节点的目录,启动:

1
java -jar corda.jar

10、发起交易测试

可以下载node-explorer来查看节点信息并发起交易,我的测试结果如下:

image-20201209165525748

扩展

因为上面的文档是按照