You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

testnet.sh 7.5 kB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. ```bash
  2. #!/bin/bash
  3. HOME=$(cd `dirname $0`; pwd)
  4. echo "" > nohup.out
  5. ps -ef|grep '$HOME'|grep -v grep|cut -c 9-15|xargs kill -9
  6. echo "Start jdchain Initialing in $HOME"
  7. ## parameters
  8. # 节点数量
  9. NODE_SIZE=4
  10. # 允许错误节点个数
  11. FAULT_SIZE=1
  12. # 数据库URI
  13. DB_URI=""
  14. # peer API server start port
  15. PEER_PORT=7080
  16. # consus start port
  17. CONSUS_PORT=10080
  18. # DB password
  19. DB_PWD=""
  20. # ledger size
  21. LEDGER_SIZE=1
  22. while getopts ":N:f:d:P:p:w:L:" opt
  23. do
  24. case $opt in
  25. N)
  26. NODE_SIZE=$OPTARG
  27. ;;
  28. f)
  29. FAULT_SIZE=$OPTARG
  30. ;;
  31. d)
  32. DB_URI=$OPTARG
  33. ;;
  34. P)
  35. PEER_PORT=$OPTARG
  36. ;;
  37. p)
  38. CONSUS_PORT=$OPTARG
  39. ;;
  40. w)
  41. DB_PWD=$OPTARG
  42. ;;
  43. L)
  44. LEDGER_SIZE=$OPTARG
  45. ;;
  46. ?)
  47. echo "unknow parameter"
  48. esac
  49. done
  50. # clear old data
  51. rm -rf peer*
  52. rm -rf gw*
  53. ####################################### init peers files
  54. KEY_NAME=`date '+%s'`
  55. i=0
  56. while(( $i<$NODE_SIZE ))
  57. do
  58. # uzip peer and gateway files
  59. unzip -oq jdchain-peer-* -d peer$i
  60. chmod 777 peer$i/bin/*
  61. # generate key
  62. mkdir peer$i/config/keys
  63. if [ -f "keygen" ];then
  64. chmod +x keygen
  65. ./keygen $KEY_NAME peer$i/config/keys "WprD3WiAi5S6Z1BSDlvUkhBVhcBiaxf"$i
  66. else
  67. cd peer$i/bin/
  68. expect -c "
  69. set timeout 10
  70. spawn ./keygen.sh -n $KEY_NAME
  71. expect \"*Input password:\"
  72. send \"1\r\"
  73. expect \"*input y or n *\"
  74. send \"y\r\"
  75. expect eof
  76. "
  77. cd $HOME
  78. fi
  79. IDs[$i]=$i
  80. PUBKs[$i]=$(cat peer$i/config/keys/$KEY_NAME.pub)
  81. PRIVs[$i]=$(cat peer$i/config/keys/$KEY_NAME.priv)
  82. PWDs[$i]=$(cat peer$i/config/keys/$KEY_NAME.pwd)
  83. let "i++"
  84. done
  85. # init peer-startup.sh
  86. i=0
  87. while(( $i<$NODE_SIZE ))
  88. do
  89. sed -ri "s#7080#$PEER_PORT#g" peer$i/bin/peer-startup.sh
  90. let "PEER_PORT++"
  91. let "i++"
  92. done
  93. ####################################### init gateway
  94. unzip -oq jdchain-gateway-* -d gw
  95. chmod 777 gw/bin/*
  96. sed -ri "s#peer.port=7080#peer.port=$((PEER_PORT-NODE_SIZE))#g" gw/config/gateway.conf
  97. sed -ri "s#keys.default.pubkey=#keys.default.pubkey=${PUBKs[0]}#g" gw/config/gateway.conf
  98. sed -ri "s#keys.default.privkey=#keys.default.privkey=${PRIVs[0]}#g" gw/config/gateway.conf
  99. sed -ri "s#keys.default.privkey-password=#keys.default.privkey-password=${PWDs[0]}#g" gw/config/gateway.conf
  100. ###################################### generate start and shutdown files
  101. echo "#!/bin/bash" > start.sh
  102. i=0
  103. while(( $i<$NODE_SIZE ))
  104. do
  105. echo "
  106. nohup ./peer$i/bin/peer-startup.sh & " >> start.sh
  107. chmod 777 start.sh
  108. let "i++"
  109. done
  110. echo "
  111. sleep 20
  112. nohup ./gw/bin/startup.sh &" >> start.sh
  113. echo "#!/bin/bash
  114. ps -ef|grep '$HOME'|grep -v grep|cut -c 9-15|xargs kill -9
  115. " > shutdown.sh
  116. chmod 777 shutdown.sh
  117. ###################################### ledger init
  118. k=0
  119. while(( $k<$LEDGER_SIZE ))
  120. do
  121. echo "初始化账本 "$k
  122. seed=`date +%s%N | md5sum |cut -c 1-32`
  123. i=0
  124. while(( $i<$NODE_SIZE ))
  125. do
  126. #### init local.conf
  127. sed -ri "s/local.parti.id(.*)/local.parti.id=$i/g" peer$i/config/init/local.conf
  128. sed -ri "s/local.parti.pubkey(.*)/local.parti.pubkey=${PUBKs[$i]}/g" peer$i/config/init/local.conf
  129. sed -ri "s/local.parti.privkey(.*)/local.parti.privkey=${PRIVs[$i]}/g" peer$i/config/init/local.conf
  130. sed -ri "s/local.parti.pwd(.*)/local.parti.pwd=${PWDs[$i]}/g" peer$i/config/init/local.conf
  131. if [ -z $DB_URI ]
  132. then
  133. sed -ri "s#ledger.db.uri(.*)#ledger.db.uri=rocksdb://$HOME/peer$i/rocksdb$k#g" peer$i/config/init/local.conf
  134. else
  135. sed -ri "s#ledger.db.uri(.*)#ledger.db.uri=$DB_URI/$i#g" peer$i/config/init/local.conf
  136. fi
  137. sed -ri "s#ledger.db.pwd(.*)#ledger.db.pwd=$DB_PWD#g" peer$i/config/init/local.conf
  138. #### init ledger.init
  139. echo "
  140. ledger.seed=$seed
  141. ledger.name=ledger$k
  142. created-time=$(date +"%Y-%m-%d %H:%M:%S").000+0800
  143. consensus.service-provider=com.jd.blockchain.consensus.bftsmart.BftsmartConsensusProvider
  144. consensus.conf=$HOME/peer$i/config/init/bftsmart.config
  145. crypto.service-providers=com.jd.blockchain.crypto.service.classic.ClassicCryptoService, com.jd.blockchain.crypto.service.sm.SMCryptoService
  146. crypto.verify-hash=true
  147. crypto.hash-algorithm=SHA256
  148. cons_parti.count=$NODE_SIZE" > peer$i/config/init/ledger.init
  149. j=0
  150. port=11010
  151. while(( $j<$NODE_SIZE ))
  152. do
  153. echo "
  154. cons_parti.$j.name=$j
  155. cons_parti.$j.pubkey=${PUBKs[$j]}
  156. cons_parti.$j.initializer.host=127.0.0.1
  157. cons_parti.$j.initializer.port=$port
  158. cons_parti.$j.initializer.secure=false" >> peer$i/config/init/ledger.init
  159. let "j++"
  160. let "port++"
  161. done
  162. #### init bftsmart.config
  163. echo "" > peer$i/config/init/bftsmart.config
  164. j=0
  165. port=$((CONSUS_PORT+k*100))
  166. while(( $j<$NODE_SIZE ))
  167. do
  168. echo "
  169. system.server.$j.network.host=127.0.0.1
  170. system.server.$j.network.port=$port
  171. system.server.$j.network.secure=false" >> peer$i/config/init/bftsmart.config
  172. let "j++"
  173. let "port++"
  174. let "port++"
  175. done
  176. echo "
  177. system.communication.useSenderThread = true
  178. system.communication.defaultkeys = true
  179. system.servers.num = $NODE_SIZE
  180. system.servers.f = $FAULT_SIZE
  181. system.totalordermulticast.timeout = 60000
  182. system.totalordermulticast.timeTolerance = 3000000
  183. system.totalordermulticast.maxbatchsize = 2000
  184. system.totalordermulticast.nonces = 10
  185. system.totalordermulticast.verifyTimestamps = false
  186. system.communication.inQueueSize = 500000
  187. system.communication.outQueueSize = 500000
  188. system.communication.send.retryInterval = 2000
  189. system.communication.send.retryCount = 100
  190. system.communication.useSignatures = 0
  191. system.communication.useMACs = 1
  192. system.debug = 0
  193. system.shutdownhook = true
  194. system.totalordermulticast.state_transfer = true
  195. system.totalordermulticast.highMark = 10000
  196. system.totalordermulticast.revival_highMark = 10
  197. system.totalordermulticast.timeout_highMark = 200
  198. system.totalordermulticast.log = true
  199. system.totalordermulticast.log_parallel = false
  200. system.totalordermulticast.log_to_disk = true
  201. system.totalordermulticast.sync_log = false
  202. system.totalordermulticast.checkpoint_period = 1000
  203. system.totalordermulticast.global_checkpoint_period = 120000
  204. system.totalordermulticast.checkpoint_to_disk = false
  205. system.totalordermulticast.sync_ckp = false
  206. system.initial.view = $( IFS=$','; echo "${IDs[*]}" )
  207. system.ttp.id = 2001
  208. system.bft = true" >> peer$i/config/init/bftsmart.config
  209. let "i++"
  210. done
  211. echo "" > nohup.out
  212. ### start ledger init
  213. i=0
  214. while(( $i<$NODE_SIZE ))
  215. do
  216. nohup expect -c "
  217. set timeout 180
  218. spawn peer$i/bin/ledger-init.sh
  219. expect \"*Any key to continue...*\"
  220. send \"1\r\"
  221. expect \"*Press any key to quit. *\"
  222. send \"quit\r\"
  223. expect eof
  224. " &
  225. let "i++"
  226. done
  227. tail -f nohup.out| while read line
  228. nSize=0
  229. do
  230. if [[ $line =~ "Update Ledger binding configuration success!" ]]
  231. then
  232. let "nSize++"
  233. if [[ $line == $NODE_SIZE ]]
  234. then
  235. echo -e ".\c"
  236. else
  237. echo ""
  238. echo "账本 "$k" 初始化完成"
  239. break
  240. fi
  241. else
  242. echo -e ".\c"
  243. fi
  244. done
  245. let "k++"
  246. sleep 1
  247. done
  248. ```