> ## Documentation Index
> Fetch the complete documentation index at: https://seilabs-docs-giga-whitepaper-v2-rework.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Join the network using StateSync

> Detailed guide for using statesync to join the network

Follow this guide to join an existing network through statesync. To quickly spin up a fresh full node and join in the network, it's recommended to sync through state sync.

## State Sync

State sync allows a new node to join a network by fetching a snapshot of the application state at a recent height instead of fetching and replaying all historical blocks. This can reduce the time needed to sync with the network from days to minutes.

## Clean Up

If you are not starting a node from fresh, then you need to do some backups and clean ups.

<Info>Note: This step is not needed for fresh nodes.</Info>

Assuming your sei home directory is `$HOME/.sei`, back up `priv_validator_key.json` and `priv_validator_state.json`:

```bash theme={null}
cp $HOME/.sei/data/priv_validator_state.json $HOME/priv_validator_state.json
cp $HOME/.sei/config/priv_validator_key.json $HOME/priv_validator_key.json
```

Reset the state:

```bash theme={null}
seid tendermint unsafe-reset-all --home $HOME/.sei
```

Finally, remove the existing data and wasm folders and restore the `priv_validator_state.json`:

```bash theme={null}
rm -rf $HOME/.sei/data/*
rm -rf $HOME/.sei/wasm
cp $HOME/priv_validator_state.json $HOME/.sei/data/priv_validator_state.json
```

## Statesync script

Set up the RPC servers for primary and secondary endpoints. You can use one of these RPC endpoints for Statesyning

For `STATE_SYNC_RPC`, pick:

* mainnet (pacific-1): `https://sei-rpc.polkachu.com:443` or `https://rpc.sei-apis.com:443`
* testnet (atlantic-2): `https://sei-testnet-rpc.polkachu.com:443` or `https://rpc-testnet.sei-apis.com:443`

For `STATE_SYNC_PEER`, pick the below as `persistent-peers`:

* mainnet (pacific-1):

```bash theme={null}
3be6b24cf86a5938cce7d48f44fb6598465a9924@p2p.state-sync-0.pacific-1.seinetwork.io:26656,b21279d7092fde2e41770832a1cacc7d0051e9dc@p2p.state-sync-1.pacific-1.seinetwork.io:26656,616c05e9ba24acc89c0de630b5e3adbedaebb478@p2p.state-sync-2.pacific-1.seinetwork.io:26656
```

* testnet (atlantic-2):

```bash theme={null}
b2664ccaa84a04b67683093fefb802b172ead6d1@sei-a2-rpc.p2p.brocha.in:30612,babc3f3f7804933265ec9c40ad94f4da8e9e0017@testnet-seed.rhinostake.com:11956
```

The script sets up `trust_height` and `trust_hash` automatically. Each statesync snapshot is created at a certain block height. The script uses the best practice here and sets the trust height to be earlier than the latest snapshot block height to avoid backward verifications.

Script that you can use:

```bash title="state_sync.sh" theme={null}
#!/bin/bash

# Prompt for the State Sync RPC Endpoint and store it in STATE_SYNC_RPC
echo -n State Sync RPC Endpoint:
read STATE_SYNC_RPC
echo

# Prompt for the State Sync Peer and store it in STATE_SYNC_PEER
echo -n State Sync Peer:
read STATE_SYNC_PEER
echo

# Create a backup directory for keys
mkdir -p $HOME/key_backup

# Backup the validator key and state files
cp $HOME/.sei/config/priv_validator_key.json $HOME/key_backup
cp $HOME/.sei/data/priv_validator_state.json $HOME/key_backup

# Create a backup directory for the entire .sei configuration
mkdir -p $HOME/.sei_backup

# Copy existing config, data, and wasm directories to the backup directory
cp -r $HOME/.sei/config $HOME/.sei_backup/
cp -r $HOME/.sei/data $HOME/.sei_backup/
cp -r $HOME/.sei/wasm $HOME/.sei_backup/ 2>/dev/null || true  # ignore error if wasm doesn't exist

# Create a temp directory on the main disk  and set the path in config.toml
mkdir -p /home/ubuntu/statesync-temp
sed -i 's|temp-dir = ""|temp-dir = "/home/ubuntu/statesync-temp"|' ~/.sei/config/config.toml

# Fetch the latest block height from the State Sync RPC endpoint
LATEST_HEIGHT=$(curl -s $STATE_SYNC_RPC/block | jq -r .block.header.height)
# Calculate the trust height (rounded down to the nearest 100,000)
BLOCK_HEIGHT=$(( (LATEST_HEIGHT / 100000) * 100000 ))
# Fetch the block hash at the trust height
TRUST_HASH=$(curl -s "$STATE_SYNC_RPC/block?height=$BLOCK_HEIGHT" | jq -r .block_id.hash)

# Update the config.toml file to enable state sync with the appropriate settings
sed -i.bak -E "s|^(enable[[:space:]]+=[[:space:]]+).*$|\1true| ; \
s|^(rpc-servers[[:space:]]+=[[:space:]]+).*$|\1\"$STATE_SYNC_RPC,$STATE_SYNC_RPC\"| ; \
s|^(trust-height[[:space:]]+=[[:space:]]+).*$|\1$BLOCK_HEIGHT| ; \
s|^(trust-hash[[:space:]]+=[[:space:]]+).*$|\1\"$TRUST_HASH\"|" $HOME/.sei/config/config.toml

# Set the persistent peers in the config.toml file to the specified State Sync Peer
sed -i.bak -e "s|^persistent-peers *=.*|persistent-peers = \"$STATE_SYNC_PEER\"|" \
  $HOME/.sei/config/config.toml
```

Finally start the daemon:

```bash theme={null}
sudo systemctl start seid
```

## Troubleshooting statesync

**Q:** I can't connect to the state sync url.

**A:** Try another statesync url in that case and inform us in the [Sei Tech Chat](https://t.me/+KZdhZ1eE-G01NmZk).

**Q:** The statesync finishes, but immediately get `AppHash` errors upon regular block syncing:

**A:** Make sure that you use the latest version of the chain node when you state-sync.

**Q:** The statesync randomly stops.

**A:** State-sync can be flaky at times, just try again. Please give it around 4-5 tries. Join our [Sei Tech Chat](https://t.me/+KZdhZ1eE-G01NmZk) for help if you have any issues.
