bootstrap.sh 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/usr/bin/env bash
  2. workspace=$(cd `dirname $0`; pwd)/..
  3. function prepare() {
  4. if ! [[ -f /usr/local/bin/geth ]];then
  5. echo "geth do not exist!"
  6. exit 1
  7. fi
  8. rm -rf ${workspace}/storage/*
  9. cd ${workspace}/genesis
  10. rm -rf validators.conf
  11. }
  12. function init_validator() {
  13. node_id=$1
  14. mkdir -p ${workspace}/storage/${node_id}
  15. geth --datadir ${workspace}/storage/${node_id} account new --password /dev/null > ${workspace}/storage/${node_id}Info
  16. validatorAddr=`cat ${workspace}/storage/${node_id}Info|grep 'Public address of the key'|awk '{print $6}'`
  17. echo "${validatorAddr},${validatorAddr},${validatorAddr},0x0000000010000000" >> ${workspace}/genesis/validators.conf
  18. echo ${validatorAddr} > ${workspace}/storage/${node_id}/address
  19. }
  20. function generate_genesis() {
  21. INIT_HOLDER_ADDRESSES=$(ls ${workspace}/init-holders | tr '\n' ',')
  22. INIT_HOLDER_ADDRESSES=${INIT_HOLDER_ADDRESSES/%,/}
  23. sed "s/{{INIT_HOLDER_ADDRESSES}}/${INIT_HOLDER_ADDRESSES}/g" ${workspace}/genesis/init_holders.template | sed "s/{{INIT_HOLDER_BALANCE}}/${INIT_HOLDER_BALANCE}/g" > ${workspace}/genesis/init_holders.js
  24. node generate-validator.js
  25. chainIDHex=$(printf '%04x\n' ${BSC_CHAIN_ID})
  26. node generate-genesis.js --chainid ${BSC_CHAIN_ID} --bscChainId ${chainIDHex}
  27. }
  28. function init_genesis_data() {
  29. node_type=$1
  30. node_id=$2
  31. geth --datadir ${workspace}/storage/${node_id} init ${workspace}/genesis/genesis.json
  32. cp ${workspace}/config/config-${node_type}.toml ${workspace}/storage/${node_id}/config.toml
  33. sed -i -e "s/{{NetworkId}}/${BSC_CHAIN_ID}/g" ${workspace}/storage/${node_id}/config.toml
  34. if [ "${node_id}" == "bsc-rpc" ]; then
  35. cp ${workspace}/init-holders/* ${workspace}/storage/${node_id}/keystore
  36. cp ${workspace}/genesis/genesis.json ${workspace}/storage/${node_id}
  37. fi
  38. }
  39. prepare
  40. # First, generate config for each validator
  41. for((i=1;i<=${NUMS_OF_VALIDATOR};i++)); do
  42. init_validator "bsc-validator${i}"
  43. done
  44. # Then, use validator configs to generate genesis file
  45. generate_genesis
  46. # Finally, use genesis file to init cluster data
  47. init_genesis_data bsc-rpc bsc-rpc
  48. for((i=1;i<=${NUMS_OF_VALIDATOR};i++)); do
  49. init_genesis_data validator "bsc-validator${i}"
  50. done