Skip to content

Commit

Permalink
Merge pull request #14 from bnb-chain/refactor-scripts
Browse files Browse the repository at this point in the history
remove sed usage for bsc cluster
  • Loading branch information
Mister-EA authored Sep 15, 2023
2 parents d113901 + 2a99d02 commit a36106a
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 103 deletions.
36 changes: 32 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,19 @@ cp ./test-crosschain-transfer ../bin/test-crosschain-transfer
```bash
make tool
```
5. Setup all nodes.

5. Configure the cluster

You can configure the cluster by modifying the following files:
- `config.toml`
- `genesis/genesis-template.json`
- `.env`

6. Setup all nodes.
two different ways, choose as you like.

**On Kubernetes environment**

```bash
#on k8s environment
minikube start
Expand All @@ -61,7 +72,7 @@ bash +x ./setup_bc_node.sh install_k8s
kubectl port-forward svc/bc-node-0 26657:26657 -n bc

bash +x ./setup_bsc_node.sh register
bash +x ./setup_bsc_node.sh generate
bash +x ./setup_bsc_node.sh generate_k8s
bash +x ./setup_bsc_node.sh install_k8s
kubectl -n bsc port-forward svc/bsc-node-0 8545:8545

Expand All @@ -71,6 +82,8 @@ bash +x ./setup_oracle_relayer.sh install_k8s
bash +x ./setup_bsc_relayer.sh docker
bash +x ./setup_bsc_relayer.sh install_k8s
```

**Natively**
```bash
#native deploy without docker
rm -rf .local
Expand All @@ -88,7 +101,13 @@ bash +x ./setup_oracle_relayer.sh native_init
bash +x ./setup_oracle_relayer.sh native_start
```

6. Execute cross chain transaction by sending BNB from BC to BSC
or simply
``` bash
# native deploy
bash +x ./start_cluster.sh
```

7. Execute cross chain transaction by sending BNB from BC to BSC
```bash
## 0x9fB29AAc15b9A4B7F17c3385939b007540f4d791 is the address used by test-crosschain-transfer as sender
## macos
Expand All @@ -98,7 +117,7 @@ echo "12345678" | ./bin/tbnbcli bridge transfer-out --amount 500000000:BNB --exp
echo "12345678" | ./bin/tbnbcli bridge transfer-out --amount 500000000:BNB --expire-time $(date --date="+300 seconds" +%s) --to 0x9fB29AAc15b9A4B7F17c3385939b007540f4d791 --from local-user --chain-id Binance-Chain-Ganges --node localhost:26657
```

7. Check the account balance
8. Check the account balance
```
curl -X POST "http://localhost:8545" -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"eth_getBalance","params":["0x9fB29AAc15b9A4B7F17c3385939b007540f4d791", "latest"],"id":1}'
```
Expand Down Expand Up @@ -132,6 +151,15 @@ bash +x ./setup_bsc_relayer.sh native_stop
bash +x ./setup_oracle_relayer.sh native_stop
```

or simply

```bash
bash +x ./stop_cluster.sh
```


### Start a standalone BSC Cluster

```bash
# one cmd to start bsc cluster all alone
bash +x ./setup_bsc_node.sh native_run_alone
Expand Down
4 changes: 2 additions & 2 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ DatabaseCache = 512
DatabaseFreezer = ""
TrieCleanCache = 256
TrieDirtyCache = 256
TriesInMemory = 128
TrieTimeout = 3600000000000
EnablePreimageRecording = false
EWASMInterpreter = ""
Expand Down Expand Up @@ -48,10 +49,9 @@ WSPort = 8545

[Node.P2P]
MaxPeers = 50
NoDiscovery = true
NoDiscovery = false
StaticNodes = []
TrustedNodes = []
ListenAddr = ":30311"
EnableMsgEvents = false

[Node.HTTPTimeouts]
Expand Down
25 changes: 8 additions & 17 deletions helm/bsc/templates/statefulset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,9 @@ spec:
imagePullPolicy: {{ .Values.image.pullPolicy }}
command: ["/bin/sh", "-c"]
args:
- geth
--config /data/config.toml
--datadir /data
--ws
--ws.port 8545
--ws.addr 0.0.0.0
--http.addr 0.0.0.0
--http.port 8545
--http.corsdomain "*"
--allow-insecure-unlock
--mine
--password /data/password.txt
-unlock "0x$(cat /data/keystore/* | sed -n 's/.*"address":"\([^"]*\).*/\1/p')"
--rpc.allow-unprotected-txs
--gcmode archive
--syncmode=full
--nodekey /data/nodekey
- geth init --datadir /data /data/genesis.json;
geth --config /data/config.toml --datadir /data --ws --ws.port 8545 --ws.addr 0.0.0.0 --http.addr 0.0.0.0 --http.port 8545 --http.corsdomain "*" --allow-insecure-unlock --mine --password /data/password.txt -unlock "0x$(cat /data/keystore/* | sed -n 's/.*"address":"\([^"]*\).*/\1/p')" --rpc.allow-unprotected-txs --gcmode archive --syncmode=full --nodekey /data/nodekey;

ports:
- name: http
containerPort: 8545
Expand All @@ -84,6 +70,11 @@ spec:
- name: config-volume
mountPath: /data/config.toml
subPath: config.toml
readOnly: false
- name: config-volume
mountPath: /data/genesis.json
subPath: genesis.json
readOnly: false
- name: nodekey-volume
mountPath: /data/nodekey
subPath: nodekey
Expand Down
2 changes: 1 addition & 1 deletion helm/bsc/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

image:
repository: ghcr.io/bnb-chain/bsc
pullPolicy: IfNotPresent
pullPolicy: Always
# Overrides the image tag whose default is the chart appVersion.
tag: "latest"

Expand Down
18 changes: 0 additions & 18 deletions init_holders.template

This file was deleted.

101 changes: 40 additions & 61 deletions setup_bsc_node.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ source ${workspace}/.env
source ${workspace}/utils.sh
size=$((${BSC_CLUSTER_SIZE}))
nodeurl="http://localhost:26657"
replaceWhitelabelRelayer="0xb005741528b86F5952469d80A8614591E3c5B632"
replaceConsensusStateBytes="42696e616e63652d436861696e2d4e696c650000000000000000000000000000000000000000000229eca254b3859bffefaf85f4c95da9fbd26527766b784272789c30ec56b380b6eb96442aaab207bc59978ba3dd477690f5c5872334fc39e627723daa97e441e88ba4515150ec3182bc82593df36f8abb25a619187fcfab7e552b94e64ed2deed000000e8d4a51000"
standalone=false

function exit_previous() {
Expand Down Expand Up @@ -97,37 +95,16 @@ function generate_static_peers() {

function clean() {
if ! [[ -f ${workspace}/bin/geth ]];then
echo "bin/geth do not exist!"
echo "bin/geth does not exist!"
exit 1
fi
rm -rf ${workspace}/.local/bsc/clusterNetwork
mkdir ${workspace}/.local/bsc/clusterNetwork
cd ${workspace}/genesis
git stash
cd ${workspace}
git submodule update --remote
cd ${workspace}/genesis
npm install
}

function prepare_config() {
rm -f ${workspace}/genesis/validators.conf
rm -f ${workspace}/genesis/init_holders.template
cp ${workspace}/init_holders.template ${workspace}/genesis/init_holders.template

sed -i -e "s/${replaceWhitelabelRelayer}/${INIT_HOLDER}/g" ${workspace}/genesis/contracts/RelayerHub.template
sed -i -e "s/function whitelistInit() external/function whitelistInit() public/g" ${workspace}/genesis/contracts/RelayerHub.template
sed -i -e "s/alreadyInit = true;/whitelistInit();\nalreadyInit = true;/g" ${workspace}/genesis/contracts/RelayerHub.template
sed -i -e "s/alreadyInit = true;/enableMaliciousVoteSlash = true;\nalreadyInit = true;/g" ${workspace}/genesis/contracts/SlashIndicator.template
sed -i -e "s/numOperator = 2;/operators[VALIDATOR_CONTRACT_ADDR] = true;\noperators[SLASH_CONTRACT_ADDR] = true;\nnumOperator = 4;/g" ${workspace}/genesis/contracts/SystemReward.template
sed -i -e "s/for (uint i; i<validatorSetPkg.validatorSet.length; ++i) {/ValidatorExtra memory validatorExtra;\nfor (uint i; i<validatorSetPkg.validatorSet.length; ++i) {\n validatorExtraSet.push(validatorExtra);\n validatorExtraSet[i].voteAddress=validatorSetPkg.voteAddrs[i];/g" ${workspace}/genesis/contracts/BSCValidatorSet.template
sed -i -e "s/\"0x\" + publicKey.pop()/vs[4]/g" ${workspace}/genesis/generate-validator.js
sed "s/{{INIT_HOLDER_ADDR}}/${INIT_HOLDER}/g" ${workspace}/genesis/init_holders.template > ${workspace}/genesis/init_holders.js
if [ ${standalone} = false ]; then
initConsensusStateBytes=$(${workspace}/bin/tool -height 1 -rpc ${nodeurl} -network-type 0)
sed -i -e "s/${replaceConsensusStateBytes}/${initConsensusStateBytes}/g" ${workspace}/genesis/generate-tendermintlightclient.js
fi

for ((i=0;i<${size};i++));do
for f in ${workspace}/.local/bsc/validator${i}/keystore/*;do
cons_addr="0x$(cat ${f} | jq -r .address)"
Expand All @@ -154,13 +131,19 @@ function prepare_config() {

cd ${workspace}/genesis/
node generate-validator.js
node generate-genesis.js --chainid ${BSC_CHAIN_ID} --bscChainId "$(printf '%04x\n' ${BSC_CHAIN_ID})"
node generate-initHolders.js --initHolders ${INIT_HOLDER}
if [ ${standalone} = false ]; then
initConsensusStateBytes=$(${workspace}/bin/tool -height 1 -rpc ${nodeurl} -network-type 0)
node generate-genesis.js --chainid ${BSC_CHAIN_ID} --network 'local' --whitelist1Address ${INIT_HOLDER} --initConsensusStateBytes ${initConsensusStateBytes}
else
node generate-genesis.js --chainid ${BSC_CHAIN_ID} --network 'local' --whitelist1Address ${INIT_HOLDER}
fi

}

function generate() {
cd ${workspace}
${workspace}/bin/geth init-network --init.dir ${workspace}/.local/bsc/clusterNetwork --init.size=${size} --config ${workspace}/config.toml ${workspace}/genesis/genesis.json
rm -rf ${workspace}/*bsc.log*
function initNetwork_k8s() {
cd ${workspace}
${workspace}/bin/geth init-network --init.dir ${workspace}/.local/bsc/clusterNetwork --init.ips=${ips_string} --init.size=${size} --config ${workspace}/config.toml ${workspace}/genesis/genesis.json
for ((i=0;i<${size};i++));do
staticPeers=$(generate_static_peers ${size} ${i})
line=`grep -n -e 'StaticNodes' ${workspace}/.local/bsc/clusterNetwork/node${i}/config.toml | cut -d : -f 1`
Expand All @@ -169,25 +152,14 @@ function generate() {
tail -n +$(($line+1)) ${workspace}/.local/bsc/clusterNetwork/node${i}/config.toml >> ${workspace}/.local/bsc/clusterNetwork/node${i}/config.toml-e
rm -f ${workspace}/.local/bsc/clusterNetwork/node${i}/config.toml
mv ${workspace}/.local/bsc/clusterNetwork/node${i}/config.toml-e ${workspace}/.local/bsc/clusterNetwork/node${i}/config.toml

sed -i -e "s/TriesInMemory = 0/TriesInMemory = 128/g" ${workspace}/.local/bsc/clusterNetwork/node${i}/config.toml
sed -i -e "s/NetworkId = 714/NetworkId = ${BSC_CHAIN_ID}/g" ${workspace}/.local/bsc/clusterNetwork/node${i}/config.toml

sed -i -e '/BerlinBlock/d' ${workspace}/.local/bsc/clusterNetwork/node${i}/config.toml
sed -i -e '/EWASMBlock/d' ${workspace}/.local/bsc/clusterNetwork/node${i}/config.toml
sed -i -e '/CatalystBlock/d' ${workspace}/.local/bsc/clusterNetwork/node${i}/config.toml
sed -i -e '/YoloV3Block/d' ${workspace}/.local/bsc/clusterNetwork/node${i}/config.toml
sed -i -e '/LondonBlock/d' ${workspace}/.local/bsc/clusterNetwork/node${i}/config.toml
sed -i -e '/ArrowGlacierBlock/d' ${workspace}/.local/bsc/clusterNetwork/node${i}/config.toml
sed -i -e '/MergeForkBlock/d' ${workspace}/.local/bsc/clusterNetwork/node${i}/config.toml
sed -i -e '/TerminalTotalDifficulty/d' ${workspace}/.local/bsc/clusterNetwork/node${i}/config.toml
sed -i -e '/BaseFee/d' ${workspace}/.local/bsc/clusterNetwork/node${i}/config.toml
sed -i -e '/RPCTxFeeCap/d' ${workspace}/.local/bsc/clusterNetwork/node${i}/config.toml
sed -i -e "s/MirrorSyncBlock = 1/MirrorSyncBlock = 0/g" ${workspace}/.local/bsc/clusterNetwork/node${i}/config.toml
sed -i -e "s/BrunoBlock = 1/BrunoBlock = 0/g" ${workspace}/.local/bsc/clusterNetwork/node${i}/config.toml
sed -i -e "s/EulerBlock = 2/EulerBlock = 0\nNanoBlock = 0/g" ${workspace}/.local/bsc/clusterNetwork/node${i}/config.toml
sed -i -e 's/PlatoBlock = 7/PlatoBlock = 7\nBerlinBlock = 8\nLondonBlock = 8\nHertzBlock= 8/' ${workspace}/.local/bsc/clusterNetwork/node${i}/config.toml
done
rm -rf ${workspace}/*bsc.log*
}

function initNetwork() {
cd ${workspace}
${workspace}/bin/geth init-network --init.dir ${workspace}/.local/bsc/clusterNetwork --init.size=${size} --config ${workspace}/config.toml ${workspace}/genesis/genesis.json
rm -rf ${workspace}/*bsc.log*
}

function prepare_k8s_config() {
Expand All @@ -207,7 +179,8 @@ function prepare_k8s_config() {

kubectl delete configmap config${i} -n bsc
kubectl create configmap config${i} -n bsc \
--from-file ${workspace}/.local/bsc/clusterNetwork/node${i}/config.toml
--from-file ${workspace}/.local/bsc/clusterNetwork/node${i}/config.toml \
--from-file ${workspace}/genesis/genesis.json

kubectl delete configmap nodekey${i} -n bsc
kubectl create configmap nodekey${i} -n bsc \
Expand Down Expand Up @@ -238,20 +211,14 @@ function native_start() {
cons_addr="0x$(cat ${j} | jq -r .address)"
done

# sorry for magic
for ((k=0;k<${size};k++));do
p2p_port_k=$((30311 + k))
if [ ${k} -ne ${i} ];then
sed -i.bak "s/bsc-node-${k}.bsc.svc.cluster.local:30311/localhost:${p2p_port_k}/" ${workspace}/.local/bsc/clusterNetwork/node${i}/config.toml
else
sed -i.bak "s/\":30311/\":${p2p_port_k}/" ${workspace}/.local/bsc/clusterNetwork/node${i}/config.toml
fi
done
HTTPPort=$((8545 + i))
WSPort=${HTTPPort}
MetricsPort=$((6060 + i))

cp ${workspace}/bin/geth ${workspace}/.local/bsc/clusterNetwork/node${i}/geth${i}
# init genesis
${workspace}/.local/bsc/clusterNetwork/node${i}/geth${i} init --datadir ${workspace}/.local/bsc/clusterNetwork/node${i} genesis/genesis.json
# run BSC node
nohup ${workspace}/.local/bsc/clusterNetwork/node${i}/geth${i} --config ${workspace}/.local/bsc/clusterNetwork/node${i}/config.toml \
--datadir ${workspace}/.local/bsc/clusterNetwork/node${i} \
--password ${workspace}/.local/bsc/password.txt \
Expand All @@ -277,9 +244,21 @@ generate)
clean
echo "===== generate configs ===="
prepare_config
generate
initNetwork
echo "===== end ===="
;;
generate_k8s)
echo "===== clean ===="
clean
echo "===== generate configs for k8s ===="
prepare_config
initNetwork_k8s
echo "===== end ===="
;;
clean)
echo "===== clean ===="
clean
;;
install_k8s)
echo "===== k8s install ===="
prepare_k8s_config
Expand All @@ -299,7 +278,7 @@ native_init)
clean
echo "===== generate configs ===="
prepare_config
generate
initNetwork
echo "===== end ===="
;;
native_run_alone)
Expand All @@ -311,7 +290,7 @@ native_run_alone)
clean
echo "===== generate configs ===="
prepare_config
generate
initNetwork
echo "===== end ===="
echo "===== start native ===="
native_start
Expand All @@ -334,6 +313,6 @@ native_stop)
echo "===== stop native end ===="
;;
*)
echo "Usage: setup_bsc_node.sh register | generate | install_k8s | uninstall_k8s | native_init | native_run_alone | native_start | native_stop"
echo "Usage: setup_bsc_node.sh register | generate | generate_k8s | clean | install_k8s | uninstall_k8s | native_init | native_run_alone | native_start | native_stop"
;;
esac
14 changes: 14 additions & 0 deletions start_cluster.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env bash
rm -rf .local

bash +x ./setup_bc_node.sh native_init
bash +x ./setup_bc_node.sh native_start

bash +x ./setup_bsc_node.sh native_init
bash +x ./setup_bsc_node.sh native_start

bash +x ./setup_bsc_relayer.sh native_init
bash +x ./setup_bsc_relayer.sh native_start

bash +x ./setup_oracle_relayer.sh native_init
bash +x ./setup_oracle_relayer.sh native_start
6 changes: 6 additions & 0 deletions stop_cluster.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash
bash +x ./setup_bc_node.sh native_stop
bash +x ./setup_bsc_node.sh native_stop
bash +x ./setup_bsc_relayer.sh native_stop
bash +x ./setup_oracle_relayer.sh native_stop

0 comments on commit a36106a

Please sign in to comment.