> ## Documentation Index
> Fetch the complete documentation index at: https://seilabs-docs-evm-cookbook.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

Use state sync to start a new full node and join an existing network quickly.

## State sync

State sync lets a new node join a network without fetching and replaying all historical blocks. Instead, the node fetches a snapshot of the application state at a recent height. This can reduce the time to sync with the network from days to minutes.

<Warning>Set `ss-backend = "pebbledb"` before you start state sync. This is especially important if you rebuild a node that previously used RocksDB. RocksDB support for the SeiDB state store will be removed, and there is no published target release for the removal. For the full migration checklist, see [Move off RocksDB](/node/node-operators#move-off-rocksdb).</Warning>

<Danger>Do not use state sync to migrate an archive node. State sync starts from a recent height and does not preserve the earlier state-store versions. Follow the archive-node guidance in [Move off RocksDB](/node/node-operators#move-off-rocksdb).</Danger>

## Clean up

If the node is not new, back up and clean up the existing node first.

<Info>Skip this step on a new node.</Info>

If 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 `priv_validator_state.json`. Before you remove them, print the state-store section from `app.toml`:

```bash theme={null}
sed -n '/^\[state-store\]/,/^\[.*\]/p' $HOME/.sei/config/app.toml
```

If `ss-db-directory` or `evm-ss-db-directory` points outside
`$HOME/.sei/data`, back up what you need from that directory. Then clear the
old state-store data there too. The commands below do not remove custom
directories. A PebbleDB state sync must not reuse RocksDB data.

```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 the primary and secondary endpoints. You can use one of these RPC endpoints for state sync.

For `STATE_SYNC_RPC`, use one of these endpoints:

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

For `STATE_SYNC_PEER`, use these values as `persistent-peers`:

* Sei 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
```

* Sei Testnet (atlantic-2):

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

The script sets `trust_height` and `trust_hash` automatically. Each state sync snapshot is taken at a specific block height. The script follows best practice and sets the trust height earlier than the latest snapshot block height. This avoids backward verification.

Use this script:

```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

# Use PebbleDB for the SeiDB state store
sed -i.bak -E '/^\[state-store\]/,/^\[.*\]/ s|^[#[:space:]]*(ss-backend[[:space:]]*=[[:space:]]*).*$|\1"pebbledb"|' \
  $HOME/.sei/config/app.toml

# Print the state-store section so you can verify the backend
sed -n '/^\[state-store\]/,/^\[.*\]/p' $HOME/.sei/config/app.toml
```

Confirm that the script output includes `ss-backend = "pebbledb"`. If the key
is missing, add it directly below `[state-store]` before you start `seid`.

Finally, start the daemon:

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

## Troubleshooting statesync

**Q:** I cannot connect to the state sync URL.

**A:** Try a different state sync URL, and report the problem in the [Sei Tech Chat](https://t.me/+KZdhZ1eE-G01NmZk).

**Q:** State sync finishes, but I get `AppHash` errors as soon as regular block sync starts.

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

**Q:** State sync stops at random.

**A:** State sync is sometimes unreliable. If it stops, start it again. It can take approximately 4 to 5 attempts. If you still have problems, ask for help in the [Sei Tech Chat](https://t.me/+KZdhZ1eE-G01NmZk).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.