# Migration of GuanYu Mainnet to Laozi Mainnet (Rehearsal version)
**IMPORTANCE:** Please make sure your node meets the requirement of RAM16GB
This document describes a procedure to upgrade Bandchain from GuanYu to Laozi. We will proceed these steps together at the following time and date
- Halt time is <TODO>
**NOTE**: Before the migration date, please add `--halt-time` flag in your `bandd` service and restart the service, so that at the specified time, `bandd` will stop automatically.
```bash
bandd start --halt-time 1630927958
```
## Step 0: Environment Setup
```bash!
sudo su ubuntu
source ~/.profile
# GuanYu's halted block
export HALTED_BLOCK=35050
# Laozi's genesis date
export GENESIS_DATE="2021-09-06T13:02:39.083358851Z"
# validator's wallet name
export WALLET_NAME=validator
# Laozi's new node name
export MONIKER=<YOUR_MONIKER>
# CHAIN ID
export CHAIN_ID_LAOZI=band-laozi-mainnet-mock
export CHAIN_ID_GUANYU=band-guanyu-mainnet-mock
export SEEDS_LAOZI="057a5e7505637adb59bea7be04a05aec012bfe59@35.247.158.55:26656,e663c4b240c3d1bae25d34e53b1f6201d1a1dad4@34.150.235.70:26656"
export PERSISTENT_PEERS_GUANYU="3b35861c7c702ae0147c4ed6645ae932acd63935@34.87.71.6:26656,b89f3fc698964b99cd8f80c86d312a010dce1dea@34.150.187.143:26656,7b21fa89b9597290cbae36fac501ad8b89bbd418@34.126.180.205:26656,3729004333d8ac9f8a03b7f441d59a28be56eee7@34.145.177.72:26656"
```
## Step 1: Migrate the Validator Node
### Step 1.1: Stop the current node by running the command below
```bash=
sudo systemctl stop bandd.service
sudo systemctl stop yoda.service
```
### Step 1.2: Upgrade GuanYu node to the latest version
Since there is latest patch that improve method of exporting genesis file, the latest version of GuanYu Bandchain is required.
```bash=
cd $HOME/bandchain/chain
git checkout master
git pull
git checkout v1.3.0
make install
```
Please make sure that `bandd` and `bandcli` is now the latest version.
```bash=
bandd version --long
# name: bandchain
# server_name: bandd
# client_name: bandcli
# version: 1.3.0
# commit: deefc4d7ca6f90ded6a64d2ffedf7853cf5c6e42
# build_tags: ledger
# go: go version go1.14.15 linux/amd64
```
### Step 1.3: Export genesis file & wallet
The following information will be included in the genesis file. This might takes a long time (around 2 hours).
**WARNING:** You might want to use [tmux](https://www.hamvocke.com/blog/a-quick-and-easy-guide-to-tmux/), since this process is long and your ssh connection may be disconnected in the middle.
```bash=
cd $HOME
bandd export --for-zero-height --height $HALTED_BLOCK > guanyu_mainnet_genesis.json
```
If there are wallets on validator nodes (keys in keyring type `os`), they should be exported by the following command.
**Note that if Ledger hardware wallet is used to store keys, following exporting keys steps may be skipped.**
```bash=
bandcli keys export $WALLET_NAME --keyring-backend test
# filename ${WALLET_NAME}_privkey.txt
```
After running the command, it will ask to input the password to encrypt the private key. Then, a private key will be exported to the specified file. Check the file to see if the contained text is similar to the example below.
```bash
#
# -----BEGIN TENDERMINT PRIVATE KEY-----
# ...
# -----END TENDERMINT PRIVATE KEY-----
#
```
### Step 1.4: Verify integrity of genesis file.
The expected hash of the genesis file will be announced on Band Protocol's [discord server](https://100x.band/discord) under `#validators` channel).
In order to preserve the consistency of generating the hash, all fields in JSON content should be sorted. This can be done by using `jq` command. Then, pipe the output to `shasum` to produce hash.
```bash=
sudo apt-get update
sudo apt-get install -y build-essential jq
jq -S -c -M '' guanyu_mainnet_genesis.json | shasum -a 256
# <TODO> -
```
We will announce a SHA256 hash to be compared with your hash, so, **Please compare them before further steps**.
### Step 1.5: Install Laozi version of bandd (v2.3.0).
After the genesis file of GuanYu is exported, a new version of bandd **v2.3.0** will be used to begin initializing and run the new Laozi node. This can be done by executing the following commands.
```bash=
# Remove old version of go (1.14)
rm -r $HOME/go/bin
sudo rm -r /usr/local/go
# Install new version of go (1.16)
wget https://dl.google.com/go/go1.16.7.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf go1.16.7.linux-amd64.tar.gz
echo "export PATH=\$PATH:/usr/local/go/bin:$HOME/go/bin" >> $HOME/.profile
source $HOME/.profile
# Clone Laozi code from the new repository and build bandd and yoda
git clone https://github.com/bandprotocol/chain.git
cd chain
git checkout v2.3.0
make install
```
Make sure that `bandd` version is the latest Laozi version.
```bash=
bandd version --long
# name: bandchain
# server_name: bandd
# version: 2.3.0
# commit: 4fe19638b33043eed4dec9861cda40962fb5b2a7
# build_tags: ledger
# go: go version go1.16.7 linux/amd64
```
### Step 1.6: Migrate the exported genesis file to be compatible with Laozi Bandchain
Since GuanYu Bandchain uses Cosmos SDK v0.39 while Laozi Bandchain uses Cosmos SDK v0.43, Migrating the genesis file is required for compatibility.
After migrated the genesis file, please make sure that SHA256 hash of the file is valid.
```bash=
# Migrate GuanYu's genesis file to be compatible with Laozi version
cd $HOME
bandd migrate guanyu_mainnet_genesis.json --chain-id=$CHAIN_ID_LAOZI --genesis-time=$GENESIS_DATE > laozi_mainnet_genesis.json
# Check integrity
jq -S -c -M '' laozi_mainnet_genesis.json | shasum -a 256
# <TODO> -
```
We will announce a SHA256 hash to be compared with your hash, so, **Please compare them before further steps**.
### Step 1.7: Initialize Laozi Bandchain node
Initialize Laozi BandChain node and copy configuration files from GuanYu folder `.bandd` to Laozi folder `.band`.
```bash=
bandd init $MONIKER --chain-id $CHAIN_ID_LAOZI
cp $HOME/.bandd/config/priv_validator_key.json $HOME/.band/config
cp $HOME/.bandd/config/node_key.json $HOME/.band/config
cp -r $HOME/.bandd/files $HOME/.band
mv laozi_mainnet_genesis.json $HOME/.band/config/genesis.json
```
Import the exported key that we saved in a file. When importing the key, it will ask for a password to decrypt the key.
```bash=
# Only import if you have exported key
bandd keys import $WALLET_NAME $HOME/${WALLET_NAME}_privkey.txt
# Please make sure that the key has been imported successfully
bandd keys list
# If key has been import successfully, then delete the file
rm $HOME/${WALLET_NAME}_privkey.txt
```
After imported the key. Make sure that the key has been imported successfully by running the command `bandd keys list`.
### Step 1.8: Update persistence peers of your node to BandChain peers.
This can be done by editing `seeds` property in `$HOME/.band/config/config.toml`.
<a style="color:#019401" href="https://hackmd.io/10PmdqikSuCgSN2PJcD7vQ?view">GREEN</a>
<a style="color:red" href="https://hackmd.io/Xg9w5eq_T9SBHyjyju_RDw?view">RED</a>
<a style="color:#fcba03" href="https://hackmd.io/9fJIcWHUQX6x_Zc9JzuHDw?view">YELLOW</a>
### Step 1.9: Finally, start Laozi Bandchain
```bash=
sudo systemctl start bandd
```
After started, log output can be viewed with `journalctl -u bandd.service -f`. If genesis time is future, the node will wait until reach the genesis time to start the first block. Log should be displayed similar to the following log
```
... INF Starting Node service impl=Node
... INF Genesis time is in the future. Sleeping until then... genTime=<TODO>
```
If all goes well, the node daemon should have been started syncing after pass the agreed block time.
**Please wait for genesis block to begin, then resume the procedure.**
```
... INF starting ABCI with Tendermint
.
.
.
... INF committed state app_hash=... height=1 module=state num_txs=0
... INF indexed block height=1 module=txindex
```
## Step 2: Migrate Yoda
### Step 2.1: Migrate Yoda's keyring
The keyring folder of Yoda should be renamed to `keyring-test`.
```bash=
# PLEASE MAKE SURE THAT NO TRALING SLASH IN BOTH PATHS
mv $HOME/.yoda/keyring-test-band $HOME/.yoda/keyring-test
```
### Step 2.2: Update chain-id config on yoda
First let's config Yoda to use the new chain ID.
```bash=
yoda config chain-id $CHAIN_ID_LAOZI
```
### Step 2.3: Update Yoda Executor (v2.3.0)
The next is configuring a new executer for our yoda. We already set up the new executer and placed its trggier url in environment variable ```${EXECUTOR_URL}```.
```bash=
yoda config executor "rest:${EXECUTOR_URL}?timeout=10s"
```
### Step 2.4: Start Yoda service
**Noted**: Please wait for the chain to start mining the block first to do the following instructions.
```
sudo systemctl restart yoda
```
type `journalctl -u bandd.service -u yoda.service -f` to view both logs in the same console.
### Step 2.5: Send MsgActivate to reactivate yourself as an oracle provider
```bash=
bandd tx oracle activate \
--from $WALLET_NAME \
--chain-id $CHAIN_ID_LAOZI
```
Now, block explorer can be accessed [here](https://cosmoscan.io) to check your validator status.
### Step 2.6: Update service file (Optional)
1. Add this line into the last line of `[Unit]` section in `bandd.service` that make `yoda` will be started when `bandd` starting a new process.
Use this command to update `bandd.service`
```bash=
# Write bandd service file to /etc/systemd/system/bandd.service
export USERNAME=$(whoami)
sudo -E bash -c 'cat << EOF > /etc/systemd/system/bandd.service
[Unit]
Description=BandChain Node Daemon
After=network-online.target
[Service]
User=$USERNAME
ExecStart=/home/$USERNAME/go/bin/bandd start
Restart=always
RestartSec=3
LimitNOFILE=4096
[Install]
WantedBy=multi-user.target
EOF'
```
Use this command to update `yoda.service`
```bash=
# Write yoda service to /etc/systemd/system/yoda.service
export USERNAME=$(whoami)
sudo -E bash -c 'cat << EOF > /etc/systemd/system/yoda.service
[Unit]
Description=Yoda Daemon
After=network-online.target
[Service]
User=$USERNAME
ExecStart=/home/$USERNAME/go/bin/yoda run
Restart=always
RestartSec=3
LimitNOFILE=4096
ExecStartPre=/bin/sleep 10
[Install]
WantedBy=multi-user.target
EOF'
```
3. After editing those files, then run these commands to restart the services.
```bash=
sudo systemctl daemon-reload
sudo systemctl restart bandd
```
And that is all about upgrading BandChain node to the new era. Happy staking :).
## Appendix A: Recovery Procedures
In case we need to roll back to GuanYu mainnet, please follow these steps.
### Step 1: Stop current node by running the command below
```bash=
sudo systemctl stop bandd
sudo systemctl stop yoda
```
### Step 2: Revert `go` version to 1.14
```bash=
rm -r $HOME/go/bin
sudo rm -r /usr/local/go
# Install go
wget https://golang.org/dl/go1.14.15.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf go1.14.15.linux-amd64.tar.gz
echo "export PATH=\$PATH:/usr/local/go/bin:$HOME/go/bin" >> $HOME/.profile
source $HOME/.profile
```
### Step 3: Bring back `bandd` **v1.2.7a** and `yoda`
```bash=
cd bandchain/chain
git checkout v1.2.7a
make install
# PLEASE MAKE SURE THAT NO TRALING SLASH IN BOTH PATHS
mv $HOME/.yoda/keyring-test $HOME/.yoda/keyring-test-band
```
### Step 4: Update persistence peers of your node to BandChain peers.
```bash=
sed -E -i \
"s/persistent_peers = \".*\"/persistent_peers = \"${PERSISTENT_PEERS_GUANYU}\"/" \
$HOME/.bandd/config/config.toml
```
### Step 5: Restart bandd service
```bash=
sudo systemctl start bandd
```
Tail the log output with `journalctl -u bandd.service -f`. If all goes well, you should see that the node daemon has started syncing on GuanYu mainnet chain again.
```
... bandd: I[..] starting ABCI with Tendermint module=main
... bandd: I[..] Executed block module=state height=xx validTxs=0 invalidTxs=0
... bandd: I[..] Committed state module=state height=xx txs=0 appHash=...
...
```
### Step 6: Re-config & Start Yoda
Set Yoda chain ID to the old ID, then start it.
```bash=
yoda config chain-id $CHAIN_ID_GUANYU
sudo systemctl start yoda
```